@accounter/client
Version:
2 lines (1 loc) • 12 kB
JavaScript
import{v as re,bK as N,bL as S,g as K,c9 as se,r as p,t as k,i as Q,ca as ie,cb as B,bJ as G,ay as V,u as ce,cc as oe,E as le,G as de,a8 as ue,w,j as e,D as me,aL as pe,B as C,ag as he,a4 as U,x as xe,al as je,am as ye,an as ge,W as Ce,Z as o,_ as l,$ as d,a0 as u,I as f,a2 as m,af as fe,a5 as De,ao as T,ap as O,aq as I,ar as L,as as A,at as _,aQ as be,bb as we,a3 as ke,au as ve,cd as Ne,aw as v,ac as H,ax as Te,be as Oe,ad as Ie,ab as Le,ae as J,aA as Ae,bf as Se}from"./index-DRqooPz9.js";const Pe=[["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"}]],Ee=re("book-copy",Pe);function Me(c){switch(c){case N.Annual:return"Annual";case N.Monthly:return"Monthly";default:throw new Error(`Unsupported Billing Cycle: ${c}`)}}function Be(c){switch(c){case S.Enterprise:return"Enterprise";case S.Pro:return"Pro";default:throw new Error(`Unsupported Subscription plan: ${c}`)}}const Ve="createContract",Fe=()=>{const[{fetching:c},h]=K(se),g=p.useCallback(async x=>{const y=`Error creating new contract for client[${x.input.clientId}]`,s=`${Ve}-${x.input.clientId}`;k.loading("Creating contract",{id:s});try{const i=await h(x);Q(i,y,s)&&k.success("Success",{id:s,description:`Contract for client ${x.input.clientId} was successfully created`})}catch(i){console.error(`${y}: ${i}`),k.error("Error",{id:s,description:y,duration:1e5,closeButton:!0})}},[h]);return{creating:c,createContract:g}},$e="updateContract",ze=()=>{const[{fetching:c},h]=K(ie),g=p.useCallback(async x=>{const y=`Error updating contract ID [${x.contractId}]`,s=`${$e}-${x.contractId}`;k.loading("Updating contract",{id:s});try{const i=await h(x),D=Q(i,y,s,"updateContract");if(D)return k.success("Success",{id:s,description:"Contract updated"}),D.updateContract}catch(i){console.error(`${y}: ${i}`),k.error("Error",{id:s,description:y,duration:1e5,closeButton:!0})}},[h]);return{updating:c,updateContract:g}},Re=ue({id:Ae().optional(),startDate:J("Start date is required"),endDate:J("End date is required"),pos:Le(H()),paymentAmount:Ie().min(0,"Payment amount must be non-negative"),paymentCurrency:v(Object.values(V),"Currency is required"),productType:v(Object.values(G)).optional(),msCloudLink:Oe().optional().or(Se("")),billingCycle:v(Object.values(N)).optional(),subscriptionPlan:v(Object.values(S)).optional(),isActive:Te(),defaultRemark:H().optional(),defaultDocumentType:v(Object.values(B)),operationsLimit:Ne().min(BigInt(0),"Operations limit must be non-negative")}),qe={startDate:"",endDate:"",pos:[],paymentAmount:0,paymentCurrency:V.Usd,productType:G.Hive,msCloudLink:void 0,billingCycle:N.Monthly,subscriptionPlan:void 0,isActive:!0,defaultRemark:void 0,defaultDocumentType:B.Proforma,operationsLimit:BigInt(0)};function _e({clientId:c,contract:h,contractId:g,onDone:x,duplicate:y}){const{isEditing:s,isDuplicating:i}=p.useMemo(()=>{const t=!!h||!!g,n=y===!0&&t;return{isEditing:t&&!n,isDuplicating:n}},[h,g,y]),[D,b]=p.useState(!1),[P,W]=p.useState(!1),[r,F]=p.useState(qe),[$,z]=p.useState(""),{updateContract:R,updating:X}=ze(),{createContract:E,creating:Z}=Fe(),[{data:M,fetching:Y}]=ce({query:oe,pause:!g||!D,variables:{contractId:g??""}}),a=le({resolver:de(Re),defaultValues:r}),q=()=>{const t=$.trim();if(t){const n=a.getValues("pos");n.includes(t)||a.setValue("pos",[...n,t],{shouldDirty:!0}),z("")}},ee=t=>{const n=a.getValues("pos");a.setValue("pos",n.filter((j,ae)=>ae!==t),{shouldDirty:!0})};p.useEffect(()=>{h&&F(h)},[h]),p.useEffect(()=>{if(M?.contractsById){const t=M.contractsById,n={startDate:t.startDate,endDate:t.endDate,pos:t.purchaseOrders,paymentAmount:t.amount.raw,paymentCurrency:t.amount.currency,productType:t.product??void 0,msCloudLink:t.msCloud?.toString()??void 0,billingCycle:t.billingCycle,subscriptionPlan:t.plan??void 0,isActive:t.isActive,defaultRemark:t.remarks??void 0,defaultDocumentType:t.documentType,operationsLimit:BigInt(t.operationsLimit)};F({id:t.id,...n})}},[M]);const te=p.useMemo(()=>r?JSON.stringify(r):null,[r]);p.useEffect(()=>{r&&a.reset({startDate:r.startDate,endDate:r.endDate,pos:r.pos,paymentAmount:r.paymentAmount,paymentCurrency:r.paymentCurrency,productType:r.productType,msCloudLink:r.msCloudLink,billingCycle:r.billingCycle,subscriptionPlan:r.subscriptionPlan,isActive:r.isActive,defaultRemark:r.defaultRemark,defaultDocumentType:r.defaultDocumentType,operationsLimit:BigInt(r.operationsLimit)})},[te,a.reset]);const ne=p.useCallback(async t=>{if(i||s){if(!r?.id)return;if(i){if(!t.billingCycle){a.setError("billingCycle",{message:"Billing cycle is required"});return}await E({input:{clientId:c,amount:{raw:t.paymentAmount,currency:t.paymentCurrency},billingCycle:t.billingCycle,documentType:t.defaultDocumentType,endDate:w(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:w(new Date(t.startDate),"yyyy-MM-dd"),operationsLimit:t.operationsLimit,deactivateContracts:P?[r.id]:void 0}})}else await R({contractId:r.id,input:{amount:{raw:t.paymentAmount,currency:t.paymentCurrency},billingCycle:t.billingCycle,documentType:t.defaultDocumentType,endDate:w(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:w(new Date(t.startDate),"yyyy-MM-dd"),operationsLimit:t.operationsLimit}})}else{if(!t.billingCycle){a.setError("billingCycle",{message:"Billing cycle is required"});return}await E({input:{clientId:c,amount:{raw:t.paymentAmount,currency:t.paymentCurrency},billingCycle:t.billingCycle,documentType:t.defaultDocumentType,endDate:w(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:w(new Date(t.startDate),"yyyy-MM-dd"),operationsLimit:t.operationsLimit}})}b(!1),x?.()},[r,x,E,R,c,a,s,i,P]);return e.jsxs(me,{open:D,onOpenChange:b,children:[e.jsx(pe,{asChild:!0,children:s?e.jsx(C,{variant:"ghost",size:"sm",onClick:()=>b(!0),children:e.jsx(he,{className:"size-5"})}):i?e.jsx(C,{variant:"ghost",size:"sm",onClick:()=>b(!0),children:e.jsx(Ee,{className:"size-5"})}):e.jsxs(C,{size:"sm",onClick:()=>b(!0),children:[e.jsx(U,{className:"size-4 mr-2"}),"New Contract"]})}),e.jsxs(xe,{className:"max-w-2xl max-h-[90vh] overflow-y-auto",children:[e.jsxs(je,{children:[e.jsx(ye,{children:s?"Edit Contract":"Create New Contract"}),e.jsx(ge,{children:s?"Update contract details":i?"Create a new contract based on an existing one":"Add a new contract with all required details"})]}),Y?e.jsx(e.Fragment,{children:"Fetching contract details..."}):e.jsx(Ce,{...a,children:e.jsxs("form",{onSubmit:a.handleSubmit(ne),children:[e.jsxs("div",{className:"grid gap-4 py-4",children:[e.jsxs("div",{className:"grid gap-4 md:grid-cols-2",children:[e.jsx(o,{control:a.control,name:"operationsLimit",render:({field:t})=>e.jsxs(l,{children:[e.jsx(d,{children:"Operations Limit"}),e.jsx(u,{children:e.jsx(f,{placeholder:"500",...t,value:t.value?.toLocaleString(),onChange:n=>{const j=n.target.value.replace(/[^0-9]/g,"");t.onChange(BigInt(j||0))}})}),e.jsx(m,{})]})}),e.jsx(o,{control:a.control,name:"pos",render:({field:t})=>e.jsxs(l,{children:[e.jsx(d,{children:"Purchase Orders"}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(f,{placeholder:"Add PO...",value:$,onChange:n=>z(n.target.value),onKeyDown:n=>{n.key==="Enter"&&(n.preventDefault(),q())}}),e.jsx(C,{type:"button",size:"sm",onClick:q,children:e.jsx(U,{className:"h-4 w-4"})})]}),e.jsx("div",{className:"flex flex-wrap gap-2 mt-2",children:t.value?.map((n,j)=>e.jsxs(fe,{variant:"secondary",className:"gap-1 max-w-xs truncate",children:[n,j===(t.value?.length??0)-1&&e.jsx(C,{variant:"ghost",size:"icon",className:"p-0 size-3",onClick:()=>ee(j),children:e.jsx(De,{className:"size-3 cursor-pointer flex-shrink-0"})})]},n))}),e.jsx(m,{})]})})]}),e.jsxs("div",{className:"grid gap-4 md:grid-cols-2",children:[e.jsx(o,{control:a.control,name:"startDate",render:({field:{onChange:t,...n}})=>e.jsxs(l,{children:[e.jsx(d,{children:"Start Date"}),e.jsx(u,{children:e.jsx(f,{type:"date",...n,onInput:j=>t(j)})}),e.jsx(m,{})]})}),e.jsx(o,{control:a.control,name:"endDate",render:({field:{onChange:t,...n}})=>e.jsxs(l,{children:[e.jsx(d,{children:"End Date"}),e.jsx(u,{children:e.jsx(f,{type:"date",...n,onInput:j=>t(j)})}),e.jsx(m,{})]})})]}),e.jsxs("div",{className:"grid gap-4 md:grid-cols-3",children:[e.jsx(o,{control:a.control,name:"paymentAmount",render:({field:t})=>e.jsxs(l,{className:"md:col-span-2",children:[e.jsx(d,{children:"Payment Amount"}),e.jsx(u,{children:e.jsx(f,{type:"number",placeholder:"24000",...t,onChange:n=>{t.onChange(n?.target.value?Number(n?.target.value):void 0)}})}),e.jsx(m,{})]})}),e.jsx(o,{control:a.control,name:"paymentCurrency",render:({field:t})=>e.jsxs(l,{children:[e.jsx(d,{children:"Currency"}),e.jsxs(T,{onValueChange:t.onChange,defaultValue:t.value,children:[e.jsx(u,{children:e.jsx(O,{children:e.jsx(I,{})})}),e.jsx(L,{children:Object.values(V).map(n=>e.jsx(A,{value:n,children:n},n))})]}),e.jsx(m,{})]})})]}),e.jsxs("div",{className:"grid gap-4 md:grid-cols-2",children:[e.jsx(o,{control:a.control,name:"productType",render:({field:t})=>e.jsxs(l,{children:[e.jsx(d,{children:"Product Type"}),e.jsx(u,{children:e.jsx(f,{placeholder:"Cloud Services",...t})}),e.jsx(m,{})]})}),e.jsx(o,{control:a.control,name:"billingCycle",render:({field:t})=>e.jsxs(l,{children:[e.jsx(d,{children:"Billing Cycle"}),e.jsxs(T,{onValueChange:t.onChange,defaultValue:t.value,children:[e.jsx(u,{children:e.jsx(O,{children:e.jsx(I,{})})}),e.jsx(L,{children:Object.values(N).map(n=>e.jsx(A,{value:n,children:Me(n)},n))})]}),e.jsx(m,{})]})})]}),e.jsxs("div",{className:"grid gap-4 md:grid-cols-2",children:[e.jsx(o,{control:a.control,name:"subscriptionPlan",render:({field:t})=>e.jsxs(l,{children:[e.jsx(d,{children:"Subscription Plan"}),e.jsx(u,{children:e.jsxs(T,{onValueChange:t.onChange,defaultValue:t.value,children:[e.jsx(u,{children:e.jsx(O,{children:e.jsx(I,{})})}),e.jsx(L,{children:Object.values(S).map(n=>e.jsx(A,{value:n,children:Be(n)},n))})]})}),e.jsx(m,{})]})}),e.jsx(o,{control:a.control,name:"isActive",render:({field:t})=>e.jsxs(l,{className:"flex flex-row items-center justify-between rounded-lg border p-4",children:[e.jsx("div",{className:"space-y-0.5",children:e.jsx(d,{className:"text-base",children:"Active Status"})}),e.jsx(u,{children:e.jsx(_,{checked:t.value,onCheckedChange:t.onChange})})]})})]}),e.jsx(o,{control:a.control,name:"msCloudLink",render:({field:t})=>e.jsxs(l,{children:[e.jsx(d,{children:"MS Cloud Link"}),e.jsx(u,{children:e.jsx(f,{type:"url",placeholder:"https://portal.azure.com/contract-id",...t})}),e.jsx(m,{})]})}),e.jsx(o,{control:a.control,name:"defaultDocumentType",render:({field:t})=>e.jsxs(l,{children:[e.jsx(d,{children:"Default Document Type"}),e.jsxs(T,{onValueChange:t.onChange,defaultValue:t.value,children:[e.jsx(u,{children:e.jsx(O,{children:e.jsx(I,{})})}),e.jsx(L,{children:Object.values(B).map(n=>e.jsx(A,{value:n,children:be(n)},n))})]}),e.jsx(m,{})]})}),e.jsx(o,{control:a.control,name:"defaultRemark",render:({field:t})=>e.jsxs(l,{children:[e.jsx(d,{children:"Default Remark"}),e.jsx(u,{children:e.jsx(we,{placeholder:"Enter default remark for this contract...",rows:3,...t})}),e.jsx(m,{})]})}),i&&e.jsxs("div",{className:"flex flex-row items-center justify-between rounded-lg border p-4",children:[e.jsx("div",{className:"space-y-0.5",children:e.jsx(ke,{className:"text-base",children:"Deactivate Original Contract"})}),e.jsx(_,{checked:P,onCheckedChange:W})]})]}),e.jsxs(ve,{children:[e.jsx(C,{type:"button",variant:"outline",onClick:()=>b(!1),children:"Cancel"}),e.jsx(C,{type:"submit",disabled:Z||X,children:s?"Update Contract":"Create Contract"})]})]})})]})]})}export{_e as M,Be as a,Me as s};