UNPKG

@accounter/client

Version:
1 lines 4.79 kB
import{s as e}from"./dist-C51EwTaa.js";import{i as t,o as n}from"./utils-DdVdMk8X.js";import{Lo as r,N as i,at as a,di as o,gi as s,ui as c}from"./error-handling-Dpfd9svJ.js";import{s as l,t as u}from"./routes-DUvpW9-n.js";import{a as d,c as f,dt as p,f as m,i as h,l as g,m as _,n as v,o as y,p as b,r as x,t as S,ut as C}from"./select-CrsvQyhZ.js";import{t as w}from"./button-Dzp2INW0.js";import{Et as T,H as E,J as D,Lt as O,Ot as k,P as A,Tt as j,U as M,W as N,a as P,i as F,n as I,o as L,r as R,t as z}from"./table--bF0wbxq.js";import{t as B}from"./subMonths-DGtu7gB6.js";import{Ft as V,I as H,cn as U,ot as W}from"./index-CL8-JoMZ.js";import{r as G,t as K}from"./use-issue-monthly-documents-dWFCMZVs.js";var q=e(n(),1),J=t();function Y({issueMonth:e,clients:t,onAdd:n}){let[i,a]=(0,q.useState)(!1),[o,l]=(0,q.useState)(``),[{data:u},y]=r({query:c,variables:{issueMonth:e,clientId:o},pause:!i}),C=(0,q.useCallback)(e=>{l(e),y()},[y,l]);return(0,q.useEffect)(()=>{o!==``&&u&&(n(H(N(s,u.clientMonthlyChargeDraft))),a(!1))},[o,u,n]),(0,J.jsxs)(f,{open:i,onOpenChange:a,children:[(0,J.jsx)(_,{asChild:!0,children:(0,J.jsx)(w,{variant:`outline`,size:`icon`,children:(0,J.jsx)(p,{})})}),(0,J.jsxs)(g,{className:`w-[400px] max-w-screen-md`,children:[(0,J.jsx)(m,{children:(0,J.jsx)(b,{children:`Select document recipient`})}),(0,J.jsxs)(S,{onValueChange:C,children:[(0,J.jsx)(h,{children:(0,J.jsx)(d,{placeholder:`Recipient`})}),(0,J.jsx)(v,{children:t.map(e=>(0,J.jsx)(x,{value:e.id,children:e.name},e.id))})]})]})]})}var X=({drafts:e})=>{let[t,n]=(0,q.useState)(e.map(e=>H(N(s,e)))),[c,d]=(0,q.useState)(O(B(new Date,1),`yyyy-MM-dd`)),[{data:f},p]=r({query:o,variables:{issueMonth:c},pause:!0});(0,q.useEffect)(()=>{f?.periodicalDocumentDrafts&&n(f.periodicalDocumentDrafts.map(e=>H(N(s,e))))},[f,n]),(0,q.useEffect)(()=>{p()},[c,p]);let m=M({values:{generateDocumentsInfo:t}}),{issueDocuments:h}=K(),[{data:g}]=r({query:i}),{fields:_,append:v,remove:b,update:x}=E({control:m.control,name:`generateDocumentsInfo`}),S=m.watch(`generateDocumentsInfo`),D=(0,q.useMemo)(()=>_.map((e,t)=>({...e,...S[t],id:e.client?.id})),[_,S]),U=(0,q.useCallback)(e=>{h(e)},[h]),X=(0,q.useMemo)(()=>g?.allOpenContracts.filter(e=>e.billingCycle===a.Monthly&&!D.some(t=>t.client?.id===e.client.id)),[D,g]);return(0,J.jsx)(`div`,{className:`p-2`,children:(0,J.jsx)(A,{...m,children:(0,J.jsxs)(`form`,{onSubmit:m.handleSubmit(U),children:[(0,J.jsxs)(`div`,{className:`flex gap-2 items-center mb-4`,children:[(0,J.jsx)(y,{children:`Issue Month:`}),(0,J.jsx)(V,{value:new Date(c),onChange:e=>{d(O(new Date(e.getFullYear(),e.getMonth(),15),`yyyy-MM-dd`))},popoverProps:{withinPortal:!0}})]}),(0,J.jsxs)(z,{children:[(0,J.jsx)(P,{children:(0,J.jsxs)(L,{children:[(0,J.jsx)(F,{children:`Name`}),(0,J.jsx)(F,{children:`Document`}),(0,J.jsx)(F,{children:`Amount`}),(0,J.jsx)(F,{children:`Remark`}),(0,J.jsx)(F,{children:`Recipients`}),(0,J.jsx)(F,{})]})}),(0,J.jsx)(I,{children:D.map(({id:e,...t},n)=>(0,J.jsxs)(L,{children:[(0,J.jsx)(R,{children:(0,J.jsx)(l,{to:e?u.BUSINESSES.DETAIL(e):u.BUSINESSES.ALL,target:`_blank`,rel:`noreferrer`,onClick:e=>e.stopPropagation(),className:`inline-flex items-center font-semibold`,children:t.client?.name})}),(0,J.jsx)(R,{children:t.type}),(0,J.jsxs)(R,{children:[t.income?.[0]?.price,` `,t.income?.[0]?.currency]}),(0,J.jsx)(R,{children:(0,J.jsx)(`span`,{className:`text-sm text-gray-500 whitespace-normal max-w-50`,children:t.remarks})}),(0,J.jsx)(R,{children:(0,J.jsx)(`div`,{className:`flex flex-col`,children:t.client?.emails?.map(e=>(0,J.jsx)(`span`,{className:`text-sm text-gray-500`,children:e},e))})}),(0,J.jsx)(R,{children:(0,J.jsxs)(`div`,{className:`flex flex-col gap-2`,children:[(0,J.jsxs)(j,{children:[(0,J.jsx)(k,{asChild:!0,type:`button`,children:(0,J.jsx)(w,{className:`size-7.5`,variant:`secondary`,onClick:()=>b(n),children:(0,J.jsx)(C,{size:16})})}),(0,J.jsx)(T,{children:(0,J.jsx)(`p`,{children:`Remove document`})})]}),(0,J.jsx)(G,{draft:t,onApprove:e=>{m.setValue(`generateDocumentsInfo.${n}`,e,{shouldDirty:!0,shouldTouch:!0,shouldValidate:!0}),x(n,{...e})}})]})})]},e))})]}),(0,J.jsxs)(`div`,{className:`flex justify-between items-center mt-4`,children:[(0,J.jsx)(Y,{clients:X?.map(e=>({id:e.client.originalBusiness.id,name:e.client.originalBusiness.name}))??[],onAdd:v,issueMonth:c}),(0,J.jsx)(W,{onConfirm:m.handleSubmit(U),title:`Are you sure you want to issue ${D.length} documents?`,children:(0,J.jsx)(w,{children:`Issue`})})]})]})})})},Z=()=>{let{setFiltersContext:e}=(0,q.useContext)(U),[{data:t,fetching:n}]=r({query:o,variables:{issueMonth:O(B(new Date,1),`yyyy-MM-dd`)}});return(0,q.useEffect)(()=>{t?.periodicalDocumentDrafts||e(null)},[t,e]),n?(0,J.jsx)(D,{}):(0,J.jsx)(X,{drafts:t?.periodicalDocumentDrafts??[]})};export{Z as IssueDocuments};