"use strict";exports.id=9812,exports.ids=[9812,6174],exports.modules={49364:(e,t,n)=>{n.d(t,{deductStockForOrder:()=>d,restoreStockForOrder:()=>s});var a=n(8194),r=n(15230),i=n(14709);async function d(e,t,n,d){let s=await (0,r.p6)(e);if(!(0,r.U6)(s.features,"inventory"))return;let o=Array.from(new Set(d.map(e=>e.menuItemId))),u=await a._.menuIngredient.findMany({where:{menuItemId:{in:o}},include:{ingredient:!0}});if(0===u.length)return;let m=new Map;for(let e of d)for(let t of u.filter(t=>t.menuItemId===e.menuItemId)){let n=m.get(t.ingredientId)||0;m.set(t.ingredientId,n+e.quantity*Number(t.quantity))}let c=Array.from(m.entries());if(await a._.$transaction(async e=>{for(let[a,r]of c)await e.stockLevel.upsert({where:{ingredientId_storeId:{ingredientId:a,storeId:t}},create:{ingredientId:a,storeId:t,currentStock:-r,minStock:0},update:{currentStock:{decrement:r}}}),await e.stockMovement.create({data:{ingredientId:a,storeId:t,type:"out",reason:"order_deduct",quantity:r,orderId:n}})}),"true"===await (0,i.C5)(e,"auto_disable_menu_on_stock_out")){let n=await a._.stockLevel.findMany({where:{storeId:t,ingredientId:{in:Array.from(m.keys())},currentStock:{lte:0}}});if(n.length>0){let t=n.map(e=>e.ingredientId),r=Array.from(new Set((await a._.menuIngredient.findMany({where:{ingredientId:{in:t}},select:{menuItemId:!0}})).map(e=>e.menuItemId)));r.length>0&&await a._.menuItem.updateMany({where:{id:{in:r},tenantId:e,isAvailable:!0},data:{isAvailable:!1}})}}}async function s(e){let t=await a._.stockMovement.findMany({where:{orderId:e,reason:"order_deduct"}});if(0!==t.length&&(await a._.$transaction(async n=>{for(let a of t)await n.stockLevel.update({where:{ingredientId_storeId:{ingredientId:a.ingredientId,storeId:a.storeId}},data:{currentStock:{increment:Number(a.quantity)}}}),await n.stockMovement.create({data:{ingredientId:a.ingredientId,storeId:a.storeId,type:"in",reason:"adjustment",quantity:Number(a.quantity),notes:"Pembatalan pesanan",orderId:e}})}),t.length>0)){let e=await a._.ingredient.findUnique({where:{id:t[0].ingredientId}});if(e&&"true"===await (0,i.C5)(e.tenantId,"auto_disable_menu_on_stock_out")){let e=Array.from(new Set(t.map(e=>e.ingredientId))),n=t[0].storeId,r=await a._.stockLevel.findMany({where:{ingredientId:{in:e},storeId:n,currentStock:{gt:0}}});if(r.length>0){let e=r.map(e=>e.ingredientId);for(let t of Array.from(new Set((await a._.menuIngredient.findMany({where:{ingredientId:{in:e}},select:{menuItemId:!0}})).map(e=>e.menuItemId)))){let e=(await a._.menuIngredient.findMany({where:{menuItemId:t}})).map(e=>e.ingredientId),r=await a._.stockLevel.findMany({where:{ingredientId:{in:e},storeId:n}});e.every(e=>{let t=r.find(t=>t.ingredientId===e);return t&&Number(t.currentStock)>0})&&await a._.menuItem.update({where:{id:t},data:{isAvailable:!0}})}}}}}},15230:(e,t,n)=>{n.d(t,{MN:()=>i,U6:()=>d,p6:()=>r});var a=n(8194);async function r(e){let t=await a._.subscription.findFirst({where:{tenantId:e,status:{in:["active","trial"]}},include:{plan:!0},orderBy:{createdAt:"desc"}});return t?{maxStores:t.plan.maxStores,maxStaff:t.plan.maxStaff,maxMenuItems:t.plan.maxMenuItems,maxOrders:t.plan.maxOrders,features:JSON.parse(t.plan.features)}:{maxStores:1,maxStaff:3,maxMenuItems:30,maxOrders:100,features:["pos","qr_menu"]}}async function i(e,t){let n=await r(e),i=0,d=0;switch(t){case"stores":i=await a._.store.count({where:{tenantId:e,isActive:!0}}),d=n.maxStores;break;case"staff":i=await a._.user.count({where:{tenantId:e,isActive:!0}}),d=n.maxStaff;break;case"menuItems":i=await a._.menuItem.count({where:{tenantId:e,isActive:!0}}),d=n.maxMenuItems;break;case"orders":let s=new Date;s.setDate(1),s.setHours(0,0,0,0),i=await a._.order.count({where:{tenantId:e,createdAt:{gte:s}}}),d=n.maxOrders}return 0===d?{allowed:!0,current:i,max:d}:{allowed:i<d,current:i,max:d}}function d(e,t){return e.includes(t)}}};