"use strict";exports.id=8347,exports.ids=[8347],exports.modules={48347:(e,t,n)=>{n.d(t,{B:()=>o,applyPromotionToOrder:()=>c,detectBundle:()=>l,detectHappyHour:()=>m,r:()=>d,recordPromotionUsage:()=>s});var r=n(8194);function i(e){return"Rp"+e.toLocaleString("id-ID")}function o(e){return"error"in e}async function a(e,t){let n=await r._.promotionStore.findMany({where:{promotionId:e},select:{storeId:!0}});return 0===n.length||n.some(e=>e.storeId===t)}async function u(e){let[t,n]=await Promise.all([r._.promotionMenuItem.findMany({where:{promotionId:e},select:{menuItemId:!0}}),r._.promotionCategory.findMany({where:{promotionId:e},select:{categoryId:!0}})]);if(0===t.length&&0===n.length)return null;let i=new Set;for(let e of t)i.add(e.menuItemId);if(n.length>0){let e=n.map(e=>e.categoryId);for(let t of(await r._.menuItem.findMany({where:{categoryId:{in:e},isActive:!0},select:{id:!0}})))i.add(t.id)}return i}async function d(e){let t,n;let{tenantId:o,storeId:u,subtotal:d,promoCode:m,customerSessionId:l}=e,c=e.currentTime??new Date;if(!m)return{error:"Kode promo wajib diisi",code:"MISSING_CODE"};let s=await r._.promotion.findFirst({where:{tenantId:o,code:m.toUpperCase(),isActive:!0,type:"voucher"}});if(!s)return{error:"Kode promo tidak ditemukan",code:"NOT_FOUND"};if(c<s.startDate||c>s.endDate)return{error:"Kode promo sudah tidak berlaku",code:"EXPIRED"};if(null!==s.maxUsage&&s.usageCount>=s.maxUsage)return{error:"Kuota promo sudah habis",code:"DEPLETED"};if(l&&null!==s.maxUsagePerUser&&s.maxUsagePerUser>0&&await r._.promotionUsage.count({where:{promotionId:s.id,customerSessionId:l}})>=s.maxUsagePerUser)return{error:"Anda sudah menggunakan promo ini sebelumnya",code:"USER_LIMIT_REACHED"};if(!await a(s.id,u))return{error:"Promo tidak berlaku untuk cabang ini",code:"STORE_NOT_ELIGIBLE"};if(null!==s.minOrderAmount&&d<Number(s.minOrderAmount))return{error:`Minimum pembelian ${i(Number(s.minOrderAmount))}`,code:"MIN_ORDER_NOT_MET"};let p=Number(s.discountValue??0),f=s.maxDiscount?Number(s.maxDiscount):null;return"percentage"===s.discountType?(t=Math.min(Math.round(d*p/100),f??d),n=`Voucher ${s.code} (-${p}%)`):(t=Math.min(p,d),n=`Voucher ${s.code} (-${i(p)})`),{promotionId:s.id,type:s.type,name:s.name,code:s.code,discountAmount:t,discountLabel:n}}async function m(e){let{tenantId:t,storeId:n,items:o,subtotal:d}=e,m=e.currentTime??new Date,l=await r._.promotion.findMany({where:{tenantId:t,type:"happy_hour",isActive:!0,startDate:{lte:m},endDate:{gte:m}}});if(0===l.length)return null;let c=m.getHours().toString().padStart(2,"0")+":"+m.getMinutes().toString().padStart(2,"0"),s=m.getDay(),p=null;for(let e of l){let t,r,m;if(e.startTime&&e.endTime&&(c<e.startTime||c>e.endTime))continue;if(e.daysOfWeek)try{if(!JSON.parse(e.daysOfWeek).includes(s))continue}catch{continue}if(!await a(e.id,n))continue;let l=await u(e.id);if((t=null===l?d:o.filter(e=>l.has(e.menuItemId)).reduce((e,t)=>e+t.subtotal,0))<=0)continue;let f=Number(e.discountValue??0),y=e.maxDiscount?Number(e.maxDiscount):null;"percentage"===e.discountType?(r=Math.min(Math.round(t*f/100),y??t),m=`Happy Hour ${e.name} (-${f}%)`):(r=Math.min(f,t),m=`Happy Hour ${e.name} (-${i(f)})`),(!p||r>p.discountAmount)&&(p={promotionId:e.id,type:e.type,name:e.name,code:null,discountAmount:r,discountLabel:m})}return p}async function l(e){let{tenantId:t,storeId:n,items:i}=e,o=e.currentTime??new Date,d=await r._.promotion.findMany({where:{tenantId:t,type:"bundle",isActive:!0,startDate:{lte:o},endDate:{gte:o}}});if(0===d.length)return null;let m=null;for(let e of d){if(!await a(e.id,n))continue;let t=e.buyQuantity??0,r=e.getQuantity??0;if(t<=0||r<=0)continue;let o=await u(e.id),d=[];for(let e of i)if(null===o||o.has(e.menuItemId))for(let t=0;t<e.quantity;t++)d.push({menuItemId:e.menuItemId,unitPrice:e.unitPrice});let l=t+r;if(d.length<l)continue;d.sort((e,t)=>e.unitPrice-t.unitPrice);let c=0;for(let e=0;e<r&&e<d.length;e++)c+=d[e].unitPrice;if((c=Math.round(c))<=0)continue;let s=`Bundle ${e.name} (Beli ${t} Gratis ${r})`;(!m||c>m.discountAmount)&&(m={promotionId:e.id,type:e.type,name:e.name,code:null,discountAmount:c,discountLabel:s})}return m}async function c(e){if(e.promoCode)return await d(e);let t=await m(e),n=await l(e);return t&&n?t.discountAmount>=n.discountAmount?t:n:t||n||null}async function s(e,t,n,i){await r._.$transaction(async r=>{await r.promotionUsage.create({data:{promotionId:e,orderId:t,discountAmount:n,customerSessionId:i??null}}),await r.promotion.update({where:{id:e},data:{usageCount:{increment:1}}})})}}};