@accounter/client
Version:
Accounter client application
1 lines • 12.7 kB
JavaScript
import{s as e}from"./dist-BmACi7k_.js";import{i as t,o as n}from"./utils-BxkqV8PM.js";import{Cn as r,Fn as i,Ga as a,Lo as o,On as s,Ro as ee,bi as c,da as l,ot as u,r as d,t as f,tr as p}from"./error-handling-wXZrXhQp.js";import{a as m,c as te,d as ne,f as h,gt as g,i as _,l as v,lt as re,m as ie,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-C2i-wLGF.js";import{Xn as T,_t as E,hr as se}from"./common-ClWrRiKb.js";import{o as ce}from"./separator-D3vGB2Vv.js";import{t as D}from"./button-BZ6z_NAl.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 L,Vt as R,Wc as z,Wt as B,Xc as V,Yc as H,h as ue,mr as U,pr as W,qc as G,zn as K}from"./table-BiqHkTL_.js";var de=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 q(e){switch(e){case u.Annual:return`Annual`;case u.Monthly:return`Monthly`;default:throw Error(`Unsupported Billing Cycle: ${e}`)}}function J(e){switch(e){case l.Enterprise:return`Enterprise`;case l.Pro:return`Pro`;default:throw Error(`Unsupported Subscription plan: ${e}`)}}var Y=e(n(),1),X=`createContract`,fe=()=>{let[{fetching:e},t]=o(s);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{f(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]=o(a);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=f(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=k({id:U().optional(),startDate:I(`Start date is required`),endDate:I(`End date is required`),pos:R(O()),paymentAmount:K().min(0,`Payment amount must be non-negative`),paymentCurrency:F(Object.values(i),`Currency is required`),productType:F(Object.values(c)).optional(),msCloudLink:W().optional().or(A(``)),billingCycle:F(Object.values(u)).optional(),subscriptionPlan:F(Object.values(l)).optional(),isActive:M(),defaultRemark:O().optional(),defaultDocumentType:F(Object.values(p)),operationsLimit:B().min(BigInt(0),`Operations limit must be non-negative`)}),he={startDate:``,endDate:``,pos:[],paymentAmount:0,paymentCurrency:i.Usd,productType:c.Hive,msCloudLink:void 0,billingCycle:u.Monthly,subscriptionPlan:void 0,isActive:!0,defaultRemark:void 0,defaultDocumentType:p.Proforma,operationsLimit:BigInt(0)};function $({clientId:e,contract:t,contractId:n,onDone:a,duplicate:o}){let{isEditing:s,isDuplicating:c}=(0,Y.useMemo)(()=>{let e=!!t||!!n,r=o===!0&&e;return{isEditing:e&&!r,isDuplicating:r}},[t,n,o]),[d,f]=(0,Y.useState)(!1),[g,O]=(0,Y.useState)(!1),[k,A]=(0,Y.useState)(he),[M,F]=(0,Y.useState)(``),{updateContract:I,updating:R}=pe(),{createContract:B,creating:U}=fe(),[{data:W,fetching:K}]=ee({query:r,pause:!n||!d,variables:{contractId:n??``}}),X=le({resolver:ue(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}),F(``)}},$=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]),(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)})},[(0,Y.useMemo)(()=>k?JSON.stringify(k):null,[k]),X.reset]);let ge=(0,Y.useCallback)(async t=>{if(c||s){if(!k?.id)return;if(c){if(!t.billingCycle){X.setError(`billingCycle`,{message:`Billing cycle is required`});return}await B({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){X.setError(`billingCycle`,{message:`Billing cycle is required`});return}await B({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),a?.()},[k,a,B,I,e,X,s,c,g]);return(0,Q.jsxs)(te,{open:d,onOpenChange:f,children:[(0,Q.jsx)(ie,{asChild:!0,children:s?(0,Q.jsx)(D,{variant:`ghost`,size:`sm`,onClick:()=>f(!0),children:(0,Q.jsx)(se,{className:`size-5`})}):c?(0,Q.jsx)(D,{variant:`ghost`,size:`sm`,onClick:()=>f(!0),children:(0,Q.jsx)(de,{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)(v,{className:`max-w-2xl max-h-[90vh] overflow-y-auto`,children:[(0,Q.jsxs)(h,{children:[(0,Q.jsx)(oe,{children:s?`Edit Contract`:`Create New Contract`}),(0,Q.jsx)(C,{children:s?`Update contract details`:c?`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)(z,{...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)(G,{control:X.control,name:`operationsLimit`,render:({field:e})=>(0,Q.jsxs)(N,{children:[(0,Q.jsx)(H,{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)(V,{})]})}),(0,Q.jsx)(G,{control:X.control,name:`pos`,render:({field:e})=>(0,Q.jsxs)(N,{children:[(0,Q.jsx)(H,{children:`Purchase Orders`}),(0,Q.jsxs)(`div`,{className:`flex gap-2`,children:[(0,Q.jsx)(x,{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)(L,{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)(re,{className:`size-3 cursor-pointer flex-shrink-0`})})]},t))}),(0,Q.jsx)(V,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(G,{control:X.control,name:`startDate`,render:({field:{onChange:e,...t}})=>(0,Q.jsxs)(N,{children:[(0,Q.jsx)(H,{children:`Start Date`}),(0,Q.jsx)(j,{children:(0,Q.jsx)(x,{type:`date`,...t,onInput:t=>e(t)})}),(0,Q.jsx)(V,{})]})}),(0,Q.jsx)(G,{control:X.control,name:`endDate`,render:({field:{onChange:e,...t}})=>(0,Q.jsxs)(N,{children:[(0,Q.jsx)(H,{children:`End Date`}),(0,Q.jsx)(j,{children:(0,Q.jsx)(x,{type:`date`,...t,onInput:t=>e(t)})}),(0,Q.jsx)(V,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-3`,children:[(0,Q.jsx)(G,{control:X.control,name:`paymentAmount`,render:({field:e})=>(0,Q.jsxs)(N,{className:`md:col-span-2`,children:[(0,Q.jsx)(H,{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)(V,{})]})}),(0,Q.jsx)(G,{control:X.control,name:`paymentCurrency`,render:({field:e})=>(0,Q.jsxs)(N,{children:[(0,Q.jsx)(H,{children:`Currency`}),(0,Q.jsxs)(S,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(j,{children:(0,Q.jsx)(_,{children:(0,Q.jsx)(m,{})})}),(0,Q.jsx)(y,{children:Object.values(i).map(e=>(0,Q.jsx)(b,{value:e,children:e},e))})]}),(0,Q.jsx)(V,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(G,{control:X.control,name:`productType`,render:({field:e})=>(0,Q.jsxs)(N,{children:[(0,Q.jsx)(H,{children:`Product Type`}),(0,Q.jsx)(j,{children:(0,Q.jsx)(x,{placeholder:`Cloud Services`,...e})}),(0,Q.jsx)(V,{})]})}),(0,Q.jsx)(G,{control:X.control,name:`billingCycle`,render:({field:e})=>(0,Q.jsxs)(N,{children:[(0,Q.jsx)(H,{children:`Billing Cycle`}),(0,Q.jsxs)(S,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(j,{children:(0,Q.jsx)(_,{children:(0,Q.jsx)(m,{})})}),(0,Q.jsx)(y,{children:Object.values(u).map(e=>(0,Q.jsx)(b,{value:e,children:q(e)},e))})]}),(0,Q.jsx)(V,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(G,{control:X.control,name:`subscriptionPlan`,render:({field:e})=>(0,Q.jsxs)(N,{children:[(0,Q.jsx)(H,{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)(_,{children:(0,Q.jsx)(m,{})})}),(0,Q.jsx)(y,{children:Object.values(l).map(e=>(0,Q.jsx)(b,{value:e,children:J(e)},e))})]})}),(0,Q.jsx)(V,{})]})}),(0,Q.jsx)(G,{control:X.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)(H,{className:`text-base`,children:`Active Status`})}),(0,Q.jsx)(j,{children:(0,Q.jsx)(E,{checked:e.value,onCheckedChange:e.onChange})})]})})]}),(0,Q.jsx)(G,{control:X.control,name:`msCloudLink`,render:({field:e})=>(0,Q.jsxs)(N,{children:[(0,Q.jsx)(H,{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)(V,{})]})}),(0,Q.jsx)(G,{control:X.control,name:`defaultDocumentType`,render:({field:e})=>(0,Q.jsxs)(N,{children:[(0,Q.jsx)(H,{children:`Default Document Type`}),(0,Q.jsxs)(S,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(j,{children:(0,Q.jsx)(_,{children:(0,Q.jsx)(m,{})})}),(0,Q.jsx)(y,{children:Object.values(p).map(e=>(0,Q.jsx)(b,{value:e,children:T(e)},e))})]}),(0,Q.jsx)(V,{})]})}),(0,Q.jsx)(G,{control:X.control,name:`defaultRemark`,render:({field:e})=>(0,Q.jsxs)(N,{children:[(0,Q.jsx)(H,{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)(V,{})]})}),c&&(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:g,onCheckedChange:O})]})]}),(0,Q.jsxs)(ne,{children:[(0,Q.jsx)(D,{type:`button`,variant:`outline`,onClick:()=>f(!1),children:`Cancel`}),(0,Q.jsx)(D,{type:`submit`,disabled:U||R,children:s?`Update Contract`:`Create Contract`})]})]})})]})]})}export{q as n,J as r,$ as t};