2023-11-10 14:05:15 +08:00
|
|
|
import { defineStore } from 'pinia';
|
|
|
|
|
2024-11-21 00:16:49 +08:00
|
|
|
import useApiUtil from '@/composables/api_util';
|
2023-11-10 14:05:15 +08:00
|
|
|
import {
|
|
|
|
IHazard,
|
|
|
|
IStockAdjustment,
|
|
|
|
IStockCategory,
|
|
|
|
IStockItem,
|
2024-05-29 14:00:08 +08:00
|
|
|
IStockItemVariant,
|
2023-11-10 14:05:15 +08:00
|
|
|
IStockOrder,
|
|
|
|
IStockProduct,
|
|
|
|
IStockUnit,
|
2024-06-29 21:19:21 +08:00
|
|
|
} from '@/models/inventory';
|
2023-11-10 14:05:15 +08:00
|
|
|
import {
|
2024-11-21 00:16:49 +08:00
|
|
|
GetAllHazardsDocument, GetAllHazardsQuery, GetAllHazardsQueryVariables,
|
|
|
|
GetAllStockAdjustmentsDocument, GetAllStockAdjustmentsQuery, GetAllStockAdjustmentsQueryVariables,
|
|
|
|
GetAllStockCategoriesDocument, GetAllStockCategoriesQuery, GetAllStockCategoriesQueryVariables,
|
|
|
|
GetAllStockItemsDocument, GetAllStockItemsQuery, GetAllStockItemsQueryVariables,
|
|
|
|
GetAllStockItemVariantsDocument, GetAllStockItemVariantsQuery, GetAllStockItemVariantsQueryVariables,
|
|
|
|
GetAllStockOrdersDocument, GetAllStockOrdersQuery, GetAllStockOrdersQueryVariables,
|
|
|
|
GetAllStockProductsDocument, GetAllStockProductsQuery, GetAllStockProductsQueryVariables,
|
|
|
|
GetAllStockUnitsDocument, GetAllStockUnitsQuery, GetAllStockUnitsQueryVariables,
|
2024-06-29 21:19:21 +08:00
|
|
|
} from '@/graphql/operations/inventory.queries';
|
|
|
|
import { IPagination, IPaginationMeta } from '@/models/pagination';
|
2023-11-10 14:05:15 +08:00
|
|
|
|
|
|
|
const { withClientQuery } = useApiUtil();
|
|
|
|
|
|
|
|
export const useInventoryStore = defineStore('inventory', {
|
|
|
|
state: () => {
|
|
|
|
return {
|
|
|
|
hazards: [],
|
|
|
|
fetchingHazards: false,
|
|
|
|
categories: [],
|
|
|
|
fetchingCategories: false,
|
|
|
|
units: [],
|
|
|
|
fetchingUnits: false,
|
|
|
|
products: [],
|
|
|
|
fetchingProducts: false,
|
|
|
|
productsPaging: {},
|
|
|
|
stockItems: [],
|
|
|
|
stockItemsPaging: {},
|
|
|
|
fetchingItems: false,
|
|
|
|
adjustments: [],
|
|
|
|
adjustmentsPaging: {},
|
|
|
|
fetchingAdjustments: false,
|
|
|
|
basket: [],
|
|
|
|
stockOrders: [],
|
|
|
|
fetchingStockOrders: false,
|
|
|
|
stockOrdersPaging: {},
|
|
|
|
} as {
|
|
|
|
hazards: IHazard[];
|
|
|
|
fetchingHazards: boolean;
|
|
|
|
categories: IStockCategory[];
|
|
|
|
fetchingCategories: boolean;
|
|
|
|
units: IStockUnit[];
|
|
|
|
fetchingUnits: boolean;
|
|
|
|
products: IStockProduct[];
|
|
|
|
fetchingProducts: boolean;
|
|
|
|
productsPaging: IPaginationMeta;
|
|
|
|
stockItems: IStockItem[];
|
|
|
|
stockItemsPaging: IPaginationMeta;
|
|
|
|
fetchingItems: boolean;
|
|
|
|
adjustments: IStockAdjustment[];
|
|
|
|
adjustmentsPaging: IPaginationMeta;
|
|
|
|
fetchingAdjustments: boolean;
|
|
|
|
basket: any[];
|
|
|
|
stockOrders: IStockOrder[];
|
|
|
|
fetchingStockOrders: boolean;
|
|
|
|
stockOrdersPaging: IPaginationMeta;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
getters: {
|
|
|
|
getHazards: state => state.hazards,
|
|
|
|
getCategories: state => state.categories,
|
|
|
|
getUnits: state => state.units,
|
|
|
|
getProducts: state => state.products,
|
|
|
|
getStockItems: state => state.stockItems,
|
|
|
|
getAdjustments: state => state.adjustments,
|
|
|
|
getBasket: state => state.basket,
|
|
|
|
getStockOrders: state => state.stockItems,
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
// getAll stock Item deps
|
|
|
|
async fetchAllDependencies() {
|
|
|
|
await this.fetchHazards();
|
|
|
|
await this.fetchCategories();
|
|
|
|
await this.fetchUnits();
|
|
|
|
},
|
|
|
|
// hazards
|
|
|
|
async fetchHazards() {
|
|
|
|
this.fetchingHazards = true;
|
2024-11-21 00:16:49 +08:00
|
|
|
await withClientQuery<GetAllHazardsQuery, GetAllHazardsQueryVariables>(GetAllHazardsDocument, {}, 'hazardAll')
|
2023-11-10 14:05:15 +08:00
|
|
|
.then((hazards: IHazard[]) => {
|
|
|
|
this.fetchingHazards = false;
|
|
|
|
this.hazards = hazards;
|
|
|
|
})
|
|
|
|
.catch(err => (this.fetchingHazards = false));
|
|
|
|
},
|
|
|
|
addHazard(payload): void {
|
|
|
|
this.hazards?.unshift(payload);
|
|
|
|
},
|
|
|
|
updateHazard(payload: IHazard): void {
|
|
|
|
const index = this.hazards?.findIndex(item => item.uid === payload?.uid);
|
|
|
|
if (index > -1) this.hazards[index] = payload;
|
|
|
|
},
|
|
|
|
|
|
|
|
// categories
|
|
|
|
async fetchCategories() {
|
|
|
|
this.fetchingCategories = true;
|
2024-11-21 00:16:49 +08:00
|
|
|
await withClientQuery<GetAllStockCategoriesQuery, GetAllStockCategoriesQueryVariables>(GetAllStockCategoriesDocument, {}, 'stockCategoryAll')
|
2023-11-10 14:05:15 +08:00
|
|
|
.then((categories: IStockCategory[]) => {
|
|
|
|
this.fetchingCategories = false;
|
|
|
|
this.categories = categories;
|
|
|
|
})
|
|
|
|
.catch(err => (this.fetchingCategories = false));
|
|
|
|
},
|
|
|
|
addCategory(payload): void {
|
|
|
|
this.categories?.unshift(payload);
|
|
|
|
},
|
|
|
|
updateCategory(payload: IStockCategory): void {
|
|
|
|
const index = this.categories?.findIndex(item => item.uid === payload?.uid);
|
|
|
|
if (index > -1) this.categories[index] = payload;
|
|
|
|
},
|
|
|
|
|
|
|
|
// units
|
|
|
|
async fetchUnits() {
|
|
|
|
this.fetchingUnits = true;
|
2024-11-21 00:16:49 +08:00
|
|
|
await withClientQuery<GetAllStockUnitsQuery, GetAllStockUnitsQueryVariables>(GetAllStockUnitsDocument, {}, 'stockUnitAll')
|
2023-11-10 14:05:15 +08:00
|
|
|
.then((units: IStockUnit[]) => {
|
|
|
|
this.fetchingUnits = false;
|
|
|
|
this.units = units;
|
|
|
|
})
|
|
|
|
.catch(err => (this.fetchingUnits = false));
|
|
|
|
},
|
|
|
|
addUnit(payload): void {
|
|
|
|
this.units?.unshift(payload);
|
|
|
|
},
|
|
|
|
updateUnit(payload: IStockUnit): void {
|
|
|
|
const index = this.units?.findIndex(item => item.uid === payload?.uid);
|
|
|
|
if (index > -1) this.units[index] = payload;
|
|
|
|
},
|
|
|
|
|
|
|
|
// products
|
|
|
|
async fetchProducts(params) {
|
|
|
|
this.fetchingProducts = true;
|
2024-11-21 00:16:49 +08:00
|
|
|
await withClientQuery<GetAllStockProductsQuery, GetAllStockProductsQueryVariables>(GetAllStockProductsDocument, params, 'stockProductAll')
|
2023-11-10 14:05:15 +08:00
|
|
|
.then((paging: IPagination<IStockProduct>) => {
|
|
|
|
this.fetchingProducts = false;
|
|
|
|
this.products = paging.items ?? [];
|
|
|
|
this.productsPaging['totalCount'] = paging.totalCount;
|
|
|
|
this.productsPaging['pageInfo'] = paging.pageInfo;
|
|
|
|
})
|
|
|
|
.catch(err => (this.fetchingProducts = false));
|
|
|
|
},
|
|
|
|
addStockProduct(payload): void {
|
|
|
|
this.products?.unshift(payload);
|
|
|
|
},
|
|
|
|
updateProduct(payload: IStockProduct): void {
|
|
|
|
const index = this.products?.findIndex(item => item.uid === payload?.uid);
|
|
|
|
const old = this.products[index];
|
|
|
|
if (index > -1) this.products[index] = { ...old, ...payload };
|
|
|
|
},
|
|
|
|
|
|
|
|
// stockItems
|
|
|
|
async fetchItems(params) {
|
|
|
|
this.fetchingItems = true;
|
2024-11-21 00:16:49 +08:00
|
|
|
await withClientQuery<GetAllStockItemsQuery, GetAllStockItemsQueryVariables>(GetAllStockItemsDocument, params, 'stockItemAll')
|
|
|
|
.then(paging => {
|
2023-11-10 14:05:15 +08:00
|
|
|
this.fetchingItems = false;
|
|
|
|
this.stockItems = paging.items ?? [];
|
|
|
|
this.stockItemsPaging['totalCount'] = paging.totalCount;
|
|
|
|
this.stockItemsPaging['pageInfo'] = paging.pageInfo;
|
|
|
|
})
|
|
|
|
.catch(err => (this.fetchingItems = false));
|
|
|
|
},
|
|
|
|
addItem(payload): void {
|
|
|
|
this.stockItems?.unshift(payload);
|
|
|
|
},
|
|
|
|
updateItem(payload: IStockItem): void {
|
|
|
|
const index = this.stockItems?.findIndex(item => item.uid === payload?.uid);
|
|
|
|
if (index > -1) this.stockItems[index] = payload;
|
|
|
|
},
|
2024-05-29 14:00:08 +08:00
|
|
|
async fetchItemVariants(stockItemUid: string) {
|
2024-11-21 00:16:49 +08:00
|
|
|
await withClientQuery<GetAllStockItemVariantsQuery, GetAllStockItemVariantsQueryVariables>(GetAllStockItemVariantsDocument, { stockItemUid }, 'stockItemVariants')
|
2024-05-29 14:00:08 +08:00
|
|
|
.then((data: IStockItemVariant[]) => {
|
|
|
|
this.stockItems?.map(item => {
|
|
|
|
if (item.uid === stockItemUid) {
|
|
|
|
item.variants = [...data] ?? [];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => (this.fetchingItems = false));
|
|
|
|
},
|
|
|
|
addItemVariant(payload): void {
|
|
|
|
this.stockItems?.map(item => {
|
|
|
|
if (item.uid === payload.stockItemUid) {
|
|
|
|
item.variants?.unshift(payload);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
updateItemVariant(payload: IStockItemVariant): void {
|
|
|
|
this.stockItems?.map(item => {
|
|
|
|
if (item.uid === payload.stockItemUid) {
|
|
|
|
const index = item.variants?.findIndex(v => v.uid === payload.uid);
|
|
|
|
if (index > -1) item.variants[index] = payload;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2023-11-10 14:05:15 +08:00
|
|
|
|
|
|
|
// stockOrders
|
|
|
|
async fetchStockOrders(params) {
|
|
|
|
this.fetchingItems = true;
|
|
|
|
this.stockOrders = []
|
2024-11-21 00:16:49 +08:00
|
|
|
await withClientQuery<GetAllStockOrdersQuery, GetAllStockOrdersQueryVariables>(GetAllStockOrdersDocument, params, 'stockOrderAll')
|
2023-11-10 14:05:15 +08:00
|
|
|
.then((paging: IPagination<IStockOrder>) => {
|
|
|
|
this.fetchingItems = false;
|
|
|
|
this.stockOrders = paging.items ?? [];
|
|
|
|
this.stockOrdersPaging['totalCount'] = paging.totalCount;
|
|
|
|
this.stockOrdersPaging['pageInfo'] = paging.pageInfo;
|
|
|
|
})
|
|
|
|
.catch(err => (this.fetchingStockOrders = false));
|
|
|
|
},
|
|
|
|
addStockOrder(payload): void {
|
|
|
|
this.stockOrders?.unshift(payload);
|
|
|
|
},
|
|
|
|
updateStockOrder(payload: IStockOrder): void {
|
|
|
|
const index = this.stockOrders?.findIndex(item => item.uid === payload?.uid);
|
|
|
|
const old = this.stockOrders[index];
|
|
|
|
if (index > -1) this.stockOrders[index] = { ...old, ...payload };
|
|
|
|
},
|
|
|
|
issueStockOrder(payload: any): void {
|
|
|
|
this.updateStockOrder(payload?.stockOrder);
|
|
|
|
for (const op of payload?.orderProducts) {
|
|
|
|
this.updateProduct(op.product);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// adjustments
|
|
|
|
async fetchAdjustments(params) {
|
|
|
|
this.fetchingAdjustments = true;
|
2024-11-21 00:16:49 +08:00
|
|
|
await withClientQuery<GetAllStockAdjustmentsQuery, GetAllStockAdjustmentsQueryVariables>(GetAllStockAdjustmentsDocument, params, 'stockAdjustmentAll')
|
2023-11-10 14:05:15 +08:00
|
|
|
.then((paging: IPagination<IStockAdjustment>) => {
|
|
|
|
this.fetchingAdjustments = false;
|
|
|
|
this.adjustments = paging.items ?? [];
|
|
|
|
this.adjustmentsPaging['totalCount'] = paging.totalCount;
|
|
|
|
this.adjustmentsPaging['pageInfo'] = paging.pageInfo;
|
|
|
|
})
|
|
|
|
.catch(err => (this.fetchingAdjustments = false));
|
|
|
|
},
|
|
|
|
addAdjustment(payload): void {
|
|
|
|
this.adjustments?.unshift(payload);
|
|
|
|
},
|
|
|
|
updateAdjustment(payload: IStockAdjustment): void {
|
|
|
|
const index = this.adjustments?.findIndex(item => item.uid === payload?.uid);
|
|
|
|
if (index > -1) this.adjustments[index] = payload;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Basket
|
2024-06-01 05:37:11 +08:00
|
|
|
addToBasket(uid: string, stockLotUid: string, quantity: number): void {
|
2023-11-10 14:05:15 +08:00
|
|
|
const index = this.products?.findIndex(item => item.uid === uid);
|
|
|
|
const product = this.products[index];
|
|
|
|
const orderItem = {
|
|
|
|
product,
|
2024-06-01 05:37:11 +08:00
|
|
|
stockLotUid,
|
2023-11-10 14:05:15 +08:00
|
|
|
quantity,
|
|
|
|
};
|
|
|
|
|
|
|
|
// if not in basket add
|
|
|
|
// if in basket modify quantity
|
|
|
|
const basketIndex = this.basket?.findIndex(oi => oi.product.uid === uid);
|
|
|
|
if (basketIndex == -1) {
|
|
|
|
this.basket.push(orderItem);
|
|
|
|
} else {
|
|
|
|
const count = this.basket[basketIndex].quantity;
|
|
|
|
this.basket[basketIndex].quantity = count + quantity;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
udateBasket(uid: string, quantity: number): void {
|
|
|
|
// modify quantity
|
|
|
|
const basketIndex = this.basket?.findIndex(oi => oi.product.uid === uid);
|
|
|
|
this.basket[basketIndex].quantity = quantity;
|
|
|
|
},
|
|
|
|
removeFromBasket(uid: string): void {
|
|
|
|
this.basket = [...this.basket.filter(oi => oi.product.uid !== uid)];
|
|
|
|
},
|
|
|
|
clearBasket(): void {
|
|
|
|
this.basket = [];
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|