OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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
 *
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.receipt.element;
15
 
16
import org.openconcerto.erp.config.Gestion;
94 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.SaisieAchatSQLComponent;
20
import org.openconcerto.erp.core.supplychain.receipt.component.BonReceptionSQLComponent;
94 ilm 21
import org.openconcerto.erp.generationDoc.gestcomm.BonReceptionXmlSheet;
22
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
18 ilm 23
import org.openconcerto.sql.Configuration;
24
import org.openconcerto.sql.element.SQLComponent;
25
import org.openconcerto.sql.element.SQLElement;
90 ilm 26
import org.openconcerto.sql.element.TreesOfSQLRows;
142 ilm 27
import org.openconcerto.sql.model.AliasedTable;
28
import org.openconcerto.sql.model.SQLName;
18 ilm 29
import org.openconcerto.sql.model.SQLRow;
174 ilm 30
import org.openconcerto.sql.model.SQLRowValues;
18 ilm 31
import org.openconcerto.sql.model.SQLSelect;
142 ilm 32
import org.openconcerto.sql.model.SQLTable;
18 ilm 33
import org.openconcerto.sql.model.Where;
142 ilm 34
import org.openconcerto.sql.request.UpdateBuilder;
18 ilm 35
import org.openconcerto.sql.view.EditFrame;
94 ilm 36
import org.openconcerto.sql.view.list.IListe;
37
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
38
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
174 ilm 39
import org.openconcerto.utils.ExceptionHandler;
132 ilm 40
import org.openconcerto.utils.ListMap;
18 ilm 41
 
132 ilm 42
import java.awt.event.ActionEvent;
43
import java.sql.SQLException;
44
import java.util.ArrayList;
45
import java.util.List;
46
 
47
import javax.swing.AbstractAction;
48
import javax.swing.ImageIcon;
49
import javax.swing.JFrame;
174 ilm 50
import javax.swing.JOptionPane;
51
import javax.swing.SwingWorker;
132 ilm 52
 
53
import org.apache.commons.dbutils.handlers.ArrayListHandler;
182 ilm 54
import org.openconcerto.erp.core.supplychain.stock.element.StockItemsUpdater;
55
import org.openconcerto.erp.core.supplychain.stock.element.StockLabel;
56
import org.openconcerto.erp.core.supplychain.stock.element.StockItemsUpdater.TypeStockUpdate;
57
import org.openconcerto.sql.model.SQLRowAccessor;
132 ilm 58
 
94 ilm 59
public class BonReceptionSQLElement extends ComptaSQLConfElement {
18 ilm 60
 
94 ilm 61
    public BonReceptionSQLElement() {
156 ilm 62
        super("BON_RECEPTION", "un bon de réception", "bons de réception");
18 ilm 63
 
94 ilm 64
        PredicateRowAction actionsTRFA = new PredicateRowAction(new AbstractAction("Transfert vers facture fournisseur") {
65
            public void actionPerformed(ActionEvent e) {
174 ilm 66
                final List<SQLRowValues> selectedRows = IListe.get(e).getSelectedRows();
67
                SwingWorker<Boolean, Object> worker = new SwingWorker<Boolean, Object>() {
68
                    @Override
69
                    protected Boolean doInBackground() throws Exception {
70
 
71
                        boolean b = TransfertBaseSQLComponent.isAlreadyAllTransfert(selectedRows, getTable(), getTable().getTable("FACTURE_FOURNISSEUR"), "TOTAL_HT", "T_HT");
72
 
73
                        if (b) {
74
                            String label = "Attention ";
75
                            if (selectedRows.size() > 1) {
76
                                label += " les " + getPluralName() + " ont déjà été transféré!";
77
                            } else {
78
                                label += getSingularName() + " a déjà été transféré!";
79
                            }
80
                            label += "\n Voulez vous continuer?";
81
 
82
                            int ans = JOptionPane.showConfirmDialog(null, label, "Transfert " + getSingularName(), JOptionPane.YES_NO_OPTION);
83
                            if (ans == JOptionPane.NO_OPTION) {
84
                                return Boolean.FALSE;
85
                            }
86
                        }
87
 
88
                        return Boolean.TRUE;
89
 
90
                    }
91
 
92
                    @Override
93
                    protected void done() {
94
                        try {
95
                            Boolean b = get();
96
                            if (b) {
97
                                TransfertBaseSQLComponent.openTransfertFrame(selectedRows, "FACTURE_FOURNISSEUR");
98
                            }
99
                        } catch (Exception e) {
100
                            ExceptionHandler.handle("Erreur lors du transfert des " + getPluralName() + "!", e);
101
                        }
102
                    }
103
                };
104
                worker.execute();
94 ilm 105
            }
106
        }, true);
107
        actionsTRFA.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
18 ilm 108
 
94 ilm 109
        PredicateRowAction actionTRSimple = new PredicateRowAction(new AbstractAction("Transfert vers facture simple") {
110
            public void actionPerformed(ActionEvent e) {
111
                transfertFacture(IListe.get(e).getSelectedRow().getID());
112
            }
113
        }, false);
114
        actionTRSimple.setPredicate(IListeEvent.getSingleSelectionPredicate());
18 ilm 115
 
94 ilm 116
        getRowActions().add(actionsTRFA);
117
        getRowActions().add(actionTRSimple);
118
 
182 ilm 119
        MouseSheetXmlListeListener mouseSheetXmlListeListener = new MouseSheetXmlListeListener(this, BonReceptionXmlSheet.class);
94 ilm 120
        mouseSheetXmlListeListener.setGenerateHeader(true);
121
        mouseSheetXmlListeListener.setShowHeader(true);
122
        getRowActions().addAll(mouseSheetXmlListeListener.getRowActions());
18 ilm 123
    }
124
 
94 ilm 125
    @Override
132 ilm 126
    public ListMap<String, String> getShowAs() {
127
        return ListMap.singleton(null, "NUMERO", "DATE");
94 ilm 128
    }
129
 
156 ilm 130
    @Override
18 ilm 131
    protected List<String> getListFields() {
156 ilm 132
        final List<String> l = new ArrayList<>(5);
18 ilm 133
        l.add("NUMERO");
174 ilm 134
        l.add("NOM");
18 ilm 135
        l.add("DATE");
136
        l.add("ID_FOURNISSEUR");
94 ilm 137
        l.add("TOTAL_HT");
18 ilm 138
        l.add("INFOS");
139
        return l;
140
    }
141
 
156 ilm 142
    @Override
18 ilm 143
    protected List<String> getComboFields() {
156 ilm 144
        final List<String> l = new ArrayList<>(2);
18 ilm 145
        l.add("NUMERO");
146
        l.add("DATE");
147
        return l;
148
    }
149
 
150
    /*
151
     * (non-Javadoc)
152
     *
153
     * @see org.openconcerto.devis.SQLElement#getComponent()
154
     */
155
    public SQLComponent createComponent() {
156
        return new BonReceptionSQLComponent();
157
    }
158
 
159
    /**
160
     * Transfert d'un BR en facture
161
     *
162
     * @param brID
163
     */
164
    public void transfertFacture(int brID) {
165
 
166
        SQLElement elt = Configuration.getInstance().getDirectory().getElement("SAISIE_ACHAT");
167
        EditFrame editFactureFrame = new EditFrame(elt);
168
        editFactureFrame.setIconImage(new ImageIcon(Gestion.class.getResource("frameicon.png")).getImage());
169
 
170
        SaisieAchatSQLComponent comp = (SaisieAchatSQLComponent) editFactureFrame.getSQLComponent();
171
        comp.loadBonReception(brID);
172
        editFactureFrame.pack();
173
        editFactureFrame.setState(JFrame.NORMAL);
174
        editFactureFrame.setVisible(true);
175
    }
176
 
142 ilm 177
    public List<Object> getCmdFrom(int brOrigin) {
178
        SQLTable tableBRElement = getTable().getTable("BON_RECEPTION_ELEMENT");
179
        SQLTable tableCmdElement = getTable().getTable("COMMANDE_ELEMENT");
180
        String up = "SELECT DISTINCT c2.\"ID_COMMANDE\" FROM " + new SQLName(tableBRElement.getDBRoot().getName(), tableBRElement.getName()).quote() + " b2, "
181
                + new SQLName(tableCmdElement.getDBRoot().getName(), tableCmdElement.getName()).quote() + " c2 WHERE b2.\"ID_BON_RECEPTION\"=" + brOrigin
182
                + " AND c2.\"ARCHIVE\"=0 AND b2.\"ARCHIVE\"=0 AND c2.\"ID\">1 AND b2.\"ID\">1 AND b2.\"ID_COMMANDE_ELEMENT\"=c2.\"ID\"";
183
        List<Object> cmds = getTable().getDBSystemRoot().getDataSource().executeCol(up);
184
        return cmds;
185
    }
186
 
187
    public void updateCmdElement(List<Object> cmds, int idbrOrigin) {
188
        SQLTable tableBRElement = getTable().getTable("BON_RECEPTION_ELEMENT");
189
        SQLTable tableCmdElement = getTable().getTable("COMMANDE_ELEMENT");
190
        UpdateBuilder build = new UpdateBuilder(tableCmdElement);
191
        build.set("QTE_RECUE", "(SELECT SUM(b.\"QTE\" * b.\"QTE_UNITAIRE\") from " + new SQLName(tableBRElement.getDBRoot().getName(), tableBRElement.getName()).quote()
192
                + " b where c.\"ID\"=b.\"ID_COMMANDE_ELEMENT\" AND c.\"ID\">1 AND c.\"ARCHIVE\"=0 AND b.\"ID\">1 AND b.\"ARCHIVE\"=0 )");
193
        AliasedTable alias = new AliasedTable(tableCmdElement, "c");
194
        build.setWhere(new Where(alias.getField("ID_COMMANDE"), cmds));
195
        getTable().getDBSystemRoot().getDataSource().execute(build.asString().replaceAll(" SET", " c SET "));
196
    }
197
 
18 ilm 198
    @Override
90 ilm 199
    protected void archive(TreesOfSQLRows trees, boolean cutLinks) throws SQLException {
142 ilm 200
        List<Object> cmds = null;
156 ilm 201
        List<Integer> ids = new ArrayList<>();
90 ilm 202
        for (SQLRow row : trees.getRows()) {
142 ilm 203
            cmds = getCmdFrom(row.getID());
204
            ids.add(row.getID());
18 ilm 205
 
90 ilm 206
            // Mise à jour des stocks
207
            SQLElement eltMvtStock = Configuration.getInstance().getDirectory().getElement("MOUVEMENT_STOCK");
208
            SQLSelect sel = new SQLSelect(eltMvtStock.getTable().getBase());
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
            List l = (List) eltMvtStock.getTable().getBase().getDataSource().execute(sel.asString(), new ArrayListHandler());
215
            if (l != null) {
216
                for (int i = 0; i < l.size(); i++) {
217
                    Object[] tmp = (Object[]) l.get(i);
218
                    eltMvtStock.archive(((Number) tmp[0]).intValue());
219
                }
18 ilm 220
            }
221
        }
90 ilm 222
        super.archive(trees, cutLinks);
142 ilm 223
        for (Integer id : ids) {
224
 
225
            updateCmdElement(cmds, id);
226
        }
18 ilm 227
    }
156 ilm 228
 
229
    @Override
230
    protected String createCodeSuffix() {
231
        return ".note";
232
    }
182 ilm 233
 
234
    /**
235
     * Mise à jour des stocks pour chaque article composant du bon
236
     *
237
     * @throws SQLException
238
     */
239
    public void updateStock(int id) throws SQLException {
240
 
241
        SQLRow row = getTable().getRow(id);
242
        StockItemsUpdater stockUpdater = new StockItemsUpdater(new StockLabel() {
243
 
244
            @Override
245
            public String getLabel(SQLRowAccessor rowOrigin, SQLRowAccessor rowElt) {
246
 
247
                return getLibelleStock(rowOrigin, rowElt);
248
            }
249
        }, row, row.getReferentRows(getTable().getTable("BON_RECEPTION_ELEMENT")),
250
                getTable().contains("CREATE_VIRTUAL_STOCK") && row.getBoolean("CREATE_VIRTUAL_STOCK") ? TypeStockUpdate.REAL_VIRTUAL_RECEPT : TypeStockUpdate.REAL_RECEPT);
251
 
252
        if (getTable().getDBRoot().contains("RELIQUAT_BR")) {
253
            List<SQLRow> l = row.getReferentRows(getTable().getTable("RELIQUAT_BR").getField("ID_BON_RECEPTION_ORIGINE"));
254
            for (SQLRow sqlRow : l) {
255
                stockUpdater.addReliquat(sqlRow.getForeign("ID_ARTICLE"), sqlRow.getInt("QTE"), sqlRow.getBigDecimal("QTE_UNITAIRE"));
256
            }
257
        }
258
 
259
        stockUpdater.update();
260
 
261
    }
262
 
263
    public String getLibelleStock(SQLRowAccessor row, SQLRowAccessor rowElt) {
264
        return "Bon de réception N°" + row.getString("NUMERO");
265
    }
18 ilm 266
}