changes
This commit is contained in:
@@ -5,7 +5,7 @@ const props = defineProps({
|
||||
invalid: Boolean,
|
||||
});
|
||||
const { $deleteapi, $formatNum, $snackbar } = useNuxtApp();
|
||||
const { getCarts } = inject("pos");
|
||||
const posStore = usePosStore();
|
||||
const showConfirmModal = ref();
|
||||
const isDeleting = ref(false);
|
||||
|
||||
@@ -26,7 +26,7 @@ async function removeFromCart() {
|
||||
await $deleteapi("Cart_Item", props.cartItem.id);
|
||||
isDeleting.value = false;
|
||||
$snackbar("Đã xoá sản phẩm khỏi giỏ hàng", "Success");
|
||||
getCarts();
|
||||
posStore.getCarts();
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -69,7 +69,7 @@ async function removeFromCart() {
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
v-if="deleteable"
|
||||
v-if="deleteable || invalid"
|
||||
@click="openConfirmModal"
|
||||
:class="['button is-danger is-light', isDeleting && 'is-loading']"
|
||||
>
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
<script setup>
|
||||
const { carts, activeCartId, activeCartItems, isChangingCus, getCarts } = inject("pos");
|
||||
import useActiveCart from "~/components/pos/composables/useActiveCart";
|
||||
|
||||
const posStore = usePosStore();
|
||||
const { carts, activeCartId, isChangingCus } = storeToRefs(posStore);
|
||||
const { activeCartItems } = useActiveCart();
|
||||
|
||||
const { $insertapi, $deleteapi } = useNuxtApp();
|
||||
const isAddingCart = ref(false);
|
||||
@@ -9,7 +13,7 @@ async function addCart() {
|
||||
const newCart = await $insertapi("Cart", { notify: false });
|
||||
activeCartId.value = newCart.id;
|
||||
isAddingCart.value = false;
|
||||
getCarts();
|
||||
posStore.getCarts();
|
||||
}
|
||||
|
||||
const isDeletingCart = ref();
|
||||
@@ -36,7 +40,7 @@ async function removeCart(cartId) {
|
||||
}
|
||||
}
|
||||
}
|
||||
getCarts();
|
||||
posStore.getCarts();
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
import AddIMEIForm from "~/components/imports/AddIMEIForm.vue";
|
||||
import ImportData from "~/components/parameter/ImportData.vue";
|
||||
import { remove } from "es-toolkit";
|
||||
import useActiveCart from "~/components/pos/composables/useActiveCart";
|
||||
|
||||
const props = defineProps({
|
||||
variant: Object,
|
||||
});
|
||||
const store = useStore();
|
||||
const { $getdata, $insertapi, $snackbar } = useNuxtApp();
|
||||
const emit = defineEmits(["close"]);
|
||||
|
||||
@@ -22,7 +22,8 @@ function toggleSelected(imeiRec) {
|
||||
}
|
||||
}
|
||||
|
||||
const { activeCart, activeCartItems, getCarts } = inject("pos");
|
||||
const posStore = usePosStore();
|
||||
const { activeCart, activeCartItems } = useActiveCart();
|
||||
const isAdding = ref(false);
|
||||
|
||||
async function addToCart() {
|
||||
@@ -41,7 +42,7 @@ async function addToCart() {
|
||||
});
|
||||
|
||||
$snackbar(`Đã thêm ${newCartItems.length} sản phẩm vào giỏ hàng`, "Success");
|
||||
getCarts();
|
||||
posStore.getCarts();
|
||||
emit("close");
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
@@ -1,21 +1,16 @@
|
||||
<script setup>
|
||||
import CartItem from "~/components/pos/CartItem.vue";
|
||||
import useActiveCart from "~/components/pos/composables/useActiveCart";
|
||||
import useOrderInfo from "~/components/pos/composables/useOrderInfo";
|
||||
import usePlaceOrder from "~/components/pos/composables/usePlaceOrder";
|
||||
|
||||
const id = "confirmOrderModal";
|
||||
const emit = defineEmits(["close"]);
|
||||
const { activeCart, activeCartItems, invalidCartItems, orderInfo, fullAddress, getCarts, subtotal, total } =
|
||||
inject("pos");
|
||||
|
||||
const posStore = usePosStore();
|
||||
const { orderInfo, invalidCartItems } = storeToRefs(posStore);
|
||||
const { activeCart, activeCartItems, subtotal } = useActiveCart();
|
||||
const { fullAddress, total } = useOrderInfo();
|
||||
const { isPending, showVietQRModal, placeOrder, testReturnPage } = usePlaceOrder({
|
||||
activeCart,
|
||||
activeCartItems,
|
||||
invalidCartItems,
|
||||
orderInfo,
|
||||
fullAddress,
|
||||
subtotal,
|
||||
total,
|
||||
getCarts,
|
||||
onSuccess: () => emit("close"),
|
||||
});
|
||||
</script>
|
||||
@@ -144,6 +139,7 @@ const { isPending, showVietQRModal, placeOrder, testReturnPage } = usePlaceOrder
|
||||
<button
|
||||
@click="placeOrder"
|
||||
:class="['button is-success', isPending && 'is-loading']"
|
||||
:disabled="invalidCartItems.length > 0 || activeCartItems.length === 0"
|
||||
>
|
||||
Đặt hàng
|
||||
</button>
|
||||
|
||||
@@ -3,32 +3,17 @@ import Address from "~/components/pos/Address.vue";
|
||||
import CartItem from "~/components/pos/CartItem.vue";
|
||||
import CartTabs from "~/components/pos/CartTabs.vue";
|
||||
import useActiveCart from "~/components/pos/composables/useActiveCart";
|
||||
import useCartData from "~/components/pos/composables/useCartData";
|
||||
import useCustomer from "~/components/pos/composables/useCustomer";
|
||||
import useOrderInfo from "~/components/pos/composables/useOrderInfo";
|
||||
import SearchBox from "~/components/SearchBox.vue";
|
||||
|
||||
const store = useStore();
|
||||
const { $insertapi, $formatNum } = useNuxtApp();
|
||||
|
||||
const { carts, cartItems, customers, getCarts, isPending } = useCartData();
|
||||
const posStore = usePosStore();
|
||||
const { carts, isPending, activeCartId, invalidCartItems, addresses, orderInfo } = storeToRefs(posStore);
|
||||
|
||||
const { activeCartId, activeCart, activeCartItems, customerIdsWithNoCart, subtotal, invalidCartItems } = useActiveCart({
|
||||
carts,
|
||||
cartItems,
|
||||
customers,
|
||||
});
|
||||
|
||||
const { addresses, getAddresses, isChangingCus, changeCustomer } = useCustomer({ activeCart, getCarts });
|
||||
|
||||
const { orderInfo, isOrderValid, fullAddress, total } = useOrderInfo({
|
||||
activeCartId,
|
||||
activeCart,
|
||||
activeCartItems,
|
||||
addresses,
|
||||
getAddresses,
|
||||
subtotal,
|
||||
});
|
||||
const { activeCart, activeCartItems, customerIdsWithNoCart, subtotal } = useActiveCart();
|
||||
const { isOrderValid, total } = useOrderInfo();
|
||||
|
||||
const showModal = ref();
|
||||
|
||||
@@ -57,19 +42,6 @@ function openAddAddressModal() {
|
||||
height: "auto",
|
||||
};
|
||||
}
|
||||
provide("pos", {
|
||||
carts,
|
||||
activeCartId,
|
||||
activeCart,
|
||||
activeCartItems,
|
||||
invalidCartItems,
|
||||
isChangingCus,
|
||||
orderInfo,
|
||||
fullAddress,
|
||||
getCarts,
|
||||
subtotal,
|
||||
total,
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -170,7 +142,7 @@ provide("pos", {
|
||||
placeholder: 'Khách hàng',
|
||||
optionid: activeCart?.customer,
|
||||
onOption: (e) => {
|
||||
if (e?.id !== activeCart?.customer) changeCustomer(e?.id || null);
|
||||
if (e?.id !== activeCart?.customer) posStore.changeCustomer(e?.id || null);
|
||||
},
|
||||
addon: {
|
||||
component: 'customer/CustomerQuickAdd',
|
||||
@@ -250,7 +222,7 @@ provide("pos", {
|
||||
:address="address"
|
||||
:selected="orderInfo.address?.id === address.id"
|
||||
@selectAddress="orderInfo.address = $event"
|
||||
@submit="getAddresses"
|
||||
@submit="posStore.getAddresses(activeCart.customer)"
|
||||
/>
|
||||
</div>
|
||||
<div
|
||||
@@ -379,7 +351,7 @@ provide("pos", {
|
||||
async () => {
|
||||
const newCart = await $insertapi('Cart', { notify: false });
|
||||
activeCartId = newCart.id;
|
||||
getCarts();
|
||||
posStore.getCarts();
|
||||
}
|
||||
"
|
||||
class="button is-primary"
|
||||
|
||||
@@ -3,7 +3,7 @@ const props = defineProps({
|
||||
invoiceId: Number,
|
||||
finalizeOrder: Function,
|
||||
});
|
||||
const { activeCartItems } = inject("pos");
|
||||
const { activeCartItems } = useActiveCart();
|
||||
const subtotal = computed(() => {
|
||||
return activeCartItems.value?.reduce((prev, curr) => prev + curr.imei__variant__price, 0);
|
||||
});
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
@@ -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 };
|
||||
}
|
||||
@@ -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 };
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user