61 lines
2.1 KiB
JavaScript
61 lines
2.1 KiB
JavaScript
import useActiveCart from "~/components/pos/composables/useActiveCart";
|
|
|
|
export default function useOrderInfo() {
|
|
const posStore = usePosStore();
|
|
const { activeCartId, addresses, orderInfo } = storeToRefs(posStore);
|
|
const { activeCart, activeCartItems, subtotal } = useActiveCart();
|
|
|
|
const total = computed(() => subtotal.value + orderInfo.value.shipping_fee);
|
|
|
|
watch(activeCartId, () => {
|
|
// reset fields when tab is switched
|
|
orderInfo.value.deliveryMethod = null;
|
|
orderInfo.value.paymentMethod = null;
|
|
});
|
|
|
|
watch(activeCart, async (newVal, oldVal) => {
|
|
// set order info
|
|
if (newVal?.customer) {
|
|
await posStore.getAddresses(newVal.customer);
|
|
if (!oldVal || !oldVal.customer || oldVal.customer !== newVal.customer) {
|
|
const defaultAddress = addresses.value.find((add) => add.is_default);
|
|
orderInfo.value.address = defaultAddress;
|
|
orderInfo.value.receiver_name = newVal.customer__fullname;
|
|
orderInfo.value.receiver_phone = newVal.customer__phone;
|
|
}
|
|
} else {
|
|
addresses.value = null;
|
|
orderInfo.value.address = null;
|
|
orderInfo.value.receiver_name = null;
|
|
orderInfo.value.receiver_phone = null;
|
|
}
|
|
});
|
|
|
|
watch(
|
|
() => orderInfo.value.deliveryMethod,
|
|
(newVal) => {
|
|
if (newVal?.code === "HOME_DELIVERY") {
|
|
const defaultAddress = addresses.value.find((add) => add.is_default);
|
|
orderInfo.value.address = defaultAddress;
|
|
}
|
|
},
|
|
);
|
|
|
|
const isOrderValid = computed(() => {
|
|
if (activeCartItems.value?.length === 0) return false;
|
|
if (!activeCart.value?.customer) return false;
|
|
if (!orderInfo.value.deliveryMethod) return false;
|
|
if (!orderInfo.value.paymentMethod) return false;
|
|
if (orderInfo.value.deliveryMethod.code === "HOME_DELIVERY" && !orderInfo.value.address) return false;
|
|
|
|
return true;
|
|
});
|
|
|
|
const fullAddress = computed(() => {
|
|
if (!orderInfo.value.address) return "";
|
|
return `${orderInfo.value.address.address_detail}, ${orderInfo.value.address.ward}, ${orderInfo.value.address.district}, ${orderInfo.value.address.city}`;
|
|
});
|
|
|
|
return { isOrderValid, fullAddress, total };
|
|
}
|