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.utils.DecimalUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class RGProvider implements SpreadSheetCellValueProvider {

    private enum TypeRGProvider {
        HT_RESTANT(true, true), TTC_RESTANT(false, true), HT_RG(true, false), TTC_RG(false, false);

        private final boolean restant, ht;

        TypeRGProvider(boolean ht, boolean restant) {
            this.restant = restant;
            this.ht = ht;
        }

        public boolean isHt() {
            return this.ht;
        }

        public boolean isRestant() {
            return this.restant;
        }
    };

    private final TypeRGProvider type;

    public RGProvider(TypeRGProvider t) {
        this.type = t;
    }

    public Object getValue(SpreadSheetCellValueContext context) {
        final BigDecimal total;
        if (this.type.isHt()) {
            total = new BigDecimal(context.getRow().getLong("T_HT")).movePointLeft(2);
        } else {
            total = new BigDecimal(context.getRow().getLong("T_TTC")).movePointLeft(2);
        }

        final BigDecimal pourcentRG = context.getRow().getBigDecimal("POURCENT_RG");
        final BigDecimal totalRG = total.multiply(pourcentRG.movePointLeft(2), DecimalUtils.HIGH_PRECISION).setScale(2, RoundingMode.HALF_UP);
        if (this.type.isRestant()) {
            return total.subtract(totalRG);
        } else {
            return totalRG;
        }
    }

    public static void register() {
        SpreadSheetCellValueProviderManager.put("sales.rg.due", new RGProvider(TypeRGProvider.TTC_RG));
        SpreadSheetCellValueProviderManager.put("sales.total.sub.rg", new RGProvider(TypeRGProvider.TTC_RESTANT));

        SpreadSheetCellValueProviderManager.put("sales.rg.due.ht", new RGProvider(TypeRGProvider.HT_RG));
        SpreadSheetCellValueProviderManager.put("sales.total.sub.rg.ht", new RGProvider(TypeRGProvider.HT_RESTANT));
    }

}