OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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