Dépôt officiel du code source de l'ERP OpenConcerto
Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU General Public License Version 3
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*/
package org.openconcerto.erp.core.sales.invoice.action;
import org.openconcerto.erp.core.finance.payment.element.EncaisserMontantSQLElement;
import org.openconcerto.erp.core.finance.payment.element.TypeReglementSQLElement;
import org.openconcerto.sql.model.DBRoot;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.model.Where;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
public class ImportReglementSage {
private final DBRoot root;
private EncaisserMontantSQLElement encaisserSQLElement;
Map<String, SQLRowValues> mapVf;
// <Reglements>
// <Reglement>
// <Code_Client>0010CHDMVA</Code_Client>
// <Date_Reglement>28/01/2019</Date_Reglement>
// <Numero_Facture>FA005088</Numero_Facture>
// <Montant_Reglement>294.00</Montant_Reglement>
// <Libelle_Reglement>Aucun</Libelle_Reglement>
// </Reglement>
// ...
// </Reglements>
public ImportReglementSage(EncaisserMontantSQLElement encaisserSQLElement) {
this.root = encaisserSQLElement.getTable().getDBRoot();
this.encaisserSQLElement = encaisserSQLElement;
}
public void importFromFile(File f) throws Exception {
final BufferedReader xmlReader = new BufferedReader(new InputStreamReader(new FileInputStream(f), Charset.forName("UTF8")));
SAXBuilder builder = new SAXBuilder();
final Document doc = builder.build(xmlReader);
Element root = doc.getRootElement();
List<Element> reglements = root.getChildren("Reglement");
List<String> numeroFacts = new ArrayList<>(reglements.size());
for (Element r : reglements) {
Element n = r.getChild("Numero_Facture");
numeroFacts.add(n.getValue());
}
this.mapVf = fetchFactures(numeroFacts);
for (Element r : reglements) {
Element n = r.getChild("Numero_Facture");
Element d = r.getChild("Date_Reglement");
Element m = r.getChild("Montant_Reglement");
Element c = r.getChild("Code_Client");
createEncaissement(n.getValue(), m.getValue(), d.getValue(), c.getValue());
}
}
private Map<String, SQLRowValues> fetchFactures(List<String> numerosFactures) {
final SQLTable tableVf = this.root.getTable("SAISIE_VENTE_FACTURE");
SQLRowValues rowValsFact = new SQLRowValues(tableVf);
rowValsFact.putNulls("DATE", "NUMERO");
rowValsFact.putRowValues("ID_MODE_REGLEMENT").putNulls("ID_TYPE_REGLEMENT");
rowValsFact.putRowValues("ID_CLIENT").putRowValues("ID_MODE_REGLEMENT").putNulls("ID_TYPE_REGLEMENT");
SQLRowValues rowValsEch = new SQLRowValues(this.root.getTable("ECHEANCE_CLIENT"));
rowValsEch.put("ID_SAISIE_VENTE_FACTURE", rowValsFact);
rowValsEch.put("REGLE", null);
rowValsEch.put("MONTANT", null);
rowValsEch.put("REG_COMPTA", null);
SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowValsFact);
List<SQLRowValues> res = fetcher.fetch(new Where(tableVf.getField("NUMERO"), numerosFactures));
Map<String, SQLRowValues> rowValsVf = new HashMap<>();
for (SQLRowValues sqlRowValues : res) {
rowValsVf.put(sqlRowValues.getString("NUMERO"), sqlRowValues);
}
return rowValsVf;
}
private void createEncaissement(String numeroFacture, String montant, String dateReglement, String codeClient) throws Exception {
SQLRowValues rowVals = this.mapVf.get(numeroFacture);
if (rowVals == null) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JOptionPane.showMessageDialog(null, "Impossible de trouver la facture numéro " + numeroFacture);
}
});
} else {
final Collection<SQLRowValues> referentRows = rowVals.getReferentRows(rowVals.getTable().getTable("ECHEANCE_CLIENT"));
if (referentRows.size() == 1) {
SQLRowValues ech = referentRows.iterator().next();
if (!ech.getBoolean("REGLE") && !ech.getBoolean("REG_COMPTA")) {
BigDecimal b = new BigDecimal(montant);
long montantL = b.movePointRight(2).setScale(0, RoundingMode.HALF_UP).longValue();
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
SQLRowValues rowValsEncaisser = new SQLRowValues(this.encaisserSQLElement.getTable());
rowValsEncaisser.put("ID_CLIENT", rowVals.getForeignID("ID_CLIENT"));
rowValsEncaisser.put("NOM", numeroFacture);
rowValsEncaisser.put("TIERS", codeClient);
rowValsEncaisser.put("DATE", format.parse(dateReglement));
rowValsEncaisser.put("MONTANT", montantL);
SQLRowAccessor rowValsMdrFact = rowVals.getForeign("ID_MODE_REGLEMENT");
int idType = rowValsMdrFact.getForeignID("ID_TYPE_REGLEMENT");
SQLRowValues rowValsMdrEnc = new SQLRowValues(rowValsMdrFact.getTable());
if (idType == TypeReglementSQLElement.CB || idType == TypeReglementSQLElement.ESPECE || idType == TypeReglementSQLElement.CHEQUE || idType == TypeReglementSQLElement.VIREMENT) {
rowValsMdrEnc.put("ID_TYPE_REGLEMENT", idType);
} else {
rowValsMdrFact = rowVals.getForeign("ID_CLIENT").getForeign("ID_MODE_REGLEMENT");
idType = rowValsMdrFact.getForeignID("ID_TYPE_REGLEMENT");
if (idType == TypeReglementSQLElement.CB || idType == TypeReglementSQLElement.ESPECE || idType == TypeReglementSQLElement.CHEQUE
|| idType == TypeReglementSQLElement.VIREMENT) {
rowValsMdrEnc.put("ID_TYPE_REGLEMENT", idType);
} else {
rowValsMdrEnc.put("ID_TYPE_REGLEMENT", TypeReglementSQLElement.VIREMENT);
}
}
rowValsMdrEnc.put("AJOURS", 0);
rowValsMdrEnc.put("LENJOUR", 0);
rowValsMdrEnc.put("COMPTANT", Boolean.TRUE);
rowValsEncaisser.put("ID_MODE_REGLEMENT", rowValsMdrEnc);
SQLRowValues rowValsEncaisserItem = new SQLRowValues(this.encaisserSQLElement.getTable().getTable("ENCAISSER_MONTANT_ELEMENT"));
rowValsEncaisserItem.put("ID_ECHEANCE_CLIENT", ech);
rowValsEncaisserItem.put("ID_ENCAISSER_MONTANT", rowValsEncaisser);
rowValsEncaisserItem.put("MONTANT_REGLE", montantL);
rowValsEncaisserItem.put("MONTANT_A_REGLER", ech.getLong("MONTANT"));
SQLRow rowEncaisser = rowValsEncaisser.commit();
this.encaisserSQLElement.regleFacture(rowEncaisser);
}
}
}
}
public static void main(String[] args) {
File f = new File("ExempleReglementSAge.xml");
ImportReglementSage i = new ImportReglementSage(null);
try {
i.importFromFile(f);
} catch (Exception e) {
e.printStackTrace();
}
}
}