@accounter/client
Version:
3 lines (2 loc) • 10.4 kB
JavaScript
import{dX as C,j as e,ah as L,R as U,ay as a,H as b,gg as y,r as u,b$ as N,gh as D,gi as k,K as R,gj as A,u as $,F as E,g9 as T,B as O,gk as P,c as z,gl as M}from"./index-DRqooPz9.js";import{P as Y}from"./page-layout-Dp9Tr43G.js";const d=(s,o)=>C(o,{minimumFractionDigits:0}).format(s);function B(s){return{id:s.id,chargeId:s.chargeId,date:s.date,description:s.description??"",reference:s.reference??"",amountILS:s.revenueLocal.raw,amountUSD:s.revenueDefaultForeign.raw,originalAmount:s.revenueOriginal.formatted}}const H=({recordData:s})=>{const o=b(y,s),r=B(o);return e.jsx("div",{className:"p-2 bg-background rounded-lg border border-border/30 text-sm",children:e.jsxs(L,{to:U.CHARGES.DETAIL(r.chargeId),target:"_blank",rel:"noreferrer",onClick:g=>g.stopPropagation(),className:"flex items-start justify-between gap-2",children:[e.jsxs("div",{className:"flex-1",children:[e.jsx("p",{className:"font-medium text-foreground",children:r.reference}),e.jsx("p",{className:"font-medium text-foreground",children:r.description}),e.jsxs("div",{className:"flex gap-4 mt-1 text-xs text-muted-foreground",children:[e.jsx("span",{children:r.chargeId}),e.jsx("span",{children:r.date})]})]}),e.jsxs("div",{className:"invisible md:visible md:text-right flex-shrink-0",children:[e.jsx("p",{className:"text-sm font-medium text-foreground",children:d(r.amountILS,a.Ils)}),e.jsx("p",{className:"text-xs text-muted-foreground",children:d(r.amountUSD,a.Usd)}),e.jsx("p",{className:"text-xs text-muted-foreground",children:r.originalAmount})]}),e.jsx("div",{className:"md:invisible ml-2 text-right",children:e.jsx("p",{className:"text-xs font-medium text-foreground",children:d(r.amountILS,a.Ils)})})]})})};function V(s){return{id:s.id,name:s.name,revenueILS:s.revenueLocal.raw,revenueUSD:s.revenueDefaultForeign.raw,records:s.records.map(o=>({id:o.id,data:o}))}}const q=({clientData:s})=>{const[o,r]=u.useState(!1),g=b(k,s),t=V(g),v=()=>{r(l=>!l)},x=u.useMemo(()=>t.records.slice().sort((l,i)=>l.data.date.localeCompare(i.data.date)),[t.records]);return e.jsxs("div",{children:[e.jsxs("button",{onClick:v,className:"w-full flex items-center justify-between p-2 bg-background rounded-lg border border-border/50 hover:bg-accent/5 transition-colors",children:[e.jsxs("div",{className:"flex items-center gap-3 flex-1",children:[o?e.jsx(N,{className:"size-4 text-muted-foreground flex-shrink-0"}):e.jsx(D,{className:"size-4 text-muted-foreground flex-shrink-0"}),e.jsxs("div",{className:"text-left",children:[e.jsx("p",{className:"font-medium text-foreground text-sm",children:t.name}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[t.records?.length||0," record",t.records?.length===1?"":"s"]})]})]}),e.jsxs("div",{className:"hidden md:grid grid-cols-2 gap-4 text-right flex-shrink-0",children:[e.jsx("div",{children:e.jsx("p",{className:"text-xs text-muted-foreground",children:d(t.revenueILS,a.Ils)})}),e.jsx("div",{children:e.jsx("p",{className:"text-xs text-muted-foreground",children:d(t.revenueUSD,a.Usd)})})]}),e.jsxs("div",{className:"md:hidden ml-4 text-right",children:[e.jsx("p",{className:"text-xs font-medium text-foreground",children:d(t.revenueILS,a.Ils)}),e.jsx("p",{className:"text-xs text-muted-foreground",children:d(t.revenueUSD,a.Usd)})]})]}),o&&x&&e.jsx("div",{className:"mt-3 ml-2 space-y-3 border-l-2 border-border/50 pl-2",children:x.map(l=>e.jsx(H,{recordData:l.data},l.id))})]})};function G(s){return{code:s.code,name:s.name,revenues:{ILS:s.revenueLocal.raw,USD:s.revenueDefaultForeign.raw},clients:s.clients.map(o=>({id:o.id,data:o}))}}const K=({countryData:s})=>{const[o,r]=u.useState(!1),g=b(A,s),t=G(g),v=()=>{r(i=>!i)},x={ILS:t.revenues.ILS,USD:t.revenues.USD},l=u.useMemo(()=>t.clients.slice().sort((i,j)=>j.data.revenueDefaultForeign.raw-i.data.revenueDefaultForeign.raw),[t.clients]);return e.jsxs(R,{className:"border border-border",children:[e.jsxs("button",{onClick:()=>v(),className:"w-full px-4 py-2 hover:bg-accent/5 transition-colors flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-1",children:[o?e.jsx(N,{className:"size-5 text-muted-foreground flex-shrink-0"}):e.jsx(D,{className:"size-5 text-muted-foreground flex-shrink-0"}),e.jsxs("div",{className:"text-left",children:[e.jsx("p",{className:"font-semibold text-foreground text-lg",children:t.name}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:[t.clients.length," clients"]})]})]}),e.jsxs("div",{className:"hidden md:grid grid-cols-2 gap-4 text-right flex-shrink-0",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground mb-1",children:"ILS"}),e.jsx("p",{className:"font-semibold text-foreground",children:d(t.revenues.ILS,a.Ils)})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground mb-1",children:"USD"}),e.jsx("p",{className:"font-semibold text-foreground",children:d(t.revenues.USD,a.Usd)})]})]})]}),e.jsxs("div",{className:"md:hidden px-2 py-2 bg-accent/5 border-t border-border flex gap-4",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground mb-1",children:"ILS"}),e.jsx("p",{className:"font-semibold text-foreground",children:d(t.revenues.ILS,a.Ils)})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground mb-1",children:"USD"}),e.jsx("p",{className:"font-semibold text-foreground",children:d(t.revenues.USD,a.Usd)})]})]}),o&&e.jsx("div",{className:"border-t border-border",children:e.jsxs("div",{className:"space-y-2 p-3 bg-muted/30",children:[l.map(i=>e.jsx(q,{clientData:i.data},i.id)),e.jsx("div",{className:"pt-2 border-t border-border/50 mt-4",children:e.jsxs("div",{className:"flex items-center justify-between p-2",children:[e.jsx("p",{className:"font-semibold text-foreground text-sm",children:"Clients Subtotal"}),e.jsxs("div",{className:"hidden md:grid grid-cols-2 gap-4 text-right",children:[e.jsx("div",{children:e.jsx("p",{className:"font-semibold text-foreground",children:d(x.ILS,a.Ils)})}),e.jsx("div",{children:e.jsx("p",{className:"font-semibold text-foreground",children:d(x.USD,a.Usd)})})]}),e.jsxs("div",{className:"md:hidden ml-4 text-right",children:[e.jsx("p",{className:"text-sm font-semibold text-foreground",children:d(x.ILS,a.Ils)}),e.jsx("p",{className:"text-xs text-muted-foreground",children:d(x.USD,a.Usd)})]})]})})]})})]},t.code)},_=()=>{const[s,o]=u.useState(new Date().getFullYear()-1),[{data:r,fetching:g}]=$({query:M,variables:{filters:{year:s}}}),{setFiltersContext:t}=u.useContext(E),v=u.useMemo(()=>`Annual Revenue Report for ${s}`,[s]),{totalClients:x,totalRevenueIls:l,totalRevenueUsd:i}=u.useMemo(()=>{const n={totalClients:0,totalRevenueIls:0,totalRevenueUsd:0};return r?.annualRevenueReport.countries.map(p=>{n.totalClients+=p.clients.length,n.totalRevenueIls+=p.revenueLocal.raw,n.totalRevenueUsd+=p.revenueDefaultForeign.raw}),n},[r?.annualRevenueReport.countries]),j=u.useCallback(()=>{const n=[];n.push("Country,Client,charge ID,Date,Description,Revenue ILS,Revenue USD"),n.push(`Annual,,,,TOTAL,${l.toFixed(2)},${i.toFixed(2)}`),r?.annualRevenueReport.countries.sort((m,c)=>c.revenueDefaultForeign.raw-m.revenueDefaultForeign.raw).map(m=>{n.push(`${m.name},,,,TOTAL,${m.revenueLocal.raw.toFixed(2)},${m.revenueDefaultForeign.raw.toFixed(2)}`),m.clients.sort((c,h)=>h.revenueDefaultForeign.raw-c.revenueDefaultForeign.raw).map(c=>{c.records&&c.records.length>0?(n.push(`${m.name},${c.name},,,TOTAL,${c.revenueLocal.raw.toFixed(2)},${c.revenueDefaultForeign.raw.toFixed(2)}`),c.records.sort((h,I)=>h.date.localeCompare(I.date)).map(h=>{n.push(`${m.name},${c.name},${h.chargeId},${h.date},${h.description},${h.revenueLocal.raw.toFixed(2)},${h.revenueDefaultForeign.raw.toFixed(2)}`)})):n.push(`${m.name},${c.name},,,,,`)})});const p=n.join(`
`),w=new Blob([p],{type:"text/csv;charset=utf-8;"}),f=document.createElement("a"),F=URL.createObjectURL(w);f.setAttribute("href",F),f.setAttribute("download",`annual-revenue-${s}.csv`),f.style.visibility="hidden",document.body.appendChild(f),f.click(),document.body.removeChild(f)},[r,s]);u.useEffect(()=>{t(e.jsxs("div",{className:"flex items-center justify-end gap-2",children:[e.jsx(T,{value:new Date(s,0,1),onChange:n=>n&&o(n?.getFullYear()),popoverProps:{withinPortal:!0},minDate:new Date(2010,0,1),maxDate:new Date}),e.jsxs(O,{onClick:j,variant:"outline",size:"sm",className:"flex items-center gap-2 bg-transparent",children:[e.jsx(P,{className:"h-4 w-4"}),e.jsx("span",{className:"hidden sm:inline",children:"Export CSV"})]})]}))},[s,g,t,j]);const S=u.useMemo(()=>r?.annualRevenueReport.countries.slice().sort((n,p)=>p.revenueDefaultForeign.raw-n.revenueDefaultForeign.raw),[r]);return e.jsx(Y,{title:"Annual Revenue Report",description:v,children:g?e.jsx(z,{}):e.jsx("div",{className:"min-h-screen bg-background",children:e.jsx("main",{className:"container mx-auto px-2 py-2 max-w-7xl",children:e.jsxs("div",{className:"space-y-2",children:[e.jsxs(R,{className:"border border-border bg-accent/10",children:[e.jsxs("div",{className:"w-full px-4 py-2 pl-12 transition-colors flex items-center justify-between",children:[e.jsx("div",{className:"flex items-center gap-2 flex-1",children:e.jsxs("div",{className:"text-left",children:[e.jsx("p",{className:"font-semibold text-foreground text-lg",children:"Total Revenue"}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:[x," clients"]})]})}),e.jsxs("div",{className:"hidden md:grid grid-cols-2 gap-4 text-right flex-shrink-0",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground mb-1",children:"ILS"}),e.jsx("p",{className:"font-semibold text-foreground",children:d(l,a.Ils)})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground mb-1",children:"USD"}),e.jsx("p",{className:"font-semibold text-foreground",children:d(i,a.Usd)})]})]})]}),e.jsxs("div",{className:"md:hidden px-2 py-2 bg-accent/5 border-t border-border flex gap-4",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground mb-1",children:"ILS"}),e.jsx("p",{className:"font-semibold text-foreground",children:d(l,a.Ils)})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground mb-1",children:"USD"}),e.jsx("p",{className:"font-semibold text-foreground",children:d(i,a.Usd)})]})]})]}),S?.map(n=>e.jsx(K,{countryData:n},n.id))]})})})})};export{_ as AnnualRevenueReport};