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
 *
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.generationEcritures;
15
 
83 ilm 16
import org.openconcerto.erp.core.common.element.BanqueSQLElement;
18 ilm 17
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
18
import org.openconcerto.erp.core.finance.accounting.element.JournalSQLElement;
19
import org.openconcerto.erp.core.finance.payment.element.ModeDeReglementSQLElement;
20
import org.openconcerto.erp.model.PrixTTC;
21
import org.openconcerto.sql.model.SQLRow;
22
import org.openconcerto.sql.model.SQLRowValues;
23
import org.openconcerto.sql.model.SQLTable;
177 ilm 24
import org.openconcerto.sql.model.Where;
25
import org.openconcerto.sql.request.UpdateBuilder;
18 ilm 26
 
27
import java.sql.SQLException;
28
import java.text.DateFormat;
29
import java.text.SimpleDateFormat;
177 ilm 30
import java.util.ArrayList;
18 ilm 31
import java.util.Date;
32
import java.util.HashMap;
80 ilm 33
import java.util.List;
18 ilm 34
import java.util.Map;
35
 
36
public class GenerationReglementAchat extends GenerationEcritures {
37
 
38
    // Journal Caisse
39
    private static final Integer journalCaisse = new Integer(JournalSQLElement.CAISSES);
40
    private static final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
41
    private static final SQLTable tableMouvement = base.getTable("MOUVEMENT");
42
    private static final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
43
 
73 ilm 44
    public GenerationReglementAchat(int idRegMontant) throws Exception {
18 ilm 45
 
46
        SQLRow regMontantRow = base.getTable("REGLER_MONTANT").getRow(idRegMontant);
47
 
80 ilm 48
        SQLRow rowFournisseur = regMontantRow.getForeign("ID_FOURNISSEUR");
18 ilm 49
 
50
        System.err.println("Génération des ecritures du reglement du mouvement " + this.idMvt);
51
 
52
        SQLRow modeRegRow = base.getTable("MODE_REGLEMENT").getRow(regMontantRow.getInt("ID_MODE_REGLEMENT"));
53
        SQLRow typeRegRow = base.getTable("TYPE_REGLEMENT").getRow(modeRegRow.getInt("ID_TYPE_REGLEMENT"));
54
 
55
        System.err.println("Mode de reglement " + regMontantRow.getInt("ID_MODE_REGLEMENT"));
56
 
57
        PrixTTC prixTTC = new PrixTTC(((Long) regMontantRow.getObject("MONTANT")).longValue());
58
 
59
        // iniatilisation des valeurs de la map
60
        this.date = (Date) regMontantRow.getObject("DATE");
61
 
62
        // "Règlement achat" + SOURCE.getNom() ??
63
        this.nom = "Règlement achat " + rowFournisseur.getString("NOM") + " (" + typeRegRow.getString("NOM") + ")";
64
 
80 ilm 65
        List<SQLRow> l = regMontantRow.getReferentRows(regMontantRow.getTable().getTable("REGLER_MONTANT_ELEMENT"));
66
        int mvtSource = -1;
177 ilm 67
        List<Integer> factureFids = new ArrayList<Integer>();
68
 
80 ilm 69
        for (SQLRow sqlRow : l) {
70
            SQLRow mvtEch = sqlRow.getForeignRow("ID_MOUVEMENT_ECHEANCE");
71
            if (mvtEch.getID() != mvtSource) {
72
                getNewMouvement("REGLER_MONTANT", idRegMontant, mvtEch.getID(), mvtEch.getInt("ID_PIECE"));
73
                if (mvtSource == -1) {
74
                    mvtSource = mvtEch.getID();
75
                }
76
            }
177 ilm 77
            if (!sqlRow.isForeignEmpty("ID_ECHEANCE_FOURNISSEUR")) {
78
                SQLRow ech = sqlRow.getForeign("ID_ECHEANCE_FOURNISSEUR");
79
                if (!ech.isForeignEmpty("ID_FACTURE_FOURNISSEUR")) {
80
                    factureFids.add(ech.getForeignID("ID_FACTURE_FOURNISSEUR"));
81
                }
82
            }
80 ilm 83
        }
84
 
85
        SQLRow rowMvtSource = tableMouvement.getRow(mvtSource);
156 ilm 86
        if (rowMvtSource == null) {
87
            throw new IllegalStateException("Aucun mouvement source associé aux échéances.\n(Mouvement source : " + mvtSource + ", REGLER_MONTANT " + regMontantRow.getID() + ")");
88
        }
80 ilm 89
 
18 ilm 90
        // si paiement comptant
91
        if ((modeRegRow.getInt("AJOURS") == 0) && (modeRegRow.getInt("LENJOUR") == 0)) {
92
 
93
            System.err.println("Règlement Comptant");
94
            // test Cheque
95
            if (typeRegRow.getID() == 2) {
96
 
97
                // Ajout dans cheque fournisseur
80 ilm 98
                paiementCheque(this.date, rowMvtSource, rowFournisseur.getID(), idRegMontant);
18 ilm 99
            } else {
100
 
101
                if (typeRegRow.getID() == 4) {
149 ilm 102
                    this.putValue("ID_JOURNAL", GenerationReglementAchat.journalCaisse);
18 ilm 103
                } else {
83 ilm 104
                    fillJournalBanqueFromRow(modeRegRow);
18 ilm 105
                }
106
 
107
                // SQLRow echeanceRow = base.getTable("ECHEANCE_FOURNISSEUR").getRow(idEchFourn);
108
 
80 ilm 109
                this.idMvt = getNewMouvement("REGLER_MONTANT", idRegMontant, rowMvtSource.getID(), rowMvtSource.getInt("ID_PIECE"));
110
 
149 ilm 111
                this.putValue("DATE", this.date);
112
                this.putValue("NOM", this.nom);
113
                this.putValue("ID_MOUVEMENT", new Integer(this.idMvt));
18 ilm 114
 
115
                // compte Fournisseurs
116
                int idCompteFourn = rowFournisseur.getInt("ID_COMPTE_PCE");
117
 
118
                if (idCompteFourn <= 1) {
119
                    idCompteFourn = rowPrefsCompte.getInt("ID_COMPTE_PCE_FOURNISSEUR");
120
                    if (idCompteFourn <= 1) {
73 ilm 121
                        idCompteFourn = ComptePCESQLElement.getIdComptePceDefault("Fournisseurs");
122
 
18 ilm 123
                    }
124
                }
125
 
149 ilm 126
                this.putValue("ID_COMPTE_PCE", new Integer(idCompteFourn));
127
                this.putValue("DEBIT", new Long(prixTTC.getLongValue()));
128
                this.putValue("CREDIT", new Long(0));
73 ilm 129
                ajoutEcriture();
130
 
131
                // compte de reglement, caisse, CB, ...
83 ilm 132
                fillCompteBanqueFromRow(modeRegRow, "AchatCB", true);
149 ilm 133
                this.putValue("DEBIT", new Long(0));
134
                this.putValue("CREDIT", new Long(prixTTC.getLongValue()));
73 ilm 135
                ajoutEcriture();
136
 
18 ilm 137
            }
177 ilm 138
 
139
            setDateReglement(regMontantRow.getTable().getTable("FACTURE_FOURNISSEUR"), factureFids, this.date);
140
 
18 ilm 141
        } else {
142
 
143
            Date dateEch = ModeDeReglementSQLElement.calculDate(modeRegRow.getInt("AJOURS"), modeRegRow.getInt("LENJOUR"), this.date);
144
            DateFormat dateFormat = new SimpleDateFormat();
145
            System.out.println("Date d'échéance " + dateFormat.format(dateEch));
146
            // System.out.println("Echeance" + dateEch);
147
 
148
            // Cheque
149
            if (typeRegRow.getID() == 2) {
150
 
151
                // Ajout dans cheque fournisseur
80 ilm 152
                paiementCheque(dateEch, rowMvtSource, rowFournisseur.getID(), idRegMontant);
18 ilm 153
            } else {
154
 
155
                // Ajout dans echeance
73 ilm 156
                Map<String, Object> mEcheance = new HashMap<String, Object>();
80 ilm 157
 
177 ilm 158
                this.idMvt = getNewMouvement(rowMvtSource.getString("SOURCE"), rowMvtSource.getInt("IDSOURCE"), rowMvtSource.getID(), rowMvtSource.getInt("ID_PIECE"));
18 ilm 159
                mEcheance.put("ID_MOUVEMENT", new Integer(this.idMvt));
160
 
161
                mEcheance.put("DATE", dateEch);
162
                mEcheance.put("MONTANT", new Long(prixTTC.getLongValue()));
163
 
80 ilm 164
                mEcheance.put("ID_FOURNISSEUR", rowFournisseur.getID());
18 ilm 165
 
166
                SQLRowValues valEcheance = new SQLRowValues(base.getTable("ECHEANCE_FOURNISSEUR"), mEcheance);
73 ilm 167
                if (valEcheance.getInvalid() == null) {
168
                    // ajout de l'ecriture
169
                    SQLRow row = valEcheance.insert();
170
                    SQLRowValues rowVals = new SQLRowValues(tableMouvement);
171
                    rowVals.put("IDSOURCE", row.getID());
172
                    rowVals.update(this.idMvt);
173
                }
18 ilm 174
 
175
            }
176
        }
177
    }
178
 
177 ilm 179
    private void setDateReglement(SQLTable table, List<Integer> ids, Date d) throws SQLException {
180
        if (!ids.isEmpty()) {
181
            UpdateBuilder b = new UpdateBuilder(table);
182
            b.setObject("DATE_REGLEMENT", d);
183
            b.setWhere(new Where(table.getKey(), ids));
184
            table.getDBSystemRoot().getDataSource().execute(b.asString());
185
        }
186
    }
187
 
80 ilm 188
    private void paiementCheque(Date dateEch, SQLRow rowMvtSource, int idFourn, int idRegMontant) throws SQLException {
18 ilm 189
 
190
        SQLRow regMontantRow = base.getTable("REGLER_MONTANT").getRow(idRegMontant);
191
        PrixTTC prixTTC = new PrixTTC(((Long) regMontantRow.getObject("MONTANT")).longValue());
192
 
193
        SQLRowValues valCheque = new SQLRowValues(base.getTable("CHEQUE_FOURNISSEUR"));
194
 
80 ilm 195
        valCheque.put("ID_FOURNISSEUR", idFourn);
18 ilm 196
        valCheque.put("DATE_ACHAT", this.date);
197
        valCheque.put("DATE_MIN_DECAISSE", dateEch);
83 ilm 198
        if (!regMontantRow.isForeignEmpty("ID_MODE_REGLEMENT")) {
199
            SQLRow rowModeRegl = regMontantRow.getForeignRow("ID_MODE_REGLEMENT");
200
            valCheque.put("ID_" + BanqueSQLElement.TABLENAME, rowModeRegl.getInt("ID_" + BanqueSQLElement.TABLENAME));
201
        }
80 ilm 202
 
203
        this.idMvt = getNewMouvement("CHEQUE_FOURNISSEUR", 1, rowMvtSource.getID(), rowMvtSource.getInt("ID_PIECE"));
18 ilm 204
        valCheque.put("ID_MOUVEMENT", new Integer(this.idMvt));
205
        valCheque.put("MONTANT", new Long(prixTTC.getLongValue()));
206
 
73 ilm 207
        if (valCheque.getInvalid() == null) {
208
            // ajout de l'ecriture
209
            SQLRow row = valCheque.insert();
210
            SQLRowValues rowVals = new SQLRowValues(tableMouvement);
211
            rowVals.put("IDSOURCE", row.getID());
212
            rowVals.update(this.idMvt);
213
        }
18 ilm 214
 
215
    }
216
}