@accounter/client
Version:
Accounter client application
1 lines • 12.7 kB
JavaScript
import{s as e}from"./dist-BmACi7k_.js";import{i as t,o as n}from"./utils-BxkqV8PM.js";import{In as r,Ka as i,Ro as a,fa as o,kn as s,nr as c,ot as l,r as u,t as d,wn as ee,xi as f,zo as te}from"./error-handling-sQ-z99nE.js";import{a as p,c as ne,d as m,f as re,gt as h,i as g,l as _,lt as v,m as ie,n as y,o as ae,p as oe,r as b,s as x,t as S,u as se,ut as C}from"./select-BJv1AsCh.js";import{Xn as ce,_t as w,hr as le}from"./common-BAPb2n5D.js";import{o as ue}from"./separator-CyMtmDO1.js";import{t as T}from"./button-BZ6z_NAl.js";import{$c as E,$n as D,Bn as O,En as k,Gc as A,Gt as j,Jc as M,Kl as N,Pt as P,Rr as F,Uc as de,Vt as I,Wc as L,Wt as R,Xc as z,Yc as B,h as V,mr as H,pr as U,qc as W,zn as G}from"./table-Ro57XJnL.js";var K=h(`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`}]]);function q(e){switch(e){case l.Annual:return`Annual`;case l.Monthly:return`Monthly`;default:throw Error(`Unsupported Billing Cycle: ${e}`)}}function J(e){switch(e){case o.Enterprise:return`Enterprise`;case o.Pro:return`Pro`;default:throw Error(`Unsupported Subscription plan: ${e}`)}}var Y=e(n(),1),X=`createContract`,fe=()=>{let[{fetching:e},t]=a(s);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}`;u.loading(`Creating contract`,{id:r});try{d(await t(e),n,r)&&u.success(`Success`,{id:r,description:`Contract for client ${e.input.clientId} was successfully created`})}catch(e){console.error(`${n}: ${e}`),u.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}},[t])}},Z=`updateContract`,pe=()=>{let[{fetching:e},t]=a(i);return{updating:e,updateContract:(0,Y.useCallback)(async e=>{let n=`Error updating contract ID [${e.contractId}]`,r=`${Z}-${e.contractId}`;u.loading(`Updating contract`,{id:r});try{let i=d(await t(e),n,r,`updateContract`);if(i)return u.success(`Success`,{id:r,description:`Contract updated`}),i.updateContract}catch(e){console.error(`${n}: ${e}`),u.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}},[t])}},Q=t(),me=O({id:H().optional(),startDate:F(`Start date is required`),endDate:F(`End date is required`),pos:I(D()),paymentAmount:G().min(0,`Payment amount must be non-negative`),paymentCurrency:P(Object.values(r),`Currency is required`),productType:P(Object.values(f)).optional(),msCloudLink:U().optional().or(k(``)),billingCycle:P(Object.values(l)).optional(),subscriptionPlan:P(Object.values(o)).optional(),isActive:j(),defaultRemark:D().optional(),defaultDocumentType:P(Object.values(c)),operationsLimit:R().min(BigInt(0),`Operations limit must be non-negative`)}),he={startDate:``,endDate:``,pos:[],paymentAmount:0,paymentCurrency:r.Usd,productType:f.Hive,msCloudLink:void 0,billingCycle:l.Monthly,subscriptionPlan:void 0,isActive:!0,defaultRemark:void 0,defaultDocumentType:c.Proforma,operationsLimit:BigInt(0)};function $({clientId:e,contract:t,contractId:n,onDone:i,duplicate:a}){let{isEditing:s,isDuplicating:u}=(0,Y.useMemo)(()=>{let e=!!t||!!n,r=a===!0&&e;return{isEditing:e&&!r,isDuplicating:r}},[t,n,a]),[d,f]=(0,Y.useState)(!1),[h,D]=(0,Y.useState)(!1),[O,k]=(0,Y.useState)(he),[j,P]=(0,Y.useState)(``),{updateContract:F,updating:I}=pe(),{createContract:R,creating:H}=fe(),[{data:U,fetching:G}]=te({query:ee,pause:!n||!d,variables:{contractId:n??``}}),X=E({resolver:V(me),defaultValues:O}),Z=()=>{let e=j.trim();if(e){let t=X.getValues(`pos`);t.includes(e)||X.setValue(`pos`,[...t,e],{shouldDirty:!0}),P(``)}},$=e=>{let t=X.getValues(`pos`);X.setValue(`pos`,t.filter((t,n)=>n!==e),{shouldDirty:!0})};(0,Y.useEffect)(()=>{t&&k(t)},[t]),(0,Y.useEffect)(()=>{if(U?.contractsById){let e=U.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)};k({id:e.id,...t})}},[U]),(0,Y.useEffect)(()=>{O&&X.reset({startDate:O.startDate,endDate:O.endDate,pos:O.pos,paymentAmount:O.paymentAmount,paymentCurrency:O.paymentCurrency,productType:O.productType,msCloudLink:O.msCloudLink,billingCycle:O.billingCycle,subscriptionPlan:O.subscriptionPlan,isActive:O.isActive,defaultRemark:O.defaultRemark,defaultDocumentType:O.defaultDocumentType,operationsLimit:BigInt(O.operationsLimit)})},[(0,Y.useMemo)(()=>O?JSON.stringify(O):null,[O]),X.reset]);let ge=(0,Y.useCallback)(async t=>{if(u||s){if(!O?.id)return;if(u){if(!t.billingCycle){X.setError(`billingCycle`,{message:`Billing cycle is required`});return}await R({input:{clientId:e,amount:{raw:t.paymentAmount,currency:t.paymentCurrency},billingCycle:t.billingCycle,documentType:t.defaultDocumentType,endDate:N(new Date(t.endDate),`yyyy-MM-dd`),isActive:t.isActive,msCloud:t.msCloudLink||null,plan:t.subscriptionPlan,product:t.productType,purchaseOrders:t.pos,remarks:t.defaultRemark,startDate:N(new Date(t.startDate),`yyyy-MM-dd`),operationsLimit:t.operationsLimit,deactivateContracts:h?[O.id]:void 0}})}else await F({contractId:O.id,input:{amount:{raw:t.paymentAmount,currency:t.paymentCurrency},billingCycle:t.billingCycle,documentType:t.defaultDocumentType,endDate:N(new Date(t.endDate),`yyyy-MM-dd`),isActive:t.isActive,msCloud:t.msCloudLink||null,plan:t.subscriptionPlan,product:t.productType,purchaseOrders:t.pos,remarks:t.defaultRemark,startDate:N(new Date(t.startDate),`yyyy-MM-dd`),operationsLimit:t.operationsLimit}})}else{if(!t.billingCycle){X.setError(`billingCycle`,{message:`Billing cycle is required`});return}await R({input:{clientId:e,amount:{raw:t.paymentAmount,currency:t.paymentCurrency},billingCycle:t.billingCycle,documentType:t.defaultDocumentType,endDate:N(new Date(t.endDate),`yyyy-MM-dd`),isActive:t.isActive,msCloud:t.msCloudLink||null,plan:t.subscriptionPlan,product:t.productType,purchaseOrders:t.pos,remarks:t.defaultRemark,startDate:N(new Date(t.startDate),`yyyy-MM-dd`),operationsLimit:t.operationsLimit}})}f(!1),i?.()},[O,i,R,F,e,X,s,u,h]);return(0,Q.jsxs)(ne,{open:d,onOpenChange:f,children:[(0,Q.jsx)(ie,{asChild:!0,children:s?(0,Q.jsx)(T,{variant:`ghost`,size:`sm`,onClick:()=>f(!0),children:(0,Q.jsx)(le,{className:`size-5`})}):u?(0,Q.jsx)(T,{variant:`ghost`,size:`sm`,onClick:()=>f(!0),children:(0,Q.jsx)(K,{className:`size-5`})}):(0,Q.jsxs)(T,{size:`sm`,onClick:()=>f(!0),children:[(0,Q.jsx)(C,{className:`size-4 mr-2`}),`New Contract`]})}),(0,Q.jsxs)(_,{className:`max-w-2xl max-h-[90vh] overflow-y-auto`,children:[(0,Q.jsxs)(re,{children:[(0,Q.jsx)(oe,{children:s?`Edit Contract`:`Create New Contract`}),(0,Q.jsx)(se,{children:s?`Update contract details`:u?`Create a new contract based on an existing one`:`Add a new contract with all required details`})]}),G?(0,Q.jsx)(Q.Fragment,{children:`Fetching contract details...`}):(0,Q.jsx)(L,{...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)(W,{control:X.control,name:`operationsLimit`,render:({field:e})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(B,{children:`Operations Limit`}),(0,Q.jsx)(A,{children:(0,Q.jsx)(x,{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)(z,{})]})}),(0,Q.jsx)(W,{control:X.control,name:`pos`,render:({field:e})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(B,{children:`Purchase Orders`}),(0,Q.jsxs)(`div`,{className:`flex gap-2`,children:[(0,Q.jsx)(x,{placeholder:`Add PO...`,value:j,onChange:e=>P(e.target.value),onKeyDown:e=>{e.key===`Enter`&&(e.preventDefault(),Z())}}),(0,Q.jsx)(T,{type:`button`,size:`sm`,onClick:Z,children:(0,Q.jsx)(C,{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)(de,{variant:`secondary`,className:`gap-1 max-w-xs truncate`,children:[t,n===(e.value?.length??0)-1&&(0,Q.jsx)(T,{variant:`ghost`,size:`icon`,className:`p-0 size-3`,onClick:()=>$(n),children:(0,Q.jsx)(v,{className:`size-3 cursor-pointer flex-shrink-0`})})]},t))}),(0,Q.jsx)(z,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(W,{control:X.control,name:`startDate`,render:({field:{onChange:e,...t}})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(B,{children:`Start Date`}),(0,Q.jsx)(A,{children:(0,Q.jsx)(x,{type:`date`,...t,onInput:t=>e(t)})}),(0,Q.jsx)(z,{})]})}),(0,Q.jsx)(W,{control:X.control,name:`endDate`,render:({field:{onChange:e,...t}})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(B,{children:`End Date`}),(0,Q.jsx)(A,{children:(0,Q.jsx)(x,{type:`date`,...t,onInput:t=>e(t)})}),(0,Q.jsx)(z,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-3`,children:[(0,Q.jsx)(W,{control:X.control,name:`paymentAmount`,render:({field:e})=>(0,Q.jsxs)(M,{className:`md:col-span-2`,children:[(0,Q.jsx)(B,{children:`Payment Amount`}),(0,Q.jsx)(A,{children:(0,Q.jsx)(x,{type:`number`,placeholder:`24000`,...e,onChange:t=>{e.onChange(t?.target.value?Number(t?.target.value):void 0)}})}),(0,Q.jsx)(z,{})]})}),(0,Q.jsx)(W,{control:X.control,name:`paymentCurrency`,render:({field:e})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(B,{children:`Currency`}),(0,Q.jsxs)(S,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(A,{children:(0,Q.jsx)(g,{children:(0,Q.jsx)(p,{})})}),(0,Q.jsx)(y,{children:Object.values(r).map(e=>(0,Q.jsx)(b,{value:e,children:e},e))})]}),(0,Q.jsx)(z,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(W,{control:X.control,name:`productType`,render:({field:e})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(B,{children:`Product Type`}),(0,Q.jsx)(A,{children:(0,Q.jsx)(x,{placeholder:`Cloud Services`,...e})}),(0,Q.jsx)(z,{})]})}),(0,Q.jsx)(W,{control:X.control,name:`billingCycle`,render:({field:e})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(B,{children:`Billing Cycle`}),(0,Q.jsxs)(S,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(A,{children:(0,Q.jsx)(g,{children:(0,Q.jsx)(p,{})})}),(0,Q.jsx)(y,{children:Object.values(l).map(e=>(0,Q.jsx)(b,{value:e,children:q(e)},e))})]}),(0,Q.jsx)(z,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(W,{control:X.control,name:`subscriptionPlan`,render:({field:e})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(B,{children:`Subscription Plan`}),(0,Q.jsx)(A,{children:(0,Q.jsxs)(S,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(A,{children:(0,Q.jsx)(g,{children:(0,Q.jsx)(p,{})})}),(0,Q.jsx)(y,{children:Object.values(o).map(e=>(0,Q.jsx)(b,{value:e,children:J(e)},e))})]})}),(0,Q.jsx)(z,{})]})}),(0,Q.jsx)(W,{control:X.control,name:`isActive`,render:({field:e})=>(0,Q.jsxs)(M,{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)(B,{className:`text-base`,children:`Active Status`})}),(0,Q.jsx)(A,{children:(0,Q.jsx)(w,{checked:e.value,onCheckedChange:e.onChange})})]})})]}),(0,Q.jsx)(W,{control:X.control,name:`msCloudLink`,render:({field:e})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(B,{children:`MS Cloud Link`}),(0,Q.jsx)(A,{children:(0,Q.jsx)(x,{type:`url`,placeholder:`https://portal.azure.com/contract-id`,...e})}),(0,Q.jsx)(z,{})]})}),(0,Q.jsx)(W,{control:X.control,name:`defaultDocumentType`,render:({field:e})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(B,{children:`Default Document Type`}),(0,Q.jsxs)(S,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(A,{children:(0,Q.jsx)(g,{children:(0,Q.jsx)(p,{})})}),(0,Q.jsx)(y,{children:Object.values(c).map(e=>(0,Q.jsx)(b,{value:e,children:ce(e)},e))})]}),(0,Q.jsx)(z,{})]})}),(0,Q.jsx)(W,{control:X.control,name:`defaultRemark`,render:({field:e})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(B,{children:`Default Remark`}),(0,Q.jsx)(A,{children:(0,Q.jsx)(ue,{placeholder:`Enter default remark for this contract...`,rows:3,...e})}),(0,Q.jsx)(z,{})]})}),u&&(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)(ae,{className:`text-base`,children:`Deactivate Original Contract`})}),(0,Q.jsx)(w,{checked:h,onCheckedChange:D})]})]}),(0,Q.jsxs)(m,{children:[(0,Q.jsx)(T,{type:`button`,variant:`outline`,onClick:()=>f(!1),children:`Cancel`}),(0,Q.jsx)(T,{type:`submit`,disabled:H||I,children:s?`Update Contract`:`Create Contract`})]})]})})]})]})}export{q as n,J as r,$ as t};