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.sales.shipment.element;
15
 
16
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
17
import org.openconcerto.erp.core.sales.shipment.component.BonDeLivraisonSQLComponent;
18
import org.openconcerto.erp.preferences.DefaultNXProps;
19
import org.openconcerto.sql.Configuration;
20
import org.openconcerto.sql.element.SQLComponent;
21
import org.openconcerto.sql.element.SQLElement;
142 ilm 22
import org.openconcerto.sql.element.SQLElementLink.LinkType;
132 ilm 23
import org.openconcerto.sql.element.SQLElementLinksSetup;
90 ilm 24
import org.openconcerto.sql.element.TreesOfSQLRows;
142 ilm 25
import org.openconcerto.sql.model.AliasedTable;
26
import org.openconcerto.sql.model.SQLName;
61 ilm 27
import org.openconcerto.sql.model.SQLRow;
156 ilm 28
import org.openconcerto.sql.model.SQLRowAccessor;
61 ilm 29
import org.openconcerto.sql.model.SQLSelect;
142 ilm 30
import org.openconcerto.sql.model.SQLTable;
182 ilm 31
import org.openconcerto.sql.model.SQLTableEvent;
32
import org.openconcerto.sql.model.SQLTableEvent.Mode;
33
import org.openconcerto.sql.model.SQLTableModifiedListener;
61 ilm 34
import org.openconcerto.sql.model.Where;
182 ilm 35
import org.openconcerto.sql.request.ListSQLRequest;
142 ilm 36
import org.openconcerto.sql.request.UpdateBuilder;
156 ilm 37
import org.openconcerto.sql.view.EditFrame;
38
import org.openconcerto.sql.view.EditPanel;
39
import org.openconcerto.sql.view.list.IListe;
40
import org.openconcerto.sql.view.list.RowAction;
182 ilm 41
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
42
import org.openconcerto.sql.view.list.action.ListEvent;
18 ilm 43
import org.openconcerto.ui.preferences.DefaultProps;
142 ilm 44
import org.openconcerto.utils.ListMap;
18 ilm 45
 
156 ilm 46
import java.awt.event.ActionEvent;
61 ilm 47
import java.sql.SQLException;
18 ilm 48
import java.util.ArrayList;
182 ilm 49
import java.util.Arrays;
18 ilm 50
import java.util.List;
51
 
156 ilm 52
import javax.swing.AbstractAction;
53
 
61 ilm 54
import org.apache.commons.dbutils.handlers.ArrayListHandler;
55
 
18 ilm 56
public class BonDeLivraisonSQLElement extends ComptaSQLConfElement {
57
 
58
    // TODO afficher uniquement les factures non livrees dans la combo
59
    // MAYBE mettre un niceCellRenderer dans les rowValuesTable
60
 
90 ilm 61
    public BonDeLivraisonSQLElement(String single, String plural) {
62
        super("BON_DE_LIVRAISON", single, plural);
182 ilm 63
        // Verrou facture
64
        if (getTable().contains("VERROU_FACTURATION")) {
65
            PredicateRowAction lock = new PredicateRowAction(new AbstractAction("Verrouiller") {
66
                @Override
67
                public void actionPerformed(ActionEvent e) {
68
                    updateVerrouFacture(IListe.get(e).getSelectedRowAccessors(), true);
69
                }
70
            }, false);
71
            lock.setPredicate(ListEvent.getNonEmptySelectionPredicate());
72
            lock.setPath(Arrays.asList("Verrou facturaction", "Verrou facturaction", "Verrou facturaction"));
73
            PredicateRowAction unlock = new PredicateRowAction(new AbstractAction("Déverrouiller") {
74
                @Override
75
                public void actionPerformed(ActionEvent e) {
76
                    updateVerrouFacture(IListe.get(e).getSelectedRowAccessors(), false);
77
                }
78
            }, false);
79
            unlock.setPath(Arrays.asList("Verrou facturaction", "Verrou facturaction", "Verrou facturaction"));
80
            unlock.setPredicate(ListEvent.getNonEmptySelectionPredicate());
81
            getRowActions().add(lock);
82
            getRowActions().add(unlock);
83
        }
84
 
90 ilm 85
    }
86
 
182 ilm 87
    @Override
88
    protected void _initListRequest(ListSQLRequest req) {
89
        super._initListRequest(req);
90
        req.addToGraphToFetch("VERROU_FACTURATION");
91
    }
92
 
93
    private void updateVerrouFacture(List<SQLRowAccessor> rows, boolean state) {
94
        UpdateBuilder builder = new UpdateBuilder(getTable());
95
        builder.setObject("VERROU_FACTURATION", state);
96
        builder.setWhere(Where.inValues(getTable().getKey(), SQLRow.getIDs(rows)));
97
        getTable().getDBSystemRoot().getDataSource().execute(builder.asString());
98
        getTable().fireTableModified();
99
    }
100
 
18 ilm 101
    public BonDeLivraisonSQLElement() {
156 ilm 102
        this("un bon de livraison", "bons de livraison");
18 ilm 103
    }
104
 
132 ilm 105
    @Override
106
    protected void setupLinks(SQLElementLinksSetup links) {
107
        super.setupLinks(links);
108
        if (getTable().contains("ID_ADRESSE")) {
109
            links.get("ID_ADRESSE").setType(LinkType.ASSOCIATION);
110
        }
111
        if (getTable().contains("ID_ADRESSE_LIVRAISON")) {
112
            links.get("ID_ADRESSE_LIVRAISON").setType(LinkType.ASSOCIATION);
113
        }
114
    }
115
 
156 ilm 116
    @Override
18 ilm 117
    protected List<String> getListFields() {
156 ilm 118
        final List<String> l = new ArrayList<>();
18 ilm 119
        l.add("NUMERO");
120
        l.add("DATE");
182 ilm 121
        if (getTable().contains("ID_COMMERCIAL")) {
174 ilm 122
            l.add("ID_COMMERCIAL");
123
        }
182 ilm 124
 
18 ilm 125
        l.add("ID_CLIENT");
126
        DefaultProps props = DefaultNXProps.getInstance();
127
        Boolean b = props.getBooleanValue("ArticleShowPoids");
128
        if (b) {
129
            l.add("TOTAL_POIDS");
130
        }
131
        l.add("NOM");
73 ilm 132
        l.add("TOTAL_HT");
18 ilm 133
        l.add("INFOS");
134
        return l;
135
    }
136
 
156 ilm 137
    @Override
18 ilm 138
    protected List<String> getComboFields() {
156 ilm 139
        final List<String> l = new ArrayList<>(2);
18 ilm 140
        l.add("NUMERO");
141
        l.add("DATE");
142
        return l;
143
    }
144
 
142 ilm 145
    @Override
146
    public ListMap<String, String> getShowAs() {
147
        return ListMap.singleton(null, "NUMERO", "DATE");
148
    }
149
 
18 ilm 150
    /*
151
     * (non-Javadoc)
152
     *
153
     * @see org.openconcerto.devis.SQLElement#getComponent()
154
     */
155
    public SQLComponent createComponent() {
156
        return new BonDeLivraisonSQLComponent();
157
    }
158
 
156 ilm 159
    public List<Object> getSourceTrRowsFrom(int blOrigin, String tableSourceItems, String tableSourceRoot) {
142 ilm 160
        SQLTable tableBLElement = getTable().getTable("BON_DE_LIVRAISON_ELEMENT");
156 ilm 161
        SQLTable tableCmdElement = getTable().getTable(tableSourceItems);
162
        String idRoot = "c2.\"" + getTable().getTable(tableSourceRoot).getKey().getFieldName() + "\"";
163
        String up = "SELECT DISTINCT c2.\"ID_" + tableSourceRoot + "\" FROm " + new SQLName(tableBLElement.getDBRoot().getName(), tableBLElement.getName()).quote() + " b2, "
142 ilm 164
                + new SQLName(tableCmdElement.getDBRoot().getName(), tableCmdElement.getName()).quote() + " c2 WHERE b2.\"ID_BON_DE_LIVRAISON\"=" + blOrigin
156 ilm 165
                + " AND c2.\"ARCHIVE\"=0 AND b2.\"ARCHIVE\"=0 AND " + idRoot + ">1 AND b2.\"ID\">1 AND b2.\"ID_" + tableSourceItems + "\"=" + idRoot;
166
        return getTable().getDBSystemRoot().getDataSource().executeCol(up);
142 ilm 167
    }
168
 
156 ilm 169
    public void updateQteLivree(List<Object> items, String tableItems, String tableRoot) {
170
        if (items != null && !items.isEmpty()) {
171
            SQLTable tableBLElement = getTable().getTable("BON_DE_LIVRAISON_ELEMENT");
172
            SQLTable tableCmdElement = getTable().getTable(tableItems);
173
            String itemsKey = "c.\"" + getTable().getTable(tableItems).getKey().getName() + "\"";
174
            UpdateBuilder build = new UpdateBuilder(tableCmdElement);
175
            build.set("QTE_LIVREE", "(SELECT SUM(b.\"QTE_LIVREE\" * b.\"QTE_UNITAIRE\") from " + new SQLName(tableBLElement.getDBRoot().getName(), tableBLElement.getName()).quote() + " b where "
176
                    + itemsKey + "=b.\"ID_" + tableItems + "\" AND " + itemsKey + ">1 AND c.\"ARCHIVE\"=0 AND b.\"ID\">1 AND b.\"ARCHIVE\"=0 )");
177
            AliasedTable alias = new AliasedTable(tableCmdElement, "c");
178
            build.setWhere(new Where(alias.getField("ID_" + tableRoot), items));
142 ilm 179
 
156 ilm 180
            getTable().getDBSystemRoot().getDataSource().execute(build.asString().replaceAll(" SET", " c SET "));
181
        }
142 ilm 182
    }
183
 
90 ilm 184
    @Override
185
    protected void archive(TreesOfSQLRows trees, boolean cutLinks) throws SQLException {
61 ilm 186
 
156 ilm 187
        List<Object> cmds = new ArrayList<>();
188
        List<Object> devis = new ArrayList<>();
189
        List<Integer> ids = new ArrayList<>();
90 ilm 190
        for (SQLRow row : trees.getRows()) {
61 ilm 191
 
156 ilm 192
            cmds.addAll(getSourceTrRowsFrom(row.getID(), "COMMANDE_CLIENT_ELEMENT", "COMMANDE_CLIENT"));
193
            devis.addAll(getSourceTrRowsFrom(row.getID(), "DEVIS_ELEMENT", "DEVIS"));
142 ilm 194
            ids.add(row.getID());
61 ilm 195
 
182 ilm 196
            // Mise à jour des stocks
197
            SQLElement eltMvtStock = Configuration.getInstance().getDirectory().getElement("MOUVEMENT_STOCK");
198
            SQLSelect sel = new SQLSelect();
199
            sel.addSelect(eltMvtStock.getTable().getField("ID"));
200
            Where w = new Where(eltMvtStock.getTable().getField("IDSOURCE"), "=", row.getID());
201
            Where w2 = new Where(eltMvtStock.getTable().getField("SOURCE"), "=", getTable().getName());
202
            sel.setWhere(w.and(w2));
90 ilm 203
 
182 ilm 204
            @SuppressWarnings("unchecked")
205
            List l = (List) eltMvtStock.getTable().getBase().getDataSource().execute(sel.asString(), new ArrayListHandler());
206
            if (l != null) {
207
                for (int i = 0; i < l.size(); i++) {
208
                    Object[] tmp = (Object[]) l.get(i);
209
                    eltMvtStock.archive(((Number) tmp[0]).intValue());
61 ilm 210
                }
211
            }
182 ilm 212
 
61 ilm 213
        }
90 ilm 214
        super.archive(trees, cutLinks);
142 ilm 215
 
156 ilm 216
        updateQteLivree(devis, "DEVIS_ELEMENT", "DEVIS");
217
        updateQteLivree(cmds, "COMMANDE_CLIENT_ELEMENT", "COMMANDE_CLIENT");
218
 
61 ilm 219
    }
156 ilm 220
 
221
    public RowAction getCloneAction() {
222
        return new RowAction(new AbstractAction() {
223
 
224
            public void actionPerformed(ActionEvent e) {
225
                SQLRowAccessor selectedRow = IListe.get(e).getSelectedRow();
226
 
227
                EditFrame editFrame = new EditFrame(BonDeLivraisonSQLElement.this, EditPanel.CREATION);
228
 
229
                ((BonDeLivraisonSQLComponent) editFrame.getSQLComponent()).duplicate(selectedRow.getID());
230
                editFrame.setVisible(true);
231
            }
232
        }, true, "sales.quote.clone") {
233
            @Override
234
            public boolean enabledFor(java.util.List<org.openconcerto.sql.model.SQLRowValues> selection) {
235
                return (selection != null && selection.size() == 1);
236
            }
237
        };
238
    }
239
 
182 ilm 240
    private final List<SQLTableModifiedListener> listenerCmdInserted = new ArrayList<>();
241
 
242
    public void addInsertedCmdListener(SQLTableModifiedListener l) {
243
        this.listenerCmdInserted.add(l);
244
    }
245
 
246
    public void removeInsertedCmdListener(SQLTableModifiedListener l) {
247
        this.listenerCmdInserted.remove(l);
248
    }
249
 
250
    public void fireInsertedCmdListener(SQLRow row) {
251
        for (SQLTableModifiedListener sqlTableModifiedListener : this.listenerCmdInserted) {
252
            sqlTableModifiedListener.tableModified(new SQLTableEvent(row, Mode.ROW_ADDED, null));
253
        }
254
    }
255
 
156 ilm 256
    @Override
257
    protected String createCodeSuffix() {
258
        return ".delivery.note";
259
    }
260
 
18 ilm 261
}