OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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