OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Rev 182 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 177 Rev 180
Line 11... Line 11...
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.erp.core.supplychain.purchase.importer;
14
 package org.openconcerto.erp.core.supplychain.purchase.importer;
15
 
15
 
-
 
16
import org.openconcerto.erp.core.finance.payment.element.ModeDeReglementSQLElement;
16
import org.openconcerto.sql.model.DBRoot;
17
import org.openconcerto.sql.model.DBRoot;
-
 
18
import org.openconcerto.sql.model.SQLRow;
-
 
19
import org.openconcerto.sql.model.SQLRowAccessor;
-
 
20
import org.openconcerto.sql.model.SQLRowValues;
-
 
21
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
-
 
22
import org.openconcerto.sql.model.SQLTable;
-
 
23
import org.openconcerto.sql.model.Where;
17
 
24
 
18
import java.math.BigDecimal;
25
import java.math.BigDecimal;
19
import java.text.DecimalFormat;
26
import java.text.DecimalFormat;
20
import java.text.DecimalFormatSymbols;
27
import java.text.DecimalFormatSymbols;
21
import java.text.SimpleDateFormat;
28
import java.text.SimpleDateFormat;
22
import java.util.ArrayList;
29
import java.util.Collection;
23
import java.util.Date;
30
import java.util.Date;
-
 
31
import java.util.HashMap;
24
import java.util.List;
32
import java.util.List;
25
import java.util.Locale;
33
import java.util.Locale;
-
 
34
import java.util.Map;
26
 
35
 
27
import org.jdom2.Document;
36
import org.jdom2.Document;
28
import org.jdom2.Element;
37
import org.jdom2.Element;
29
import org.jdom2.Namespace;
38
import org.jdom2.Namespace;
30
import org.jdom2.output.Format;
39
import org.jdom2.output.Format;
Line 32... Line 41...
32
 
41
 
33
/**
42
/**
34
 * FacturX export (EN 16931 format)
43
 * FacturX export (EN 16931 format)
35
 */
44
 */
36
public class FacturXExporter {
45
public class FacturXExporter {
37
 
-
 
38
    private class Tax {
46
    private class Tax {
39
        // Taux, ex pour 20% : 20.00
47
        // Taux, ex pour 20% : 20.00
40
        BigDecimal rate;
48
        final BigDecimal rate;
41
        // Montant de la TVA
49
        // Montant de la TVA
42
        BigDecimal amount;
50
        BigDecimal amount = BigDecimal.ZERO;
43
 
51
 
44
        // montant HT sur lequel s'applique la TVA
52
        // montant HT sur lequel s'applique la TVA
45
        BigDecimal basisAmount;
53
        BigDecimal basisAmount = BigDecimal.ZERO;
-
 
54
 
-
 
55
        public Tax(BigDecimal rate) {
-
 
56
            this.rate = rate;
-
 
57
        }
-
 
58
 
-
 
59
        public void add(BigDecimal taxAmount,
46
 
60
 
-
 
61
                BigDecimal basisAmount) {
-
 
62
            this.amount = this.amount.add(taxAmount);
-
 
63
            this.basisAmount = this.basisAmount.add(basisAmount);
-
 
64
        }
47
    }
65
    }
48
 
66
 
49
    public static Namespace QDT_NS = Namespace.getNamespace("qdt", "urn:un:unece:uncefact:data:standard:QualifiedDataType:100");
67
    public static Namespace QDT_NS = Namespace.getNamespace("qdt", "urn:un:unece:uncefact:data:standard:QualifiedDataType:100");
50
    public static Namespace RAM_NS = Namespace.getNamespace("ram", "urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100");
68
    public static Namespace RAM_NS = Namespace.getNamespace("ram", "urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100");
51
    public static Namespace RSM_NS = Namespace.getNamespace("rsm", "urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100");
69
    public static Namespace RSM_NS = Namespace.getNamespace("rsm", "urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100");
Line 54... Line 72...
54
 
72
 
55
    public static void main(String[] args) {
73
    public static void main(String[] args) {
56
 
74
 
57
        FacturXExporter ex = new FacturXExporter();
75
        FacturXExporter ex = new FacturXExporter();
58
        System.err.println("FacturXExporter.main() " + ex.checkEAN13("5987854125989"));
76
        System.err.println("FacturXExporter.main() " + ex.checkEAN13("5987854125989"));
59
        String xml = ex.createXMLFrom(null, 1);
77
        // String xml = ex.createXMLFrom(null, 1);
60
        System.out.println(xml);
78
        // System.out.println(xml);
61
    }
79
    }
62
 
80
 
63
    public String createXMLFrom(DBRoot row, int invoiceId) {
81
    DecimalFormat formatAmount = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
64
 
82
 
-
 
83
    private boolean useCommonClient = false;
-
 
84
 
-
 
85
    public String createXMLFrom(DBRoot dbRoot, int invoiceId, SQLRow societeRow) {
-
 
86
 
-
 
87
        SQLTable factureTable = dbRoot.getTable("SAISIE_VENTE_FACTURE");
-
 
88
        SQLRowValues rowValsToFetch = new SQLRowValues(factureTable);
-
 
89
        rowValsToFetch.putNulls("NUMERO", "DATE", "NOM", "T_TVA", "T_HT", "T_TTC", "NET_A_PAYER");
-
 
90
        rowValsToFetch.putRowValues("ID_MODE_REGLEMENT").putNulls("COMPTANT", "AJOURS", "LENJOUR", "DATE_FACTURE", "FIN_MOIS").putRowValues("ID_TYPE_REGLEMENT").putNulls("NOM");
-
 
91
        SQLRowValues putRowValuesClient = rowValsToFetch.putRowValues("ID_CLIENT");
-
 
92
        putRowValuesClient.putNulls("NOM", "RESPONSABLE", "SIRET", "NUMERO_TVA", "MAIL", "TEL");
-
 
93
        if (putRowValuesClient.getTable().contains("ID_CLIENT")) {
-
 
94
            this.useCommonClient = true;
-
 
95
            putRowValuesClient.putRowValues("ID_CLIENT").putNulls("NOM", "RESPONSABLE", "SIRET", "NUMERO_TVA", "MAIL", "TEL").putRowValues("ID_ADRESSE").putNulls("RUE", "VILLE", "CODE_POSTAL",
-
 
96
                    "PAYS");
-
 
97
        }
-
 
98
        putRowValuesClient.putRowValues("ID_ADRESSE").putNulls("RUE", "VILLE", "CODE_POSTAL", "PAYS");
-
 
99
        putRowValuesClient.putRowValues("ID_ADRESSE_F").putNulls("RUE", "VILLE", "CODE_POSTAL", "PAYS");
-
 
100
        rowValsToFetch.putRowValues("ID_ADRESSE").putNulls("RUE", "VILLE", "CODE_POSTAL", "PAYS");
-
 
101
 
-
 
102
        SQLTable factureItemtable = dbRoot.getTable("SAISIE_VENTE_FACTURE_ELEMENT");
-
 
103
        SQLRowValues rowValsItemsToFetch = new SQLRowValues(factureItemtable);
-
 
104
 
-
 
105
        rowValsItemsToFetch.putNulls("NIVEAU", "CODE", "NOM", "QTE", "QTE_UNITAIRE", "PV_HT", "T_PV_HT", "T_PV_TTC").putRowValues("ID_TAXE").put("TAUX", null);
-
 
106
        rowValsItemsToFetch.put("ID_SAISIE_VENTE_FACTURE", rowValsToFetch);
-
 
107
        List<SQLRowValues> factures = SQLRowValuesListFetcher.create(rowValsToFetch).fetch(new Where(factureTable.getKey(), "=", invoiceId));
-
 
108
        SQLRowValues facture = factures.get(0);
-
 
109
 
-
 
110
        Collection<? extends SQLRowAccessor> factureItems = facture.getReferentRows(factureItemtable.getField("ID_SAISIE_VENTE_FACTURE"));
-
 
111
 
65
        List<Tax> taxes = new ArrayList<>();
112
        Map<Integer, Tax> taxes = new HashMap<>();
-
 
113
 
66
        Tax t1 = new Tax();
114
        // Tax t1 = new Tax();
67
        t1.rate = new BigDecimal("20.0");
115
        // t1.rate = new BigDecimal("20.0");
68
        t1.amount = new BigDecimal("40.0");
116
        // t1.amount = new BigDecimal("40.0");
69
        t1.basisAmount = new BigDecimal("200.0");
117
        // t1.basisAmount = new BigDecimal("200.0");
70
        taxes.add(t1);
118
        // taxes.add(t1);
71
 
119
 
72
        DecimalFormat format = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
-
 
73
        DecimalFormat fQty = new DecimalFormat("#0.########", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
120
        DecimalFormat fQty = new DecimalFormat("#0.########", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
74
        String invoiceNumber = "-";
121
        String invoiceNumber = facture.getString("NUMERO");
75
        Date invoiceDate = new Date();
122
        Date invoiceDate = facture.getDate("DATE").getTime();
76
        String regNote = "SARL au capital de 50 000 EUR";
123
        String regNote = societeRow.getString("TYPE") + " au capital de " + societeRow.getObject("CAPITAL") + " €";
77
        String legNote = "RCS MAVILLE 123 456 789";
124
        String legNote = societeRow.getString("RCS");
78
        int nbLines = 4;
125
        int nbLines = factureItems.size();
79
 
126
 
80
        final Document doc = new Document();
127
        final Document doc = new Document();
81
        Element root = new Element("CrossIndustryInvoice", RSM_NS);
128
        Element root = new Element("CrossIndustryInvoice", RSM_NS);
82
        root.addNamespaceDeclaration(QDT_NS);
129
        root.addNamespaceDeclaration(QDT_NS);
83
        root.addNamespaceDeclaration(RAM_NS);
130
        root.addNamespaceDeclaration(RAM_NS);
Line 130... Line 177...
130
        addIncludedNote(regNote, "REG", ed);
177
        addIncludedNote(regNote, "REG", ed);
131
        addIncludedNote(legNote, "ABL", ed);
178
        addIncludedNote(legNote, "ABL", ed);
132
 
179
 
133
        // SupplyChainTradeTransaction
180
        // SupplyChainTradeTransaction
134
        Element eSupplyChainTradeTransaction = new Element("SupplyChainTradeTransaction", RSM_NS);
181
        Element eSupplyChainTradeTransaction = new Element("SupplyChainTradeTransaction", RSM_NS);
135
        for (int i = 0; i < nbLines; i++) {
182
        int lineID = 1;
136
            String productCode = "a";
183
        for (SQLRowAccessor rowItem : factureItems) {
137
            String productName = "b";
184
            String productCode = rowItem.getString("CODE");
-
 
185
            String productName = rowItem.getString("NOM").trim().length() == 0 ? "Ligne" : rowItem.getString("NOM");
138
 
186
 
-
 
187
            final BigDecimal pHT, pTTC, totalHTLigne, totalTTCLigne;
-
 
188
            SQLRowAccessor taxeItem = rowItem.getForeign("ID_TAXE");
-
 
189
            BigDecimal taxValue = new BigDecimal(taxeItem.getFloat("TAUX")).setScale(2);
-
 
190
            if (rowItem.getInt("NIVEAU") != 1) {
139
            BigDecimal pHT = new BigDecimal("12.46");
191
                pHT = BigDecimal.ZERO;
-
 
192
                pTTC = BigDecimal.ZERO;
140
            BigDecimal tax = new BigDecimal("0.20");
193
                totalHTLigne = BigDecimal.ZERO;
141
            BigDecimal pTTC = new BigDecimal("14.95");
194
                totalTTCLigne = BigDecimal.ZERO;
-
 
195
            } else {
142
            BigDecimal qte = new BigDecimal("10.0");
196
                pHT = rowItem.getBigDecimal("PV_HT");
-
 
197
                pTTC = pHT.multiply(taxValue.add(BigDecimal.ONE));
143
            BigDecimal totalHTLigne = new BigDecimal("124.60");
198
                totalHTLigne = rowItem.getBigDecimal("T_PV_HT");
-
 
199
                totalTTCLigne = rowItem.getBigDecimal("T_PV_TTC");
-
 
200
            }
-
 
201
 
-
 
202
            int idTaxe = taxeItem.getID();
-
 
203
            if (!taxes.containsKey(idTaxe)) {
-
 
204
                Tax t = new Tax(taxValue);
-
 
205
                taxes.put(idTaxe, t);
-
 
206
            }
-
 
207
            taxes.get(idTaxe).add(totalTTCLigne.subtract(totalHTLigne), totalHTLigne);
-
 
208
            BigDecimal qte = new BigDecimal(rowItem.getInt("QTE")).multiply(rowItem.getBigDecimal("QTE_UNITAIRE"));
144
 
209
 
145
            Element eLineItem = new Element("IncludedSupplyChainTradeLineItem", RAM_NS);
210
            Element eLineItem = new Element("IncludedSupplyChainTradeLineItem", RAM_NS);
-
 
211
 
146
            // AssociatedDocumentLineDocument
212
            // AssociatedDocumentLineDocument
147
            Element eAssociatedDocumentLineDocument = new Element("AssociatedDocumentLineDocument", RAM_NS);
213
            Element eAssociatedDocumentLineDocument = new Element("AssociatedDocumentLineDocument", RAM_NS);
148
            Element eLineID = new Element("LineID", RAM_NS);
214
            Element eLineID = new Element("LineID", RAM_NS);
149
            eLineID.setText(String.valueOf(i + 1));
215
            eLineID.setText(String.valueOf(lineID++));
150
            eAssociatedDocumentLineDocument.addContent(eLineID);
216
            eAssociatedDocumentLineDocument.addContent(eLineID);
151
            eLineItem.addContent(eAssociatedDocumentLineDocument);
217
            eLineItem.addContent(eAssociatedDocumentLineDocument);
152
            // SpecifiedTradeProduct
218
            // SpecifiedTradeProduct
153
            Element eSpecifiedTradeProduct = new Element("SpecifiedTradeProduct", RAM_NS);
219
            Element eSpecifiedTradeProduct = new Element("SpecifiedTradeProduct", RAM_NS);
154
            if (!productCode.isEmpty()) {
220
            if (!productCode.isEmpty()) {
Line 174... Line 240...
174
            //
240
            //
175
            Element eSpecifiedLineTradeAgreement = new Element("SpecifiedLineTradeAgreement", RAM_NS);
241
            Element eSpecifiedLineTradeAgreement = new Element("SpecifiedLineTradeAgreement", RAM_NS);
176
            // Prix unitaire TTC
242
            // Prix unitaire TTC
177
            Element eGrossPriceProductTradePrice = new Element("GrossPriceProductTradePrice", RAM_NS);
243
            Element eGrossPriceProductTradePrice = new Element("GrossPriceProductTradePrice", RAM_NS);
178
            Element eChargeAmount = new Element("ChargeAmount", RAM_NS);
244
            Element eChargeAmount = new Element("ChargeAmount", RAM_NS);
179
            eChargeAmount.setText(format.format(pTTC));
245
            eChargeAmount.setText(formatAmount.format(pTTC));
180
            eGrossPriceProductTradePrice.addContent(eChargeAmount);
246
            eGrossPriceProductTradePrice.addContent(eChargeAmount);
181
            Element eAppliedTradeAllowanceCharge = new Element("AppliedTradeAllowanceCharge", RAM_NS);
247
            Element eAppliedTradeAllowanceCharge = new Element("AppliedTradeAllowanceCharge", RAM_NS);
182
            Element eChargeIndicator = new Element("ChargeIndicator", RAM_NS);
248
            Element eChargeIndicator = new Element("ChargeIndicator", RAM_NS);
183
            Element eIndicator = new Element("ChargeIndicator", UDT_NS);
249
            Element eIndicator = new Element("Indicator", UDT_NS);
184
            // pas de remise
250
            // pas de remise
185
            eIndicator.setText("false");
251
            eIndicator.setText("false");
186
            eChargeIndicator.addContent(eIndicator);
252
            eChargeIndicator.addContent(eIndicator);
187
            eAppliedTradeAllowanceCharge.addContent(eChargeIndicator);
253
            eAppliedTradeAllowanceCharge.addContent(eChargeIndicator);
188
 
254
 
189
            Element eActualAmount = new Element("ActualAmount", RAM_NS);
255
            Element eActualAmount = new Element("ActualAmount", RAM_NS);
190
            // Montant de TVA (unitaire)
256
            // Montant de TVA (unitaire)
191
            eActualAmount.setText(format.format(pTTC.subtract(pHT)));
257
            eActualAmount.setText(formatAmount.format(pTTC.subtract(pHT)));
192
 
258
 
193
            eAppliedTradeAllowanceCharge.addContent(eActualAmount);
259
            eAppliedTradeAllowanceCharge.addContent(eActualAmount);
194
 
260
 
195
            eGrossPriceProductTradePrice.addContent(eAppliedTradeAllowanceCharge);
261
            // eGrossPriceProductTradePrice.addContent(eAppliedTradeAllowanceCharge);
196
 
262
 
197
            eSpecifiedLineTradeAgreement.addContent(eGrossPriceProductTradePrice);
263
            eSpecifiedLineTradeAgreement.addContent(eGrossPriceProductTradePrice);
198
            // Prix unitaire HT
264
            // Prix unitaire HT
199
            Element eNetPriceProductTradePrice = new Element("NetPriceProductTradePrice", RAM_NS);
265
            Element eNetPriceProductTradePrice = new Element("NetPriceProductTradePrice", RAM_NS);
200
            Element eChargeAmountHT = new Element("ChargeAmount", RAM_NS);
266
            Element eChargeAmountHT = new Element("ChargeAmount", RAM_NS);
201
            eChargeAmountHT.setText(format.format(pHT));
267
            eChargeAmountHT.setText(formatAmount.format(pHT));
202
 
268
 
203
            eNetPriceProductTradePrice.addContent(eChargeAmountHT);
269
            eNetPriceProductTradePrice.addContent(eChargeAmountHT);
204
            eSpecifiedLineTradeAgreement.addContent(eNetPriceProductTradePrice);
270
            eSpecifiedLineTradeAgreement.addContent(eNetPriceProductTradePrice);
205
            eLineItem.addContent(eSpecifiedLineTradeAgreement);
271
            eLineItem.addContent(eSpecifiedLineTradeAgreement);
206
 
272
 
Line 238... Line 304...
238
            eCategoryCode.setText("S");
304
            eCategoryCode.setText("S");
239
            eApplicableTradeTaxt.addContent(eCategoryCode);
305
            eApplicableTradeTaxt.addContent(eCategoryCode);
240
 
306
 
241
            Element eRateApplicablePercent = new Element("RateApplicablePercent", RAM_NS);
307
            Element eRateApplicablePercent = new Element("RateApplicablePercent", RAM_NS);
242
            // 20% -> 20.0
308
            // 20% -> 20.0
243
            eRateApplicablePercent.setText(format.format(tax.multiply(new BigDecimal(100))));
309
            eRateApplicablePercent.setText(formatAmount.format(taxValue));
244
            eApplicableTradeTaxt.addContent(eRateApplicablePercent);
310
            eApplicableTradeTaxt.addContent(eRateApplicablePercent);
245
 
311
 
246
            eSpecifiedLineTradeSettlement.addContent(eApplicableTradeTaxt);
312
            eSpecifiedLineTradeSettlement.addContent(eApplicableTradeTaxt);
247
 
313
 
248
            Element eSpecifiedTradeSettlementLineMonetarySummation = new Element("SpecifiedTradeSettlementLineMonetarySummation", RAM_NS);
314
            Element eSpecifiedTradeSettlementLineMonetarySummation = new Element("SpecifiedTradeSettlementLineMonetarySummation", RAM_NS);
249
            // Total HT
315
            // Total HT
250
            Element eLineTotalAmount = new Element("LineTotalAmount", RAM_NS);
316
            Element eLineTotalAmount = new Element("LineTotalAmount", RAM_NS);
251
            eLineTotalAmount.setText(format.format(totalHTLigne));
317
            eLineTotalAmount.setText(formatAmount.format(totalHTLigne));
252
            eSpecifiedTradeSettlementLineMonetarySummation.addContent(eLineTotalAmount);
318
            eSpecifiedTradeSettlementLineMonetarySummation.addContent(eLineTotalAmount);
253
            eSpecifiedLineTradeSettlement.addContent(eSpecifiedTradeSettlementLineMonetarySummation);
319
            eSpecifiedLineTradeSettlement.addContent(eSpecifiedTradeSettlementLineMonetarySummation);
254
 
320
 
255
            eLineItem.addContent(eSpecifiedLineTradeSettlement);
321
            eLineItem.addContent(eSpecifiedLineTradeSettlement);
256
            //
322
            //
257
            eSupplyChainTradeTransaction.addContent(eLineItem);
323
            eSupplyChainTradeTransaction.addContent(eLineItem);
258
        }
324
        }
259
 
325
 
260
        addApplicableHeader(eSupplyChainTradeTransaction, taxes);
-
 
261
 
-
 
262
        ed.addContent(eSupplyChainTradeTransaction);
-
 
263
 
-
 
264
        root.addContent(ed);
326
        root.addContent(ed);
-
 
327
        addApplicableHeader(societeRow, facture, eSupplyChainTradeTransaction, taxes.values());
-
 
328
 
-
 
329
        root.addContent(eSupplyChainTradeTransaction);
265
 
330
 
266
        final XMLOutputter xmlOutput = new XMLOutputter();
331
        final XMLOutputter xmlOutput = new XMLOutputter();
267
        xmlOutput.setFormat(Format.getPrettyFormat());
332
        xmlOutput.setFormat(Format.getPrettyFormat());
268
        return xmlOutput.outputString(doc);
333
        return xmlOutput.outputString(doc);
269
    }
334
    }
Line 271... Line 336...
271
    public void addValue(Element parent, Element element, String value) {
336
    public void addValue(Element parent, Element element, String value) {
272
        element.setText(value);
337
        element.setText(value);
273
        parent.addContent(element);
338
        parent.addContent(element);
274
    }
339
    }
275
 
340
 
276
    private void addApplicableHeader(Element eSupplyChainTradeTransaction, List<Tax> taxes) {
341
    private void addApplicableHeader(SQLRowAccessor rowSociete, SQLRowAccessor rowFacture, Element eSupplyChainTradeTransaction, Collection<Tax> taxes) {
277
        //
342
        //
278
        // ApplicableHeaderTradeAgreement
343
        // ApplicableHeaderTradeAgreement
279
        //
344
        //
280
        final Element eApplicableHeaderTradeAgreement = new Element("ApplicableHeaderTradeAgreement", RAM_NS);
345
        final Element eApplicableHeaderTradeAgreement = new Element("ApplicableHeaderTradeAgreement", RAM_NS);
281
        addSupplierInfo(eApplicableHeaderTradeAgreement);
346
        addSupplierInfo(eApplicableHeaderTradeAgreement, rowSociete);
282
        addBuyerInfo(eApplicableHeaderTradeAgreement);
347
        addBuyerInfo(eApplicableHeaderTradeAgreement, rowFacture);
283
 
348
 
284
        String orderRef = "";
349
        String orderRef = "";
285
        String contractRef = "";
350
        String contractRef = "";
286
        // Date de livraison, facultative
351
        // Date de livraison, facultative
287
        Date effectiveDeliveryDate = new Date(65454654);
352
        Date effectiveDeliveryDate = rowFacture.getDate("DATE").getTime();
288
        String invoiceNumber = "FA-2017-0010";
353
        String invoiceNumber = rowFacture.getString("NUMERO");
289
        String currencyCode = "EUR";
354
        String currencyCode = "EUR";
-
 
355
        SQLRowAccessor foreignMdr = rowFacture.getForeign("ID_MODE_REGLEMENT");
290
        String dueDescription = "30% d'acompte, solde à 30 j";
356
        int ajours = foreignMdr.getInt("AJOURS");
291
        Date dueDate = new Date(65455654);
357
        int lenjour = foreignMdr.getInt("LENJOUR");
-
 
358
        String dueDescription = (ajours > 0 ? "A " + ajours : "") + (lenjour > 0 ? " le " + lenjour : "");
-
 
359
        Date dueDate = ModeDeReglementSQLElement.calculDate(foreignMdr, rowFacture.getDate("DATE").getTime());
292
 
360
 
293
        final Element eBuyerOrderReferencedDocument = new Element("BuyerOrderReferencedDocument", RAM_NS);
361
        final Element eBuyerOrderReferencedDocument = new Element("BuyerOrderReferencedDocument", RAM_NS);
294
        addValue(eBuyerOrderReferencedDocument, new Element("IssuerAssignedID", RAM_NS), orderRef);
362
        addValue(eBuyerOrderReferencedDocument, new Element("IssuerAssignedID", RAM_NS), orderRef);
295
        eApplicableHeaderTradeAgreement.addContent(eBuyerOrderReferencedDocument);
363
        eApplicableHeaderTradeAgreement.addContent(eBuyerOrderReferencedDocument);
296
 
364
 
Line 328... Line 396...
328
        //
396
        //
329
        final Element eApplicableHeaderTradeSettlement = new Element("ApplicableHeaderTradeSettlement", RAM_NS);
397
        final Element eApplicableHeaderTradeSettlement = new Element("ApplicableHeaderTradeSettlement", RAM_NS);
330
        addValue(eApplicableHeaderTradeSettlement, new Element("PaymentReference", RAM_NS), invoiceNumber);
398
        addValue(eApplicableHeaderTradeSettlement, new Element("PaymentReference", RAM_NS), invoiceNumber);
331
        addValue(eApplicableHeaderTradeSettlement, new Element("InvoiceCurrencyCode", RAM_NS), currencyCode);
399
        addValue(eApplicableHeaderTradeSettlement, new Element("InvoiceCurrencyCode", RAM_NS), currencyCode);
332
        final Element aSpecifiedTradeSettlementPaymentMeans = new Element("SpecifiedTradeSettlementPaymentMeans", RAM_NS);
400
        final Element aSpecifiedTradeSettlementPaymentMeans = new Element("SpecifiedTradeSettlementPaymentMeans", RAM_NS);
-
 
401
        addValue(aSpecifiedTradeSettlementPaymentMeans, new Element("TypeCode", RAM_NS), "57");
333
 
402
 
334
        // <ram:TypeCode>30</ram:TypeCode>
403
        // <ram:TypeCode>30</ram:TypeCode>
335
        // <ram:Information>Virement sur compte Banque Fiducial</ram:Information>
404
        // <ram:Information>Virement sur compte Banque Fiducial</ram:Information>
336
        // <ram:PayeePartyCreditorFinancialAccount>
405
        // <ram:PayeePartyCreditorFinancialAccount>
337
        // <ram:IBANID>FR2012421242124212421242124</ram:IBANID>
406
        // <ram:IBANID>FR2012421242124212421242124</ram:IBANID>
Line 378... Line 447...
378
        addDateTime(dueDate, eDueDateDateTime);
447
        addDateTime(dueDate, eDueDateDateTime);
379
        eSpecifiedTradePaymentTerms.addContent(eDueDateDateTime);
448
        eSpecifiedTradePaymentTerms.addContent(eDueDateDateTime);
380
        eApplicableHeaderTradeSettlement.addContent(eSpecifiedTradePaymentTerms);
449
        eApplicableHeaderTradeSettlement.addContent(eSpecifiedTradePaymentTerms);
381
 
450
 
382
        final Element eSpecifiedTradeSettlementHeaderMonetarySummation = new Element("SpecifiedTradeSettlementHeaderMonetarySummation", RAM_NS);
451
        final Element eSpecifiedTradeSettlementHeaderMonetarySummation = new Element("SpecifiedTradeSettlementHeaderMonetarySummation", RAM_NS);
-
 
452
        final Element eTotalHT = new Element("LineTotalAmount", RAM_NS);
-
 
453
        eTotalHT.setText(this.formatAmount.format(new BigDecimal(rowFacture.getLong("T_HT")).movePointLeft(2)));
-
 
454
        eSpecifiedTradeSettlementHeaderMonetarySummation.addContent(eTotalHT);
-
 
455
 
-
 
456
        final Element eTotalBaseTVA = new Element("TaxBasisTotalAmount", RAM_NS);
-
 
457
 
-
 
458
        BigDecimal totalBaseTVA = BigDecimal.ZERO;
-
 
459
        for (Tax tax : taxes) {
-
 
460
            totalBaseTVA = totalBaseTVA.add(tax.basisAmount);
-
 
461
        }
-
 
462
        eTotalBaseTVA.setText(this.formatAmount.format(totalBaseTVA));
-
 
463
        eSpecifiedTradeSettlementHeaderMonetarySummation.addContent(eTotalBaseTVA);
-
 
464
 
-
 
465
        final Element eTotalTVA = new Element("TaxTotalAmount", RAM_NS);
-
 
466
        eTotalTVA.setAttribute("currencyID", "EUR");
-
 
467
        eTotalTVA.setText(this.formatAmount.format(new BigDecimal(rowFacture.getLong("T_TVA")).movePointLeft(2)));
-
 
468
        eSpecifiedTradeSettlementHeaderMonetarySummation.addContent(eTotalTVA);
-
 
469
 
-
 
470
        final Element eTotalTTC = new Element("GrandTotalAmount", RAM_NS);
-
 
471
        BigDecimal totalTTC = new BigDecimal(rowFacture.getLong("T_TTC")).movePointLeft(2);
-
 
472
        eTotalTTC.setText(this.formatAmount.format(totalTTC));
-
 
473
        eSpecifiedTradeSettlementHeaderMonetarySummation.addContent(eTotalTTC);
-
 
474
 
-
 
475
        final Element eTotalPrepaid = new Element("TotalPrepaidAmount", RAM_NS);
-
 
476
        BigDecimal netApayer = new BigDecimal(rowFacture.getLong("NET_A_PAYER")).movePointLeft(2);
-
 
477
        eTotalPrepaid.setText(this.formatAmount.format(totalTTC.subtract(netApayer)));
-
 
478
        eSpecifiedTradeSettlementHeaderMonetarySummation.addContent(eTotalPrepaid);
-
 
479
 
-
 
480
        final Element eTotalDue = new Element("DuePayableAmount", RAM_NS);
-
 
481
        eTotalDue.setText(this.formatAmount.format(netApayer));
-
 
482
        eSpecifiedTradeSettlementHeaderMonetarySummation.addContent(eTotalDue);
383
 
483
 
384
        // Total HT
484
        // Total HT
385
        // <ram:LineTotalAmount>624.90</ram:LineTotalAmount>
485
        // <ram:LineTotalAmount>624.90</ram:LineTotalAmount>
386
        // Total HT sur leque est appliqué la TVA
486
        // Total HT sur leque est appliqué la TVA
387
        // <ram:TaxBasisTotalAmount>624.90</ram:TaxBasisTotalAmount>
487
        // <ram:TaxBasisTotalAmount>624.90</ram:TaxBasisTotalAmount>
Line 398... Line 498...
398
 
498
 
399
        eSupplyChainTradeTransaction.addContent(eApplicableHeaderTradeSettlement);
499
        eSupplyChainTradeTransaction.addContent(eApplicableHeaderTradeSettlement);
400
 
500
 
401
    }
501
    }
402
 
502
 
403
    private void addBuyerInfo(final Element eApplicableHeaderTradeAgreement) {
503
    private void addBuyerInfo(final Element eApplicableHeaderTradeAgreement, final SQLRowAccessor rowFacture) {
-
 
504
 
-
 
505
        SQLRowAccessor client = rowFacture.getForeign("ID_CLIENT");
-
 
506
        SQLRowAccessor clientGestion = client;
-
 
507
        if (this.useCommonClient) {
-
 
508
            client = client.getForeign("ID_CLIENT");
-
 
509
        }
-
 
510
        final SQLRowAccessor adr;
-
 
511
        if (!rowFacture.isForeignEmpty("ID_ADRESSE")) {
-
 
512
            adr = rowFacture.getForeign("ID_ADRESSE");
-
 
513
        } else if (!clientGestion.isForeignEmpty("ID_ADRESSE_F")) {
-
 
514
            adr = clientGestion.getForeign("ID_ADRESSE_F");
-
 
515
        } else {
-
 
516
            adr = client.getForeign("ID_ADRESSE");
-
 
517
        }
-
 
518
 
404
        // Acheteur
519
        // Acheteur
405
        String buyerName = "Ma jolie boutique";
520
        String buyerName = client.getString("NOM");
406
        String buyerSIRET = "78787878400035";
521
        String buyerSIRET = client.getString("SIRET");
407
        String buyerVAT = "FR19787878784";
522
        String buyerVAT = client.getString("NUMERO_TVA");
408
        String buyerContactName = "Alexandre Payet";
523
        String buyerContactName = client.getString("RESPONSABLE");
409
        String buyerPhone = "+33 4 72 07 08 67";
524
        String buyerPhone = client.getString("TEL");
410
        String buyerEmail = "alexandre.payet@majolieboutique.net";
525
        String buyerEmail = client.getString("MAIL");
411
        String buyerAddrL1 = "35 rue de la République";
526
        String buyerAddrL1 = adr.getString("RUE");
412
        String buyerAddrL2 = "";
527
        String buyerAddrL2 = "";
413
        String buyerAddrL3 = "";
528
        String buyerAddrL3 = "";
414
        String buyerPostalCode = "69001";
529
        String buyerPostalCode = adr.getString("CODE_POSTAL");
415
        String buyerCity = "Lyon";
530
        String buyerCity = adr.getString("VILLE");
416
        String buyerCountryCode = "FR";
531
        String buyerCountryCode = resolveCountryCode(adr.getString("PAYS"));
417
 
532
 
418
        final Element eBuyerTradeParty = new Element("BuyerTradeParty", RAM_NS);
533
        final Element eBuyerTradeParty = new Element("BuyerTradeParty", RAM_NS);
419
        this.addValue(eBuyerTradeParty, new Element("Name", RAM_NS), buyerName);
534
        this.addValue(eBuyerTradeParty, new Element("Name", RAM_NS), buyerName);
420
 
535
 
421
        final Element eSpecifiedLegalOrganization = new Element("SpecifiedLegalOrganization", RAM_NS);
536
        final Element eSpecifiedLegalOrganization = new Element("SpecifiedLegalOrganization", RAM_NS);
Line 437... Line 552...
437
        eEmailURIUniversalCommunication.addContent(eURIID);
552
        eEmailURIUniversalCommunication.addContent(eURIID);
438
        eDefinedTradeContact.addContent(eEmailURIUniversalCommunication);
553
        eDefinedTradeContact.addContent(eEmailURIUniversalCommunication);
439
        eBuyerTradeParty.addContent(eDefinedTradeContact);
554
        eBuyerTradeParty.addContent(eDefinedTradeContact);
440
        // Adresse postale
555
        // Adresse postale
441
        final Element ePostalTradeAddress = new Element("PostalTradeAddress", RAM_NS);
556
        final Element ePostalTradeAddress = new Element("PostalTradeAddress", RAM_NS);
-
 
557
        addValue(ePostalTradeAddress, new Element("PostcodeCode", RAM_NS), buyerPostalCode);
442
        addValue(ePostalTradeAddress, new Element("LineOne>", RAM_NS), buyerAddrL1);
558
        addValue(ePostalTradeAddress, new Element("LineOne", RAM_NS), buyerAddrL1);
443
        if (buyerAddrL2 != null && !buyerAddrL2.trim().isEmpty()) {
559
        if (buyerAddrL2 != null && !buyerAddrL2.trim().isEmpty()) {
444
            addValue(ePostalTradeAddress, new Element("LineTwo>", RAM_NS), buyerAddrL2);
560
            addValue(ePostalTradeAddress, new Element("LineTwo", RAM_NS), buyerAddrL2);
445
        }
561
        }
446
        if (buyerAddrL3 != null && !buyerAddrL3.trim().isEmpty()) {
562
        if (buyerAddrL3 != null && !buyerAddrL3.trim().isEmpty()) {
447
            addValue(ePostalTradeAddress, new Element("LineThree>", RAM_NS), buyerAddrL3);
563
            addValue(ePostalTradeAddress, new Element("LineThree", RAM_NS), buyerAddrL3);
448
        }
564
        }
449
        addValue(ePostalTradeAddress, new Element("PostcodeCode>", RAM_NS), buyerPostalCode);
-
 
450
        addValue(ePostalTradeAddress, new Element("CityName>", RAM_NS), buyerCity);
565
        addValue(ePostalTradeAddress, new Element("CityName", RAM_NS), buyerCity);
451
        addValue(ePostalTradeAddress, new Element("CountryID>", RAM_NS), buyerCountryCode);
566
        addValue(ePostalTradeAddress, new Element("CountryID", RAM_NS), buyerCountryCode);
452
 
567
 
453
        eBuyerTradeParty.addContent(ePostalTradeAddress);
568
        eBuyerTradeParty.addContent(ePostalTradeAddress);
454
 
569
 
-
 
570
        if (buyerVAT != null && buyerVAT.trim().length() > 0) {
455
        // TVA
571
            // TVA
456
        final Element eSpecifiedTaxRegistration = new Element("SpecifiedTaxRegistration", RAM_NS);
572
            final Element eSpecifiedTaxRegistration = new Element("SpecifiedTaxRegistration", RAM_NS);
457
        final Element eSpecifiedTaxRegistrationId = new Element("ID", RAM_NS);
573
            final Element eSpecifiedTaxRegistrationId = new Element("ID", RAM_NS);
458
        eSpecifiedTaxRegistrationId.setAttribute("schemeID", "VA");
574
            eSpecifiedTaxRegistrationId.setAttribute("schemeID", "VA");
459
        eSpecifiedTaxRegistrationId.setText(buyerVAT);
575
            eSpecifiedTaxRegistrationId.setText(buyerVAT);
460
        eSpecifiedTaxRegistration.addContent(eSpecifiedTaxRegistrationId);
576
            eSpecifiedTaxRegistration.addContent(eSpecifiedTaxRegistrationId);
461
        eBuyerTradeParty.addContent(eSpecifiedTaxRegistration);
577
            eBuyerTradeParty.addContent(eSpecifiedTaxRegistration);
462
 
578
        }
463
        eApplicableHeaderTradeAgreement.addContent(eBuyerTradeParty);
579
        eApplicableHeaderTradeAgreement.addContent(eBuyerTradeParty);
464
    }
580
    }
465
 
581
 
466
    private void addSupplierInfo(final Element eApplicableHeaderTradeAgreement) {
582
    private void addSupplierInfo(final Element eApplicableHeaderTradeAgreement, SQLRowAccessor rowSociete) {
467
        String supplierName = "Au bon moulin";
583
        String supplierName = rowSociete.getString("NOM");
468
        String supplierSIRET = "121321321321";
584
        String supplierSIRET = rowSociete.getString("NUM_SIRET");
469
        String supplierContactName = "Tony Dubois";
585
        String supplierContactName = "";
470
        String supplierContactPhone = "+33 4 72 07 08 56";
586
        String supplierContactPhone = rowSociete.getString("NUM_TEL");
471
        String supplierContactEmail = "tony.dubois@aubonmoulin.fr";
587
        String supplierContactEmail = rowSociete.getString("MAIL");
-
 
588
        SQLRowAccessor adr = rowSociete.getForeign("ID_ADRESSE_COMMON");
472
        String supplierAddrL1 = "3 rue du pont";
589
        String supplierAddrL1 = adr.getString("RUE");
473
        String supplierAddrL2 = "";
590
        String supplierAddrL2 = "";
474
        String supplierAddrL3 = "";
591
        String supplierAddrL3 = "";
475
        String supplierAddrPostalCode = "80100";
592
        String supplierAddrPostalCode = adr.getString("CODE_POSTAL");
476
        String supplierAddrCityName = "Abbeville";
593
        String supplierAddrCityName = adr.getString("VILLE");
477
        String supplierAddrCountryID = "FR";
594
        String supplierAddrCountryID = resolveCountryCode(adr.getString("PAYS"));
478
        String supplierNumTVA = "FR121321321321";
595
        String supplierNumTVA = rowSociete.getString("NUM_NII");
479
        // Vendeur
596
        // Vendeur
480
        final Element eSellerTradeParty = new Element("SellerTradeParty", RAM_NS);
597
        final Element eSellerTradeParty = new Element("SellerTradeParty", RAM_NS);
481
        addValue(eSellerTradeParty, new Element("Name", RAM_NS), supplierName);
598
        addValue(eSellerTradeParty, new Element("Name", RAM_NS), supplierName);
482
 
599
 
483
        final Element eSpecifiedLegalOrganization = new Element("SpecifiedLegalOrganization", RAM_NS);
600
        final Element eSpecifiedLegalOrganization = new Element("SpecifiedLegalOrganization", RAM_NS);
Line 505... Line 622...
505
        eEmailURIUniversalCommunication.addContent(eURIID);
622
        eEmailURIUniversalCommunication.addContent(eURIID);
506
        eDefinedTradeContact.addContent(eEmailURIUniversalCommunication);
623
        eDefinedTradeContact.addContent(eEmailURIUniversalCommunication);
507
        eSellerTradeParty.addContent(eDefinedTradeContact);
624
        eSellerTradeParty.addContent(eDefinedTradeContact);
508
 
625
 
509
        final Element ePostalTradeAddress = new Element("PostalTradeAddress", RAM_NS);
626
        final Element ePostalTradeAddress = new Element("PostalTradeAddress", RAM_NS);
-
 
627
        addValue(ePostalTradeAddress, new Element("PostcodeCode", RAM_NS), supplierAddrPostalCode);
510
        addValue(ePostalTradeAddress, new Element("LineOne", RAM_NS), supplierAddrL1);
628
        addValue(ePostalTradeAddress, new Element("LineOne", RAM_NS), supplierAddrL1);
511
        if (supplierAddrL2 != null && !supplierAddrL2.trim().isEmpty()) {
629
        if (supplierAddrL2 != null && !supplierAddrL2.trim().isEmpty()) {
512
            addValue(ePostalTradeAddress, new Element("LineTwo", RAM_NS), supplierAddrL2);
630
            addValue(ePostalTradeAddress, new Element("LineTwo", RAM_NS), supplierAddrL2);
513
        }
631
        }
514
        if (supplierAddrL3 != null && !supplierAddrL3.trim().isEmpty()) {
632
        if (supplierAddrL3 != null && !supplierAddrL3.trim().isEmpty()) {
515
            addValue(ePostalTradeAddress, new Element("LineThree", RAM_NS), supplierAddrL3);
633
            addValue(ePostalTradeAddress, new Element("LineThree", RAM_NS), supplierAddrL3);
516
        }
634
        }
517
        addValue(ePostalTradeAddress, new Element("PostcodeCode", RAM_NS), supplierAddrPostalCode);
-
 
518
        addValue(ePostalTradeAddress, new Element("CityName", RAM_NS), supplierAddrCityName);
635
        addValue(ePostalTradeAddress, new Element("CityName", RAM_NS), supplierAddrCityName);
519
        addValue(ePostalTradeAddress, new Element("CountryID", RAM_NS), supplierAddrCountryID);
636
        addValue(ePostalTradeAddress, new Element("CountryID", RAM_NS), supplierAddrCountryID);
520
 
637
 
521
        eSellerTradeParty.addContent(ePostalTradeAddress);
638
        eSellerTradeParty.addContent(ePostalTradeAddress);
522
 
639
 
Line 566... Line 683...
566
        if (checkdigit != 0) {
683
        if (checkdigit != 0) {
567
            checkdigit = 10 - checkdigit;
684
            checkdigit = 10 - checkdigit;
568
        }
685
        }
569
        return Integer.valueOf(code.substring(12, 13)).intValue() == checkdigit;
686
        return Integer.valueOf(code.substring(12, 13)).intValue() == checkdigit;
570
    }
687
    }
-
 
688
 
-
 
689
    private String resolveCountryCode(String pays) {
-
 
690
        if (pays == null || pays.trim().length() == 0) {
-
 
691
            return "FR";
-
 
692
        } else if (pays.toLowerCase().equals("france")) {
-
 
693
            return "FR";
-
 
694
        } else if (pays.toLowerCase().equals("fr")) {
-
 
695
            return "FR";
-
 
696
        } else if (pays.toLowerCase().equals("polska")) {
-
 
697
            return "PL";
-
 
698
        } else if (pays.toLowerCase().equals("pologne")) {
-
 
699
            return "PL";
-
 
700
        } else if (pays.toLowerCase().equals("pl")) {
-
 
701
            return "PL";
-
 
702
        } else if (pays.toLowerCase().equals("allemagne")) {
-
 
703
            return "DE";
-
 
704
        } else if (pays.toLowerCase().equals("de")) {
-
 
705
            return "DE";
-
 
706
        } else if (pays.toLowerCase().equals("deutschland")) {
-
 
707
            return "DE";
-
 
708
        } else if (pays.toLowerCase().equals("italie")) {
-
 
709
            return "IT";
-
 
710
        } else if (pays.toLowerCase().equals("italy")) {
-
 
711
            return "IT";
-
 
712
        } else if (pays.toLowerCase().equals("italia")) {
-
 
713
            return "IT";
-
 
714
        } else if (pays.toLowerCase().equals("it")) {
-
 
715
            return "IT";
-
 
716
        } else if (pays.toLowerCase().equals("espagne")) {
-
 
717
            return "SP";
-
 
718
        } else if (pays.toLowerCase().equals("spain")) {
-
 
719
            return "SP";
-
 
720
        } else if (pays.toLowerCase().equals("sp")) {
-
 
721
            return "SP";
-
 
722
        } else if (pays.toLowerCase().equals("luxembourg")) {
-
 
723
            return "LU";
-
 
724
        } else if (pays.toLowerCase().equals("lu")) {
-
 
725
            return "LU";
-
 
726
        } else if (pays.toLowerCase().equals("portugal")) {
-
 
727
            return "PT";
-
 
728
        } else if (pays.toLowerCase().equals("pt")) {
-
 
729
            return "PT";
-
 
730
        } else if (pays.toLowerCase().equals("belgique")) {
-
 
731
            return "BE";
-
 
732
        } else if (pays.toLowerCase().equals("belgium")) {
-
 
733
            return "BE";
-
 
734
        } else if (pays.toLowerCase().equals("be")) {
-
 
735
            return "BE";
-
 
736
        } else {
-
 
737
            return "";
-
 
738
        }
-
 
739
    }
571
}
740
}