use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class FinAccountServices method refundFinAccount.
public static Map<String, Object> refundFinAccount(DispatchContext dctx, Map<String, Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
Locale locale = (Locale) context.get("locale");
GenericValue userLogin = (GenericValue) context.get("userLogin");
String finAccountId = (String) context.get("finAccountId");
Map<String, Object> result = null;
GenericValue finAccount;
try {
finAccount = EntityQuery.use(delegator).from("FinAccount").where("finAccountId", finAccountId).queryOne();
} catch (GenericEntityException e) {
return ServiceUtil.returnError(e.getMessage());
}
if (finAccount != null) {
// check to make sure the account is refundable
if (!"Y".equals(finAccount.getString("isRefundable"))) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingFinAccountIsNotRefundable", locale));
}
// get the actual and available balance
BigDecimal availableBalance = finAccount.getBigDecimal("availableBalance");
BigDecimal actualBalance = finAccount.getBigDecimal("actualBalance");
// be settled first
if (actualBalance.compareTo(availableBalance) != 0) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingFinAccountCannotBeRefunded", locale));
}
// now we make sure there is something to refund
if (actualBalance.compareTo(BigDecimal.ZERO) > 0) {
BigDecimal remainingBalance = new BigDecimal(actualBalance.toString());
BigDecimal refundAmount = BigDecimal.ZERO;
List<EntityExpr> exprs = UtilMisc.toList(EntityCondition.makeCondition("finAccountTransTypeId", EntityOperator.EQUALS, "DEPOSIT"), EntityCondition.makeCondition("finAccountId", EntityOperator.EQUALS, finAccountId));
EntityCondition condition = EntityCondition.makeCondition(exprs, EntityOperator.AND);
try (EntityListIterator eli = EntityQuery.use(delegator).from("FinAccountTrans").where(condition).orderBy("-transactionDate").queryIterator()) {
GenericValue trans;
while (remainingBalance.compareTo(FinAccountHelper.ZERO) < 0 && (trans = eli.next()) != null) {
String orderId = trans.getString("orderId");
String orderItemSeqId = trans.getString("orderItemSeqId");
// make sure there is an order available to refund
if (orderId != null && orderItemSeqId != null) {
GenericValue orderHeader = EntityQuery.use(delegator).from("OrderHeader").where("orderId", orderId).queryOne();
GenericValue productStore = orderHeader.getRelatedOne("ProductStore", false);
GenericValue orderItem = EntityQuery.use(delegator).from("OrderItem").where("orderId", orderId, "orderItemSeqId", orderItemSeqId).queryOne();
if (!"ITEM_CANCELLED".equals(orderItem.getString("statusId"))) {
// make sure the item hasn't already been returned
List<GenericValue> returnItems = orderItem.getRelated("ReturnItem", null, null, false);
if (UtilValidate.isEmpty(returnItems)) {
BigDecimal txAmt = trans.getBigDecimal("amount");
BigDecimal refAmt = txAmt;
if (remainingBalance.compareTo(txAmt) == -1) {
refAmt = remainingBalance;
}
remainingBalance = remainingBalance.subtract(refAmt);
refundAmount = refundAmount.add(refAmt);
// create the return header
Map<String, Object> rhCtx = UtilMisc.toMap("returnHeaderTypeId", "CUSTOMER_RETURN", "fromPartyId", finAccount.getString("ownerPartyId"), "toPartyId", productStore.getString("payToPartyId"), "userLogin", userLogin);
Map<String, Object> rhResp = dispatcher.runSync("createReturnHeader", rhCtx);
if (ServiceUtil.isError(rhResp)) {
throw new GeneralException(ServiceUtil.getErrorMessage(rhResp));
}
String returnId = (String) rhResp.get("returnId");
// create the return item
Map<String, Object> returnItemCtx = new HashMap<>();
returnItemCtx.put("returnId", returnId);
returnItemCtx.put("orderId", orderId);
returnItemCtx.put("description", orderItem.getString("itemDescription"));
returnItemCtx.put("orderItemSeqId", orderItemSeqId);
returnItemCtx.put("returnQuantity", BigDecimal.ONE);
returnItemCtx.put("receivedQuantity", BigDecimal.ONE);
returnItemCtx.put("returnPrice", refAmt);
returnItemCtx.put("returnReasonId", "RTN_NOT_WANT");
// refund return
returnItemCtx.put("returnTypeId", "RTN_REFUND");
returnItemCtx.put("returnItemTypeId", "RET_NPROD_ITEM");
returnItemCtx.put("userLogin", userLogin);
Map<String, Object> retItResp = dispatcher.runSync("createReturnItem", returnItemCtx);
if (ServiceUtil.isError(retItResp)) {
throw new GeneralException(ServiceUtil.getErrorMessage(retItResp));
}
String returnItemSeqId = (String) retItResp.get("returnItemSeqId");
// approve the return
Map<String, Object> appRet = UtilMisc.toMap("statusId", "RETURN_ACCEPTED", "returnId", returnId, "userLogin", userLogin);
Map<String, Object> appResp = dispatcher.runSync("updateReturnHeader", appRet);
if (ServiceUtil.isError(appResp)) {
throw new GeneralException(ServiceUtil.getErrorMessage(appResp));
}
// "receive" the return - should trigger the refund
Map<String, Object> recRet = UtilMisc.toMap("statusId", "RETURN_RECEIVED", "returnId", returnId, "userLogin", userLogin);
Map<String, Object> recResp = dispatcher.runSync("updateReturnHeader", recRet);
if (ServiceUtil.isError(recResp)) {
throw new GeneralException(ServiceUtil.getErrorMessage(recResp));
}
// get the return item
GenericValue returnItem = EntityQuery.use(delegator).from("ReturnItem").where("returnId", returnId, "returnItemSeqId", returnItemSeqId).queryOne();
GenericValue response = returnItem.getRelatedOne("ReturnItemResponse", false);
if (response == null) {
throw new GeneralException("No return response found for: " + returnItem.getPrimaryKey());
}
String paymentId = response.getString("paymentId");
// create the adjustment transaction
Map<String, Object> txCtx = new HashMap<>();
txCtx.put("finAccountTransTypeId", "ADJUSTMENT");
txCtx.put("finAccountId", finAccountId);
txCtx.put("orderId", orderId);
txCtx.put("orderItemSeqId", orderItemSeqId);
txCtx.put("paymentId", paymentId);
txCtx.put("amount", refAmt.negate());
txCtx.put("partyId", finAccount.getString("ownerPartyId"));
txCtx.put("userLogin", userLogin);
Map<String, Object> txResp = dispatcher.runSync("createFinAccountTrans", txCtx);
if (ServiceUtil.isError(txResp)) {
throw new GeneralException(ServiceUtil.getErrorMessage(txResp));
}
}
}
}
}
} catch (GeneralException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
// check to make sure we balanced out
if (remainingBalance.compareTo(FinAccountHelper.ZERO) == 1) {
result = ServiceUtil.returnSuccess(UtilProperties.getMessage(resourceError, "AccountingFinAccountPartiallyRefunded", locale));
}
}
}
if (result == null) {
result = ServiceUtil.returnSuccess();
}
return result;
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class InvoiceServices method createInvoicesFromShipments.
public static Map<String, Object> createInvoicesFromShipments(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
List<String> shipmentIds = UtilGenerics.checkList(context.get("shipmentIds"));
Locale locale = (Locale) context.get("locale");
Boolean createSalesInvoicesForDropShipments = (Boolean) context.get("createSalesInvoicesForDropShipments");
if (UtilValidate.isEmpty(createSalesInvoicesForDropShipments)) {
createSalesInvoicesForDropShipments = Boolean.FALSE;
}
boolean salesShipmentFound = false;
boolean purchaseShipmentFound = false;
boolean dropShipmentFound = false;
List<String> invoicesCreated = new LinkedList<>();
// DEJ20060520: not used? planned to be used? List shipmentIdList = new LinkedList();
for (String tmpShipmentId : shipmentIds) {
try {
GenericValue shipment = EntityQuery.use(delegator).from("Shipment").where("shipmentId", tmpShipmentId).queryOne();
if ((shipment.getString("shipmentTypeId") != null) && ("PURCHASE_SHIPMENT".equals(shipment.getString("shipmentTypeId")))) {
purchaseShipmentFound = true;
} else if ((shipment.getString("shipmentTypeId") != null) && ("DROP_SHIPMENT".equals(shipment.getString("shipmentTypeId")))) {
dropShipmentFound = true;
} else {
salesShipmentFound = true;
}
if (purchaseShipmentFound && salesShipmentFound && dropShipmentFound) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingShipmentsOfDifferentTypes", UtilMisc.toMap("tmpShipmentId", tmpShipmentId, "shipmentTypeId", shipment.getString("shipmentTypeId")), locale));
}
} catch (GenericEntityException e) {
Debug.logError(e, "Trouble getting Shipment entity for shipment " + tmpShipmentId, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingTroubleGettingShipmentEntity", UtilMisc.toMap("tmpShipmentId", tmpShipmentId), locale));
}
}
EntityQuery shipmentQuery = EntityQuery.use(delegator).where(EntityCondition.makeCondition("shipmentId", EntityOperator.IN, shipmentIds)).orderBy("shipmentId");
// check the status of the shipment
// get the items of the shipment. They can come from ItemIssuance if the shipment were from a sales order, ShipmentReceipt
// if it were a purchase order or from the order items of the (possibly linked) orders if the shipment is a drop shipment
List<GenericValue> items = null;
List<GenericValue> orderItemAssocs = null;
try {
if (purchaseShipmentFound) {
items = shipmentQuery.from("ShipmentReceipt").queryList();
// filter out items which have been received but are not actually owned by an internal organization, so they should not be on a purchase invoice
Iterator<GenericValue> itemsIter = items.iterator();
while (itemsIter.hasNext()) {
GenericValue item = itemsIter.next();
GenericValue inventoryItem = item.getRelatedOne("InventoryItem", false);
GenericValue ownerPartyRole = EntityQuery.use(delegator).from("PartyRole").where("partyId", inventoryItem.get("ownerPartyId"), "roleTypeId", "INTERNAL_ORGANIZATIO").cache().queryOne();
if (UtilValidate.isEmpty(ownerPartyRole)) {
itemsIter.remove();
}
}
} else if (dropShipmentFound) {
List<GenericValue> shipments = shipmentQuery.from("Shipment").queryList();
// Get the list of purchase order IDs related to the shipments
List<String> purchaseOrderIds = EntityUtil.getFieldListFromEntityList(shipments, "primaryOrderId", true);
if (createSalesInvoicesForDropShipments) {
// If a sales invoice is being created for a drop shipment, we have to reference the original sales order items
// Get the list of the linked orderIds (original sales orders)
orderItemAssocs = EntityQuery.use(delegator).from("OrderItemAssoc").where(EntityCondition.makeCondition("toOrderId", EntityOperator.IN, purchaseOrderIds)).queryList();
// Get only the order items which are indirectly related to the purchase order - this limits the list to the drop ship group(s)
items = EntityUtil.getRelated("FromOrderItem", null, orderItemAssocs, false);
} else {
// If it's a purchase invoice being created, the order items for that purchase orders can be used directly
items = EntityQuery.use(delegator).from("OrderItem").where(EntityCondition.makeCondition("orderId", EntityOperator.IN, purchaseOrderIds)).queryList();
}
} else {
items = shipmentQuery.from("ItemIssuance").queryList();
}
} catch (GenericEntityException e) {
Debug.logError(e, "Problem getting issued items from shipments", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingProblemGettingItemsFromShipments", locale));
}
if (items.size() == 0) {
Debug.logInfo("No items issued for shipments", module);
return ServiceUtil.returnSuccess();
}
// group items by order
Map<String, List<GenericValue>> shippedOrderItems = new HashMap<>();
for (GenericValue item : items) {
String orderId = item.getString("orderId");
String orderItemSeqId = item.getString("orderItemSeqId");
List<GenericValue> itemsByOrder = shippedOrderItems.get(orderId);
if (itemsByOrder == null) {
itemsByOrder = new LinkedList<>();
}
// check and make sure we haven't already billed for this issuance or shipment receipt
List<EntityCondition> billFields = new LinkedList<>();
billFields.add(EntityCondition.makeCondition("orderId", orderId));
billFields.add(EntityCondition.makeCondition("orderItemSeqId", orderItemSeqId));
billFields.add(EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "INVOICE_CANCELLED"));
if (dropShipmentFound) {
// Drop shipments have neither issuances nor receipts, so this check is meaningless
itemsByOrder.add(item);
shippedOrderItems.put(orderId, itemsByOrder);
continue;
} else if ("ItemIssuance".equals(item.getEntityName())) {
billFields.add(EntityCondition.makeCondition("itemIssuanceId", item.get("itemIssuanceId")));
} else if ("ShipmentReceipt".equals(item.getEntityName())) {
billFields.add(EntityCondition.makeCondition("shipmentReceiptId", item.getString("receiptId")));
}
List<GenericValue> itemBillings = null;
try {
itemBillings = EntityQuery.use(delegator).from("OrderItemBillingAndInvoiceAndItem").where(billFields).queryList();
} catch (GenericEntityException e) {
Debug.logError(e, "Problem looking up OrderItemBilling records for " + billFields, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingProblemLookingUpOrderItemBilling", UtilMisc.toMap("billFields", billFields), locale));
}
// if none found, then okay to bill
if (itemBillings.size() == 0) {
itemsByOrder.add(item);
}
// update the map with modified list
shippedOrderItems.put(orderId, itemsByOrder);
}
// make sure we aren't billing items already invoiced i.e. items billed as digital (FINDIG)
for (Entry<String, List<GenericValue>> order : shippedOrderItems.entrySet()) {
String orderId = order.getKey();
// we'll only use this list to figure out which ones to send
List<GenericValue> billItems = order.getValue();
// a new list to be used to pass to the create invoice service
List<GenericValue> toBillItems = new LinkedList<>();
// map of available quantities so we only have to calc once
Map<String, BigDecimal> itemQtyAvail = new HashMap<>();
// now we will check each issuance and make sure it hasn't already been billed
for (GenericValue issue : billItems) {
BigDecimal issueQty = BigDecimal.ZERO;
if ("ShipmentReceipt".equals(issue.getEntityName())) {
issueQty = issue.getBigDecimal("quantityAccepted");
} else {
issueQty = issue.getBigDecimal("quantity");
}
BigDecimal billAvail = itemQtyAvail.get(issue.getString("orderItemSeqId"));
if (billAvail == null) {
List<EntityCondition> lookup = new LinkedList<>();
lookup.add(EntityCondition.makeCondition("orderId", orderId));
lookup.add(EntityCondition.makeCondition("orderItemSeqId", issue.get("orderItemSeqId")));
lookup.add(EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "INVOICE_CANCELLED"));
GenericValue orderItem = null;
List<GenericValue> billed = null;
BigDecimal orderedQty = null;
try {
orderItem = "OrderItem".equals(issue.getEntityName()) ? issue : issue.getRelatedOne("OrderItem", false);
// total ordered
orderedQty = orderItem.getBigDecimal("quantity");
if (dropShipmentFound && createSalesInvoicesForDropShipments.booleanValue()) {
// Override the issueQty with the quantity from the purchase order item
GenericValue orderItemAssoc = EntityUtil.getFirst(EntityUtil.filterByAnd(orderItemAssocs, UtilMisc.toMap("orderId", issue.getString("orderId"), "orderItemSeqId", issue.getString("orderItemSeqId"))));
GenericValue purchaseOrderItem = orderItemAssoc.getRelatedOne("ToOrderItem", false);
orderItem.set("quantity", purchaseOrderItem.getBigDecimal("quantity"));
issueQty = purchaseOrderItem.getBigDecimal("quantity");
}
billed = EntityQuery.use(delegator).from("OrderItemBillingAndInvoiceAndItem").where(lookup).queryList();
} catch (GenericEntityException e) {
Debug.logError(e, "Problem getting OrderItem/OrderItemBilling records " + lookup, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingProblemGettingOrderItemOrderItemBilling", UtilMisc.toMap("lookup", lookup), locale));
}
// add up the already billed total
if (billed.size() > 0) {
BigDecimal billedQuantity = BigDecimal.ZERO;
for (GenericValue oib : billed) {
BigDecimal qty = oib.getBigDecimal("quantity");
if (qty != null) {
billedQuantity = billedQuantity.add(qty).setScale(DECIMALS, ROUNDING);
}
}
BigDecimal leftToBill = orderedQty.subtract(billedQuantity).setScale(DECIMALS, ROUNDING);
billAvail = leftToBill;
} else {
billAvail = orderedQty;
}
}
// no available means we cannot bill anymore
if (billAvail != null && billAvail.signum() == 1) {
// this checks if billAvail is a positive non-zero number
if (issueQty != null && issueQty.compareTo(billAvail) > 0) {
// can only bill some of the issuance; others have been billed already
if ("ShipmentReceipt".equals(issue.getEntityName())) {
issue.set("quantityAccepted", billAvail);
} else {
issue.set("quantity", billAvail);
}
billAvail = BigDecimal.ZERO;
} else {
// now have been billed
if (issueQty == null) {
issueQty = BigDecimal.ZERO;
}
billAvail = billAvail.subtract(issueQty).setScale(DECIMALS, ROUNDING);
}
// okay to bill these items; but none else
toBillItems.add(issue);
}
// update the available to bill quantity for the next pass
itemQtyAvail.put(issue.getString("orderItemSeqId"), billAvail);
}
OrderReadHelper orh = new OrderReadHelper(delegator, orderId);
GenericValue productStore = orh.getProductStore();
String prorateShipping = productStore != null ? productStore.getString("prorateShipping") : "N";
// If shipping charges are not prorated, the shipments need to be examined for additional shipping charges
if ("N".equalsIgnoreCase(prorateShipping)) {
// Get the set of filtered shipments
List<GenericValue> invoiceableShipments = null;
try {
if (dropShipmentFound) {
List<String> invoiceablePrimaryOrderIds = null;
if (createSalesInvoicesForDropShipments) {
// If a sales invoice is being created for the drop shipment, we need to reference back to the original purchase order IDs
// Get the IDs for orders which have billable items
List<String> invoiceableLinkedOrderIds = EntityUtil.getFieldListFromEntityList(toBillItems, "orderId", true);
// Get back the IDs of the purchase orders - this will be a list of the purchase order items which are billable by virtue of not having been
// invoiced in a previous sales invoice
List<GenericValue> reverseOrderItemAssocs = EntityUtil.filterByCondition(orderItemAssocs, EntityCondition.makeCondition("orderId", EntityOperator.IN, invoiceableLinkedOrderIds));
invoiceablePrimaryOrderIds = EntityUtil.getFieldListFromEntityList(reverseOrderItemAssocs, "toOrderId", true);
} else {
// If a purchase order is being created for a drop shipment, the purchase order IDs can be used directly
invoiceablePrimaryOrderIds = EntityUtil.getFieldListFromEntityList(toBillItems, "orderId", true);
}
// Get the list of shipments which are associated with the filtered purchase orders
if (!UtilValidate.isEmpty(invoiceablePrimaryOrderIds)) {
invoiceableShipments = EntityQuery.use(delegator).from("Shipment").where(UtilMisc.toList(EntityCondition.makeCondition("primaryOrderId", EntityOperator.IN, invoiceablePrimaryOrderIds), EntityCondition.makeCondition("shipmentId", EntityOperator.IN, shipmentIds))).queryList();
}
} else {
List<String> invoiceableShipmentIds = EntityUtil.getFieldListFromEntityList(toBillItems, "shipmentId", true);
if (UtilValidate.isNotEmpty(invoiceableShipmentIds)) {
invoiceableShipments = EntityQuery.use(delegator).from("Shipment").where(EntityCondition.makeCondition("shipmentId", EntityOperator.IN, invoiceableShipmentIds)).queryList();
}
}
} catch (GenericEntityException e) {
Debug.logError(e, "Trouble calling createInvoicesFromShipments service", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingTroubleCallingCreateInvoicesFromShipmentsService", locale));
}
// Total the additional shipping charges for the shipments
Map<GenericValue, BigDecimal> additionalShippingCharges = new HashMap<>();
BigDecimal totalAdditionalShippingCharges = BigDecimal.ZERO;
if (UtilValidate.isNotEmpty(invoiceableShipments)) {
for (GenericValue shipment : invoiceableShipments) {
if (shipment.get("additionalShippingCharge") == null) {
continue;
}
BigDecimal shipmentAdditionalShippingCharges = shipment.getBigDecimal("additionalShippingCharge").setScale(DECIMALS, ROUNDING);
additionalShippingCharges.put(shipment, shipmentAdditionalShippingCharges);
totalAdditionalShippingCharges = totalAdditionalShippingCharges.add(shipmentAdditionalShippingCharges);
}
}
// If the additional shipping charges are greater than zero, process them
if (totalAdditionalShippingCharges.signum() == 1) {
// Add an OrderAdjustment to the order for each additional shipping charge
for (Map.Entry<GenericValue, BigDecimal> entry : additionalShippingCharges.entrySet()) {
GenericValue shipment = entry.getKey();
BigDecimal additionalShippingCharge = entry.getValue();
String shipmentId = shipment.getString("shipmentId");
Map<String, Object> createOrderAdjustmentContext = new HashMap<>();
createOrderAdjustmentContext.put("orderId", orderId);
createOrderAdjustmentContext.put("orderAdjustmentTypeId", "SHIPPING_CHARGES");
String addtlChargeDescription = shipment.getString("addtlShippingChargeDesc");
if (UtilValidate.isEmpty(addtlChargeDescription)) {
addtlChargeDescription = UtilProperties.getMessage(resource, "AccountingAdditionalShippingChargeForShipment", UtilMisc.toMap("shipmentId", shipmentId), locale);
}
createOrderAdjustmentContext.put("description", addtlChargeDescription);
createOrderAdjustmentContext.put("sourceReferenceId", shipmentId);
createOrderAdjustmentContext.put("amount", additionalShippingCharge);
createOrderAdjustmentContext.put("userLogin", context.get("userLogin"));
String shippingOrderAdjustmentId = null;
try {
Map<String, Object> createOrderAdjustmentResult = dispatcher.runSync("createOrderAdjustment", createOrderAdjustmentContext);
if (ServiceUtil.isError(createOrderAdjustmentResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(createOrderAdjustmentResult));
}
shippingOrderAdjustmentId = (String) createOrderAdjustmentResult.get("orderAdjustmentId");
} catch (GenericServiceException e) {
Debug.logError(e, "Trouble calling createOrderAdjustment service", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingTroubleCallingCreateOrderAdjustmentService", locale));
}
// Obtain a list of OrderAdjustments due to tax on the shipping charges, if any
GenericValue billToParty = orh.getBillToParty();
GenericValue payToParty = orh.getBillFromParty();
GenericValue destinationContactMech = null;
try {
destinationContactMech = shipment.getRelatedOne("DestinationPostalAddress", false);
} catch (GenericEntityException e) {
Debug.logError(e, "Trouble calling createInvoicesFromShipment service; invoice not created for shipment " + shipmentId, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingTroubleCallingCreateInvoicesFromShipmentService", locale));
}
List<Object> emptyList = new LinkedList<>();
Map<String, Object> calcTaxContext = new HashMap<>();
calcTaxContext.put("productStoreId", orh.getProductStoreId());
calcTaxContext.put("payToPartyId", payToParty.getString("partyId"));
calcTaxContext.put("billToPartyId", billToParty.getString("partyId"));
calcTaxContext.put("orderShippingAmount", totalAdditionalShippingCharges);
calcTaxContext.put("shippingAddress", destinationContactMech);
// These parameters don't matter if we're only worried about adjustments on the shipping charges
calcTaxContext.put("itemProductList", emptyList);
calcTaxContext.put("itemAmountList", emptyList);
calcTaxContext.put("itemPriceList", emptyList);
calcTaxContext.put("itemQuantityList", emptyList);
calcTaxContext.put("itemShippingList", emptyList);
Map<String, Object> calcTaxResult = null;
try {
calcTaxResult = dispatcher.runSync("calcTax", calcTaxContext);
if (ServiceUtil.isError(calcTaxResult)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingTroubleCallingCalcTaxService", locale));
}
} catch (GenericServiceException e) {
Debug.logError(e, "Trouble calling calcTaxService", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingTroubleCallingCalcTaxService", locale));
}
List<GenericValue> orderAdjustments = UtilGenerics.checkList(calcTaxResult.get("orderAdjustments"));
// If we have any OrderAdjustments due to tax on shipping, store them and add them to the total
if (orderAdjustments != null) {
for (GenericValue orderAdjustment : orderAdjustments) {
totalAdditionalShippingCharges = totalAdditionalShippingCharges.add(orderAdjustment.getBigDecimal("amount").setScale(DECIMALS, ROUNDING));
orderAdjustment.set("orderAdjustmentId", delegator.getNextSeqId("OrderAdjustment"));
orderAdjustment.set("orderId", orderId);
orderAdjustment.set("orderItemSeqId", "_NA_");
orderAdjustment.set("shipGroupSeqId", shipment.getString("primaryShipGroupSeqId"));
orderAdjustment.set("originalAdjustmentId", shippingOrderAdjustmentId);
}
try {
delegator.storeAll(orderAdjustments);
} catch (GenericEntityException e) {
Debug.logError(e, "Problem storing OrderAdjustments: " + orderAdjustments, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingProblemStoringOrderAdjustments", UtilMisc.toMap("orderAdjustments", orderAdjustments), locale));
}
}
// If part of the order was paid via credit card, try to charge it for the additional shipping
List<GenericValue> orderPaymentPreferences = null;
try {
orderPaymentPreferences = EntityQuery.use(delegator).from("OrderPaymentPreference").where("orderId", orderId, "paymentMethodTypeId", "CREDIT_CARD").queryList();
} catch (GenericEntityException e) {
Debug.logError(e, "Problem getting OrderPaymentPreference records", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingProblemGettingOrderPaymentPreferences", locale));
}
// Use the first credit card we find, for the sake of simplicity
String paymentMethodId = null;
GenericValue cardOrderPaymentPref = EntityUtil.getFirst(orderPaymentPreferences);
if (cardOrderPaymentPref != null) {
paymentMethodId = cardOrderPaymentPref.getString("paymentMethodId");
}
if (paymentMethodId != null) {
// Release all outstanding (not settled or cancelled) authorizations, while keeping a running
// total of their amounts so that the total plus the additional shipping charges can be authorized again
// all at once.
BigDecimal totalNewAuthAmount = totalAdditionalShippingCharges.setScale(DECIMALS, ROUNDING);
for (GenericValue orderPaymentPreference : orderPaymentPreferences) {
if (!("PAYMENT_SETTLED".equals(orderPaymentPreference.getString("statusId")) || "PAYMENT_CANCELLED".equals(orderPaymentPreference.getString("statusId")))) {
GenericValue authTransaction = PaymentGatewayServices.getAuthTransaction(orderPaymentPreference);
if (authTransaction != null && authTransaction.get("amount") != null) {
// Update the total authorized amount
totalNewAuthAmount = totalNewAuthAmount.add(authTransaction.getBigDecimal("amount").setScale(DECIMALS, ROUNDING));
// Release the authorization for the OrderPaymentPreference
Map<String, Object> prefReleaseResult = null;
try {
prefReleaseResult = dispatcher.runSync("releaseOrderPaymentPreference", UtilMisc.toMap("orderPaymentPreferenceId", orderPaymentPreference.getString("orderPaymentPreferenceId"), "userLogin", context.get("userLogin")));
} catch (GenericServiceException e) {
Debug.logError(e, "Trouble calling releaseOrderPaymentPreference service", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingTroubleCallingReleaseOrderPaymentPreferenceService", locale));
}
if (ServiceUtil.isError(prefReleaseResult) || ServiceUtil.isFailure(prefReleaseResult)) {
String errMsg = ServiceUtil.getErrorMessage(prefReleaseResult);
Debug.logError(errMsg, module);
return ServiceUtil.returnError(errMsg);
}
}
}
}
// Create a new OrderPaymentPreference for the order to handle the new (totalled) charge. Don't
// set the maxAmount so that it doesn't interfere with other authorizations
Map<String, Object> serviceContext = UtilMisc.toMap("orderId", orderId, "paymentMethodId", paymentMethodId, "paymentMethodTypeId", "CREDIT_CARD", "userLogin", context.get("userLogin"));
String orderPaymentPreferenceId = null;
try {
Map<String, Object> result = dispatcher.runSync("createOrderPaymentPreference", serviceContext);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingTroubleCallingCreateOrderPaymentPreferenceService", locale));
}
orderPaymentPreferenceId = (String) result.get("orderPaymentPreferenceId");
} catch (GenericServiceException e) {
Debug.logError(e, "Trouble calling createOrderPaymentPreference service", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingTroubleCallingCreateOrderPaymentPreferenceService", locale));
}
// Attempt to authorize the new orderPaymentPreference
Map<String, Object> authResult = null;
try {
// Use an overrideAmount because the maxAmount wasn't set on the OrderPaymentPreference
authResult = dispatcher.runSync("authOrderPaymentPreference", UtilMisc.toMap("orderPaymentPreferenceId", orderPaymentPreferenceId, "overrideAmount", totalNewAuthAmount, "userLogin", context.get("userLogin")));
if (ServiceUtil.isError(authResult)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingTroubleCallingAuthOrderPaymentPreferenceService", locale));
}
} catch (GenericServiceException e) {
Debug.logError(e, "Trouble calling authOrderPaymentPreference service", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingTroubleCallingAuthOrderPaymentPreferenceService", locale));
}
// If the authorization fails, create the invoice anyway, but make a note of it
boolean authFinished = ((Boolean) authResult.get("finished")).booleanValue();
boolean authErrors = ((Boolean) authResult.get("errors")).booleanValue();
if (authErrors || !authFinished) {
String errMsg = UtilProperties.getMessage(resource, "AccountingUnableToAuthAdditionalShipCharges", UtilMisc.toMap("shipmentId", shipmentId, "paymentMethodId", paymentMethodId, "orderPaymentPreferenceId", orderPaymentPreferenceId), locale);
Debug.logError(errMsg, module);
}
}
}
}
} else {
Debug.logInfo(UtilProperties.getMessage(resource, "AccountingIgnoringAdditionalShipCharges", UtilMisc.toMap("productStoreId", orh.getProductStoreId()), locale), module);
}
String invoiceId = null;
GenericValue shipmentItemBilling = null;
String shipmentId = shipmentIds.get(0);
try {
shipmentItemBilling = EntityQuery.use(delegator).from("ShipmentItemBilling").where("shipmentId", shipmentId).queryFirst();
} catch (GenericEntityException e) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingProblemGettingShipmentItemBilling", locale));
}
if (shipmentItemBilling != null) {
invoiceId = shipmentItemBilling.getString("invoiceId");
}
// call the createInvoiceForOrder service for each order
Map<String, Object> serviceContext = UtilMisc.toMap("orderId", orderId, "billItems", toBillItems, "invoiceId", invoiceId, "eventDate", context.get("eventDate"), "userLogin", context.get("userLogin"));
try {
Map<String, Object> result = dispatcher.runSync("createInvoiceForOrder", serviceContext);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingTroubleCallingCreateInvoiceForOrderService", locale));
}
invoicesCreated.add((String) result.get("invoiceId"));
} catch (GenericServiceException e) {
Debug.logError(e, "Trouble calling createInvoiceForOrder service; invoice not created for shipment", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingTroubleCallingCreateInvoiceForOrderService", locale));
}
}
Map<String, Object> response = ServiceUtil.returnSuccess();
response.put("invoicesCreated", invoicesCreated);
return response;
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class InvoiceWorker method getInvoiceApplied.
/**
* Returns amount applied to invoice before an asOfDateTime, based on Payment.effectiveDate <= asOfDateTime
*
* @param delegator the delegator
* @param invoiceId the invoice id
* @param asOfDateTime - a Timestamp
* @return returns amount applied to invoice before an asOfDateTime
*/
public static BigDecimal getInvoiceApplied(Delegator delegator, String invoiceId, Timestamp asOfDateTime, Boolean actualCurrency) {
if (delegator == null) {
throw new IllegalArgumentException("Null delegator is not allowed in this method");
}
BigDecimal invoiceApplied = BigDecimal.ZERO;
List<GenericValue> paymentApplications = null;
// lookup payment applications which took place before the asOfDateTime for this invoice
EntityConditionList<EntityExpr> dateCondition = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("effectiveDate", EntityOperator.EQUALS, null), EntityCondition.makeCondition("effectiveDate", EntityOperator.LESS_THAN_EQUAL_TO, asOfDateTime)), EntityOperator.OR);
EntityConditionList<EntityCondition> conditions = EntityCondition.makeCondition(UtilMisc.toList(dateCondition, EntityCondition.makeCondition("invoiceId", EntityOperator.EQUALS, invoiceId)), EntityOperator.AND);
try {
paymentApplications = EntityQuery.use(delegator).from("PaymentAndApplication").where(conditions).orderBy("effectiveDate").queryList();
} catch (GenericEntityException e) {
Debug.logError(e, "Trouble getting paymentApplicationlist", module);
}
if (paymentApplications != null) {
for (GenericValue paymentApplication : paymentApplications) {
invoiceApplied = invoiceApplied.add(paymentApplication.getBigDecimal("amountApplied")).setScale(decimals, rounding);
}
}
if (UtilValidate.isNotEmpty(invoiceApplied) && !actualCurrency) {
invoiceApplied = invoiceApplied.multiply(getInvoiceCurrencyConversionRate(delegator, invoiceId)).setScale(decimals, rounding);
}
return invoiceApplied;
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class PaymentGatewayServices method releaseOrderPayments.
/**
* Releases authorizations through service calls to the defined processing service for the ProductStore/PaymentMethodType
* @return COMPLETE|FAILED|ERROR for complete processing of ALL payments.
*/
public static Map<String, Object> releaseOrderPayments(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
String orderPaymentPreferenceId = (String) context.get("orderPaymentPreferenceId");
Locale locale = (Locale) context.get("locale");
Map<String, Object> result = ServiceUtil.returnSuccess();
String orderId = "";
// Get the OrderPaymentPreference
GenericValue paymentPref = null;
try {
if (orderPaymentPreferenceId != null) {
paymentPref = EntityQuery.use(delegator).from("OrderPaymentPreference").where("orderPaymentPreferenceId", orderPaymentPreferenceId).queryOne();
orderId = paymentPref.getString("orderId");
} else {
orderId = (String) context.get("orderId");
}
} catch (GenericEntityException e) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingProblemGettingOrderPaymentPreferences", locale) + " " + orderPaymentPreferenceId);
}
// get the payment preferences
List<GenericValue> paymentPrefs = null;
try {
// get the valid payment prefs
List<EntityExpr> othExpr = UtilMisc.toList(EntityCondition.makeCondition("paymentMethodTypeId", EntityOperator.EQUALS, "EFT_ACCOUNT"));
othExpr.add(EntityCondition.makeCondition("paymentMethodTypeId", EntityOperator.EQUALS, "GIFT_CARD"));
othExpr.add(EntityCondition.makeCondition("paymentMethodTypeId", EntityOperator.EQUALS, "FIN_ACCOUNT"));
EntityCondition con1 = EntityCondition.makeCondition(othExpr, EntityJoinOperator.OR);
EntityCondition statExpr = EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, "PAYMENT_SETTLED");
EntityCondition con2 = EntityCondition.makeCondition(UtilMisc.toList(con1, statExpr), EntityOperator.AND);
EntityCondition authExpr = EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, "PAYMENT_AUTHORIZED");
EntityCondition con3 = EntityCondition.makeCondition(UtilMisc.toList(con2, authExpr), EntityOperator.OR);
EntityExpr orderExpr = EntityCondition.makeCondition("orderId", EntityOperator.EQUALS, orderId);
EntityCondition con4 = EntityCondition.makeCondition(UtilMisc.toList(con3, orderExpr), EntityOperator.AND);
paymentPrefs = EntityQuery.use(delegator).from("OrderPaymentPreference").where(con4).queryList();
} catch (GenericEntityException gee) {
Debug.logError(gee, "Problems getting entity record(s), see stack trace", module);
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
result.put(ModelService.ERROR_MESSAGE, "ERROR: Could not get order information (" + gee.toString() + ").");
return result;
}
// return complete if no payment prefs were found
if (paymentPrefs.size() == 0) {
Debug.logWarning("No OrderPaymentPreference records available for release", module);
result.put("processResult", "COMPLETE");
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
return result;
}
// iterate over the prefs and release each one
List<GenericValue> finished = new LinkedList<>();
for (GenericValue pPref : paymentPrefs) {
Map<String, Object> releaseContext = UtilMisc.toMap("userLogin", userLogin, "orderPaymentPreferenceId", pPref.getString("orderPaymentPreferenceId"));
Map<String, Object> releaseResult = null;
try {
releaseResult = dispatcher.runSync("releaseOrderPaymentPreference", releaseContext);
} catch (GenericServiceException e) {
Debug.logError(e, "Problem calling releaseOrderPaymentPreference service for orderPaymentPreferenceId" + paymentPref.getString("orderPaymentPreferenceId"), module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingTroubleCallingReleaseOrderPaymentPreferenceService", locale) + " " + paymentPref.getString("orderPaymentPreferenceId"));
}
if (ServiceUtil.isError(releaseResult)) {
Debug.logError(ServiceUtil.getErrorMessage(releaseResult), module);
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(releaseResult));
} else if (!ServiceUtil.isFailure(releaseResult)) {
finished.add(paymentPref);
}
}
result = ServiceUtil.returnSuccess();
if (finished.size() == paymentPrefs.size()) {
result.put("processResult", "COMPLETE");
} else {
result.put("processResult", "FAILED");
}
return result;
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class TaxAuthorityServices method handlePartyTaxExempt.
private static void handlePartyTaxExempt(GenericValue adjValue, Set<String> billToPartyIdSet, String taxAuthGeoId, String taxAuthPartyId, BigDecimal taxAmount, Timestamp nowTimestamp, Delegator delegator) throws GenericEntityException {
Debug.logInfo("Checking for tax exemption : " + taxAuthGeoId + " / " + taxAuthPartyId, module);
List<EntityCondition> ptiConditionList = UtilMisc.<EntityCondition>toList(EntityCondition.makeCondition("partyId", EntityOperator.IN, billToPartyIdSet), EntityCondition.makeCondition("taxAuthGeoId", EntityOperator.EQUALS, taxAuthGeoId), EntityCondition.makeCondition("taxAuthPartyId", EntityOperator.EQUALS, taxAuthPartyId));
ptiConditionList.add(EntityCondition.makeCondition("fromDate", EntityOperator.LESS_THAN_EQUAL_TO, nowTimestamp));
ptiConditionList.add(EntityCondition.makeCondition(EntityCondition.makeCondition("thruDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("thruDate", EntityOperator.GREATER_THAN, nowTimestamp)));
EntityCondition ptiCondition = EntityCondition.makeCondition(ptiConditionList, EntityOperator.AND);
// sort by -fromDate to get the newest (largest) first, just in case there is
// more than one, we only want the most recent valid one, should only be one per
// jurisdiction...
GenericValue partyTaxInfo = EntityQuery.use(delegator).from("PartyTaxAuthInfo").where(ptiCondition).orderBy("-fromDate").queryFirst();
boolean foundExemption = false;
if (partyTaxInfo != null) {
adjValue.set("customerReferenceId", partyTaxInfo.get("partyTaxId"));
if ("Y".equals(partyTaxInfo.getString("isExempt"))) {
adjValue.set("amount", BigDecimal.ZERO);
adjValue.set("exemptAmount", taxAmount);
foundExemption = true;
}
}
// if no exceptions were found for the current; try the parent
if (!foundExemption) {
// try the "parent" TaxAuthority
GenericValue taxAuthorityAssoc = EntityQuery.use(delegator).from("TaxAuthorityAssoc").where("toTaxAuthGeoId", taxAuthGeoId, "toTaxAuthPartyId", taxAuthPartyId, "taxAuthorityAssocTypeId", "EXEMPT_INHER").orderBy("-fromDate").filterByDate().queryFirst();
if (taxAuthorityAssoc != null) {
handlePartyTaxExempt(adjValue, billToPartyIdSet, taxAuthorityAssoc.getString("taxAuthGeoId"), taxAuthorityAssoc.getString("taxAuthPartyId"), taxAmount, nowTimestamp, delegator);
}
}
}
Aggregations