@accounter/client
Version:
Accounter client application
1 lines • 12.2 kB
JavaScript
import{An as e,Dn as t,Dt as n,En as r,Es as i,Et as a,Fi as ee,Fu as te,Hu as o,Ii as s,Kl as c,Kt as ne,Li as l,Mi as u,Ml as re,Nd as d,Ni as f,On as p,Pi as ie,Pl as m,Rd as h,Ri as g,St as _,Tt as v,Tu as y,Wa as b,Wi as x,Xi as S,Zi as ae,_i as C,_n as w,_t as T,bt as E,dl as D,dn as O,fn as k,fs as A,gn as j,gt as M,hn as N,ht as P,in as oe,ji as se,jl as F,kn as I,mt as ce,ns as le,pn as L,qu as R,sc as z,ss as B,vi as V,vn as H,vt as U,wn as ue,wt as W,xd as G,xt as K,zc as q,zi as de}from"./index-DJOqGznr.js";var J=G(`book-copy`,[[`path`,{d:`M5 7a2 2 0 0 0-2 2v11`,key:`1yhqjt`}],[`path`,{d:`M5.803 18H5a2 2 0 0 0 0 4h9.5a.5.5 0 0 0 .5-.5V21`,key:`edzzo5`}],[`path`,{d:`M9 15V4a2 2 0 0 1 2-2h9.5a.5.5 0 0 1 .5.5v14a.5.5 0 0 1-.5.5H11a2 2 0 0 1 0-4h10`,key:`1nwzrg`}]]),Y=h(d(),1),X=`createContract`,fe=()=>{let[{fetching:e},t]=F(B);return{creating:e,createContract:(0,Y.useCallback)(async e=>{let n=`Error creating new contract for client[${e.input.clientId}]`,r=`${X}-${e.input.clientId}`;V.loading(`Creating contract`,{id:r});try{C(await t(e),n,r)&&V.success(`Success`,{id:r,description:`Contract for client ${e.input.clientId} was successfully created`})}catch(e){console.error(`${n}: ${e}`),V.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}},[t])}},Z=`updateContract`,pe=()=>{let[{fetching:e},t]=F(D);return{updating:e,updateContract:(0,Y.useCallback)(async e=>{let n=`Error updating contract ID [${e.contractId}]`,r=`${Z}-${e.contractId}`;V.loading(`Updating contract`,{id:r});try{let i=C(await t(e),n,r,`updateContract`);if(i)return V.success(`Success`,{id:r,description:`Contract updated`}),i.updateContract}catch(e){console.error(`${n}: ${e}`),V.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}},[t])}},Q=y(),me=_({id:a().optional(),startDate:n(`Start date is required`),endDate:n(`End date is required`),pos:M(W()),paymentAmount:K().min(0,`Payment amount must be non-negative`),paymentCurrency:P(Object.values(A),`Currency is required`),productType:P(Object.values(z)).optional(),msCloudLink:v().optional().or(E(``)),billingCycle:P(Object.values(b)).optional(),subscriptionPlan:P(Object.values(q)).optional(),isActive:U(),defaultRemark:W().optional(),defaultDocumentType:P(Object.values(i)),operationsLimit:T().min(BigInt(0),`Operations limit must be non-negative`)}),he={startDate:``,endDate:``,pos:[],paymentAmount:0,paymentCurrency:A.Usd,productType:z.Hive,msCloudLink:void 0,billingCycle:b.Monthly,subscriptionPlan:void 0,isActive:!0,defaultRemark:void 0,defaultDocumentType:i.Proforma,operationsLimit:BigInt(0)};function $({clientId:n,contract:a,contractId:d,onDone:h,duplicate:_}){let{isEditing:v,isDuplicating:y}=(0,Y.useMemo)(()=>{let e=!!a||!!d,t=_===!0&&e;return{isEditing:e&&!t,isDuplicating:t}},[a,d,_]),[C,T]=(0,Y.useState)(!1),[E,D]=(0,Y.useState)(!1),[M,P]=(0,Y.useState)(he),[F,z]=(0,Y.useState)(``),{updateContract:B,updating:V}=pe(),{createContract:U,creating:W}=fe(),[{data:G,fetching:K}]=re({query:le,pause:!d||!C,variables:{contractId:d??``}}),X=ue({resolver:ce(me),defaultValues:M}),Z=()=>{let e=F.trim();if(e){let t=X.getValues(`pos`);t.includes(e)||X.setValue(`pos`,[...t,e],{shouldDirty:!0}),z(``)}},$=e=>{let t=X.getValues(`pos`);X.setValue(`pos`,t.filter((t,n)=>n!==e),{shouldDirty:!0})};(0,Y.useEffect)(()=>{a&&P(a)},[a]),(0,Y.useEffect)(()=>{if(G?.contractsById){let e=G.contractsById,t={startDate:e.startDate,endDate:e.endDate,pos:e.purchaseOrders,paymentAmount:e.amount.raw,paymentCurrency:e.amount.currency,productType:e.product??void 0,msCloudLink:e.msCloud?.toString()??void 0,billingCycle:e.billingCycle,subscriptionPlan:e.plan??void 0,isActive:e.isActive,defaultRemark:e.remarks??void 0,defaultDocumentType:e.documentType,operationsLimit:BigInt(e.operationsLimit)};P({id:e.id,...t})}},[G]),(0,Y.useEffect)(()=>{M&&X.reset({startDate:M.startDate,endDate:M.endDate,pos:M.pos,paymentAmount:M.paymentAmount,paymentCurrency:M.paymentCurrency,productType:M.productType,msCloudLink:M.msCloudLink,billingCycle:M.billingCycle,subscriptionPlan:M.subscriptionPlan,isActive:M.isActive,defaultRemark:M.defaultRemark,defaultDocumentType:M.defaultDocumentType,operationsLimit:BigInt(M.operationsLimit)})},[(0,Y.useMemo)(()=>M?JSON.stringify(M):null,[M]),X.reset]);let ge=(0,Y.useCallback)(async e=>{if(y||v){if(!M?.id)return;if(y){if(!e.billingCycle){X.setError(`billingCycle`,{message:`Billing cycle is required`});return}await U({input:{clientId:n,amount:{raw:e.paymentAmount,currency:e.paymentCurrency},billingCycle:e.billingCycle,documentType:e.defaultDocumentType,endDate:m(new Date(e.endDate),`yyyy-MM-dd`),isActive:e.isActive,msCloud:e.msCloudLink,plan:e.subscriptionPlan,product:e.productType,purchaseOrders:e.pos,remarks:e.defaultRemark,startDate:m(new Date(e.startDate),`yyyy-MM-dd`),operationsLimit:e.operationsLimit,deactivateContracts:E?[M.id]:void 0}})}else await B({contractId:M.id,input:{amount:{raw:e.paymentAmount,currency:e.paymentCurrency},billingCycle:e.billingCycle,documentType:e.defaultDocumentType,endDate:m(new Date(e.endDate),`yyyy-MM-dd`),isActive:e.isActive,msCloud:e.msCloudLink,plan:e.subscriptionPlan,product:e.productType,purchaseOrders:e.pos,remarks:e.defaultRemark,startDate:m(new Date(e.startDate),`yyyy-MM-dd`),operationsLimit:e.operationsLimit}})}else{if(!e.billingCycle){X.setError(`billingCycle`,{message:`Billing cycle is required`});return}await U({input:{clientId:n,amount:{raw:e.paymentAmount,currency:e.paymentCurrency},billingCycle:e.billingCycle,documentType:e.defaultDocumentType,endDate:m(new Date(e.endDate),`yyyy-MM-dd`),isActive:e.isActive,msCloud:e.msCloudLink,plan:e.subscriptionPlan,product:e.productType,purchaseOrders:e.pos,remarks:e.defaultRemark,startDate:m(new Date(e.startDate),`yyyy-MM-dd`),operationsLimit:e.operationsLimit}})}T(!1),h?.()},[M,h,U,B,n,X,v,y,E]);return(0,Q.jsxs)(f,{open:C,onOpenChange:T,children:[(0,Q.jsx)(de,{asChild:!0,children:v?(0,Q.jsx)(c,{variant:`ghost`,size:`sm`,onClick:()=>T(!0),children:(0,Q.jsx)(o,{className:`size-5`})}):y?(0,Q.jsx)(c,{variant:`ghost`,size:`sm`,onClick:()=>T(!0),children:(0,Q.jsx)(J,{className:`size-5`})}):(0,Q.jsxs)(c,{size:`sm`,onClick:()=>T(!0),children:[(0,Q.jsx)(R,{className:`size-4 mr-2`}),`New Contract`]})}),(0,Q.jsxs)(ie,{className:`max-w-2xl max-h-[90vh] overflow-y-auto`,children:[(0,Q.jsxs)(l,{children:[(0,Q.jsx)(g,{children:v?`Edit Contract`:`Create New Contract`}),(0,Q.jsx)(ee,{children:v?`Update contract details`:y?`Create a new contract based on an existing one`:`Add a new contract with all required details`})]}),K?(0,Q.jsx)(Q.Fragment,{children:`Fetching contract details...`}):(0,Q.jsx)(k,{...X,children:(0,Q.jsxs)(`form`,{onSubmit:X.handleSubmit(ge),children:[(0,Q.jsxs)(`div`,{className:`grid gap-4 py-4`,children:[(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(N,{control:X.control,name:`operationsLimit`,render:({field:e})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(w,{children:`Operations Limit`}),(0,Q.jsx)(L,{children:(0,Q.jsx)(u,{placeholder:`500`,...e,value:e.value?.toLocaleString(),onChange:t=>{let n=t.target.value.replace(/[^0-9]/g,``);e.onChange(BigInt(n||0))}})}),(0,Q.jsx)(H,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`pos`,render:({field:e})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(w,{children:`Purchase Orders`}),(0,Q.jsxs)(`div`,{className:`flex gap-2`,children:[(0,Q.jsx)(u,{placeholder:`Add PO...`,value:F,onChange:e=>z(e.target.value),onKeyDown:e=>{e.key===`Enter`&&(e.preventDefault(),Z())}}),(0,Q.jsx)(c,{type:`button`,size:`sm`,onClick:Z,children:(0,Q.jsx)(R,{className:`h-4 w-4`})})]}),(0,Q.jsx)(`div`,{className:`flex flex-wrap gap-2 mt-2`,children:e.value?.map((t,n)=>(0,Q.jsxs)(ne,{variant:`secondary`,className:`gap-1 max-w-xs truncate`,children:[t,n===(e.value?.length??0)-1&&(0,Q.jsx)(c,{variant:`ghost`,size:`icon`,className:`p-0 size-3`,onClick:()=>$(n),children:(0,Q.jsx)(te,{className:`size-3 cursor-pointer flex-shrink-0`})})]},t))}),(0,Q.jsx)(H,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(N,{control:X.control,name:`startDate`,render:({field:{onChange:e,...t}})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(w,{children:`Start Date`}),(0,Q.jsx)(L,{children:(0,Q.jsx)(u,{type:`date`,...t,onInput:t=>e(t)})}),(0,Q.jsx)(H,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`endDate`,render:({field:{onChange:e,...t}})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(w,{children:`End Date`}),(0,Q.jsx)(L,{children:(0,Q.jsx)(u,{type:`date`,...t,onInput:t=>e(t)})}),(0,Q.jsx)(H,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-3`,children:[(0,Q.jsx)(N,{control:X.control,name:`paymentAmount`,render:({field:e})=>(0,Q.jsxs)(j,{className:`md:col-span-2`,children:[(0,Q.jsx)(w,{children:`Payment Amount`}),(0,Q.jsx)(L,{children:(0,Q.jsx)(u,{type:`number`,placeholder:`24000`,...e,onChange:t=>{e.onChange(t?.target.value?Number(t?.target.value):void 0)}})}),(0,Q.jsx)(H,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`paymentCurrency`,render:({field:n})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(w,{children:`Currency`}),(0,Q.jsxs)(r,{onValueChange:n.onChange,defaultValue:n.value,children:[(0,Q.jsx)(L,{children:(0,Q.jsx)(I,{children:(0,Q.jsx)(e,{})})}),(0,Q.jsx)(t,{children:Object.values(A).map(e=>(0,Q.jsx)(p,{value:e,children:e},e))})]}),(0,Q.jsx)(H,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(N,{control:X.control,name:`productType`,render:({field:e})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(w,{children:`Product Type`}),(0,Q.jsx)(L,{children:(0,Q.jsx)(u,{placeholder:`Cloud Services`,...e})}),(0,Q.jsx)(H,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`billingCycle`,render:({field:n})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(w,{children:`Billing Cycle`}),(0,Q.jsxs)(r,{onValueChange:n.onChange,defaultValue:n.value,children:[(0,Q.jsx)(L,{children:(0,Q.jsx)(I,{children:(0,Q.jsx)(e,{})})}),(0,Q.jsx)(t,{children:Object.values(b).map(e=>(0,Q.jsx)(p,{value:e,children:S(e)},e))})]}),(0,Q.jsx)(H,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(N,{control:X.control,name:`subscriptionPlan`,render:({field:n})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(w,{children:`Subscription Plan`}),(0,Q.jsx)(L,{children:(0,Q.jsxs)(r,{onValueChange:n.onChange,defaultValue:n.value,children:[(0,Q.jsx)(L,{children:(0,Q.jsx)(I,{children:(0,Q.jsx)(e,{})})}),(0,Q.jsx)(t,{children:Object.values(q).map(e=>(0,Q.jsx)(p,{value:e,children:ae(e)},e))})]})}),(0,Q.jsx)(H,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`isActive`,render:({field:e})=>(0,Q.jsxs)(j,{className:`flex flex-row items-center justify-between rounded-lg border p-4`,children:[(0,Q.jsx)(`div`,{className:`space-y-0.5`,children:(0,Q.jsx)(w,{className:`text-base`,children:`Active Status`})}),(0,Q.jsx)(L,{children:(0,Q.jsx)(O,{checked:e.value,onCheckedChange:e.onChange})})]})})]}),(0,Q.jsx)(N,{control:X.control,name:`msCloudLink`,render:({field:e})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(w,{children:`MS Cloud Link`}),(0,Q.jsx)(L,{children:(0,Q.jsx)(u,{type:`url`,placeholder:`https://portal.azure.com/contract-id`,...e})}),(0,Q.jsx)(H,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`defaultDocumentType`,render:({field:n})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(w,{children:`Default Document Type`}),(0,Q.jsxs)(r,{onValueChange:n.onChange,defaultValue:n.value,children:[(0,Q.jsx)(L,{children:(0,Q.jsx)(I,{children:(0,Q.jsx)(e,{})})}),(0,Q.jsx)(t,{children:Object.values(i).map(e=>(0,Q.jsx)(p,{value:e,children:x(e)},e))})]}),(0,Q.jsx)(H,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`defaultRemark`,render:({field:e})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(w,{children:`Default Remark`}),(0,Q.jsx)(L,{children:(0,Q.jsx)(oe,{placeholder:`Enter default remark for this contract...`,rows:3,...e})}),(0,Q.jsx)(H,{})]})}),y&&(0,Q.jsxs)(`div`,{className:`flex flex-row items-center justify-between rounded-lg border p-4`,children:[(0,Q.jsx)(`div`,{className:`space-y-0.5`,children:(0,Q.jsx)(se,{className:`text-base`,children:`Deactivate Original Contract`})}),(0,Q.jsx)(O,{checked:E,onCheckedChange:D})]})]}),(0,Q.jsxs)(s,{children:[(0,Q.jsx)(c,{type:`button`,variant:`outline`,onClick:()=>T(!1),children:`Cancel`}),(0,Q.jsx)(c,{type:`submit`,disabled:W||V,children:v?`Update Contract`:`Create Contract`})]})]})})]})]})}export{$ as t};