[英]How do I get the total amount on all items on a for loop?
我有 6 個訂單。 我循環了 6 個訂單以獲得每個訂單的總價。
我想要做的是在循環中添加每個訂單的總價格,然后將其與
authorized amount
進行比較。
假設授權金額為 2000 。
發生的情況是將授權金額與 PER ORDER TOTAL PRICE 進行比較,而不是ALL ORDERS TOTAL PRICE 。
這是一個示例代碼:
for (Entity checkout : checkouts) {
List<Entity> selectedJOs = AppUtil.getEntities(AppUtil.getListIds(checkout, "job-orders"), jobOrders);
long finalGrandTotalAmount = 0;
for (Entity job_order : selectedJOs) {
grandTotalAmount = 0;
long totalJobOrderPurchasedAmount = 0;
long totalJobOrderDeliveryFee = 0;
long totalJobOrderShoppingFee = 0;
long totalJobOrderDiscount = 0;
totalJobOrderRecalled = 0;
long totalCampaignDiscount = 0;
if (!job_order.attributes.status.equals("cancelled")) {
totalJobOrderPurchasedAmount += (job_order.attributes.purchased_deliverables_amount_in_cents + job_order.attributes.non_membership_fee_in_cents);
totalJobOrderDeliveryFee += job_order.attributes.delivery_fee_in_cents;
totalJobOrderShoppingFee += job_order.attributes.shopping_fee_in_cents;
totalJobOrderRecalled += job_order.attributes.recalled_deliverables_amount_in_cents;
List<Entity> selectedCampaigns = AppUtil.getEntities(AppUtil.getListIds(job_order, "job-order-fmcg-campaign-vouchers"), campaignDiscounts);
for (Entity campaignDiscount : selectedCampaigns) {
totalCampaignDiscount += campaignDiscount.attributes.discount_in_cents;
}
for (Entity discount : discounts) {
long job_order_discount;
if (discount.relationships.job_order.data.id.equals(job_order.id)) {
long purchase_amount = job_order.attributes.purchased_deliverables_amount_in_cents;
if (discount.attributes.sum_in_cents_off <= 0) {
job_order_discount = Math.round(purchase_amount * discount.attributes.percent_off);
} else {
job_order_discount = discount.attributes.sum_in_cents_off;
}
totalJobOrderDiscount += job_order_discount;
}
}
long discounts_total = totalJobOrderDiscount > 0 ? totalJobOrderDiscount + totalCampaignDiscount : totalCampaignDiscount;
grandTotalAmount = (totalJobOrderPurchasedAmount - discounts_total) + (totalJobOrderShoppingFee + totalJobOrderDeliveryFee) - job_order.attributes.credit_amount_of_cents;
boolean isLoginPrimary = true;
List<Entity> selected_consignments = AppUtil.getEntities(AppUtil.getListIds(job_order, "consignments"), consignments);
for (Entity consignment : selected_consignments) {
if (consignment.attributes.primary) {
Entity user = AppUtil.getEntity(consignment.relationships.user.data.id, users);
isLoginPrimary = user.relationships.profile.data.type.equals("runner-profiles") && user.id.equals(Preferences.getString(Prefkey.session_user_id)) && isLoginPrimary;
}
}
if (job_order.relationships.completed_by_user.data != null) {
if (job_order.relationships.completed_by_user.data.id.equals(Preferences.getString(Prefkey.session_user_id))) {
if (checkout.attributes.payment_mode.equals("credit-card")) {
if (checkout.attributes.authorized_amount_in_cents < grandTotalAmount) {
grandTotalAmountCc += checkout.attributes.authorized_amount_in_cents - totalJobOrderRecalled;
grandTotalAmountCash += checkout.attributes.paid_cash_difference_in_cents;
} else {
grandTotalAmountCc += grandTotalAmount - totalJobOrderRecalled;
grandTotalAmountCash += 0;
}
} else {
grandTotalAmountCash += grandTotalAmount - totalJobOrderRecalled;
}
}
}
}
}
}
對於每個循環,您都在重置 grandTotalAmount。 通過使其全球化,將解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.