This commit is contained in:
Viet An
2026-06-30 15:12:04 +07:00
parent 8b3da71895
commit e9e4fbe24f
18 changed files with 149 additions and 158 deletions

View File

@@ -1,21 +1,12 @@
import { without } from "es-toolkit";
export default function useActiveCart({ carts, cartItems, customers }) {
const activeCartId = ref();
export default function useActiveCart() {
const posStore = usePosStore();
const { carts, cartItems, customers, activeCartId } = storeToRefs(posStore);
const activeCart = computed(() => carts.value.find((c) => c.id === activeCartId.value));
const activeCartItems = computed(() => cartItems.value.filter((ci) => ci.cart === activeCartId.value));
watch(
carts,
(newVal) => {
if (!activeCartId.value && newVal.length) {
activeCartId.value = newVal[0].id;
}
},
{ immediate: true },
);
const customerIdsWithNoCart = computed(() => {
const cusIds = customers.value.map((c) => c.id);
const cusIdsWithCart = carts.value.filter((c) => c.customer).map((c) => c.customer);
@@ -27,10 +18,5 @@ export default function useActiveCart({ carts, cartItems, customers }) {
return activeCartItems.value?.reduce((prev, curr) => prev + curr.imei__variant__price, 0);
});
const invalidCartItems = ref([]);
watch(activeCartId, () => {
invalidCartItems.value = [];
});
return { activeCartId, activeCart, activeCartItems, customerIdsWithNoCart, subtotal, invalidCartItems };
return { activeCart, activeCartItems, customerIdsWithNoCart, subtotal };
}

View File

@@ -1,22 +0,0 @@
export default function useCartData() {
const { $findapi, $getapi } = useNuxtApp();
const carts = ref([]);
const cartItems = ref([]);
const customers = ref([]);
const isPending = ref(false);
async function getCarts() {
isPending.value = true;
const apis = $findapi(["Cart", "Cart_Item", "customer"]);
const [cartsRes, cartItemsRes, customersRes] = await $getapi(apis);
isPending.value = false;
carts.value = cartsRes.data.rows || [];
cartItems.value = cartItemsRes.data.rows || [];
customers.value = customersRes.data.rows || [];
}
onMounted(getCarts);
return { carts, cartItems, customers, getCarts, isPending };
}

View File

@@ -1,24 +0,0 @@
export default function useCustomer({ activeCart, getCarts }) {
const { $getdata, $patchapi } = useNuxtApp();
const addresses = ref([]);
async function getAddresses() {
addresses.value =
(await $getdata("Customer_Address", {
filter: { customer: activeCart.value.customer },
})) || [];
}
const isChangingCus = ref(false);
async function changeCustomer(cusId) {
isChangingCus.value = true;
const updatedCart = await $patchapi("Cart", {
id: activeCart.value.id,
customer: cusId,
});
await getCarts();
isChangingCus.value = false;
}
return { addresses, getAddresses, isChangingCus, changeCustomer };
}

View File

@@ -1,12 +1,9 @@
export default function useOrderInfo({ activeCartId, activeCart, activeCartItems, addresses, getAddresses, subtotal }) {
const orderInfo = ref({
address: null,
receiver_name: null,
receiver_phone: null,
shipping_fee: null,
deliveryMethod: null,
paymentMethod: null,
});
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);
@@ -19,7 +16,7 @@ export default function useOrderInfo({ activeCartId, activeCart, activeCartItems
watch(activeCart, async (newVal, oldVal) => {
// set order info
if (newVal?.customer) {
await getAddresses();
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;
@@ -59,5 +56,5 @@ export default function useOrderInfo({ activeCartId, activeCart, activeCartItems
return `${orderInfo.value.address.address_detail}, ${orderInfo.value.address.ward}, ${orderInfo.value.address.district}, ${orderInfo.value.address.city}`;
});
return { orderInfo, isOrderValid, fullAddress, total };
return { isOrderValid, fullAddress, total };
}

View File

@@ -1,15 +1,14 @@
export default function usePlaceOrder({
activeCart,
activeCartItems,
invalidCartItems,
orderInfo,
fullAddress,
subtotal,
total,
getCarts,
onSuccess,
}) {
import useActiveCart from "~/components/pos/composables/useActiveCart";
import useOrderInfo from "~/components/pos/composables/useOrderInfo";
export default function usePlaceOrder({ onSuccess }) {
const { $dayjs, $id, $getdata, $insertapi, $patchapi, $deleteapi, $snackbar } = useNuxtApp();
const posStore = usePosStore();
const { invalidCartItems, orderInfo } = storeToRefs(posStore);
const { activeCart, activeCartItems, subtotal } = useActiveCart();
const { fullAddress, total } = useOrderInfo();
const isPending = ref(false);
const paidPaymentStatus = ref();
const invoice = ref();
@@ -206,7 +205,7 @@ export default function usePlaceOrder({
customer: null,
}),
]);
getCarts();
posStore.getCarts();
}
async function placeOrder() {