OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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