174 |
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.core.sales.invoice.action;
|
|
|
15 |
|
|
|
16 |
import org.openconcerto.erp.core.finance.payment.element.EncaisserMontantSQLElement;
|
|
|
17 |
import org.openconcerto.erp.core.finance.payment.element.TypeReglementSQLElement;
|
|
|
18 |
import org.openconcerto.sql.model.DBRoot;
|
|
|
19 |
import org.openconcerto.sql.model.SQLRow;
|
|
|
20 |
import org.openconcerto.sql.model.SQLRowAccessor;
|
|
|
21 |
import org.openconcerto.sql.model.SQLRowValues;
|
|
|
22 |
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
|
|
|
23 |
import org.openconcerto.sql.model.SQLTable;
|
|
|
24 |
import org.openconcerto.sql.model.Where;
|
|
|
25 |
|
|
|
26 |
import java.io.BufferedReader;
|
|
|
27 |
import java.io.File;
|
|
|
28 |
import java.io.FileInputStream;
|
|
|
29 |
import java.io.InputStreamReader;
|
|
|
30 |
import java.math.BigDecimal;
|
|
|
31 |
import java.math.RoundingMode;
|
|
|
32 |
import java.nio.charset.Charset;
|
|
|
33 |
import java.text.DateFormat;
|
|
|
34 |
import java.text.SimpleDateFormat;
|
|
|
35 |
import java.util.ArrayList;
|
|
|
36 |
import java.util.Collection;
|
|
|
37 |
import java.util.HashMap;
|
|
|
38 |
import java.util.List;
|
|
|
39 |
import java.util.Map;
|
|
|
40 |
|
|
|
41 |
import javax.swing.JOptionPane;
|
|
|
42 |
import javax.swing.SwingUtilities;
|
|
|
43 |
|
|
|
44 |
import org.jdom2.Document;
|
|
|
45 |
import org.jdom2.Element;
|
|
|
46 |
import org.jdom2.input.SAXBuilder;
|
|
|
47 |
|
|
|
48 |
public class ImportReglementSage {
|
|
|
49 |
|
|
|
50 |
private final DBRoot root;
|
|
|
51 |
private EncaisserMontantSQLElement encaisserSQLElement;
|
|
|
52 |
Map<String, SQLRowValues> mapVf;
|
|
|
53 |
// <Reglements>
|
|
|
54 |
// <Reglement>
|
|
|
55 |
// <Code_Client>0010CHDMVA</Code_Client>
|
|
|
56 |
// <Date_Reglement>28/01/2019</Date_Reglement>
|
|
|
57 |
// <Numero_Facture>FA005088</Numero_Facture>
|
|
|
58 |
// <Montant_Reglement>294.00</Montant_Reglement>
|
|
|
59 |
// <Libelle_Reglement>Aucun</Libelle_Reglement>
|
|
|
60 |
// </Reglement>
|
|
|
61 |
// ...
|
|
|
62 |
// </Reglements>
|
|
|
63 |
|
|
|
64 |
public ImportReglementSage(EncaisserMontantSQLElement encaisserSQLElement) {
|
|
|
65 |
this.root = encaisserSQLElement.getTable().getDBRoot();
|
|
|
66 |
this.encaisserSQLElement = encaisserSQLElement;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
public void importFromFile(File f) throws Exception {
|
|
|
70 |
final BufferedReader xmlReader = new BufferedReader(new InputStreamReader(new FileInputStream(f), Charset.forName("UTF8")));
|
|
|
71 |
SAXBuilder builder = new SAXBuilder();
|
|
|
72 |
final Document doc = builder.build(xmlReader);
|
|
|
73 |
|
|
|
74 |
Element root = doc.getRootElement();
|
|
|
75 |
List<Element> reglements = root.getChildren("Reglement");
|
|
|
76 |
List<String> numeroFacts = new ArrayList<>(reglements.size());
|
|
|
77 |
for (Element r : reglements) {
|
|
|
78 |
Element n = r.getChild("Numero_Facture");
|
|
|
79 |
numeroFacts.add(n.getValue());
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
this.mapVf = fetchFactures(numeroFacts);
|
|
|
83 |
for (Element r : reglements) {
|
|
|
84 |
Element n = r.getChild("Numero_Facture");
|
|
|
85 |
Element d = r.getChild("Date_Reglement");
|
|
|
86 |
Element m = r.getChild("Montant_Reglement");
|
|
|
87 |
Element c = r.getChild("Code_Client");
|
|
|
88 |
createEncaissement(n.getValue(), m.getValue(), d.getValue(), c.getValue());
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
private Map<String, SQLRowValues> fetchFactures(List<String> numerosFactures) {
|
|
|
94 |
|
|
|
95 |
final SQLTable tableVf = this.root.getTable("SAISIE_VENTE_FACTURE");
|
|
|
96 |
SQLRowValues rowValsFact = new SQLRowValues(tableVf);
|
|
|
97 |
rowValsFact.putNulls("DATE", "NUMERO");
|
|
|
98 |
rowValsFact.putRowValues("ID_MODE_REGLEMENT").putNulls("ID_TYPE_REGLEMENT");
|
|
|
99 |
rowValsFact.putRowValues("ID_CLIENT").putRowValues("ID_MODE_REGLEMENT").putNulls("ID_TYPE_REGLEMENT");
|
|
|
100 |
SQLRowValues rowValsEch = new SQLRowValues(this.root.getTable("ECHEANCE_CLIENT"));
|
|
|
101 |
rowValsEch.put("ID_SAISIE_VENTE_FACTURE", rowValsFact);
|
|
|
102 |
rowValsEch.put("REGLE", null);
|
|
|
103 |
rowValsEch.put("MONTANT", null);
|
|
|
104 |
rowValsEch.put("REG_COMPTA", null);
|
|
|
105 |
|
|
|
106 |
SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowValsFact);
|
|
|
107 |
List<SQLRowValues> res = fetcher.fetch(new Where(tableVf.getField("NUMERO"), numerosFactures));
|
|
|
108 |
Map<String, SQLRowValues> rowValsVf = new HashMap<>();
|
|
|
109 |
for (SQLRowValues sqlRowValues : res) {
|
|
|
110 |
rowValsVf.put(sqlRowValues.getString("NUMERO"), sqlRowValues);
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
return rowValsVf;
|
|
|
114 |
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
private void createEncaissement(String numeroFacture, String montant, String dateReglement, String codeClient) throws Exception {
|
|
|
118 |
SQLRowValues rowVals = this.mapVf.get(numeroFacture);
|
|
|
119 |
if (rowVals == null) {
|
|
|
120 |
SwingUtilities.invokeLater(new Runnable() {
|
|
|
121 |
|
|
|
122 |
@Override
|
|
|
123 |
public void run() {
|
|
|
124 |
JOptionPane.showMessageDialog(null, "Impossible de trouver la facture numéro " + numeroFacture);
|
|
|
125 |
}
|
|
|
126 |
});
|
|
|
127 |
} else {
|
|
|
128 |
final Collection<SQLRowValues> referentRows = rowVals.getReferentRows(rowVals.getTable().getTable("ECHEANCE_CLIENT"));
|
|
|
129 |
if (referentRows.size() == 1) {
|
|
|
130 |
SQLRowValues ech = referentRows.iterator().next();
|
|
|
131 |
if (!ech.getBoolean("REGLE") && !ech.getBoolean("REG_COMPTA")) {
|
|
|
132 |
BigDecimal b = new BigDecimal(montant);
|
|
|
133 |
long montantL = b.movePointRight(2).setScale(0, RoundingMode.HALF_UP).longValue();
|
|
|
134 |
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
|
|
|
135 |
SQLRowValues rowValsEncaisser = new SQLRowValues(this.encaisserSQLElement.getTable());
|
|
|
136 |
rowValsEncaisser.put("ID_CLIENT", rowVals.getForeignID("ID_CLIENT"));
|
|
|
137 |
rowValsEncaisser.put("NOM", numeroFacture);
|
|
|
138 |
rowValsEncaisser.put("TIERS", codeClient);
|
|
|
139 |
rowValsEncaisser.put("DATE", format.parse(dateReglement));
|
|
|
140 |
|
|
|
141 |
rowValsEncaisser.put("MONTANT", montantL);
|
|
|
142 |
|
|
|
143 |
SQLRowAccessor rowValsMdrFact = rowVals.getForeign("ID_MODE_REGLEMENT");
|
|
|
144 |
int idType = rowValsMdrFact.getForeignID("ID_TYPE_REGLEMENT");
|
|
|
145 |
|
|
|
146 |
SQLRowValues rowValsMdrEnc = new SQLRowValues(rowValsMdrFact.getTable());
|
|
|
147 |
|
|
|
148 |
if (idType == TypeReglementSQLElement.CB || idType == TypeReglementSQLElement.ESPECE || idType == TypeReglementSQLElement.CHEQUE || idType == TypeReglementSQLElement.VIREMENT) {
|
|
|
149 |
rowValsMdrEnc.put("ID_TYPE_REGLEMENT", idType);
|
|
|
150 |
} else {
|
|
|
151 |
rowValsMdrFact = rowVals.getForeign("ID_CLIENT").getForeign("ID_MODE_REGLEMENT");
|
|
|
152 |
idType = rowValsMdrFact.getForeignID("ID_TYPE_REGLEMENT");
|
|
|
153 |
if (idType == TypeReglementSQLElement.CB || idType == TypeReglementSQLElement.ESPECE || idType == TypeReglementSQLElement.CHEQUE
|
|
|
154 |
|| idType == TypeReglementSQLElement.VIREMENT) {
|
|
|
155 |
rowValsMdrEnc.put("ID_TYPE_REGLEMENT", idType);
|
|
|
156 |
} else {
|
|
|
157 |
rowValsMdrEnc.put("ID_TYPE_REGLEMENT", TypeReglementSQLElement.VIREMENT);
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
rowValsMdrEnc.put("AJOURS", 0);
|
|
|
161 |
rowValsMdrEnc.put("LENJOUR", 0);
|
|
|
162 |
rowValsMdrEnc.put("COMPTANT", Boolean.TRUE);
|
|
|
163 |
rowValsEncaisser.put("ID_MODE_REGLEMENT", rowValsMdrEnc);
|
|
|
164 |
|
|
|
165 |
SQLRowValues rowValsEncaisserItem = new SQLRowValues(this.encaisserSQLElement.getTable().getTable("ENCAISSER_MONTANT_ELEMENT"));
|
|
|
166 |
rowValsEncaisserItem.put("ID_ECHEANCE_CLIENT", ech);
|
|
|
167 |
rowValsEncaisserItem.put("ID_ENCAISSER_MONTANT", rowValsEncaisser);
|
|
|
168 |
rowValsEncaisserItem.put("MONTANT_REGLE", montantL);
|
|
|
169 |
rowValsEncaisserItem.put("MONTANT_A_REGLER", ech.getLong("MONTANT"));
|
|
|
170 |
|
|
|
171 |
SQLRow rowEncaisser = rowValsEncaisser.commit();
|
|
|
172 |
|
180 |
ilm |
173 |
this.encaisserSQLElement.regleFacture(rowEncaisser, null, false);
|
174 |
ilm |
174 |
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
}
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
public static void main(String[] args) {
|
|
|
181 |
File f = new File("ExempleReglementSAge.xml");
|
|
|
182 |
ImportReglementSage i = new ImportReglementSage(null);
|
|
|
183 |
try {
|
|
|
184 |
i.importFromFile(f);
|
|
|
185 |
} catch (Exception e) {
|
|
|
186 |
e.printStackTrace();
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
}
|