"use strict";(()=>{var e={};e.id=3032,e.ids=[3032],e.modules={72934:e=>{e.exports=require("next/dist/client/components/action-async-storage.external.js")},54580:e=>{e.exports=require("next/dist/client/components/request-async-storage.external.js")},45869:e=>{e.exports=require("next/dist/client/components/static-generation-async-storage.external.js")},20399:e=>{e.exports=require("next/dist/compiled/next-server/app-page.runtime.prod.js")},30517:e=>{e.exports=require("next/dist/compiled/next-server/app-route.runtime.prod.js")},78893:e=>{e.exports=require("buffer")},84770:e=>{e.exports=require("crypto")},76162:e=>{e.exports=require("stream")},21764:e=>{e.exports=require("util")},75966:(e,t,r)=>{r.r(t),r.d(t,{originalPathname:()=>y,patchFetch:()=>w,requestAsyncStorage:()=>m,routeModule:()=>p,serverHooks:()=>f,staticGenerationAsyncStorage:()=>x});var a={};r.r(a),r.d(a,{GET:()=>l});var s=r(49303),n=r(88716),o=r(60670),u=r(87070),i=r(95456),d=r(8194),c=r(45239);async function l(e){try{let t=await (0,i.ts)();if(!t)return u.NextResponse.json({error:"Unauthorized"},{status:401});if(!(0,i.MH)(t.role,["owner","admin","manager"]))return u.NextResponse.json({error:"Forbidden"},{status:403});let r=await (0,c.F)(t.tenantId,"advanced_analytics");if(r)return r;let a=e.nextUrl.searchParams.get("storeId"),s=e.nextUrl.searchParams.get("period")||"30d",n=30;"7d"===s&&(n=7),"90d"===s&&(n=90);let o=new Date;o.setDate(o.getDate()-n);let l=new Date;l.setDate(l.getDate()-2*n);let p={tenantId:t.tenantId};t.storeId?p.storeId=t.storeId:a&&(p.storeId=a);let m=t.tenantId,x=t.storeId||a,f=x?"AND o.storeId = ?":"",y=x?[m,o,x]:[m,o],w=(await d._.$queryRawUnsafe(`SELECT
        DATE(o.createdAt) as date,
        SUM(o.total) as revenue,
        COUNT(*) as orders
      FROM orders o
      WHERE o.tenantId = ?
        AND o.status = 'completed'
        AND o.paymentStatus = 'paid'
        AND o.createdAt >= ?
        ${f}
      GROUP BY DATE(o.createdAt)
      ORDER BY date ASC`,...y)).map(e=>({date:"string"==typeof e.date?e.date:new Date(e.date).toISOString().slice(0,10),revenue:Number(e.revenue),orders:Number(e.orders)})),g=(await d._.$queryRawUnsafe(`SELECT
        HOUR(o.createdAt) as hour,
        DAYOFWEEK(o.createdAt) - 1 as day,
        COUNT(*) as count
      FROM orders o
      WHERE o.tenantId = ?
        AND o.status = 'completed'
        AND o.paymentStatus = 'paid'
        AND o.createdAt >= ?
        ${f}
      GROUP BY HOUR(o.createdAt), DAYOFWEEK(o.createdAt)
      ORDER BY day ASC, hour ASC`,...y)).map(e=>({hour:Number(e.hour),day:Number(e.day),count:Number(e.count)})),A=(await d._.order.groupBy({by:["orderType"],where:{...p,status:"completed",paymentStatus:"paid",createdAt:{gte:o}},_count:{id:!0},_sum:{total:!0}})).map(e=>({type:e.orderType,count:e._count.id,revenue:Number(e._sum.total||0)})),h=await d._.order.aggregate({where:{...p,status:"completed",paymentStatus:"paid",createdAt:{gte:o}},_sum:{total:!0},_count:{id:!0}}),v=await d._.order.aggregate({where:{...p,status:"completed",paymentStatus:"paid",createdAt:{gte:l,lt:o}},_sum:{total:!0},_count:{id:!0}}),_=Number(h._sum.total||0),S=Number(v._sum.total||0),R=h._count.id,I=v._count.id,N=R>0?_/R:0,D=I>0?S/I:0,O=(e,t)=>0===t?e>0?100:0:Math.round((e-t)/t*100),E={revenue:{current:_,previous:S,percentage:O(_,S)},orders:{current:R,previous:I,percentage:O(R,I)},avgOrderValue:{current:Math.round(N),previous:Math.round(D),percentage:O(N,D)}};return u.NextResponse.json({revenueTrend:w,hourlyHeatmap:g,orderTypeBreakdown:A,growth:E})}catch(e){return console.error("Sales analytics error:",e),u.NextResponse.json({error:"Internal server error"},{status:500})}}let p=new s.AppRouteRouteModule({definition:{kind:n.x.APP_ROUTE,page:"/api/analytics/sales/route",pathname:"/api/analytics/sales",filename:"route",bundlePath:"app/api/analytics/sales/route"},resolvedPagePath:"D:\\PROJECT\\resto-saas\\src\\app\\api\\analytics\\sales\\route.ts",nextConfigOutput:"",userland:a}),{requestAsyncStorage:m,staticGenerationAsyncStorage:x,serverHooks:f}=p,y="/api/analytics/sales/route";function w(){return(0,o.patchFetch)({serverHooks:f,staticGenerationAsyncStorage:x})}},95456:(e,t,r)=>{r.d(t,{Gv:()=>l,MH:()=>f,PT:()=>y,RA:()=>p,WX:()=>m,c_:()=>c,ts:()=>x});var a=r(41482),s=r.n(a),n=r(42023),o=r.n(n),u=r(71615),i=r(8194);let d=process.env.JWT_SECRET||"fallback-secret-change-me";async function c(e){return o().hash(e,12)}async function l(e,t){return o().compare(e,t)}function p(e){return s().sign(e,d,{expiresIn:"7d"})}function m(e){try{return s().verify(e,d)}catch{return null}}async function x(){let e=await (0,u.cookies)(),t=e.get("token")?.value;if(!t)return null;let r=m(t);return r?await i._.user.findUnique({where:{id:r.userId},include:{tenant:!0,store:!0}}):null}function f(e,t){return t.includes(e)}function y(e){return!["owner","admin"].includes(e.role)&&!!e.storeId&&!!e.store&&!e.store.isActive}},8194:(e,t,r)=>{r.d(t,{_:()=>s});let a=require("@prisma/client"),s=globalThis.prisma??new a.PrismaClient},45239:(e,t,r)=>{r.d(t,{F:()=>n});var a=r(87070),s=r(15230);async function n(e,t){let r=await (0,s.p6)(e);return(0,s.U6)(r.features,t)?null:a.NextResponse.json({error:"Fitur ini tidak tersedia pada paket Anda. Silakan upgrade."},{status:403})}},15230:(e,t,r)=>{r.d(t,{MN:()=>n,U6:()=>o,p6:()=>s});var a=r(8194);async function s(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 n(e,t){let r=await s(e),n=0,o=0;switch(t){case"stores":n=await a._.store.count({where:{tenantId:e,isActive:!0}}),o=r.maxStores;break;case"staff":n=await a._.user.count({where:{tenantId:e,isActive:!0}}),o=r.maxStaff;break;case"menuItems":n=await a._.menuItem.count({where:{tenantId:e,isActive:!0}}),o=r.maxMenuItems;break;case"orders":let u=new Date;u.setDate(1),u.setHours(0,0,0,0),n=await a._.order.count({where:{tenantId:e,createdAt:{gte:u}}}),o=r.maxOrders}return 0===o?{allowed:!0,current:n,max:o}:{allowed:n<o,current:n,max:o}}function o(e,t){return e.includes(t)}}};var t=require("../../../../webpack-runtime.js");t.C(e);var r=e=>t(t.s=e),a=t.X(0,[8948,5972,4261,2023],()=>r(75966));module.exports=a})();