OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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