UNPKG

@accounter/client

Version:
1 lines • 12.8 kB
import{s as e}from"./dist-ypP48_Ax.js";import{i as t,o as n}from"./utils-CbS-oR5I.js";import{r,t as i}from"./error-handling-CKe_iwSC.js";import{t as a}from"./createLucideIcon-BK8Z-P8J.js";import{er as o,yr as s,yt as c}from"./common-CXB0UgWw.js";import{a as l,c as ee,d as te,f as u,i as d,l as ne,lt as re,m as ie,n as f,o as ae,p as oe,r as p,s as m,t as h,u as g,ut as _}from"./select-DrAHsHyM.js";import{o as v}from"./separator-CQBUMn7G.js";import{t as y}from"./button-DUNsVBiu.js";import{Bc as se,Cn as b,Fr as x,Gc as S,Hc as C,Ht as w,In as T,Kc as E,Kl as D,Ln as O,Rt as k,Vc as ce,Vt as A,Wc as j,Xn as M,Zc as N,dr as P,f as F,jt as I,qc as L,ur as R}from"./table-Dje35oxp.js";import{Bi as z,In as B,Pa as V,at as H,kn as U,ls as W,mo as G,nr as K,us as le,wn as ue}from"./graphql-BIob9ctl.js";var de=a(`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 H.Annual:return`Annual`;case H.Monthly:return`Monthly`;default:throw Error(`Unsupported Billing Cycle: ${e}`)}}function J(e){switch(e){case V.Enterprise:return`Enterprise`;case V.Pro:return`Pro`;default:throw Error(`Unsupported Subscription plan: ${e}`)}}var Y=e(n(),1),X=`createContract`,fe=()=>{let[{fetching:e},t]=W(U);return{creating:e,createContract:(0,Y.useCallback)(async e=>{let n=`Error creating new contract for client[${e.input.clientId}]`,a=`${X}-${e.input.clientId}`;r.loading(`Creating contract`,{id:a});try{let o=await t(e);i(o,n,a)&&r.success(`Success`,{id:a,description:`Contract for client ${e.input.clientId} was successfully created`})}catch(e){console.error(`${n}: ${e}`),r.error(`Error`,{id:a,description:n,duration:1e5,closeButton:!0})}},[t])}},Z=`updateContract`,pe=()=>{let[{fetching:e},t]=W(G);return{updating:e,updateContract:(0,Y.useCallback)(async e=>{let n=`Error updating contract ID [${e.contractId}]`,a=`${Z}-${e.contractId}`;r.loading(`Updating contract`,{id:a});try{let o=await t(e),s=i(o,n,a,`updateContract`);if(s)return r.success(`Success`,{id:a,description:`Contract updated`}),s.updateContract}catch(e){console.error(`${n}: ${e}`),r.error(`Error`,{id:a,description:n,duration:1e5,closeButton:!0})}},[t])}},Q=t(),me=O({id:P().optional(),startDate:x(`Start date is required`),endDate:x(`End date is required`),pos:k(M()),paymentAmount:T().min(0,`Payment amount must be non-negative`),paymentCurrency:I(Object.values(B),`Currency is required`),productType:I(Object.values(z)).optional(),msCloudLink:R().optional().or(b(``)),billingCycle:I(Object.values(H)).optional(),subscriptionPlan:I(Object.values(V)).optional(),isActive:w(),defaultRemark:M().optional(),defaultDocumentType:I(Object.values(K)),operationsLimit:A().min(BigInt(0),`Operations limit must be non-negative`)}),he={startDate:``,endDate:``,pos:[],paymentAmount:0,paymentCurrency:B.Usd,productType:z.Hive,msCloudLink:void 0,billingCycle:H.Monthly,subscriptionPlan:void 0,isActive:!0,defaultRemark:void 0,defaultDocumentType:K.Proforma,operationsLimit:BigInt(0)};function $({clientId:e,contract:t,contractId:n,onDone:r,duplicate:i}){let{isEditing:a,isDuplicating:b}=(0,Y.useMemo)(()=>{let e=!!t||!!n,r=i===!0&&e;return{isEditing:e&&!r,isDuplicating:r}},[t,n,i]),[x,w]=(0,Y.useState)(!1),[T,O]=(0,Y.useState)(!1),[k,A]=(0,Y.useState)(he),[M,P]=(0,Y.useState)(``),{updateContract:I,updating:R}=pe(),{createContract:z,creating:U}=fe(),[{data:W,fetching:G}]=le({query:ue,pause:!n||!x,variables:{contractId:n??``}}),X=N({resolver:F(me),defaultValues:k}),Z=()=>{let e=M.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&&A(t)},[t]),(0,Y.useEffect)(()=>{if(W?.contractsById){let e=W.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})}},[W]);let ge=(0,Y.useMemo)(()=>k?JSON.stringify(k):null,[k]);(0,Y.useEffect)(()=>{k&&X.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,X.reset]);let _e=(0,Y.useCallback)(async t=>{if(b||a){if(!k?.id)return;if(b){if(!t.billingCycle){X.setError(`billingCycle`,{message:`Billing cycle is required`});return}await z({input:{clientId:e,amount:{raw:t.paymentAmount,currency:t.paymentCurrency},billingCycle:t.billingCycle,documentType:t.defaultDocumentType,endDate:D(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:D(new Date(t.startDate),`yyyy-MM-dd`),operationsLimit:t.operationsLimit,deactivateContracts:T?[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:D(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:D(new Date(t.startDate),`yyyy-MM-dd`),operationsLimit:t.operationsLimit}})}else{if(!t.billingCycle){X.setError(`billingCycle`,{message:`Billing cycle is required`});return}await z({input:{clientId:e,amount:{raw:t.paymentAmount,currency:t.paymentCurrency},billingCycle:t.billingCycle,documentType:t.defaultDocumentType,endDate:D(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:D(new Date(t.startDate),`yyyy-MM-dd`),operationsLimit:t.operationsLimit}})}w(!1),r?.()},[k,r,z,I,e,X,a,b,T]);return(0,Q.jsxs)(ee,{open:x,onOpenChange:w,children:[(0,Q.jsx)(ie,{asChild:!0,children:a?(0,Q.jsx)(y,{variant:`ghost`,size:`sm`,onClick:()=>w(!0),children:(0,Q.jsx)(s,{className:`size-5`})}):b?(0,Q.jsx)(y,{variant:`ghost`,size:`sm`,onClick:()=>w(!0),children:(0,Q.jsx)(de,{className:`size-5`})}):(0,Q.jsxs)(y,{size:`sm`,onClick:()=>w(!0),children:[(0,Q.jsx)(_,{className:`size-4 mr-2`}),`New Contract`]})}),(0,Q.jsxs)(ne,{className:`max-w-2xl max-h-[90vh] overflow-y-auto`,children:[(0,Q.jsxs)(u,{children:[(0,Q.jsx)(oe,{children:a?`Edit Contract`:`Create New Contract`}),(0,Q.jsx)(g,{children:a?`Update contract details`:b?`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)(ce,{...X,children:(0,Q.jsxs)(`form`,{onSubmit:X.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)(j,{control:X.control,name:`operationsLimit`,render:({field:e})=>(0,Q.jsxs)(S,{children:[(0,Q.jsx)(E,{children:`Operations Limit`}),(0,Q.jsx)(C,{children:(0,Q.jsx)(m,{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)(L,{})]})}),(0,Q.jsx)(j,{control:X.control,name:`pos`,render:({field:e})=>(0,Q.jsxs)(S,{children:[(0,Q.jsx)(E,{children:`Purchase Orders`}),(0,Q.jsxs)(`div`,{className:`flex gap-2`,children:[(0,Q.jsx)(m,{placeholder:`Add PO...`,value:M,onChange:e=>P(e.target.value),onKeyDown:e=>{e.key===`Enter`&&(e.preventDefault(),Z())}}),(0,Q.jsx)(y,{type:`button`,size:`sm`,onClick:Z,children:(0,Q.jsx)(_,{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)(se,{variant:`secondary`,className:`gap-1 max-w-xs truncate`,children:[t,n===(e.value?.length??0)-1&&(0,Q.jsx)(y,{variant:`ghost`,size:`icon`,className:`p-0 size-3`,onClick:()=>$(n),children:(0,Q.jsx)(re,{className:`size-3 cursor-pointer flex-shrink-0`})})]},t))}),(0,Q.jsx)(L,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(j,{control:X.control,name:`startDate`,render:({field:{onChange:e,...t}})=>(0,Q.jsxs)(S,{children:[(0,Q.jsx)(E,{children:`Start Date`}),(0,Q.jsx)(C,{children:(0,Q.jsx)(m,{type:`date`,...t,onInput:t=>e(t)})}),(0,Q.jsx)(L,{})]})}),(0,Q.jsx)(j,{control:X.control,name:`endDate`,render:({field:{onChange:e,...t}})=>(0,Q.jsxs)(S,{children:[(0,Q.jsx)(E,{children:`End Date`}),(0,Q.jsx)(C,{children:(0,Q.jsx)(m,{type:`date`,...t,onInput:t=>e(t)})}),(0,Q.jsx)(L,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-3`,children:[(0,Q.jsx)(j,{control:X.control,name:`paymentAmount`,render:({field:e})=>(0,Q.jsxs)(S,{className:`md:col-span-2`,children:[(0,Q.jsx)(E,{children:`Payment Amount`}),(0,Q.jsx)(C,{children:(0,Q.jsx)(m,{type:`number`,placeholder:`24000`,...e,onChange:t=>{e.onChange(t?.target.value?Number(t?.target.value):void 0)}})}),(0,Q.jsx)(L,{})]})}),(0,Q.jsx)(j,{control:X.control,name:`paymentCurrency`,render:({field:e})=>(0,Q.jsxs)(S,{children:[(0,Q.jsx)(E,{children:`Currency`}),(0,Q.jsxs)(h,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(C,{children:(0,Q.jsx)(d,{children:(0,Q.jsx)(l,{})})}),(0,Q.jsx)(f,{children:Object.values(B).map(e=>(0,Q.jsx)(p,{value:e,children:e},e))})]}),(0,Q.jsx)(L,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(j,{control:X.control,name:`productType`,render:({field:e})=>(0,Q.jsxs)(S,{children:[(0,Q.jsx)(E,{children:`Product Type`}),(0,Q.jsx)(C,{children:(0,Q.jsx)(m,{placeholder:`Cloud Services`,...e})}),(0,Q.jsx)(L,{})]})}),(0,Q.jsx)(j,{control:X.control,name:`billingCycle`,render:({field:e})=>(0,Q.jsxs)(S,{children:[(0,Q.jsx)(E,{children:`Billing Cycle`}),(0,Q.jsxs)(h,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(C,{children:(0,Q.jsx)(d,{children:(0,Q.jsx)(l,{})})}),(0,Q.jsx)(f,{children:Object.values(H).map(e=>(0,Q.jsx)(p,{value:e,children:q(e)},e))})]}),(0,Q.jsx)(L,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(j,{control:X.control,name:`subscriptionPlan`,render:({field:e})=>(0,Q.jsxs)(S,{children:[(0,Q.jsx)(E,{children:`Subscription Plan`}),(0,Q.jsx)(C,{children:(0,Q.jsxs)(h,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(C,{children:(0,Q.jsx)(d,{children:(0,Q.jsx)(l,{})})}),(0,Q.jsx)(f,{children:Object.values(V).map(e=>(0,Q.jsx)(p,{value:e,children:J(e)},e))})]})}),(0,Q.jsx)(L,{})]})}),(0,Q.jsx)(j,{control:X.control,name:`isActive`,render:({field:e})=>(0,Q.jsxs)(S,{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)(E,{className:`text-base`,children:`Active Status`})}),(0,Q.jsx)(C,{children:(0,Q.jsx)(c,{checked:e.value,onCheckedChange:e.onChange})})]})})]}),(0,Q.jsx)(j,{control:X.control,name:`msCloudLink`,render:({field:e})=>(0,Q.jsxs)(S,{children:[(0,Q.jsx)(E,{children:`MS Cloud Link`}),(0,Q.jsx)(C,{children:(0,Q.jsx)(m,{type:`url`,placeholder:`https://portal.azure.com/contract-id`,...e})}),(0,Q.jsx)(L,{})]})}),(0,Q.jsx)(j,{control:X.control,name:`defaultDocumentType`,render:({field:e})=>(0,Q.jsxs)(S,{children:[(0,Q.jsx)(E,{children:`Default Document Type`}),(0,Q.jsxs)(h,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(C,{children:(0,Q.jsx)(d,{children:(0,Q.jsx)(l,{})})}),(0,Q.jsx)(f,{children:Object.values(K).map(e=>(0,Q.jsx)(p,{value:e,children:o(e)},e))})]}),(0,Q.jsx)(L,{})]})}),(0,Q.jsx)(j,{control:X.control,name:`defaultRemark`,render:({field:e})=>(0,Q.jsxs)(S,{children:[(0,Q.jsx)(E,{children:`Default Remark`}),(0,Q.jsx)(C,{children:(0,Q.jsx)(v,{placeholder:`Enter default remark for this contract...`,rows:3,...e})}),(0,Q.jsx)(L,{})]})}),b&&(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)(c,{checked:T,onCheckedChange:O})]})]}),(0,Q.jsxs)(te,{children:[(0,Q.jsx)(y,{type:`button`,variant:`outline`,onClick:()=>w(!1),children:`Cancel`}),(0,Q.jsx)(y,{type:`submit`,disabled:U||R,children:a?`Update Contract`:`Create Contract`})]})]})})]})]})}export{q as n,J as r,$ as t};