Dépôt officiel du code source de l'ERP OpenConcerto
/trunk/OpenConcerto/src/org/openconcerto/erp/core/common/ui/TotalCalculator.java |
---|
17,6 → 17,8 |
import org.openconcerto.erp.core.finance.tax.model.TaxeCache; |
import org.openconcerto.erp.preferences.GestionCommercialeGlobalPreferencePanel; |
import org.openconcerto.sql.Configuration; |
import org.openconcerto.sql.model.SQLBackgroundTableCache; |
import org.openconcerto.sql.model.SQLBackgroundTableCacheItem; |
import org.openconcerto.sql.model.SQLRow; |
import org.openconcerto.sql.model.SQLRowAccessor; |
import org.openconcerto.sql.model.SQLRowValues; |
28,6 → 30,7 |
import java.math.BigDecimal; |
import java.math.RoundingMode; |
import java.util.Collection; |
import java.util.HashMap; |
import java.util.HashSet; |
import java.util.List; |
42,6 → 45,8 |
private boolean imputEcart = true; |
private SQLRowAccessor rowDefaultCptProduit, rowDefaultCptService, rowDefaultCptTVACollecte, rowDefaultCptTVADeductible, rowDefaultCptAchat; |
private final SQLRowAccessor rowCatComptable; |
private SQLRowAccessor rowDefaultCptProduitStandard; |
private double totalPoids; |
72,8 → 77,8 |
private BigDecimal totalHTAvantRemise; |
private boolean intraComm = false; |
public TotalCalculator(String fieldHA, String fieldHT, String fieldDeviseTotal) { |
this(fieldHA, fieldHT, fieldDeviseTotal, false, null); |
public TotalCalculator(String fieldHA, String fieldHT, String fieldDeviseTotal, SQLRowAccessor rowCatComptable) { |
this(fieldHA, fieldHT, fieldDeviseTotal, false, null, rowCatComptable); |
} |
public void setRowDefaultCptService(SQLRowAccessor rowDefaultCptService) { |
84,7 → 89,7 |
this.intraComm = intraComm; |
} |
public TotalCalculator(String fieldHA, String fieldHT, String fieldDeviseTotal, boolean achat, SQLRowAccessor defaultCompte) { |
public TotalCalculator(String fieldHA, String fieldHT, String fieldDeviseTotal, boolean achat, SQLRowAccessor defaultCompte, SQLRowAccessor rowCatComptable) { |
this.achat = achat; |
initValues(); |
93,13 → 98,16 |
this.fieldHA = fieldHA; |
this.fieldHT = fieldHT; |
final SQLTable tablePrefCompte = Configuration.getInstance().getRoot().findTable("PREFS_COMPTE"); |
final SQLRow rowPrefsCompte = tablePrefCompte.getRow(2); |
final SQLBackgroundTableCacheItem cacheForTablePref = SQLBackgroundTableCache.getInstance().getCacheForTable(tablePrefCompte); |
final SQLBackgroundTableCacheItem cacheForTableCompte = SQLBackgroundTableCache.getInstance().getCacheForTable(tablePrefCompte.getTable("COMPTE_PCE")); |
final SQLRow rowPrefsCompte = cacheForTablePref.getRowFromId(2); |
SQLPreferences prefs = SQLPreferences.getMemCached(tablePrefCompte.getDBRoot()); |
this.imputEcart = prefs.getBoolean(GestionCommercialeGlobalPreferencePanel.IMPUT_ECART, true); |
// 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"); |
this.rowDefaultCptService = cacheForTableCompte.getRowFromId(rowPrefsCompte.getForeignID("ID_COMPTE_PCE_VENTE_SERVICE")); |
if (this.rowDefaultCptService == null || this.rowDefaultCptService.isUndefined()) { |
try { |
this.rowDefaultCptService = ComptePCESQLElement.getRowComptePceDefault("VentesServices"); |
108,8 → 116,14 |
} |
} |
this.rowCatComptable = rowCatComptable; |
if (defaultCompte == null || defaultCompte.isUndefined()) { |
this.rowDefaultCptProduit = rowPrefsCompte.getForeign("ID_COMPTE_PCE_VENTE_PRODUIT"); |
if (rowCatComptable != null && !rowCatComptable.isUndefined() && !rowCatComptable.isForeignEmpty("ID_COMPTE_PCE_VENTE")) { |
this.rowDefaultCptProduit = cacheForTableCompte.getRowFromId(rowCatComptable.getForeignID("ID_COMPTE_PCE_VENTE")); |
} else { |
this.rowDefaultCptProduit = cacheForTableCompte.getRowFromId(rowPrefsCompte.getForeignID("ID_COMPTE_PCE_VENTE_PRODUIT")); |
if (this.rowDefaultCptProduit == null || this.rowDefaultCptProduit.isUndefined()) { |
try { |
this.rowDefaultCptProduit = ComptePCESQLElement.getRowComptePceDefault("VentesProduits"); |
117,12 → 131,13 |
e.printStackTrace(); |
} |
} |
} |
} else { |
this.rowDefaultCptProduit = defaultCompte; |
} |
this.rowDefaultCptProduitStandard = this.rowDefaultCptProduit; |
this.rowDefaultCptTVACollecte = rowPrefsCompte.getForeign("ID_COMPTE_PCE_TVA_VENTE"); |
this.rowDefaultCptTVACollecte = cacheForTableCompte.getRowFromId(rowPrefsCompte.getForeignID("ID_COMPTE_PCE_TVA_VENTE")); |
if (this.rowDefaultCptTVACollecte == null || this.rowDefaultCptTVACollecte.isUndefined()) { |
try { |
this.rowDefaultCptTVACollecte = ComptePCESQLElement.getRowComptePceDefault("TVACollectee"); |
131,7 → 146,7 |
} |
} |
this.rowDefaultCptTVADeductible = rowPrefsCompte.getForeign("ID_COMPTE_PCE_TVA_ACHAT"); |
this.rowDefaultCptTVADeductible = cacheForTableCompte.getRowFromId(rowPrefsCompte.getForeignID("ID_COMPTE_PCE_TVA_ACHAT")); |
if (this.rowDefaultCptTVADeductible == null || this.rowDefaultCptTVADeductible.isUndefined()) { |
try { |
this.rowDefaultCptTVADeductible = ComptePCESQLElement.getRowComptePceDefault("TVADeductible"); |
141,7 → 156,10 |
} |
if (defaultCompte == null || defaultCompte.isUndefined()) { |
this.rowDefaultCptAchat = rowPrefsCompte.getForeign("ID_COMPTE_PCE_ACHAT"); |
if (rowCatComptable != null && !rowCatComptable.isUndefined() && !rowCatComptable.isForeignEmpty("ID_COMPTE_PCE_ACHAT")) { |
this.rowDefaultCptProduit = cacheForTableCompte.getRowFromId(rowCatComptable.getForeignID("ID_COMPTE_PCE_ACHAT")); |
} else { |
this.rowDefaultCptAchat = cacheForTableCompte.getRowFromId(rowPrefsCompte.getForeignID("ID_COMPTE_PCE_ACHAT")); |
if (this.rowDefaultCptAchat == null || this.rowDefaultCptAchat.isUndefined()) { |
try { |
this.rowDefaultCptAchat = ComptePCESQLElement.getRowComptePceDefault("Achats"); |
149,6 → 167,7 |
e.printStackTrace(); |
} |
} |
} |
} else { |
this.rowDefaultCptAchat = defaultCompte; |
} |
290,6 → 309,7 |
BigDecimal ttc; |
BigDecimal totalTVA; |
final SQLBackgroundTableCacheItem cacheForTableCompte = SQLBackgroundTableCache.getInstance().getCacheForTable(cptArticle.getTable()); |
if (tva == null || tva.isUndefined()) { |
ttc = ht; |
305,27 → 325,31 |
if (this.intraComm) { |
// Intra comm TTC=HT et solde de TVA |
rowCptTva = tva.getForeign("ID_COMPTE_PCE_DED_INTRA"); |
rowCptTva = cacheForTableCompte.getRowFromId(tva.getForeignID("ID_COMPTE_PCE_DED_INTRA")); |
if (rowCptTva == null || rowCptTva.isUndefined()) { |
rowCptTva = this.rowDefaultCptTVADeductible; |
} |
SQLRowAccessor rowCptTvaIntra = tva.getForeign("ID_COMPTE_PCE_COLLECTE_INTRA"); |
SQLRowAccessor rowCptTvaIntra = cacheForTableCompte.getRowFromId(tva.getForeignID("ID_COMPTE_PCE_COLLECTE_INTRA")); |
if (rowCptTvaIntra == null || rowCptTvaIntra.isUndefined()) { |
rowCptTvaIntra = this.rowDefaultCptTVADeductible; |
} |
if (mapHtTVAIntra.get(rowCptTvaIntra) == null) { |
mapHtTVAIntra.put(rowCptTvaIntra, totalTVA); |
} else { |
BigDecimal l = mapHtTVAIntra.get(rowCptTvaIntra); |
mapHtTVAIntra.put(rowCptTvaIntra, l.add(totalTVA)); |
} |
ht = ttc; |
ttc = ht; |
} else if (this.achat) { |
rowCptTva = tva.getForeign("ID_COMPTE_PCE_DED"); |
rowCptTva = cacheForTableCompte.getRowFromId(tva.getForeignID("ID_COMPTE_PCE_DED")); |
if (rowCptTva == null || rowCptTva.isUndefined()) { |
rowCptTva = this.rowDefaultCptTVADeductible; |
} |
} else { |
rowCptTva = tva.getForeign("ID_COMPTE_PCE_COLLECTE"); |
rowCptTva = cacheForTableCompte.getRowFromId(tva.getForeignID("ID_COMPTE_PCE_COLLECTE")); |
if (rowCptTva == null || rowCptTva.isUndefined()) { |
rowCptTva = this.rowDefaultCptTVACollecte; |
} |
437,14 → 461,20 |
if (mapTVA == null || lastTVAfetchedTime + 30 * 1000 < System.currentTimeMillis()) { |
fetchTVA(); |
} |
Integer idTVA = null; |
if (rowAccessorLine.getObject("ID_TAXE") != null && !rowAccessorLine.isForeignEmpty("ID_TAXE")) { |
idTVA = rowAccessorLine.getForeignID("ID_TAXE"); |
} else { |
final SQLRowAccessor foreignTVA = rowAccessorLine.getForeign("ID_TAXE"); |
Integer idTVA = null; |
if (foreignTVA != null) { |
idTVA = foreignTVA.getID(); |
} |
} |
SQLRowAccessor tva = mapTVA.get(idTVA); |
SQLRowAccessor cpt = (achat ? this.rowDefaultCptAchat : this.rowDefaultCptProduit); |
final SQLBackgroundTableCacheItem cacheForTableCompte = SQLBackgroundTableCache.getInstance().getCacheForTable(cpt.getTable()); |
if (!achat) { |
// Total Service |
if (bServiceActive != null && bServiceActive && service != null && service.booleanValue()) { |
451,19 → 481,32 |
totalService = totalService.add(totalLineHT); |
cpt = this.rowDefaultCptService; |
if (tva != null && !tva.isForeignEmpty("ID_COMPTE_PCE_VENTE_SERVICE")) { |
cpt = tva.getForeign("ID_COMPTE_PCE_VENTE_SERVICE"); |
cpt = cacheForTableCompte.getRowFromId(tva.getForeignID("ID_COMPTE_PCE_VENTE_SERVICE")); |
} |
} else { |
// Compte defini par défaut dans la TVA |
if (tva != null && !tva.isForeignEmpty("ID_COMPTE_PCE_VENTE")) { |
cpt = tva.getForeign("ID_COMPTE_PCE_VENTE"); |
cpt = cacheForTableCompte.getRowFromId(tva.getForeignID("ID_COMPTE_PCE_VENTE")); |
} |
} |
} |
if (article != null && !article.isUndefined()) { |
SQLRowAccessor cptCatComptable = null; |
// TODO Optimiser les requetes |
if (rowCatComptable != null && !rowCatComptable.isUndefined()) { |
String suffix = (this.achat ? "_ACHAT" : "_VENTE"); |
Collection<? extends SQLRowAccessor> rows = article.getReferentRows(compteTable.getTable("ARTICLE_CATEGORIE_COMPTABLE")); |
for (SQLRowAccessor sqlRowAccessor : rows) { |
if (sqlRowAccessor.getForeignID("ID_CATEGORIE_COMPTABLE") == this.rowCatComptable.getID()) { |
cptCatComptable = cacheForTableCompte.getRowFromId(rowCatComptable.getForeignID("ID_COMPTE_PCE" + suffix)); |
} |
} |
} |
if (cptCatComptable == null) { |
String suffix = (this.achat ? "_ACHAT" : ""); |
SQLRowAccessor compteArticle = article.getForeign("ID_COMPTE_PCE" + suffix); |
SQLRowAccessor compteArticle = cacheForTableCompte.getRowFromId(article.getForeignID("ID_COMPTE_PCE" + suffix)); |
if (compteArticle != null && !compteArticle.isUndefined()) { |
cpt = compteArticle; |
} else { |
472,15 → 515,19 |
while (familleArticle != null && !familleArticle.isUndefined() && !unique.contains(familleArticle)) { |
unique.add(familleArticle); |
SQLRowAccessor compteFamilleArticle = familleArticle.getForeign("ID_COMPTE_PCE" + suffix); |
if (familleArticle.getObject("ID_COMPTE_PCE" + suffix) != null && !familleArticle.isForeignEmpty("ID_COMPTE_PCE" + suffix)) { |
SQLRowAccessor compteFamilleArticle = cacheForTableCompte.getRowFromId(familleArticle.getForeignID("ID_COMPTE_PCE" + suffix)); |
if (compteFamilleArticle != null && !compteFamilleArticle.isUndefined()) { |
cpt = compteFamilleArticle; |
break; |
} |
} |
familleArticle = familleArticle.getForeign("ID_FAMILLE_ARTICLE_PERE"); |
} |
} |
} else { |
cpt = cptCatComptable; |
} |
if (!achat) { |
SQLRowAccessor taxeCompl = (article.getFields().contains("ID_TAXE_COMPLEMENTAIRE") && article.getObject("ID_TAXE_COMPLEMENTAIRE") != null |
&& !article.isForeignEmpty("ID_TAXE_COMPLEMENTAIRE") ? article.getForeign("ID_TAXE_COMPLEMENTAIRE") : null); |