{"version":3,"file":"itemUtils-a351d9ac.js","sources":["../../src/business_logic/stock/itemUtils.ts"],"sourcesContent":["import api from \"@/app/api\";\nimport { buildCache } from \"@/app/caching/buildCache\";\nimport type { IHasItemsCache } from \"@/app/caching/caches/itemCache\";\nimport type { IHasPackageTypesCache } from \"@/app/caching/caches/packageTypesCache\";\nimport type { IHasProductBrandsCache } from \"@/app/caching/caches/productBrandsCache\";\nimport type { IHasStockGroupsCache } from \"@/app/caching/caches/stockGroupsCache\";\nimport type { IHasSupplierManufacturersCache } from \"@/app/caching/caches/supplierManufacturersCache\";\nimport type { IHasUnitsOfMeasureCache } from \"@/app/caching/caches/unitsOfMeasureCache\";\nimport type { IHasVatCodesCache } from \"@/app/caching/caches/vatCodesCache\";\nimport { tenantService } from \"@/app/services/extensions/tenantServiceExtended\";\nimport {\n itemService,\n type Item,\n type PurchasingDetails,\n} from \"@/app/services/generated/itemService\";\nimport type Guid from \"@/app/types/common/guid\";\nimport type { Maybe } from \"@/components/utils/typeHelpers\";\nimport tenantStore from \"@/store/modules/tenantStore\";\n\nexport function itemUom(item: Maybe, cache: IHasUnitsOfMeasureCache) {\n return cache.unitsOfMeasure.getById(item?.details.unitOfMeasureId) ?? null;\n}\n\nexport function itemPackageType(\n item: Maybe,\n cache: IHasPackageTypesCache\n) {\n return cache.packageTypes.getById(item?.packaging?.packageTypeId) ?? null;\n}\n\nexport function itemIdToUom(\n itemId: Maybe,\n cache: IHasItemsCache & IHasUnitsOfMeasureCache\n) {\n return itemUom(cache.items.getById(itemId), cache);\n}\n\nexport function itemStockGroup(item: Maybe, cache: IHasStockGroupsCache) {\n return cache.stockGroups.getById(item?.stock?.stockGroupId);\n}\n\nexport function itemManufacturer(\n item: Maybe,\n cache: IHasSupplierManufacturersCache\n) {\n return cache.supplierManufacturers.getById(item?.alcoholDuty?.manufacturerId);\n}\n\nexport function itemResolvedTastingNotes(\n item: Maybe,\n cache: IHasProductBrandsCache\n) {\n return item?.labelling.labelUsesBrandTastingNotes\n ? cache.productBrands.getById(item.invoicing?.productBrandId)?.tastingNotes\n : item?.labelling.labelTastingNotes;\n}\n\nexport function itemResolvedIngredientsAndAllergens(\n item: Maybe,\n cache: IHasProductBrandsCache\n) {\n return item?.labelling.labelUsesBrandIngredientsAndAllergens\n ? cache.productBrands.getById(item.invoicing?.productBrandId)\n ?.ingredientsAndAllergens\n : item?.labelling.labelIngredientsAndAllergens;\n}\n\nexport function itemBestBeforeDate(item: Maybe) {\n const bestBeforeDays = item?.labelling.bestBeforeDays;\n return bestBeforeDays ? tenantService.today.plusDays(bestBeforeDays) : null;\n}\n\nexport function itemIsTraceable(\n item: Maybe,\n cache: IHasPackageTypesCache\n) {\n return (\n cache.packageTypes.getById(item?.packaging.packageTypeId)?.isTraceable ??\n false\n );\n}\n\nexport function itemIsReturnable(\n item: Maybe,\n cache: IHasPackageTypesCache\n) {\n return (\n cache.packageTypes.getById(item?.packaging.packageTypeId)?.isReturnable ??\n false\n );\n}\n\nexport function itemDefaultSupplierId(item: Maybe) {\n return (\n item?.purchasingDetails.find((p: { isDefault: boolean }) => p.isDefault)\n ?.supplierId ?? null\n );\n}\n\nexport function itemDefaultSupplier(\n item: Maybe,\n cache: IHasSupplierManufacturersCache\n) {\n return (\n cache.supplierManufacturers.getById(itemDefaultSupplierId(item)) ?? null\n );\n}\n\nexport function itemPurchasingDetailsForSupplierId(\n item: Maybe,\n supplierId: Maybe\n) {\n return (\n item?.purchasingDetails.find(\n (p) => supplierId && supplierId.equals(p.supplierId)\n ) ??\n item?.purchasingDetails.find((p) => !p.supplierId) ??\n null\n );\n}\n\nexport function purchasingDetailsPricePerUnit(\n purchasingDetails: PurchasingDetails\n) {\n return purchasingDetails?.purchasePrice && purchasingDetails?.purchasePackSize\n ? purchasingDetails?.purchasePrice / purchasingDetails?.purchasePackSize\n : null;\n}\n\nexport function itemUsesHopAlpha(item: Maybe) {\n return (\n tenantStore.state.tenantSettings.usesHopAlpha &&\n !!item?.materialDetails?.defaultHopAlpha\n );\n}\n\nexport function itemIdUsesHopAlpha(cache: IHasItemsCache, itemId: Maybe) {\n return itemUsesHopAlpha(cache.items.getById(itemId));\n}\n\nexport async function itemsApplyCostPriceUpdates(\n updates: readonly { itemId: Guid; newCostPrice: number }[]\n) {\n if (updates.length > 0) {\n const cache = await buildCache().withItems().buildAndUpdate();\n\n const itemsToSave = updates.map((update) => {\n const toSave = cache.items.forId(update.itemId).clone();\n toSave.details.costPrice = update.newCostPrice;\n return toSave;\n });\n\n await Promise.all(\n itemsToSave.map((item) => itemService.saveItem(api.tenantId, item))\n );\n }\n}\n\nexport function itemVatCode(item: Maybe, cache: IHasVatCodesCache) {\n return cache.vatCodes.getById(item?.vat?.vatCodeId) ?? null;\n}\n"],"names":[],"mappings":"6OAmBgB,SAAA,QAAQ,KAAmB,MAAgC,CACzE,OAAO,MAAM,eAAe,QAAQ,uBAAM,QAAQ,eAAe,GAAK,IACxE,CAFgB,0BAIA,SAAA,gBACd,KACA,MACA,QACA,OAAO,MAAM,aAAa,SAAQ,0BAAM,YAAN,eAAiB,aAAa,GAAK,IACvE,CALgB,0CAOA,SAAA,YACd,OACA,MACA,CACA,OAAO,QAAQ,MAAM,MAAM,QAAQ,MAAM,EAAG,KAAK,CACnD,CALgB,kCAOA,SAAA,eAAe,KAAmB,MAA6B,QAC7E,OAAO,MAAM,YAAY,SAAQ,0BAAM,QAAN,eAAa,YAAY,CAC5D,CAFgB,wCAIA,SAAA,iBACd,KACA,MACA,QACA,OAAO,MAAM,sBAAsB,SAAQ,0BAAM,cAAN,eAAmB,cAAc,CAC9E,CALgB,4CAOA,SAAA,yBACd,KACA,MACA,WACA,OAAO,iBAAM,UAAU,4BACnB,SAAM,cAAc,SAAQ,QAAK,YAAL,eAAgB,cAAc,IAA1D,eAA6D,aAC7D,uBAAM,UAAU,iBACtB,CAPgB,4DASA,SAAA,oCACd,KACA,MACA,WACA,OAAO,iBAAM,UAAU,uCACnB,SAAM,cAAc,SAAQ,QAAK,YAAL,eAAgB,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