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