This commit is contained in:
Viet An
2026-06-23 14:06:29 +07:00
parent e9fcfbe610
commit 059f232283
10 changed files with 441 additions and 118 deletions

View File

@@ -1,117 +1,17 @@
<script setup>
import CartItem from "~/components/pos/CartItem.vue";
import usePlaceOrder from "~/components/pos/composables/usePlaceOrder";
const id = "confirmOrderModal";
const emit = defineEmits(["close"]);
const { $id, $patchapi, $deleteapi, $insertapi, $dayjs, $snackbar } = useNuxtApp();
const id = "confirmOrder";
const isPending = ref(false);
const { activeCart, activeCartItems, orderInfo, getCarts } = inject("pos");
const subtotal = computed(() => {
return activeCartItems.value?.reduce((prev, curr) => prev + curr.imei__variant__price, 0);
const { subtotal, shipping_address, isPending, showVietQRModal, placeOrder, testReturnPage } = usePlaceOrder({
activeCart,
activeCartItems,
orderInfo,
getCarts,
onSuccess: () => emit("close"),
});
const shipping_address = computed(() => {
return `${orderInfo.value.address.address_detail}, ${orderInfo.value.address.ward}, ${orderInfo.value.address.district}, ${orderInfo.value.address.city}`;
});
async function createOrder() {
try {
isPending.value = true;
const invoicePayload = {
customer: activeCart.value.customer,
customer_name: activeCart.value.customer__fullname,
customer_phone: activeCart.value.customer__phone,
customer_email: activeCart.value.customer__email,
shipping_address: shipping_address.value,
product_amount: activeCartItems.value.length,
shipping_fee: 0,
total_amount: subtotal.value,
discount_amount: 0,
final_amount: subtotal.value,
order_type: "pos",
status: "pending",
payment_status: 1,
staff: 1,
ordered_at: new Date(),
paid_at: null,
delivery_method: orderInfo.value.deliveryMethod.id,
};
const invoice = await $insertapi("Invoice", {
data: invoicePayload,
notify: false,
});
const imeisSoldPayload = activeCartItems.value.map((cartItem) => ({
imei: cartItem.imei__imei,
variant: cartItem.imei__variant,
sold_date: $dayjs().format("YYYY-MM-DD"),
}));
const imeisSold = await $insertapi("IMEI_Sold", {
data: imeisSoldPayload,
notify: false,
});
const invoiceDetailPayload = activeCartItems.value.map((cartItem) => {
const imeiSold = imeisSold.find((imeiSold) => imeiSold.imei === cartItem.imei__imei);
return {
invoice: invoice.id,
variant: cartItem.imei__variant,
imei_sold: imeiSold.id,
price: cartItem.imei__variant__price,
};
});
const invoiceDetails = await $insertapi("Invoice_Detail", {
data: invoiceDetailPayload,
notify: false,
});
if (orderInfo.value.deliveryMethod.code === "HOME_DELIVERY") {
const deliveryInfoPayload = {
invoice: invoice.id,
receiver_name: orderInfo.value.receiver_name,
receiver_phone: orderInfo.value.receiver_phone,
city: orderInfo.value.address.city,
district: orderInfo.value.address.district,
ward: orderInfo.value.address.ward,
address: orderInfo.value.address.address_detail,
shipping_fee: 0,
carrier: orderInfo.value.deliveryMethod.id,
tracking_code: $id(),
status: "pending",
staff: 1,
};
const deliveryInfo = await $insertapi("Delivery_Info", {
data: deliveryInfoPayload,
notify: false,
});
}
$snackbar("Tạo đơn hàng thành công", "Success");
await Promise.all([
$deleteapi(
"Cart_Item",
activeCartItems.value.map((c) => c.id),
),
$patchapi("Cart", {
id: activeCartItems.value[0].cart,
customer: null,
}),
]);
getCarts();
emit("close");
} catch (error) {
console.error(error);
$snackbar("Tạo đơn hàng không thành công", "Error");
} finally {
isPending.value = false;
}
}
</script>
<template>
@@ -231,7 +131,7 @@ async function createOrder() {
Huỷ
</button>
<button
@click="createOrder"
@click="placeOrder"
:class="['button is-success', isPending && 'is-loading']"
>
<span class="icon">
@@ -242,8 +142,19 @@ async function createOrder() {
</span>
<span>Đặt hàng</span>
</button>
<!-- <button
@click="testReturnPage"
class="button is-yellow"
>
Test Return Page
</button> -->
</div>
</Teleport>
<Modal
v-if="showVietQRModal"
v-bind="showVietQRModal"
@close="showVietQRModal = null"
/>
</div>
</template>