"use strict";exports.id=6174,exports.ids=[6174],exports.modules={49364:(e,t,n)=>{n.d(t,{deductStockForOrder:()=>d,restoreStockForOrder:()=>o});var r=n(8194),i=n(15230),a=n(14709);async function d(e,t,n,d){let o=await (0,i.p6)(e);if(!(0,i.U6)(o.features,"inventory"))return;let u=Array.from(new Set(d.map(e=>e.menuItemId))),m=await r._.menuIngredient.findMany({where:{menuItemId:{in:u}},include:{ingredient:!0}});if(0===m.length)return;let s=new Map;for(let e of d)for(let t of m.filter(t=>t.menuItemId===e.menuItemId)){let n=s.get(t.ingredientId)||0;s.set(t.ingredientId,n+e.quantity*Number(t.quantity))}let I=Array.from(s.entries());if(await r._.$transaction(async e=>{for(let[r,i]of I)await e.stockLevel.upsert({where:{ingredientId_storeId:{ingredientId:r,storeId:t}},create:{ingredientId:r,storeId:t,currentStock:-i,minStock:0},update:{currentStock:{decrement:i}}}),await e.stockMovement.create({data:{ingredientId:r,storeId:t,type:"out",reason:"order_deduct",quantity:i,orderId:n}})}),"true"===await (0,a.C5)(e,"auto_disable_menu_on_stock_out")){let n=await r._.stockLevel.findMany({where:{storeId:t,ingredientId:{in:Array.from(s.keys())},currentStock:{lte:0}}});if(n.length>0){let t=n.map(e=>e.ingredientId),i=Array.from(new Set((await r._.menuIngredient.findMany({where:{ingredientId:{in:t}},select:{menuItemId:!0}})).map(e=>e.menuItemId)));i.length>0&&await r._.menuItem.updateMany({where:{id:{in:i},tenantId:e,isAvailable:!0},data:{isAvailable:!1}})}}}async function o(e){let t=await r._.stockMovement.findMany({where:{orderId:e,reason:"order_deduct"}});if(0!==t.length&&(await r._.$transaction(async n=>{for(let r of t)await n.stockLevel.update({where:{ingredientId_storeId:{ingredientId:r.ingredientId,storeId:r.storeId}},data:{currentStock:{increment:Number(r.quantity)}}}),await n.stockMovement.create({data:{ingredientId:r.ingredientId,storeId:r.storeId,type:"in",reason:"adjustment",quantity:Number(r.quantity),notes:"Pembatalan pesanan",orderId:e}})}),t.length>0)){let e=await r._.ingredient.findUnique({where:{id:t[0].ingredientId}});if(e&&"true"===await (0,a.C5)(e.tenantId,"auto_disable_menu_on_stock_out")){let e=Array.from(new Set(t.map(e=>e.ingredientId))),n=t[0].storeId,i=await r._.stockLevel.findMany({where:{ingredientId:{in:e},storeId:n,currentStock:{gt:0}}});if(i.length>0){let e=i.map(e=>e.ingredientId);for(let t of Array.from(new Set((await r._.menuIngredient.findMany({where:{ingredientId:{in:e}},select:{menuItemId:!0}})).map(e=>e.menuItemId)))){let e=(await r._.menuIngredient.findMany({where:{menuItemId:t}})).map(e=>e.ingredientId),i=await r._.stockLevel.findMany({where:{ingredientId:{in:e},storeId:n}});e.every(e=>{let t=i.find(t=>t.ingredientId===e);return t&&Number(t.currentStock)>0})&&await r._.menuItem.update({where:{id:t},data:{isAvailable:!0}})}}}}}}};