1import{j as e}from"./motion-Db2Xga7i.js";import{r as d,d as A,B as se,C as B,X as I,Y as $,T as L,e as te,P as Te,f as Fe,g as we,h as Pe,L as qe,i as ae}from"./charts-B1Y5zxTN.js";import{s as S,a5 as Ae,B as ne,j as Le,q as Ee,r as Qe,t as k,v as V,w as Re,x as y}from"./index-C66_C_P1.js";import{C as m,a as x,b as u,c as j,d as z}from"./card-DXxMF52i.js";import{S as K,a as Z,b as G,c as H,d as C}from"./select-DAot6aZb.js";import{T as Oe,a as De,b as le}from"./tabs-CIcQut5I.js";import{u as Ye}from"./useClienteData-DUNP-wT2.js";import{e as Be}from"./exportCsv-DngXSoT3.js";import{D as Ie}from"./download-m4owDuOM.js";import{P as $e}from"./printer-DuNPlL1s.js";import{T as ke}from"./trending-up-Bmm7P64v.js";import{C as Ve}from"./calendar-days-aEahorKv.js";import{E as Ke}from"./euro-BXN7gcpm.js";import"./pdf-BkPZWjdg.js";import"./radix-CndBjKvF.js";import"./vendor-data-DCPnHH6p.js";import"./dates-ivKbcnBu.js";import"./chevron-down-D_XrtiFJ.js";import"./check-D5Kn9MgH.js";import"./fetchAllSupabase-Bf-jrtGh.js";const re=["#0088FE","#00C49F","#FFBB28","#FF8042","#8884d8","#82ca9d"],v=["Gen","Feb","Mar","Apr","Mag","Giu","Lug","Ago","Set","Ott","Nov","Dic"],E={backgroundColor:"hsl(var(--card))",border:"1px solid hsl(var(--border))",borderRadius:8,color:"hsl(var(--card-foreground))"},Q={color:"hsl(var(--card-foreground))"},R={color:"hsl(var(--card-foreground))"};function hs(){const{cliente:p,loading:oe,isReseller:O}=Ye(),[U,ie]=d.useState([new Date().getFullYear()]),[T,de]=d.useState([]),[_,ce]=d.useState([]),[F,me]=d.useState([]),[xe,W]=d.useState(!0),[l,he]=d.useState(new Date().getFullYear()),[o,ue]=d.useState(null),[X,je]=d.useState("annuale"),[w,pe]=d.useState([]),[f,fe]=d.useState("all");d.useEffect(()=>{p&&(async()=>{const[s,t]=await Promise.all([S.rpc("cliente_stats_available_years",{_reseller_id:p.id}),O?S.from("clienti").select("id, nome").eq("created_by_cliente_id",p.id):Promise.resolve({data:[]})]),a=(s.data||[]).map(n=>n.anno);a.includes(new Date().getFullYear())||a.unshift(new Date().getFullYear()),ie(a.sort((n,c)=>c-n)),pe(t.data||[])})()},[p,O]);const P=d.useMemo(()=>{var s;return f==="all"?null:((s=w.find(t=>t.id===f))==null?void 0:s.nome)||null},[f,w]),D=f==="all"?null:f;d.useEffect(()=>{if(!p)return;W(!0);const s=o?[l,o]:[l];(async()=>{try{const[t,a,n]=await Promise.all([S.rpc("cliente_stats_kpi",{_reseller_id:p.id,_years:s,_sub_cliente_nome:P,_sub_cliente_id:D}),S.rpc("cliente_stats_monthly",{_reseller_id:p.id,_years:s,_sub_cliente_nome:P,_sub_cliente_id:D}),S.rpc("cliente_stats_monthly_by_model",{_reseller_id:p.id,_year:l,_sub_cliente_nome:P,_sub_cliente_id:D})]);de(t.data||[]),ce(a.data||[]),me(n.data||[])}catch(t){console.error("Error fetching stats:",t)}finally{W(!1)}})()},[p,l,o,P]);const r=d.useMemo(()=>T.find(s=>s.anno===l),[T,l]),N=d.useMemo(()=>o?T.find(s=>s.anno===o):void 0,[T,o]),Ne=Number((r==null?void 0:r.n_ordini)||0),q=Number((r==null?void 0:r.fatturato)||0),be=Number((N==null?void 0:N.n_ordini)||0),b=Number((N==null?void 0:N.fatturato)||0),ge=Number((r==null?void 0:r.tap_mq)||0),ye=Number((r==null?void 0:r.tap_pz)||0),ve=Number((r==null?void 0:r.porte_mq)||0),J=Number((r==null?void 0:r.porte_pz)||0),_e=Number((r==null?void 0:r.ven_mq)||0),ee=Number((r==null?void 0:r.ven_pz)||0),M=d.useMemo(()=>{const s=new Map;return F.forEach(t=>{!(t.modello==="Porte a Soffietto"||t.modello==="Veneziane")&&t.pz===0||s.set(t.modello,(s.get(t.modello)||0)+Number(t.mq))}),Array.from(s.entries()).map(([t,a])=>({modello:t,mq:parseFloat(a.toFixed(2))})).sort((t,a)=>a.mq-t.mq)},[F]),Me=d.useMemo(()=>v.map((s,t)=>{const a=t+1,n=_.find(i=>i.anno===l&&i.mese===a),c={mese:s,[l]:Number((n==null?void 0:n.fatturato)||0)};if(o){const i=_.find(g=>g.anno===o&&g.mese===a);c[o]=Number((i==null?void 0:i.fatturato)||0)}return c}),[_,l,o]),Se=d.useMemo(()=>v.map((s,t)=>{const a=t+1,n=_.find(c=>c.anno===l&&c.mese===a);return{mese:s,ordini:Number((n==null?void 0:n.n_ordini)||0)}}),[_,l]),h=d.useMemo(()=>{const s=new Map;return F.forEach(t=>{s.has(t.modello)||s.set(t.modello,Array.from({length:12},()=>({mq:0,pz:0})));const a=s.get(t.modello);a[t.mese-1].mq+=Number(t.mq),a[t.mese-1].pz+=Number(t.pz)}),Array.from(s.entries()).map(([t,a])=>{const n=a.reduce((i,g)=>i+g.mq,0),c=a.reduce((i,g)=>i+g.pz,0);return{modello:t,months:a.map(i=>({mq:parseFloat(i.mq.toFixed(2)),pz:Math.round(i.pz)})),totalMQ:parseFloat(n.toFixed(2)),totalPZ:Math.round(c)}}).sort((t,a)=>a.totalMQ-t.totalMQ)},[F]),ze=()=>{window.print()},Ce=()=>{if(h.length===0)return;const s=["Modello",...v,"Totale MQ","Totale PZ"],t=h.map(a=>[a.modello,...a.months.map(n=>n.mq),a.totalMQ,a.totalPZ]);t.push(["TOTALE",...v.map((a,n)=>parseFloat(h.reduce((c,i)=>c+i.months[n].mq,0).toFixed(2))),parseFloat(h.reduce((a,n)=>a+n.totalMQ,0).toFixed(2)),h.reduce((a,n)=>a+n.totalPZ,0)]),Be(`statistiche_${l}.csv`,s,t)},Y=s=>new Intl.NumberFormat("it-IT",{style:"currency",currency:"EUR"}).format(s);return oe||xe?e.jsx(Ae,{}):p?O?e.jsxs("div",{className:"space-y-6 print-area",children:[e.jsxs("div",{className:"flex items-center justify-between flex-wrap gap-3",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold",children:"Le Tue Statistiche"}),e.jsx("p",{className:"text-muted-foreground",children:"Analisi dei tuoi ordini e acquisti"})]}),e.jsxs("div",{className:"flex gap-2 print:hidden",children:[e.jsxs(ne,{variant:"outline",onClick:Ce,className:"gap-2",children:[e.jsx(Ie,{className:"h-4 w-4"}),"CSV"]}),e.jsxs(ne,{variant:"outline",onClick:ze,className:"gap-2",children:[e.jsx($e,{className:"h-4 w-4"}),"Stampa"]})]})]}),e.jsx(m,{children:e.jsx(x,{className:"pt-6",children:e.jsxs("div",{className:"flex flex-wrap gap-4 items-end",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium",children:"Anno"}),e.jsxs(K,{value:l.toString(),onValueChange:s=>he(parseInt(s)),children:[e.jsx(Z,{className:"w-[120px]",children:e.jsx(G,{})}),e.jsx(H,{children:U.map(s=>e.jsx(C,{value:s.toString(),children:s},s))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium",children:"Confronta con"}),e.jsxs(K,{value:(o==null?void 0:o.toString())||"none",onValueChange:s=>ue(s==="none"?null:parseInt(s)),children:[e.jsx(Z,{className:"w-[140px]",children:e.jsx(G,{placeholder:"Nessuno"})}
1),e.jsxs(H,{children:[e.jsx(C,{value:"none",children:"Nessuno"}),U.filter(s=>s!==l).map(s=>e.jsx(C,{value:s.toString(),children:s},s))]})]})]}),w.length>0&&e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium",children:"Cliente"}),e.jsxs(K,{value:f,onValueChange:fe,children:[e.jsx(Z,{className:"w-[180px]",children:e.jsx(G,{})}),e.jsxs(H,{children:[e.jsx(C,{value:"all",children:"Tutti i clienti"}),w.map(s=>e.jsx(C,{value:s.id,children:s.nome},s.id))]})]})]}),e.jsx(Oe,{value:X,onValueChange:s=>je(s),children:e.jsxs(De,{children:[e.jsxs(le,{value:"annuale",className:"gap-2",children:[e.jsx(ke,{className:"h-4 w-4"}),"Annuale"]}),e.jsxs(le,{value:"mensile",className:"gap-2",children:[e.jsx(Ve,{className:"h-4 w-4"}),"Mensile"]})]})})]})})}),e.jsxs("div",{className:"grid gap-4 md:grid-cols-4",children:[e.jsxs(m,{children:[e.jsx(u,{className:"pb-2",children:e.jsxs(j,{className:"text-sm font-medium text-muted-foreground flex items-center gap-2",children:[e.jsx(Le,{className:"h-4 w-4"}),"Ordini ",l]})}),e.jsxs(x,{children:[e.jsx("div",{className:"text-3xl font-bold",children:Ne}),o&&e.jsxs("p",{className:"text-xs text-muted-foreground mt-1",children:["vs ",be," nel ",o]})]})]}),e.jsxs(m,{children:[e.jsx(u,{className:"pb-2",children:e.jsxs(j,{className:"text-sm font-medium text-muted-foreground flex items-center gap-2",children:[e.jsx(Ke,{className:"h-4 w-4"}),"Fatturato ",l]})}),e.jsxs(x,{children:[e.jsx("div",{className:"text-2xl font-bold",children:Y(q)}),o&&e.jsxs("p",{className:"text-xs text-muted-foreground mt-1",children:["vs ",Y(b)," nel ",o]})]})]}),e.jsxs(m,{children:[e.jsx(u,{className:"pb-2",children:e.jsx(j,{className:"text-sm font-medium text-muted-foreground",children:"Tapparelle"})}),e.jsxs(x,{children:[e.jsxs("div",{className:"text-3xl font-bold",children:[ge.toFixed(2)," MQ"]}),e.jsxs("p",{className:"text-sm text-muted-foreground mt-1",children:[Math.round(ye)," pezzi"]})]})]}),J>0&&e.jsxs(m,{children:[e.jsx(u,{className:"pb-2",children:e.jsx(j,{className:"text-sm font-medium text-muted-foreground",children:"Porte a Soffietto"})}),e.jsxs(x,{children:[e.jsxs("div",{className:"text-3xl font-bold",children:[ve.toFixed(2)," MQ"]}),e.jsxs("p",{className:"text-sm text-muted-foreground mt-1",children:[Math.round(J)," pezzi"]})]})]}),ee>0&&e.jsxs(m,{children:[e.jsx(u,{className:"pb-2",children:e.jsx(j,{className:"text-sm font-medium text-muted-foreground",children:"Veneziane"})}),e.jsxs(x,{children:[e.jsxs("div",{className:"text-3xl font-bold",children:[_e.toFixed(2)," MQ"]}),e.jsxs("p",{className:"text-sm text-muted-foreground mt-1",children:[Math.round(ee)," pezzi"]})]})]}),o&&e.jsxs(m,{children:[e.jsx(u,{className:"pb-2",children:e.jsx(j,{className:"text-sm font-medium text-muted-foreground",children:"Variazione Fatturato"})}),e.jsx(x,{children:e.jsx("div",{className:`text-3xl font-bold ${q>=b?"text-green-600":"text-red-600"}`,children:b>0?`${q>=b?"+":""}${((q-b)/b*100).toFixed(1)}%`:"N/A"})})]})]}),X==="annuale"?e.jsxs("div",{className:"grid gap-6 lg:grid-cols-2",children:[e.jsxs(m,{children:[e.jsxs(u,{children:[e.jsx(j,{children:"Tapparelle per Modello"}),e.jsxs(z,{children:["Anno ",l]})]}),e.jsx(x,{children:e.jsx("div",{className:"h-[300px]",children:M.length>0?e.jsx(A,{width:"100%",height:"100%",children:e.jsxs(se,{data:M,layout:"vertical",children:[e.jsx(B,{strokeDasharray:"3 3"}),e.jsx(I,{type:"number"}),e.jsx($,{dataKey:"modello",type:"category",width:100,tick:{fontSize:12}}),e.jsx(L,{contentStyle:E,labelStyle:Q,itemStyle:R}),e.jsx(te,{dataKey:"mq",fill:"hsl(var(--primary))",name:"MQ"})]})}):e.jsx("div",{className:"flex items-center justify-center h-full text-muted-foreground",children:"Nessun dato disponibile"})})})]}),e.jsxs(m,{children:[e.jsxs(u,{children:[e.jsx(j,{children:"Distribuzione Modelli"}),e.jsxs(z,{children:["Anno ",l]})]}),e.jsx(x,{children:e.jsx("div",{className:"h-[300px]",children:M.length>0?e.jsx(A,{width:"100%",height:"100%",children:e.jsxs(Te,{children:[e.jsx(Fe,{data:M,cx:"50%",cy:"50%",labelLine:!1,label:({modello:s,percent:t})=>`${s} (${(t*100).toFixed(0)}%)`,outerRadius:100,fill:"#8884d8",dataKey:"mq",children:M.map((s,t)=>e.jsx(we,{fill:re[t%re.length]},`cell-${t}`))}),e.jsx(L,{contentStyle:E,labelStyle:Q,itemStyle:R})]})}):e.jsx("div",{className:"flex items-center justify-center h-full text-muted-foreground",children:"Nessun dato disponibile"})})})]})]}):e.jsxs("div",{className:"grid gap-6",children:[e.jsxs(m,{children:[e.jsxs(u,{children:[e.jsx(j,{children:"Andamento Fatturato Mensile"}),e.jsx(z,{children:o?`Confronto ${l} vs ${o}`:`Anno ${l}`})]}),e.jsx(x,{children:e.jsx("div",{className:"h-[400px]",children:e.jsx(A,{width:"100%",height:"100%",children:e.jsxs(Pe,{data:Me,children:[e.jsx(B,{strokeDasharray:"3 3"}),e.jsx(I,{dataKey:"mese"}),e.jsx($,{tickFormatter:s=>`â¬${(s/1e3).toFixed(0)}k`}),e.jsx(L,{contentStyle:E,labelStyle:Q,itemStyle:R,formatter:s=>Y(s)}),e.jsx(qe,{}),e.jsx(ae,{type:"monotone",dataKey:l,stroke:"hsl(var(--primary))",strokeWidth:2,name:l.toString()}),o&&e.jsx(ae,{type:"monotone",dataKey:o,stroke:"#82ca9d",strokeWidth:2,strokeDasharray:"5 5",name:o.toString()})]})})})})]}),e.jsxs(m,{children:[e.jsxs(u,{children:[e.jsx(j,{children:"Dettaglio Produzione per Modello"}),e.jsxs(z,{children:["MQ per mese - Anno ",l]})]}),e.jsx(x,{children:h.length>0?e.jsx("div",{className:"overflow-x-auto",children:e.jsxs(Ee,{children:[e.jsx(Qe,{children:e.jsxs(k,{children:[e.jsx(V,{className:"sticky left-0 bg-background z-10 min-w-[120px]",children:"Modello"}),v.map(s=>e.jsx(V,{className:"text-center min-w-[60px]",children:s},s)),e.jsx(V,{className:"text-center font-bold min-w-[70px]",children:"Totale"})]})}),e.jsxs(Re,{children:[h.map(s=>e.jsxs(k,{children:[e.jsx(y,{className:"sticky left-0 bg-background z-10 font-medium",children:s.modello}),s.months.map((t,a)=>e.jsx(y,{className:"text-center text-sm",children:t.mq>0?e.jsxs("div",{children:[e.jsx("span",{className:"font-medium",children:t.mq}),t.pz>0&&e.jsxs("span",{className:"text-xs text-muted-foreground block",children:[t.pz," pz"]})]}):"-"},a)),e.jsx(y,{className:"text-center font-bold",children:e.jsxs("div",{children:[e.jsx("span",{children:s.totalMQ}),s.totalPZ>0&&e.jsxs("span",{className:"text-xs text-muted-foreground block",children:[s.totalPZ," pz"]})]})})]},s.modello)),e.jsxs(k,{className:"bg-muted/50 font-bold",children:[e.jsx(y,{className:"sticky left-0 bg-muted/50 z-10 font-bold",children:"TOTALE"}),v.map((s,t)=>{const a=h.reduce((c,i)=>c+i.months[t].mq,0),n=h.reduce((c,i)=>c+i.months[t].pz,0);return e.jsx(y,{className:"text-center",children:a>
10?e.jsxs("div",{children:[e.jsx("span",{children:a.toFixed(2)}),n>0&&e.jsxs("span",{className:"text-xs text-muted-foreground block",children:[n," pz"]})]}):"-"},t)}),e.jsx(y,{className:"text-center",children:e.jsxs("div",{children:[e.jsx("span",{children:h.reduce((s,t)=>s+t.totalMQ,0).toFixed(2)}),e.jsxs("span",{className:"text-xs text-muted-foreground block",children:[h.reduce((s,t)=>s+t.totalPZ,0)," pz"]})]})})]})]})]})}):e.jsx("div",{className:"text-center py-8 text-muted-foreground",children:"Nessun dato disponibile"})})]}),e.jsxs(m,{children:[e.jsxs(u,{children:[e.jsx(j,{children:"Numero Ordini per Mese"}),e.jsxs(z,{children:["Anno ",l]})]}),e.jsx(x,{children:e.jsx("div",{className:"h-[300px]",children:e.jsx(A,{width:"100%",height:"100%",children:e.jsxs(se,{data:Se,children:[e.jsx(B,{strokeDasharray:"3 3"}),e.jsx(I,{dataKey:"mese"}),e.jsx($,{}),e.jsx(L,{contentStyle:E,labelStyle:Q,itemStyle:R}),e.jsx(te,{dataKey:"ordini",fill:"hsl(var(--primary))",name:"Ordini"})]})})})})]})]})]}):e.jsx("div",{className:"text-center py-8",children:e.jsx("p",{className:"text-muted-foreground",children:"Questa sezione è riservata ai negozi."})}):e.jsx("div",{className:"text-center py-8",children:e.jsx("p",{className:"text-muted-foreground",children:"Nessun profilo cliente associato."})})}export{hs as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.