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

@@ -359,11 +359,15 @@ export default defineComponent({
if ([1, 2, 3].includes(this.deliveryOrder.payment_type)) {
this.getPaymentCard(this.deliveryOrder.payment_card_id);
}
this.FinalizeOilOrderForm.cash_recieved = response.data.delivery.cash_recieved;
this.FinalizeOilOrderForm.check_number = response.data.delivery.check_number;
// Properly initialize all form fields with existing delivery data
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.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 {
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...
if (transactionResponse.data && transactionResponse.data.transaction_type === 1 && transactionResponse.data.status === 0) {
// Recalculate the final amount based on the GALLONS DELIVERED from the form
const gallons = this.FinalizeOilOrderForm.gallons_delivered || '0';
const pricePerGallon = parseFloat(this.deliveryOrder.customer_price);
let finalAmount = parseFloat(gallons) * pricePerGallon;
async onSubmit() {
// Step 1: ALWAYS build the payload with the latest form data.
const payload = {
cash_recieved: this.FinalizeOilOrderForm.cash_recieved,
check_number: this.FinalizeOilOrderForm.check_number,
delivery_status: this.FinalizeOilOrderForm.delivery_status,
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) {
finalAmount += parseFloat(this.pricing.price_prime.toString()) || 0;
}
if (this.deliveryOrder.same_day == 1) {
finalAmount += parseFloat(this.pricing.price_same_day.toString()) || 0;
}
// Step 2: ALWAYS update the delivery order with the payload.
// We make this an async call and wait for it to finish.
try {
const path = `${import.meta.env.VITE_BASE_URL}/deliverydata/finalize/${this.deliveryOrder.id}`;
const finalizeResponse = await axios.put(path, payload, { withCredentials: true, headers: authHeader() });
// ...then redirect to the capture page with the correct data.
this.$router.push({
name: 'captureAuthorize',
params: { id: this.$route.params.id },
query: {
gallons: gallons,
amount: finalAmount.toFixed(2).toString()
}
});
return; // IMPORTANT: Stop execution here to prevent finalization.
}
} catch (error: any) { // ✅ FIX: Added ': any' to solve TypeScript error
// This is the expected path if no pre-auth transaction exists.
// We log the error for debugging but continue to the finalization logic below.
console.log("No pre-authorized transaction found. Proceeding with standard finalization.");
if (!finalizeResponse.data.ok) {
// If the update fails, stop everything and show an error.
notify({
title: "Error",
text: finalizeResponse.data.error || "Could not update delivery details.",
type: "error"
});
return; // Stop execution.
}
} catch (error: any) {
notify({
title: "Error",
text: "Failed to update delivery details.",
type: "error"
});
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.
const payload = {
cash_recieved: this.FinalizeOilOrderForm.cash_recieved,
check_number: this.FinalizeOilOrderForm.check_number,
delivery_status: this.FinalizeOilOrderForm.delivery_status,
driver_employee_id: this.FinalizeOilOrderForm.driver,
gallons_delivered: this.FinalizeOilOrderForm.gallons_delivered,
customer_filled: this.FinalizeOilOrderForm.customer_filled,
fill_location: this.FinalizeOilOrderForm.fill_location,
};
this.$router.push({
name: 'captureAuthorize',
params: { id: this.$route.params.id },
query: {
gallons: gallons,
amount: finalAmount.toFixed(2).toString()
}
});
} 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>