Dépôt officiel du code source de l'ERP OpenConcerto
Rev 149 | 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.finance.tax.model.TaxeCache;
import org.openconcerto.erp.generationDoc.SpreadSheetCellValueContext;
import org.openconcerto.erp.generationDoc.SpreadSheetCellValueProviderManager;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.utils.DecimalUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class PrixUnitaireRemiseProvider extends UserInitialsValueProvider {
public static int UNITAIRE_REMISE = 0;
public static int TOTAL_NON_ACOMPTE = 1;
public static int UNITAIRE_REMISE_TTC = 2;
public static int UNITAIRE_TTC = 3;
public final int type;
public PrixUnitaireRemiseProvider(int t) {
this.type = t;
}
@Override
public Object getValue(SpreadSheetCellValueContext context) {
SQLRowAccessor row = context.getRow();
if (this.type == UNITAIRE_REMISE || this.type == UNITAIRE_REMISE_TTC) {
return getPrixUnitaire(row, true);
} else if (this.type == UNITAIRE_TTC) {
return getPrixUnitaire(row, false);
} else {
return getPrixTotalOrigin(row);
}
}
public Object getPrixTotalOrigin(SQLRowAccessor row) {
final BigDecimal pv = row.getBigDecimal("PV_HT");
if (pv == null) {
return null;
}
BigDecimal remise = (BigDecimal) row.getObject("POURCENT_REMISE");
if (pv == null || remise == null) {
remise = BigDecimal.ZERO;
}
BigDecimal acompte = BigDecimal.ONE;
BigDecimal result = BigDecimal.ONE.subtract(remise.movePointLeft(2)).multiply(pv, DecimalUtils.HIGH_PRECISION).multiply(acompte, DecimalUtils.HIGH_PRECISION);
return result.multiply(row.getBigDecimal("QTE_UNITAIRE")).multiply(new BigDecimal(row.getInt("QTE"))).setScale(2, RoundingMode.HALF_UP);
}
public Object getPrixUnitaire(SQLRowAccessor row, boolean withRemise) {
final BigDecimal pv = row.getBigDecimal("PV_HT");
if (pv == null) {
return null;
}
BigDecimal remise = (BigDecimal) row.getObject("POURCENT_REMISE");
if (remise == null || !withRemise) {
remise = BigDecimal.ZERO;
}
BigDecimal acompte = BigDecimal.ONE;
if (row.getTable().contains("POURCENT_ACOMPTE") && row.getObject("POURCENT_ACOMPTE") != null) {
acompte = ((BigDecimal) row.getObject("POURCENT_ACOMPTE")).movePointLeft(2);
}
BigDecimal result = BigDecimal.ONE.subtract(remise.movePointLeft(2)).multiply(pv, DecimalUtils.HIGH_PRECISION).multiply(acompte, DecimalUtils.HIGH_PRECISION);
if (this.type == UNITAIRE_REMISE_TTC) {
float t = TaxeCache.getCache().getTauxFromId(row.getForeignID("ID_TAXE"));
result = result.multiply(new BigDecimal(t).movePointLeft(2).add(BigDecimal.ONE));
}
final BigDecimal setScale = result.setScale(2, RoundingMode.HALF_UP);
return setScale;
}
public static void register() {
SpreadSheetCellValueProviderManager.put("PrixUnitaireTTC", new PrixUnitaireRemiseProvider(UNITAIRE_TTC));
SpreadSheetCellValueProviderManager.put("PrixUnitaireRemise", new PrixUnitaireRemiseProvider(UNITAIRE_REMISE));
SpreadSheetCellValueProviderManager.put("PrixUnitaireRemiseTTC", new PrixUnitaireRemiseProvider(UNITAIRE_REMISE_TTC));
SpreadSheetCellValueProviderManager.put("PrixTotalSansAcompte", new PrixUnitaireRemiseProvider(TOTAL_NON_ACOMPTE));
}
}