OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
18 ilm 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.order.element;
15
 
142 ilm 16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
18 ilm 17
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
142 ilm 18
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
174 ilm 19
import org.openconcerto.erp.core.sales.product.element.UniteVenteArticleSQLElement;
142 ilm 20
import org.openconcerto.erp.core.supplychain.stock.element.MouvementStockSQLElement;
156 ilm 21
import org.openconcerto.erp.core.supplychain.stock.element.StockItemsUpdater;
22
import org.openconcerto.erp.core.supplychain.stock.element.StockItemsUpdater.TypeStockUpdate;
23
import org.openconcerto.erp.core.supplychain.stock.element.StockLabel;
182 ilm 24
import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
18 ilm 25
import org.openconcerto.sql.element.SQLComponent;
142 ilm 26
import org.openconcerto.sql.element.SQLElement;
18 ilm 27
import org.openconcerto.sql.element.UISQLComponent;
142 ilm 28
import org.openconcerto.sql.model.SQLInjector;
29
import org.openconcerto.sql.model.SQLRow;
30
import org.openconcerto.sql.model.SQLRowAccessor;
156 ilm 31
import org.openconcerto.sql.model.SQLRowListRSH;
142 ilm 32
import org.openconcerto.sql.model.SQLRowValues;
156 ilm 33
import org.openconcerto.sql.model.SQLSelect;
142 ilm 34
import org.openconcerto.sql.model.SQLTable;
156 ilm 35
import org.openconcerto.sql.model.Where;
182 ilm 36
import org.openconcerto.sql.preferences.SQLPreferences;
156 ilm 37
import org.openconcerto.sql.request.ListSQLRequest;
38
import org.openconcerto.sql.request.UpdateBuilder;
18 ilm 39
import org.openconcerto.sql.sqlobject.ElementComboBox;
156 ilm 40
import org.openconcerto.sql.users.UserManager;
41
import org.openconcerto.sql.view.EditFrame;
42
import org.openconcerto.sql.view.EditPanel.EditMode;
142 ilm 43
import org.openconcerto.sql.view.list.IListe;
44
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
45
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
156 ilm 46
import org.openconcerto.ui.FrameUtil;
142 ilm 47
import org.openconcerto.utils.DecimalUtils;
132 ilm 48
import org.openconcerto.utils.ListMap;
18 ilm 49
 
142 ilm 50
import java.awt.event.ActionEvent;
51
import java.math.BigDecimal;
156 ilm 52
import java.sql.SQLException;
18 ilm 53
import java.util.ArrayList;
142 ilm 54
import java.util.Arrays;
55
import java.util.HashSet;
18 ilm 56
import java.util.List;
142 ilm 57
import java.util.Set;
18 ilm 58
 
142 ilm 59
import javax.swing.AbstractAction;
19 ilm 60
import javax.swing.JTextField;
18 ilm 61
 
62
public class CommandeClientElementSQLElement extends ComptaSQLConfElement {
63
 
64
    public CommandeClientElementSQLElement() {
65
        super("COMMANDE_CLIENT_ELEMENT", "un element de commande", "éléments de commande");
142 ilm 66
 
67
        PredicateRowAction rowAction = new PredicateRowAction(new AbstractAction("Transfert vers commande fournisseur") {
68
 
69
            @Override
70
            public void actionPerformed(ActionEvent e) {
174 ilm 71
                final List<SQLRow> rows = SQLRowListRSH.fetch(IListe.get(e).getRequest().getPrimaryTable(), IListe.get(e).getSelection().getSelectedIDs());
72
                ((CommandeClientSQLElement) getForeignElement("ID_COMMANDE_CLIENT")).transfertEltToCommandeF(rows);
142 ilm 73
            }
74
 
75
        }, true);
76
        rowAction.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
77
        getRowActions().add(rowAction);
156 ilm 78
 
177 ilm 79
        PredicateRowAction rowActionBL = new PredicateRowAction(new AbstractAction("Transférer la commande en BL") {
80
 
81
            @Override
82
            public void actionPerformed(ActionEvent e) {
83
                List<SQLRowValues> resultId = new ArrayList<>();
84
                CommandeClientSQLElement cmdElt = (CommandeClientSQLElement) getForeignElement("ID_COMMANDE_CLIENT");
85
                for (SQLRowValues sqlRowAccessor : IListe.get(e).getSelectedRows()) {
86
                    resultId.add(sqlRowAccessor.getForeign("ID_COMMANDE_CLIENT").asRowValues());
87
                }
88
                cmdElt.transfertBonLivraisonClient(resultId);
89
            }
90
 
91
        }, true);
92
        rowActionBL.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
93
        getRowActions().add(rowActionBL);
94
 
156 ilm 95
        PredicateRowAction rowActionCmd = new PredicateRowAction(new AbstractAction("Modifier la commande associée") {
96
 
97
            @Override
98
            public void actionPerformed(ActionEvent e) {
99
                SQLRowValues selectedRow = IListe.get(e).getSelectedRow();
100
                EditFrame f = new EditFrame(getForeignElement("ID_COMMANDE_CLIENT"), EditMode.MODIFICATION);
101
                f.getSQLComponent().select(selectedRow.getForeignID("ID_COMMANDE_CLIENT"));
102
                FrameUtil.showPacked(f);
103
            }
104
 
105
        }, true);
106
        rowActionCmd.setPredicate(IListeEvent.getSingleSelectionPredicate());
107
        getRowActions().add(rowActionCmd);
108
 
109
        if (getTable().getForeignTable("ID_USER_COMMON_CREATE").getRow(UserManager.getUserID()).getBoolean("ADMIN")) {
110
            PredicateRowAction rowActionForceLivr = new PredicateRowAction(new AbstractAction("Forcer la livraison") {
111
 
112
                @Override
113
                public void actionPerformed(ActionEvent e) {
114
                    updateForceLivrer(e, Boolean.TRUE);
115
 
116
                }
117
 
118
            }, true);
119
            rowActionForceLivr.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
120
            getRowActions().add(rowActionForceLivr);
121
 
122
            PredicateRowAction rowActionAnnuler = new PredicateRowAction(new AbstractAction("Annuler Forcer la livraison") {
123
 
124
                @Override
125
                public void actionPerformed(ActionEvent e) {
126
                    updateForceLivrer(e, Boolean.FALSE);
127
 
128
                }
129
 
130
            }, true);
131
            rowActionAnnuler.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
132
            getRowActions().add(rowActionAnnuler);
133
 
134
            PredicateRowAction rowActionUpdateStockTh = new PredicateRowAction(new AbstractAction("Recalculer le stock théorique") {
135
 
136
                @Override
137
                public void actionPerformed(ActionEvent e) {
138
                    recalculStockTh();
139
 
140
                }
141
 
142
            }, true);
143
            rowActionUpdateStockTh.setPredicate(IListeEvent.createTotalRowCountPredicate(0, Integer.MAX_VALUE));
144
            getRowActions().add(rowActionUpdateStockTh);
145
 
146
        }
147
 
18 ilm 148
    }
149
 
156 ilm 150
    private void updateForceLivrer(ActionEvent e, Boolean state) {
151
        final List<SQLRowValues> selectedRows = IListe.get(e).getSelectedRows();
152
        final Set<Integer> ids = new HashSet<Integer>();
153
        for (SQLRowValues sqlRowValues : selectedRows) {
154
            ids.add(sqlRowValues.getID());
155
        }
156
        UpdateBuilder build = new UpdateBuilder(getTable());
157
        build.setObject("LIVRE_FORCED", state);
158
        build.setWhere(new Where(getTable().getKey(), ids));
159
        getTable().getDBSystemRoot().getDataSource().execute(build.asString());
160
        IListe.get(e).getModel().updateAll();
161
    }
162
 
163
    public void recalculStockTh() {
164
 
165
        // RAZ des stocks TH --> TH = REEL
166
        final SQLTable tableStock = getTable().getDBRoot().findTable("STOCK");
167
        String req = "UPDATE " + tableStock.getSQLName().quote() + " SET " + tableStock.getField("QTE_TH").getQuotedName() + " = " + tableStock.getField("QTE_REEL").getQuotedName() + ","
168
                + tableStock.getField("QTE_RECEPT_ATTENTE").getQuotedName() + " = 0," + tableStock.getField("QTE_LIV_ATTENTE").getQuotedName() + "=0";
169
        tableStock.getDBSystemRoot().getDataSource().execute(req);
170
 
171
        {
172
            SQLSelect selCmdElt = new SQLSelect();
173
            final SQLTable tableCmdElt = tableStock.getTable("COMMANDE_ELEMENT");
174
            selCmdElt.addSelectStar(tableCmdElt);
175
            Where w = new Where(tableCmdElt.getField("RECU_FORCED"), "=", Boolean.FALSE).and(new Where(tableCmdElt.getField("RECU"), "=", Boolean.FALSE));
180 ilm 176
            w = w.and(Where.createRaw(
177
                    tableCmdElt.getField("QTE_RECUE").getQuotedName() + " < (" + tableCmdElt.getField("QTE").getQuotedName() + "*" + tableCmdElt.getField("QTE_UNITAIRE").getQuotedName() + ")",
178
                    tableCmdElt.getField("QTE_UNITAIRE"), tableCmdElt.getField("QTE"), tableCmdElt.getField("QTE_RECUE")));
179
 
156 ilm 180
            selCmdElt.setWhere(w);
181
            List<SQLRow> res = SQLRowListRSH.execute(selCmdElt);
182
            if (res != null && res.size() > 0) {
183
                StockItemsUpdater updater = new StockItemsUpdater(new StockLabel() {
184
 
185
                    @Override
186
                    public String getLabel(SQLRowAccessor rowOrigin, SQLRowAccessor rowElt) {
187
                        return "Commande fournisseur N°" + rowElt.getForeign("ID_COMMANDE").getString("NUMERO");
188
                    }
189
                }, res.get(0), res, TypeStockUpdate.VIRTUAL_RECEPT, false);
190
                try {
191
                    updater.update();
192
                } catch (SQLException e1) {
193
                    e1.printStackTrace();
194
                }
195
            }
196
        }
197
        {
198
            SQLSelect selCmdElt = new SQLSelect();
199
            final SQLTable tableCmdElt = tableStock.getTable("COMMANDE_CLIENT_ELEMENT");
200
            selCmdElt.addSelectStar(tableCmdElt);
201
            Where w = new Where(tableCmdElt.getField("LIVRE_FORCED"), "=", Boolean.FALSE).and(new Where(tableCmdElt.getField("LIVRE"), "=", Boolean.FALSE));
180 ilm 202
            w = w.and(Where.createRaw(
203
                    tableCmdElt.getField("QTE_LIVREE").getQuotedName() + " < (" + tableCmdElt.getField("QTE").getQuotedName() + "*" + tableCmdElt.getField("QTE_UNITAIRE").getQuotedName() + ")",
204
                    tableCmdElt.getField("QTE_UNITAIRE"), tableCmdElt.getField("QTE"), tableCmdElt.getField("QTE_LIVREE")));
205
 
156 ilm 206
            selCmdElt.setWhere(w);
207
            List<SQLRow> res = SQLRowListRSH.execute(selCmdElt);
208
            if (res != null && res.size() > 0) {
209
                StockItemsUpdater updater = new StockItemsUpdater(new StockLabel() {
210
 
211
                    @Override
212
                    public String getLabel(SQLRowAccessor rowOrigin, SQLRowAccessor rowElt) {
213
                        return "Commande N°" + rowElt.getForeign("ID_COMMANDE_CLIENT").getString("NUMERO");
214
                    }
215
                }, res.get(0), res, TypeStockUpdate.VIRTUAL_DELIVER, false);
216
                try {
217
                    updater.update();
182 ilm 218
                } catch (
219
 
220
                SQLException e1) {
156 ilm 221
                    e1.printStackTrace();
222
                }
223
            }
224
        }
225
    }
226
 
132 ilm 227
    @Override
228
    protected String getParentFFName() {
229
        return "ID_COMMANDE_CLIENT";
230
    }
231
 
18 ilm 232
    protected List<String> getListFields() {
233
        final List<String> l = new ArrayList<String>();
234
        l.add("CODE");
235
        l.add("NOM");
182 ilm 236
        SQLPreferences prefs = new SQLPreferences(getTable().getDBRoot());
237
        if (prefs.getBoolean(GestionArticleGlobalPreferencePanel.ACTIVER_DECLINAISON, false)) {
238
 
239
            for (String fieldName : getTable().getFieldsName()) {
240
                if (fieldName.startsWith("ID_ARTICLE_DECLINAISON_")) {
241
                    l.add(fieldName);
242
                }
243
            }
244
        }
142 ilm 245
        l.add("ID_COMMANDE_CLIENT");
246
        l.add("ID_ARTICLE");
174 ilm 247
        l.add("ID_DEPOT_STOCK");
18 ilm 248
        l.add("PA_HT");
249
        l.add("PV_HT");
151 ilm 250
        l.add("T_PA_HT");
251
        l.add("T_PV_HT");
252
        l.add("T_PV_TTC");
142 ilm 253
        l.add("QTE");
254
        l.add("QTE_UNITAIRE");
255
        l.add("QTE_LIVREE");
156 ilm 256
        l.add("LIVRE");
257
        l.add("LIVRE_FORCED");
18 ilm 258
        return l;
259
    }
260
 
142 ilm 261
    /**
262
     * Transfert d'une commande en commande fournisseur
263
     *
264
     * @param commandeID
265
     */
182 ilm 266
    public void transfertCommande(List<SQLRowAccessor> commandeClientEltsRows) {
142 ilm 267
 
174 ilm 268
        ComptaPropsConfiguration.getInstanceCompta().getNonInteractiveSQLExecutor().execute(new Runnable() {
142 ilm 269
 
174 ilm 270
            @Override
271
            public void run() {
142 ilm 272
 
174 ilm 273
                SQLTable tableCmdElt = getDirectory().getElement("COMMANDE_ELEMENT").getTable();
274
                SQLElement eltArticle = getDirectory().getElement("ARTICLE");
142 ilm 275
 
174 ilm 276
                final ListMap<SQLRow, SQLRowValues> map = new ListMap<SQLRow, SQLRowValues>();
277
                List<String> fields2copy = Arrays.asList("CODE", "NOM", "VALEUR_METRIQUE_1", "VALEUR_METRIQUE_2", "VALEUR_METRIQUE_3");
142 ilm 278
 
174 ilm 279
                Set<Integer> artAdded = new HashSet<Integer>();
182 ilm 280
                for (SQLRowAccessor sqlRow : commandeClientEltsRows) {
174 ilm 281
                    boolean article = false;
282
                    if (sqlRow.getTable().getName().equalsIgnoreCase("ARTICLE")) {
283
                        article = true;
284
                    }
142 ilm 285
 
174 ilm 286
                    SQLRowAccessor rowArticleFind;
287
                    int qte = 1;
288
                    BigDecimal qteUV = BigDecimal.ONE;
289
                    boolean gestionStock = true;
290
                    if (!article) {
182 ilm 291
                        SQLRowValues rowArticle;
292
                        if (sqlRow.contains("ID_ARTICLE") && sqlRow.getNonEmptyForeign("ID_ARTICLE") != null) {
293
                            // on récupére l'article qui lui correspond
294
                            rowArticleFind = sqlRow.asRow().getForeign("ID_ARTICLE");
295
                            rowArticle = rowArticleFind.asRowValues();
296
                        } else {
297
                            // on récupére l'article qui lui correspond
298
                            rowArticle = new SQLRowValues(eltArticle.getTable());
299
                            for (String field : fields2copy) {
300
                                rowArticle.put(field, sqlRow.asRow().getObject(field));
301
                            }
302
                            int idArticle = ReferenceArticleSQLElement.getIdForCNM(rowArticle, true);
303
                            rowArticleFind = eltArticle.getTable().getRow(idArticle);
174 ilm 304
                        }
305
                        gestionStock = rowArticleFind.getBoolean("GESTION_STOCK");
306
                        if (!gestionStock) {
307
                            qte = sqlRow.getInt("QTE");
308
                            qteUV = sqlRow.getBigDecimal("QTE_UNITAIRE");
142 ilm 309
 
174 ilm 310
                            if (sqlRow.getObject("ID_UNITE_VENTE") != null && sqlRow.getForeignID("ID_UNITE_VENTE") != UniteVenteArticleSQLElement.A_LA_PIECE) {
311
                                qteUV = qteUV.multiply(new BigDecimal(qte));
312
                                qte = 1;
313
                            }
314
                        } else {
315
                            if (rowArticle.getForeign("ID_STOCK") != null && !rowArticle.isForeignEmpty("ID_STOCK")) {
316
                                SQLRowAccessor rowStock = rowArticle.getForeign("ID_STOCK");
317
                                qte = -Math.round(rowStock.getFloat("QTE_TH") - rowStock.getFloat("QTE_MIN"));
318
                            }
319
                        }
320
                    } else {
321
                        rowArticleFind = sqlRow;
182 ilm 322
                        if (sqlRow.getForeign("ID_STOCK") != null && !sqlRow.isForeignEmpty("ID_STOCK")) {
323
                            SQLRowAccessor rowStock = sqlRow.getForeign("ID_STOCK");
174 ilm 324
                            qte = -Math.round(rowStock.getFloat("QTE_TH") - rowStock.getFloat("QTE_MIN"));
325
                        }
326
                    }
327
                    if (rowArticleFind != null && !rowArticleFind.isUndefined() && (!gestionStock || !artAdded.contains(rowArticleFind.getID()))) {
142 ilm 328
 
174 ilm 329
                        artAdded.add(rowArticleFind.getID());
142 ilm 330
 
182 ilm 331
                        SQLInjector inj = SQLInjector.getInjector(eltArticle.getTable(), tableCmdElt);
174 ilm 332
                        SQLRowValues rowValsElt = new SQLRowValues(inj.createRowValuesFrom(rowArticleFind.asRow()));
142 ilm 333
 
174 ilm 334
                        rowValsElt.put("QTE", qte);
335
                        rowValsElt.put("QTE_UNITAIRE", qteUV);
336
 
337
                        rowValsElt.put("T_POIDS", rowValsElt.getLong("POIDS") * rowValsElt.getInt("QTE"));
338
                        rowValsElt.put("T_PA_HT", ((BigDecimal) rowValsElt.getObject("PA_HT")).multiply(new BigDecimal(rowValsElt.getInt("QTE")), DecimalUtils.HIGH_PRECISION));
339
                        rowValsElt.put("T_PA_TTC", ((BigDecimal) rowValsElt.getObject("T_PA_HT")).multiply(new BigDecimal((rowValsElt.getForeign("ID_TAXE").getFloat("TAUX") / 100.0 + 1.0)),
340
                                DecimalUtils.HIGH_PRECISION));
341
                        map.add(rowArticleFind.asRow().getForeignRow("ID_FOURNISSEUR"), rowValsElt);
142 ilm 342
                    }
343
                }
174 ilm 344
 
345
                MouvementStockSQLElement.createCommandeF(map, null, "");
142 ilm 346
            }
347
        });
348
    }
349
 
18 ilm 350
    protected List<String> getComboFields() {
351
        final List<String> l = new ArrayList<String>();
73 ilm 352
        if (getTable().contains("ID_ARTICLE")) {
353
            l.add("ID_ARTICLE");
354
        }
18 ilm 355
        l.add("NOM");
356
        l.add("PV_HT");
357
        return l;
358
    }
359
 
73 ilm 360
    @Override
156 ilm 361
    protected void _initListRequest(ListSQLRequest req) {
362
        super._initListRequest(req);
363
        req.addToGraphToFetch("ID_DEPOT_STOCK");
364
    }
365
 
366
    @Override
132 ilm 367
    public ListMap<String, String> getShowAs() {
368
        final ListMap<String, String> res = new ListMap<String, String>();
156 ilm 369
        res.putCollection("ID_COMMANDE_CLIENT", "NUMERO", "DATE", "DATE_LIVRAISON_PREV", "ID_CLIENT");
370
 
73 ilm 371
        if (getTable().contains("ID_ARTICLE")) {
174 ilm 372
            res.putCollection("ID_ARTICLE", "GESTION_STOCK", "ID_FAMILLE_ARTICLE", "ID_FOURNISSEUR");
73 ilm 373
        }
132 ilm 374
        res.putCollection(null, "NOM");
73 ilm 375
        return res;
376
    }
377
 
18 ilm 378
    /*
379
     * (non-Javadoc)
380
     *
381
     * @see org.openconcerto.devis.SQLElement#getComponent()
382
     */
383
    public SQLComponent createComponent() {
384
        return new UISQLComponent(this) {
385
            public void addViews() {
386
                this.addRequiredSQLObject(new JTextField(), "NOM", "left");
387
                this.addRequiredSQLObject(new JTextField(), "CODE", "right");
388
 
389
                this.addSQLObject(new ElementComboBox(), "ID_STYLE", "left");
390
 
156 ilm 391
                this.addRequiredSQLObject(new JTextField(), "PA_HT", "left");
392
                this.addSQLObject(new JTextField(), "PV_HT", "right");
18 ilm 393
 
394
                this.addSQLObject(new JTextField(), "POIDS", "left");
395
                this.addSQLObject(new ElementComboBox(), "ID_TAXE", "right");
396
            }
397
        };
398
    }
57 ilm 399
 
400
    @Override
401
    protected String createCode() {
156 ilm 402
        return createCodeOfPackage() + ".item";
57 ilm 403
    }
18 ilm 404
}