OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | 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.credit.element;
15
 
16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
17
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
132 ilm 18
import org.openconcerto.erp.core.finance.accounting.element.EcritureSQLElement;
18 ilm 19
import org.openconcerto.erp.core.sales.credit.component.AvoirClientSQLComponent;
93 ilm 20
import org.openconcerto.erp.generationDoc.gestcomm.AvoirClientXmlSheet;
132 ilm 21
import org.openconcerto.erp.generationEcritures.GenerationMvtSaisieVenteFacture;
93 ilm 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;
132 ilm 26
import org.openconcerto.sql.element.SQLElementLink.LinkType;
27
import org.openconcerto.sql.element.SQLElementLinksSetup;
28
import org.openconcerto.sql.element.TreesOfSQLRows;
18 ilm 29
import org.openconcerto.sql.model.SQLRow;
132 ilm 30
import org.openconcerto.sql.model.SQLRowAccessor;
67 ilm 31
import org.openconcerto.sql.model.SQLRowValues;
18 ilm 32
import org.openconcerto.sql.model.SQLSelect;
33
import org.openconcerto.sql.model.Where;
67 ilm 34
import org.openconcerto.sql.request.ListSQLRequest;
142 ilm 35
import org.openconcerto.sql.view.list.SQLTableModelSource;
132 ilm 36
import org.openconcerto.utils.ExceptionHandler;
142 ilm 37
import org.openconcerto.utils.ListMap;
18 ilm 38
 
39
import java.sql.SQLException;
40
import java.util.ArrayList;
132 ilm 41
import java.util.Collection;
18 ilm 42
import java.util.List;
43
 
44
import org.apache.commons.dbutils.handlers.ArrayListHandler;
45
 
46
public class AvoirClientSQLElement extends ComptaSQLConfElement {
47
 
48
    public AvoirClientSQLElement() {
49
        super("AVOIR_CLIENT", "une facture d'avoir", "factures d'avoir");
182 ilm 50
        getRowActions().addAll(new MouseSheetXmlListeListener(this, AvoirClientXmlSheet.class).getRowActions());
18 ilm 51
    }
52
 
132 ilm 53
    @Override
54
    protected void setupLinks(SQLElementLinksSetup links) {
55
        super.setupLinks(links);
56
        if (getTable().contains("ID_ADRESSE")) {
57
            links.get("ID_ADRESSE").setType(LinkType.ASSOCIATION);
58
        }
59
        if (getTable().contains("ID_ADRESSE_LIVRAISON")) {
60
            links.get("ID_ADRESSE_LIVRAISON").setType(LinkType.ASSOCIATION);
61
        }
62
    }
63
 
18 ilm 64
    public List<String> getListFields() {
65
        final List<String> l = new ArrayList<String>();
66
        l.add("NUMERO");
61 ilm 67
            l.add("ID_COMMERCIAL");
18 ilm 68
        l.add("ID_CLIENT");
19 ilm 69
 
18 ilm 70
        l.add("NOM");
19 ilm 71
 
72
 
18 ilm 73
        l.add("DATE");
74
        l.add("MONTANT_HT");
75
        l.add("MONTANT_TTC");
76
        l.add("MONTANT_SOLDE");
77
        l.add("MONTANT_RESTANT");
78
        l.add("SOLDE");
79
        return l;
80
    }
81
 
82
    protected List<String> getComboFields() {
83
        final List<String> l = new ArrayList<String>();
84
        l.add("DATE");
85
        l.add("NOM");
86
        l.add("NUMERO");
87
        l.add("MONTANT_TTC");
88
        l.add("MONTANT_SOLDE");
89
        return l;
90
    }
91
 
92
    @Override
142 ilm 93
    protected synchronized void _initTableSource(final SQLTableModelSource table) {
94
        super._initTableSource(table);
94 ilm 95
    }
96
 
18 ilm 97
    /*
98
     * (non-Javadoc)
99
     *
100
     * @see org.openconcerto.devis.SQLElement#getComponent()
101
     */
102
    public SQLComponent createComponent() {
103
        return new AvoirClientSQLComponent();
104
    }
105
 
106
    @Override
132 ilm 107
    protected void _initListRequest(ListSQLRequest req) {
108
        super._initListRequest(req);
109
        req.addToGraphToFetch("A_DEDUIRE", "MOTIF");
110
    }
111
 
142 ilm 112
    @Override
113
    public ListMap<String, String> getShowAs() {
114
        ListMap<String, String> map = new ListMap<String, String>();
115
        map.putCollection(null, "NUMERO", "DATE", "ID_COMMERCIAL");
116
        return map;
117
    }
118
 
132 ilm 119
    public void annulationAvoir(SQLRowAccessor row) {
120
        Collection<? extends SQLRowAccessor> rows = row.getReferentRows(getTable().getTable("SAISIE_VENTE_FACTURE"));
121
        for (SQLRowAccessor sqlRowAccessor : rows) {
122
            SQLRowAccessor rowAvoir = sqlRowAccessor.getForeign("ID_AVOIR_CLIENT");
123
 
124
            Long montantSolde = (Long) rowAvoir.getObject("MONTANT_SOLDE");
125
            Long avoirTTC = (Long) sqlRowAccessor.getObject("T_AVOIR_TTC");
126
 
127
            long montant = montantSolde - avoirTTC;
128
            if (montant < 0) {
129
                montant = 0;
67 ilm 130
            }
132 ilm 131
 
132
            SQLRowValues rowVals = rowAvoir.createEmptyUpdateRow();
133
 
134
            // Soldé
135
            rowVals.put("SOLDE", Boolean.FALSE);
136
            rowVals.put("MONTANT_SOLDE", montant);
137
            Long restant = (Long) rowAvoir.getObject("MONTANT_TTC") - montantSolde;
138
            rowVals.put("MONTANT_RESTANT", restant);
139
 
140
            try {
141
                rowVals.update();
142
 
143
                final SQLRowValues createEmptyUpdateRow = sqlRowAccessor.createEmptyUpdateRow();
144
                createEmptyUpdateRow.putEmptyLink("ID_AVOIR_CLIENT");
145
                createEmptyUpdateRow.put("NET_A_PAYER", sqlRowAccessor.getObject("T_TTC"));
146
                createEmptyUpdateRow.put("T_AVOIR_TTC", 0L);
147
                createEmptyUpdateRow.update();
148
                EcritureSQLElement eltEcr = (EcritureSQLElement) Configuration.getInstance().getDirectory().getElement("ECRITURE");
149
                final int foreignIDmvt = sqlRowAccessor.getForeignID("ID_MOUVEMENT");
150
                eltEcr.archiveMouvementProfondeur(foreignIDmvt, false);
151
 
152
                System.err.println("Regeneration des ecritures");
153
                new GenerationMvtSaisieVenteFacture(sqlRowAccessor.getID(), foreignIDmvt);
154
                System.err.println("Fin regeneration");
155
 
156
            } catch (SQLException e) {
157
                ExceptionHandler.handle("Erreur lors de l'annulation de l'avoir", e);
158
            }
159
        }
67 ilm 160
    }
161
 
162
    @Override
132 ilm 163
    protected void archive(TreesOfSQLRows trees, boolean cutLinks) throws SQLException {
164
        // FIXME Vérifier si l'avoir est affecté sur une facture et recalculer les échéances pour la
165
        // facture associée
166
        for (SQLRow row : trees.getRows()) {
18 ilm 167
 
132 ilm 168
            annulationAvoir(row);
18 ilm 169
 
132 ilm 170
            // Mise à jour des stocks
171
            SQLElement eltMvtStock = Configuration.getInstance().getDirectory().getElement("MOUVEMENT_STOCK");
172
            SQLSelect sel = new SQLSelect(eltMvtStock.getTable().getBase());
173
            sel.addSelect(eltMvtStock.getTable().getField("ID"));
174
            Where w = new Where(eltMvtStock.getTable().getField("IDSOURCE"), "=", row.getID());
175
            Where w2 = new Where(eltMvtStock.getTable().getField("SOURCE"), "=", getTable().getName());
176
            sel.setWhere(w.and(w2));
18 ilm 177
 
132 ilm 178
            List l = (List) eltMvtStock.getTable().getBase().getDataSource().execute(sel.asString(), new ArrayListHandler());
179
            if (l != null) {
180
                for (int i = 0; i < l.size(); i++) {
181
                    Object[] tmp = (Object[]) l.get(i);
182
                    eltMvtStock.archive(((Number) tmp[0]).intValue());
183
                }
18 ilm 184
            }
185
        }
132 ilm 186
        super.archive(trees, cutLinks);
187
 
18 ilm 188
    }
189
 
156 ilm 190
    @Override
191
    protected String createCodeSuffix() {
192
        return ".note";
193
    }
18 ilm 194
}