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