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