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.gestcomm;

import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.utils.cc.ITransformer;
import org.openconcerto.utils.DecimalUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class OptionDocProcessor implements ITransformer<List<SQLRowAccessor>, List<SQLRowAccessor>> {

    @Override
    public List<SQLRowAccessor> transformChecked(List<SQLRowAccessor> input) {

        List<SQLRowValues> resultRowValues = new ArrayList(input.size());
        SQLRowValues lastRowNonOptionnal = null;
        Set<SQLRowValues> recalculPrixUnitaire = new HashSet<SQLRowValues>();
        for (SQLRowAccessor sqlRowAccessor : input) {
            final SQLRowAccessor nonEmptyForeign = sqlRowAccessor.getNonEmptyForeign("ID_ARTICLE");
            final SQLRowValues asRowValues = sqlRowAccessor.asRowValues();
            if (lastRowNonOptionnal != null && nonEmptyForeign != null && nonEmptyForeign.getBoolean("OPTION")) {
                lastRowNonOptionnal.put("T_PV_HT", lastRowNonOptionnal.getBigDecimal("T_PV_HT").add(asRowValues.getBigDecimal("T_PV_HT")));
                lastRowNonOptionnal.put("T_PV_TTC", lastRowNonOptionnal.getBigDecimal("T_PV_TTC").add(asRowValues.getBigDecimal("T_PV_TTC")));
                recalculPrixUnitaire.add(lastRowNonOptionnal);
                asRowValues.put("PRIX_METRIQUE_VT_1", null);
                asRowValues.put("PV_HT", null);
                asRowValues.put("T_PV_HT", null);
                asRowValues.put("T_PV_TTC", null);
                asRowValues.putEmptyLink("ID_TAXE");
            } else {
                lastRowNonOptionnal = asRowValues;
            }
            resultRowValues.add(asRowValues);
        }
        List<SQLRowAccessor> result = new ArrayList(input.size());

        for (SQLRowValues sqlRowValues : resultRowValues) {
            if (recalculPrixUnitaire.contains(sqlRowValues)) {

                String fieldQte = sqlRowValues.getTable().getName().startsWith("BON_DE_LIVRAISON") ? "QTE_LIVREE" : "QTE";

                BigDecimal qte = sqlRowValues.getBigDecimal("QTE_UNITAIRE").multiply(new BigDecimal(sqlRowValues.getInt(fieldQte), DecimalUtils.HIGH_PRECISION));
                if (qte.signum() != 0) {

                    BigDecimal totalHT = sqlRowValues.getBigDecimal("T_PV_HT");
                    final BigDecimal remisePercent = sqlRowValues.getBigDecimal("POURCENT_REMISE");
                    if (remisePercent != null && remisePercent.signum() > 0 && remisePercent.movePointLeft(2).compareTo(BigDecimal.ONE) != 0) {
                        if (BigDecimal.ONE.subtract(remisePercent.movePointLeft(2)).signum() != 0) {
                            totalHT = totalHT.divide(BigDecimal.ONE.subtract(remisePercent.movePointLeft(2)), org.openconcerto.utils.DecimalUtils.HIGH_PRECISION);
                        }
                    }
                    // On ne recalcule pas le prix unitaire si la remise est de 100% sinon le prix
                    // unitaire est à 0 et les provider de quantité n'affiche plus rien
                    if (remisePercent == null || remisePercent.movePointLeft(2).compareTo(BigDecimal.ONE) != 0) {
                        BigDecimal unitPrice = totalHT.divide(qte, DecimalUtils.HIGH_PRECISION);
                        sqlRowValues.put("PV_HT", unitPrice);
                        sqlRowValues.put("PRIX_METRIQUE_VT_1", unitPrice);
                    }
                }
            }
            result.add(sqlRowValues);
        }

        return result;
    }

}