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 java.math.BigDecimal;

public class ResteALivrerDocProvider implements SpreadSheetCellValueProvider {

    protected enum ResteProvider {
        RESTE, LIVRE_TOTAL, RESTE_CMD, LIVRE_TOTAL_CMD
    };

    protected final ResteProvider type;

    public ResteALivrerDocProvider(ResteProvider t) {
        this.type = t;
    }

    @Override
    public Object getValue(SpreadSheetCellValueContext context) {
        SQLRowAccessor sqlRowAccessor = context.getRow();
        return geTotalFromRow(sqlRowAccessor);

    }

    protected BigDecimal geTotalFromRow(SQLRowAccessor sqlRowAccessor) {
        BigDecimal totalD;
        BigDecimal total = sqlRowAccessor.getBigDecimal("QTE_UNITAIRE");
        total = total.multiply(new BigDecimal(sqlRowAccessor.getInt("QTE")));

        if (this.type == ResteProvider.RESTE_CMD || this.type == ResteProvider.LIVRE_TOTAL_CMD) {
            totalD = new BigDecimal(sqlRowAccessor.getInt("QTE_LIVREE"));
            final BigDecimal returnedValue;
            if (this.type == ResteProvider.RESTE_CMD) {
                returnedValue = total.subtract(totalD);
            } else {
                returnedValue = totalD;
            }
            if (!sqlRowAccessor.isForeignEmpty("ID_ARTICLE") && returnedValue.signum() > 0) {
                return returnedValue;
            } else {
                return null;
            }
        } else {

            final SQLRowAccessor nonEmptyForeignCmdItem = sqlRowAccessor.getNonEmptyForeign("ID_COMMANDE_CLIENT_ELEMENT");
            if (nonEmptyForeignCmdItem == null) {

                totalD = sqlRowAccessor.getBigDecimal("QTE_UNITAIRE");
                totalD = totalD.multiply(new BigDecimal(sqlRowAccessor.getInt("QTE_LIVREE")));

                return total.subtract(totalD);
            } else {
                totalD = nonEmptyForeignCmdItem.getBigDecimal("QTE_LIVREE");
            }
            if (this.type == ResteProvider.LIVRE_TOTAL) {
                if (totalD.signum() > 0) {
                    return totalD;
                } else {
                    return null;
                }
            } else {
                BigDecimal r = total.subtract(totalD);
                if (r.signum() > 0) {
                    return r;
                } else {
                    return null;
                }
            }
        }
    }

    public static void register() {
        SpreadSheetCellValueProviderManager.put("sales.qty.delivred.total", new ResteALivrerDocProvider(ResteProvider.LIVRE_TOTAL));
        SpreadSheetCellValueProviderManager.put("sales.qty.delivred.remained", new ResteALivrerDocProvider(ResteProvider.RESTE));
        SpreadSheetCellValueProviderManager.put("sales.qty.cmd.delivred.total", new ResteALivrerDocProvider(ResteProvider.LIVRE_TOTAL_CMD));
        SpreadSheetCellValueProviderManager.put("sales.qty.cmd.delivred.remained", new ResteALivrerDocProvider(ResteProvider.RESTE_CMD));
    }
}