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{n as a}from"./loader-circle-CfW9jOIr.js";import{Xn as o,hr as s,vt as c}from"./common-D-vYt_Yl.js";import{a as l,c as ee,d as u,f as te,i as d,l as ne,lt as f,m as re,n as p,o as ie,p as ae,r as m,s as h,t as g,u as oe,ut as _}from"./select-Zdatwv1b.js";import{o as se}from"./separator-BvKylNhK.js";import{t as v}from"./button-DUNsVBiu.js";import{Bc as ce,Cn as y,Fr as b,Gc as x,Hc as S,Ht as C,In as w,Kc as T,Ln as E,Rt as D,Ul as O,Vc as le,Vt as k,Wc as A,Xc as ue,Xn as j,dr as M,f as de,jt as N,qc as P,ur as F}from"./table-AJA7Uyxy.js";import{Dn as I,Pn as L,Qo as R,Sa as z,Sn as B,Zo as V,er as H,it as U,ji as W,no as G}from"./graphql-DC5GYCFE.js";var fe=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 K(e){switch(e){case U.Annual:return`Annual`;case U.Monthly:return`Monthly`;default:throw Error(`Unsupported Billing Cycle: ${e}`)}}function q(e){switch(e){case z.Enterprise:return`Enterprise`;case z.Pro:return`Pro`;default:throw Error(`Unsupported Subscription plan: ${e}`)}}var J=e(n(),1),Y=`createContract`,pe=()=>{let[{fetching:e},t]=V(I);return{creating:e,createContract:(0,J.useCallback)(async e=>{let n=`Error creating new contract for client[${e.input.clientId}]`,a=`${Y}-${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])}},X=`updateContract`,Z=()=>{let[{fetching:e},t]=V(G);return{updating:e,updateContract:(0,J.useCallback)(async e=>{let n=`Error updating contract ID [${e.contractId}]`,a=`${X}-${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=E({id:M().optional(),startDate:b(`Start date is required`),endDate:b(`End date is required`),pos:D(j()),paymentAmount:w().min(0,`Payment amount must be non-negative`),paymentCurrency:N(Object.values(L),`Currency is required`),productType:N(Object.values(W)).optional(),msCloudLink:F().optional().or(y(``)),billingCycle:N(Object.values(U)).optional(),subscriptionPlan:N(Object.values(z)).optional(),isActive:C(),defaultRemark:j().optional(),defaultDocumentType:N(Object.values(H)),operationsLimit:k().min(BigInt(0),`Operations limit must be non-negative`)}),he={startDate:``,endDate:``,pos:[],paymentAmount:0,paymentCurrency:L.Usd,productType:W.Hive,msCloudLink:void 0,billingCycle:U.Monthly,subscriptionPlan:void 0,isActive:!0,defaultRemark:void 0,defaultDocumentType:H.Proforma,operationsLimit:BigInt(0)};function $({clientId:e,contract:t,contractId:n,onDone:r,duplicate:i}){let{isEditing:a,isDuplicating:y}=(0,J.useMemo)(()=>{let e=!!t||!!n,r=i===!0&&e;return{isEditing:e&&!r,isDuplicating:r}},[t,n,i]),[b,C]=(0,J.useState)(!1),[w,E]=(0,J.useState)(!1),[D,k]=(0,J.useState)(he),[j,M]=(0,J.useState)(``),{updateContract:N,updating:F}=Z(),{createContract:I,creating:V}=pe(),[{data:W,fetching:G}]=R({query:B,pause:!n||!b,variables:{contractId:n??``}}),Y=ue({resolver:de(me),defaultValues:D}),X=()=>{let e=j.trim();if(e){let t=Y.getValues(`pos`);t.includes(e)||Y.setValue(`pos`,[...t,e],{shouldDirty:!0}),M(``)}},$=e=>{let t=Y.getValues(`pos`);Y.setValue(`pos`,t.filter((t,n)=>n!==e),{shouldDirty:!0})};(0,J.useEffect)(()=>{t&&k(t)},[t]),(0,J.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)};k({id:e.id,...t})}},[W]);let ge=(0,J.useMemo)(()=>D?JSON.stringify(D):null,[D]);(0,J.useEffect)(()=>{D&&Y.reset({startDate:D.startDate,endDate:D.endDate,pos:D.pos,paymentAmount:D.paymentAmount,paymentCurrency:D.paymentCurrency,productType:D.productType,msCloudLink:D.msCloudLink,billingCycle:D.billingCycle,subscriptionPlan:D.subscriptionPlan,isActive:D.isActive,defaultRemark:D.defaultRemark,defaultDocumentType:D.defaultDocumentType,operationsLimit:BigInt(D.operationsLimit)})},[ge,Y.reset]);let _e=(0,J.useCallback)(async t=>{if(y||a){if(!D?.id)return;if(y){if(!t.billingCycle){Y.setError(`billingCycle`,{message:`Billing cycle is required`});return}await I({input:{clientId:e,amount:{raw:t.paymentAmount,currency:t.paymentCurrency},billingCycle:t.billingCycle,documentType:t.defaultDocumentType,endDate:O(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:O(new Date(t.startDate),`yyyy-MM-dd`),operationsLimit:t.operationsLimit,deactivateContracts:w?[D.id]:void 0}})}else await N({contractId:D.id,input:{amount:{raw:t.paymentAmount,currency:t.paymentCurrency},billingCycle:t.billingCycle,documentType:t.defaultDocumentType,endDate:O(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:O(new Date(t.startDate),`yyyy-MM-dd`),operationsLimit:t.operationsLimit}})}else{if(!t.billingCycle){Y.setError(`billingCycle`,{message:`Billing cycle is required`});return}await I({input:{clientId:e,amount:{raw:t.paymentAmount,currency:t.paymentCurrency},billingCycle:t.billingCycle,documentType:t.defaultDocumentType,endDate:O(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:O(new Date(t.startDate),`yyyy-MM-dd`),operationsLimit:t.operationsLimit}})}C(!1),r?.()},[D,r,I,N,e,Y,a,y,w]);return(0,Q.jsxs)(ee,{open:b,onOpenChange:C,children:[(0,Q.jsx)(re,{asChild:!0,children:a?(0,Q.jsx)(v,{variant:`ghost`,size:`sm`,onClick:()=>C(!0),children:(0,Q.jsx)(s,{className:`size-5`})}):y?(0,Q.jsx)(v,{variant:`ghost`,size:`sm`,onClick:()=>C(!0),children:(0,Q.jsx)(fe,{className:`size-5`})}):(0,Q.jsxs)(v,{size:`sm`,onClick:()=>C(!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)(te,{children:[(0,Q.jsx)(ae,{children:a?`Edit Contract`:`Create New Contract`}),(0,Q.jsx)(oe,{children:a?`Update contract details`:y?`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)(le,{...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)(A,{control:Y.control,name:`operationsLimit`,render:({field:e})=>(0,Q.jsxs)(x,{children:[(0,Q.jsx)(T,{children:`Operations Limit`}),(0,Q.jsx)(S,{children:(0,Q.jsx)(h,{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)(P,{})]})}),(0,Q.jsx)(A,{control:Y.control,name:`pos`,render:({field:e})=>(0,Q.jsxs)(x,{children:[(0,Q.jsx)(T,{children:`Purchase Orders`}),(0,Q.jsxs)(`div`,{className:`flex gap-2`,children:[(0,Q.jsx)(h,{placeholder:`Add PO...`,value:j,onChange:e=>M(e.target.value),onKeyDown:e=>{e.key===`Enter`&&(e.preventDefault(),X())}}),(0,Q.jsx)(v,{type:`button`,size:`sm`,onClick:X,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)(ce,{variant:`secondary`,className:`gap-1 max-w-xs truncate`,children:[t,n===(e.value?.length??0)-1&&(0,Q.jsx)(v,{variant:`ghost`,size:`icon`,className:`p-0 size-3`,onClick:()=>$(n),children:(0,Q.jsx)(f,{className:`size-3 cursor-pointer flex-shrink-0`})})]},t))}),(0,Q.jsx)(P,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(A,{control:Y.control,name:`startDate`,render:({field:{onChange:e,...t}})=>(0,Q.jsxs)(x,{children:[(0,Q.jsx)(T,{children:`Start Date`}),(0,Q.jsx)(S,{children:(0,Q.jsx)(h,{type:`date`,...t,onInput:t=>e(t)})}),(0,Q.jsx)(P,{})]})}),(0,Q.jsx)(A,{control:Y.control,name:`endDate`,render:({field:{onChange:e,...t}})=>(0,Q.jsxs)(x,{children:[(0,Q.jsx)(T,{children:`End Date`}),(0,Q.jsx)(S,{children:(0,Q.jsx)(h,{type:`date`,...t,onInput:t=>e(t)})}),(0,Q.jsx)(P,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-3`,children:[(0,Q.jsx)(A,{control:Y.control,name:`paymentAmount`,render:({field:e})=>(0,Q.jsxs)(x,{className:`md:col-span-2`,children:[(0,Q.jsx)(T,{children:`Payment Amount`}),(0,Q.jsx)(S,{children:(0,Q.jsx)(h,{type:`number`,placeholder:`24000`,...e,onChange:t=>{e.onChange(t?.target.value?Number(t?.target.value):void 0)}})}),(0,Q.jsx)(P,{})]})}),(0,Q.jsx)(A,{control:Y.control,name:`paymentCurrency`,render:({field:e})=>(0,Q.jsxs)(x,{children:[(0,Q.jsx)(T,{children:`Currency`}),(0,Q.jsxs)(g,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(S,{children:(0,Q.jsx)(d,{children:(0,Q.jsx)(l,{})})}),(0,Q.jsx)(p,{children:Object.values(L).map(e=>(0,Q.jsx)(m,{value:e,children:e},e))})]}),(0,Q.jsx)(P,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(A,{control:Y.control,name:`productType`,render:({field:e})=>(0,Q.jsxs)(x,{children:[(0,Q.jsx)(T,{children:`Product Type`}),(0,Q.jsx)(S,{children:(0,Q.jsx)(h,{placeholder:`Cloud Services`,...e})}),(0,Q.jsx)(P,{})]})}),(0,Q.jsx)(A,{control:Y.control,name:`billingCycle`,render:({field:e})=>(0,Q.jsxs)(x,{children:[(0,Q.jsx)(T,{children:`Billing Cycle`}),(0,Q.jsxs)(g,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(S,{children:(0,Q.jsx)(d,{children:(0,Q.jsx)(l,{})})}),(0,Q.jsx)(p,{children:Object.values(U).map(e=>(0,Q.jsx)(m,{value:e,children:K(e)},e))})]}),(0,Q.jsx)(P,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(A,{control:Y.control,name:`subscriptionPlan`,render:({field:e})=>(0,Q.jsxs)(x,{children:[(0,Q.jsx)(T,{children:`Subscription Plan`}),(0,Q.jsx)(S,{children:(0,Q.jsxs)(g,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(S,{children:(0,Q.jsx)(d,{children:(0,Q.jsx)(l,{})})}),(0,Q.jsx)(p,{children:Object.values(z).map(e=>(0,Q.jsx)(m,{value:e,children:q(e)},e))})]})}),(0,Q.jsx)(P,{})]})}),(0,Q.jsx)(A,{control:Y.control,name:`isActive`,render:({field:e})=>(0,Q.jsxs)(x,{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)(T,{className:`text-base`,children:`Active Status`})}),(0,Q.jsx)(S,{children:(0,Q.jsx)(c,{checked:e.value,onCheckedChange:e.onChange})})]})})]}),(0,Q.jsx)(A,{control:Y.control,name:`msCloudLink`,render:({field:e})=>(0,Q.jsxs)(x,{children:[(0,Q.jsx)(T,{children:`MS Cloud Link`}),(0,Q.jsx)(S,{children:(0,Q.jsx)(h,{type:`url`,placeholder:`https://portal.azure.com/contract-id`,...e})}),(0,Q.jsx)(P,{})]})}),(0,Q.jsx)(A,{control:Y.control,name:`defaultDocumentType`,render:({field:e})=>(0,Q.jsxs)(x,{children:[(0,Q.jsx)(T,{children:`Default Document Type`}),(0,Q.jsxs)(g,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(S,{children:(0,Q.jsx)(d,{children:(0,Q.jsx)(l,{})})}),(0,Q.jsx)(p,{children:Object.values(H).map(e=>(0,Q.jsx)(m,{value:e,children:o(e)},e))})]}),(0,Q.jsx)(P,{})]})}),(0,Q.jsx)(A,{control:Y.control,name:`defaultRemark`,render:({field:e})=>(0,Q.jsxs)(x,{children:[(0,Q.jsx)(T,{children:`Default Remark`}),(0,Q.jsx)(S,{children:(0,Q.jsx)(se,{placeholder:`Enter default remark for this contract...`,rows:3,...e})}),(0,Q.jsx)(P,{})]})}),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)(ie,{className:`text-base`,children:`Deactivate Original Contract`})}),(0,Q.jsx)(c,{checked:w,onCheckedChange:E})]})]}),(0,Q.jsxs)(u,{children:[(0,Q.jsx)(v,{type:`button`,variant:`outline`,onClick:()=>C(!1),children:`Cancel`}),(0,Q.jsx)(v,{type:`submit`,disabled:V||F,children:a?`Update Contract`:`Create Contract`})]})]})})]})]})}export{K as n,q as r,$ as t};