OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 19 | Rev 80 | 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
 
16
import org.openconcerto.erp.core.finance.accounting.element.ComptePCESQLElement;
17
import org.openconcerto.erp.core.finance.accounting.element.JournalSQLElement;
18
import org.openconcerto.erp.core.finance.payment.element.ModeDeReglementSQLElement;
19
import org.openconcerto.erp.model.PrixTTC;
20
import org.openconcerto.sql.model.SQLRow;
21
import org.openconcerto.sql.model.SQLRowValues;
22
import org.openconcerto.sql.model.SQLTable;
23
 
24
import java.sql.SQLException;
25
import java.text.DateFormat;
26
import java.text.SimpleDateFormat;
27
import java.util.Date;
28
import java.util.HashMap;
29
import java.util.Map;
30
 
31
public class GenerationReglementAchat extends GenerationEcritures {
32
 
33
    // Journal Caisse
34
    private static final Integer journalCaisse = new Integer(JournalSQLElement.CAISSES);
35
    private static final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
36
    private static final SQLTable tableMouvement = base.getTable("MOUVEMENT");
37
    private static final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
38
 
73 ilm 39
    public GenerationReglementAchat(int idRegMontant) throws Exception {
18 ilm 40
 
41
        SQLRow regMontantRow = base.getTable("REGLER_MONTANT").getRow(idRegMontant);
42
 
43
        int idEchFourn = regMontantRow.getInt("ID_ECHEANCE_FOURNISSEUR");
44
        SQLRow echeanceRow = base.getTable("ECHEANCE_FOURNISSEUR").getRow(idEchFourn);
45
        SQLRow rowFournisseur = base.getTable("FOURNISSEUR").getRow(echeanceRow.getInt("ID_FOURNISSEUR"));
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
 
62
        // si paiement comptant
63
        if ((modeRegRow.getInt("AJOURS") == 0) && (modeRegRow.getInt("LENJOUR") == 0)) {
64
 
65
            System.err.println("Règlement Comptant");
66
            // test Cheque
67
            if (typeRegRow.getID() == 2) {
68
 
69
                // Ajout dans cheque fournisseur
70
                paiementCheque(this.date, idEchFourn, idRegMontant);
71
            } else {
72
 
73
                if (typeRegRow.getID() == 4) {
74
                    this.mEcritures.put("ID_JOURNAL", GenerationReglementAchat.journalCaisse);
75
                } else {
76
                    this.mEcritures.put("ID_JOURNAL", JournalSQLElement.BANQUES);
77
                }
78
 
79
                // SQLRow echeanceRow = base.getTable("ECHEANCE_FOURNISSEUR").getRow(idEchFourn);
80
                SQLRow rowMouv = tableMouvement.getRow(echeanceRow.getInt("ID_MOUVEMENT"));
81
                this.idMvt = getNewMouvement("REGLER_MONTANT", idRegMontant, rowMouv.getID(), rowMouv.getInt("ID_PIECE"));
82
 
83
                this.mEcritures.put("DATE", this.date);
84
                this.mEcritures.put("NOM", this.nom);
85
                this.mEcritures.put("ID_MOUVEMENT", new Integer(this.idMvt));
86
 
87
                // compte Fournisseurs
88
                int idCompteFourn = rowFournisseur.getInt("ID_COMPTE_PCE");
89
 
90
                if (idCompteFourn <= 1) {
91
                    idCompteFourn = rowPrefsCompte.getInt("ID_COMPTE_PCE_FOURNISSEUR");
92
                    if (idCompteFourn <= 1) {
73 ilm 93
                        idCompteFourn = ComptePCESQLElement.getIdComptePceDefault("Fournisseurs");
94
 
18 ilm 95
                    }
96
                }
97
 
73 ilm 98
                this.mEcritures.put("ID_COMPTE_PCE", new Integer(idCompteFourn));
99
                this.mEcritures.put("DEBIT", new Long(prixTTC.getLongValue()));
100
                this.mEcritures.put("CREDIT", new Long(0));
101
                ajoutEcriture();
102
 
103
                // compte de reglement, caisse, CB, ...
104
                int idCompteRegl = typeRegRow.getInt("ID_COMPTE_PCE_FOURN");
105
                if (idCompteRegl <= 1) {
106
                    try {
107
                        idCompteRegl = ComptePCESQLElement.getIdComptePceDefault("AchatCB");
108
                    } catch (Exception e) {
109
                        e.printStackTrace();
18 ilm 110
                    }
111
                }
73 ilm 112
                this.mEcritures.put("ID_COMPTE_PCE", new Integer(idCompteRegl));
113
                this.mEcritures.put("DEBIT", new Long(0));
114
                this.mEcritures.put("CREDIT", new Long(prixTTC.getLongValue()));
115
                ajoutEcriture();
116
 
18 ilm 117
            }
118
        } else {
119
 
120
            Date dateEch = ModeDeReglementSQLElement.calculDate(modeRegRow.getInt("AJOURS"), modeRegRow.getInt("LENJOUR"), this.date);
121
            DateFormat dateFormat = new SimpleDateFormat();
122
            System.out.println("Date d'échéance " + dateFormat.format(dateEch));
123
            // System.out.println("Echeance" + dateEch);
124
 
125
            // Cheque
126
            if (typeRegRow.getID() == 2) {
127
 
128
                // Ajout dans cheque fournisseur
129
                paiementCheque(dateEch, idEchFourn, idRegMontant);
130
            } else {
131
 
132
                // Ajout dans echeance
73 ilm 133
                Map<String, Object> mEcheance = new HashMap<String, Object>();
18 ilm 134
                SQLRow rowMouv = tableMouvement.getRow(echeanceRow.getInt("ID_MOUVEMENT"));
135
                this.idMvt = getNewMouvement("ECHEANCE_FOURNISSEUR", 1, rowMouv.getID(), rowMouv.getInt("ID_PIECE"));
136
                mEcheance.put("ID_MOUVEMENT", new Integer(this.idMvt));
137
 
138
                mEcheance.put("DATE", dateEch);
139
                mEcheance.put("MONTANT", new Long(prixTTC.getLongValue()));
140
 
141
                if (idEchFourn > 1) {
142
                    mEcheance.put("ID_FOURNISSEUR", new Integer(echeanceRow.getInt("ID_FOURNISSEUR")));
143
                }
144
 
145
                SQLRowValues valEcheance = new SQLRowValues(base.getTable("ECHEANCE_FOURNISSEUR"), mEcheance);
73 ilm 146
                if (valEcheance.getInvalid() == null) {
147
                    // ajout de l'ecriture
148
                    SQLRow row = valEcheance.insert();
149
                    SQLRowValues rowVals = new SQLRowValues(tableMouvement);
150
                    rowVals.put("IDSOURCE", row.getID());
151
                    rowVals.update(this.idMvt);
152
                }
18 ilm 153
 
154
            }
155
        }
156
    }
157
 
73 ilm 158
    private void paiementCheque(Date dateEch, int idEchFourn, int idRegMontant) throws SQLException {
18 ilm 159
 
160
        SQLRow regMontantRow = base.getTable("REGLER_MONTANT").getRow(idRegMontant);
161
        SQLRow echeanceRow = base.getTable("ECHEANCE_FOURNISSEUR").getRow(idEchFourn);
162
        PrixTTC prixTTC = new PrixTTC(((Long) regMontantRow.getObject("MONTANT")).longValue());
163
 
164
        SQLRowValues valCheque = new SQLRowValues(base.getTable("CHEQUE_FOURNISSEUR"));
165
 
166
        if (idEchFourn > 1) {
167
            valCheque.put("ID_FOURNISSEUR", new Integer(echeanceRow.getInt("ID_FOURNISSEUR")));
168
        }
169
        valCheque.put("DATE_ACHAT", this.date);
170
        valCheque.put("DATE_MIN_DECAISSE", dateEch);
171
        SQLRow rowMvtPere = tableMouvement.getRow(echeanceRow.getInt("ID_MOUVEMENT"));
172
        this.idMvt = getNewMouvement("CHEQUE_FOURNISSEUR", 1, rowMvtPere.getID(), rowMvtPere.getInt("ID_PIECE"));
173
        valCheque.put("ID_MOUVEMENT", new Integer(this.idMvt));
174
        valCheque.put("MONTANT", new Long(prixTTC.getLongValue()));
175
 
73 ilm 176
        if (valCheque.getInvalid() == null) {
177
            // ajout de l'ecriture
178
            SQLRow row = valCheque.insert();
179
            SQLRowValues rowVals = new SQLRowValues(tableMouvement);
180
            rowVals.put("IDSOURCE", row.getID());
181
            rowVals.update(this.idMvt);
182
        }
18 ilm 183
 
184
    }
185
}