diff --git a/src/services/ProductService.ts b/src/services/ProductService.ts index f0e37882..9731c5ff 100644 --- a/src/services/ProductService.ts +++ b/src/services/ProductService.ts @@ -1,4 +1,5 @@ import { api } from '@/adapter'; +import { hasError } from "@/utils"; const fetchProducts = async (query: any): Promise => { return api({ @@ -34,9 +35,45 @@ const updateVariance = async (query: any): Promise => { }) } +const isProductFacilityAssocExists = async (productId: string, facilityId: string): Promise => { + try { + + const resp = await api({ + url: 'performFind', + method: 'POST', + data: { + "inputFields": { + facilityId: facilityId, + productId: productId, + }, + "viewSize": 1, + "entityName": "ProductFacility", + "fieldList": ["productId", "facilityId"], + noConditionFind: 'Y' + } + }) as any + if (!hasError(resp) && resp.data.docs.length) { + return true + } + return false + } catch (err) { + return false + } +} + +const addProductToFacility = async (payload: any): Promise => { + return api({ + url: "service/addProductToFacility", + method: "post", + data: payload + }); +} + export const ProductService = { + addProductToFacility, fetchProducts, importInventoryCount, + isProductFacilityAssocExists, getFacilityLocations, updateVariance } \ No newline at end of file diff --git a/src/views/count.vue b/src/views/count.vue index 985bf093..1b5f55f4 100644 --- a/src/views/count.vue +++ b/src/views/count.vue @@ -219,6 +219,8 @@ if (this.quantity) { this.product.quantity = this.quantity; this.product.availableQOH = this.availableQOH; + //Create the ProductFacility record if it does not exist. + this.checkAndCreateProductFacilityAssoc(); this.store.dispatch('product/updateInventoryCount', { ...this.product, locationId: this.product.locationId }); showToast(translate("Item added to upload list"), [{ text: translate('View'), @@ -232,6 +234,21 @@ showToast(translate("Enter the stock count for the product")) } }, + async checkAndCreateProductFacilityAssoc() { + try { + if (!await ProductService.isProductFacilityAssocExists(this.product.productId, this.facility.facilityId)) { + const resp = await ProductService.addProductToFacility({ + productId: this.product.productId, + facilityId: this.facility.facilityId + }); + if (hasError(resp)) { + throw resp.data; + } + } + } catch (err) { + console.error(err); + } + }, async getInventory() { if (!this.product.locationId) { console.warn("There are no facility locations, skipping fetching the inventory");