OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | 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
 *
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));
174
            selCmdElt.setWhere(w);
175
            List<SQLRow> res = SQLRowListRSH.execute(selCmdElt);
176
            if (res != null && res.size() > 0) {
177
                StockItemsUpdater updater = new StockItemsUpdater(new StockLabel() {
178
 
179
                    @Override
180
                    public String getLabel(SQLRowAccessor rowOrigin, SQLRowAccessor rowElt) {
181
                        return "Commande fournisseur N°" + rowElt.getForeign("ID_COMMANDE").getString("NUMERO");
182
                    }
183
                }, res.get(0), res, TypeStockUpdate.VIRTUAL_RECEPT, false);
184
                try {
185
                    updater.update();
186
                } catch (SQLException e1) {
187
                    e1.printStackTrace();
188
                }
189
            }
190
        }
191
        {
192
            SQLSelect selCmdElt = new SQLSelect();
193
            final SQLTable tableCmdElt = tableStock.getTable("COMMANDE_CLIENT_ELEMENT");
194
            selCmdElt.addSelectStar(tableCmdElt);
195
            Where w = new Where(tableCmdElt.getField("LIVRE_FORCED"), "=", Boolean.FALSE).and(new Where(tableCmdElt.getField("LIVRE"), "=", Boolean.FALSE));
196
            selCmdElt.setWhere(w);
197
            List<SQLRow> res = SQLRowListRSH.execute(selCmdElt);
198
            if (res != null && res.size() > 0) {
199
                StockItemsUpdater updater = new StockItemsUpdater(new StockLabel() {
200
 
201
                    @Override
202
                    public String getLabel(SQLRowAccessor rowOrigin, SQLRowAccessor rowElt) {
203
                        return "Commande N°" + rowElt.getForeign("ID_COMMANDE_CLIENT").getString("NUMERO");
204
                    }
205
                }, res.get(0), res, TypeStockUpdate.VIRTUAL_DELIVER, false);
206
                try {
207
                    updater.update();
208
                } catch (SQLException e1) {
209
                    e1.printStackTrace();
210
                }
211
            }
212
        }
213
    }
214
 
132 ilm 215
    @Override
216
    protected String getParentFFName() {
217
        return "ID_COMMANDE_CLIENT";
218
    }
219
 
18 ilm 220
    protected List<String> getListFields() {
221
        final List<String> l = new ArrayList<String>();
222
        l.add("CODE");
223
        l.add("NOM");
142 ilm 224
        l.add("ID_COMMANDE_CLIENT");
225
        l.add("ID_ARTICLE");
174 ilm 226
        l.add("ID_DEPOT_STOCK");
18 ilm 227
        l.add("PA_HT");
228
        l.add("PV_HT");
151 ilm 229
        l.add("T_PA_HT");
230
        l.add("T_PV_HT");
231
        l.add("T_PV_TTC");
142 ilm 232
        l.add("QTE");
233
        l.add("QTE_UNITAIRE");
234
        l.add("QTE_LIVREE");
156 ilm 235
        l.add("LIVRE");
236
        l.add("LIVRE_FORCED");
18 ilm 237
        return l;
238
    }
239
 
142 ilm 240
    /**
241
     * Transfert d'une commande en commande fournisseur
242
     *
243
     * @param commandeID
244
     */
245
    public void transfertCommande(List<SQLRowValues> commandeClientEltsRows) {
246
 
174 ilm 247
        ComptaPropsConfiguration.getInstanceCompta().getNonInteractiveSQLExecutor().execute(new Runnable() {
142 ilm 248
 
174 ilm 249
            @Override
250
            public void run() {
142 ilm 251
 
174 ilm 252
                SQLTable tableCmdElt = getDirectory().getElement("COMMANDE_ELEMENT").getTable();
253
                SQLElement eltArticle = getDirectory().getElement("ARTICLE");
142 ilm 254
 
174 ilm 255
                final ListMap<SQLRow, SQLRowValues> map = new ListMap<SQLRow, SQLRowValues>();
256
                List<String> fields2copy = Arrays.asList("CODE", "NOM", "VALEUR_METRIQUE_1", "VALEUR_METRIQUE_2", "VALEUR_METRIQUE_3");
142 ilm 257
 
174 ilm 258
                Set<Integer> artAdded = new HashSet<Integer>();
259
                for (SQLRowValues sqlRow : commandeClientEltsRows) {
260
                    boolean article = false;
261
                    if (sqlRow.getTable().getName().equalsIgnoreCase("ARTICLE")) {
262
                        article = true;
263
                    }
142 ilm 264
 
174 ilm 265
                    // renderer sur liste des ar
142 ilm 266
 
174 ilm 267
                    // if()
142 ilm 268
 
174 ilm 269
                    /// rowValsElt.put("QTE", sqlRow.getObject("QTE"));
270
                    SQLRowAccessor rowArticleFind;
271
                    SQLRowValues rowArticle;
272
                    int qte = 1;
273
                    BigDecimal qteUV = BigDecimal.ONE;
274
                    boolean gestionStock = true;
275
                    if (!article) {
276
                        // on récupére l'article qui lui correspond
277
                        rowArticle = new SQLRowValues(eltArticle.getTable());
278
                        for (String field : fields2copy) {
279
                            // if (sqlRow.getTable().getFieldsName().contains(field.getName())) {
280
                            rowArticle.put(field, sqlRow.asRow().getObject(field));
281
                            // }
282
                        }
142 ilm 283
 
174 ilm 284
                        int idArticle = ReferenceArticleSQLElement.getIdForCNM(rowArticle, true);
285
                        rowArticleFind = eltArticle.getTable().getRow(idArticle);
142 ilm 286
 
174 ilm 287
                        gestionStock = rowArticleFind.getBoolean("GESTION_STOCK");
288
                        if (!gestionStock) {
289
                            qte = sqlRow.getInt("QTE");
290
                            qteUV = sqlRow.getBigDecimal("QTE_UNITAIRE");
142 ilm 291
 
174 ilm 292
                            if (sqlRow.getObject("ID_UNITE_VENTE") != null && sqlRow.getForeignID("ID_UNITE_VENTE") != UniteVenteArticleSQLElement.A_LA_PIECE) {
293
                                qteUV = qteUV.multiply(new BigDecimal(qte));
294
                                qte = 1;
295
                            }
296
                        } else {
297
                            if (rowArticle.getForeign("ID_STOCK") != null && !rowArticle.isForeignEmpty("ID_STOCK")) {
298
                                SQLRowAccessor rowStock = rowArticle.getForeign("ID_STOCK");
299
                                qte = -Math.round(rowStock.getFloat("QTE_TH") - rowStock.getFloat("QTE_MIN"));
300
                            }
301
                        }
302
                    } else {
303
                        rowArticleFind = sqlRow;
304
                        rowArticle = sqlRow;
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
                    if (rowArticleFind != null && !rowArticleFind.isUndefined() && (!gestionStock || !artAdded.contains(rowArticleFind.getID()))) {
142 ilm 311
 
174 ilm 312
                        artAdded.add(rowArticleFind.getID());
142 ilm 313
 
174 ilm 314
                        SQLInjector inj = SQLInjector.getInjector(rowArticle.getTable(), tableCmdElt);
315
                        SQLRowValues rowValsElt = new SQLRowValues(inj.createRowValuesFrom(rowArticleFind.asRow()));
142 ilm 316
 
174 ilm 317
                        rowValsElt.put("ID_STYLE", sqlRow.getObject("ID_STYLE"));
156 ilm 318
 
174 ilm 319
                        rowValsElt.put("QTE", qte);
320
                        rowValsElt.put("QTE_UNITAIRE", qteUV);
321
 
322
                        rowValsElt.put("T_POIDS", rowValsElt.getLong("POIDS") * rowValsElt.getInt("QTE"));
323
                        rowValsElt.put("T_PA_HT", ((BigDecimal) rowValsElt.getObject("PA_HT")).multiply(new BigDecimal(rowValsElt.getInt("QTE")), DecimalUtils.HIGH_PRECISION));
324
                        rowValsElt.put("T_PA_TTC", ((BigDecimal) rowValsElt.getObject("T_PA_HT")).multiply(new BigDecimal((rowValsElt.getForeign("ID_TAXE").getFloat("TAUX") / 100.0 + 1.0)),
325
                                DecimalUtils.HIGH_PRECISION));
326
                        // rowValsElt.put("ID_DEVISE",
327
                        // rowCmd.getForeignRow("ID_TARIF").getForeignID("ID_DEVISE"));
328
                        map.add(rowArticleFind.asRow().getForeignRow("ID_FOURNISSEUR"), rowValsElt);
142 ilm 329
                    }
330
                }
174 ilm 331
 
332
                MouvementStockSQLElement.createCommandeF(map, null, "");
142 ilm 333
            }
334
        });
335
    }
336
 
18 ilm 337
    protected List<String> getComboFields() {
338
        final List<String> l = new ArrayList<String>();
73 ilm 339
        if (getTable().contains("ID_ARTICLE")) {
340
            l.add("ID_ARTICLE");
341
        }
18 ilm 342
        l.add("NOM");
343
        l.add("PV_HT");
344
        return l;
345
    }
346
 
73 ilm 347
    @Override
156 ilm 348
    protected void _initListRequest(ListSQLRequest req) {
349
        super._initListRequest(req);
350
        req.addToGraphToFetch("ID_DEPOT_STOCK");
351
    }
352
 
353
    @Override
132 ilm 354
    public ListMap<String, String> getShowAs() {
355
        final ListMap<String, String> res = new ListMap<String, String>();
156 ilm 356
        res.putCollection("ID_COMMANDE_CLIENT", "NUMERO", "DATE", "DATE_LIVRAISON_PREV", "ID_CLIENT");
357
 
73 ilm 358
        if (getTable().contains("ID_ARTICLE")) {
174 ilm 359
            res.putCollection("ID_ARTICLE", "GESTION_STOCK", "ID_FAMILLE_ARTICLE", "ID_FOURNISSEUR");
73 ilm 360
        }
132 ilm 361
        res.putCollection(null, "NOM");
73 ilm 362
        return res;
363
    }
364
 
18 ilm 365
    /*
366
     * (non-Javadoc)
367
     *
368
     * @see org.openconcerto.devis.SQLElement#getComponent()
369
     */
370
    public SQLComponent createComponent() {
371
        return new UISQLComponent(this) {
372
            public void addViews() {
373
                this.addRequiredSQLObject(new JTextField(), "NOM", "left");
374
                this.addRequiredSQLObject(new JTextField(), "CODE", "right");
375
 
376
                this.addSQLObject(new ElementComboBox(), "ID_STYLE", "left");
377
 
156 ilm 378
                this.addRequiredSQLObject(new JTextField(), "PA_HT", "left");
379
                this.addSQLObject(new JTextField(), "PV_HT", "right");
18 ilm 380
 
381
                this.addSQLObject(new JTextField(), "POIDS", "left");
382
                this.addSQLObject(new ElementComboBox(), "ID_TAXE", "right");
383
            }
384
        };
385
    }
57 ilm 386
 
387
    @Override
388
    protected String createCode() {
156 ilm 389
        return createCodeOfPackage() + ".item";
57 ilm 390
    }
18 ilm 391
}