OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011-2019 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.pos;

import org.openconcerto.erp.config.ComptaPropsConfiguration;
import org.openconcerto.erp.core.common.ui.TotalCalculator;
import org.openconcerto.erp.core.finance.tax.model.TaxeCache;
import org.openconcerto.erp.core.sales.pos.model.Article;
import org.openconcerto.erp.core.sales.pos.model.Categorie;
import org.openconcerto.erp.core.sales.pos.model.Ticket;
import org.openconcerto.erp.core.sales.pos.model.TicketItem;
import org.openconcerto.erp.generationDoc.AbstractListeSheetXml;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.utils.DecimalUtils;
import org.openconcerto.utils.Tuple2;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TicketSheetXML extends AbstractListeSheetXml {

    public static final String TEMPLATE_ID = "VenteFactureTicket";
    public static final String TEMPLATE_PROPERTY_NAME = DEFAULT_PROPERTY_NAME;

    private final Ticket t;
    private final ComptaPropsConfiguration conf;

    public TicketSheetXML(Ticket t, ComptaPropsConfiguration conf) {
        super(null);
        this.t = t;
        this.conf = conf;
    }

    @Override
    protected void createListeValues() {

        this.mapAllSheetValues = new HashMap<Integer, Map<String, Object>>();
        Map<String, Object> mapValues = new HashMap<String, Object>();

        final SQLRow rowSociete = this.conf.getRowSociete();
        mapValues.put("SOCIETE_NOM", rowSociete.getString("NOM"));
        final SQLRow foreignAdr = rowSociete.getForeign("ID_ADRESSE_COMMON");
        mapValues.put("SOCIETE_RUE", foreignAdr.getString("RUE"));
        mapValues.put("SOCIETE_CODE_POSTAL", foreignAdr.getString("CODE_POSTAL"));
        mapValues.put("SOCIETE_VILLE", foreignAdr.getString("VILLE"));
        mapValues.put("SOCIETE_CODE_POSTAL_VILLE", foreignAdr.getString("CODE_POSTAL") + " " + foreignAdr.getString("VILLE"));
        mapValues.put("SOCIETE_SIRET", rowSociete.getString("NUM_SIRET"));
        mapValues.put("SOCIETE_TEL", rowSociete.getString("NUM_TEL"));
        mapValues.put("SOCIETE_TVA", rowSociete.getString("NUM_NII"));
        mapValues.put("SOCIETE_CAPITAL", rowSociete.getFloat("CAPITAL"));
        mapValues.put("SOCIETE_MAIL", rowSociete.getString("MAIL"));
        mapValues.put("SOCIETE_TYPE", rowSociete.getString("TYPE"));
        mapValues.put("SOCIETE_TYPE_CAPITAL", rowSociete.getString("TYPE") + " au capital de " + rowSociete.getFloat("CAPITAL"));
        mapValues.put("NUMERO", this.t.getReceiptCode().getCode());
        mapValues.put("CLIENT", this.t.getClient().getFullName());
        mapValues.put("ADRESSE", this.t.getClient().getAddr());
        mapValues.put("DATE", this.t.getCreationDate());

        this.mapAllSheetValues.put(0, mapValues);

        List<Map<String, Object>> values = new ArrayList<Map<String, Object>>();

        this.listAllSheetValues.put(0, values);
        this.styleAllSheetValues = new HashMap<Integer, Map<Integer, String>>();
        Map<Integer, String> styles = new HashMap<Integer, String>();
        this.styleAllSheetValues.put(0, styles);

        List<TicketItem> itemsToPrint = new ArrayList<>(this.t.getItems());
        Collections.sort(itemsToPrint, new Comparator<TicketItem>() {

            @Override
            public int compare(TicketItem o1, TicketItem o2) {
                final Article p1 = o1.getArticle();
                final Article p2 = o2.getArticle();
                final Categorie c1 = p1.getCategorie();
                final Categorie c2 = p2.getCategorie();
                if (c1.equals(c2)) {
                    return p1.getName().compareTo(p2.getName());
                }
                // Unknown first
                if (c1.isUnknown()) {
                    return -1;
                }
                if (c2.isUnknown()) {
                    return 1;
                }
                // Sort by name
                return c1.getName().compareTo(c2.getName());
            }
        });
        for (TicketItem item : itemsToPrint) {
            Map<String, Object> mapV = new HashMap<>();
            styles.put(values.size(), "Normal");
            values.add(mapV);
            final Article article = item.getArticle();
            final BigDecimal nb = item.getQty();
            final Float tauxFromId = TaxeCache.getCache().getTauxFromId(article.getIdTaxe());
            final BigDecimal tauxTVA = BigDecimal.valueOf(tauxFromId).movePointLeft(2).add(BigDecimal.ONE);

            final BigDecimal ht = article.getPriceWithoutTax(nb, false);
            final BigDecimal htRemise = article.getPriceWithoutTax(nb, true);

            final BigDecimal ttc = article.getPriceWithTax(nb, false);
            final BigDecimal ttcRemise = article.getPriceWithTax(nb, true);

            final BigDecimal totalTTC = ttc.multiply(nb, DecimalUtils.HIGH_PRECISION);
            final BigDecimal totalHT;
            if (tauxTVA.signum() != 0) {
                totalHT = totalTTC.divide(tauxTVA, DecimalUtils.HIGH_PRECISION);
            } else {
                totalHT = totalTTC;
            }

            final BigDecimal totalTTCRemise = ttcRemise.multiply(nb, DecimalUtils.HIGH_PRECISION);
            final BigDecimal totalHTRemise;
            if (tauxTVA.signum() != 0) {
                totalHTRemise = totalTTCRemise.divide(tauxTVA, DecimalUtils.HIGH_PRECISION);
            } else {
                totalHTRemise = totalTTCRemise;
            }

            mapV.put("NOM", item.getArticle().getName());
            mapV.put("CODE", item.getArticle().getCode());
            mapV.put("POURCENT_REMISE", item.getArticle().getDiscountPct());
            mapV.put("QTE", nb);
            mapV.put("PV_HT", ht);
            mapV.put("PV_HT_REMISE", htRemise);
            mapV.put("PV_TTC", ttc);
            mapV.put("PV_TTC_REMISE", ttcRemise);
            mapV.put("TVA", tauxFromId);
            mapV.put("T_HT", totalHT);
            mapV.put("T_TTC", totalTTC);
            mapV.put("T_HT_REMISE", totalHTRemise);
            mapV.put("T_TTC_REMISE", totalTTCRemise);
        }

        final TotalCalculator calc = this.t.getTotalCalculator();

        final Map<SQLRowAccessor, Tuple2<BigDecimal, BigDecimal>> mapHtTVARowTaux = calc.getMapHtTVARowTaux();

        int tvaIndex = 1;
        for (final SQLRowAccessor row : mapHtTVARowTaux.keySet()) {
            final Tuple2<BigDecimal, BigDecimal> htTVA = mapHtTVARowTaux.get(row);
            final float tvaTaux = TaxeCache.getCache().getTauxFromId(row.getID());
            final BigDecimal montantTVA = htTVA.get1();
            if (montantTVA != null && montantTVA.signum() != 0) {
                mapValues.put("TVA_" + tvaIndex, "TVA " + tvaTaux);
                mapValues.put("T_HT_TVA_" + tvaIndex, htTVA.get0());
                mapValues.put("T_TVA_" + tvaIndex, montantTVA);
                tvaIndex++;
            }
        }
        mapValues.put("T_HT", calc.getTotalHT());
        mapValues.put("T_TTC", calc.getTotalTTC());
    }

    @Override
    public String getDefaultTemplateId() {

        return TEMPLATE_ID;
    }

    private Date d;

    @Override
    public String getName() {
        if (this.d == null) {
            this.d = new Date();
        }
        return "FactureTicket" + this.d.getTime();
    }

}