fixes to amounts

This commit is contained in:
2025-09-18 14:12:41 -04:00
parent dc71eee4db
commit f7bc23d9ed
4 changed files with 247 additions and 42 deletions

View File

@@ -68,33 +68,39 @@
<!-- Financial Summary Card -->
<div class="bg-neutral rounded-lg p-5">
<h3 class="text-xl font-bold mb-4">Financial Summary</h3>
<div class="space-y-3">
<!-- Pricing & Gallons -->
<div class="grid grid-cols-2 gap-3 text-sm">
<div>
<div class="font-bold">Price / Gallon</div>
<div class="opacity-80">${{ Number(deliveryOrder.customer_price).toFixed(2) }}</div>
</div>
<div>
<div class="font-bold">Gallons Delivered</div>
<div class="opacity-80">{{ gallonsDelivered || '0.00' }}</div>
</div>
<div class="space-y-2">
<div class="flex justify-between">
<span>Gallons Delivered:</span>
<span>{{ gallonsDelivered || 0 }} gallons</span>
</div>
<!-- Fees -->
<div class="text-sm space-y-1 border-t border-base-100 pt-3">
<div v-if="deliveryOrder.prime == 1" class="flex justify-between">
<span>Prime Fee</span>
<span>${{ Number(pricing.price_prime).toFixed(2) }}</span>
</div>
<div v-if="deliveryOrder.same_day === 1" class="flex justify-between">
<span>Same Day Fee</span>
<span>${{ Number(pricing.price_same_day).toFixed(2) }}</span>
</div>
<div class="flex justify-between">
<span>Price per Gallon:</span>
<span>${{ deliveryOrder.customer_price || 0 }}</span>
</div>
<!-- Total -->
<div class="flex justify-between items-center border-t border-base-100 pt-3">
<span class="text-lg font-bold">Total Amount</span>
<span class="text-2xl font-bold text-success">${{ Number(captureAmount).toFixed(2) }}</span>
<div class="flex justify-between font-semibold">
<span>Subtotal:</span>
<span>${{ calculateSubtotal() }}</span>
</div>
<div v-if="deliveryOrder.prime == 1" class="flex justify-between text-sm">
<span>Prime Fee:</span>
<span>+ ${{ pricing.price_prime || 0 }}</span>
</div>
<div v-if="deliveryOrder.same_day == 1" class="flex justify-between text-sm">
<span>Same Day Fee:</span>
<span>+ ${{ pricing.price_same_day || 0 }}</span>
</div>
<div v-if="deliveryOrder.emergency == 1" class="flex justify-between text-sm">
<span>Emergency Fee:</span>
<span>+ ${{ pricing.price_emergency || 0 }}</span>
</div>
<div v-if="promo_active" class="flex justify-between text-success">
<span>{{ promo.name_of_promotion }}:</span>
<span>- ${{ discount }}</span>
</div>
<hr class="my-3">
<div class="flex justify-between font-bold text-lg">
<span>Total:</span>
<span>${{ calculateTotalAmount() }}</span>
</div>
</div>
</div>
@@ -293,6 +299,8 @@ export default defineComponent({
dispatcher_notes: '',
prime: 0,
same_day: 0,
emergency: 0,
promo_id: 0,
payment_type: 0,
payment_card_id: '',
driver_employee_id: 0,
@@ -306,8 +314,19 @@ export default defineComponent({
price_for_employee: 0,
price_same_day: 0,
price_prime: 0,
price_emergency: 0,
date: "",
},
promo_active: false,
promo: {
name_of_promotion: '',
description: '',
money_off_delivery: 0,
text_on_ticket: ''
},
total_amount: 0,
discount: 0,
total_amount_after_discount: 0,
}
},
@@ -318,6 +337,48 @@ export default defineComponent({
},
methods: {
sumdelivery(delivery_id: any) {
let path = import.meta.env.VITE_BASE_URL + "/delivery/total/" + delivery_id;
axios({
method: "get",
url: path,
withCredentials: true,
})
.then((response: any) => {
if (response.data.ok) {
this.total_amount = parseFloat(response.data.total_amount) || 0;
this.discount = parseFloat(response.data.discount) || 0;
this.total_amount_after_discount = parseFloat(response.data.total_amount_after_discount) || 0;
// Set capture amount to the calculated total including fees and discount
this.captureAmount = this.calculateTotalAsNumber();
}
})
.catch(() => {
notify({
title: "Error",
text: "Could not get totals",
type: "error",
});
});
},
getPromo(promo_id: any) {
let path = import.meta.env.VITE_BASE_URL + "/promo/" + promo_id;
axios({
method: "get",
url: path,
withCredentials: true,
headers: authHeader(),
})
.then((response: any) => {
if (response.data) {
this.promo = response.data
this.promo_active = true
}
})
},
getOilOrder(delivery_id: any) {
const path = `${import.meta.env.VITE_BASE_URL}/delivery/order/${delivery_id}`;
axios.get(path, { withCredentials: true, headers: authHeader() })
@@ -326,13 +387,16 @@ export default defineComponent({
this.deliveryOrder = response.data.delivery;
this.gallonsDelivered = this.deliveryOrder.gallons_delivered;
this.getCustomer(this.deliveryOrder.customer_id);
this.sumdelivery(delivery_id);
if ([1, 2, 3].includes(this.deliveryOrder.payment_type)) {
this.getPaymentCard(this.deliveryOrder.payment_card_id);
}
// Calculate capture amount if pricing is already loaded
this.calculateCaptureAmount();
if (this.deliveryOrder.promo_id != null) {
this.getPromo(this.deliveryOrder.promo_id);
this.promo_active = true;
}
} else {
console.error("API Error:", response.data.error || "Failed to fetch delivery data.");
}
@@ -464,6 +528,58 @@ export default defineComponent({
}
},
calculateSubtotal() {
const gallons = parseFloat(this.gallonsDelivered || '0') || 0;
const pricePerGallon = typeof this.deliveryOrder.customer_price === 'string' ? parseFloat(this.deliveryOrder.customer_price) : Number(this.deliveryOrder.customer_price) || 0;
return (gallons * pricePerGallon).toFixed(2);
},
calculateTotalAmount() {
if (this.total_amount_after_discount == null || this.total_amount_after_discount === undefined) {
return '0.00';
}
let totalNum = Number(this.total_amount_after_discount);
if (isNaN(totalNum)) {
return '0.00';
}
if (this.deliveryOrder && this.deliveryOrder.prime == 1 && this.pricing && this.pricing.price_prime) {
totalNum += Number(this.pricing.price_prime) || 0;
}
if (this.deliveryOrder && this.deliveryOrder.same_day == 1 && this.pricing && this.pricing.price_same_day) {
totalNum += Number(this.pricing.price_same_day) || 0;
}
if (this.deliveryOrder && this.deliveryOrder.emergency == 1 && this.pricing && this.pricing.price_emergency) {
totalNum += Number(this.pricing.price_emergency) || 0;
}
return totalNum.toFixed(2);
},
calculateTotalAsNumber() {
if (this.total_amount_after_discount == null || this.total_amount_after_discount === undefined) {
return 0;
}
let totalNum = Number(this.total_amount_after_discount);
if (isNaN(totalNum)) {
return 0;
}
if (this.deliveryOrder && this.deliveryOrder.prime == 1 && this.pricing && this.pricing.price_prime) {
totalNum += Number(this.pricing.price_prime) || 0;
}
if (this.deliveryOrder && this.deliveryOrder.same_day == 1 && this.pricing && this.pricing.price_same_day) {
totalNum += Number(this.pricing.price_same_day) || 0;
}
if (this.deliveryOrder && this.deliveryOrder.emergency == 1 && this.pricing && this.pricing.price_emergency) {
totalNum += Number(this.pricing.price_emergency) || 0;
}
return totalNum;
},
calculateCaptureAmount() {
// Only calculate if we have both delivery order and pricing data
if (this.deliveryOrder.id && this.pricing.price_for_customer) {