OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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