OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 83 | 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;
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);
75
 
18 ilm 76
        // si paiement comptant
77
        if ((modeRegRow.getInt("AJOURS") == 0) && (modeRegRow.getInt("LENJOUR") == 0)) {
78
 
79
            System.err.println("Règlement Comptant");
80
            // test Cheque
81
            if (typeRegRow.getID() == 2) {
82
 
83
                // Ajout dans cheque fournisseur
80 ilm 84
                paiementCheque(this.date, rowMvtSource, rowFournisseur.getID(), idRegMontant);
18 ilm 85
            } else {
86
 
87
                if (typeRegRow.getID() == 4) {
149 ilm 88
                    this.putValue("ID_JOURNAL", GenerationReglementAchat.journalCaisse);
18 ilm 89
                } else {
83 ilm 90
                    fillJournalBanqueFromRow(modeRegRow);
18 ilm 91
                }
92
 
93
                // SQLRow echeanceRow = base.getTable("ECHEANCE_FOURNISSEUR").getRow(idEchFourn);
94
 
80 ilm 95
                this.idMvt = getNewMouvement("REGLER_MONTANT", idRegMontant, rowMvtSource.getID(), rowMvtSource.getInt("ID_PIECE"));
96
 
149 ilm 97
                this.putValue("DATE", this.date);
98
                this.putValue("NOM", this.nom);
99
                this.putValue("ID_MOUVEMENT", new Integer(this.idMvt));
18 ilm 100
 
101
                // compte Fournisseurs
102
                int idCompteFourn = rowFournisseur.getInt("ID_COMPTE_PCE");
103
 
104
                if (idCompteFourn <= 1) {
105
                    idCompteFourn = rowPrefsCompte.getInt("ID_COMPTE_PCE_FOURNISSEUR");
106
                    if (idCompteFourn <= 1) {
73 ilm 107
                        idCompteFourn = ComptePCESQLElement.getIdComptePceDefault("Fournisseurs");
108
 
18 ilm 109
                    }
110
                }
111
 
149 ilm 112
                this.putValue("ID_COMPTE_PCE", new Integer(idCompteFourn));
113
                this.putValue("DEBIT", new Long(prixTTC.getLongValue()));
114
                this.putValue("CREDIT", new Long(0));
73 ilm 115
                ajoutEcriture();
116
 
117
                // compte de reglement, caisse, CB, ...
83 ilm 118
                fillCompteBanqueFromRow(modeRegRow, "AchatCB", true);
149 ilm 119
                this.putValue("DEBIT", new Long(0));
120
                this.putValue("CREDIT", new Long(prixTTC.getLongValue()));
73 ilm 121
                ajoutEcriture();
122
 
18 ilm 123
            }
124
        } else {
125
 
126
            Date dateEch = ModeDeReglementSQLElement.calculDate(modeRegRow.getInt("AJOURS"), modeRegRow.getInt("LENJOUR"), this.date);
127
            DateFormat dateFormat = new SimpleDateFormat();
128
            System.out.println("Date d'échéance " + dateFormat.format(dateEch));
129
            // System.out.println("Echeance" + dateEch);
130
 
131
            // Cheque
132
            if (typeRegRow.getID() == 2) {
133
 
134
                // Ajout dans cheque fournisseur
80 ilm 135
                paiementCheque(dateEch, rowMvtSource, rowFournisseur.getID(), idRegMontant);
18 ilm 136
            } else {
137
 
138
                // Ajout dans echeance
73 ilm 139
                Map<String, Object> mEcheance = new HashMap<String, Object>();
80 ilm 140
 
141
                this.idMvt = getNewMouvement("ECHEANCE_FOURNISSEUR", 1, rowMvtSource.getID(), rowMvtSource.getInt("ID_PIECE"));
18 ilm 142
                mEcheance.put("ID_MOUVEMENT", new Integer(this.idMvt));
143
 
144
                mEcheance.put("DATE", dateEch);
145
                mEcheance.put("MONTANT", new Long(prixTTC.getLongValue()));
146
 
80 ilm 147
                mEcheance.put("ID_FOURNISSEUR", rowFournisseur.getID());
18 ilm 148
 
149
                SQLRowValues valEcheance = new SQLRowValues(base.getTable("ECHEANCE_FOURNISSEUR"), mEcheance);
73 ilm 150
                if (valEcheance.getInvalid() == null) {
151
                    // ajout de l'ecriture
152
                    SQLRow row = valEcheance.insert();
153
                    SQLRowValues rowVals = new SQLRowValues(tableMouvement);
154
                    rowVals.put("IDSOURCE", row.getID());
155
                    rowVals.update(this.idMvt);
156
                }
18 ilm 157
 
158
            }
159
        }
160
    }
161
 
80 ilm 162
    private void paiementCheque(Date dateEch, SQLRow rowMvtSource, int idFourn, int idRegMontant) throws SQLException {
18 ilm 163
 
164
        SQLRow regMontantRow = base.getTable("REGLER_MONTANT").getRow(idRegMontant);
165
        PrixTTC prixTTC = new PrixTTC(((Long) regMontantRow.getObject("MONTANT")).longValue());
166
 
167
        SQLRowValues valCheque = new SQLRowValues(base.getTable("CHEQUE_FOURNISSEUR"));
168
 
80 ilm 169
        valCheque.put("ID_FOURNISSEUR", idFourn);
18 ilm 170
        valCheque.put("DATE_ACHAT", this.date);
171
        valCheque.put("DATE_MIN_DECAISSE", dateEch);
83 ilm 172
        if (!regMontantRow.isForeignEmpty("ID_MODE_REGLEMENT")) {
173
            SQLRow rowModeRegl = regMontantRow.getForeignRow("ID_MODE_REGLEMENT");
174
            valCheque.put("ID_" + BanqueSQLElement.TABLENAME, rowModeRegl.getInt("ID_" + BanqueSQLElement.TABLENAME));
175
        }
80 ilm 176
 
177
        this.idMvt = getNewMouvement("CHEQUE_FOURNISSEUR", 1, rowMvtSource.getID(), rowMvtSource.getInt("ID_PIECE"));
18 ilm 178
        valCheque.put("ID_MOUVEMENT", new Integer(this.idMvt));
179
        valCheque.put("MONTANT", new Long(prixTTC.getLongValue()));
180
 
73 ilm 181
        if (valCheque.getInvalid() == null) {
182
            // ajout de l'ecriture
183
            SQLRow row = valCheque.insert();
184
            SQLRowValues rowVals = new SQLRowValues(tableMouvement);
185
            rowVals.put("IDSOURCE", row.getID());
186
            rowVals.update(this.idMvt);
187
        }
18 ilm 188
 
189
    }
190
}