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