diff --git a/applications/accounting/script/org/ofbiz/accounting/ledger/GeneralLedgerServices.xml b/applications/accounting/script/org/ofbiz/accounting/ledger/GeneralLedgerServices.xml index e8b5c3eb7e..8d8f620d16 100644 --- a/applications/accounting/script/org/ofbiz/accounting/ledger/GeneralLedgerServices.xml +++ b/applications/accounting/script/org/ofbiz/accounting/ledger/GeneralLedgerServices.xml @@ -1976,7 +1976,14 @@ under the License. - + + + + + + + + @@ -2052,12 +2059,38 @@ under the License. + + + + + + + + + + + + + + + + + + + + + + + + + @@ -2081,13 +2114,17 @@ under the License. - + - - - + + + + + + + @@ -2121,11 +2158,38 @@ under the License. + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceWorker.java b/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceWorker.java index d6f2f19d1b..1c1409ee66 100644 --- a/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceWorker.java +++ b/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceWorker.java @@ -23,8 +23,10 @@ import java.math.MathContext; import java.sql.Timestamp; import java.util.Iterator; import java.util.List; +import java.util.Map; import javolution.util.FastList; +import javolution.util.FastMap; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilDateTime; @@ -37,6 +39,7 @@ import org.ofbiz.entity.GenericEntityException; import org.ofbiz.entity.GenericValue; import org.ofbiz.entity.condition.EntityCondition; import org.ofbiz.entity.condition.EntityConditionList; +import org.ofbiz.entity.condition.EntityExpr; import org.ofbiz.entity.condition.EntityOperator; import org.ofbiz.entity.util.EntityUtil; import org.ofbiz.service.LocalDispatcher; @@ -116,8 +119,8 @@ public class InvoiceWorker { GenericDelegator delegator = invoice.getDelegator(); EntityConditionList condition = EntityCondition.makeCondition(UtilMisc.toList( EntityCondition.makeCondition("invoiceId", invoice.get("invoiceId")), - EntityCondition.makeCondition("invoiceItemTypeId", EntityOperator.IN, getTaxableInvoiceItemTypeIds(delegator)) - ), EntityOperator.AND); + EntityCondition.makeCondition("invoiceItemTypeId", EntityOperator.IN, getTaxableInvoiceItemTypeIds(delegator))), + EntityOperator.AND); invoiceTaxItems = delegator.findList("InvoiceItem", condition, null, null, null, false); } catch (GenericEntityException e) { Debug.logError(e, "Trouble getting InvoiceItem list", module); @@ -155,9 +158,15 @@ public class InvoiceWorker { public static BigDecimal getInvoiceTotal(GenericValue invoice, Boolean actualCurrency) { BigDecimal invoiceTotal = ZERO; BigDecimal invoiceTaxTotal = ZERO; + Map invoiceTaxByTaxAuthGeoAndPartyResult = getInvoiceTaxByTaxAuthGeoAndParty(invoice); + List taxByTaxAuthGeoAndPartyList = (List) invoiceTaxByTaxAuthGeoAndPartyResult.get("taxByTaxAuthGeoAndPartyList"); + invoiceTaxTotal = (BigDecimal) invoiceTaxByTaxAuthGeoAndPartyResult.get("taxGrandTotal"); + List invoiceItems = null; try { invoiceItems = invoice.getRelated("InvoiceItem"); + invoiceItems = EntityUtil.filterByAnd(invoiceItems, UtilMisc.toList(EntityCondition.makeCondition("invoiceItemTypeId", EntityOperator.NOT_EQUAL, "PINV_SALES_TAX"), EntityCondition.makeCondition("invoiceItemTypeId", EntityOperator.NOT_EQUAL, "PITM_SALES_TAX"), + EntityCondition.makeCondition("invoiceItemTypeId", EntityOperator.NOT_EQUAL, "ITM_SALES_TAX"), EntityCondition.makeCondition("invoiceItemTypeId", EntityOperator.NOT_EQUAL, "INV_SALES_TAX"))); } catch (GenericEntityException e) { Debug.logError(e, "Trouble getting InvoiceItem list", module); } @@ -171,11 +180,7 @@ public class InvoiceWorker { amount = ZERO; if (quantity == null) quantity = BigDecimal.ONE; - if ("ITM_SALES_TAX".equals(invoiceItem.get("invoiceItemTypeId"))) { - invoiceTaxTotal = invoiceTaxTotal.add(amount.multiply(quantity)).setScale(taxDecimals, taxRounding); - } else { - invoiceTotal = invoiceTotal.add(amount.multiply(quantity)).setScale(decimals,rounding); - } + invoiceTotal = invoiceTotal.add( amount.multiply(quantity)).setScale(decimals,rounding); } } invoiceTotal = invoiceTotal.add(invoiceTaxTotal).setScale(decimals, rounding); @@ -605,4 +610,54 @@ public class InvoiceWorker { return getInvoiceCurrencyConversionRate(invoice); } + + public static Map getInvoiceTaxByTaxAuthGeoAndParty(GenericValue invoice) { + BigDecimal taxGrandTotal = ZERO; + List> taxByTaxAuthGeoAndPartyList = FastList.newInstance(); + List invoiceItems = null; + if (UtilValidate.isNotEmpty(invoice)) { + try { + invoiceItems = invoice.getRelated("InvoiceItem"); + } catch (GenericEntityException e) { + Debug.logError(e, "Trouble getting InvoiceItem list", module); + } + if ("SALES_INVOICE".equals(invoice.get("invoiceTypeId"))) { + invoiceItems = EntityUtil.filterByOr(invoiceItems, UtilMisc.toList(EntityCondition.makeCondition("invoiceItemTypeId", EntityOperator.EQUALS, "INV_SALES_TAX"), EntityCondition.makeCondition("invoiceItemTypeId", EntityOperator.EQUALS, "ITM_SALES_TAX"))); + } else if (("PURCHASE_INVOICE".equals(invoice.get("invoiceTypeId")))) { + invoiceItems = EntityUtil.filterByOr(invoiceItems, UtilMisc.toList(EntityCondition.makeCondition("invoiceItemTypeId", EntityOperator.EQUALS, "PINV_SALES_TAX"), EntityCondition.makeCondition("invoiceItemTypeId", EntityOperator.EQUALS, "PITM_SALES_TAX"))); + } else { + invoiceItems = null; + } + if (UtilValidate.isNotEmpty(invoiceItems)) { + invoiceItems = EntityUtil.orderBy(invoiceItems, UtilMisc.toList("taxAuthGeoId","taxAuthPartyId")); + // get the list of all distinct taxAuthGeoId and taxAuthPartyId. It is for getting the number of taxAuthGeoId and taxAuthPartyId in invoiceItems. + List distinctTaxAuthGeoIdList = EntityUtil.getFieldListFromEntityList(invoiceItems, "taxAuthGeoId", true); + List distinctTaxAuthPartyIdList = EntityUtil.getFieldListFromEntityList(invoiceItems, "taxAuthPartyId", true); + for (String taxAuthGeoId : distinctTaxAuthGeoIdList ) { + for (String taxAuthPartyId : distinctTaxAuthPartyIdList) { + //get all records for invoices filtered by taxAuthGeoId and taxAurhPartyId + List invoiceItemsByTaxAuthGeoAndPartyIds = EntityUtil.filterByAnd(invoiceItems, UtilMisc.toMap("taxAuthGeoId", taxAuthGeoId, "taxAuthPartyId", taxAuthPartyId)); + if (UtilValidate.isNotEmpty(invoiceItemsByTaxAuthGeoAndPartyIds)) { + BigDecimal totalAmount = ZERO; + //Now for each invoiceItem record get and add amount. + for (GenericValue invoiceItem : invoiceItemsByTaxAuthGeoAndPartyIds) { + BigDecimal amount = invoiceItem.getBigDecimal("amount"); + if (amount == null) { + amount = ZERO; + } + totalAmount = totalAmount.add(amount).setScale(taxDecimals, taxRounding); + } + totalAmount = totalAmount.setScale(UtilNumber.getBigDecimalScale("salestax.calc.decimals"), UtilNumber.getBigDecimalRoundingMode("salestax.rounding")); + taxByTaxAuthGeoAndPartyList.add(UtilMisc.toMap("taxAuthPartyId", taxAuthPartyId, "taxAuthGeoId", taxAuthGeoId, "totalAmount", totalAmount)); + taxGrandTotal = taxGrandTotal.add(totalAmount); + } + } + } + } + } + Map result = FastMap.newInstance(); + result.put("taxByTaxAuthGeoAndPartyList", taxByTaxAuthGeoAndPartyList); + result.put("taxGrandTotal", taxGrandTotal); + return result; + } } diff --git a/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java b/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java index a6f2e80b36..09abdbcef5 100644 --- a/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java +++ b/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java @@ -1104,8 +1104,7 @@ public class OrderReadHelper { // get the payments of the desired type for these invoices TODO: in models where invoices can have many orders, this needs to be refined List conditions = UtilMisc.toList( EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, "PMNT_RECEIVED"), - EntityCondition.makeCondition("invoiceId", EntityOperator.IN, invoiceIds) - ); + EntityCondition.makeCondition("invoiceId", EntityOperator.IN, invoiceIds)); if (paymentMethodTypeId != null) { conditions.add(EntityCondition.makeCondition("paymentMethodTypeId", EntityOperator.EQUALS, paymentMethodTypeId)); } @@ -2282,8 +2281,13 @@ public class OrderReadHelper { } public static BigDecimal getOrderGrandTotal(List orderItems, List adjustments) { + Map orderTaxByTaxAuthGeoAndParty = getOrderTaxByTaxAuthGeoAndParty(adjustments); + List taxByTaxAuthGeoAndPartyList = (List) orderTaxByTaxAuthGeoAndParty.get("taxByTaxAuthGeoAndPartyList"); + BigDecimal taxGrandTotal = (BigDecimal) orderTaxByTaxAuthGeoAndParty.get("taxGrandTotal"); + adjustments = EntityUtil.filterByAnd(adjustments, UtilMisc.toList(EntityCondition.makeCondition("orderAdjustmentTypeId", EntityOperator.NOT_EQUAL, "SALES_TAX"))); BigDecimal total = getOrderItemsTotal(orderItems, adjustments); BigDecimal adj = getOrderAdjustmentsTotal(orderItems, adjustments); + total = total.add(taxGrandTotal).setScale(scale,rounding); return total.add(adj).setScale(scale,rounding); } @@ -2833,4 +2837,44 @@ public class OrderReadHelper { return EntityUtil.orderBy(EntityUtil.filterByAnd(newOrderStatuses, contraints2), UtilMisc.toList("-statusDatetime")); } + + public static Map getOrderTaxByTaxAuthGeoAndParty(List orderAdjustments) { + BigDecimal taxGrandTotal = BigDecimal.ZERO; + List> taxByTaxAuthGeoAndPartyList = FastList.newInstance(); + if (UtilValidate.isNotEmpty(orderAdjustments)) { + // get orderAdjustment where orderAdjustmentTypeId is SALES_TAX. + orderAdjustments = EntityUtil.filterByAnd(orderAdjustments, UtilMisc.toMap("orderAdjustmentTypeId","SALES_TAX")); + orderAdjustments = EntityUtil.orderBy(orderAdjustments, UtilMisc.toList("taxAuthGeoId","taxAuthPartyId")); + + // get the list of all distinct taxAuthGeoId and taxAuthPartyId. It is for getting the number of taxAuthGeo and taxAuthPartyId in adjustments. + List distinctTaxAuthGeoIdList = EntityUtil.getFieldListFromEntityList(orderAdjustments, "taxAuthGeoId", true); + List distinctTaxAuthPartyIdList = EntityUtil.getFieldListFromEntityList(orderAdjustments, "taxAuthPartyId", true); + + // For each taxAuthGeoId get and add amount from orderAdjustment + for (String taxAuthGeoId : distinctTaxAuthGeoIdList) { + for (String taxAuthPartyId : distinctTaxAuthPartyIdList) { + //get all records for orderAdjustments filtered by taxAuthGeoId and taxAurhPartyId + List orderAdjByTaxAuthGeoAndPartyIds = EntityUtil.filterByAnd(orderAdjustments, UtilMisc.toMap("taxAuthGeoId", taxAuthGeoId, "taxAuthPartyId", taxAuthPartyId)); + if (UtilValidate.isNotEmpty(orderAdjByTaxAuthGeoAndPartyIds)) { + BigDecimal totalAmount = BigDecimal.ZERO; + //Now for each orderAdjustment record get and add amount. + for (GenericValue orderAdjustment : orderAdjByTaxAuthGeoAndPartyIds) { + BigDecimal amount = orderAdjustment.getBigDecimal("amount"); + if (amount == null) { + amount = ZERO; + } + totalAmount = totalAmount.add(amount).setScale(UtilNumber.getBigDecimalScale("salestax.calc.decimals"), taxRounding); + } + totalAmount = totalAmount.setScale(UtilNumber.getBigDecimalScale("salestax.final.decimals"), UtilNumber.getBigDecimalRoundingMode("salestax.rounding")); + taxByTaxAuthGeoAndPartyList.add(UtilMisc.toMap("taxAuthPartyId", taxAuthPartyId, "taxAuthGeoId", taxAuthGeoId, "totalAmount", totalAmount)); + taxGrandTotal = taxGrandTotal.add(totalAmount); + } + } + } + } + Map result = FastMap.newInstance(); + result.put("taxByTaxAuthGeoAndPartyList", taxByTaxAuthGeoAndPartyList); + result.put("taxGrandTotal", taxGrandTotal); + return result; + } }