OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 19 | Rev 57 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.supplychain.receipt.component;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.common.component.TransfertBaseSQLComponent;
18
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
19
import org.openconcerto.erp.core.common.ui.DeviseField;
20
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
21
import org.openconcerto.erp.core.supplychain.receipt.element.BonReceptionSQLElement;
22
import org.openconcerto.erp.core.supplychain.receipt.ui.BonReceptionItemTable;
23
import org.openconcerto.erp.core.supplychain.stock.element.MouvementStockSQLElement;
24
import org.openconcerto.erp.preferences.DefaultNXProps;
25
import org.openconcerto.sql.Configuration;
26
import org.openconcerto.sql.element.SQLElement;
27
import org.openconcerto.sql.model.SQLField;
28
import org.openconcerto.sql.model.SQLInjector;
29
import org.openconcerto.sql.model.SQLRow;
30
import org.openconcerto.sql.model.SQLRowAccessor;
31
import org.openconcerto.sql.model.SQLRowValues;
32
import org.openconcerto.sql.model.SQLSelect;
33
import org.openconcerto.sql.model.SQLTable;
34
import org.openconcerto.sql.model.Where;
35
import org.openconcerto.sql.sqlobject.ElementComboBox;
36
import org.openconcerto.sql.sqlobject.JUniqueTextField;
37
import org.openconcerto.sql.view.EditFrame;
38
import org.openconcerto.sql.view.list.RowValuesTableModel;
39
import org.openconcerto.ui.DefaultGridBagConstraints;
25 ilm 40
import org.openconcerto.ui.FormLayouter;
18 ilm 41
import org.openconcerto.ui.JDate;
42
import org.openconcerto.ui.JLabelBold;
43
import org.openconcerto.ui.TitledSeparator;
44
import org.openconcerto.ui.preferences.DefaultProps;
45
import org.openconcerto.utils.ExceptionHandler;
46
import org.openconcerto.utils.GestionDevise;
47
 
48
import java.awt.Color;
49
import java.awt.GridBagConstraints;
50
import java.awt.GridBagLayout;
51
import java.awt.Insets;
52
import java.sql.SQLException;
19 ilm 53
import java.util.Arrays;
18 ilm 54
import java.util.List;
55
 
56
import javax.swing.JLabel;
57
import javax.swing.JPanel;
58
import javax.swing.JScrollPane;
59
import javax.swing.JSeparator;
60
import javax.swing.JTextArea;
61
import javax.swing.JTextField;
62
import javax.swing.SwingConstants;
63
import javax.swing.SwingUtilities;
64
import javax.swing.event.TableModelEvent;
65
import javax.swing.event.TableModelListener;
66
 
67
import org.apache.commons.dbutils.handlers.ArrayListHandler;
68
 
69
public class BonReceptionSQLComponent extends TransfertBaseSQLComponent {
70
    private BonReceptionItemTable tableBonItem;
71
    private ElementComboBox selectCommande;
72
    private ElementComboBox fournisseur;
73
    private JUniqueTextField textNumeroUnique;
74
    private final SQLTable tableNum = getTable().getBase().getTable("NUMEROTATION_AUTO");
75
    private final DeviseField textTotalHT = new DeviseField();
76
    private final DeviseField textTotalTVA = new DeviseField();
77
    private final DeviseField textTotalTTC = new DeviseField();
78
    private final JTextField textPoidsTotal = new JTextField(6);
79
    private final JTextField textReference = new JTextField(25);
80
 
81
    public BonReceptionSQLComponent() {
82
        super(Configuration.getInstance().getDirectory().getElement("BON_RECEPTION"));
83
    }
84
 
85
    @Override
86
    protected SQLRowValues createDefaults() {
87
        this.tableBonItem.getModel().clearRows();
88
        return super.createDefaults();
89
    }
90
 
91
    public void addViews() {
92
        this.setLayout(new GridBagLayout());
93
        final GridBagConstraints c = new DefaultGridBagConstraints();
94
 
25 ilm 95
        // Champ Module
96
        c.gridx = 0;
97
        c.gridy++;
98
        c.gridwidth = GridBagConstraints.REMAINDER;
99
        final JPanel addP = new JPanel();
100
        this.setAdditionalFieldsPanel(new FormLayouter(addP, 1));
101
        this.add(addP, c);
102
 
103
        c.gridy++;
104
        c.gridwidth = 1;
105
 
18 ilm 106
        this.textTotalHT.setOpaque(false);
107
        this.textTotalTVA.setOpaque(false);
108
        this.textTotalTTC.setOpaque(false);
109
 
110
        this.selectCommande = new ElementComboBox();
111
        // Numero
112
        JLabel labelNum = new JLabel(getLabelFor("NUMERO"));
113
        labelNum.setHorizontalAlignment(SwingConstants.RIGHT);
114
        this.add(labelNum, c);
115
 
116
        this.textNumeroUnique = new JUniqueTextField(16);
117
        c.gridx++;
118
        c.weightx = 1;
119
        c.weighty = 0;
120
        c.fill = GridBagConstraints.NONE;
121
        DefaultGridBagConstraints.lockMinimumSize(this.textNumeroUnique);
122
        this.add(this.textNumeroUnique, c);
123
 
124
        // Date
125
        JLabel labelDate = new JLabel(getLabelFor("DATE"));
126
        labelDate.setHorizontalAlignment(SwingConstants.RIGHT);
127
        c.fill = GridBagConstraints.HORIZONTAL;
128
        c.gridx++;
129
        c.weightx = 0;
130
        this.add(labelDate, c);
131
 
132
        JDate date = new JDate(true);
133
        c.gridx++;
134
        c.weightx = 0;
135
        c.weighty = 0;
136
        this.add(date, c);
137
        // Reference
138
        c.gridy++;
139
        c.gridx = 0;
140
        final JLabel labelNom = new JLabel(getLabelFor("NOM"));
141
        labelNom.setHorizontalAlignment(SwingConstants.RIGHT);
142
        this.add(labelNom, c);
143
        c.gridx++;
144
        c.fill = GridBagConstraints.HORIZONTAL;
145
        DefaultGridBagConstraints.lockMinimumSize(this.textReference);
146
        this.add(this.textReference, c);
147
        // Fournisseur
148
        JLabel labelFournisseur = new JLabel(getLabelFor("ID_FOURNISSEUR"));
149
        labelFournisseur.setHorizontalAlignment(SwingConstants.RIGHT);
150
        c.gridx = 0;
151
        c.gridy++;
152
        c.weightx = 0;
153
        c.weighty = 0;
154
        this.add(labelFournisseur, c);
155
 
156
        this.fournisseur = new ElementComboBox();
157
        c.gridx++;
158
        c.weightx = 0;
159
        c.weighty = 0;
160
        c.fill = GridBagConstraints.NONE;
161
        this.add(this.fournisseur, c);
162
 
19 ilm 163
        // Devise
164
        c.gridx = 0;
165
        c.gridy++;
166
        c.weightx = 0;
167
        c.fill = GridBagConstraints.HORIZONTAL;
168
        this.add(new JLabel(getLabelFor("ID_DEVISE"), SwingConstants.RIGHT), c);
169
 
170
        final ElementComboBox boxDevise = new ElementComboBox();
171
        c.gridx = GridBagConstraints.RELATIVE;
172
        c.gridwidth = 1;
173
        c.weightx = 1;
174
        c.weighty = 0;
175
        c.fill = GridBagConstraints.NONE;
176
        this.add(boxDevise, c);
177
        this.addView(boxDevise, "ID_DEVISE");
178
 
18 ilm 179
        // Element du bon
180
        this.tableBonItem = new BonReceptionItemTable();
181
        c.gridx = 0;
182
        c.gridy++;
183
        c.weightx = 1;
184
        c.weighty = 1;
185
        c.gridwidth = GridBagConstraints.REMAINDER;
186
        c.fill = GridBagConstraints.BOTH;
187
        this.add(this.tableBonItem, c);
188
        c.anchor = GridBagConstraints.EAST;
189
        // Totaux
190
        reconfigure(this.textTotalHT);
191
        reconfigure(this.textTotalTVA);
192
        reconfigure(this.textTotalTTC);
193
 
194
        // Poids Total
195
        c.gridy++;
196
        c.gridx = 1;
197
        c.weightx = 0;
198
        c.weighty = 0;
199
        c.anchor = GridBagConstraints.EAST;
200
        c.gridwidth = 1;
201
        c.fill = GridBagConstraints.NONE;
202
 
203
        DefaultProps props = DefaultNXProps.getInstance();
204
        Boolean b = props.getBooleanValue("ArticleShowPoids");
205
        if (b) {
206
            JPanel panelPoids = new JPanel(new GridBagLayout());
207
            GridBagConstraints c2 = new DefaultGridBagConstraints();
208
            c2.fill = GridBagConstraints.NONE;
209
 
210
            panelPoids.add(new JLabel(getLabelFor("TOTAL_POIDS")), c2);
211
            // Necessaire pour ne pas avoir de saut de layout
212
            DefaultGridBagConstraints.lockMinimumSize(this.textPoidsTotal);
213
            this.textPoidsTotal.setEnabled(false);
214
            this.textPoidsTotal.setHorizontalAlignment(JTextField.RIGHT);
215
            this.textPoidsTotal.setDisabledTextColor(Color.BLACK);
216
            c2.gridx++;
217
            c2.weightx = 1;
218
            c2.fill = GridBagConstraints.HORIZONTAL;
219
            panelPoids.add(this.textPoidsTotal, c2);
220
            this.add(panelPoids, c);
221
 
222
        }
223
 
224
        c.gridx = 2;
225
        c.gridwidth = GridBagConstraints.REMAINDER;
226
        c.weightx = 0;
227
        c.weighty = 0;
228
        c.anchor = GridBagConstraints.EAST;
229
        c.fill = GridBagConstraints.HORIZONTAL;
230
 
231
        final GridBagConstraints cTotalPan = new DefaultGridBagConstraints();
232
 
233
        JPanel panelTotalHT = new JPanel();
234
        panelTotalHT.setLayout(new GridBagLayout());
235
        cTotalPan.gridx = 0;
236
        cTotalPan.weightx = 0;
237
        cTotalPan.fill = GridBagConstraints.HORIZONTAL;
238
        cTotalPan.anchor = GridBagConstraints.WEST;
239
        final JLabelBold labelTotalHT = new JLabelBold(getLabelFor("TOTAL_HT"));
240
        panelTotalHT.add(labelTotalHT, cTotalPan);
241
        cTotalPan.anchor = GridBagConstraints.EAST;
242
        cTotalPan.gridx++;
243
        cTotalPan.weightx = 1;
244
        this.textTotalHT.setFont(labelTotalHT.getFont());
245
        DefaultGridBagConstraints.lockMinimumSize(this.textTotalHT);
246
        panelTotalHT.add(this.textTotalHT, cTotalPan);
247
        this.add(panelTotalHT, c);
248
 
249
        JPanel panelTotalTVA = new JPanel();
250
        panelTotalTVA.setLayout(new GridBagLayout());
251
        cTotalPan.gridx = 0;
252
        cTotalPan.weightx = 0;
253
        cTotalPan.anchor = GridBagConstraints.WEST;
254
        cTotalPan.fill = GridBagConstraints.HORIZONTAL;
255
        panelTotalTVA.add(new JLabelBold(getLabelFor("TOTAL_TVA")), cTotalPan);
256
        cTotalPan.anchor = GridBagConstraints.EAST;
257
        cTotalPan.gridx++;
258
        cTotalPan.weightx = 1;
259
        DefaultGridBagConstraints.lockMinimumSize(this.textTotalTVA);
260
        panelTotalTVA.add(this.textTotalTVA, cTotalPan);
261
        c.gridy++;
262
        c.fill = GridBagConstraints.HORIZONTAL;
263
        this.add(panelTotalTVA, c);
264
 
265
        JPanel panelTotalTTC = new JPanel();
266
        panelTotalTTC.setLayout(new GridBagLayout());
267
        cTotalPan.gridx = 0;
268
        cTotalPan.anchor = GridBagConstraints.WEST;
269
        cTotalPan.gridwidth = GridBagConstraints.REMAINDER;
270
        cTotalPan.fill = GridBagConstraints.BOTH;
271
        cTotalPan.weightx = 1;
272
        panelTotalTTC.add(new JSeparator(), cTotalPan);
273
        cTotalPan.gridwidth = 1;
274
        cTotalPan.fill = GridBagConstraints.NONE;
275
        cTotalPan.weightx = 0;
276
        cTotalPan.gridy++;
277
        panelTotalTTC.add(new JLabelBold(getLabelFor("TOTAL_TTC")), cTotalPan);
278
        cTotalPan.anchor = GridBagConstraints.EAST;
279
        cTotalPan.gridx++;
280
        this.textTotalTTC.setFont(labelTotalHT.getFont());
281
        DefaultGridBagConstraints.lockMinimumSize(this.textTotalTTC);
282
        panelTotalTTC.add(this.textTotalTTC, cTotalPan);
283
        c.gridy++;
284
        // probleme de tremblement vertical
285
        this.add(panelTotalTTC, c);
286
        c.anchor = GridBagConstraints.WEST;
287
 
288
        /*******************************************************************************************
289
         * * INFORMATIONS COMPLEMENTAIRES
290
         ******************************************************************************************/
291
        c.gridwidth = GridBagConstraints.REMAINDER;
292
        c.weightx = 1;
293
        c.fill = GridBagConstraints.HORIZONTAL;
294
        c.gridx = 0;
295
        c.gridy++;
296
        TitledSeparator sep = new TitledSeparator("Informations complémentaires");
297
        c.insets = new Insets(10, 2, 1, 2);
298
        this.add(sep, c);
299
        c.insets = new Insets(2, 2, 1, 2);
300
 
301
        c.gridx = 0;
302
        c.gridy++;
303
        c.gridheight = 1;
304
        c.gridwidth = GridBagConstraints.REMAINDER;
305
        c.weightx = 1;
306
        c.weighty = 0;
307
        c.fill = GridBagConstraints.BOTH;
308
        final JTextArea textInfos = new JTextArea(4, 4);
309
        JScrollPane scrollPane = new JScrollPane(textInfos);
310
        DefaultGridBagConstraints.lockMinimumSize(scrollPane);
311
 
312
        this.add(textInfos, c);
313
 
314
        this.addRequiredSQLObject(date, "DATE");
315
        this.addSQLObject(textInfos, "INFOS");
316
        this.addSQLObject(this.textReference, "NOM");
317
        this.addSQLObject(this.selectCommande, "ID_COMMANDE");
318
        this.addRequiredSQLObject(this.textNumeroUnique, "NUMERO");
319
        this.addSQLObject(this.textPoidsTotal, "TOTAL_POIDS");
320
        this.addRequiredSQLObject(this.textTotalHT, "TOTAL_HT");
321
        this.addRequiredSQLObject(this.textTotalTVA, "TOTAL_TVA");
322
        this.addRequiredSQLObject(this.textTotalTTC, "TOTAL_TTC");
323
        this.addRequiredSQLObject(this.fournisseur, "ID_FOURNISSEUR");
324
 
325
        this.textNumeroUnique.setText(NumerotationAutoSQLElement.getNextNumero(BonReceptionSQLElement.class));
326
 
327
        // Listeners
328
        this.tableBonItem.getModel().addTableModelListener(new TableModelListener() {
329
            public void tableChanged(TableModelEvent e) {
330
 
331
                int columnIndexHT = BonReceptionSQLComponent.this.tableBonItem.getModel().getColumnIndexForElement(BonReceptionSQLComponent.this.tableBonItem.getPrixTotalHTElement());
332
                int columnIndexTTC = BonReceptionSQLComponent.this.tableBonItem.getModel().getColumnIndexForElement(BonReceptionSQLComponent.this.tableBonItem.getPrixTotalTTCElement());
333
                int columnIndexPoids = BonReceptionSQLComponent.this.tableBonItem.getModel().getColumnIndexForElement(BonReceptionSQLComponent.this.tableBonItem.getPoidsTotalElement());
334
 
335
                if (e.getColumn() == TableModelEvent.ALL_COLUMNS || e.getColumn() == columnIndexHT || e.getColumn() == columnIndexTTC) {
336
                    updateTotal();
337
                }
338
                if (e.getColumn() == TableModelEvent.ALL_COLUMNS || e.getColumn() == columnIndexPoids) {
339
                    BonReceptionSQLComponent.this.textPoidsTotal.setText(String.valueOf(Math.round(BonReceptionSQLComponent.this.tableBonItem.getPoidsTotal() * 1000) / 1000.0));
340
                }
341
            }
342
        });
343
 
344
        // Lock UI
345
        DefaultGridBagConstraints.lockMinimumSize(this.fournisseur);
346
 
347
    }
348
 
349
    private void reconfigure(JTextField field) {
350
        field.setEnabled(false);
351
        field.setHorizontalAlignment(JTextField.RIGHT);
352
        field.setDisabledTextColor(Color.BLACK);
353
        field.setBorder(null);
354
    }
355
 
356
    public int insert(SQLRow order) {
357
 
358
        int idBon = getSelectedID();
359
        // on verifie qu'un bon du meme numero n'a pas été inséré entre temps
360
        if (this.textNumeroUnique.checkValidation()) {
361
            idBon = super.insert(order);
362
            this.tableBonItem.updateField("ID_BON_RECEPTION", idBon);
363
 
364
            // incrémentation du numéro auto
365
            if (NumerotationAutoSQLElement.getNextNumero(BonReceptionSQLElement.class).equalsIgnoreCase(this.textNumeroUnique.getText().trim())) {
366
                SQLRowValues rowVals = new SQLRowValues(this.tableNum);
367
                int val = this.tableNum.getRow(2).getInt("BON_R_START");
368
                val++;
369
                rowVals.put("BON_R_START", new Integer(val));
370
 
371
                try {
372
                    rowVals.update(2);
373
                } catch (SQLException e) {
374
                    e.printStackTrace();
375
                }
376
            }
377
            calculPHaPondere(idBon);
378
            updateStock(idBon);
379
        } else {
380
            ExceptionHandler.handle("Impossible d'ajouter, numéro de bon de livraison existant.");
381
            Object root = SwingUtilities.getRoot(this);
382
            if (root instanceof EditFrame) {
383
                EditFrame frame = (EditFrame) root;
384
                frame.getPanel().setAlwaysVisible(true);
385
            }
386
        }
387
 
388
        return idBon;
389
    }
390
 
391
    @Override
392
    public void select(SQLRowAccessor r) {
393
        if (r != null) {
394
            this.textNumeroUnique.setIdSelected(r.getID());
395
        }
396
        super.select(r);
397
        if (r != null) {
398
            this.tableBonItem.insertFrom("ID_BON_RECEPTION", r.getID());
399
        }
400
    }
401
 
402
    @Override
403
    public void update() {
404
 
405
        if (!this.textNumeroUnique.checkValidation()) {
406
            ExceptionHandler.handle("Impossible d'ajouter, numéro de bon de livraison existant.");
407
            Object root = SwingUtilities.getRoot(this);
408
            if (root instanceof EditFrame) {
409
                EditFrame frame = (EditFrame) root;
410
                frame.getPanel().setAlwaysVisible(true);
411
            }
412
            return;
413
        } else {
414
 
415
            // On efface les anciens mouvements de stocks
416
            SQLRow row = getTable().getRow(getSelectedID());
417
            SQLElement eltMvtStock = Configuration.getInstance().getDirectory().getElement("MOUVEMENT_STOCK");
418
            SQLSelect sel = new SQLSelect(eltMvtStock.getTable().getBase());
419
            sel.addSelect(eltMvtStock.getTable().getField("ID"));
420
            Where w = new Where(eltMvtStock.getTable().getField("IDSOURCE"), "=", row.getID());
421
            Where w2 = new Where(eltMvtStock.getTable().getField("SOURCE"), "=", getTable().getName());
422
            sel.setWhere(w.and(w2));
423
 
424
            List l = (List) eltMvtStock.getTable().getBase().getDataSource().execute(sel.asString(), new ArrayListHandler());
425
            if (l != null) {
426
                for (int i = 0; i < l.size(); i++) {
427
                    Object[] tmp = (Object[]) l.get(i);
428
                    try {
429
                        eltMvtStock.archive(((Number) tmp[0]).intValue());
430
                    } catch (SQLException e) {
431
                        e.printStackTrace();
432
                    }
433
                }
434
            }
435
 
436
            // Mise à jour de l'élément
437
            super.update();
438
            this.tableBonItem.updateField("ID_BON_RECEPTION", getSelectedID());
439
 
440
            // Mise à jour du stock
441
            updateStock(getSelectedID());
442
        }
443
    }
444
 
445
    private void updateTotal() {
446
        RowValuesTableModel model = this.tableBonItem.getModel();
447
 
448
        long totalHT = 0;
449
        long totalTTC = 0;
450
        int columnIndexHT = model.getColumnIndexForElement(this.tableBonItem.getPrixTotalHTElement());
451
        int columnIndexTTC = model.getColumnIndexForElement(this.tableBonItem.getPrixTotalTTCElement());
452
 
453
        // columnIndexHT = model.getColumnIndexForElement(getTable().get);
454
        for (int i = 0; i < model.getRowCount(); i++) {
455
            Number nHT = (Number) model.getValueAt(i, columnIndexHT);
456
            totalHT += nHT.longValue();
457
 
458
            Number nTTC = (Number) model.getValueAt(i, columnIndexTTC);
459
            totalTTC += nTTC.longValue();
460
        }
461
 
462
        this.textTotalHT.setText(GestionDevise.currencyToString(totalHT));
463
        this.textTotalTVA.setText(GestionDevise.currencyToString(totalTTC - totalHT));
464
        this.textTotalTTC.setText(GestionDevise.currencyToString(totalTTC));
465
    }
466
 
467
    /**
468
     * Création d'un bon de réception à partir d'une commande
469
     *
470
     * @param idCommande
471
     *
472
     */
473
    public void loadCommande(int idCommande) {
474
 
475
        SQLElement commande = Configuration.getInstance().getDirectory().getElement("COMMANDE");
476
        SQLElement commandeElt = Configuration.getInstance().getDirectory().getElement("COMMANDE_ELEMENT");
477
 
478
        if (idCommande > 1) {
479
            SQLInjector injector = SQLInjector.getInjector(commande.getTable(), this.getTable());
480
            this.select(injector.createRowValuesFrom(idCommande));
481
        }
482
 
483
        loadItem(this.tableBonItem, commande, idCommande, commandeElt);
484
    }
485
 
486
    /**
487
     * Calcul du prix d'achat pondéré pour chacun des articles du bon de reception
488
     *
489
     * @param id id du bon de reception
490
     */
491
    private void calculPHaPondere(int id) {
492
        SQLTable sqlTableArticle = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("ARTICLE");
493
        SQLElement eltArticle = Configuration.getInstance().getDirectory().getElement(sqlTableArticle);
494
        SQLElement eltStock = Configuration.getInstance().getDirectory().getElement("STOCK");
495
        SQLRow row = getTable().getRow(id);
496
 
497
        // On récupére les articles qui composent la facture
498
        SQLTable sqlTableBonElt = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("BON_RECEPTION_ELEMENT");
499
        List<SQLRow> elts = row.getReferentRows(sqlTableBonElt);
500
 
501
        for (SQLRow rowEltBon : elts) {
502
 
503
            SQLRowValues rowVals = rowEltBon.createUpdateRow();
504
 
505
            // recupere l'ancien prix d'achat
506
            int idArticle = ReferenceArticleSQLElement.getIdForCNM(rowVals, false);
507
            if (idArticle > 1) {
508
                // Prix d'achat de l'article à l'origine
509
                SQLRow rowArticle = eltArticle.getTable().getRow(idArticle);
510
                Long prixHA = (Long) rowArticle.getObject("PRIX_METRIQUE_HA_1");
511
 
512
                // Quantité en stock
513
                int idStock = rowArticle.getInt("ID_STOCK");
514
                SQLRow rowStock = eltStock.getTable().getRow(idStock);
515
                int qteStock = rowStock.getInt("QTE_REEL");
516
                if (prixHA != null && qteStock > 0) {
517
                    int qteRecue = rowEltBon.getInt("QTE");
518
                    Long prixHACmd = (Long) rowEltBon.getObject("PRIX_METRIQUE_HA_1");
519
                    if (qteRecue > 0 && prixHACmd != null) {
520
                        long prixHaPond = (qteRecue * prixHACmd + qteStock * prixHA) / (qteRecue + qteStock);
521
                        SQLRowValues rowValsArticle = rowArticle.createEmptyUpdateRow();
522
                        rowValsArticle.put("PRIX_METRIQUE_HA_1", Long.valueOf(prixHaPond));
523
                        try {
524
                            rowValsArticle.commit();
525
                        } catch (SQLException e) {
526
                            // TODO Auto-generated catch block
527
                            e.printStackTrace();
528
                        }
529
                    }
530
                }
531
            }
532
 
533
        }
534
    }
535
 
536
    /**
537
     * Mise à jour des stocks pour chaque article composant du bon
538
     */
539
    private void updateStock(int id) {
540
 
541
        SQLTable sqlTableArticle = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("ARTICLE");
542
        SQLTable sqlTableBonElt = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("BON_RECEPTION_ELEMENT");
543
        SQLElement eltArticle = Configuration.getInstance().getDirectory().getElement(sqlTableArticle);
544
        SQLRow row = getTable().getRow(id);
545
 
546
        // On récupére les articles qui composent la facture
547
        List<SQLRow> elts = row.getReferentRows(sqlTableBonElt);
548
 
549
        for (SQLRow rowElt : elts) {
550
 
551
            // on récupére l'article qui lui correspond
552
            SQLRowValues rowArticle = new SQLRowValues(eltArticle.getTable());
553
            for (SQLField field : eltArticle.getTable().getFields()) {
554
                if (rowElt.getTable().getFieldsName().contains(field.getName())) {
555
                    rowArticle.put(field.getName(), rowElt.getObject(field.getName()));
556
                }
557
            }
558
            // rowArticle.loadAllSafe(rowEltFact);
559
            int idArticle = ReferenceArticleSQLElement.getIdForCNM(rowArticle, true);
560
 
561
            // on crée un mouvement de stock pour chacun des articles
562
            SQLElement eltMvtStock = Configuration.getInstance().getDirectory().getElement("MOUVEMENT_STOCK");
563
            SQLRowValues rowVals = new SQLRowValues(eltMvtStock.getTable());
564
            rowVals.put("QTE", rowElt.getInt("QTE"));
565
            rowVals.put("NOM", "Bon de réception N°" + row.getString("NUMERO"));
566
            rowVals.put("IDSOURCE", id);
567
            rowVals.put("SOURCE", getTable().getName());
568
            rowVals.put("ID_ARTICLE", idArticle);
569
            rowVals.put("DATE", row.getObject("DATE"));
570
            try {
571
                SQLRow rowInsert = rowVals.insert();
19 ilm 572
                MouvementStockSQLElement.updateStock(Arrays.asList(rowInsert.getID()));
18 ilm 573
            } catch (SQLException e) {
574
                e.printStackTrace();
575
            }
576
 
577
        }
578
 
579
    }
580
}