UNPKG

@accounter/client

Version:
1 lines • 12.4 kB
import{s as e}from"./dist-C51EwTaa.js";import{i as t,o as n}from"./utils-DdVdMk8X.js";import{En as r,Ga as i,Io as a,Lo as o,Rn as s,at as c,da as l,jn as u,r as d,rr as f,t as p,xi as m}from"./error-handling-Dpfd9svJ.js";import{_t as h,a as g,c as _,d as ee,dt as v,f as te,i as y,l as b,m as ne,n as x,o as re,p as S,r as C,s as w,t as T,u as ie,ut as E}from"./select-CrsvQyhZ.js";import{t as D}from"./button-Dzp2INW0.js";import{A as O,B as k,D as A,E as j,F as M,L as N,Lt as P,M as F,N as I,P as L,R,S as z,T as B,U as ae,b as V,h as H,j as U,k as W,v as G,x as K,z as q}from"./table--bF0wbxq.js";import{Cr as oe,It as J,Xr as se,kt as ce,vr as le,wr as ue}from"./index-CL8-JoMZ.js";var de=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`}]]),Y=e(n(),1),X=`createContract`,fe=()=>{let[{fetching:e},t]=a(u);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}`;d.loading(`Creating contract`,{id:r});try{p(await t(e),n,r)&&d.success(`Success`,{id:r,description:`Contract for client ${e.input.clientId} was successfully created`})}catch(e){console.error(`${n}: ${e}`),d.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}`;d.loading(`Updating contract`,{id:r});try{let i=p(await t(e),n,r,`updateContract`);if(i)return d.success(`Success`,{id:r,description:`Contract updated`}),i.updateContract}catch(e){console.error(`${n}: ${e}`),d.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}},[t])}},Q=t(),me=A({id:U().optional(),startDate:F(`Start date is required`),endDate:F(`End date is required`),pos:V(W()),paymentAmount:j().min(0,`Payment amount must be non-negative`),paymentCurrency:G(Object.values(s),`Currency is required`),productType:G(Object.values(m)).optional(),msCloudLink:O().optional().or(B(``)),billingCycle:G(Object.values(c)).optional(),subscriptionPlan:G(Object.values(l)).optional(),isActive:z(),defaultRemark:W().optional(),defaultDocumentType:G(Object.values(f)),operationsLimit:K().min(BigInt(0),`Operations limit must be non-negative`)}),he={startDate:``,endDate:``,pos:[],paymentAmount:0,paymentCurrency:s.Usd,productType:m.Hive,msCloudLink:void 0,billingCycle:c.Monthly,subscriptionPlan:void 0,isActive:!0,defaultRemark:void 0,defaultDocumentType:f.Proforma,operationsLimit:BigInt(0)};function $({clientId:e,contract:t,contractId:n,onDone:i,duplicate:a}){let{isEditing:u,isDuplicating:d}=(0,Y.useMemo)(()=>{let e=!!t||!!n,r=a===!0&&e;return{isEditing:e&&!r,isDuplicating:r}},[t,n,a]),[p,m]=(0,Y.useState)(!1),[h,O]=(0,Y.useState)(!1),[A,j]=(0,Y.useState)(he),[F,z]=(0,Y.useState)(``),{updateContract:B,updating:V}=pe(),{createContract:U,creating:W}=fe(),[{data:G,fetching:K}]=o({query:r,pause:!n||!p,variables:{contractId:n??``}}),X=ae({resolver:H(me),defaultValues:A}),Z=()=>{let e=F.trim();if(e){let t=X.getValues(`pos`);t.includes(e)||X.setValue(`pos`,[...t,e],{shouldDirty:!0}),z(``)}},$=e=>{let t=X.getValues(`pos`);X.setValue(`pos`,t.filter((t,n)=>n!==e),{shouldDirty:!0})};(0,Y.useEffect)(()=>{t&&j(t)},[t]),(0,Y.useEffect)(()=>{if(G?.contractsById){let e=G.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)};j({id:e.id,...t})}},[G]),(0,Y.useEffect)(()=>{A&&X.reset({startDate:A.startDate,endDate:A.endDate,pos:A.pos,paymentAmount:A.paymentAmount,paymentCurrency:A.paymentCurrency,productType:A.productType,msCloudLink:A.msCloudLink,billingCycle:A.billingCycle,subscriptionPlan:A.subscriptionPlan,isActive:A.isActive,defaultRemark:A.defaultRemark,defaultDocumentType:A.defaultDocumentType,operationsLimit:BigInt(A.operationsLimit)})},[(0,Y.useMemo)(()=>A?JSON.stringify(A):null,[A]),X.reset]);let ge=(0,Y.useCallback)(async t=>{if(d||u){if(!A?.id)return;if(d){if(!t.billingCycle){X.setError(`billingCycle`,{message:`Billing cycle is required`});return}await U({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,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:h?[A.id]:void 0}})}else await B({contractId:A.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,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){X.setError(`billingCycle`,{message:`Billing cycle is required`});return}await U({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,plan:t.subscriptionPlan,product:t.productType,purchaseOrders:t.pos,remarks:t.defaultRemark,startDate:P(new Date(t.startDate),`yyyy-MM-dd`),operationsLimit:t.operationsLimit}})}m(!1),i?.()},[A,i,U,B,e,X,u,d,h]);return(0,Q.jsxs)(_,{open:p,onOpenChange:m,children:[(0,Q.jsx)(ne,{asChild:!0,children:u?(0,Q.jsx)(D,{variant:`ghost`,size:`sm`,onClick:()=>m(!0),children:(0,Q.jsx)(se,{className:`size-5`})}):d?(0,Q.jsx)(D,{variant:`ghost`,size:`sm`,onClick:()=>m(!0),children:(0,Q.jsx)(de,{className:`size-5`})}):(0,Q.jsxs)(D,{size:`sm`,onClick:()=>m(!0),children:[(0,Q.jsx)(v,{className:`size-4 mr-2`}),`New Contract`]})}),(0,Q.jsxs)(b,{className:`max-w-2xl max-h-[90vh] overflow-y-auto`,children:[(0,Q.jsxs)(te,{children:[(0,Q.jsx)(S,{children:u?`Edit Contract`:`Create New Contract`}),(0,Q.jsx)(ie,{children:u?`Update contract details`:d?`Create a new contract based on an existing one`:`Add a new contract with all required details`})]}),K?(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)(N,{control:X.control,name:`operationsLimit`,render:({field:e})=>(0,Q.jsxs)(R,{children:[(0,Q.jsx)(q,{children:`Operations Limit`}),(0,Q.jsx)(M,{children:(0,Q.jsx)(w,{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)(k,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`pos`,render:({field:e})=>(0,Q.jsxs)(R,{children:[(0,Q.jsx)(q,{children:`Purchase Orders`}),(0,Q.jsxs)(`div`,{className:`flex gap-2`,children:[(0,Q.jsx)(w,{placeholder:`Add PO...`,value:F,onChange:e=>z(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)(v,{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)(I,{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)(E,{className:`size-3 cursor-pointer flex-shrink-0`})})]},t))}),(0,Q.jsx)(k,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(N,{control:X.control,name:`startDate`,render:({field:{onChange:e,...t}})=>(0,Q.jsxs)(R,{children:[(0,Q.jsx)(q,{children:`Start Date`}),(0,Q.jsx)(M,{children:(0,Q.jsx)(w,{type:`date`,...t,onInput:t=>e(t)})}),(0,Q.jsx)(k,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`endDate`,render:({field:{onChange:e,...t}})=>(0,Q.jsxs)(R,{children:[(0,Q.jsx)(q,{children:`End Date`}),(0,Q.jsx)(M,{children:(0,Q.jsx)(w,{type:`date`,...t,onInput:t=>e(t)})}),(0,Q.jsx)(k,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-3`,children:[(0,Q.jsx)(N,{control:X.control,name:`paymentAmount`,render:({field:e})=>(0,Q.jsxs)(R,{className:`md:col-span-2`,children:[(0,Q.jsx)(q,{children:`Payment Amount`}),(0,Q.jsx)(M,{children:(0,Q.jsx)(w,{type:`number`,placeholder:`24000`,...e,onChange:t=>{e.onChange(t?.target.value?Number(t?.target.value):void 0)}})}),(0,Q.jsx)(k,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`paymentCurrency`,render:({field:e})=>(0,Q.jsxs)(R,{children:[(0,Q.jsx)(q,{children:`Currency`}),(0,Q.jsxs)(T,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(M,{children:(0,Q.jsx)(y,{children:(0,Q.jsx)(g,{})})}),(0,Q.jsx)(x,{children:Object.values(s).map(e=>(0,Q.jsx)(C,{value:e,children:e},e))})]}),(0,Q.jsx)(k,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(N,{control:X.control,name:`productType`,render:({field:e})=>(0,Q.jsxs)(R,{children:[(0,Q.jsx)(q,{children:`Product Type`}),(0,Q.jsx)(M,{children:(0,Q.jsx)(w,{placeholder:`Cloud Services`,...e})}),(0,Q.jsx)(k,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`billingCycle`,render:({field:e})=>(0,Q.jsxs)(R,{children:[(0,Q.jsx)(q,{children:`Billing Cycle`}),(0,Q.jsxs)(T,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(M,{children:(0,Q.jsx)(y,{children:(0,Q.jsx)(g,{})})}),(0,Q.jsx)(x,{children:Object.values(c).map(e=>(0,Q.jsx)(C,{value:e,children:oe(e)},e))})]}),(0,Q.jsx)(k,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(N,{control:X.control,name:`subscriptionPlan`,render:({field:e})=>(0,Q.jsxs)(R,{children:[(0,Q.jsx)(q,{children:`Subscription Plan`}),(0,Q.jsx)(M,{children:(0,Q.jsxs)(T,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(M,{children:(0,Q.jsx)(y,{children:(0,Q.jsx)(g,{})})}),(0,Q.jsx)(x,{children:Object.values(l).map(e=>(0,Q.jsx)(C,{value:e,children:ue(e)},e))})]})}),(0,Q.jsx)(k,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`isActive`,render:({field:e})=>(0,Q.jsxs)(R,{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)(q,{className:`text-base`,children:`Active Status`})}),(0,Q.jsx)(M,{children:(0,Q.jsx)(J,{checked:e.value,onCheckedChange:e.onChange})})]})})]}),(0,Q.jsx)(N,{control:X.control,name:`msCloudLink`,render:({field:e})=>(0,Q.jsxs)(R,{children:[(0,Q.jsx)(q,{children:`MS Cloud Link`}),(0,Q.jsx)(M,{children:(0,Q.jsx)(w,{type:`url`,placeholder:`https://portal.azure.com/contract-id`,...e})}),(0,Q.jsx)(k,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`defaultDocumentType`,render:({field:e})=>(0,Q.jsxs)(R,{children:[(0,Q.jsx)(q,{children:`Default Document Type`}),(0,Q.jsxs)(T,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(M,{children:(0,Q.jsx)(y,{children:(0,Q.jsx)(g,{})})}),(0,Q.jsx)(x,{children:Object.values(f).map(e=>(0,Q.jsx)(C,{value:e,children:le(e)},e))})]}),(0,Q.jsx)(k,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`defaultRemark`,render:({field:e})=>(0,Q.jsxs)(R,{children:[(0,Q.jsx)(q,{children:`Default Remark`}),(0,Q.jsx)(M,{children:(0,Q.jsx)(ce,{placeholder:`Enter default remark for this contract...`,rows:3,...e})}),(0,Q.jsx)(k,{})]})}),d&&(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)(re,{className:`text-base`,children:`Deactivate Original Contract`})}),(0,Q.jsx)(J,{checked:h,onCheckedChange:O})]})]}),(0,Q.jsxs)(ee,{children:[(0,Q.jsx)(D,{type:`button`,variant:`outline`,onClick:()=>m(!1),children:`Cancel`}),(0,Q.jsx)(D,{type:`submit`,disabled:W||V,children:u?`Update Contract`:`Create Contract`})]})]})})]})]})}export{$ as t};