OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
144 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
 /*
15
 * Créé le 18 oct. 2011
16
 */
17
package org.openconcerto.erp.core.supplychain.order.element;
18
 
19
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
20
import org.openconcerto.erp.core.common.element.StyleSQLElement;
149 ilm 21
import org.openconcerto.erp.core.common.ui.DeviseTableCellRenderer;
144 ilm 22
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
149 ilm 23
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
144 ilm 24
import org.openconcerto.erp.core.supplychain.order.ui.EtatDemandeAchatRenderer;
25
import org.openconcerto.sql.element.SQLComponent;
26
import org.openconcerto.sql.model.SQLInjector;
27
import org.openconcerto.sql.model.SQLRow;
28
import org.openconcerto.sql.model.SQLRowValues;
29
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
30
import org.openconcerto.sql.model.SQLSelect;
31
import org.openconcerto.sql.model.SQLTable;
32
import org.openconcerto.sql.model.Where;
33
import org.openconcerto.sql.view.EditFrame;
34
import org.openconcerto.sql.view.list.IListe;
149 ilm 35
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
144 ilm 36
import org.openconcerto.sql.view.list.RowAction;
149 ilm 37
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
144 ilm 38
import org.openconcerto.sql.view.list.SQLTableModelSource;
39
import org.openconcerto.ui.FrameUtil;
40
import org.openconcerto.utils.ExceptionHandler;
41
import org.openconcerto.utils.ListMap;
42
import org.openconcerto.utils.cc.ITransformer;
43
 
44
import java.awt.event.ActionEvent;
45
import java.math.BigDecimal;
46
import java.sql.SQLException;
47
import java.util.ArrayList;
48
import java.util.Arrays;
49
import java.util.Collection;
50
import java.util.Date;
51
import java.util.List;
52
 
53
import javax.swing.AbstractAction;
54
 
55
public class DemandeAchatItemSQLElement extends ComptaSQLConfElement {
56
 
57
    public DemandeAchatItemSQLElement() {
156 ilm 58
        super("DEMANDE_ACHAT_ELEMENT");
149 ilm 59
        {
60
            PredicateRowAction actionP = new PredicateRowAction(new AbstractAction("Créer à partir de") {
144 ilm 61
 
149 ilm 62
                @Override
63
                public void actionPerformed(ActionEvent e) {
64
                    SQLRow row = IListe.get(e).getSelectedRow().asRow();
65
                    SQLRowValues rowDupl = new SQLRowValues(row.getTable());
66
                    List<String> copyFields = Arrays.asList("COLOR", "NUMERO_SE", "DESCRIPTIF_SE", "REVISION", "P_HT", "T_HT", "REPERE_SOURCE", "CODE_SOURCE", "NOM_SOURCE", "CODE", "NOM", "QTE",
67
                            "QTE_UNITAIRE", "REPERE", "REFERENCE", "ID_FABRICANT", "ID_UNITE_VENTE", "ID_ARTICLE", "ID_FOURNISSEUR", "ID_FAMILLE_ARTICLE", "ID_AFFAIRE");
68
                    // "CODE" character varying(256) NOT NULL DEFAULT ''::character varying,
69
                    //
70
                    // "DATE" date,
71
                    // "ID_ETAT_DEMANDE_ACHAT_ELEMENT" integer DEFAULT 1,
72
                    // "REPRISE" boolean NOT NULL DEFAULT false,
73
                    for (String string : copyFields) {
74
                        if (string.startsWith("ID_")) {
75
                            rowDupl.put(string, row.getForeignID(string));
76
                        } else {
77
                            rowDupl.put(string, row.getObject(string));
78
                        }
79
                    }
80
                    rowDupl.put("DATE", new Date());
81
                    EditFrame frame = new EditFrame(getDirectory().getElement("DEMANDE_ACHAT_ELEMENT"));
82
                    frame.getSQLComponent().select(rowDupl);
83
                    FrameUtil.showPacked(frame);
84
                }
85
            }, true);
86
 
87
            actionP.setPredicate(IListeEvent.getSingleSelectionPredicate());
88
            getRowActions().add(actionP);
89
        }
144 ilm 90
        {
91
            RowAction actionP = new RowAction(new AbstractAction("Transfert en demande de prix") {
92
 
93
                @Override
94
                public void actionPerformed(ActionEvent e) {
95
                    List<SQLRowValues> rows = IListe.get(e).getSelectedRows();
96
                    StyleSQLElement styleElt = getDirectory().getElement(StyleSQLElement.class);
97
                    int idNormal = styleElt.getAllStyleByName().get("Normal");
98
 
99
                    SQLRowValues rowVals = new SQLRowValues(getTable().getTable("DEMANDE_PRIX"));
100
                    rowVals.put("DATE", new Date());
101
                    SQLInjector inj = SQLInjector.getInjector(getTable(), getTable().getTable("DEMANDE_PRIX_ELEMENT"));
102
                    for (SQLRowValues sqlRowValues : rows) {
103
                        SQLRow row = sqlRowValues.asRow();
104
                        row.fetchValues();
105
                        rowVals.put("ID_AFFAIRE", row.getForeignID("ID_AFFAIRE"));
106
                        SQLRowValues rowValsCmdElt = inj.createRowValuesFrom(row);
107
                        rowValsCmdElt.put("ID_STYLE", idNormal);
149 ilm 108
                        rowValsCmdElt.put("ID_MODE_VENTE_ARTICLE", ReferenceArticleSQLElement.A_LA_PIECE);
144 ilm 109
                        rowValsCmdElt.put("ID_TAXE", TaxeCache.getCache().getFirstTaxe().getID());
110
                        rowValsCmdElt.put("ID_DEMANDE_PRIX", rowVals);
111
                        rowValsCmdElt.put("ID_DEMANDE_ACHAT_ELEMENT", row.getID());
112
                        if (row.getObject("ID_ARTICLE") != null && !row.isForeignEmpty("ID_ARTICLE")) {
113
                            BigDecimal ha = row.getForeign("ID_ARTICLE").getBigDecimal("PA_HT");
114
                            rowValsCmdElt.put("PA_HT", ha);
115
                            rowValsCmdElt.put("PRIX_METRIQUE_HA_1", ha);
116
                        } else {
117
                            rowValsCmdElt.put("PA_HT", BigDecimal.ZERO);
118
                            rowValsCmdElt.put("PRIX_METRIQUE_HA_1", BigDecimal.ZERO);
119
                        }
120
                    }
121
 
122
                    EditFrame frame = new EditFrame(getDirectory().getElement("DEMANDE_PRIX"));
123
                    frame.getSQLComponent().select(rowVals);
124
                    FrameUtil.showPacked(frame);
125
                }
126
            }, true) {
127
                @Override
128
                public boolean enabledFor(List<SQLRowValues> selection) {
129
                    if (selection != null && selection.size() > 0) {
130
                        int idAff = -1;
131
                        for (SQLRowValues sqlRowValues : selection) {
132
                            if (getTable().contains("ID_AFFAIRE")) {
133
                                if (sqlRowValues.getObject("ID_AFFAIRE") != null && !sqlRowValues.isForeignEmpty("ID_AFFAIRE")) {
134
                                    if (idAff == -1) {
135
                                        idAff = sqlRowValues.getForeignID("ID_AFFAIRE");
136
                                    }
137
                                    if (idAff != sqlRowValues.getForeignID("ID_AFFAIRE")) {
138
                                        return false;
139
                                    }
140
                                }
141
                            }
142
                        }
143
                        return true;
144
                    }
145
                    return false;
146
                }
147
            };
148
            getRowActions().add(actionP);
149
        }
150
        {
151
            RowAction actionP = new RowAction(new AbstractAction("Transfert en commande") {
152
 
153
                @Override
154
                public void actionPerformed(ActionEvent e) {
155
                    List<SQLRowValues> rows = IListe.get(e).getSelectedRows();
156
                    StyleSQLElement styleElt = getDirectory().getElement(StyleSQLElement.class);
157
                    int idNormal = styleElt.getAllStyleByName().get("Normal");
158
 
159
                    SQLRowValues rowVals = new SQLRowValues(getTable().getTable("COMMANDE"));
160
                    rowVals.put("DATE", new Date());
161
                    SQLInjector inj = SQLInjector.getInjector(getTable(), getTable().getTable("COMMANDE_ELEMENT"));
162
                    for (SQLRowValues sqlRowValues : rows) {
163
                        SQLRow row = sqlRowValues.asRow();
164
                        row.fetchValues();
165
                        rowVals.put("ID_AFFAIRE", row.getForeignID("ID_AFFAIRE"));
166
                        SQLRowValues rowValsCmdElt = inj.createRowValuesFrom(row);
167
                        rowValsCmdElt.put("ID_STYLE", idNormal);
149 ilm 168
                        rowValsCmdElt.put("ID_MODE_VENTE_ARTICLE", ReferenceArticleSQLElement.A_LA_PIECE);
144 ilm 169
                        rowValsCmdElt.put("ID_TAXE", TaxeCache.getCache().getFirstTaxe().getID());
170
                        rowValsCmdElt.put("ID_COMMANDE", rowVals);
171
                        rowValsCmdElt.put("ID_DEMANDE_ACHAT_ELEMENT", row.getID());
172
                        if (row.getObject("ID_ARTICLE") != null && !row.isForeignEmpty("ID_ARTICLE")) {
173
                            BigDecimal ha = row.getForeign("ID_ARTICLE").getBigDecimal("PA_HT");
174
                            rowValsCmdElt.put("PA_HT", ha);
175
                            rowValsCmdElt.put("PRIX_METRIQUE_HA_1", ha);
176
                        } else {
177
                            rowValsCmdElt.put("PA_HT", BigDecimal.ZERO);
178
                            rowValsCmdElt.put("PRIX_METRIQUE_HA_1", BigDecimal.ZERO);
179
                        }
180
                    }
181
 
182
                    EditFrame frame = new EditFrame(getDirectory().getElement("COMMANDE"));
183
                    frame.getSQLComponent().select(rowVals);
184
                    FrameUtil.showPacked(frame);
185
                }
186
            }, true) {
187
                @Override
188
                public boolean enabledFor(List<SQLRowValues> selection) {
189
                    if (selection != null && selection.size() > 0) {
190
                        int idAff = -1;
191
                        for (SQLRowValues sqlRowValues : selection) {
192
                            if (getTable().contains("ID_AFFAIRE")) {
193
                                if (sqlRowValues.getObject("ID_AFFAIRE") != null && !sqlRowValues.isForeignEmpty("ID_AFFAIRE")) {
194
                                    if (idAff == -1) {
195
                                        idAff = sqlRowValues.getForeignID("ID_AFFAIRE");
196
                                    }
197
                                    if (idAff != sqlRowValues.getForeignID("ID_AFFAIRE")) {
198
                                        return false;
199
                                    }
200
                                }
201
                            }
202
                        }
203
                        return true;
204
                    }
205
                    return false;
206
                }
207
            };
208
            getRowActions().add(actionP);
209
        }
210
    }
211
 
212
    public void updateStatus(final SQLTable table, final SQLTable tableElt, final int id) {
213
        SQLRowValues rowVals = new SQLRowValues(tableElt);
214
        rowVals.put("ID_DEMANDE_ACHAT_ELEMENT", null);
215
        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowVals);
216
        fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
217
 
218
            @Override
219
            public SQLSelect transformChecked(SQLSelect input) {
220
 
221
                Where w = new Where(tableElt.getField("ID_" + table.getName()), "=", id);
222
                w = w.and(new Where(tableElt.getField("ID_DEMANDE_ACHAT_ELEMENT"), "!=", (Object) null));
223
                w = w.and(new Where(tableElt.getField("ID_DEMANDE_ACHAT_ELEMENT"), ">", 1));
224
                input.setWhere(w);
225
 
226
                return input;
227
            }
228
        });
229
 
230
        List<SQLRowValues> result = fetcher.fetch();
231
        List<Integer> ids = new ArrayList<Integer>();
232
        for (SQLRowValues sqlRowValues : result) {
233
            ids.add(sqlRowValues.getForeignID("ID_DEMANDE_ACHAT_ELEMENT"));
234
        }
235
        updateAllStatus(ids);
236
    }
237
 
238
    private void updateAllStatus(final List<Integer> ids) {
239
 
240
        SQLRowValues rowVals = new SQLRowValues(getTable());
241
        rowVals.put("QTE", null);
149 ilm 242
        rowVals.put("QTE_UNITAIRE", null);
144 ilm 243
        rowVals.put("ID_ETAT_DEMANDE_ACHAT_ELEMENT", null);
244
 
245
        final List<String> refTable = Arrays.asList("COMMANDE_ELEMENT", "DEMANDE_PRIX_ELEMENT", "BON_RECEPTION_ELEMENT");
246
        for (String tableName : refTable) {
247
            SQLRowValues rowValsRef = new SQLRowValues(getTable().getTable(tableName));
248
            rowValsRef.put("QTE", null);
149 ilm 249
            rowValsRef.put("PA_HT", null);
144 ilm 250
            rowValsRef.put("ID_DEMANDE_ACHAT_ELEMENT", rowVals);
251
            rowValsRef.put("QTE_UNITAIRE", null);
252
            rowValsRef.put("ID_" + tableName.replaceAll("_ELEMENT", ""), null);
253
        }
254
 
255
        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowVals);
256
        fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
257
 
258
            @Override
259
            public SQLSelect transformChecked(SQLSelect input) {
260
 
261
                input.setWhere(new Where(input.getTable("DEMANDE_ACHAT_ELEMENT").getKey(), ids));
262
 
263
                return input;
264
            }
265
        });
266
        final List<SQLRowValues> result = fetcher.fetch();
267
        for (SQLRowValues sqlRowValues : result) {
268
            updateStatus(sqlRowValues);
269
        }
270
    }
271
 
272
    private void updateStatus(SQLRowValues rowValsDmd) {
273
        Collection<SQLRowValues> rowValsBR = rowValsDmd.getReferentRows(getTable().getTable("BON_RECEPTION_ELEMENT"));
274
        int newStatus = EtatDemandeAchatItemSQLElement.EN_ATTENTE;
275
        final SQLRowValues createEmptyUpdateRow = rowValsDmd.createEmptyUpdateRow();
276
        if (!rowValsBR.isEmpty()) {
277
            int qte = 0;
278
            int idBR = 1;
149 ilm 279
            BigDecimal prix = BigDecimal.ZERO;
144 ilm 280
            for (SQLRowValues sqlRowValues : rowValsBR) {
281
                qte += sqlRowValues.getInt("QTE");
282
                idBR = sqlRowValues.getForeignID("ID_BON_RECEPTION");
149 ilm 283
                prix = sqlRowValues.getBigDecimal("PA_HT");
144 ilm 284
            }
285
            if (qte > 0) {
286
                createEmptyUpdateRow.put("ID_BON_RECEPTION", idBR);
287
                createEmptyUpdateRow.put("QTE_RECUE", qte);
149 ilm 288
                createEmptyUpdateRow.put("P_HT", prix);
289
                createEmptyUpdateRow.put("T_HT", prix.multiply(rowValsDmd.getBigDecimal("QTE_UNITAIRE").multiply(new BigDecimal(rowValsDmd.getInt("QTE")))));
144 ilm 290
                if (qte >= rowValsDmd.getInt("QTE")) {
291
                    newStatus = EtatDemandeAchatItemSQLElement.RECPETION;
292
                } else {
293
                    newStatus = EtatDemandeAchatItemSQLElement.RECPETION_PARTIEL;
294
                }
295
            }
296
        } else {
297
            Collection<SQLRowValues> rowValsCmd = rowValsDmd.getReferentRows(getTable().getTable("COMMANDE_ELEMENT"));
298
            if (!rowValsCmd.isEmpty()) {
149 ilm 299
                final SQLRowValues next = rowValsCmd.iterator().next();
300
                createEmptyUpdateRow.put("ID_COMMANDE", next.getForeignID("ID_COMMANDE"));
301
                createEmptyUpdateRow.put("P_HT", next.getBigDecimal("PA_HT"));
302
                createEmptyUpdateRow.put("T_HT", next.getBigDecimal("PA_HT").multiply(rowValsDmd.getBigDecimal("QTE_UNITAIRE").multiply(new BigDecimal(rowValsDmd.getInt("QTE")))));
144 ilm 303
                newStatus = EtatDemandeAchatItemSQLElement.COMMANDE;
304
            } else {
305
                Collection<SQLRowValues> rowValsDmdElt = rowValsDmd.getReferentRows(getTable().getTable("DEMANDE_PRIX_ELEMENT"));
306
                if (!rowValsDmdElt.isEmpty()) {
307
                    newStatus = EtatDemandeAchatItemSQLElement.DEMANDE;
308
                }
309
 
310
            }
311
        }
312
 
313
        // if (newStatus != rowValsDmd.getForeignID("ID_ETAT_DEMANDE_ACHAT_ELEMENT")) {
314
        try {
315
 
316
            createEmptyUpdateRow.put("ID_ETAT_DEMANDE_ACHAT_ELEMENT", newStatus).commit();
317
        } catch (SQLException e) {
318
            ExceptionHandler.handle("Erreur lors la mise à jour de l'état de la demande d'achat [" + rowValsDmd.getID() + "]", e);
319
        }
320
        // }
321
    }
322
 
323
    @Override
324
    protected List<String> getListFields() {
325
        final List<String> l = new ArrayList<String>();
149 ilm 326
        l.add("DATE");
144 ilm 327
        l.add("REVISION");
328
        l.add("CODE");
329
        l.add("NOM");
149 ilm 330
        l.add("IMPORT");
331
        l.add("REPRISE");
144 ilm 332
        l.add("QTE_UNITAIRE");
333
        l.add("ID_UNITE_VENTE");
149 ilm 334
        l.add("P_HT");
144 ilm 335
        l.add("QTE");
149 ilm 336
        l.add("T_HT");
144 ilm 337
        l.add("ID_FABRICANT");
338
        l.add("ID_FOURNISSEUR");
339
        l.add("CODE_SOURCE");
340
        l.add("NOM_SOURCE");
341
        l.add("ID_COMMANDE");
342
        l.add("ID_BON_RECEPTION");
343
        l.add("QTE_RECUE");
344
        l.add("ID_ETAT_DEMANDE_ACHAT_ELEMENT");
345
        return l;
346
    }
347
 
348
    @Override
349
    protected void _initTableSource(SQLTableModelSource res) {
350
        final SQLTable foreignTable = getTable().getForeignTable("ID_ETAT_DEMANDE_ACHAT_ELEMENT");
351
        res.init();
352
        res.getColumn(foreignTable.getField("NOM")).setRenderer(new EtatDemandeAchatRenderer(foreignTable));
353
 
149 ilm 354
        res.getColumn(getTable().getField("P_HT")).setRenderer(new DeviseTableCellRenderer());
355
        res.getColumn(getTable().getField("T_HT")).setRenderer(new DeviseTableCellRenderer());
356
        res.getColumn(getTable().getField("QTE_UNITAIRE")).setRenderer(new DeviseTableCellRenderer());
144 ilm 357
 
149 ilm 358
        // BaseSQLTableModelColumn dpt = new BaseSQLTableModelColumn("Prix Total", BigDecimal.class)
359
        // {
360
        //
361
        // @Override
362
        // protected Object show_(SQLRowAccessor r) {
363
        //
364
        // if (r.getObject("ID_ARTICLE") != null && !r.isForeignEmpty("ID_ARTICLE")) {
365
        // SQLRowAccessor rA = r.getForeign("ID_ARTICLE");
366
        // return rA.getBigDecimal("PA_HT");
367
        // } else {
368
        // return null;
369
        // }
370
        // }
371
        //
372
        // @Override
373
        // public Set<FieldPath> getPaths() {
374
        // Path p = new Path(getTable());
375
        // final SQLTable clientT = getTable().getForeignTable("ID_ARTICLE");
376
        // p = p.add(clientT);
377
        // return CollectionUtils.createSet(new FieldPath(p, "PA_HT"));
378
        // }
379
        // };
380
        //
381
        // res.getColumns().add(dpt);
144 ilm 382
 
383
        super._initTableSource(res);
384
    }
385
 
386
    // @Override
387
    // public Set<String> getReadOnlyFields() {
388
    // Set<String> s = new HashSet<String>(super.getReadOnlyFields());
389
    // s.add("NUMERO_CARTE");
390
    // return s;
391
    // }
392
 
393
    @Override
394
    protected List<String> getComboFields() {
395
        final List<String> l = new ArrayList<String>();
149 ilm 396
        if (getTable().contains("ID_AFFAIRE")) {
397
            l.add("ID_AFFAIRE");
398
        }
399
        l.add("DATE");
400
        l.add("REVISION");
401
        l.add("CODE");
402
        l.add("NOM");
403
 
144 ilm 404
        return l;
405
    }
406
 
407
    @Override
408
    public ListMap<String, String> getShowAs() {
409
        return ListMap.singleton(null, "CODE", "NOM");
410
    }
411
 
412
    @Override
413
    public SQLComponent createComponent() {
414
        return new DemandeAchatItemSQLComponent(this);
415
    }
416
 
417
    @Override
418
    protected String createCode() {
156 ilm 419
        return createCodeOfPackage() + ".demande.achat";
144 ilm 420
    }
421
 
422
}