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

82
app/stores/usePosStore.js Normal file
View File

@@ -0,0 +1,82 @@
import { defineStore } from "pinia";
export const usePosStore = defineStore("pos", () => {
const { $findapi, $getapi, $getdata, $patchapi } = useNuxtApp();
const carts = ref([]);
const cartItems = ref([]);
const customers = ref([]);
const isPending = ref(false);
const activeCartId = ref();
const invalidCartItems = ref([]);
const isChangingCus = ref(false);
const addresses = ref([]);
const orderInfo = ref({
address: null,
receiver_name: null,
receiver_phone: null,
shipping_fee: null,
deliveryMethod: null,
paymentMethod: null,
});
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);
async function changeCustomer(cusId) {
isChangingCus.value = true;
const updatedCart = await $patchapi("Cart", {
id: activeCartId.value,
customer: cusId,
});
await getCarts();
isChangingCus.value = false;
}
async function getAddresses(activeCartCustomerId) {
addresses.value =
(await $getdata("Customer_Address", {
filter: { customer: activeCartCustomerId },
})) || [];
}
watch(
carts,
(newVal) => {
if (!activeCartId.value && newVal.length) {
activeCartId.value = newVal[0].id;
}
},
{ immediate: true },
);
watch(activeCartId, () => {
invalidCartItems.value = [];
});
return {
carts,
cartItems,
customers,
isPending,
getCarts,
getAddresses,
changeCustomer,
activeCartId,
invalidCartItems,
isChangingCus,
addresses,
orderInfo,
};
});