OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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