OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 182 | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
18 ilm 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.supplychain.order.element;
15
 
16
import org.openconcerto.erp.config.Gestion;
132 ilm 17
import org.openconcerto.erp.core.common.component.TransfertBaseSQLComponent;
18 ilm 18
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
180 ilm 19
import org.openconcerto.erp.core.edm.AttachmentAction;
182 ilm 20
import org.openconcerto.erp.core.sales.order.ui.ReliquatCommandeTableModel;
18 ilm 21
import org.openconcerto.erp.core.supplychain.order.component.CommandeSQLComponent;
22
import org.openconcerto.erp.core.supplychain.order.component.SaisieAchatSQLComponent;
185 ilm 23
import org.openconcerto.erp.core.supplychain.order.ui.EtatCommandeFournisseur;
24
import org.openconcerto.erp.core.supplychain.order.ui.EtatCommandeRenderer;
132 ilm 25
import org.openconcerto.erp.core.supplychain.receipt.component.BonReceptionSQLComponent;
26
import org.openconcerto.erp.generationDoc.gestcomm.CommandeXmlSheet;
27
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
174 ilm 28
import org.openconcerto.erp.preferences.GestionCommercialeGlobalPreferencePanel;
18 ilm 29
import org.openconcerto.sql.Configuration;
30
import org.openconcerto.sql.element.SQLComponent;
31
import org.openconcerto.sql.element.SQLElement;
83 ilm 32
import org.openconcerto.sql.element.TreesOfSQLRows;
33
import org.openconcerto.sql.model.SQLRow;
156 ilm 34
import org.openconcerto.sql.model.SQLRowAccessor;
94 ilm 35
import org.openconcerto.sql.model.SQLRowValues;
132 ilm 36
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
83 ilm 37
import org.openconcerto.sql.model.SQLSelect;
132 ilm 38
import org.openconcerto.sql.model.SQLSelectJoin;
39
import org.openconcerto.sql.model.SQLTable;
182 ilm 40
import org.openconcerto.sql.model.SQLTableEvent;
41
import org.openconcerto.sql.model.SQLTableEvent.Mode;
42
import org.openconcerto.sql.model.SQLTableModifiedListener;
83 ilm 43
import org.openconcerto.sql.model.Where;
174 ilm 44
import org.openconcerto.sql.preferences.SQLPreferences;
185 ilm 45
import org.openconcerto.sql.request.UpdateBuilder;
18 ilm 46
import org.openconcerto.sql.view.EditFrame;
156 ilm 47
import org.openconcerto.sql.view.EditPanel;
185 ilm 48
import org.openconcerto.sql.view.EditPanelListener;
28 ilm 49
import org.openconcerto.sql.view.list.IListe;
50
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
156 ilm 51
import org.openconcerto.sql.view.list.RowAction;
28 ilm 52
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
185 ilm 53
import org.openconcerto.sql.view.list.SQLTableModelColumn;
54
import org.openconcerto.sql.view.list.SQLTableModelSource;
182 ilm 55
import org.openconcerto.ui.PanelFrame;
94 ilm 56
import org.openconcerto.utils.ExceptionHandler;
132 ilm 57
import org.openconcerto.utils.cc.ITransformer;
18 ilm 58
 
185 ilm 59
import java.awt.Component;
182 ilm 60
import java.awt.GridBagConstraints;
61
import java.awt.GridBagLayout;
28 ilm 62
import java.awt.event.ActionEvent;
83 ilm 63
import java.sql.SQLException;
18 ilm 64
import java.util.ArrayList;
185 ilm 65
import java.util.Arrays;
18 ilm 66
import java.util.List;
67
 
28 ilm 68
import javax.swing.AbstractAction;
18 ilm 69
import javax.swing.ImageIcon;
70
import javax.swing.JFrame;
185 ilm 71
import javax.swing.JLabel;
174 ilm 72
import javax.swing.JOptionPane;
182 ilm 73
import javax.swing.JPanel;
74
import javax.swing.JScrollPane;
75
import javax.swing.JTable;
174 ilm 76
import javax.swing.SwingWorker;
185 ilm 77
import javax.swing.table.DefaultTableCellRenderer;
18 ilm 78
 
83 ilm 79
import org.apache.commons.dbutils.handlers.ArrayListHandler;
80
 
18 ilm 81
public class CommandeSQLElement extends ComptaSQLConfElement {
82
 
83
    public CommandeSQLElement() {
84
        super("COMMANDE", "une commande fournisseur", "commandes fournisseur");
28 ilm 85
 
180 ilm 86
        if (getTable().contains("ATTACHMENTS")) {
87
            PredicateRowAction actionAttachment = new PredicateRowAction(new AttachmentAction().getAction(), true);
88
            actionAttachment.setPredicate(IListeEvent.getSingleSelectionPredicate());
89
            getRowActions().add(actionAttachment);
90
        }
91
 
185 ilm 92
        for (final EtatCommandeFournisseur etat : EtatCommandeFournisseur.values()) {
93
 
94
            PredicateRowAction action = new PredicateRowAction(new AbstractAction(etat.getTranslation()) {
95
 
96
                @Override
97
                public void actionPerformed(ActionEvent e) {
98
                    changeStateOfRows(IListe.get(e).getSelectedRows(), etat);
99
                }
100
            }, false);
101
            action.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
102
            action.setPath(Arrays.asList("Etat", "Etat", "Etat"));
103
            getRowActions().add(action);
104
        }
105
 
182 ilm 106
        getRowActions().addAll(new MouseSheetXmlListeListener(this, CommandeXmlSheet.class).getRowActions());
132 ilm 107
 
108
        // Transfert vers BR
109
        PredicateRowAction bonAction = new PredicateRowAction(new AbstractAction() {
110
            public void actionPerformed(ActionEvent e) {
111
                final List<SQLRowValues> selectedRows = IListe.get(e).getSelectedRows();
112
                transfertBR(selectedRows);
113
            }
114
 
174 ilm 115
        }, true, "supplychain.order.create.receipt");
132 ilm 116
 
117
        bonAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
118
        getRowActions().add(bonAction);
119
 
174 ilm 120
        PredicateRowAction actionsTRFA = new PredicateRowAction(new AbstractAction("Transfert vers facture fournisseur") {
121
            public void actionPerformed(ActionEvent e) {
122
                final List<SQLRowValues> selectedRows = IListe.get(e).getSelectedRows();
123
                SwingWorker<Boolean, Object> worker = new SwingWorker<Boolean, Object>() {
124
                    @Override
125
                    protected Boolean doInBackground() throws Exception {
126
 
127
                        boolean b = TransfertBaseSQLComponent.isAlreadyAllTransfert(selectedRows, getTable(), getTable().getTable("FACTURE_FOURNISSEUR"), "T_HT", "T_HT");
128
 
129
                        if (b) {
130
                            String label = "Attention ";
131
                            if (selectedRows.size() > 1) {
132
                                label += " les " + getPluralName() + " ont déjà été transféré!";
133
                            } else {
134
                                label += getSingularName() + " a déjà été transféré!";
135
                            }
136
                            label += "\n Voulez vous continuer?";
137
 
138
                            int ans = JOptionPane.showConfirmDialog(null, label, "Transfert " + getSingularName(), JOptionPane.YES_NO_OPTION);
139
                            if (ans == JOptionPane.NO_OPTION) {
140
                                return Boolean.FALSE;
141
                            }
142
                        }
143
 
144
                        return Boolean.TRUE;
145
 
146
                    }
147
 
148
                    @Override
149
                    protected void done() {
150
                        try {
151
                            Boolean b = get();
152
                            if (b) {
153
                                TransfertBaseSQLComponent.openTransfertFrame(selectedRows, "FACTURE_FOURNISSEUR");
154
                            }
155
                        } catch (Exception e) {
156
                            ExceptionHandler.handle("Erreur lors du transfert des " + getPluralName() + "!", e);
157
                        }
158
                    }
159
                };
160
                worker.execute();
161
            }
162
        }, false);
163
        actionsTRFA.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
164
        getRowActions().add(actionsTRFA);
165
 
28 ilm 166
        // Transfert vers facture
67 ilm 167
        PredicateRowAction factureAction = new PredicateRowAction(new AbstractAction() {
28 ilm 168
            public void actionPerformed(ActionEvent e) {
169
 
170
                CommandeSQLElement.this.transfertFacture(IListe.get(e).getSelectedRow().getID());
171
            }
67 ilm 172
        }, false, "supplychain.order.create.purchase");
28 ilm 173
        factureAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
174
        getRowActions().add(factureAction);
175
 
156 ilm 176
        getRowActions().add(getCloneAction());
177
 
94 ilm 178
        PredicateRowAction tagValidAction = new PredicateRowAction(new AbstractAction() {
179
            public void actionPerformed(ActionEvent e) {
180
 
181
                final SQLRowValues asRowValues = IListe.get(e).getSelectedRow().asRow().createEmptyUpdateRow();
182
                asRowValues.put("EN_COURS", Boolean.FALSE);
183
                try {
184
                    asRowValues.commit();
185
                } catch (SQLException e1) {
186
                    ExceptionHandler.handle("Une erreur est survenue pour notifier la commande valider", e1);
187
                }
188
            }
189
        }, false, "supplychain.order.valid");
190
        tagValidAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
191
        getRowActions().add(tagValidAction);
182 ilm 192
 
193
        // Reliquat
194
        PredicateRowAction reliquatAction = new PredicateRowAction(new AbstractAction() {
195
            public void actionPerformed(ActionEvent e) {
196
                JPanel panelReliquat = new JPanel(new GridBagLayout());
197
                GridBagConstraints c = new GridBagConstraints();
198
                final ReliquatCommandeTableModel dm = new ReliquatCommandeTableModel(IListe.get(e).getSelectedRowAccessor());
199
                if (dm.getRowCount() > 0) {
200
                    JTable table = new JTable(dm);
201
                    JScrollPane comp = new JScrollPane(table);
202
                    c.weightx = 1;
203
                    c.weighty = 1;
204
                    c.fill = GridBagConstraints.BOTH;
205
                    panelReliquat.add(comp, c);
206
                    PanelFrame frame = new PanelFrame(panelReliquat, "Reliquat commande");
207
                    frame.pack();
208
                    frame.setVisible(true);
209
                } else {
210
                    JOptionPane.showMessageDialog(null, "Aucun reliquat restant");
211
                }
212
            }
213
 
214
        }, false, "sales.order.reliquat.show");
215
 
216
        reliquatAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
217
        getRowActions().add(reliquatAction);
18 ilm 218
    }
219
 
156 ilm 220
    @Override
18 ilm 221
    protected List<String> getListFields() {
156 ilm 222
        final List<String> l = new ArrayList<>(8);
18 ilm 223
        l.add("NUMERO");
224
        l.add("NOM");
225
        l.add("DATE");
226
        l.add("ID_FOURNISSEUR");
174 ilm 227
        l.add("ID_COMMERCIAL");
28 ilm 228
        l.add("T_HT");
229
        l.add("T_TTC");
174 ilm 230
        Boolean useCommandeEnCours = SQLPreferences.getMemCached(getTable().getDBRoot()).getBoolean(GestionCommercialeGlobalPreferencePanel.COMMANDE_FOURNISSEUR_EN_COURS, false);
231
        if (useCommandeEnCours) {
232
            l.add("EN_COURS");
233
        }
185 ilm 234
        l.add("ETAT_COMMANDE");
18 ilm 235
        l.add("INFOS");
236
        return l;
237
    }
238
 
156 ilm 239
    @Override
18 ilm 240
    protected List<String> getComboFields() {
156 ilm 241
        final List<String> l = new ArrayList<>(3);
18 ilm 242
        l.add("NUMERO");
243
        l.add("NOM");
244
        l.add("DATE");
245
        return l;
246
    }
247
 
185 ilm 248
    @Override
249
    protected void _initTableSource(SQLTableModelSource res) {
250
        // TODO Auto-generated method stub
251
        super._initTableSource(res);
252
        SQLTableModelColumn col = res.getColumn(getTable().getField("ETAT_COMMANDE"));
253
        if (col != null) {
254
            col.setRenderer(new EtatCommandeRenderer());
255
 
256
        }
257
 
258
    }
259
 
18 ilm 260
    /*
261
     * (non-Javadoc)
262
     *
263
     * @see org.openconcerto.devis.SQLElement#getComponent()
264
     */
265
    public SQLComponent createComponent() {
266
        return new CommandeSQLComponent();
267
    }
268
 
182 ilm 269
    private final List<SQLTableModifiedListener> listenerCmdInserted = new ArrayList<>();
270
 
271
    public void addInsertedCmdListener(SQLTableModifiedListener l) {
272
        this.listenerCmdInserted.add(l);
273
    }
274
 
275
    public void removeInsertedCmdListener(SQLTableModifiedListener l) {
276
        this.listenerCmdInserted.remove(l);
277
    }
278
 
279
    public void fireInsertedCmdListener(SQLRow row) {
280
        for (SQLTableModifiedListener sqlTableModifiedListener : this.listenerCmdInserted) {
281
            sqlTableModifiedListener.tableModified(new SQLTableEvent(row, Mode.ROW_ADDED, null));
282
        }
283
    }
284
 
132 ilm 285
    public void transfertBR(final List<SQLRowValues> selectedRows) {
286
 
287
        EditFrame f = TransfertBaseSQLComponent.openTransfertFrame(selectedRows, "BON_RECEPTION");
288
        BonReceptionSQLComponent comp = (BonReceptionSQLComponent) f.getSQLComponent();
289
        final SQLTable tableElt = comp.getElement().getTable().getTable("BON_RECEPTION_ELEMENT");
290
        SQLRowValues rowVals = new SQLRowValues(tableElt);
291
        rowVals.put("QTE_UNITAIRE", null);
292
        rowVals.put("QTE", null);
293
        rowVals.put("ID_ARTICLE", null);
294
 
295
        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowVals);
296
        fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
297
 
298
            @Override
299
            public SQLSelect transformChecked(SQLSelect input) {
156 ilm 300
                final List<Integer> ids = new ArrayList<>(selectedRows.size());
132 ilm 301
                for (SQLRowValues sqlRowValues : selectedRows) {
302
                    ids.add(sqlRowValues.getID());
303
                }
304
                SQLSelectJoin joinBR = input.addJoin("RIGHT", tableElt.getTable("BON_RECEPTION_ELEMENT").getField("ID_BON_RECEPTION"));
305
                SQLSelectJoin joinTR = input.addBackwardJoin("RIGHT", tableElt.getTable("TR_COMMANDE").getField("ID_BON_RECEPTION"), joinBR.getJoinedTable().getAlias());
306
                joinTR.setWhere(new Where(joinTR.getJoinedTable().getField("ID_COMMANDE"), ids));
307
                return input;
308
            }
309
        });
310
        comp.loadQuantity(fetcher.fetch());
182 ilm 311
        comp.removeZeroQtyLines();
132 ilm 312
    }
313
 
18 ilm 314
    /**
315
     * Transfert d'une commande en facture
316
     *
317
     * @param commandeID
318
     */
319
    public void transfertFacture(int commandeID) {
320
        SQLElement elt = Configuration.getInstance().getDirectory().getElement("SAISIE_ACHAT");
321
        EditFrame editFactureFrame = new EditFrame(elt);
322
        editFactureFrame.setIconImage(new ImageIcon(Gestion.class.getResource("frameicon.png")).getImage());
323
 
324
        SaisieAchatSQLComponent comp = (SaisieAchatSQLComponent) editFactureFrame.getSQLComponent();
325
        comp.loadCommande(commandeID);
326
 
327
        editFactureFrame.pack();
328
        editFactureFrame.setState(JFrame.NORMAL);
329
        editFactureFrame.setVisible(true);
185 ilm 330
        editFactureFrame.addEditPanelListener(new EditPanelListener() {
331
 
332
            @Override
333
            public void modified() {
334
 
335
            }
336
 
337
            @Override
338
            public void inserted(int id) {
339
                try {
340
                    SQLRowValues rowValsTR = new SQLRowValues(getTable().getTable("TR_COMMANDE"));
341
                    rowValsTR.put("ID_COMMANDE", commandeID);
342
                    rowValsTR.put("ID_SAISIE_ACHAT", id);
343
                    rowValsTR.commit();
344
                } catch (SQLException e) {
345
                    ExceptionHandler.handle("Erreur lors de l'insertion de la ligne de transfert", e);
346
                }
347
            }
348
 
349
            @Override
350
            public void deleted() {
351
            }
352
 
353
            @Override
354
            public void cancelled() {
355
            }
356
        });
18 ilm 357
    }
83 ilm 358
 
156 ilm 359
    public RowAction getCloneAction() {
360
        return new RowAction(new AbstractAction() {
361
 
362
            public void actionPerformed(ActionEvent e) {
363
                SQLRowAccessor selectedRow = IListe.get(e).getSelectedRow();
364
 
365
                SQLElement eltFact = Configuration.getInstance().getDirectory().getElement("COMMANDE");
366
                EditFrame editFrame = new EditFrame(eltFact, EditPanel.CREATION);
367
 
368
                ((CommandeSQLComponent) editFrame.getSQLComponent()).duplicate(selectedRow.getID());
369
                editFrame.setVisible(true);
370
            }
371
        }, true, "sales.quote.clone") {
372
            @Override
373
            public boolean enabledFor(java.util.List<org.openconcerto.sql.model.SQLRowValues> selection) {
374
                return (selection != null && selection.size() == 1);
375
            }
376
        };
377
    }
378
 
185 ilm 379
    private void changeStateOfRows(List<SQLRowValues> l, EtatCommandeFournisseur etat) {
380
 
381
        List<Integer> ids = new ArrayList<Integer>(l.size());
382
        for (SQLRowValues sqlRowValues : l) {
383
            ids.add(sqlRowValues.getID());
384
        }
385
 
386
        UpdateBuilder builder = new UpdateBuilder(getTable());
387
        builder.setObject("ETAT_COMMANDE", etat.getId());
388
        builder.setWhere(Where.inValues(getTable().getKey(), ids));
389
 
390
        getTable().getDBSystemRoot().getDataSource().execute(builder.asString());
391
        getTable().fireTableModified(-1);
392
 
393
        if (etat == EtatCommandeFournisseur.ANNULEE) {
394
 
395
            // Suppression des stocks si commande annulée
396
            SQLElement eltMvtStock = getDirectory().getElement("MOUVEMENT_STOCK");
397
            SQLSelect sel = new SQLSelect();
398
            sel.addSelect(eltMvtStock.getTable().getField("ID"));
399
            Where w = Where.inValues(eltMvtStock.getTable().getField("IDSOURCE"), ids);
400
            Where w2 = new Where(eltMvtStock.getTable().getField("SOURCE"), "=", getTable().getName());
401
            sel.setWhere(w.and(w2));
402
 
403
            try {
404
                @SuppressWarnings("rawtypes")
405
                List res = (List) eltMvtStock.getTable().getBase().getDataSource().execute(sel.asString(), new ArrayListHandler());
406
                if (res != null) {
407
                    for (int i = 0; i < res.size(); i++) {
408
                        Object[] tmp = (Object[]) res.get(i);
409
                        eltMvtStock.archive(((Number) tmp[0]).intValue());
410
                    }
411
                }
412
 
413
                final SQLTable tableCmdItem = getTable().getTable("COMMANDE_ELEMENT");
414
                UpdateBuilder build = new UpdateBuilder(tableCmdItem);
415
                build.setObject("RECU_FORCED", Boolean.TRUE);
416
                build.setWhere(Where.inValues(tableCmdItem.getField("ID_COMMANDE"), ids));
417
                getTable().getDBSystemRoot().getDataSource().execute(build.asString());
418
 
419
            } catch (SQLException e) {
420
                ExceptionHandler.handle("Erreur lors de la suppression des mouvements de stocks", e);
421
            }
422
        }
423
    }
424
 
83 ilm 425
    @Override
90 ilm 426
    protected void archive(TreesOfSQLRows trees, boolean cutLinks) throws SQLException {
83 ilm 427
 
90 ilm 428
        for (SQLRow row : trees.getRows()) {
429
 
430
            // Mise à jour des stocks
431
            SQLElement eltMvtStock = Configuration.getInstance().getDirectory().getElement("MOUVEMENT_STOCK");
432
            SQLSelect sel = new SQLSelect();
433
            sel.addSelect(eltMvtStock.getTable().getField("ID"));
434
            Where w = new Where(eltMvtStock.getTable().getField("IDSOURCE"), "=", row.getID());
435
            Where w2 = new Where(eltMvtStock.getTable().getField("SOURCE"), "=", getTable().getName());
436
            sel.setWhere(w.and(w2));
437
 
438
            @SuppressWarnings("rawtypes")
439
            List l = (List) eltMvtStock.getTable().getBase().getDataSource().execute(sel.asString(), new ArrayListHandler());
440
            if (l != null) {
441
                for (int i = 0; i < l.size(); i++) {
442
                    Object[] tmp = (Object[]) l.get(i);
443
                    eltMvtStock.archive(((Number) tmp[0]).intValue());
444
                }
83 ilm 445
            }
446
        }
90 ilm 447
        super.archive(trees, cutLinks);
83 ilm 448
    }
449
 
156 ilm 450
    @Override
451
    protected String createCode() {
452
        return "supplychain.order";
453
    }
18 ilm 454
}