OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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