UNPKG

@accounter/client

Version:
4 lines (3 loc) 11.4 kB
import{j as e,m as S,aR as H,ah as z,R as F,a8 as D,av as d,bx as L,bQ as h,bT as B,c2 as G,r as c,dp as Z,u as ee,e7 as te,bY as ne,aV as f,bO as V,bP as k,bR as O,bS as P,e8 as se,I as ae,bZ as re,b_ as oe,B as ie,b$ as le,c0 as ce,c1 as me,ap as ue,aq as de,ar as ge,as as xe,at as fe,P as he,c4 as pe,c3 as we,c5 as je,c6 as be}from"./index-CDv8MTrU.js";import{D as p}from"./data-table-column-header-DF5fndn9.js";import{D as Ce}from"./download-csv-button-FxjssS8w.js";const Se=({amount:t})=>!t||t.raw===0?e.jsx("span",{className:"text-sm text-gray-400",children:"—"}):e.jsx("span",{className:S("text-sm font-medium whitespace-nowrap",t.raw>0?"text-green-700":"text-red-600"),children:t.formatted}),Ne=({balance:t,currency:r})=>{if(t===0)return e.jsx("span",{className:"text-sm text-gray-400",children:"—"});const n=H(t,r);return e.jsx("span",{className:S("text-sm font-medium whitespace-nowrap",t>0?"text-green-700":"text-red-600"),children:n})},ye=({business:t})=>e.jsx(z,{to:F.BUSINESSES.DETAIL(t.id),target:"_blank",rel:"noopener noreferrer",onClick:r=>r.stopPropagation(),className:"inline-flex items-center font-semibold whitespace-nowrap",children:t.name}),ve=({counterAccount:t})=>t?e.jsx(z,{to:F.BUSINESSES.DETAIL(t.id),target:"_blank",rel:"noopener noreferrer",onClick:r=>r.stopPropagation(),className:"inline-flex items-center font-semibold whitespace-nowrap text-sm",children:t.name}):e.jsx("span",{className:"text-sm text-gray-400",children:"—"}),Ae=({row:t,currency:r})=>{const n=t.foreignAmount&&t.foreignAmount.currency===r?t.foreignAmount:null;return!n||n.raw===0?e.jsx("span",{className:"text-sm text-gray-400",children:"—"}):e.jsx("span",{className:S("text-sm font-medium whitespace-nowrap",n.raw>0?"text-green-700":"text-red-600"),children:n.formatted})},Ie=({row:t,currency:r})=>{const n=`${r.toLowerCase()}Balance`,a=t[n]??0;if(a===0)return e.jsx("span",{className:"text-sm text-gray-400",children:"—"});const l=H(a,r);return e.jsx("span",{className:S("text-sm font-medium whitespace-nowrap",a>0?"text-green-700":"text-red-600"),children:l})},De=({date:t})=>t?e.jsx("span",{className:"text-sm font-medium whitespace-nowrap",children:D(new Date(t),"dd/MM/yy")}):e.jsx("span",{className:"text-sm text-gray-400",children:"—"}),Be=()=>[{accessorKey:"business.name",id:"businessName",header:"Business Name",cell:({row:t})=>e.jsx(ye,{business:t.original.business}),enableSorting:!1,enableHiding:!1},{accessorKey:"invoiceDate",id:"date",header:({column:t})=>e.jsx(p,{column:t,title:"Date"}),cell:({row:t})=>e.jsx(De,{date:t.original.invoiceDate}),sortingFn:"datetime"},{accessorKey:"amount.raw",id:"amount",header:({column:t})=>e.jsx(p,{column:t,title:"Amount"}),cell:({row:t})=>e.jsx(Se,{amount:t.original.amount}),sortingFn:"basic"},{accessorKey:"ilsBalance",id:"ilsBalance",header:({column:t})=>e.jsx(p,{column:t,title:"Amount Balance"}),cell:({row:t})=>e.jsx(Ne,{balance:t.original.ilsBalance,currency:d.Ils}),sortingFn:"basic"}],Fe=()=>Object.values(d).filter(n=>n!==d.Ils).sort((n,a)=>{const l=L.includes(n),m=L.includes(a);return l&&!m?-1:!l&&m?1:n.localeCompare(a)}).flatMap(n=>[{id:`${n.toLowerCase()}Amount`,accessorFn:a=>a.foreignAmount?.currency===n?a.foreignAmount.raw:null,header:({column:a})=>e.jsx(p,{column:a,title:`${n} Amount`}),cell:({row:a})=>e.jsx(Ae,{row:a.original,currency:n}),sortingFn:"basic",enableHiding:!0},{id:`${n.toLowerCase()}Balance`,accessorFn:a=>a[`${n.toLowerCase()}Balance`]??null,header:({column:a})=>e.jsx(p,{column:a,title:`${n} Balance`}),cell:({row:a})=>e.jsx(Ie,{row:a.original,currency:n}),sortingFn:"basic",enableHiding:!0}]),$e=()=>[{accessorKey:"reference",id:"reference",header:"Reference",cell:({row:t})=>e.jsx("p",{className:"text-sm whitespace-nowrap",children:t.original.reference??""}),enableSorting:!1},{accessorKey:"details",id:"details",header:"Details",cell:({row:t})=>e.jsx("p",{className:"text-sm whitespace-nowrap",children:t.original.details??""}),enableSorting:!1},{accessorKey:"counterAccount.name",id:"counterAccount",header:"Counter Account",cell:({row:t})=>e.jsx(ve,{counterAccount:t.original.counterAccount}),enableSorting:!1}],Me=()=>[...Be(),...Fe(),...$e()],Re=t=>{const r={};for(const n of Object.values(d).filter(a=>a!==d.Ils)){const a=t.has(n),l=`${n.toLowerCase()}Amount`,m=`${n.toLowerCase()}Balance`;a||(r[l]=!1,r[m]=!1)}return r},Te=({row:t})=>{const r=n=>{n.stopPropagation(),window.open(F.CHARGES.DETAIL(t.original.chargeId),"_blank","noopener noreferrer")};return e.jsx(h,{onClick:r,className:"cursor-pointer hover:bg-gray-50 transition-colors",children:t.getVisibleCells().map(n=>e.jsx(B,{children:G(n.column.columnDef.cell,n.getContext())},n.id))})},Ee=({ledgerRecords:t,businessName:r,fromDate:n,toDate:a})=>{const l=c.useCallback(async()=>{const m=_e(t);return{fileName:`business_${r}_ledger_records${n?`_${n}`:""}${a?`_${a}`:""}`,fileContent:m}},[t,r,n,a]);return e.jsx(Ce,{createFileVariables:l})},_e=t=>{let r="";const n=Array.from(new Set(t.filter(a=>a.foreignAmount?.currency).map(a=>a.foreignAmount.currency)));r+=`Sorting Date,Date,Amount,Amount Balance,${Le(n)}Reference,Details,Counter Account\r `;for(const a of t){const l=ke(a,n);r+=l}return r};function Le(t){let r="";return t.map(n=>{const a=Z(n);r+=`${n}(${a}) Amount,${n}(${a}) Balance,`}),r}function Ve(t,r){let n="";return r.map(a=>{t.foreignAmount&&t.foreignAmount.currency===a?n+=`${t.foreignAmount.raw},${t[`${a.toLowerCase()}Balance`]??0},`:n+=",,"}),n}function ke(t,r){let n="";const a=t.invoiceDate?D(new Date(t.invoiceDate),"yyy-MM-dd"):null,l=t.invoiceDate?D(new Date(t.invoiceDate),"dd/MM/yy"):null,m=t.amount.raw,{ilsBalance:x,reference:w,details:N}=t,y=t.counterAccount?.name??"";return n+=`${a},${l},${m},${x},${Ve(t,r)}${I(w??"")},${I(N??"")},${I(y)},\r `,n}function I(t){return t===""?"":Number.isNaN(Number(t))?String(t).replace(/"/g,'""').replace(/,/g,"."):Number(t).toFixed(2).toString()}const K="businessLedger",C={SORTING:`${K}_sorting`,COLUMN_VISIBILITY:`${K}_columnVisibility`};function He({businessID:t,filter:r}){const[n,a]=c.useState(()=>{try{const s=localStorage.getItem(C.SORTING);return s?JSON.parse(s):[]}catch{return[]}}),[l,m]=c.useState([]),[x,w]=c.useState(""),[N,y]=c.useState({pageIndex:0,pageSize:50});c.useEffect(()=>{try{localStorage.setItem(C.SORTING,JSON.stringify(n))}catch(s){console.warn("Failed to save sorting to localStorage:",s)}},[n]);const{fromDate:U,ownerIds:Y,toDate:J}=r??{},[{data:v,fetching:q}]=ee({query:te,variables:{filters:{fromDate:U,ownerIds:Y,toDate:J,businessIDs:[t]}}}),g=c.useMemo(()=>v?.businessTransactionsFromLedgerRecords.__typename==="CommonError"?[]:v?.businessTransactionsFromLedgerRecords.businessTransactions.sort((s,o)=>s.invoiceDate>o.invoiceDate?1:-1)??[],[v]),$=c.useMemo(()=>{const s=[];for(let o=0;o<g.length;o++){const{__typename:W,...u}=g[o],X=o===0?u.amount.raw:(s[o-1].ilsBalance??0)+u.amount.raw,E={};Object.values(d).map(b=>{if(b!==d.Ils){const _=`${b.toLowerCase()}Balance`;E[_]=o===0?(u.foreignAmount?.currency===b?u.foreignAmount?.raw:0)??0:(s[o-1]?.[_]??0)+(u.foreignAmount?.currency===b?u.foreignAmount?.raw:0)}}),s.push({...u,ilsBalance:X,...E})}return s},[g]),M=c.useMemo(()=>new Set(g.filter(s=>s.foreignAmount?.currency).map(s=>s.foreignAmount.currency)),[g]),R=Me(),j=c.useMemo(()=>Re(M),[M]),[A,T]=c.useState(()=>{try{const s=localStorage.getItem(C.COLUMN_VISIBILITY);if(s){const o=JSON.parse(s);return{...j,...o}}}catch{}return j});c.useEffect(()=>{try{localStorage.setItem(C.COLUMN_VISIBILITY,JSON.stringify(A))}catch(s){console.warn("Failed to save column visibility to localStorage:",s)}},[A]),c.useEffect(()=>{T(s=>({...j,...s}))},[j]);const i=ne({data:$,columns:R,getCoreRowModel:be(),getSortedRowModel:je(),getFilteredRowModel:we(),getPaginationRowModel:pe(),onSortingChange:a,onColumnFiltersChange:m,onColumnVisibilityChange:T,onGlobalFilterChange:w,onPaginationChange:y,globalFilterFn:"includesString",state:{sorting:n,columnFilters:l,columnVisibility:A,globalFilter:x,pagination:N}}),Q=g[0]?.business.name??"unknown";return q?e.jsxs("div",{className:"flex flex-col gap-4 max-w-[90vw] m-2",children:[e.jsxs("div",{className:"flex items-center justify-between gap-4",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx(f,{className:"h-10 w-64"}),e.jsx(f,{className:"h-10 w-32"})]}),e.jsx(f,{className:"size-10"})]}),e.jsx("div",{className:"rounded-md border overflow-x-auto",children:e.jsxs(V,{children:[e.jsx(k,{children:e.jsx(h,{children:Array.from({length:8}).map((s,o)=>e.jsx(O,{children:e.jsx(f,{className:"h-4 w-24"})},o))})}),e.jsx(P,{children:Array.from({length:5}).map((s,o)=>e.jsx(h,{children:Array.from({length:8}).map((W,u)=>e.jsx(B,{children:e.jsx(f,{className:"h-4 w-full"})},u))},o))})]})})]}):e.jsxs("div",{className:"flex flex-col gap-4 max-w-[90vw] m-2",children:[e.jsxs("div",{className:"flex items-center justify-between gap-4 flex-wrap",children:[e.jsxs("div",{className:"flex items-center gap-4 flex-wrap",children:[e.jsxs("div",{className:"relative flex-1 min-w-[200px] max-w-sm",children:[e.jsx(se,{className:"absolute left-2.5 top-2.5 h-4 w-4 text-gray-500"}),e.jsx(ae,{placeholder:"Search reference, details, business...",value:x??"",onChange:s=>w(s.target.value),className:"pl-8"})]}),e.jsxs(re,{children:[e.jsx(oe,{asChild:!0,children:e.jsxs(ie,{variant:"outline",size:"sm",children:[e.jsx(le,{className:"mr-2 h-4 w-4"}),"Columns"]})}),e.jsx(ce,{align:"end",className:"max-h-96 overflow-y-auto",children:i.getAllColumns().filter(s=>s.getCanHide()).map(s=>e.jsx(me,{className:"capitalize",checked:s.getIsVisible(),onCheckedChange:o=>s.toggleVisibility(!!o),children:s.id},s.id))})]})]}),e.jsx(Ee,{ledgerRecords:$,businessName:Q,fromDate:r?.fromDate??void 0,toDate:r?.toDate??void 0})]}),e.jsx("div",{className:"rounded-md border overflow-x-auto",children:e.jsxs(V,{children:[e.jsx(k,{className:"sticky top-0 z-20 bg-white border-b",children:i.getHeaderGroups().map(s=>e.jsx(h,{children:s.headers.map(o=>e.jsx(O,{colSpan:o.colSpan,children:o.isPlaceholder?null:G(o.column.columnDef.header,o.getContext())},o.id))},s.id))}),e.jsx(P,{children:i.getRowModel().rows?.length?i.getRowModel().rows.map(s=>e.jsx(Te,{row:s},s.id)):e.jsx(h,{children:e.jsx(B,{colSpan:R.length,className:"h-24 text-center",children:"No transactions found."})})})]})}),e.jsxs("div",{className:"flex items-center justify-between gap-4 flex-wrap",children:[e.jsx("div",{className:"flex items-center gap-2",children:e.jsxs("p",{className:"text-sm text-gray-700",children:["Showing"," ",i.getState().pagination.pageIndex*i.getState().pagination.pageSize+1," to"," ",Math.min((i.getState().pagination.pageIndex+1)*i.getState().pagination.pageSize,i.getFilteredRowModel().rows.length)," ","of ",i.getFilteredRowModel().rows.length," transactions"]})}),e.jsxs("div",{className:"flex items-center gap-6",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("p",{className:"text-sm font-medium",children:"Rows per page:"}),e.jsxs(ue,{value:String(i.getState().pagination.pageSize),onValueChange:s=>{i.setPageSize(Number(s))},children:[e.jsx(de,{className:"h-8 w-[70px]",children:e.jsx(ge,{placeholder:i.getState().pagination.pageSize})}),e.jsx(xe,{side:"top",children:[25,50,100,200,500].map(s=>e.jsx(fe,{value:String(s),children:s},s))})]})]}),e.jsx(he,{className:"w-fit mx-0",currentPageIndex:i.getState().pagination.pageIndex,totalPages:i.getPageCount(),onChange:s=>i.setPageIndex(s)})]})]})]})}export{He as B};