feat: print receipt
This commit is contained in:
@@ -12,6 +12,8 @@ export default function usePlaceOrder({
|
||||
const isPending = ref(false);
|
||||
const paidPaymentStatus = ref();
|
||||
const invoice = ref();
|
||||
const invoice_details = ref();
|
||||
const payment_record = ref();
|
||||
const showVietQRModal = ref();
|
||||
|
||||
/**
|
||||
@@ -41,12 +43,13 @@ export default function usePlaceOrder({
|
||||
delivery_method: orderInfo.value.deliveryMethod.id,
|
||||
};
|
||||
|
||||
const invoice = await $insertapi("Invoice", {
|
||||
const invoiceRecord = await $insertapi("Invoice", {
|
||||
data: invoicePayload,
|
||||
notify: false,
|
||||
});
|
||||
|
||||
return invoice;
|
||||
invoice.value = invoiceRecord;
|
||||
return invoiceRecord;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
$snackbar("Tạo đơn hàng không thành công", "Error");
|
||||
@@ -55,6 +58,27 @@ export default function usePlaceOrder({
|
||||
}
|
||||
}
|
||||
|
||||
async function printReceipt() {
|
||||
return await $fetch("/api/print/receipt", {
|
||||
method: "POST",
|
||||
body: {
|
||||
invoice: invoice.value,
|
||||
invoice_details: invoice_details.value,
|
||||
payment_record: payment_record.value,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints receipt, displays snackbar, resets cart, closes modal
|
||||
*/
|
||||
async function onOrderSuccess() {
|
||||
$snackbar("Khởi tạo đơn hàng thành công", "Success");
|
||||
printReceipt();
|
||||
resetCart();
|
||||
onSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark sold IMEIs, create invoice details, delivery info, payment records, reset cart.
|
||||
* Runs when order payment is confirmed.
|
||||
@@ -67,7 +91,7 @@ export default function usePlaceOrder({
|
||||
}
|
||||
|
||||
const imeisSold = await markImeisSold();
|
||||
await createInvoiceDetails(invoiceId, imeisSold);
|
||||
invoice_details.value = await createInvoiceDetails(invoiceId, imeisSold);
|
||||
await createPaymentRecord(invoiceId);
|
||||
|
||||
if (orderInfo.value.deliveryMethod.code === "HOME_DELIVERY") {
|
||||
@@ -76,9 +100,7 @@ export default function usePlaceOrder({
|
||||
|
||||
// update invoice.payment_status from PENDING to PAID
|
||||
await $patchapi("Invoice", { id: invoiceId, payment_status: paidPaymentStatus.value.id });
|
||||
$snackbar("Khởi tạo đơn hàng thành công", "Success");
|
||||
resetCart();
|
||||
onSuccess();
|
||||
await onOrderSuccess();
|
||||
}
|
||||
|
||||
async function markImeisSold() {
|
||||
@@ -110,6 +132,8 @@ export default function usePlaceOrder({
|
||||
const invoiceDetails = await $insertapi("Invoice_Detail", {
|
||||
data: invoiceDetailPayload,
|
||||
notify: false,
|
||||
values:
|
||||
"id,code,invoice,variant,variant__code,variant__product,variant__product__name,variant__color,variant__color__name,variant__ram__code,variant__ram__capacity,variant__internal_storage__code,variant__internal_storage__capacity,variant__price,variant__image__path,imei_sold,price,status,note,deleted,create_time,update_time",
|
||||
});
|
||||
|
||||
return invoiceDetails;
|
||||
@@ -150,6 +174,7 @@ export default function usePlaceOrder({
|
||||
notify: false,
|
||||
});
|
||||
|
||||
payment_record.value = paymentRecord;
|
||||
return paymentRecord;
|
||||
}
|
||||
|
||||
@@ -168,7 +193,7 @@ export default function usePlaceOrder({
|
||||
}
|
||||
|
||||
async function placeOrder() {
|
||||
invoice.value = await createOrder();
|
||||
await createOrder();
|
||||
|
||||
if (orderInfo.value.paymentMethod.code === "CASH") {
|
||||
finalizeOrder(invoice.value.id);
|
||||
|
||||
Reference in New Issue
Block a user