OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 182 | Blame | Compare with Previous | 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.core.common.ui.NumericFormat;
import org.openconcerto.erp.core.sales.product.element.UniteVenteArticleSQLElement;
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 java.math.BigDecimal;

public class FormatedGlobalQtyTotalProvider implements SpreadSheetCellValueProvider {

    private final boolean shortName, alwaysShowOnZeroQty, pieceName;

    public static enum Type {
        NORMAL, SHIPMENT, REMAIND
    }

    public final Type type;

    private FormatedGlobalQtyTotalProvider(Type t, boolean shortName) {
        this(t, shortName, false, false);
    }

    private FormatedGlobalQtyTotalProvider(Type t, boolean shortName, boolean alwaysShowOnZeroQty, boolean withPieceName) {
        this.shortName = shortName;
        this.type = t;
        this.alwaysShowOnZeroQty = alwaysShowOnZeroQty;
        this.pieceName = withPieceName;
    }

    private final BigDecimal cent = new BigDecimal(100);

    public Object getValue(SpreadSheetCellValueContext context) {
        final SQLRowAccessor row = context.getRow();
        final BigDecimal pv = row.getBigDecimal("PV_HT");
        final BigDecimal pR = row.getBigDecimal("POURCENT_REMISE");
        if (!this.alwaysShowOnZeroQty && pv != null && pv.compareTo(BigDecimal.ZERO) == 0 && (pR == null || pR.compareTo(cent) != 0)) {
            return null;
        }

        final String field;
        if (this.type == Type.NORMAL) {
            field = "QTE";
        } else if (row.getTable().contains("QTE_LIVREE")) {
            field = "QTE_LIVREE";
        } else if (row.getTable().contains("QTE_ORIGINE")) {
            field = "QTE_ORIGINE";
        } else {
            return null;
        }
        if (row.getObject(field) == null) {
            return null;
        }

        int qte = row.getInt(field);
        if (this.type == Type.REMAIND) {
            if (row.getObject("QTE") != null) {
                if (field.equalsIgnoreCase("QTE_ORIGINE")) {
                    qte = qte - row.getInt("QTE");
                } else {
                    qte = row.getInt("QTE") - qte;
                }
            }
        }

        if (!this.pieceName && row.getInt("ID_UNITE_VENTE") == UniteVenteArticleSQLElement.A_LA_PIECE) {
            return String.valueOf(qte);
        }
        String result = "";
        if (this.alwaysShowOnZeroQty || qte != 0) {
            result += qte + " x ";
            final BigDecimal qteUV = row.getBigDecimal("QTE_UNITAIRE");

            result += NumericFormat.getQtyDecimalFormat().format(qteUV);
            final SQLRowAccessor rMode = row.getForeign("ID_UNITE_VENTE");
            result += " " + ((this.shortName) ? rMode.getString("CODE") : rMode.getString("NOM"));
            // 3 x 5.5 meters
            // 1 x 6.3 meters -> 6.3 meters
        }
        return result;
    }

    public static void register() {
        SpreadSheetCellValueProviderManager.put("supplychain.element.qtyunit.short.with.quantity", new FormatedGlobalQtyTotalProvider(Type.NORMAL, true, true, false));
        SpreadSheetCellValueProviderManager.put("supplychain.element.qtyunit.short", new FormatedGlobalQtyTotalProvider(Type.NORMAL, true));
        SpreadSheetCellValueProviderManager.put("supplychain.element.qtyunit", new FormatedGlobalQtyTotalProvider(Type.NORMAL, false));
        SpreadSheetCellValueProviderManager.put("supplychain.element.qtyunit.deliver.short", new FormatedGlobalQtyTotalProvider(Type.SHIPMENT, true));
        SpreadSheetCellValueProviderManager.put("supplychain.element.qtyunit.deliver", new FormatedGlobalQtyTotalProvider(Type.SHIPMENT, false));
        SpreadSheetCellValueProviderManager.put("supplychain.element.qtyunit.deliver.short.with.quantity", new FormatedGlobalQtyTotalProvider(Type.SHIPMENT, true, true, true));
        SpreadSheetCellValueProviderManager.put("supplychain.element.qtyunit.remainder.short", new FormatedGlobalQtyTotalProvider(Type.REMAIND, true));
        SpreadSheetCellValueProviderManager.put("supplychain.element.qtyunit.remainder", new FormatedGlobalQtyTotalProvider(Type.REMAIND, false));
        SpreadSheetCellValueProviderManager.put("supplychain.element.qtyunit.remainder.short.with.quantity", new FormatedGlobalQtyTotalProvider(Type.REMAIND, true, true, true));
        SpreadSheetCellValueProviderManager.put("supplychain.element.qtyunit.alwaysnamed.short.with.quantity", new FormatedGlobalQtyTotalProvider(Type.NORMAL, true, true, true));
        SpreadSheetCellValueProviderManager.put("supplychain.element.qtyunit.alwaysnamed.short", new FormatedGlobalQtyTotalProvider(Type.NORMAL, true, false, true));
        SpreadSheetCellValueProviderManager.put("supplychain.element.qtyunit.alwaysnamed", new FormatedGlobalQtyTotalProvider(Type.NORMAL, false, false, true));
        SpreadSheetCellValueProviderManager.put("supplychain.element.qtyunit.alwaysnamed.deliver.short", new FormatedGlobalQtyTotalProvider(Type.SHIPMENT, true, false, true));
        SpreadSheetCellValueProviderManager.put("supplychain.element.qtyunit.alwaysnamed.deliver", new FormatedGlobalQtyTotalProvider(Type.SHIPMENT, false, false, true));
        SpreadSheetCellValueProviderManager.put("supplychain.element.qtyunit.alwaysnamed.remainder.short", new FormatedGlobalQtyTotalProvider(Type.REMAIND, true, false, true));
        SpreadSheetCellValueProviderManager.put("supplychain.element.qtyunit.alwaysnamed.remainder", new FormatedGlobalQtyTotalProvider(Type.REMAIND, false, false, true));
    }
}