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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations