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
80 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;
80 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;
132 ilm 20
import org.openconcerto.erp.core.finance.payment.element.TypeReglementSQLElement;
80 ilm 21
import org.openconcerto.erp.model.PrixTTC;
22
import org.openconcerto.sql.model.SQLRow;
23
import org.openconcerto.sql.model.SQLRowValues;
24
import org.openconcerto.sql.model.SQLTable;
25
import org.openconcerto.utils.ExceptionHandler;
26
 
27
import java.sql.SQLException;
28
import java.text.DateFormat;
29
import java.text.SimpleDateFormat;
30
import java.util.Calendar;
31
import java.util.Date;
32
import java.util.HashMap;
33
import java.util.Map;
34
 
35
// FIXME mettre toute les generations dans des threads à part
36
public final class GenerationMvtReglementFactureFournisseur extends GenerationEcritures implements Runnable {
37
 
38
    private int idfacture;
39
 
40
    // Journal Caisse
41
    private static final Integer journalCaisse = Integer.valueOf(JournalSQLElement.CAISSES);
42
 
43
    private static final SQLTable tablePrefCompte = base.getTable("PREFS_COMPTE");
44
    private static final SQLTable tableMouvement = base.getTable("MOUVEMENT");
45
    private static final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2);
46
    private int idPere = 1; // Id du mouvement pere
47
 
48
    public GenerationMvtReglementFactureFournisseur(int idFacture, int idMvt) {
49
        this.idfacture = idFacture;
50
        this.idPere = idMvt;
51
        new Thread(GenerationMvtReglementFactureFournisseur.this).start();
52
    }
53
 
54
    private void genereReglement() throws Exception {
55
 
56
        System.out.println("Génération des ecritures du reglement du mouvement " + this.idMvt);
57
 
58
        SQLRow saisieRow = base.getTable("FACTURE_FOURNISSEUR").getRow(this.idfacture);
59
        SQLRow modeRegRow = base.getTable("MODE_REGLEMENT").getRow(saisieRow.getInt("ID_MODE_REGLEMENT"));
60
        SQLRow typeRegRow = base.getTable("TYPE_REGLEMENT").getRow(modeRegRow.getInt("ID_TYPE_REGLEMENT"));
61
 
62
        System.out.println("Mode de reglement " + saisieRow.getInt("ID_MODE_REGLEMENT"));
63
 
64
        // PrixTTC prixTTC = new PrixTTC(((Long) saisieRow.getObject("MONTANT_TTC")).longValue());
65
 
66
        PrixTTC prixTTC;
67
        // int idAvoir = saisieRow.getInt("ID_AVOIR_FOURNISSEUR");
68
        // if (idAvoir > 1) {
69
        // SQLRow avoirRow = base.getTable("AVOIR_FOURNISSEUR").getRow(idAvoir);
70
        // long l = ((Number) avoirRow.getObject("MONTANT_TTC")).longValue();
71
        // prixTTC = new PrixTTC(((Long) saisieRow.getObject("T_TTC")).longValue() - l);
72
        // } else {
156 ilm 73
        prixTTC = new PrixTTC(((Long) saisieRow.getObject("NET_A_PAYER")).longValue());
80 ilm 74
        // }
75
 
76
        this.date = (Date) saisieRow.getObject("DATE");
77
        SQLRow rowFournisseur = base.getTable("FOURNISSEUR").getRow(saisieRow.getInt("ID_FOURNISSEUR"));
78
        this.nom = "Règlement Achat : " + rowFournisseur.getString("NOM") + " Facture : " + saisieRow.getObject("NUMERO").toString() + " (" + typeRegRow.getString("NOM") + ")";
79
 
80
        // si paiement comptant
81
        if ((modeRegRow.getInt("AJOURS") == 0) && (modeRegRow.getInt("LENJOUR") == 0)) {
82
 
83
            System.out.println("Règlement Comptant");
84
            // test Cheque
85
            if (typeRegRow.getID() == 2) {
86
                Calendar c = modeRegRow.getDate("DATE_DEPOT");
87
                if (c != null) {
88
                    paiementCheque(c.getTime());
89
                } else {
90
                    paiementCheque(this.date);
91
                }
92
            } else {
93
 
94
                this.idMvt = this.idPere;
95
 
96
                // iniatilisation des valeurs de la map
149 ilm 97
                this.putValue("DATE", new java.sql.Date(this.date.getTime()));
98
                this.putValue("NOM", this.nom);
99
                this.putValue("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
80 ilm 100
 
101
                if (typeRegRow.getID() == 4) {
149 ilm 102
                    this.putValue("ID_JOURNAL", GenerationMvtReglementFactureFournisseur.journalCaisse);
80 ilm 103
                } else {
104
 
83 ilm 105
                    fillJournalBanqueFromRow(modeRegRow);
80 ilm 106
                }
107
 
108
                // compte Fournisseurs
109
                int compteFourn = rowFournisseur.getInt("ID_COMPTE_PCE");
110
                if (compteFourn <= 1) {
111
                    compteFourn = rowPrefsCompte.getInt("ID_COMPTE_PCE_FOURNISSEUR");
112
                    if (compteFourn <= 1) {
113
                        compteFourn = ComptePCESQLElement.getIdComptePceDefault("Fournisseurs");
114
                    }
115
                }
116
 
149 ilm 117
                this.putValue("ID_COMPTE_PCE", Integer.valueOf(compteFourn));
118
                this.putValue("DEBIT", Long.valueOf(prixTTC.getLongValue()));
119
                this.putValue("CREDIT", Long.valueOf(0));
80 ilm 120
                ajoutEcriture();
121
 
122
                // compte de reglement, caisse, CB, ...
132 ilm 123
                if (typeRegRow.getID() == TypeReglementSQLElement.ESPECE) {
124
                    int idCompteRegl = typeRegRow.getInt("ID_COMPTE_PCE_FOURN");
125
                    if (idCompteRegl <= 1) {
126
                        idCompteRegl = ComptePCESQLElement.getIdComptePceDefault("AchatEspece");
127
                    }
128
 
149 ilm 129
                    this.putValue("ID_COMPTE_PCE", Integer.valueOf(idCompteRegl));
132 ilm 130
                } else {
131
                    fillCompteBanqueFromRow(modeRegRow, "AchatCB", true);
132
                }
149 ilm 133
                this.putValue("DEBIT", Long.valueOf(0));
134
                this.putValue("CREDIT", Long.valueOf(prixTTC.getLongValue()));
80 ilm 135
                ajoutEcriture();
136
            }
137
        } else {
138
 
139
            Date dateEch = ModeDeReglementSQLElement.calculDate(modeRegRow.getInt("AJOURS"), modeRegRow.getInt("LENJOUR"), this.date);
140
            DateFormat dateFormat = new SimpleDateFormat();
141
            System.out.println("Date d'échéance " + dateFormat.format(dateEch));
142
            // System.out.println("Echeance" + dateEch);
143
 
144
            // Cheque
145
            if (typeRegRow.getID() == 2) {
146
 
147
                // Ajout dans cheque fournisseur
148
                paiementCheque(dateEch);
149
            } else {
150
 
151
                // Ajout dans echeance
152
                Map<String, Object> mEcheance = new HashMap<String, Object>();
153
 
154
                SQLRow rowMvtPere = tableMouvement.getRow(this.idPere);
155
                this.idMvt = getNewMouvement("ECHEANCE_FOURNISSEUR", 1, this.idPere, rowMvtPere.getInt("ID_PIECE"));
156
 
157
                mEcheance.put("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
158
                mEcheance.put("DATE", new java.sql.Date(dateEch.getTime()));
159
                mEcheance.put("MONTANT", Long.valueOf(prixTTC.getLongValue()));
160
                mEcheance.put("ID_FOURNISSEUR", Integer.valueOf(saisieRow.getInt("ID_FOURNISSEUR")));
161
 
162
                SQLTable echeanceTable = base.getTable("ECHEANCE_FOURNISSEUR");
163
                SQLRowValues valEcheance = new SQLRowValues(echeanceTable, mEcheance);
164
 
165
                if (valEcheance.getInvalid() == null) {
166
                    // ajout de l'ecriture
167
                    SQLRow row = valEcheance.insert();
168
                    SQLRowValues rowVals = new SQLRowValues(tableMouvement);
169
                    rowVals.put("IDSOURCE", row.getID());
170
                    rowVals.update(this.idMvt);
171
                }
172
 
173
            }
174
        }
175
    }
176
 
177
    /**
178
     * Reglement par cheque. Crée un cheque fournisseur à décaisser.
179
     *
180
     * @param dateEch date d'echeance d'encaissement du cheque
181
     * @throws SQLException
182
     */
183
    private void paiementCheque(Date dateEch) throws SQLException {
184
 
185
        SQLRow saisieRow = base.getTable("FACTURE_FOURNISSEUR").getRow(this.idfacture);
186
        // PrixTTC prixTTC = new PrixTTC(((Long) saisieRow.getObject("MONTANT_TTC")).longValue());
187
 
188
        PrixTTC prixTTC;
189
        // int idAvoir = saisieRow.getInt("ID_AVOIR_FOURNISSEUR");
190
        // if (idAvoir > 1) {
191
        // SQLRow avoirRow = base.getTable("AVOIR_FOURNISSEUR").getRow(idAvoir);
192
        // long l = ((Number) avoirRow.getObject("MONTANT_TTC")).longValue();
193
        // prixTTC = new PrixTTC(((Long) saisieRow.getObject("T_TTC")).longValue() - l);
194
        // } else {
156 ilm 195
        prixTTC = new PrixTTC(((Long) saisieRow.getObject("NET_A_PAYER")).longValue());
80 ilm 196
        // }
197
 
198
        // Ajout dans cheque fournisseur
199
        Map<String, Object> mEncaisse = new HashMap<String, Object>();
200
        mEncaisse.put("ID_FOURNISSEUR", Integer.valueOf(saisieRow.getInt("ID_FOURNISSEUR")));
201
        mEncaisse.put("DATE_ACHAT", new java.sql.Date(this.date.getTime()));
202
        mEncaisse.put("DATE_MIN_DECAISSE", new java.sql.Date(dateEch.getTime()));
203
        mEncaisse.put("MONTANT", Long.valueOf(prixTTC.getLongValue()));
83 ilm 204
        if (!saisieRow.isForeignEmpty("ID_MODE_REGLEMENT")) {
205
            SQLRow rowModeRegl = saisieRow.getForeignRow("ID_MODE_REGLEMENT");
206
            mEncaisse.put("ID_" + BanqueSQLElement.TABLENAME, rowModeRegl.getInt("ID_" + BanqueSQLElement.TABLENAME));
207
        }
208
 
80 ilm 209
        SQLRow rowMvtPere = tableMouvement.getRow(this.idPere);
210
        this.idMvt = getNewMouvement("CHEQUE_FOURNISSEUR", 1, this.idPere, rowMvtPere.getInt("ID_PIECE"));
211
 
212
        mEncaisse.put("ID_MOUVEMENT", Integer.valueOf(this.idMvt));
213
 
214
        SQLTable chqFournTable = base.getTable("CHEQUE_FOURNISSEUR");
215
 
216
        SQLRowValues valDecaisse = new SQLRowValues(chqFournTable, mEncaisse);
217
 
218
        if (valDecaisse.getInvalid() == null) {
219
 
220
            // ajout de l'ecriture
221
            SQLRow row = valDecaisse.insert();
222
 
223
            SQLRowValues rowVals = new SQLRowValues(tableMouvement);
224
            rowVals.put("IDSOURCE", row.getID());
225
            rowVals.update(this.idMvt);
226
        }
227
 
228
    }
229
 
230
    public void run() {
231
        try {
232
            genereReglement();
233
        } catch (Exception e) {
234
            ExceptionHandler.handle("Erreur pendant la générations des écritures comptables", e);
235
            e.printStackTrace();
236
        }
237
    }
238
}