Search in sources :

Example 46 with EntityExpr

use of org.apache.ofbiz.entity.condition.EntityExpr in project ofbiz-framework by apache.

the class OrderReadHelper method getOrderReturnedQuantity.

/**
 * Get the total quantity of returned items for an order. This will count
 * only the ReturnItems that are directly correlated to an OrderItem.
 */
public BigDecimal getOrderReturnedQuantity() {
    List<GenericValue> returnedItemsBase = getOrderReturnItems();
    List<GenericValue> returnedItems = new ArrayList<>(returnedItemsBase.size());
    // filter just order items
    List<EntityExpr> orderItemExprs = UtilMisc.toList(EntityCondition.makeCondition("returnItemTypeId", EntityOperator.EQUALS, "RET_PROD_ITEM"));
    orderItemExprs.add(EntityCondition.makeCondition("returnItemTypeId", EntityOperator.EQUALS, "RET_FPROD_ITEM"));
    orderItemExprs.add(EntityCondition.makeCondition("returnItemTypeId", EntityOperator.EQUALS, "RET_DPROD_ITEM"));
    orderItemExprs.add(EntityCondition.makeCondition("returnItemTypeId", EntityOperator.EQUALS, "RET_FDPROD_ITEM"));
    orderItemExprs.add(EntityCondition.makeCondition("returnItemTypeId", EntityOperator.EQUALS, "RET_PROD_FEATR_ITEM"));
    orderItemExprs.add(EntityCondition.makeCondition("returnItemTypeId", EntityOperator.EQUALS, "RET_SPROD_ITEM"));
    orderItemExprs.add(EntityCondition.makeCondition("returnItemTypeId", EntityOperator.EQUALS, "RET_WE_ITEM"));
    orderItemExprs.add(EntityCondition.makeCondition("returnItemTypeId", EntityOperator.EQUALS, "RET_TE_ITEM"));
    returnedItemsBase = EntityUtil.filterByOr(returnedItemsBase, orderItemExprs);
    // get only the RETURN_RECEIVED and RETURN_COMPLETED statusIds
    returnedItems.addAll(EntityUtil.filterByAnd(returnedItemsBase, UtilMisc.toMap("statusId", "RETURN_RECEIVED")));
    returnedItems.addAll(EntityUtil.filterByAnd(returnedItemsBase, UtilMisc.toMap("statusId", "RETURN_COMPLETED")));
    BigDecimal returnedQuantity = ZERO;
    for (GenericValue returnedItem : returnedItems) {
        if (returnedItem.get("returnQuantity") != null) {
            returnedQuantity = returnedQuantity.add(returnedItem.getBigDecimal("returnQuantity")).setScale(scale, rounding);
        }
    }
    return returnedQuantity.setScale(scale, rounding);
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) ArrayList(java.util.ArrayList) EntityExpr(org.apache.ofbiz.entity.condition.EntityExpr) BigDecimal(java.math.BigDecimal)

Example 47 with EntityExpr

use of org.apache.ofbiz.entity.condition.EntityExpr in project ofbiz-framework by apache.

the class OrderReadHelper method getOrderPaymentReceivedTotalByType.

/**
 * Get the total payment received amount by payment type.  Specify null to get amount
 * over all types. This method works by going through all the PaymentAndApplications
 * for all order Invoices that have status PMNT_RECEIVED.
 */
public BigDecimal getOrderPaymentReceivedTotalByType(String paymentMethodTypeId) {
    BigDecimal total = ZERO;
    try {
        // get a set of invoice IDs that belong to the order
        List<GenericValue> orderItemBillings = orderHeader.getRelated("OrderItemBilling", null, null, false);
        Set<String> invoiceIds = new HashSet<>();
        for (GenericValue orderItemBilling : orderItemBillings) {
            invoiceIds.add(orderItemBilling.getString("invoiceId"));
        }
        // 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<EntityExpr> conditions = UtilMisc.toList(EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, "PMNT_RECEIVED"), EntityCondition.makeCondition("invoiceId", EntityOperator.IN, invoiceIds));
        if (paymentMethodTypeId != null) {
            conditions.add(EntityCondition.makeCondition("paymentMethodTypeId", EntityOperator.EQUALS, paymentMethodTypeId));
        }
        EntityConditionList<EntityExpr> ecl = EntityCondition.makeCondition(conditions, EntityOperator.AND);
        List<GenericValue> payments = orderHeader.getDelegator().findList("PaymentAndApplication", ecl, null, null, null, true);
        for (GenericValue payment : payments) {
            if (payment.get("amountApplied") == null) {
                continue;
            }
            total = total.add(payment.getBigDecimal("amountApplied")).setScale(scale, rounding);
        }
    } catch (GenericEntityException e) {
        Debug.logError(e, e.getMessage(), module);
    }
    return total;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) BigDecimal(java.math.BigDecimal) EntityExpr(org.apache.ofbiz.entity.condition.EntityExpr) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 48 with EntityExpr

use of org.apache.ofbiz.entity.condition.EntityExpr in project ofbiz-framework by apache.

the class OrderReadHelper method getValidDigitalItems.

public List<GenericValue> getValidDigitalItems() {
    List<GenericValue> digitalItems = new LinkedList<>();
    // only approved or complete items apply
    List<EntityExpr> exprs = UtilMisc.toList(EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, "ITEM_APPROVED"), EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, "ITEM_COMPLETED"));
    List<GenericValue> items = EntityUtil.filterByOr(getOrderItems(), exprs);
    for (GenericValue item : items) {
        if (item.get("productId") != null) {
            GenericValue product = null;
            try {
                product = item.getRelatedOne("Product", false);
            } catch (GenericEntityException e) {
                Debug.logError(e, "Unable to get Product from OrderItem", module);
            }
            if (product != null) {
                GenericValue productType = null;
                try {
                    productType = product.getRelatedOne("ProductType", false);
                } catch (GenericEntityException e) {
                    Debug.logError(e, "ERROR: Unable to get ProductType from Product", module);
                }
                if (productType != null) {
                    String isDigital = productType.getString("isDigital");
                    if (isDigital != null && "Y".equalsIgnoreCase(isDigital)) {
                        // make sure we have an OrderItemBilling record
                        List<GenericValue> orderItemBillings = null;
                        try {
                            orderItemBillings = item.getRelated("OrderItemBilling", null, null, false);
                        } catch (GenericEntityException e) {
                            Debug.logError(e, "Unable to get OrderItemBilling from OrderItem");
                        }
                        if (UtilValidate.isNotEmpty(orderItemBillings)) {
                            // get the ProductContent records
                            List<GenericValue> productContents = null;
                            try {
                                productContents = product.getRelated("ProductContent", null, null, false);
                            } catch (GenericEntityException e) {
                                Debug.logError("Unable to get ProductContent from Product", module);
                            }
                            List<EntityExpr> cExprs = UtilMisc.toList(EntityCondition.makeCondition("productContentTypeId", EntityOperator.EQUALS, "DIGITAL_DOWNLOAD"), EntityCondition.makeCondition("productContentTypeId", EntityOperator.EQUALS, "FULFILLMENT_EMAIL"), EntityCondition.makeCondition("productContentTypeId", EntityOperator.EQUALS, "FULFILLMENT_EXTERNAL"));
                            // add more as needed
                            productContents = EntityUtil.filterByDate(productContents);
                            productContents = EntityUtil.filterByOr(productContents, cExprs);
                            if (UtilValidate.isNotEmpty(productContents)) {
                                // make sure we are still within the allowed timeframe and use limits
                                for (GenericValue productContent : productContents) {
                                    Timestamp fromDate = productContent.getTimestamp("purchaseFromDate");
                                    Timestamp thruDate = productContent.getTimestamp("purchaseThruDate");
                                    if (fromDate == null || item.getTimestamp("orderDate").after(fromDate)) {
                                        if (thruDate == null || item.getTimestamp("orderDate").before(thruDate)) {
                                            // TODO: Implement use count and days
                                            digitalItems.add(item);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return digitalItems;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) Timestamp(java.sql.Timestamp) LinkedList(java.util.LinkedList) EntityExpr(org.apache.ofbiz.entity.condition.EntityExpr)

Example 49 with EntityExpr

use of org.apache.ofbiz.entity.condition.EntityExpr in project ofbiz-framework by apache.

the class OrderReadHelper method getItemPickedQuantityBd.

public BigDecimal getItemPickedQuantityBd(GenericValue orderItem) {
    BigDecimal quantityPicked = ZERO;
    EntityConditionList<EntityExpr> pickedConditions = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("orderId", EntityOperator.EQUALS, orderItem.get("orderId")), EntityCondition.makeCondition("orderItemSeqId", EntityOperator.EQUALS, orderItem.getString("orderItemSeqId")), EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "PICKLIST_CANCELLED")), EntityOperator.AND);
    List<GenericValue> picked = null;
    try {
        picked = orderHeader.getDelegator().findList("PicklistAndBinAndItem", pickedConditions, null, null, null, false);
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
        this.orderHeader = null;
    }
    if (picked != null) {
        for (GenericValue pickedItem : picked) {
            BigDecimal issueQty = pickedItem.getBigDecimal("quantity");
            if (issueQty != null) {
                quantityPicked = quantityPicked.add(issueQty).setScale(scale, rounding);
            }
        }
    }
    return quantityPicked.setScale(scale, rounding);
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) BigDecimal(java.math.BigDecimal) EntityExpr(org.apache.ofbiz.entity.condition.EntityExpr)

Example 50 with EntityExpr

use of org.apache.ofbiz.entity.condition.EntityExpr in project ofbiz-framework by apache.

the class OrderReadHelper method getReceivedPaymentTotalsByPaymentMethod.

/**
 * Returns a Map of paymentMethodId -&gt; amount charged (BigDecimal) based on PaymentGatewayResponse.
 * @return returns a Map of paymentMethodId -&gt; amount charged (BigDecimal) based on PaymentGatewayResponse.
 */
public Map<String, BigDecimal> getReceivedPaymentTotalsByPaymentMethod() {
    Map<String, BigDecimal> paymentMethodAmounts = new HashMap<>();
    List<GenericValue> paymentPrefs = getPaymentPreferences();
    for (GenericValue paymentPref : paymentPrefs) {
        List<GenericValue> payments = new LinkedList<>();
        try {
            List<EntityExpr> exprs = UtilMisc.toList(EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, "PMNT_RECEIVED"), EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, "PMNT_CONFIRMED"));
            payments = paymentPref.getRelated("Payment", null, null, false);
            payments = EntityUtil.filterByOr(payments, exprs);
            List<EntityExpr> conds = UtilMisc.toList(EntityCondition.makeCondition("paymentTypeId", EntityOperator.EQUALS, "CUSTOMER_PAYMENT"), EntityCondition.makeCondition("paymentTypeId", EntityOperator.EQUALS, "CUSTOMER_DEPOSIT"), EntityCondition.makeCondition("paymentTypeId", EntityOperator.EQUALS, "INTEREST_RECEIPT"), EntityCondition.makeCondition("paymentTypeId", EntityOperator.EQUALS, "GC_DEPOSIT"), EntityCondition.makeCondition("paymentTypeId", EntityOperator.EQUALS, "POS_PAID_IN"));
            payments = EntityUtil.filterByOr(payments, conds);
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
        }
        BigDecimal chargedToPaymentPref = ZERO;
        for (GenericValue payment : payments) {
            if (payment.get("amount") != null) {
                chargedToPaymentPref = chargedToPaymentPref.add(payment.getBigDecimal("amount")).setScale(scale + 1, rounding);
            }
        }
        if (chargedToPaymentPref.compareTo(ZERO) > 0) {
            // key of the resulting map is paymentMethodId or paymentMethodTypeId if the paymentMethodId is not available
            String paymentMethodKey = paymentPref.getString("paymentMethodId") != null ? paymentPref.getString("paymentMethodId") : paymentPref.getString("paymentMethodTypeId");
            if (paymentMethodAmounts.containsKey(paymentMethodKey)) {
                BigDecimal value = paymentMethodAmounts.get(paymentMethodKey);
                if (value != null) {
                    chargedToPaymentPref = chargedToPaymentPref.add(value);
                }
            }
            paymentMethodAmounts.put(paymentMethodKey, chargedToPaymentPref.setScale(scale, rounding));
        }
    }
    return paymentMethodAmounts;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) BigDecimal(java.math.BigDecimal) LinkedList(java.util.LinkedList) EntityExpr(org.apache.ofbiz.entity.condition.EntityExpr)

Aggregations

EntityExpr (org.apache.ofbiz.entity.condition.EntityExpr)57 GenericValue (org.apache.ofbiz.entity.GenericValue)51 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)47 Delegator (org.apache.ofbiz.entity.Delegator)29 LinkedList (java.util.LinkedList)27 HashMap (java.util.HashMap)24 Locale (java.util.Locale)23 BigDecimal (java.math.BigDecimal)19 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)18 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)16 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)15 Timestamp (java.sql.Timestamp)13 ArrayList (java.util.ArrayList)11 GeneralException (org.apache.ofbiz.base.util.GeneralException)7 List (java.util.List)6 Map (java.util.Map)6 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)6 Calendar (com.ibm.icu.util.Calendar)4 LinkedHashSet (java.util.LinkedHashSet)4 EntityConditionList (org.apache.ofbiz.entity.condition.EntityConditionList)4