@accounter/client
Version:
Accounter client application
1 lines • 11.7 kB
JavaScript
import{s as e}from"./dist-ypP48_Ax.js";import{i as t,o as n,t as r}from"./utils-CbS-oR5I.js";import{s as i,t as a}from"./routes-Cawu7UJi.js";import{$n as o,Ct as s,It as c,Qn as l,Zn as u,n as ee,ur as d}from"./common-D-vYt_Yl.js";import{a as f,i as p,n as m,pt as te,r as ne,s as h,t as g}from"./select-Zdatwv1b.js";import{b as re,d as _,h as v,l as y,u as b}from"./separator-BvKylNhK.js";import{t as x}from"./button-DUNsVBiu.js";import{Ul as S,a as C,c as w,d as T,i as E,n as D,o as O,r as k,s as A,t as j}from"./table-AJA7Uyxy.js";import{Pn as M,Qo as N,dt as ie}from"./graphql-DC5GYCFE.js";var P=e(n(),1),F=t(),I=({amount:e})=>!e||e.raw===0?(0,F.jsx)(`span`,{className:`text-sm text-gray-400`,children:`—`}):(0,F.jsx)(`span`,{className:r(`text-sm font-medium whitespace-nowrap`,e.raw>0?`text-green-700`:`text-red-600`),children:e.formatted}),L=({balance:e,currency:t})=>{if(e===0)return(0,F.jsx)(`span`,{className:`text-sm text-gray-400`,children:`—`});let n=o(e,t);return(0,F.jsx)(`span`,{className:r(`text-sm font-medium whitespace-nowrap`,e>0?`text-green-700`:`text-red-600`),children:n})},R=({business:e})=>(0,F.jsx)(i,{to:a.BUSINESSES.DETAIL(e.id),target:`_blank`,rel:`noopener noreferrer`,onClick:e=>e.stopPropagation(),className:`inline-flex items-center font-semibold whitespace-nowrap`,children:e.name}),z=({counterAccount:e})=>e?(0,F.jsx)(i,{to:a.BUSINESSES.DETAIL(e.id),target:`_blank`,rel:`noopener noreferrer`,onClick:e=>e.stopPropagation(),className:`inline-flex items-center font-semibold whitespace-nowrap text-sm`,children:e.name}):(0,F.jsx)(`span`,{className:`text-sm text-gray-400`,children:`—`}),B=({row:e,currency:t})=>{let n=e.foreignAmount&&e.foreignAmount.currency===t?e.foreignAmount:null;return!n||n.raw===0?(0,F.jsx)(`span`,{className:`text-sm text-gray-400`,children:`—`}):(0,F.jsx)(`span`,{className:r(`text-sm font-medium whitespace-nowrap`,n.raw>0?`text-green-700`:`text-red-600`),children:n.formatted})},V=({row:e,currency:t})=>{let n=e[`${t.toLowerCase()}Balance`]??0;if(n===0)return(0,F.jsx)(`span`,{className:`text-sm text-gray-400`,children:`—`});let i=o(n,t);return(0,F.jsx)(`span`,{className:r(`text-sm font-medium whitespace-nowrap`,n>0?`text-green-700`:`text-red-600`),children:i})},H=({date:e})=>e?(0,F.jsx)(`span`,{className:`text-sm font-medium whitespace-nowrap`,children:S(new Date(e),`dd/MM/yy`)}):(0,F.jsx)(`span`,{className:`text-sm text-gray-400`,children:`—`}),U=()=>[{accessorKey:`business.name`,id:`businessName`,header:`Business Name`,cell:({row:e})=>(0,F.jsx)(R,{business:e.original.business}),enableSorting:!1,enableHiding:!1},{accessorKey:`invoiceDate`,id:`date`,header:({column:e})=>(0,F.jsx)(s,{column:e,title:`Date`}),cell:({row:e})=>(0,F.jsx)(H,{date:e.original.invoiceDate}),sortFn:`datetime`},{accessorKey:`amount.raw`,id:`amount`,header:({column:e})=>(0,F.jsx)(s,{column:e,title:`Amount`}),cell:({row:e})=>(0,F.jsx)(I,{amount:e.original.amount}),sortFn:`basic`},{accessorKey:`ilsBalance`,id:`ilsBalance`,header:({column:e})=>(0,F.jsx)(s,{column:e,title:`Amount Balance`}),cell:({row:e})=>(0,F.jsx)(L,{balance:e.original.ilsBalance,currency:M.Ils}),sortFn:`basic`}],W=()=>Object.values(M).filter(e=>e!==M.Ils).sort((e,t)=>{let n=u.includes(e),r=u.includes(t);return n&&!r?-1:!n&&r?1:e.localeCompare(t)}).flatMap(e=>[{id:`${e.toLowerCase()}Amount`,accessorFn:t=>t.foreignAmount?.currency===e?t.foreignAmount.raw:null,header:({column:t})=>(0,F.jsx)(s,{column:t,title:`${e} Amount`}),cell:({row:t})=>(0,F.jsx)(B,{row:t.original,currency:e}),sortFn:`basic`,enableHiding:!0},{id:`${e.toLowerCase()}Balance`,accessorFn:t=>t[`${e.toLowerCase()}Balance`]??null,header:({column:t})=>(0,F.jsx)(s,{column:t,title:`${e} Balance`}),cell:({row:t})=>(0,F.jsx)(V,{row:t.original,currency:e}),sortFn:`basic`,enableHiding:!0}]),G=()=>[{accessorKey:`reference`,id:`reference`,header:`Reference`,cell:({row:e})=>(0,F.jsx)(`p`,{className:`text-sm whitespace-nowrap`,children:e.original.reference??``}),enableSorting:!1},{accessorKey:`details`,id:`details`,header:`Details`,cell:({row:e})=>(0,F.jsx)(`p`,{className:`text-sm whitespace-nowrap`,children:e.original.details??``}),enableSorting:!1},{accessorKey:`counterAccount.name`,id:`counterAccount`,header:`Counter Account`,cell:({row:e})=>(0,F.jsx)(z,{counterAccount:e.original.counterAccount}),enableSorting:!1}],K=()=>[...U(),...W(),...G()],q=e=>{let t={};for(let n of Object.values(M).filter(e=>e!==M.Ils)){let r=e.has(n),i=`${n.toLowerCase()}Amount`,a=`${n.toLowerCase()}Balance`;r||(t[i]=!1,t[a]=!1)}return t},J=({row:e})=>(0,F.jsx)(O,{onClick:t=>{t.stopPropagation(),window.open(a.CHARGES.DETAIL(e.original.chargeId),`_blank`,`noopener noreferrer`)},className:`cursor-pointer hover:bg-gray-50 transition-colors`,children:e.getVisibleCells().map(e=>(0,F.jsx)(k,{children:T(e.column.columnDef.cell,e.getContext())},e.id))}),ae=({ledgerRecords:e,businessName:t,fromDate:n,toDate:r})=>{let i=(0,P.useCallback)(async()=>{let i=Y(e);return{fileName:`business_${t}_ledger_records${n?`_${n}`:``}${r?`_${r}`:``}`,fileContent:i}},[e,t,n,r]);return(0,F.jsx)(c,{createFileVariables:i})},Y=e=>{let t=``,n=Array.from(new Set(e.filter(e=>e.foreignAmount?.currency).map(e=>e.foreignAmount.currency)));t+=`Sorting Date,Date,Amount,Amount Balance,${X(n)}Reference,Details,Counter Account\r\n`;for(let r of e){let e=se(r,n);t+=e}return t};function X(e){let t=``;return e.map(e=>{let n=l(e);t+=`${e}(${n}) Amount,${e}(${n}) Balance,`}),t}function oe(e,t){let n=``;return t.map(t=>{e.foreignAmount&&e.foreignAmount.currency===t?n+=`${e.foreignAmount.raw},${e[`${t.toLowerCase()}Balance`]??0},`:n+=`,,`}),n}function se(e,t){let n=``,r=e.invoiceDate?S(new Date(e.invoiceDate),`yyy-MM-dd`):null,i=e.invoiceDate?S(new Date(e.invoiceDate),`dd/MM/yy`):null,a=e.amount.raw,{ilsBalance:o,reference:s,details:c}=e,l=e.counterAccount?.name??``;return n+=`${r},${i},${a},${o},${oe(e,t)}${Z(s??``)},${Z(c??``)},${Z(l)},\r\n`,n}function Z(e){return e===``?``:Number.isNaN(Number(e))?String(e).replace(/"/g,`""`).replace(/,/g,`.`):Number(e).toFixed(2).toString()}var Q=`businessLedger`,$={SORTING:`${Q}_sorting`,COLUMN_VISIBILITY:`${Q}_columnVisibility`};function ce({businessID:e,filter:t}){let[n,r]=(0,P.useState)(()=>{try{let e=localStorage.getItem($.SORTING);return e?JSON.parse(e):[]}catch{return[]}}),[i,a]=(0,P.useState)([]),[o,s]=(0,P.useState)(``),[c,l]=(0,P.useState)({pageIndex:0,pageSize:50});(0,P.useEffect)(()=>{try{localStorage.setItem($.SORTING,JSON.stringify(n))}catch(e){console.warn(`Failed to save sorting to localStorage:`,e)}},[n]);let{fromDate:u,ownerIds:S,toDate:I}=t??{},[{data:L,fetching:R}]=N({query:ie,variables:{filters:{fromDate:u,ownerIds:S,toDate:I,businessIDs:[e]}}}),z=(0,P.useMemo)(()=>L?.businessTransactionsFromLedgerRecords.__typename===`CommonError`?[]:L?.businessTransactionsFromLedgerRecords.businessTransactions.sort((e,t)=>e.invoiceDate>t.invoiceDate?1:-1)??[],[L]),B=(0,P.useMemo)(()=>{let e=[];for(let t=0;t<z.length;t++){let n=z[t],r=t===0?n.amount.raw:(e[t-1].ilsBalance??0)+n.amount.raw,i={};Object.values(M).map(r=>{if(r!==M.Ils){let a=`${r.toLowerCase()}Balance`;i[a]=t===0?(n.foreignAmount?.currency===r?n.foreignAmount?.raw:0)??0:(e[t-1]?.[a]??0)+(n.foreignAmount?.currency===r?n.foreignAmount?.raw:0)}}),e.push({...n,ilsBalance:r,...i})}return e},[z]),V=(0,P.useMemo)(()=>new Set(z.filter(e=>e.foreignAmount?.currency).map(e=>e.foreignAmount.currency)),[z]),H=K(),U=(0,P.useMemo)(()=>q(V),[V]),[W,G]=(0,P.useState)(()=>{try{let e=localStorage.getItem($.COLUMN_VISIBILITY);if(e){let t=JSON.parse(e);return{...U,...t}}}catch{}return U});(0,P.useEffect)(()=>{try{localStorage.setItem($.COLUMN_VISIBILITY,JSON.stringify(W))}catch(e){console.warn(`Failed to save column visibility to localStorage:`,e)}},[W]),(0,P.useEffect)(()=>{G(e=>({...U,...e}))},[U]);let Y=w({features:A,data:B,columns:H,onSortingChange:r,onColumnFiltersChange:a,onColumnVisibilityChange:G,onGlobalFilterChange:s,onPaginationChange:l,globalFilterFn:`includesString`,state:{sorting:n,columnFilters:i,columnVisibility:W,globalFilter:o,pagination:c}}),X=z[0]?.business.name??`unknown`;return R?(0,F.jsxs)(`div`,{className:`flex flex-col gap-4 max-w-[90vw] m-2`,children:[(0,F.jsxs)(`div`,{className:`flex items-center justify-between gap-4`,children:[(0,F.jsxs)(`div`,{className:`flex items-center gap-4`,children:[(0,F.jsx)(d,{className:`h-10 w-64`}),(0,F.jsx)(d,{className:`h-10 w-32`})]}),(0,F.jsx)(d,{className:`size-10`})]}),(0,F.jsx)(`div`,{className:`rounded-md border overflow-x-auto`,children:(0,F.jsxs)(j,{children:[(0,F.jsx)(C,{children:(0,F.jsx)(O,{children:Array.from({length:8}).map((e,t)=>(0,F.jsx)(E,{children:(0,F.jsx)(d,{className:`h-4 w-24`})},t))})}),(0,F.jsx)(D,{children:Array.from({length:5}).map((e,t)=>(0,F.jsx)(O,{children:Array.from({length:8}).map((e,t)=>(0,F.jsx)(k,{children:(0,F.jsx)(d,{className:`h-4 w-full`})},t))},t))})]})})]}):(0,F.jsxs)(`div`,{className:`flex flex-col gap-4 max-w-[90vw] m-2`,children:[(0,F.jsxs)(`div`,{className:`flex items-center justify-between gap-4 flex-wrap`,children:[(0,F.jsxs)(`div`,{className:`flex items-center gap-4 flex-wrap`,children:[(0,F.jsxs)(`div`,{className:`relative flex-1 min-w-[200px] max-w-sm`,children:[(0,F.jsx)(re,{className:`absolute left-2.5 top-2.5 h-4 w-4 text-gray-500`}),(0,F.jsx)(h,{placeholder:`Search reference, details, business...`,value:o??``,onChange:e=>s(e.target.value),className:`pl-8`})]}),(0,F.jsxs)(y,{children:[(0,F.jsx)(v,{asChild:!0,children:(0,F.jsxs)(x,{variant:`outline`,size:`sm`,children:[(0,F.jsx)(te,{className:`mr-2 h-4 w-4`}),`Columns`]})}),(0,F.jsx)(_,{align:`end`,className:`max-h-96 overflow-y-auto`,children:Y.getAllColumns().filter(e=>e.getCanHide()).map(e=>(0,F.jsx)(b,{className:`capitalize`,checked:e.getIsVisible(),onCheckedChange:t=>e.toggleVisibility(!!t),children:e.id},e.id))})]})]}),(0,F.jsx)(ae,{ledgerRecords:B,businessName:X,fromDate:t?.fromDate??void 0,toDate:t?.toDate??void 0})]}),(0,F.jsx)(`div`,{className:`rounded-md border overflow-x-auto`,children:(0,F.jsxs)(j,{children:[(0,F.jsx)(C,{className:`sticky top-0 z-20 bg-white border-b`,children:Y.getHeaderGroups().map(e=>(0,F.jsx)(O,{children:e.headers.map(e=>(0,F.jsx)(E,{colSpan:e.colSpan,children:e.isPlaceholder?null:T(e.column.columnDef.header,e.getContext())},e.id))},e.id))}),(0,F.jsx)(D,{children:Y.getRowModel().rows?.length?Y.getRowModel().rows.map(e=>(0,F.jsx)(J,{row:e},e.id)):(0,F.jsx)(O,{children:(0,F.jsx)(k,{colSpan:H.length,className:`h-24 text-center`,children:`No transactions found.`})})})]})}),(0,F.jsxs)(`div`,{className:`flex items-center justify-between gap-4 flex-wrap`,children:[(0,F.jsx)(`div`,{className:`flex items-center gap-2`,children:(0,F.jsxs)(`p`,{className:`text-sm text-gray-700`,children:[`Showing `,Y.state.pagination.pageIndex*Y.state.pagination.pageSize+1,` to`,` `,Math.min((Y.state.pagination.pageIndex+1)*Y.state.pagination.pageSize,Y.getFilteredRowModel().rows.length),` `,`of `,Y.getFilteredRowModel().rows.length,` transactions`]})}),(0,F.jsxs)(`div`,{className:`flex items-center gap-6`,children:[(0,F.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,F.jsx)(`p`,{className:`text-sm font-medium`,children:`Rows per page:`}),(0,F.jsxs)(g,{value:String(Y.state.pagination.pageSize),onValueChange:e=>{Y.setPageSize(Number(e))},children:[(0,F.jsx)(p,{className:`h-8 w-[70px]`,children:(0,F.jsx)(f,{placeholder:Y.state.pagination.pageSize})}),(0,F.jsx)(m,{side:`top`,children:[25,50,100,200,500].map(e=>(0,F.jsx)(ne,{value:String(e),children:e},e))})]})]}),(0,F.jsx)(ee,{className:`w-fit mx-0`,currentPageIndex:Y.state.pagination.pageIndex,totalPages:Y.getPageCount(),onChange:e=>Y.setPageIndex(e)})]})]})]})}export{ce as t};