OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | 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.sales.product.element;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
174 ilm 17
import org.openconcerto.erp.config.update.Updater_1_5;
18 ilm 18
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
142 ilm 19
import org.openconcerto.erp.core.edm.AttachmentAction;
83 ilm 20
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
156 ilm 21
import org.openconcerto.erp.core.reports.history.ui.HistoriqueArticleFrame;
94 ilm 22
import org.openconcerto.erp.core.sales.product.action.InventairePanel;
18 ilm 23
import org.openconcerto.erp.core.sales.product.component.ReferenceArticleSQLComponent;
156 ilm 24
import org.openconcerto.erp.core.supplychain.stock.element.DepotStockSQLElement;
83 ilm 25
import org.openconcerto.erp.generationDoc.gestcomm.FicheArticleXmlSheet;
26
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
18 ilm 27
import org.openconcerto.erp.preferences.DefaultNXProps;
73 ilm 28
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
18 ilm 29
import org.openconcerto.sql.Configuration;
30
import org.openconcerto.sql.element.SQLComponent;
31
import org.openconcerto.sql.element.SQLElement;
94 ilm 32
import org.openconcerto.sql.model.FieldPath;
18 ilm 33
import org.openconcerto.sql.model.SQLRow;
94 ilm 34
import org.openconcerto.sql.model.SQLRowAccessor;
83 ilm 35
import org.openconcerto.sql.model.SQLRowListRSH;
18 ilm 36
import org.openconcerto.sql.model.SQLRowValues;
37
import org.openconcerto.sql.model.SQLSelect;
38
import org.openconcerto.sql.model.SQLTable;
39
import org.openconcerto.sql.model.Where;
94 ilm 40
import org.openconcerto.sql.model.graph.Path;
73 ilm 41
import org.openconcerto.sql.preferences.SQLPreferences;
156 ilm 42
import org.openconcerto.sql.request.ComboSQLRequest;
90 ilm 43
import org.openconcerto.sql.view.EditFrame;
44
import org.openconcerto.sql.view.EditPanel.EditMode;
94 ilm 45
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
90 ilm 46
import org.openconcerto.sql.view.list.IListe;
47
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
48
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
83 ilm 49
import org.openconcerto.sql.view.list.SQLTableModelColumn;
142 ilm 50
import org.openconcerto.sql.view.list.SQLTableModelSource;
90 ilm 51
import org.openconcerto.ui.FrameUtil;
156 ilm 52
import org.openconcerto.ui.PanelFrame;
94 ilm 53
import org.openconcerto.utils.CollectionUtils;
90 ilm 54
import org.openconcerto.utils.DecimalUtils;
55
import org.openconcerto.utils.ListMap;
18 ilm 56
 
90 ilm 57
import java.awt.event.ActionEvent;
67 ilm 58
import java.math.BigDecimal;
18 ilm 59
import java.sql.SQLException;
60
import java.util.ArrayList;
94 ilm 61
import java.util.Collection;
156 ilm 62
import java.util.HashMap;
18 ilm 63
import java.util.List;
156 ilm 64
import java.util.Map;
94 ilm 65
import java.util.Set;
18 ilm 66
 
90 ilm 67
import javax.swing.AbstractAction;
68
 
18 ilm 69
import org.apache.commons.dbutils.handlers.ArrayListHandler;
70
 
71
public class ReferenceArticleSQLElement extends ComptaSQLConfElement {
72
    public static final int AU_METRE_LONGUEUR = 2;
73
    public static final int AU_METRE_CARRE = 3;
74
    public static final int AU_POID_METRECARRE = 4;
75
    public static final int A_LA_PIECE = 5;
76
    public static final int AU_METRE_LARGEUR = 6;
77
    private static final int PRIX_HA = 1;
78
    private static final int PRIX_VT = 2;
156 ilm 79
    protected PredicateRowAction stock;
18 ilm 80
 
94 ilm 81
    public static final String[] CONDITIONS = new String[] { "CFR", "CIF", "CPT", "DAT", "DDP", "DDU", "EXW", "FCA", "FOB" };
82
 
18 ilm 83
    public ReferenceArticleSQLElement() {
84
        super("ARTICLE", "un article", "articles");
83 ilm 85
 
86
        getRowActions().addAll(new MouseSheetXmlListeListener(FicheArticleXmlSheet.class).getRowActions());
94 ilm 87
        this.stock = new PredicateRowAction(new AbstractAction("Mettre à jour les stocks") {
88
 
89
            @Override
90
            public void actionPerformed(ActionEvent e) {
91
 
156 ilm 92
                final List<SQLRowValues> selectedRows = IListe.get(e).getSelectedRows();
93
                List<SQLRowAccessor> l = new ArrayList<SQLRowAccessor>();
94
                for (SQLRowValues sqlRowValues : selectedRows) {
95
                    l.add(sqlRowValues.asRow().getForeign("ID_STOCK"));
96
                }
97
 
98
                PanelFrame p = new PanelFrame(new InventairePanel(IListe.get(e), l), "Mise à jour des stocks");
94 ilm 99
                FrameUtil.show(p);
100
 
101
            }
102
        }, true, false);
103
        stock.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
104
        getRowActions().add(stock);
105
 
156 ilm 106
        PredicateRowAction history = new PredicateRowAction(new AbstractAction("Historique") {
107
 
108
            @Override
109
            public void actionPerformed(ActionEvent e) {
110
                HistoriqueArticleFrame frame = new HistoriqueArticleFrame(ReferenceArticleSQLElement.this);
111
                frame.selectId(IListe.get(e).getSelectedId());
112
                frame.setVisible(true);
113
            }
114
        }, false, true);
115
        history.setPredicate(IListeEvent.getSingleSelectionPredicate());
116
        getRowActions().add(history);
117
 
90 ilm 118
        PredicateRowAction clone = new PredicateRowAction(new AbstractAction("Dupliquer") {
83 ilm 119
 
90 ilm 120
            @Override
121
            public void actionPerformed(ActionEvent e) {
122
 
123
                SQLRowValues rowVals = ReferenceArticleSQLElement.this.createCopy(IListe.get(e).getSelectedId());
124
                ListMap<SQLTable, SQLRow> children = ReferenceArticleSQLElement.this.getChildrenRows(IListe.get(e).getSelectedRow().asRow());
125
                for (SQLRow child : children.allValues()) {
126
                    Configuration.getInstance().getDirectory().getElement(child.getTable()).createCopy(child, false, rowVals);
127
                }
128
                EditFrame f = new EditFrame(createComponent(), EditMode.CREATION);
129
                f.getSQLComponent().select(rowVals);
130
                FrameUtil.show(f);
131
 
132
            }
133
        }, true, false);
134
        clone.setPredicate(IListeEvent.getSingleSelectionPredicate());
135
        getRowActions().add(clone);
142 ilm 136
 
137
        PredicateRowAction actionAttachment = new PredicateRowAction(new AttachmentAction().getAction(), true);
138
        actionAttachment.setPredicate(IListeEvent.getSingleSelectionPredicate());
156 ilm 139
 
142 ilm 140
        getRowActions().add(actionAttachment);
18 ilm 141
    }
142
 
83 ilm 143
    @Override
142 ilm 144
    protected synchronized void _initTableSource(final SQLTableModelSource source) {
145
        super._initTableSource(source);
93 ilm 146
        source.init();
147
        final SQLTableModelColumn pvHA1Col = source.getColumn(getTable().getField("PRIX_METRIQUE_HA_1"));
148
        if (pvHA1Col != null) {
149
            pvHA1Col.setRenderer(CURRENCY_RENDERER);
150
        }
83 ilm 151
 
93 ilm 152
        final SQLTableModelColumn pvHT1Col = source.getColumn(getTable().getField("PRIX_METRIQUE_VT_1"));
153
        if (pvHT1Col != null) {
154
            pvHT1Col.setRenderer(CURRENCY_RENDERER);
155
        }
156
 
83 ilm 157
        final SQLTableModelColumn pvHTCol = source.getColumn(getTable().getField("PV_HT"));
158
        if (pvHTCol != null) {
159
            pvHTCol.setRenderer(CURRENCY_RENDERER);
160
        }
161
        final SQLTableModelColumn pvTTCCol = source.getColumn(getTable().getField("PV_TTC"));
162
        if (pvTTCCol != null) {
163
            pvTTCCol.setRenderer(CURRENCY_RENDERER);
164
        }
165
        final SQLTableModelColumn paHTCol = source.getColumn(getTable().getField("PA_HT"));
166
        if (paHTCol != null) {
167
            paHTCol.setRenderer(CURRENCY_RENDERER);
168
        }
169
 
94 ilm 170
        if (getTable().getDBRoot().contains("ARTICLE_ELEMENT") && !getTable().getDBRoot().contains("TARIF_AGENCE")) {
171
            source.getColumns().add(new BaseSQLTableModelColumn("Type", String.class) {
172
 
173
                @Override
174
                protected Object show_(SQLRowAccessor r) {
175
 
176
                    Collection<? extends SQLRowAccessor> c = r.getReferentRows(getTable().getTable("ARTICLE_ELEMENT").getField("ID_ARTICLE_PARENT"));
177
                    if (c.size() == 0) {
178
                        // "Article simple"
179
                        return "Article simple";
180
                    } else {
181
                        // "Kit
182
                        return "Nomenclature";
183
                    }
184
                }
185
 
186
                @Override
187
                public Set<FieldPath> getPaths() {
188
                    Path p = new Path(getTable());
189
                    p = p.add(getTable().getTable("ARTICLE_ELEMENT").getField("ID_ARTICLE_PARENT"));
190
                    return CollectionUtils.createSet(new FieldPath(p, "QTE"));
191
                }
192
            });
193
        }
83 ilm 194
    }
195
 
18 ilm 196
    protected List<String> getListFields() {
197
        final List<String> l = new ArrayList<String>();
198
 
199
        l.add("CODE");
93 ilm 200
        SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
201
        if (prefs.getBoolean(GestionArticleGlobalPreferencePanel.SHOW_PRODUCT_BAR_CODE, false)) {
202
            l.add("CODE_BARRE");
203
        }
18 ilm 204
        l.add("NOM");
205
        String articleAdvanced = DefaultNXProps.getInstance().getStringProperty("ArticleModeVenteAvance");
206
        Boolean bArticleAdvanced = Boolean.valueOf(articleAdvanced);
41 ilm 207
 
208
        if (bArticleAdvanced) {
18 ilm 209
            l.add("POIDS");
210
            l.add("PRIX_METRIQUE_HA_1");
211
            l.add("PRIX_METRIQUE_VT_1");
41 ilm 212
        }
18 ilm 213
        l.add("PA_HT");
214
        l.add("PV_HT");
215
            l.add("ID_TAXE");
216
        l.add("PV_TTC");
28 ilm 217
        l.add("ID_FAMILLE_ARTICLE");
19 ilm 218
        l.add("ID_FOURNISSEUR");
93 ilm 219
        l.add("SKU");
156 ilm 220
 
221
        // if (!prefs.getBoolean(GestionArticleGlobalPreferencePanel.STOCK_MULTI_DEPOT, false)) {
174 ilm 222
        l.add("GESTION_STOCK");
18 ilm 223
        l.add("ID_STOCK");
156 ilm 224
        // }
18 ilm 225
        String val = DefaultNXProps.getInstance().getStringProperty("ArticleService");
226
        Boolean b = Boolean.valueOf(val);
227
        if (b != null && b.booleanValue()) {
228
            l.add("SERVICE");
229
        }
230
        return l;
231
    }
232
 
83 ilm 233
    @Override
132 ilm 234
    public ListMap<String, String> getShowAs() {
235
        final ListMap<String, String> res = new ListMap<String, String>();
93 ilm 236
        SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
237
        if (prefs.getBoolean(GestionArticleGlobalPreferencePanel.SHOW_PRODUCT_BAR_CODE, false)) {
132 ilm 238
            res.add(null, "CODE_BARRE");
93 ilm 239
        }
156 ilm 240
        res.addAll(null, "CODE", "NOM", "ID_FAMILLE_ARTICLE");
83 ilm 241
        return res;
242
    }
243
 
18 ilm 244
    protected List<String> getComboFields() {
245
        final List<String> l = new ArrayList<String>();
246
        l.add("CODE");
73 ilm 247
        SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
248
        if (prefs.getBoolean(GestionArticleGlobalPreferencePanel.SHOW_PRODUCT_BAR_CODE, false)) {
249
            l.add("CODE_BARRE");
250
        }
251
 
18 ilm 252
        l.add("NOM");
253
        return l;
254
    }
255
 
256
    /*
257
     * (non-Javadoc)
258
     *
259
     * @see org.openconcerto.devis.SQLElement#getComponent()
260
     */
261
    public SQLComponent createComponent() {
262
 
263
        return new ReferenceArticleSQLComponent(this);
264
    }
265
 
266
    /**
267
     * Calcul le prix d'HA total par rapport aux metriques
268
     *
269
     * @param rowVals
270
     * @return le prix d'achat en centimes
271
     */
67 ilm 272
    public static BigDecimal getPrixHAFromDetails(SQLRowValues rowVals) {
18 ilm 273
        return getValuePiece(rowVals, PRIX_HA);
274
    }
275
 
276
    /**
277
     * Calcul le prix de VT total par rapport aux metriques
278
     *
279
     * @param rowVals
280
     * @return le prix de vente en centimes
281
     */
67 ilm 282
    public static BigDecimal getPrixVTFromDetails(SQLRowValues rowVals) {
18 ilm 283
        return getValuePiece(rowVals, PRIX_VT);
284
    }
285
 
286
    /**
287
     * Calcul le poids total par rapport aux metriques
288
     *
289
     * @param rowVals
290
     * @return le poids total arrondi à trois chiffres apres la virgule
291
     */
292
    public static float getPoidsFromDetails(SQLRowValues rowVals) {
293
 
294
        // Valeur
295
        float valMetrique1 = (rowVals.getObject("VALEUR_METRIQUE_1") == null) ? 0.0F : rowVals.getFloat("VALEUR_METRIQUE_1");
296
        float valMetrique2 = (rowVals.getObject("VALEUR_METRIQUE_2") == null) ? 0.0F : rowVals.getFloat("VALEUR_METRIQUE_2");
297
        float valMetrique3 = (rowVals.getObject("VALEUR_METRIQUE_3") == null) ? 0.0F : rowVals.getFloat("VALEUR_METRIQUE_3");
298
 
299
        final float produit = valMetrique1 * valMetrique2 * valMetrique3;
300
        if (produit > 0.0F) {
301
            return Math.round(produit * 1000.0F) / 1000.0F;
302
        }
303
        if (rowVals.getObject("POIDS") != null) {
304
            float p = rowVals.getFloat("POIDS");
305
            return p;
306
        }
307
        return 0.0F;
308
    }
309
 
67 ilm 310
    private static BigDecimal getValuePiece(SQLRowValues rowVals, int value) {
18 ilm 311
        if (rowVals.getObject("ID_MODE_VENTE_ARTICLE") == null) {
312
            throw new IllegalArgumentException("La SQLRowValues ne contient pas ID_MODE_VENTE_ARTICLE");
313
        }
314
        int mode = rowVals.getInt("ID_MODE_VENTE_ARTICLE");
315
        if (mode == 1) {
316
            mode = A_LA_PIECE;
317
        }
318
        // prix HA
67 ilm 319
        BigDecimal metrique1HA = rowVals.getObject("PRIX_METRIQUE_HA_1") == null ? BigDecimal.ZERO : ((BigDecimal) rowVals.getObject("PRIX_METRIQUE_HA_1"));
18 ilm 320
 
321
        // Prix VT
67 ilm 322
        BigDecimal metrique1VT = rowVals.getObject("PRIX_METRIQUE_VT_1") == null ? BigDecimal.ZERO : ((BigDecimal) rowVals.getObject("PRIX_METRIQUE_VT_1"));
18 ilm 323
 
324
        // Valeur
325
        float valMetrique1 = (rowVals.getObject("VALEUR_METRIQUE_1") == null) ? 0.0F : rowVals.getFloat("VALEUR_METRIQUE_1");
326
        float valMetrique2 = (rowVals.getObject("VALEUR_METRIQUE_2") == null) ? 0.0F : rowVals.getFloat("VALEUR_METRIQUE_2");
327
        float valMetrique3 = (rowVals.getObject("VALEUR_METRIQUE_3") == null) ? 0.0F : rowVals.getFloat("VALEUR_METRIQUE_3");
328
 
329
        // Mode de vente au metre carré
330
        if (mode == AU_METRE_CARRE) {
331
            float surface = valMetrique1 * valMetrique2;
332
            if (value == PRIX_HA) {
90 ilm 333
                return metrique1HA.multiply(BigDecimal.valueOf(surface), DecimalUtils.HIGH_PRECISION);
18 ilm 334
            }
90 ilm 335
            return metrique1VT.multiply(BigDecimal.valueOf(surface), DecimalUtils.HIGH_PRECISION);
18 ilm 336
        }
337
        // Mode de vente au metre, largeur
338
        if (mode == AU_METRE_LARGEUR) {
339
            if (value == PRIX_HA) {
90 ilm 340
                return metrique1HA.multiply(BigDecimal.valueOf(valMetrique2), DecimalUtils.HIGH_PRECISION);
18 ilm 341
            }
90 ilm 342
            return metrique1VT.multiply(BigDecimal.valueOf(valMetrique2), DecimalUtils.HIGH_PRECISION);
18 ilm 343
        }
344
        // Mode de vente au metre, longueur
345
        if (mode == AU_METRE_LONGUEUR) {
346
            if (value == PRIX_HA) {
90 ilm 347
                return metrique1HA.multiply(BigDecimal.valueOf(valMetrique1), DecimalUtils.HIGH_PRECISION);
18 ilm 348
            }
90 ilm 349
            return metrique1VT.multiply(BigDecimal.valueOf(valMetrique1), DecimalUtils.HIGH_PRECISION);
18 ilm 350
        }
351
        // Mode de vente au poids / m2
352
        if (mode == AU_POID_METRECARRE) {
353
            float surface = valMetrique1 * valMetrique2;
354
            float p = surface * valMetrique3;
355
            if (value == PRIX_HA) {
90 ilm 356
                return metrique1HA.multiply(BigDecimal.valueOf(p), DecimalUtils.HIGH_PRECISION);
18 ilm 357
            }
90 ilm 358
            return metrique1VT.multiply(BigDecimal.valueOf(p), DecimalUtils.HIGH_PRECISION);
18 ilm 359
        }
360
 
93 ilm 361
        // Mode de vente à la piece et autres
362
        if (value == PRIX_HA) {
363
            if (rowVals.getObject("PA_HT") != null) {
364
                return (BigDecimal) rowVals.getObject("PA_HT");
365
            }
366
            return BigDecimal.ZERO;
367
        }
368
        if (rowVals.getObject("PV_HT") != null) {
369
            return (BigDecimal) rowVals.getObject("PV_HT");
370
        }
371
        return BigDecimal.ZERO;
372
 
18 ilm 373
    }
374
 
375
    /**
376
     * retourne l'id d'un article ayant le meme Code, Nom et valeur Metrique, le cas échéant -1
377
     *
378
     * @param row
379
     * @param createIfNotExist
380
     * @return id de l'article correspondant
381
     */
382
    public static int getIdForCNM(SQLRowValues row, boolean createIfNotExist) {
383
        return getIdFor(row, true, createIfNotExist);
384
    }
385
 
386
    /**
387
     * retourne l'id d'un article ayant le meme Code, Nom le cas échéant -1
388
     *
389
     * @param row
390
     * @param createIfNotExist
391
     * @return id de l'article correspondant
392
     */
393
    public static int getIdForCN(SQLRowValues row, boolean createIfNotExist) {
394
 
395
        return getIdFor(row, false, createIfNotExist);
396
    }
397
 
93 ilm 398
    @SuppressWarnings("rawtypes")
18 ilm 399
    private static int getIdFor(SQLRowValues row, boolean includeMetrique, boolean createIfNotExist) {
400
 
401
        // On cherche l'article qui lui correspond
402
        SQLTable tableArt = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("ARTICLE");
403
        SQLElement eltArticle = Configuration.getInstance().getDirectory().getElement(tableArt);
404
        String req = getMatchRequest(row, includeMetrique);
405
        List result = (List) eltArticle.getTable().getBase().getDataSource().execute(req, new ArrayListHandler());
406
 
407
        if (result != null && result.size() != 0) {
408
            Object[] tmp = (Object[]) result.get(0);
409
            return ((Number) tmp[0]).intValue();
410
        }
411
 
93 ilm 412
        if (createIfNotExist && row.getString("CODE") != null && row.getString("CODE").trim().length() > 0 && row.getString("NOM") != null && row.getString("NOM").trim().length() > 0) {
18 ilm 413
            SQLRowValues vals = new SQLRowValues(row);
83 ilm 414
            BigDecimal taux = BigDecimal.ONE.add(new BigDecimal(TaxeCache.getCache().getTauxFromId(row.getForeignID("ID_TAXE")) / 100f));
415
            vals.put("PV_TTC", vals.getBigDecimal("PV_HT").multiply(taux));
156 ilm 416
            vals.put("ID_DEPOT_STOCK", DepotStockSQLElement.DEFAULT_ID);
93 ilm 417
            int idArticle;
18 ilm 418
            try {
83 ilm 419
 
420
                // Liaison avec l'article fournisseur si il existe
421
                SQLSelect selMatchingCodeF = new SQLSelect();
422
                final SQLTable table = tableArt.getTable("ARTICLE_FOURNISSEUR");
423
                selMatchingCodeF.addSelect(table.getKey());
424
                selMatchingCodeF.addSelect(table.getField("ID_FOURNISSEUR"));
425
                selMatchingCodeF.addSelect(table.getField("CODE_BARRE"));
426
                Where wMatchingCodeF = new Where(table.getField("CODE"), "=", vals.getString("CODE"));
427
                wMatchingCodeF = wMatchingCodeF.and(new Where(table.getField("NOM"), "=", vals.getString("NOM")));
428
                selMatchingCodeF.setWhere(wMatchingCodeF);
429
 
93 ilm 430
                // FIXME utiliser une transaction bloquante sur la table
431
                List<SQLRow> l = SQLRowListRSH.execute(selMatchingCodeF, false, false);
83 ilm 432
                if (l.size() > 0) {
433
                    SQLRowValues rowVals = l.get(0).asRowValues();
434
                    vals.put("ID_FOURNISSEUR", rowVals.getObject("ID_FOURNISSEUR"));
435
                    vals.put("CODE_BARRE", rowVals.getObject("CODE_BARRE"));
90 ilm 436
                    vals.put("QTE_ACHAT", rowVals.getObject("QTE_ACHAT"));
93 ilm 437
                    SQLRow rowNew = vals.insert();
438
                    idArticle = rowNew.getID();
439
                    rowVals.put("ID_ARTICLE", idArticle);
83 ilm 440
                    rowVals.commit();
441
                } else {
93 ilm 442
                    SQLSelect selMatchingCodeArticle = new SQLSelect();
443
                    selMatchingCodeArticle.addSelect(tableArt.getKey());
444
                    Where wMatchingCode = new Where(tableArt.getField("CODE"), "=", vals.getString("CODE"));
445
                    wMatchingCode = wMatchingCode.and(new Where(tableArt.getField("NOM"), "=", vals.getString("NOM")));
446
                    selMatchingCodeArticle.setWhere(wMatchingCode);
447
                    List<SQLRow> matchingArticles = SQLRowListRSH.execute(selMatchingCodeArticle, false, false);
448
                    if (matchingArticles.size() > 0) {
449
                        idArticle = matchingArticles.get(0).getID();
450
                    } else {
451
                        SQLRow rowNew = vals.insert();
452
                        idArticle = rowNew.getID();
453
                    }
83 ilm 454
                }
93 ilm 455
                return idArticle;
18 ilm 456
            } catch (SQLException e) {
457
                e.printStackTrace();
458
            }
459
 
460
        }
461
        return -1;
462
 
463
    }
464
 
465
    /**
466
     * teste si un article ayant le meme nom et code existe
467
     *
468
     * @param row
469
     * @return true si au moins un article correspond
470
     */
471
    public static boolean isArticleForCNExist(SQLRowValues row) {
472
        return isArticleMatchExist(row, false);
473
    }
474
 
475
    /**
476
     * teste si un article ayant le meme nom, code et valeur metrique existe
477
     *
478
     * @param row
479
     * @return true si au moins un article correspond
480
     */
481
    public static boolean isArticleForCNMExist(SQLRowValues row) {
482
        return isArticleMatchExist(row, true);
483
    }
484
 
93 ilm 485
    @SuppressWarnings("rawtypes")
18 ilm 486
    private static boolean isArticleMatchExist(SQLRowValues row, boolean includeMetrique) {
487
        SQLTable sqlTableArticle = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("ARTICLE");
488
        SQLElement eltArticle = Configuration.getInstance().getDirectory().getElement(sqlTableArticle);
489
        String req = getMatchRequest(row, includeMetrique);
490
        List result = (List) eltArticle.getTable().getBase().getDataSource().execute(req, new ArrayListHandler());
491
 
492
        return (result != null && result.size() != 0);
493
    }
494
 
495
    private static String getMatchRequest(SQLRowValues row, boolean includeMetrique) {
496
        // On cherche l'article qui lui correspond
497
        SQLTable sqlTableArticle = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("ARTICLE");
498
        SQLElement eltArticle = Configuration.getInstance().getDirectory().getElement(sqlTableArticle);
499
        SQLSelect sel = new SQLSelect(eltArticle.getTable().getBase());
500
        sel.addSelect(eltArticle.getTable().getField("ID"));
501
 
90 ilm 502
        Where w = new Where(eltArticle.getTable().getField("CODE"), "=", row.getString("CODE").trim());
18 ilm 503
        if (includeMetrique) {
504
 
505
            float value1 = ((Number) row.getObject("VALEUR_METRIQUE_1")).floatValue();
506
            float value2 = ((Number) row.getObject("VALEUR_METRIQUE_2")).floatValue();
507
            float value3 = ((Number) row.getObject("VALEUR_METRIQUE_3")).floatValue();
508
            w = w.and(new Where(eltArticle.getTable().getField("VALEUR_METRIQUE_1"), "<=", new Float(value1 + 0.00001)));
509
            w = w.and(new Where(eltArticle.getTable().getField("VALEUR_METRIQUE_1"), ">=", new Float(value1 - 0.00001)));
510
 
511
            w = w.and(new Where(eltArticle.getTable().getField("VALEUR_METRIQUE_2"), "<=", new Float(value2 + 0.00001)));
512
            w = w.and(new Where(eltArticle.getTable().getField("VALEUR_METRIQUE_2"), ">=", new Float(value2 - 0.00001)));
513
 
514
            w = w.and(new Where(eltArticle.getTable().getField("VALEUR_METRIQUE_3"), "<=", new Float(value3 + 0.00001)));
515
            w = w.and(new Where(eltArticle.getTable().getField("VALEUR_METRIQUE_3"), ">=", new Float(value3 - 0.00001)));
516
 
517
        }
518
        sel.setWhere(w);
519
        return sel.asString();
520
    }
19 ilm 521
 
522
    public static boolean isReferenceEquals(SQLRowValues rowVals1, SQLRowValues rowVals2) {
523
        return (rowVals1.getObject("CODE").equals(rowVals2.getObject("CODE")) && rowVals1.getString("VALEUR_METRIQUE_1").equals(rowVals2.getString("VALEUR_METRIQUE_1"))
524
                && rowVals1.getString("VALEUR_METRIQUE_2").equals(rowVals2.getString("VALEUR_METRIQUE_2")) && rowVals1.getString("VALEUR_METRIQUE_3").equals(rowVals2.getString("VALEUR_METRIQUE_3")));
525
    }
57 ilm 526
 
156 ilm 527
    public void initStock(int id) {
528
        SQLRow row = getTable().getRow(id);
174 ilm 529
        Updater_1_5.initStock(row);
156 ilm 530
    }
531
 
57 ilm 532
    @Override
533
    protected String createCode() {
156 ilm 534
        return createCodeOfPackage() + ".ref";
57 ilm 535
    }
156 ilm 536
 
537
    @Override
538
    protected void _initComboRequest(ComboSQLRequest req) {
539
        super._initComboRequest(req);
540
        req.addToGraphToFetch("ID_DEPOT_STOCK");
541
        // req.addForeignToGraphToFetch("ID_DEPOT_STOCK", Arrays.asList("ID"));
542
    }
543
 
18 ilm 544
}