@accounter/client
Version:
2 lines (1 loc) • 9.93 kB
JavaScript
import{v as E,j as t,w as K,r as l,af as N,B as p,dI as y,b$ as D,g as U,ic as H,t as b,i as q,u as $,fE as z,D as V,aL as _,x as G,al as P,am as Q,a3 as W,ao as J,ap as Y,aq as X,ar as Z,as as ee,au as te,aC as se,id as ae,H as ne,ie as re,ay as x,bY as oe,c5 as ce,c6 as ie,L as le,bO as de,bP as ue,bQ as T,bR as ge,c2 as A,bS as me,bT as F}from"./index-DRqooPz9.js";const he=[["path",{d:"M12 18H4a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v5",key:"x6cv4u"}],["path",{d:"m16 19 3 3 3-3",key:"1ibux0"}],["path",{d:"M18 12h.01",key:"yjnet6"}],["path",{d:"M19 16v6",key:"tddt3s"}],["path",{d:"M6 12h.01",key:"c2rlol"}],["circle",{cx:"12",cy:"12",r:"2",key:"1c9p78"}]],R=E("banknote-arrow-down",he);const xe=[["path",{d:"M12 18H4a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v5",key:"x6cv4u"}],["path",{d:"M18 12h.01",key:"yjnet6"}],["path",{d:"M19 22v-6",key:"qhmiwi"}],["path",{d:"m22 19-3-3-3 3",key:"rn6bg2"}],["path",{d:"M6 12h.01",key:"c2rlol"}],["circle",{cx:"12",cy:"12",r:"2",key:"1c9p78"}]],fe=E("banknote-arrow-up",xe);function pe({transaction:e}){return t.jsx("div",{className:"flex flex-col justify-center",children:e.eventDate?K(new Date(e.eventDate),"dd/MM/yyyy"):"-"})}function je({transaction:e}){const{isInterest:a,amount:n}=e,r=l.useMemo(()=>Number(n.raw)<0,[n.raw]);return t.jsx("div",{className:"flex items-center gap-2",children:a?t.jsxs(t.Fragment,{children:[t.jsx(R,{className:"size-4 text-red-600"}),t.jsx(N,{variant:"outline",className:"bg-red-50 text-red-700",children:"Interest"})]}):r?t.jsxs(t.Fragment,{children:[t.jsx(fe,{className:"size-4 text-green-600"}),t.jsx(N,{variant:"outline",className:"bg-green-50 text-green-700",children:"Deposit"})]}):t.jsxs(t.Fragment,{children:[t.jsx(R,{className:"size-4 text-red-600"}),t.jsx(N,{variant:"outline",className:"bg-red-50 text-red-700",children:"Withdrawal"})]})})}function ye({transaction:e}){const n=Number(e.amount.raw)<0?"text-green-700":"text-red-500";return t.jsx("div",{className:`flex flex-col justify-center font-mono ${n}`,children:e.amount.formatted})}function De({transaction:e}){const a=l.useMemo(()=>{const{currency:n}=e.amount;return new Intl.NumberFormat("en-US",{style:"currency",currency:n,minimumFractionDigits:2,maximumFractionDigits:2}).format(e.cumulativeBalance)},[e.cumulativeBalance,e.amount]);return t.jsx("div",{className:"flex flex-col justify-center font-mono font-semibold",children:a})}function Ce({transaction:e}){const a=l.useMemo(()=>{const{currency:n}=e.amount;return new Intl.NumberFormat("en-US",{style:"currency",currency:n,minimumFractionDigits:2,maximumFractionDigits:2}).format(e.totalInterest)},[e.totalInterest,e.amount]);return t.jsx("div",{className:"flex flex-col justify-center font-mono font-semibold",children:a})}function ve({transaction:e}){const a=l.useMemo(()=>new Intl.NumberFormat("en-US",{style:"currency",currency:e.localCurrency,minimumFractionDigits:2,maximumFractionDigits:2}).format(e.localAmount),[e.localAmount,e.localCurrency]);return t.jsx("div",{className:"flex flex-col justify-center font-mono text-muted-foreground",children:a})}function Ie({transaction:e}){const a=l.useMemo(()=>new Intl.NumberFormat("en-US",{style:"currency",currency:e.localCurrency,minimumFractionDigits:2,maximumFractionDigits:2}).format(e.localCumulativeBalance),[e.localCumulativeBalance,e.localCurrency]);return t.jsx("div",{className:"flex flex-col justify-center font-mono font-semibold text-muted-foreground",children:a})}const L=[{id:"date",accessorKey:"eventDate",sortingFn:e=>e.original.eventDate?new Date(e.original.eventDate).getTime():0,header:({column:e})=>t.jsxs(p,{variant:"ghost",onClick:()=>e.toggleSorting(e.getIsSorted()==="asc"),children:["Date",e.getIsSorted()&&(e.getIsSorted()==="asc"?t.jsx(y,{className:"ml-2 h-4 w-4"}):t.jsx(D,{className:"ml-2 h-4 w-4"}))]}),cell:({row:e})=>t.jsx(pe,{transaction:e.original})},{id:"indicator",accessorKey:"amount.raw",header:"Type",cell:({row:e})=>t.jsx(je,{transaction:e.original})},{id:"amount",accessorKey:"amount.raw",header:({column:e})=>t.jsxs(p,{variant:"ghost",onClick:()=>e.toggleSorting(e.getIsSorted()==="asc"),children:["Origin Amount",e.getIsSorted()&&(e.getIsSorted()==="asc"?t.jsx(y,{className:"ml-2 h-4 w-4"}):t.jsx(D,{className:"ml-2 h-4 w-4"}))]}),cell:({row:e})=>t.jsx(ye,{transaction:e.original})},{id:"cumulativeBalance",accessorKey:"cumulativeBalance",header:({column:e})=>t.jsxs(p,{variant:"ghost",onClick:()=>e.toggleSorting(e.getIsSorted()==="asc"),children:["Origin Balance",e.getIsSorted()&&(e.getIsSorted()==="asc"?t.jsx(y,{className:"ml-2 h-4 w-4"}):t.jsx(D,{className:"ml-2 h-4 w-4"}))]}),cell:({row:e})=>t.jsx(De,{transaction:e.original})},{id:"totalInterest",accessorKey:"totalInterest",header:({column:e})=>t.jsxs(p,{variant:"ghost",onClick:()=>e.toggleSorting(e.getIsSorted()==="asc"),children:["Interest",e.getIsSorted()&&(e.getIsSorted()==="asc"?t.jsx(y,{className:"ml-2 h-4 w-4"}):t.jsx(D,{className:"ml-2 h-4 w-4"}))]}),cell:({row:e})=>t.jsx(Ce,{transaction:e.original})},{id:"localAmount",accessorKey:"localAmount",header:({column:e})=>t.jsxs(p,{variant:"ghost",onClick:()=>e.toggleSorting(e.getIsSorted()==="asc"),children:["Local Amount",e.getIsSorted()&&(e.getIsSorted()==="asc"?t.jsx(y,{className:"ml-2 h-4 w-4"}):t.jsx(D,{className:"ml-2 h-4 w-4"}))]}),cell:({row:e})=>t.jsx(ve,{transaction:e.original})},{id:"localCumulativeBalance",accessorKey:"localCumulativeBalance",header:({column:e})=>t.jsxs(p,{variant:"ghost",onClick:()=>e.toggleSorting(e.getIsSorted()==="asc"),children:["Local Balance",e.getIsSorted()&&(e.getIsSorted()==="asc"?t.jsx(y,{className:"ml-2 h-4 w-4"}):t.jsx(D,{className:"ml-2 h-4 w-4"}))]}),cell:({row:e})=>t.jsx(Ie,{transaction:e.original})}],we="assignChargeToDeposit",Se=()=>{const[{fetching:e},a]=U(H),n=l.useCallback(async r=>{const h=`Error assigning charge ${r.chargeId} to deposit ${r.depositId}`,o=`${we}-${r.chargeId}`;b.loading("Assigning to deposit",{id:o});try{const c=await a(r),u=q(c,h,o);u&&b.success("Success",{id:o,description:`Charge assigned to deposit ${u.assignChargeToDeposit.id}`})}catch(c){console.error(`${h}: ${c}`),b.error("Error",{id:o,description:h,duration:1e5,closeButton:!0})}},[a]);return{assigning:e,assignChargeToDeposit:n}};function Ne({depositId:e,chargeId:a,refetch:n}){const[r,h]=l.useState(!1),[o,c]=l.useState(void 0),[{data:u}]=$({query:z,pause:!r}),{assigning:g,assignChargeToDeposit:d}=Se(),f=l.useMemo(()=>(u?.allDeposits??[]).filter(s=>s.isOpen&&s.id!==e),[u?.allDeposits,e]),m=l.useCallback(async()=>{o&&(await d({chargeId:a,depositId:o}),h(!1),c(void 0),n?.())},[d,o,n,a]);return l.useEffect(()=>{r&&c(void 0)},[r]),t.jsxs(V,{open:r,onOpenChange:h,children:[t.jsx(_,{children:t.jsx(p,{variant:"ghost",size:"sm",children:"Reassign"})}),t.jsxs(G,{children:[t.jsx(P,{children:t.jsx(Q,{children:"Reassign Transaction"})}),t.jsx("div",{className:"space-y-4",children:t.jsxs("div",{className:"flex flex-col gap-2",children:[t.jsx(W,{htmlFor:"target-deposit-select",children:"Target Deposit"}),t.jsxs(J,{value:o,onValueChange:s=>c(s),children:[t.jsx(Y,{className:"min-w-[220px]",children:t.jsx(X,{id:"target-deposit-select",placeholder:f.length?"Select deposit":"No open deposits"})}),t.jsx(Z,{children:f.map(s=>t.jsxs(ee,{value:s.id,children:[s.id," (",s.currency,")"]},s.id))})]})]})}),t.jsx(te,{children:t.jsx(p,{variant:"secondary",disabled:!o||g,onClick:m,children:g?"Reassigning…":"Reassign"})})]})]})}function be(e,a){const{getId:n,getChargeId:r,getAmount:h}=a,o=new Map;for(const u of e){const g=r(u);if(!g)continue;const d=o.get(g)??[];d.push(u),o.has(g)||o.set(g,d)}const c=new Set;for(const[,u]of o)if(u.length>1){const g=[...u].sort((d,f)=>Math.abs(h(f))-Math.abs(h(d)));for(let d=1;d<g.length;d++)c.add(n(g[d]))}return c}const Te={[x.Eur]:"eur",[x.Gbp]:"gbp",[x.Cad]:"cad",[x.Jpy]:"jpy",[x.Aud]:"aud",[x.Sek]:"sek",[x.Usd]:"usd"};function ke({depositId:e,enableReassign:a=!1,refetch:n}){const{userContext:r}=l.useContext(se),[h,o]=l.useState([{id:"date",desc:!1}]),[{data:c,fetching:u}]=$({query:ae,variables:{depositId:e}}),g=l.useMemo(()=>{if(!c?.deposit?.transactions)return[];const m=c.deposit.transactions.map(i=>ne(re,i)),s=be(m,{getId:i=>i.id,getChargeId:i=>i.chargeId,getAmount:i=>Number(i.amount.raw??0)}),S=[...m].sort((i,C)=>{const v=i.eventDate?new Date(i.eventDate).getTime():0,I=C.eventDate?new Date(C.eventDate).getTime():0;return v-I});let w=0,j=0,M=0;return S.map(i=>{let C=null;const v=Te[i.amount.currency];v&&(C=i.debitExchangeRates?.[v]??i.eventExchangeRates?.[v]??null);const I=Number(i.amount.raw),k=I*(C??1),O=r?.context.defaultLocalCurrency??x.Ils,B=s.has(i.id);return B?j+=I:(w+=I,M+=k),{...i,cumulativeBalance:w,localCumulativeBalance:M,localAmount:k,localCurrency:O,totalInterest:j,isInterest:B}})},[c?.deposit,r]),d=l.useMemo(()=>{const m=r?.context.defaultLocalCurrency??x.Ils,S=!!c?.deposit?.currency&&c.deposit.currency===m?L.filter(j=>j.id!=="amount"&&j.id!=="cumulativeBalance"):L,w=a?[{id:"actions",header:"Actions",cell:({row:j})=>t.jsx(Ne,{depositId:e,chargeId:j.original.chargeId,refetch:n})}]:[];return[...S,...w]},[a,e,n,c?.deposit?.currency,r?.context.defaultLocalCurrency]),f=oe({data:g,columns:d,getCoreRowModel:ie(),onSortingChange:o,getSortedRowModel:ce(),state:{sorting:h}});return u?t.jsx("div",{className:"flex h-64 w-full items-center justify-center",children:t.jsx(le,{className:"size-10 animate-spin"})}):t.jsxs(de,{children:[t.jsx(ue,{children:f.getHeaderGroups().map(m=>t.jsx(T,{children:m.headers.map(s=>t.jsx(ge,{colSpan:s.colSpan,children:s.isPlaceholder?null:A(s.column.columnDef.header,s.getContext())},s.id))},m.id))}),t.jsx(me,{children:f.getRowModel().rows?.length?f.getRowModel().rows.map(m=>t.jsx(T,{children:m.getVisibleCells().map(s=>t.jsx(F,{children:A(s.column.columnDef.cell,s.getContext())},s.id))},m.id)):t.jsx(T,{children:t.jsx(F,{colSpan:d.length,className:"h-24 text-center",children:"No transactions in this deposit."})})})]})}export{ke as D,Se as u};