Search in sources :

Example 11 with EntityExpr

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

the class PaymentWorker method getPaymentApplied.

/**
 * Method to return the total amount of a payment which is applied to a payment
 * @param payment GenericValue object of the Payment
 * @param actual false for currency of the payment, true for the actual currency
 * @return the applied total as BigDecimal in the currency of the payment
 */
public static BigDecimal getPaymentApplied(GenericValue payment, Boolean actual) {
    BigDecimal paymentApplied = BigDecimal.ZERO;
    List<GenericValue> paymentApplications = null;
    try {
        List<EntityExpr> cond = UtilMisc.toList(EntityCondition.makeCondition("paymentId", EntityOperator.EQUALS, payment.getString("paymentId")), EntityCondition.makeCondition("toPaymentId", EntityOperator.EQUALS, payment.getString("paymentId")));
        EntityCondition partyCond = EntityCondition.makeCondition(cond, EntityOperator.OR);
        paymentApplications = payment.getDelegator().findList("PaymentApplication", partyCond, null, UtilMisc.toList("invoiceId", "billingAccountId"), null, false);
        if (UtilValidate.isNotEmpty(paymentApplications)) {
            for (GenericValue paymentApplication : paymentApplications) {
                BigDecimal amountApplied = paymentApplication.getBigDecimal("amountApplied");
                // check currency invoice and if different convert amount applied for display
                if (actual.equals(Boolean.FALSE) && paymentApplication.get("invoiceId") != null && payment.get("actualCurrencyAmount") != null && payment.get("actualCurrencyUomId") != null) {
                    GenericValue invoice = paymentApplication.getRelatedOne("Invoice", false);
                    if (payment.getString("actualCurrencyUomId").equals(invoice.getString("currencyUomId"))) {
                        amountApplied = amountApplied.multiply(payment.getBigDecimal("amount")).divide(payment.getBigDecimal("actualCurrencyAmount"), new MathContext(100));
                    }
                }
                paymentApplied = paymentApplied.add(amountApplied).setScale(decimals, rounding);
            }
        }
    } catch (GenericEntityException e) {
        Debug.logError(e, "Trouble getting entities", module);
    }
    return paymentApplied;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) BigDecimal(java.math.BigDecimal) EntityExpr(org.apache.ofbiz.entity.condition.EntityExpr) MathContext(java.math.MathContext)

Example 12 with EntityExpr

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

the class UtilAccounting method getGlExchangeRateOfPurchaseInvoice.

public static BigDecimal getGlExchangeRateOfPurchaseInvoice(GenericValue paymentApplication) throws GenericEntityException {
    BigDecimal exchangeRate = BigDecimal.ONE;
    Delegator delegator = paymentApplication.getDelegator();
    List<EntityExpr> andConditions = UtilMisc.toList(EntityCondition.makeCondition("glAccountTypeId", "ACCOUNTS_PAYABLE"), EntityCondition.makeCondition("debitCreditFlag", "C"), EntityCondition.makeCondition("acctgTransTypeId", "PURCHASE_INVOICE"), EntityCondition.makeCondition("invoiceId", paymentApplication.getString("invoiceId")));
    EntityCondition whereCondition = EntityCondition.makeCondition(andConditions, EntityJoinOperator.AND);
    GenericValue amounts = EntityQuery.use(delegator).select("origAmount", "amount").from("AcctgTransAndEntries").where(whereCondition).queryFirst();
    if (amounts == null) {
        return exchangeRate;
    }
    BigDecimal origAmount = amounts.getBigDecimal("origAmount");
    BigDecimal amount = amounts.getBigDecimal("amount");
    if (origAmount != null && amount != null && BigDecimal.ZERO.compareTo(origAmount) != 0 && BigDecimal.ZERO.compareTo(amount) != 0 && amount.compareTo(origAmount) != 0) {
        exchangeRate = amount.divide(origAmount, UtilNumber.getBigDecimalScale("ledger.decimals"), UtilNumber.getRoundingMode("invoice.rounding"));
    }
    return exchangeRate;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) BigDecimal(java.math.BigDecimal) EntityExpr(org.apache.ofbiz.entity.condition.EntityExpr)

Example 13 with EntityExpr

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

the class UtilAccounting method getGlExchangeRateOfOutgoingPayment.

public static BigDecimal getGlExchangeRateOfOutgoingPayment(GenericValue paymentApplication) throws GenericEntityException {
    BigDecimal exchangeRate = BigDecimal.ONE;
    Delegator delegator = paymentApplication.getDelegator();
    List<EntityExpr> andConditions = UtilMisc.toList(EntityCondition.makeCondition("glAccountTypeId", "CURRENT_ASSET"), EntityCondition.makeCondition("debitCreditFlag", "C"), EntityCondition.makeCondition("acctgTransTypeId", "OUTGOING_PAYMENT"), EntityCondition.makeCondition("paymentId", paymentApplication.getString("paymentId")));
    EntityCondition whereCondition = EntityCondition.makeCondition(andConditions, EntityJoinOperator.AND);
    GenericValue amounts = EntityQuery.use(delegator).select("origAmount", "amount").from("AcctgTransAndEntries").where(whereCondition).queryFirst();
    if (amounts == null) {
        return exchangeRate;
    }
    BigDecimal origAmount = amounts.getBigDecimal("origAmount");
    BigDecimal amount = amounts.getBigDecimal("amount");
    if (origAmount != null && amount != null && BigDecimal.ZERO.compareTo(origAmount) != 0 && BigDecimal.ZERO.compareTo(amount) != 0 && amount.compareTo(origAmount) != 0) {
        exchangeRate = amount.divide(origAmount, UtilNumber.getBigDecimalScale("ledger.decimals"), UtilNumber.getRoundingMode("invoice.rounding"));
    }
    return exchangeRate;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) BigDecimal(java.math.BigDecimal) EntityExpr(org.apache.ofbiz.entity.condition.EntityExpr)

Example 14 with EntityExpr

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

the class ContentServicesComplex method getAssocAndContentAndDataResourceMethod.

public static Map<String, Object> getAssocAndContentAndDataResourceMethod(Delegator delegator, String contentId, String mapKey, String direction, Timestamp fromDate, Timestamp thruDate, String fromDateStr, String thruDateStr, List<String> assocTypes, List<String> contentTypes) {
    List<EntityCondition> exprList = new LinkedList<EntityCondition>();
    EntityExpr joinExpr = null;
    String viewName = null;
    if (mapKey != null) {
        EntityExpr mapKeyExpr = EntityCondition.makeCondition("caMapKey", EntityOperator.EQUALS, mapKey);
        exprList.add(mapKeyExpr);
    }
    if (direction != null && "From".equalsIgnoreCase(direction)) {
        joinExpr = EntityCondition.makeCondition("caContentIdTo", EntityOperator.EQUALS, contentId);
        viewName = "ContentAssocDataResourceViewFrom";
    } else {
        joinExpr = EntityCondition.makeCondition("caContentId", EntityOperator.EQUALS, contentId);
        viewName = "ContentAssocDataResourceViewTo";
    }
    exprList.add(joinExpr);
    if (UtilValidate.isNotEmpty(assocTypes)) {
        exprList.add(EntityCondition.makeCondition("caContentAssocTypeId", EntityOperator.IN, assocTypes));
    }
    if (UtilValidate.isNotEmpty(contentTypes)) {
        exprList.add(EntityCondition.makeCondition("contentTypeId", EntityOperator.IN, contentTypes));
    }
    if (fromDate == null && fromDateStr != null) {
        fromDate = UtilDateTime.toTimestamp(fromDateStr);
    }
    if (thruDate == null && thruDateStr != null) {
        thruDate = UtilDateTime.toTimestamp(thruDateStr);
    }
    if (fromDate != null) {
        EntityExpr fromExpr = EntityCondition.makeCondition("caFromDate", EntityOperator.LESS_THAN, fromDate);
        exprList.add(fromExpr);
    }
    if (thruDate != null) {
        List<EntityExpr> thruList = new LinkedList<EntityExpr>();
        EntityExpr thruExpr = EntityCondition.makeCondition("caThruDate", EntityOperator.LESS_THAN, thruDate);
        thruList.add(thruExpr);
        EntityExpr thruExpr2 = EntityCondition.makeCondition("caThruDate", EntityOperator.EQUALS, null);
        thruList.add(thruExpr2);
        EntityConditionList<EntityExpr> thruExprList = EntityCondition.makeCondition(thruList, EntityOperator.OR);
        exprList.add(thruExprList);
    } else if (fromDate != null) {
        List<EntityExpr> thruList = new LinkedList<EntityExpr>();
        EntityExpr thruExpr = EntityCondition.makeCondition("caThruDate", EntityOperator.GREATER_THAN, fromDate);
        thruList.add(thruExpr);
        EntityExpr thruExpr2 = EntityCondition.makeCondition("caThruDate", EntityOperator.EQUALS, null);
        thruList.add(thruExpr2);
        EntityConditionList<EntityExpr> thruExprList = EntityCondition.makeCondition(thruList, EntityOperator.OR);
        exprList.add(thruExprList);
    }
    EntityConditionList<EntityCondition> assocExprList = EntityCondition.makeCondition(exprList, EntityOperator.AND);
    List<GenericValue> relatedAssocs = null;
    try {
        relatedAssocs = EntityQuery.use(delegator).from(viewName).where(assocExprList).orderBy("caFromDate").queryList();
    } catch (GenericEntityException e) {
        return ServiceUtil.returnError(e.getMessage());
    }
    for (int i = 0; i < relatedAssocs.size(); i++) {
        GenericValue a = relatedAssocs.get(i);
        if (Debug.verboseOn())
            Debug.logVerbose(" contentId:" + a.get("contentId") + " To:" + a.get("caContentIdTo") + " fromDate:" + a.get("caFromDate") + " thruDate:" + a.get("caThruDate") + " AssocTypeId:" + a.get("caContentAssocTypeId"), null);
    }
    Map<String, Object> results = new HashMap<String, Object>();
    results.put("entityList", relatedAssocs);
    return results;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) EntityConditionList(org.apache.ofbiz.entity.condition.EntityConditionList) HashMap(java.util.HashMap) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) LinkedList(java.util.LinkedList) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ArrayList(java.util.ArrayList) EntityConditionList(org.apache.ofbiz.entity.condition.EntityConditionList) LinkedList(java.util.LinkedList) List(java.util.List) EntityExpr(org.apache.ofbiz.entity.condition.EntityExpr)

Example 15 with EntityExpr

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

the class JobManager method reloadCrashedJobs.

public synchronized void reloadCrashedJobs() {
    assertIsRunning();
    if (crashedJobsReloaded) {
        return;
    }
    List<GenericValue> crashed = null;
    List<EntityExpr> statusExprList = UtilMisc.toList(EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, "SERVICE_PENDING"), EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, "SERVICE_QUEUED"), EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, "SERVICE_RUNNING"));
    EntityCondition statusCondition = EntityCondition.makeCondition(statusExprList, EntityOperator.OR);
    EntityCondition mainCondition = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("runByInstanceId", instanceId), statusCondition));
    try {
        crashed = EntityQuery.use(delegator).from("JobSandbox").where(mainCondition).orderBy("startDateTime").queryList();
    } catch (GenericEntityException e) {
        Debug.logWarning(e, "Unable to load crashed jobs", module);
    }
    if (UtilValidate.isNotEmpty(crashed)) {
        int rescheduled = 0;
        Timestamp now = UtilDateTime.nowTimestamp();
        for (GenericValue job : crashed) {
            try {
                if (Debug.infoOn()) {
                    Debug.logInfo("Scheduling Job : " + job, module);
                }
                String pJobId = job.getString("parentJobId");
                if (pJobId == null) {
                    pJobId = job.getString("jobId");
                }
                GenericValue newJob = GenericValue.create(job);
                newJob.set("statusId", "SERVICE_PENDING");
                newJob.set("runTime", now);
                newJob.set("previousJobId", job.getString("jobId"));
                newJob.set("parentJobId", pJobId);
                newJob.set("startDateTime", null);
                newJob.set("runByInstanceId", null);
                // don't set a recurrent schedule on the new job, run it just one time
                newJob.set("tempExprId", null);
                newJob.set("recurrenceInfoId", null);
                delegator.createSetNextSeqId(newJob);
                // set the cancel time on the old job to the same as the re-schedule time
                job.set("statusId", "SERVICE_CRASHED");
                job.set("cancelDateTime", now);
                delegator.store(job);
                rescheduled++;
            } catch (GenericEntityException e) {
                Debug.logWarning(e, module);
            }
        }
        if (Debug.infoOn()) {
            Debug.logInfo("-- " + rescheduled + " jobs re-scheduled", module);
        }
    } else {
        if (Debug.infoOn()) {
            Debug.logInfo("No crashed jobs to re-schedule", module);
        }
    }
    crashedJobsReloaded = true;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) Timestamp(java.sql.Timestamp) 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