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.generationDoc.provider;

import org.openconcerto.erp.generationDoc.SpreadSheetCellValueContext;
import org.openconcerto.erp.generationDoc.SpreadSheetCellValueProvider;
import org.openconcerto.erp.generationDoc.SpreadSheetCellValueProviderManager;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
import org.openconcerto.sql.model.Where;
import org.openconcerto.utils.DecimalUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.List;

public class RecapLigneFactureProvider implements SpreadSheetCellValueProvider {

    private enum TypeLineRecapFactureProvider {
        HT, TTC, PERCENT;
    };

    private final TypeLineRecapFactureProvider type;
    private final boolean old, withAvoir;

    public RecapLigneFactureProvider(TypeLineRecapFactureProvider t, boolean old) {
        this(t, old, false);
    }

    public RecapLigneFactureProvider(TypeLineRecapFactureProvider t, boolean old, boolean withAvoir) {
        this.type = t;
        this.old = old;
        this.withAvoir = withAvoir;
    }

    public Object getValue(SpreadSheetCellValueContext context) {
        SQLRowAccessor row = context.getRow();
        final SQLRowAccessor factureOrigin = row.getNonEmptyForeign("ID_SAISIE_VENTE_FACTURE");
        final Calendar date = factureOrigin.getDate("DATE");

        final SQLRowAccessor foreignCmdItem = row.getNonEmptyForeign("ID_COMMANDE_CLIENT_ELEMENT");
        if (foreignCmdItem != null && foreignCmdItem.getBigDecimal("T_PV_HT").signum() != 0) {

            SQLRowValues rowValsFactItem2Fetch = new SQLRowValues(row.getTable());
            rowValsFactItem2Fetch.putNulls("T_PV_HT", "T_PV_TTC");

            rowValsFactItem2Fetch.putRowValues("ID_SAISIE_VENTE_FACTURE").putNulls("DATE", "ID_AVOIR_CLIENT");

            final List<SQLRowValues> fetch = SQLRowValuesListFetcher.create(rowValsFactItem2Fetch).fetch(new Where(row.getTable().getField("ID_COMMANDE_CLIENT_ELEMENT"), "=", foreignCmdItem.getID()));

            BigDecimal total = BigDecimal.ZERO;
            BigDecimal totalTTC = BigDecimal.ZERO;
            for (SQLRowAccessor sqlRowAccessor : fetch) {
                final SQLRowAccessor nonEmptyForeign = sqlRowAccessor.getNonEmptyForeign("ID_SAISIE_VENTE_FACTURE");
                if (nonEmptyForeign != null && (!withAvoir || (withAvoir && sqlRowAccessor.isForeignEmpty("ID_AVOIR_CLIENT")))) {
                    final Calendar date2 = nonEmptyForeign.getDate("DATE");
                    final boolean same = old && factureOrigin.getID() == nonEmptyForeign.getID();
                    if (same || date2.before(date) || (date2.equals(date) && nonEmptyForeign.getID() < factureOrigin.getID())) {
                        total = total.add(sqlRowAccessor.getBigDecimal("T_PV_HT"));
                        totalTTC = totalTTC.add(sqlRowAccessor.getBigDecimal("T_PV_TTC"));
                    }
                }
            }
            if (this.type == TypeLineRecapFactureProvider.HT) {
                return total;
            } else if (this.type == TypeLineRecapFactureProvider.TTC) {
                return totalTTC;
            } else {
                if (foreignCmdItem.getBigDecimal("T_PV_HT").signum() != 0) {
                    return total.divide(foreignCmdItem.getBigDecimal("T_PV_HT"), DecimalUtils.HIGH_PRECISION).movePointRight(2).setScale(2, RoundingMode.HALF_UP) + "%";
                } else {
                    return "";
                }
            }
        } else {
            return "";
        }
    }

    public static void register() {
        SpreadSheetCellValueProviderManager.put("sales.account.line.history", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.HT, false));
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.ttc", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.TTC, false));
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.percent", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.PERCENT, false));
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.total", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.HT, true));
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.total.ttc", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.TTC, true));
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.total.percent", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.PERCENT, true));
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.with.credit", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.HT, false, true));
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.with.credit.ttc", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.TTC, false, true));
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.with.credit.percent", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.PERCENT, false, true));
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.with.credit.total", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.HT, true, true));
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.with.credit.total.ttc", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.TTC, true, true));
        SpreadSheetCellValueProviderManager.put("sales.account.line.history.with.credit.total.percent", new RecapLigneFactureProvider(TypeLineRecapFactureProvider.PERCENT, true, true));
    }

}