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