OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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