OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 19 | Rev 41 | 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.invoice.element;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.config.Gestion;
18
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
19
import org.openconcerto.erp.core.sales.credit.component.AvoirClientSQLComponent;
20
import org.openconcerto.erp.core.sales.invoice.component.SaisieVenteFactureSQLComponent;
19 ilm 21
import org.openconcerto.erp.core.sales.product.element.ReferenceArticleSQLElement;
22
import org.openconcerto.erp.core.sales.shipment.component.BonDeLivraisonSQLComponent;
23
import org.openconcerto.erp.core.supplychain.stock.element.MouvementStockSQLElement;
18 ilm 24
import org.openconcerto.sql.Configuration;
25
import org.openconcerto.sql.element.SQLComponent;
26
import org.openconcerto.sql.element.SQLElement;
19 ilm 27
import org.openconcerto.sql.model.SQLField;
18 ilm 28
import org.openconcerto.sql.model.SQLInjector;
29
import org.openconcerto.sql.model.SQLRow;
30
import org.openconcerto.sql.model.SQLRowValues;
31
import org.openconcerto.sql.model.SQLSelect;
19 ilm 32
import org.openconcerto.sql.model.SQLTable;
18 ilm 33
import org.openconcerto.sql.model.Where;
34
import org.openconcerto.sql.request.ListSQLRequest;
35
import org.openconcerto.sql.view.EditFrame;
19 ilm 36
import org.openconcerto.utils.CollectionMap;
18 ilm 37
import org.openconcerto.utils.ExceptionHandler;
38
 
19 ilm 39
import java.math.BigDecimal;
18 ilm 40
import java.sql.SQLException;
41
import java.util.ArrayList;
42
import java.util.HashSet;
43
import java.util.List;
44
import java.util.Set;
45
 
46
import javax.swing.ImageIcon;
47
import javax.swing.JFrame;
48
 
49
import org.apache.commons.dbutils.handlers.ArrayListHandler;
50
 
51
public class SaisieVenteFactureSQLElement extends ComptaSQLConfElement {
52
 
25 ilm 53
    public static final String TABLENAME = "SAISIE_VENTE_FACTURE";
54
 
18 ilm 55
    public SaisieVenteFactureSQLElement() {
25 ilm 56
        super(TABLENAME, "une facture", "factures");
18 ilm 57
    }
58
 
59
    protected List<String> getListFields() {
60
        final List<String> l = new ArrayList<String>();
61
        l.add("NUMERO");
62
        l.add("DATE");
63
        l.add("NOM");
64
        l.add("ID_CLIENT");
65
            l.add("ID_MODE_REGLEMENT");
19 ilm 66
 
67
        l.add("T_HA");
68
        l.add("T_HT");
69
        l.add("T_TTC");
70
        l.add("INFOS");
71
 
18 ilm 72
                l.add("DATE_ENVOI");
73
            l.add("DATE_REGLEMENT");
74
        return l;
75
    }
76
 
77
    @Override
19 ilm 78
    public synchronized ListSQLRequest createListRequest() {
18 ilm 79
        return new ListSQLRequest(this.getTable(), this.getListFields()) {
80
            @Override
81
            protected void customizeToFetch(SQLRowValues graphToFetch) {
82
                super.customizeToFetch(graphToFetch);
83
                graphToFetch.put("ACOMPTE", null);
84
                graphToFetch.put("COMPLEMENT", null);
19 ilm 85
 
18 ilm 86
                graphToFetch.put("PREVISIONNELLE", null);
87
                    graphToFetch.grow("ID_MODE_REGLEMENT").put("AJOURS", null).put("LENJOUR", null);
88
            }
89
        };
90
    }
91
 
92
    protected List<String> getComboFields() {
93
        final List<String> l = new ArrayList<String>();
94
        l.add("NUMERO");
95
        return l;
96
    }
97
 
98
    protected List<String> getPrivateFields() {
99
        final List<String> l = new ArrayList<String>();
100
        l.add("ID_MODE_REGLEMENT");
101
        return l;
102
    }
103
 
104
    @Override
105
    protected Set<String> getChildren() {
106
        Set<String> set = new HashSet<String>();
107
        set.add("SAISIE_VENTE_FACTURE_ELEMENT");
108
        return set;
109
    }
110
 
111
    @Override
112
    public Set<String> getReadOnlyFields() {
113
        Set<String> s = new HashSet<String>(1);
114
        s.add("CONTROLE_TECHNIQUE");
115
        return s;
116
    }
117
 
118
    /*
119
     * (non-Javadoc)
120
     *
121
     * @see org.openconcerto.devis.SQLElement#getComponent()
122
     */
123
    public SQLComponent createComponent() {
124
        return new SaisieVenteFactureSQLComponent();
125
    }
126
 
127
    @Override
128
    protected void archive(SQLRow row, boolean cutLinks) throws SQLException {
129
 
130
        // On retire l'avoir
131
        if (row.getInt("ID_AVOIR_CLIENT") > 1) {
132
            SQLElement eltAvoir = Configuration.getInstance().getDirectory().getElement("AVOIR_CLIENT");
133
            SQLRow rowAvoir = eltAvoir.getTable().getRow(row.getInt("ID_AVOIR_CLIENT"));
134
 
135
            Long montantSolde = (Long) rowAvoir.getObject("MONTANT_SOLDE");
136
 
137
            Long avoirTTC = (Long) row.getObject("T_AVOIR_TTC");
138
 
139
            long montant = montantSolde - avoirTTC;
140
            if (montant < 0) {
141
                montant = 0;
142
            }
143
 
144
            SQLRowValues rowVals = rowAvoir.createEmptyUpdateRow();
145
 
146
            // Soldé
147
            rowVals.put("SOLDE", Boolean.FALSE);
148
            rowVals.put("MONTANT_SOLDE", montant);
149
            Long restant = (Long) rowAvoir.getObject("MONTANT_TTC") - montantSolde;
150
            rowVals.put("MONTANT_RESTANT", restant);
151
            try {
152
                rowVals.update();
153
            } catch (SQLException e) {
154
                e.printStackTrace();
155
            }
156
        }
157
 
158
        super.archive(row, cutLinks);
159
 
160
 
161
        // Mise à jour des stocks
162
        SQLElement eltMvtStock = Configuration.getInstance().getDirectory().getElement("MOUVEMENT_STOCK");
163
        SQLSelect sel = new SQLSelect(eltMvtStock.getTable().getBase());
164
        sel.addSelect(eltMvtStock.getTable().getField("ID"));
165
        Where w = new Where(eltMvtStock.getTable().getField("IDSOURCE"), "=", row.getID());
166
        Where w2 = new Where(eltMvtStock.getTable().getField("SOURCE"), "=", getTable().getName());
167
        sel.setWhere(w.and(w2));
168
 
169
        List l = (List) eltMvtStock.getTable().getBase().getDataSource().execute(sel.asString(), new ArrayListHandler());
170
        if (l != null) {
171
            for (int i = 0; i < l.size(); i++) {
172
                Object[] tmp = (Object[]) l.get(i);
173
                eltMvtStock.archive(((Number) tmp[0]).intValue());
174
            }
175
        }
176
    }
177
 
19 ilm 178
    public void transfertBL(int idFacture) {
179
        final SQLElement elt = Configuration.getInstance().getDirectory().getElement("BON_DE_LIVRAISON");
180
        final EditFrame editAvoirFrame = new EditFrame(elt);
181
        editAvoirFrame.setIconImage(new ImageIcon(Gestion.class.getResource("frameicon.png")).getImage());
182
 
183
        final BonDeLivraisonSQLComponent comp = (BonDeLivraisonSQLComponent) editAvoirFrame.getSQLComponent();
184
        final SQLInjector inject = SQLInjector.getInjector(this.getTable(), elt.getTable());
185
        SQLRowValues createRowValuesFrom = inject.createRowValuesFrom(idFacture);
186
        SQLRow rowFacture = getTable().getRow(idFacture);
187
        String string = rowFacture.getString("NOM");
188
        createRowValuesFrom.put("NOM", string + (string.trim().length() == 0 ? "" : ", ") + rowFacture.getString("NUMERO"));
189
        comp.select(createRowValuesFrom);
190
        // comp.loadFactureItem(idFacture);
191
 
192
        editAvoirFrame.pack();
193
        editAvoirFrame.setState(JFrame.NORMAL);
194
        editAvoirFrame.setVisible(true);
195
 
196
    }
197
 
18 ilm 198
    public void transfertAvoir(int idFacture) {
199
        final SQLElement elt = Configuration.getInstance().getDirectory().getElement("AVOIR_CLIENT");
200
        final EditFrame editAvoirFrame = new EditFrame(elt);
201
        editAvoirFrame.setIconImage(new ImageIcon(Gestion.class.getResource("frameicon.png")).getImage());
202
 
203
        final AvoirClientSQLComponent comp = (AvoirClientSQLComponent) editAvoirFrame.getSQLComponent();
204
        final SQLInjector inject = SQLInjector.getInjector(this.getTable(), elt.getTable());
205
        comp.select(inject.createRowValuesFrom(idFacture));
206
        comp.loadFactureItem(idFacture);
207
 
208
        editAvoirFrame.pack();
209
        editAvoirFrame.setState(JFrame.NORMAL);
210
        editAvoirFrame.setVisible(true);
211
 
212
    }
19 ilm 213
 
214
    public void transfertCommande(int idFacture) {
215
        // final SQLElement elt = Configuration.getInstance().getDirectory().getElement("COMMANDE");
216
        // final EditFrame editAvoirFrame = new EditFrame(elt);
217
        // editAvoirFrame.setIconImage(new
218
        // ImageIcon(Gestion.class.getResource("frameicon.png")).getImage());
219
        //
220
        // final CommandeSQLComponent comp = (CommandeSQLComponent)
221
        // editAvoirFrame.getSQLComponent();
222
        // final SQLInjector inject = SQLInjector.getInjector(this.getTable(), elt.getTable());
223
        // comp.select(inject.createRowValuesFrom(idFacture));
224
        // comp.loadFacture(idFacture);
225
        //
226
        // editAvoirFrame.pack();
227
        // editAvoirFrame.setState(JFrame.NORMAL);
228
        // editAvoirFrame.setVisible(true);
229
 
230
        SQLElement elt = Configuration.getInstance().getDirectory().getElement("SAISIE_VENTE_FACTURE_ELEMENT");
231
        SQLTable tableCmdElt = Configuration.getInstance().getDirectory().getElement("COMMANDE_ELEMENT").getTable();
232
        SQLElement eltArticle = Configuration.getInstance().getDirectory().getElement("ARTICLE");
233
        List<SQLRow> rows = getTable().getRow(idFacture).getReferentRows(elt.getTable());
234
        CollectionMap<SQLRow, List<SQLRowValues>> map = new CollectionMap<SQLRow, List<SQLRowValues>>();
235
        SQLRow rowDeviseF = null;
236
        for (SQLRow sqlRow : rows) {
237
            // on récupére l'article qui lui correspond
238
            SQLRowValues rowArticle = new SQLRowValues(eltArticle.getTable());
239
            for (SQLField field : eltArticle.getTable().getFields()) {
240
                if (sqlRow.getTable().getFieldsName().contains(field.getName())) {
241
                    rowArticle.put(field.getName(), sqlRow.getObject(field.getName()));
242
                }
243
            }
244
            // rowArticle.loadAllSafe(rowEltFact);
245
            int idArticle = ReferenceArticleSQLElement.getIdForCNM(rowArticle, true);
246
            SQLRow rowArticleFind = eltArticle.getTable().getRow(idArticle);
247
            SQLInjector inj = SQLInjector.getInjector(rowArticle.getTable(), tableCmdElt);
248
            SQLRowValues rowValsElt = new SQLRowValues(inj.createRowValuesFrom(rowArticleFind));
25 ilm 249
            rowValsElt.put("ID_STYLE", sqlRow.getObject("ID_STYLE"));
19 ilm 250
            rowValsElt.put("QTE", sqlRow.getObject("QTE"));
251
            rowValsElt.put("T_POIDS", rowValsElt.getLong("POIDS") * rowValsElt.getInt("QTE"));
252
 
253
            // gestion de la devise
254
            rowDeviseF = sqlRow.getForeignRow("ID_DEVISE");
255
            SQLRow rowDeviseHA = rowArticleFind.getForeignRow("ID_DEVISE_HA");
256
            if (rowDeviseF != null && !rowDeviseF.isUndefined()) {
257
                if (rowDeviseF.getID() == rowDeviseHA.getID()) {
258
                    rowValsElt.put("PA_DEVISE", rowArticleFind.getLong("PA_DEVISE"));
259
                    rowValsElt.put("PA_DEVISE_T", rowArticleFind.getLong("PA_DEVISE") * rowValsElt.getInt("QTE"));
260
                    rowValsElt.put("ID_DEVISE", rowDeviseF.getID());
261
                } else {
262
                    BigDecimal taux = (BigDecimal) rowDeviseF.getObject("TAUX");
263
                    rowValsElt.put("PA_DEVISE", taux.multiply(new BigDecimal(rowValsElt.getLong("PA_HT"))).longValue());
264
                    rowValsElt.put("PA_DEVISE_T", rowValsElt.getLong("PA_DEVISE") * rowValsElt.getInt("QTE"));
265
                    rowValsElt.put("ID_DEVISE", rowDeviseF.getID());
266
                }
267
            }
268
 
269
            rowValsElt.put("T_PA_HT", rowValsElt.getLong("PA_HT") * rowValsElt.getInt("QTE"));
270
 
271
            rowValsElt.put("T_PA_HT", rowValsElt.getLong("PA_HT") * rowValsElt.getInt("QTE"));
272
            rowValsElt.put("T_PA_TTC", rowValsElt.getLong("T_PA_HT") * (rowValsElt.getForeign("ID_TAXE").getFloat("TAUX") / 100.0 + 1.0));
273
 
274
            map.put(rowArticleFind.getForeignRow("ID_FOURNISSEUR"), rowValsElt);
275
 
276
        }
277
        MouvementStockSQLElement.createCommandeF(map, rowDeviseF);
278
 
279
    }
18 ilm 280
}