OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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
 *
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;
132 ilm 23
import org.openconcerto.erp.core.supplychain.receipt.component.BonReceptionSQLComponent;
24
import org.openconcerto.erp.generationDoc.gestcomm.CommandeXmlSheet;
25
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
174 ilm 26
import org.openconcerto.erp.preferences.GestionCommercialeGlobalPreferencePanel;
18 ilm 27
import org.openconcerto.sql.Configuration;
28
import org.openconcerto.sql.element.SQLComponent;
29
import org.openconcerto.sql.element.SQLElement;
83 ilm 30
import org.openconcerto.sql.element.TreesOfSQLRows;
31
import org.openconcerto.sql.model.SQLRow;
156 ilm 32
import org.openconcerto.sql.model.SQLRowAccessor;
94 ilm 33
import org.openconcerto.sql.model.SQLRowValues;
132 ilm 34
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
83 ilm 35
import org.openconcerto.sql.model.SQLSelect;
132 ilm 36
import org.openconcerto.sql.model.SQLSelectJoin;
37
import org.openconcerto.sql.model.SQLTable;
182 ilm 38
import org.openconcerto.sql.model.SQLTableEvent;
39
import org.openconcerto.sql.model.SQLTableEvent.Mode;
40
import org.openconcerto.sql.model.SQLTableModifiedListener;
83 ilm 41
import org.openconcerto.sql.model.Where;
174 ilm 42
import org.openconcerto.sql.preferences.SQLPreferences;
18 ilm 43
import org.openconcerto.sql.view.EditFrame;
156 ilm 44
import org.openconcerto.sql.view.EditPanel;
28 ilm 45
import org.openconcerto.sql.view.list.IListe;
46
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
156 ilm 47
import org.openconcerto.sql.view.list.RowAction;
28 ilm 48
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
182 ilm 49
import org.openconcerto.ui.PanelFrame;
94 ilm 50
import org.openconcerto.utils.ExceptionHandler;
132 ilm 51
import org.openconcerto.utils.cc.ITransformer;
18 ilm 52
 
182 ilm 53
import java.awt.GridBagConstraints;
54
import java.awt.GridBagLayout;
28 ilm 55
import java.awt.event.ActionEvent;
83 ilm 56
import java.sql.SQLException;
18 ilm 57
import java.util.ArrayList;
58
import java.util.List;
59
 
28 ilm 60
import javax.swing.AbstractAction;
18 ilm 61
import javax.swing.ImageIcon;
62
import javax.swing.JFrame;
174 ilm 63
import javax.swing.JOptionPane;
182 ilm 64
import javax.swing.JPanel;
65
import javax.swing.JScrollPane;
66
import javax.swing.JTable;
174 ilm 67
import javax.swing.SwingWorker;
18 ilm 68
 
83 ilm 69
import org.apache.commons.dbutils.handlers.ArrayListHandler;
70
 
18 ilm 71
public class CommandeSQLElement extends ComptaSQLConfElement {
72
 
73
    public CommandeSQLElement() {
74
        super("COMMANDE", "une commande fournisseur", "commandes fournisseur");
28 ilm 75
 
180 ilm 76
        if (getTable().contains("ATTACHMENTS")) {
77
            PredicateRowAction actionAttachment = new PredicateRowAction(new AttachmentAction().getAction(), true);
78
            actionAttachment.setPredicate(IListeEvent.getSingleSelectionPredicate());
79
            getRowActions().add(actionAttachment);
80
        }
81
 
182 ilm 82
        getRowActions().addAll(new MouseSheetXmlListeListener(this, CommandeXmlSheet.class).getRowActions());
132 ilm 83
 
84
        // Transfert vers BR
85
        PredicateRowAction bonAction = new PredicateRowAction(new AbstractAction() {
86
            public void actionPerformed(ActionEvent e) {
87
                final List<SQLRowValues> selectedRows = IListe.get(e).getSelectedRows();
88
                transfertBR(selectedRows);
89
            }
90
 
174 ilm 91
        }, true, "supplychain.order.create.receipt");
132 ilm 92
 
93
        bonAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
94
        getRowActions().add(bonAction);
95
 
174 ilm 96
        PredicateRowAction actionsTRFA = new PredicateRowAction(new AbstractAction("Transfert vers facture fournisseur") {
97
            public void actionPerformed(ActionEvent e) {
98
                final List<SQLRowValues> selectedRows = IListe.get(e).getSelectedRows();
99
                SwingWorker<Boolean, Object> worker = new SwingWorker<Boolean, Object>() {
100
                    @Override
101
                    protected Boolean doInBackground() throws Exception {
102
 
103
                        boolean b = TransfertBaseSQLComponent.isAlreadyAllTransfert(selectedRows, getTable(), getTable().getTable("FACTURE_FOURNISSEUR"), "T_HT", "T_HT");
104
 
105
                        if (b) {
106
                            String label = "Attention ";
107
                            if (selectedRows.size() > 1) {
108
                                label += " les " + getPluralName() + " ont déjà été transféré!";
109
                            } else {
110
                                label += getSingularName() + " a déjà été transféré!";
111
                            }
112
                            label += "\n Voulez vous continuer?";
113
 
114
                            int ans = JOptionPane.showConfirmDialog(null, label, "Transfert " + getSingularName(), JOptionPane.YES_NO_OPTION);
115
                            if (ans == JOptionPane.NO_OPTION) {
116
                                return Boolean.FALSE;
117
                            }
118
                        }
119
 
120
                        return Boolean.TRUE;
121
 
122
                    }
123
 
124
                    @Override
125
                    protected void done() {
126
                        try {
127
                            Boolean b = get();
128
                            if (b) {
129
                                TransfertBaseSQLComponent.openTransfertFrame(selectedRows, "FACTURE_FOURNISSEUR");
130
                            }
131
                        } catch (Exception e) {
132
                            ExceptionHandler.handle("Erreur lors du transfert des " + getPluralName() + "!", e);
133
                        }
134
                    }
135
                };
136
                worker.execute();
137
            }
138
        }, false);
139
        actionsTRFA.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
140
        getRowActions().add(actionsTRFA);
141
 
28 ilm 142
        // Transfert vers facture
67 ilm 143
        PredicateRowAction factureAction = new PredicateRowAction(new AbstractAction() {
28 ilm 144
            public void actionPerformed(ActionEvent e) {
145
 
146
                CommandeSQLElement.this.transfertFacture(IListe.get(e).getSelectedRow().getID());
147
            }
67 ilm 148
        }, false, "supplychain.order.create.purchase");
28 ilm 149
        factureAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
150
        getRowActions().add(factureAction);
151
 
156 ilm 152
        getRowActions().add(getCloneAction());
153
 
94 ilm 154
        PredicateRowAction tagValidAction = new PredicateRowAction(new AbstractAction() {
155
            public void actionPerformed(ActionEvent e) {
156
 
157
                final SQLRowValues asRowValues = IListe.get(e).getSelectedRow().asRow().createEmptyUpdateRow();
158
                asRowValues.put("EN_COURS", Boolean.FALSE);
159
                try {
160
                    asRowValues.commit();
161
                } catch (SQLException e1) {
162
                    ExceptionHandler.handle("Une erreur est survenue pour notifier la commande valider", e1);
163
                }
164
            }
165
        }, false, "supplychain.order.valid");
166
        tagValidAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
167
        getRowActions().add(tagValidAction);
182 ilm 168
 
169
        // Reliquat
170
        PredicateRowAction reliquatAction = new PredicateRowAction(new AbstractAction() {
171
            public void actionPerformed(ActionEvent e) {
172
                JPanel panelReliquat = new JPanel(new GridBagLayout());
173
                GridBagConstraints c = new GridBagConstraints();
174
                final ReliquatCommandeTableModel dm = new ReliquatCommandeTableModel(IListe.get(e).getSelectedRowAccessor());
175
                if (dm.getRowCount() > 0) {
176
                    JTable table = new JTable(dm);
177
                    JScrollPane comp = new JScrollPane(table);
178
                    c.weightx = 1;
179
                    c.weighty = 1;
180
                    c.fill = GridBagConstraints.BOTH;
181
                    panelReliquat.add(comp, c);
182
                    PanelFrame frame = new PanelFrame(panelReliquat, "Reliquat commande");
183
                    frame.pack();
184
                    frame.setVisible(true);
185
                } else {
186
                    JOptionPane.showMessageDialog(null, "Aucun reliquat restant");
187
                }
188
            }
189
 
190
        }, false, "sales.order.reliquat.show");
191
 
192
        reliquatAction.setPredicate(IListeEvent.getSingleSelectionPredicate());
193
        getRowActions().add(reliquatAction);
18 ilm 194
    }
195
 
156 ilm 196
    @Override
18 ilm 197
    protected List<String> getListFields() {
156 ilm 198
        final List<String> l = new ArrayList<>(8);
18 ilm 199
        l.add("NUMERO");
200
        l.add("NOM");
201
        l.add("DATE");
202
        l.add("ID_FOURNISSEUR");
174 ilm 203
        l.add("ID_COMMERCIAL");
28 ilm 204
        l.add("T_HT");
205
        l.add("T_TTC");
174 ilm 206
        Boolean useCommandeEnCours = SQLPreferences.getMemCached(getTable().getDBRoot()).getBoolean(GestionCommercialeGlobalPreferencePanel.COMMANDE_FOURNISSEUR_EN_COURS, false);
207
        if (useCommandeEnCours) {
208
            l.add("EN_COURS");
209
        }
18 ilm 210
        l.add("INFOS");
211
        return l;
212
    }
213
 
156 ilm 214
    @Override
18 ilm 215
    protected List<String> getComboFields() {
156 ilm 216
        final List<String> l = new ArrayList<>(3);
18 ilm 217
        l.add("NUMERO");
218
        l.add("NOM");
219
        l.add("DATE");
220
        return l;
221
    }
222
 
223
    /*
224
     * (non-Javadoc)
225
     *
226
     * @see org.openconcerto.devis.SQLElement#getComponent()
227
     */
228
    public SQLComponent createComponent() {
229
        return new CommandeSQLComponent();
230
    }
231
 
182 ilm 232
    private final List<SQLTableModifiedListener> listenerCmdInserted = new ArrayList<>();
233
 
234
    public void addInsertedCmdListener(SQLTableModifiedListener l) {
235
        this.listenerCmdInserted.add(l);
236
    }
237
 
238
    public void removeInsertedCmdListener(SQLTableModifiedListener l) {
239
        this.listenerCmdInserted.remove(l);
240
    }
241
 
242
    public void fireInsertedCmdListener(SQLRow row) {
243
        for (SQLTableModifiedListener sqlTableModifiedListener : this.listenerCmdInserted) {
244
            sqlTableModifiedListener.tableModified(new SQLTableEvent(row, Mode.ROW_ADDED, null));
245
        }
246
    }
247
 
132 ilm 248
    public void transfertBR(final List<SQLRowValues> selectedRows) {
249
 
250
        EditFrame f = TransfertBaseSQLComponent.openTransfertFrame(selectedRows, "BON_RECEPTION");
251
        BonReceptionSQLComponent comp = (BonReceptionSQLComponent) f.getSQLComponent();
252
        final SQLTable tableElt = comp.getElement().getTable().getTable("BON_RECEPTION_ELEMENT");
253
        SQLRowValues rowVals = new SQLRowValues(tableElt);
254
        rowVals.put("QTE_UNITAIRE", null);
255
        rowVals.put("QTE", null);
256
        rowVals.put("ID_ARTICLE", null);
257
 
258
        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowVals);
259
        fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
260
 
261
            @Override
262
            public SQLSelect transformChecked(SQLSelect input) {
156 ilm 263
                final List<Integer> ids = new ArrayList<>(selectedRows.size());
132 ilm 264
                for (SQLRowValues sqlRowValues : selectedRows) {
265
                    ids.add(sqlRowValues.getID());
266
                }
267
                SQLSelectJoin joinBR = input.addJoin("RIGHT", tableElt.getTable("BON_RECEPTION_ELEMENT").getField("ID_BON_RECEPTION"));
268
                SQLSelectJoin joinTR = input.addBackwardJoin("RIGHT", tableElt.getTable("TR_COMMANDE").getField("ID_BON_RECEPTION"), joinBR.getJoinedTable().getAlias());
269
                joinTR.setWhere(new Where(joinTR.getJoinedTable().getField("ID_COMMANDE"), ids));
270
                return input;
271
            }
272
        });
273
        comp.loadQuantity(fetcher.fetch());
182 ilm 274
        comp.removeZeroQtyLines();
132 ilm 275
    }
276
 
18 ilm 277
    /**
278
     * Transfert d'une commande en facture
279
     *
280
     * @param commandeID
281
     */
282
    public void transfertFacture(int commandeID) {
283
        SQLElement elt = Configuration.getInstance().getDirectory().getElement("SAISIE_ACHAT");
284
        EditFrame editFactureFrame = new EditFrame(elt);
285
        editFactureFrame.setIconImage(new ImageIcon(Gestion.class.getResource("frameicon.png")).getImage());
286
 
287
        SaisieAchatSQLComponent comp = (SaisieAchatSQLComponent) editFactureFrame.getSQLComponent();
288
        comp.loadCommande(commandeID);
289
 
290
        editFactureFrame.pack();
291
        editFactureFrame.setState(JFrame.NORMAL);
292
        editFactureFrame.setVisible(true);
293
    }
83 ilm 294
 
156 ilm 295
    public RowAction getCloneAction() {
296
        return new RowAction(new AbstractAction() {
297
 
298
            public void actionPerformed(ActionEvent e) {
299
                SQLRowAccessor selectedRow = IListe.get(e).getSelectedRow();
300
 
301
                SQLElement eltFact = Configuration.getInstance().getDirectory().getElement("COMMANDE");
302
                EditFrame editFrame = new EditFrame(eltFact, EditPanel.CREATION);
303
 
304
                ((CommandeSQLComponent) editFrame.getSQLComponent()).duplicate(selectedRow.getID());
305
                editFrame.setVisible(true);
306
            }
307
        }, true, "sales.quote.clone") {
308
            @Override
309
            public boolean enabledFor(java.util.List<org.openconcerto.sql.model.SQLRowValues> selection) {
310
                return (selection != null && selection.size() == 1);
311
            }
312
        };
313
    }
314
 
83 ilm 315
    @Override
90 ilm 316
    protected void archive(TreesOfSQLRows trees, boolean cutLinks) throws SQLException {
83 ilm 317
 
90 ilm 318
        for (SQLRow row : trees.getRows()) {
319
 
320
            // Mise à jour des stocks
321
            SQLElement eltMvtStock = Configuration.getInstance().getDirectory().getElement("MOUVEMENT_STOCK");
322
            SQLSelect sel = new SQLSelect();
323
            sel.addSelect(eltMvtStock.getTable().getField("ID"));
324
            Where w = new Where(eltMvtStock.getTable().getField("IDSOURCE"), "=", row.getID());
325
            Where w2 = new Where(eltMvtStock.getTable().getField("SOURCE"), "=", getTable().getName());
326
            sel.setWhere(w.and(w2));
327
 
328
            @SuppressWarnings("rawtypes")
329
            List l = (List) eltMvtStock.getTable().getBase().getDataSource().execute(sel.asString(), new ArrayListHandler());
330
            if (l != null) {
331
                for (int i = 0; i < l.size(); i++) {
332
                    Object[] tmp = (Object[]) l.get(i);
333
                    eltMvtStock.archive(((Number) tmp[0]).intValue());
334
                }
83 ilm 335
            }
336
        }
90 ilm 337
        super.archive(trees, cutLinks);
83 ilm 338
    }
339
 
156 ilm 340
    @Override
341
    protected String createCode() {
342
        return "supplychain.order";
343
    }
18 ilm 344
}