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.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;
54
 
94 ilm 55
public class BonReceptionSQLElement extends ComptaSQLConfElement {
18 ilm 56
 
94 ilm 57
    public BonReceptionSQLElement() {
156 ilm 58
        super("BON_RECEPTION", "un bon de réception", "bons de réception");
18 ilm 59
 
94 ilm 60
        PredicateRowAction actionsTRFA = new PredicateRowAction(new AbstractAction("Transfert vers facture fournisseur") {
61
            public void actionPerformed(ActionEvent e) {
174 ilm 62
                final List<SQLRowValues> selectedRows = IListe.get(e).getSelectedRows();
63
                SwingWorker<Boolean, Object> worker = new SwingWorker<Boolean, Object>() {
64
                    @Override
65
                    protected Boolean doInBackground() throws Exception {
66
 
67
                        boolean b = TransfertBaseSQLComponent.isAlreadyAllTransfert(selectedRows, getTable(), getTable().getTable("FACTURE_FOURNISSEUR"), "TOTAL_HT", "T_HT");
68
 
69
                        if (b) {
70
                            String label = "Attention ";
71
                            if (selectedRows.size() > 1) {
72
                                label += " les " + getPluralName() + " ont déjà été transféré!";
73
                            } else {
74
                                label += getSingularName() + " a déjà été transféré!";
75
                            }
76
                            label += "\n Voulez vous continuer?";
77
 
78
                            int ans = JOptionPane.showConfirmDialog(null, label, "Transfert " + getSingularName(), JOptionPane.YES_NO_OPTION);
79
                            if (ans == JOptionPane.NO_OPTION) {
80
                                return Boolean.FALSE;
81
                            }
82
                        }
83
 
84
                        return Boolean.TRUE;
85
 
86
                    }
87
 
88
                    @Override
89
                    protected void done() {
90
                        try {
91
                            Boolean b = get();
92
                            if (b) {
93
                                TransfertBaseSQLComponent.openTransfertFrame(selectedRows, "FACTURE_FOURNISSEUR");
94
                            }
95
                        } catch (Exception e) {
96
                            ExceptionHandler.handle("Erreur lors du transfert des " + getPluralName() + "!", e);
97
                        }
98
                    }
99
                };
100
                worker.execute();
94 ilm 101
            }
102
        }, true);
103
        actionsTRFA.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
18 ilm 104
 
94 ilm 105
        PredicateRowAction actionTRSimple = new PredicateRowAction(new AbstractAction("Transfert vers facture simple") {
106
            public void actionPerformed(ActionEvent e) {
107
                transfertFacture(IListe.get(e).getSelectedRow().getID());
108
            }
109
        }, false);
110
        actionTRSimple.setPredicate(IListeEvent.getSingleSelectionPredicate());
18 ilm 111
 
94 ilm 112
        getRowActions().add(actionsTRFA);
113
        getRowActions().add(actionTRSimple);
114
 
115
        MouseSheetXmlListeListener mouseSheetXmlListeListener = new MouseSheetXmlListeListener(BonReceptionXmlSheet.class);
116
        mouseSheetXmlListeListener.setGenerateHeader(true);
117
        mouseSheetXmlListeListener.setShowHeader(true);
118
        getRowActions().addAll(mouseSheetXmlListeListener.getRowActions());
18 ilm 119
    }
120
 
94 ilm 121
    @Override
132 ilm 122
    public ListMap<String, String> getShowAs() {
123
        return ListMap.singleton(null, "NUMERO", "DATE");
94 ilm 124
    }
125
 
156 ilm 126
    @Override
18 ilm 127
    protected List<String> getListFields() {
156 ilm 128
        final List<String> l = new ArrayList<>(5);
18 ilm 129
        l.add("NUMERO");
174 ilm 130
        l.add("NOM");
18 ilm 131
        l.add("DATE");
132
        l.add("ID_FOURNISSEUR");
94 ilm 133
        l.add("TOTAL_HT");
18 ilm 134
        l.add("INFOS");
135
        return l;
136
    }
137
 
156 ilm 138
    @Override
18 ilm 139
    protected List<String> getComboFields() {
156 ilm 140
        final List<String> l = new ArrayList<>(2);
18 ilm 141
        l.add("NUMERO");
142
        l.add("DATE");
143
        return l;
144
    }
145
 
146
    /*
147
     * (non-Javadoc)
148
     *
149
     * @see org.openconcerto.devis.SQLElement#getComponent()
150
     */
151
    public SQLComponent createComponent() {
152
        return new BonReceptionSQLComponent();
153
    }
154
 
155
    /**
156
     * Transfert d'un BR en facture
157
     *
158
     * @param brID
159
     */
160
    public void transfertFacture(int brID) {
161
 
162
        SQLElement elt = Configuration.getInstance().getDirectory().getElement("SAISIE_ACHAT");
163
        EditFrame editFactureFrame = new EditFrame(elt);
164
        editFactureFrame.setIconImage(new ImageIcon(Gestion.class.getResource("frameicon.png")).getImage());
165
 
166
        SaisieAchatSQLComponent comp = (SaisieAchatSQLComponent) editFactureFrame.getSQLComponent();
167
        comp.loadBonReception(brID);
168
        editFactureFrame.pack();
169
        editFactureFrame.setState(JFrame.NORMAL);
170
        editFactureFrame.setVisible(true);
171
    }
172
 
142 ilm 173
    public List<Object> getCmdFrom(int brOrigin) {
174
        SQLTable tableBRElement = getTable().getTable("BON_RECEPTION_ELEMENT");
175
        SQLTable tableCmdElement = getTable().getTable("COMMANDE_ELEMENT");
176
        String up = "SELECT DISTINCT c2.\"ID_COMMANDE\" FROM " + new SQLName(tableBRElement.getDBRoot().getName(), tableBRElement.getName()).quote() + " b2, "
177
                + new SQLName(tableCmdElement.getDBRoot().getName(), tableCmdElement.getName()).quote() + " c2 WHERE b2.\"ID_BON_RECEPTION\"=" + brOrigin
178
                + " AND c2.\"ARCHIVE\"=0 AND b2.\"ARCHIVE\"=0 AND c2.\"ID\">1 AND b2.\"ID\">1 AND b2.\"ID_COMMANDE_ELEMENT\"=c2.\"ID\"";
179
        List<Object> cmds = getTable().getDBSystemRoot().getDataSource().executeCol(up);
180
        return cmds;
181
    }
182
 
183
    public void updateCmdElement(List<Object> cmds, int idbrOrigin) {
184
        SQLTable tableBRElement = getTable().getTable("BON_RECEPTION_ELEMENT");
185
        SQLTable tableCmdElement = getTable().getTable("COMMANDE_ELEMENT");
186
        UpdateBuilder build = new UpdateBuilder(tableCmdElement);
187
        build.set("QTE_RECUE", "(SELECT SUM(b.\"QTE\" * b.\"QTE_UNITAIRE\") from " + new SQLName(tableBRElement.getDBRoot().getName(), tableBRElement.getName()).quote()
188
                + " b where c.\"ID\"=b.\"ID_COMMANDE_ELEMENT\" AND c.\"ID\">1 AND c.\"ARCHIVE\"=0 AND b.\"ID\">1 AND b.\"ARCHIVE\"=0 )");
189
        AliasedTable alias = new AliasedTable(tableCmdElement, "c");
190
        build.setWhere(new Where(alias.getField("ID_COMMANDE"), cmds));
191
        getTable().getDBSystemRoot().getDataSource().execute(build.asString().replaceAll(" SET", " c SET "));
192
    }
193
 
18 ilm 194
    @Override
90 ilm 195
    protected void archive(TreesOfSQLRows trees, boolean cutLinks) throws SQLException {
142 ilm 196
        List<Object> cmds = null;
156 ilm 197
        List<Integer> ids = new ArrayList<>();
90 ilm 198
        for (SQLRow row : trees.getRows()) {
142 ilm 199
            cmds = getCmdFrom(row.getID());
200
            ids.add(row.getID());
18 ilm 201
 
90 ilm 202
            // Mise à jour des stocks
203
            SQLElement eltMvtStock = Configuration.getInstance().getDirectory().getElement("MOUVEMENT_STOCK");
204
            SQLSelect sel = new SQLSelect(eltMvtStock.getTable().getBase());
205
            sel.addSelect(eltMvtStock.getTable().getField("ID"));
206
            Where w = new Where(eltMvtStock.getTable().getField("IDSOURCE"), "=", row.getID());
207
            Where w2 = new Where(eltMvtStock.getTable().getField("SOURCE"), "=", getTable().getName());
208
            sel.setWhere(w.and(w2));
209
 
210
            List l = (List) eltMvtStock.getTable().getBase().getDataSource().execute(sel.asString(), new ArrayListHandler());
211
            if (l != null) {
212
                for (int i = 0; i < l.size(); i++) {
213
                    Object[] tmp = (Object[]) l.get(i);
214
                    eltMvtStock.archive(((Number) tmp[0]).intValue());
215
                }
18 ilm 216
            }
217
        }
90 ilm 218
        super.archive(trees, cutLinks);
142 ilm 219
        for (Integer id : ids) {
220
 
221
            updateCmdElement(cmds, id);
222
        }
18 ilm 223
    }
156 ilm 224
 
225
    @Override
226
    protected String createCodeSuffix() {
227
        return ".note";
228
    }
18 ilm 229
}