This commit is contained in:
2025-09-07 18:29:42 -04:00
parent ea52f7ba62
commit fd11c9e794
4 changed files with 108 additions and 72 deletions

View File

@@ -124,18 +124,15 @@
<div> <div>
<label class="label"><span class="label-text font-bold">Expected Delivery Date</span></label> <label class="label"><span class="label-text font-bold">Expected Delivery Date</span></label>
<input v-model="formDelivery.expected_delivery_date" class="input input-bordered input-sm w-full max-w-xs" type="date" /> <input v-model="formDelivery.expected_delivery_date" class="input input-bordered input-sm w-full max-w-xs" type="date" />
<div class="flex flex-wrap gap-2 mt-2">
<button @click.prevent="setDeliveryDate(0)" class="btn btn-xs btn-outline">Today</button>
<button @click.prevent="setDeliveryDate(1)" class="btn btn-xs btn-outline">Tomorrow</button>
<button @click.prevent="setDeliveryDate(2)" class="btn btn-xs btn-outline">In 2 Days</button>
<button @click.prevent="setDeliveryDate(3)" class="btn btn-xs btn-outline">In 3 Days</button>
</div>
<span v-if="v$.formDelivery.expected_delivery_date.$error" class="text-red-500 text-xs mt-1">Date is required.</span> <span v-if="v$.formDelivery.expected_delivery_date.$error" class="text-red-500 text-xs mt-1">Date is required.</span>
</div> </div>
<div>
<label class="label"><span class="label-text font-bold">Assigned Driver</span></label>
<select class="select select-bordered select-sm w-full max-w-xs" v-model="formDelivery.driver_employee_id">
<option disabled value="">Select a driver</option>
<option v-for="driver in truckDriversList" :key="driver.id" :value="driver.id">
{{ driver.employee_first_name }} {{ driver.employee_last_name }}
</option>
</select>
<span v-if="v$.formDelivery.driver_employee_id.$error" class="text-red-500 text-xs mt-1">Driver is required.</span>
</div>
<div> <div>
<label class="label"><span class="label-text font-bold">Apply Promotion</span></label> <label class="label"><span class="label-text font-bold">Apply Promotion</span></label>
<select class="select select-bordered select-sm w-full max-w-xs" v-model="formDelivery.promo_id"> <select class="select select-bordered select-sm w-full max-w-xs" v-model="formDelivery.promo_id">
@@ -358,7 +355,6 @@ interface DeliveryFormData {
other: boolean; other: boolean;
credit_card_id: number; credit_card_id: number;
promo_id: number; promo_id: number;
driver_employee_id: string | number;
} }
interface CardFormData { interface CardFormData {
card_name: string; card_name: string;
@@ -398,7 +394,6 @@ export default defineComponent({
other: false, other: false,
credit_card_id: 0, credit_card_id: 0,
promo_id: 0, promo_id: 0,
driver_employee_id: '',
} as DeliveryFormData, } as DeliveryFormData,
formCard: { formCard: {
card_name: '', card_name: '',
@@ -428,7 +423,6 @@ export default defineComponent({
} }
}, },
expected_delivery_date: { required }, expected_delivery_date: { required },
driver_employee_id: { required },
credit_card_id: { credit_card_id: {
// *** THIS IS THE FIX for both runtime and TypeScript *** // *** THIS IS THE FIX for both runtime and TypeScript ***
// Adding `this: any` tells TypeScript what the context of `this` will be. // Adding `this: any` tells TypeScript what the context of `this` will be.
@@ -494,6 +488,11 @@ export default defineComponent({
this.formDelivery.gallons_ordered = String(amount); this.formDelivery.gallons_ordered = String(amount);
this.formDelivery.customer_asked_for_fill = false; this.formDelivery.customer_asked_for_fill = false;
}, },
setDeliveryDate(days: number) {
const date = new Date();
date.setDate(date.getDate() + days);
this.formDelivery.expected_delivery_date = date.toISOString().split('T')[0];
},
getPricingTiers() { getPricingTiers() {
let path = import.meta.env.VITE_BASE_URL + "/info/price/oil/tiers"; let path = import.meta.env.VITE_BASE_URL + "/info/price/oil/tiers";
axios({ method: "get", url: path, withCredentials: true, headers: authHeader() }) axios({ method: "get", url: path, withCredentials: true, headers: authHeader() })
@@ -593,7 +592,6 @@ export default defineComponent({
other: this.formDelivery.other, other: this.formDelivery.other,
credit_card_id: this.formDelivery.credit_card_id, credit_card_id: this.formDelivery.credit_card_id,
promo_id: this.formDelivery.promo_id, promo_id: this.formDelivery.promo_id,
driver_employee_id: this.formDelivery.driver_employee_id,
}; };
let path = `${import.meta.env.VITE_BASE_URL}/delivery/create/${this.customer.id}`; let path = `${import.meta.env.VITE_BASE_URL}/delivery/create/${this.customer.id}`;
@@ -656,4 +654,4 @@ export default defineComponent({
}, },
}, },
}) })
</script> </script>

View File

@@ -123,7 +123,7 @@
</option> </option>
</select> </select>
</div> </div>
<div> <!-- <div>
<label class="label"><span class="label-text font-bold">Assigned Driver</span></label> <label class="label"><span class="label-text font-bold">Assigned Driver</span></label>
<select v-model="CreateOilOrderForm.basicInfo.driver_employee_id" class="select select-bordered select-sm w-full"> <select v-model="CreateOilOrderForm.basicInfo.driver_employee_id" class="select select-bordered select-sm w-full">
<option disabled :value="0">Select a driver</option> <option disabled :value="0">Select a driver</option>
@@ -131,7 +131,7 @@
{{ driver.employee_first_name }} {{ driver.employee_last_name }} {{ driver.employee_first_name }} {{ driver.employee_last_name }}
</option> </option>
</select> </select>
</div> </div> -->
</div> </div>
<!-- Dispatcher Notes --> <!-- Dispatcher Notes -->

View File

@@ -359,11 +359,15 @@ export default defineComponent({
if ([1, 2, 3].includes(this.deliveryOrder.payment_type)) { if ([1, 2, 3].includes(this.deliveryOrder.payment_type)) {
this.getPaymentCard(this.deliveryOrder.payment_card_id); this.getPaymentCard(this.deliveryOrder.payment_card_id);
} }
this.FinalizeOilOrderForm.cash_recieved = response.data.delivery.cash_recieved; // Properly initialize all form fields with existing delivery data
this.FinalizeOilOrderForm.check_number = response.data.delivery.check_number; this.FinalizeOilOrderForm.cash_recieved = response.data.delivery.cash_recieved || '';
this.FinalizeOilOrderForm.check_number = response.data.delivery.check_number || '';
this.FinalizeOilOrderForm.credit_card_id = response.data.delivery.payment_card_id; this.FinalizeOilOrderForm.credit_card_id = response.data.delivery.payment_card_id;
this.FinalizeOilOrderForm.customer_filled = response.data.delivery.customer_filled == 1; this.FinalizeOilOrderForm.customer_filled = response.data.delivery.customer_filled == 1;
this.FinalizeOilOrderForm.gallons_delivered = response.data.delivery.gallons_delivered || '';
this.FinalizeOilOrderForm.driver = response.data.delivery.driver_employee_id || 0;
this.FinalizeOilOrderForm.delivery_status = 10; // Finalization status - hardcoded for finalizing action
} else { } else {
console.error("API Error:", response.data.error || "Failed to fetch delivery data."); console.error("API Error:", response.data.error || "Failed to fetch delivery data.");
} }
@@ -485,62 +489,96 @@ export default defineComponent({
}); });
}, },
async onSubmit() {
// First, check if there's a pre-authorized transaction for this delivery.
try {
// This is the CORRECT URL for the backend endpoint.
const correctedUrl = `${import.meta.env.VITE_AUTHORIZE_URL}/api/transaction/delivery/${this.$route.params.id}`;
const transactionResponse = await axios.get(correctedUrl, {
withCredentials: true,
headers: authHeader(),
});
// If a valid, approved, pre-auth transaction is found... async onSubmit() {
if (transactionResponse.data && transactionResponse.data.transaction_type === 1 && transactionResponse.data.status === 0) { // Step 1: ALWAYS build the payload with the latest form data.
const payload = {
// Recalculate the final amount based on the GALLONS DELIVERED from the form cash_recieved: this.FinalizeOilOrderForm.cash_recieved,
const gallons = this.FinalizeOilOrderForm.gallons_delivered || '0'; check_number: this.FinalizeOilOrderForm.check_number,
const pricePerGallon = parseFloat(this.deliveryOrder.customer_price); delivery_status: this.FinalizeOilOrderForm.delivery_status,
let finalAmount = parseFloat(gallons) * pricePerGallon; driver_employee_id: this.FinalizeOilOrderForm.driver,
gallons_delivered: this.FinalizeOilOrderForm.gallons_delivered,
customer_filled: this.FinalizeOilOrderForm.customer_filled,
fill_location: this.FinalizeOilOrderForm.fill_location,
};
if (this.deliveryOrder.prime == 1) { // Step 2: ALWAYS update the delivery order with the payload.
finalAmount += parseFloat(this.pricing.price_prime.toString()) || 0; // We make this an async call and wait for it to finish.
} try {
if (this.deliveryOrder.same_day == 1) { const path = `${import.meta.env.VITE_BASE_URL}/deliverydata/finalize/${this.deliveryOrder.id}`;
finalAmount += parseFloat(this.pricing.price_same_day.toString()) || 0; const finalizeResponse = await axios.put(path, payload, { withCredentials: true, headers: authHeader() });
}
// ...then redirect to the capture page with the correct data. if (!finalizeResponse.data.ok) {
this.$router.push({ // If the update fails, stop everything and show an error.
name: 'captureAuthorize', notify({
params: { id: this.$route.params.id }, title: "Error",
query: { text: finalizeResponse.data.error || "Could not update delivery details.",
gallons: gallons, type: "error"
amount: finalAmount.toFixed(2).toString() });
} return; // Stop execution.
}); }
return; // IMPORTANT: Stop execution here to prevent finalization. } catch (error: any) {
} notify({
} catch (error: any) { // ✅ FIX: Added ': any' to solve TypeScript error title: "Error",
// This is the expected path if no pre-auth transaction exists. text: "Failed to update delivery details.",
// We log the error for debugging but continue to the finalization logic below. type: "error"
console.log("No pre-authorized transaction found. Proceeding with standard finalization."); });
console.error("FinalizeOrder error:", error);
return; // Stop execution.
}
// Step 3: NOW, check if there's a pre-authorized transaction.
try {
const transactionUrl = `${import.meta.env.VITE_AUTHORIZE_URL}/api/transaction/delivery/${this.$route.params.id}`;
const transactionResponse = await axios.get(transactionUrl, {
withCredentials: true,
headers: authHeader(),
});
// If a valid, approved, pre-auth transaction is found...
if (transactionResponse.data && transactionResponse.data.transaction_type === 1 && transactionResponse.data.status === 0) {
// ...redirect to the capture page. The delivery is already updated.
const gallons = this.FinalizeOilOrderForm.gallons_delivered || '0';
const pricePerGallon = parseFloat(this.deliveryOrder.customer_price);
let finalAmount = parseFloat(gallons) * pricePerGallon;
if (this.deliveryOrder.prime == 1) {
finalAmount += parseFloat(this.pricing.price_prime.toString()) || 0;
}
if (this.deliveryOrder.same_day == 1) {
finalAmount += parseFloat(this.pricing.price_same_day.toString()) || 0;
} }
// If no pre-auth transaction was found, proceed with the normal finalization flow. this.$router.push({
const payload = { name: 'captureAuthorize',
cash_recieved: this.FinalizeOilOrderForm.cash_recieved, params: { id: this.$route.params.id },
check_number: this.FinalizeOilOrderForm.check_number, query: {
delivery_status: this.FinalizeOilOrderForm.delivery_status, gallons: gallons,
driver_employee_id: this.FinalizeOilOrderForm.driver, amount: finalAmount.toFixed(2).toString()
gallons_delivered: this.FinalizeOilOrderForm.gallons_delivered, }
customer_filled: this.FinalizeOilOrderForm.customer_filled, });
fill_location: this.FinalizeOilOrderForm.fill_location,
}; } else {
// Step 4a: If no pre-auth transaction, finalize the financial part and redirect.
notify({ title: "Success", text: "Ticket details have been updated.", type: "success" });
this.CreateTransaction();
this.$router.push({ name: "deliveryOrder", params: { id: this.deliveryOrder.id } });
}
} catch (error: any) {
// Step 4b: If checking for pre-auth fails, but the update succeeded,
// finalize the financial part and redirect.
console.log("No pre-authorized transaction found. Proceeding with standard finalization.");
notify({ title: "Success", text: "Ticket details have been updated.", type: "success" });
this.CreateTransaction();
this.$router.push({ name: "deliveryOrder", params: { id: this.deliveryOrder.id } });
}
},
this.FinalizeOrder(payload);
},
}, },
}); });
</script> </script>

View File

@@ -84,10 +84,10 @@
<span v-else-if="deliveryOrder.delivery_status == 10">Finalized</span> <span v-else-if="deliveryOrder.delivery_status == 10">Finalized</span>
</div> </div>
</div> </div>
<div> <!-- <div>
<div class="font-bold text-sm">Assigned Driver</div> <div class="font-bold text-sm">Assigned Driver</div>
<div>{{ deliveryOrder.driver_first_name }} {{ deliveryOrder.driver_last_name }}</div> <div>{{ deliveryOrder.driver_first_name }} {{ deliveryOrder.driver_last_name }}</div>
</div> </div> -->
<div> <div>
<div class="font-bold text-sm">Scheduled Date</div> <div class="font-bold text-sm">Scheduled Date</div>
<div>{{ deliveryOrder.expected_delivery_date }}</div> <div>{{ deliveryOrder.expected_delivery_date }}</div>