Dépôt officiel du code source de l'ERP OpenConcerto
/trunk/OpenConcerto/src/org/openconcerto/erp/core/common/ui/TotalCalculator.java |
---|
22,6 → 22,7 |
import org.openconcerto.sql.model.SQLRowValuesListFetcher; |
import org.openconcerto.sql.model.SQLTable; |
import org.openconcerto.utils.DecimalUtils; |
import org.openconcerto.utils.Tuple2; |
import java.math.BigDecimal; |
import java.math.RoundingMode; |
45,8 → 46,9 |
private BigDecimal totalDevise, totalDeviseSel; |
private BigDecimal totalHA, totalHASel; |
private BigDecimal totalEco, totalEcoSel; |
private BigDecimal totalService, totalServiceSel; |
private BigDecimal totalTTC, totalTTCSel; |
private BigDecimal totalHTSansFActurable, totalTTC, totalTTCSel; |
private long remiseHT, remiseRestante; |
private final boolean achat; |
56,11 → 58,17 |
// Total des TVA par comptes |
private Map<SQLRowAccessor, BigDecimal> mapHtTVA = new HashMap<SQLRowAccessor, BigDecimal>(); |
private Map<SQLRowAccessor, BigDecimal> mapHtTaxeCompl = new HashMap<SQLRowAccessor, BigDecimal>(); |
private Map<SQLRowAccessor, BigDecimal> mapHtTVAIntra = new HashMap<SQLRowAccessor, BigDecimal>(); |
private Map<SQLRowAccessor, BigDecimal> mapHtTVASel = new HashMap<SQLRowAccessor, BigDecimal>(); |
// Total HT par TVA |
private Map<SQLRowAccessor, Tuple2<BigDecimal, BigDecimal>> mapHtTVARowTaux = new HashMap<SQLRowAccessor, Tuple2<BigDecimal, BigDecimal>>(); |
private int[] selectedRows; |
private Boolean bServiceActive; |
private BigDecimal totalHTAvantRemise; |
private boolean intraComm = false; |
public TotalCalculator(String fieldHA, String fieldHT, String fieldDeviseTotal) { |
this(fieldHA, fieldHT, fieldDeviseTotal, false, null); |
70,6 → 78,10 |
this.rowDefaultCptService = rowDefaultCptService; |
} |
public void setIntraComm(boolean intraComm) { |
this.intraComm = intraComm; |
} |
public TotalCalculator(String fieldHA, String fieldHT, String fieldDeviseTotal, boolean achat, SQLRowAccessor defaultCompte) { |
this.achat = achat; |
80,7 → 92,7 |
this.fieldHT = fieldHT; |
final SQLTable tablePrefCompte = Configuration.getInstance().getRoot().findTable("PREFS_COMPTE"); |
final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2); |
// FIXME faire un fetcher pour ne pas faire 5 requetes (1 par getForeign) |
// Comptes par défaut |
this.rowDefaultCptService = rowPrefsCompte.getForeign("ID_COMPTE_PCE_VENTE_SERVICE"); |
if (this.rowDefaultCptService == null || this.rowDefaultCptService.isUndefined()) { |
177,9 → 189,13 |
this.selectedRows = null; |
this.totalHTSansFActurable = BigDecimal.ZERO; |
this.totalTTC = BigDecimal.ZERO; |
this.totalTTCSel = BigDecimal.ZERO; |
this.totalEco = BigDecimal.ZERO; |
this.totalEcoSel = BigDecimal.ZERO; |
this.totalHA = BigDecimal.ZERO; |
this.totalHASel = BigDecimal.ZERO; |
197,6 → 213,9 |
// Total des TVA par comptes |
this.mapHtTVA.clear(); |
this.mapHtTaxeCompl.clear(); |
this.mapHtTVAIntra.clear(); |
this.mapHtTVARowTaux.clear(); |
this.mapHtTVASel.clear(); |
} |
206,7 → 225,7 |
} |
public void addEchantillon(BigDecimal ht, SQLRowAccessor tva) { |
addHT(ht, tva, this.rowDefaultCptProduit, false); |
addHT(ht, ht, tva, this.rowDefaultCptProduit, false); |
} |
private Map<Integer, SQLRowAccessor> mapTVA; |
240,6 → 259,16 |
rowVals.put("ID_COMPTE_PCE_VENTE", rowValsC3); |
rowVals.put("ID_COMPTE_PCE_VENTE_SERVICE", rowValsC4); |
if (tvaTable.contains("ID_COMPTE_PCE_COLLECTE_INTRA")) { |
SQLRowValues rowValsC1Intra = new SQLRowValues(compteTable); |
rowValsC1Intra.put("NUMERO", null); |
rowValsC1Intra.put("ID", null); |
rowVals.put("ID_COMPTE_PCE_COLLECTE_INTRA", rowValsC1Intra); |
SQLRowValues rowValsC2Intra = new SQLRowValues(compteTable); |
rowValsC2Intra.put("NUMERO", null); |
rowValsC2Intra.put("ID", null); |
rowVals.put("ID_COMPTE_PCE_DED_INTRA", rowValsC2Intra); |
} |
SQLRowValuesListFetcher fetch = SQLRowValuesListFetcher.create(rowVals); |
List<SQLRowValues> rowValsList = fetch.fetch(); |
248,7 → 277,7 |
} |
} |
private void addHT(BigDecimal ht, SQLRowAccessor tva, SQLRowAccessor cptArticle, boolean selection) { |
private void addHT(BigDecimal ht, BigDecimal htSansFacturable, SQLRowAccessor tva, SQLRowAccessor cptArticle, boolean selection) { |
BigDecimal ttc; |
BigDecimal totalTVA; |
264,7 → 293,24 |
if (tva != null && !tva.isUndefined()) { |
SQLRowAccessor rowCptTva; |
if (this.achat) { |
if (this.intraComm) { |
// Intra comm TTC=HT et solde de TVA |
rowCptTva = tva.getForeign("ID_COMPTE_PCE_DED_INTRA"); |
if (rowCptTva == null || rowCptTva.isUndefined()) { |
rowCptTva = this.rowDefaultCptTVADeductible; |
} |
SQLRowAccessor rowCptTvaIntra = tva.getForeign("ID_COMPTE_PCE_COLLECTE_INTRA"); |
if (rowCptTvaIntra == null || rowCptTvaIntra.isUndefined()) { |
rowCptTvaIntra = this.rowDefaultCptTVADeductible; |
} |
if (mapHtTVAIntra.get(rowCptTvaIntra) == null) { |
mapHtTVAIntra.put(rowCptTvaIntra, totalTVA); |
} |
ht = ttc; |
} else if (this.achat) { |
rowCptTva = tva.getForeign("ID_COMPTE_PCE_DED"); |
if (rowCptTva == null || rowCptTva.isUndefined()) { |
rowCptTva = this.rowDefaultCptTVADeductible; |
281,6 → 327,14 |
BigDecimal l = mapHtTVA.get(rowCptTva); |
mapHtTVA.put(rowCptTva, l.add(totalTVA)); |
} |
if (ht.signum() != 0) { |
if (mapHtTVARowTaux.get(tva) == null) { |
mapHtTVARowTaux.put(tva, Tuple2.create(ht, totalTVA)); |
} else { |
Tuple2<BigDecimal, BigDecimal> l = mapHtTVARowTaux.get(tva); |
mapHtTVARowTaux.put(tva, Tuple2.create(ht.add(l.get0()), l.get1().add(totalTVA))); |
} |
} |
if (selection) { |
if (mapHtTVASel.get(rowCptTva) == null) { |
mapHtTVASel.put(rowCptTva, totalTVA); |
299,7 → 353,7 |
} |
this.totalTTC = this.totalTTC.add(ttc); |
this.totalHTSansFActurable = this.totalHTSansFActurable.add(htSansFacturable); |
if (selection) { |
if (mapHtSel.get(cptArticle) == null) { |
327,9 → 381,21 |
public void addLine(SQLRowAccessor rowAccessorLine, SQLRowAccessor article, int lineNumber, boolean last) { |
if (rowAccessorLine.getFields().contains("NIVEAU") && rowAccessorLine.getInt("NIVEAU") != 1) { |
return; |
} |
// Total HT de la ligne |
BigDecimal totalLineHT = rowAccessorLine.getObject(fieldHT) == null ? BigDecimal.ZERO : (BigDecimal) rowAccessorLine.getObject(fieldHT); |
BigDecimal totalLineEco = rowAccessorLine.getObject("T_ECO_CONTRIBUTION") == null ? BigDecimal.ZERO : (BigDecimal) rowAccessorLine.getObject("T_ECO_CONTRIBUTION"); |
BigDecimal totalLineHTSansFacturable = totalLineHT; |
if (!achat) { |
totalLineHTSansFacturable = rowAccessorLine.getObject("PV_HT") == null ? BigDecimal.ZERO : (BigDecimal) rowAccessorLine.getObject("PV_HT"); |
BigDecimal qteUV = rowAccessorLine.getObject("QTE_UNITAIRE") == null ? BigDecimal.ZERO : (BigDecimal) rowAccessorLine.getObject("QTE_UNITAIRE"); |
int qte = rowAccessorLine.getInt("QTE"); |
totalLineHTSansFacturable = totalLineHTSansFacturable.multiply(qteUV).multiply(new BigDecimal(qte)); |
} |
// Prix Unitaire de la ligne |
// TODO voir pour passer le prix total et non le prix unitaire |
BigDecimal totalHALigne = rowAccessorLine.getObject(fieldHA) == null ? BigDecimal.ZERO : (BigDecimal) rowAccessorLine.getObject(fieldHA); |
346,6 → 412,7 |
// Si c'est la derniere ligne, on applique le restant de la remise |
if (last) { |
totalLineHT = totalLineHT.subtract(new BigDecimal(this.remiseRestante).movePointLeft(2)); |
totalLineHTSansFacturable = totalLineHTSansFacturable.subtract(new BigDecimal(this.remiseRestante).movePointLeft(2)); |
this.remiseRestante = 0; |
} else { |
BigDecimal percent = totalLineHT.divide(this.totalHTAvantRemise, DecimalUtils.HIGH_PRECISION); |
352,6 → 419,7 |
BigDecimal remiseApply = percent.multiply(new BigDecimal(this.remiseHT), DecimalUtils.HIGH_PRECISION).setScale(0, RoundingMode.HALF_UP); |
totalLineHT = totalLineHT.subtract(remiseApply.movePointLeft(2)); |
totalLineHTSansFacturable = totalLineHTSansFacturable.subtract(remiseApply.movePointLeft(2)); |
this.remiseRestante -= remiseApply.longValue(); |
} |
} |
404,7 → 472,18 |
familleArticle = familleArticle.getForeign("ID_FAMILLE_ARTICLE_PERE"); |
} |
} |
if (!achat) { |
SQLRowAccessor taxeCompl = (article.getFields().contains("ID_TAXE_COMPLEMENTAIRE") ? article.getForeign("ID_TAXE_COMPLEMENTAIRE") : null); |
if (taxeCompl != null && !taxeCompl.isUndefined()) { |
BigDecimal b = this.mapHtTaxeCompl.get(taxeCompl); |
if (b == null) { |
b = BigDecimal.ZERO; |
} |
b = b.add(totalLineHT); |
this.mapHtTaxeCompl.put(taxeCompl, b); |
} |
} |
} |
if (achat) { |
// Total Service |
428,6 → 507,9 |
totalPoids += nPoids == null ? 0 : nPoids.doubleValue(); |
// Eco-contribution |
this.totalEco = this.totalEco.add(totalLineEco); |
// Calcul total sélectionné |
boolean selection = containsInt(selectedRows, lineNumber); |
if (selection) { |
439,6 → 521,7 |
totalServiceSel = totalServiceSel.add(totalLineHT); |
} |
} |
this.totalEcoSel = this.totalEcoSel.add(totalLineEco); |
if (totalLineDevise != null) { |
totalDeviseSel = totalDeviseSel.add(totalLineDevise); |
445,7 → 528,7 |
} |
} |
addHT(totalLineHT, tva, cpt, selection); |
addHT(totalLineHT, totalLineHTSansFacturable, tva, cpt, selection); |
} |
/** |
456,7 → 539,7 |
BigDecimal tva = getTotalTVA(); |
BigDecimal totalTTC2 = getTotalTTC(); |
BigDecimal reste = totalTTC2.subtract(ht.add(tva)); |
if (reste.compareTo(BigDecimal.ZERO) != 0) { |
if (!intraComm && reste.compareTo(BigDecimal.ZERO) != 0) { |
System.err.print("Ecarts: " + reste + "(HT:" + ht); |
System.err.print(" TVA:" + tva); |
System.err.println(" TTC:" + totalTTC2); |
494,6 → 577,10 |
return totalServiceSel; |
} |
public Map<SQLRowAccessor, BigDecimal> getMapHtTaxeCompl() { |
return mapHtTaxeCompl; |
} |
public BigDecimal getTotalHT() { |
BigDecimal ht = BigDecimal.ZERO; |
for (SQLRowAccessor row : this.mapHt.keySet()) { |
511,6 → 598,18 |
return tva; |
} |
public BigDecimal getTotalEco() { |
return this.totalEco.setScale(2, RoundingMode.HALF_UP); |
} |
public BigDecimal getTotalEcoSel() { |
return this.totalEcoSel.setScale(2, RoundingMode.HALF_UP); |
} |
public BigDecimal getTotalHTSansFActurable() { |
return totalHTSansFActurable; |
} |
public BigDecimal getTotalTTC() { |
return this.totalTTC.setScale(2, RoundingMode.HALF_UP); |
} |
541,7 → 640,15 |
return mapHt; |
} |
public Map<SQLRowAccessor, Tuple2<BigDecimal, BigDecimal>> getMapHtTVARowTaux() { |
return mapHtTVARowTaux; |
} |
public Map<SQLRowAccessor, BigDecimal> getMapHtTVA() { |
return mapHtTVA; |
} |
public Map<SQLRowAccessor, BigDecimal> getMapHtTVAIntra() { |
return mapHtTVAIntra; |
} |
} |