UNPKG

@accounter/client

Version:
1 lines 4.83 kB
import{s as e}from"./dist-ypP48_Ax.js";import{i as t,o as n}from"./utils-CbS-oR5I.js";import{s as r,t as i}from"./routes-Cawu7UJi.js";import{H as a,_t as o,h as s}from"./common-D-vYt_Yl.js";import{a as c,c as l,f as u,i as d,l as f,lt as p,m,n as h,o as g,p as _,r as v,t as y,ut as b}from"./select-Zdatwv1b.js";import{c as x}from"./separator-BvKylNhK.js";import{t as S}from"./button-DUNsVBiu.js";import{Fl as C,Ml as w,Nl as T,Ul as E,Vc as D,Xc as O,Yc as k,Zc as A,a as j,i as M,n as N,o as P,r as F,t as I,tl as L}from"./table-AJA7Uyxy.js";import{A as R,Ei as z,Qo as B,bi as V,it as H,xi as U}from"./graphql-DC5GYCFE.js";import{t as W}from"./subMonths-Bu3hmucc.js";import{r as G,t as K}from"./use-issue-monthly-documents-8jTXjMZp.js";var q=e(n(),1),J=t();function Y({issueMonth:e,clients:t,onAdd:n}){let[r,i]=(0,q.useState)(!1),[a,o]=(0,q.useState)(``),[{data:p},g]=B({query:V,variables:{issueMonth:e,clientId:a},pause:!r}),x=(0,q.useCallback)(e=>{o(e),g()},[g,o]);return(0,q.useEffect)(()=>{a!==``&&p&&(n(s(A(z,p.clientMonthlyChargeDraft))),i(!1))},[a,p,n]),(0,J.jsxs)(l,{open:r,onOpenChange:i,children:[(0,J.jsx)(m,{asChild:!0,children:(0,J.jsx)(S,{variant:`outline`,size:`icon`,children:(0,J.jsx)(b,{})})}),(0,J.jsxs)(f,{className:`w-[400px] max-w-screen-md`,children:[(0,J.jsx)(u,{children:(0,J.jsx)(_,{children:`Select document recipient`})}),(0,J.jsxs)(y,{onValueChange:x,children:[(0,J.jsx)(d,{children:(0,J.jsx)(c,{placeholder:`Recipient`})}),(0,J.jsx)(h,{children:t.map(e=>(0,J.jsx)(v,{value:e.id,children:e.name},e.id))})]})]})]})}var X=({drafts:e})=>{let[t,n]=(0,q.useState)(e.map(e=>s(A(z,e)))),c=E(W(new Date,1),`yyyy-MM-dd`),[l,u]=(0,q.useState)(c),[{data:d},f]=B({query:U,variables:{issueMonth:l},pause:!0});(0,q.useEffect)(()=>{d?.periodicalDocumentDrafts&&n(d.periodicalDocumentDrafts.map(e=>s(A(z,e))))},[d,n]),(0,q.useEffect)(()=>{f()},[l,f]);let m=O({values:{generateDocumentsInfo:t}}),{issueDocuments:h}=K(),[{data:_}]=B({query:R}),{fields:v,append:y,remove:b,update:x}=k({control:m.control,name:`generateDocumentsInfo`}),L=m.watch(`generateDocumentsInfo`),V=(0,q.useMemo)(()=>v.map((e,t)=>({...e,...L[t],id:e.client?.id})),[v,L]),X=(0,q.useCallback)(e=>{h(e)},[h]),Z=(0,q.useMemo)(()=>_?.allOpenContracts.filter(e=>e.billingCycle===H.Monthly&&!V.some(t=>t.client?.id===e.client.id)),[V,_]);return(0,J.jsx)(`div`,{className:`p-2`,children:(0,J.jsx)(D,{...m,children:(0,J.jsxs)(`form`,{onSubmit:m.handleSubmit(X),children:[(0,J.jsxs)(`div`,{className:`flex gap-2 items-center mb-4`,children:[(0,J.jsx)(g,{children:`Issue Month:`}),(0,J.jsx)(o,{value:new Date(l),onChange:e=>{let t=new Date(e.getFullYear(),e.getMonth(),15);u(E(t,`yyyy-MM-dd`))},popoverProps:{withinPortal:!0}})]}),(0,J.jsxs)(I,{children:[(0,J.jsx)(j,{children:(0,J.jsxs)(P,{children:[(0,J.jsx)(M,{children:`Name`}),(0,J.jsx)(M,{children:`Document`}),(0,J.jsx)(M,{children:`Amount`}),(0,J.jsx)(M,{children:`Remark`}),(0,J.jsx)(M,{children:`Recipients`}),(0,J.jsx)(M,{})]})}),(0,J.jsx)(N,{children:V.map(({id:e,...t},n)=>(0,J.jsxs)(P,{children:[(0,J.jsx)(F,{children:(0,J.jsx)(r,{to:e?i.BUSINESSES.DETAIL(e):i.BUSINESSES.ALL,target:`_blank`,rel:`noreferrer`,onClick:e=>e.stopPropagation(),className:`inline-flex items-center font-semibold`,children:t.client?.name})}),(0,J.jsx)(F,{children:t.type}),(0,J.jsxs)(F,{children:[t.income?.[0]?.price,` `,t.income?.[0]?.currency]}),(0,J.jsx)(F,{children:(0,J.jsx)(`span`,{className:`text-sm text-gray-500 whitespace-normal max-w-50`,children:t.remarks})}),(0,J.jsx)(F,{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)(F,{children:(0,J.jsxs)(`div`,{className:`flex flex-col gap-2`,children:[(0,J.jsxs)(w,{children:[(0,J.jsx)(C,{asChild:!0,type:`button`,children:(0,J.jsx)(S,{className:`size-7.5`,variant:`secondary`,onClick:()=>b(n),children:(0,J.jsx)(p,{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:Z?.map(e=>({id:e.client.originalBusiness.id,name:e.client.originalBusiness.name}))??[],onAdd:y,issueMonth:l}),(0,J.jsx)(a,{onConfirm:m.handleSubmit(X),title:`Are you sure you want to issue ${V.length} documents?`,children:(0,J.jsx)(S,{children:`Issue`})})]})]})})})},Z=()=>{let{setFiltersContext:e}=(0,q.useContext)(x),[{data:t,fetching:n}]=B({query:U,variables:{issueMonth:E(W(new Date,1),`yyyy-MM-dd`)}});return(0,q.useEffect)(()=>{t?.periodicalDocumentDrafts||e(null)},[t,e]),n?(0,J.jsx)(L,{}):(0,J.jsx)(X,{drafts:t?.periodicalDocumentDrafts??[]})};export{Z as IssueDocuments};