1import{q as L,j as r,L as U,r as O}from"./vendor-react-0b52e17d.js";import{H as G}from"./vendor-misc-60f9a457.js";import{u as j}from"./use-ai-copilot-fa211c3a.js";import{R as P,a as $}from"./ReportPage-8057dafd.js";import{q as Y,e as z,p as V,az as q,Q as K,T as J,bD as Q,w as X,k as Z}from"./app-core-d11f9ebf.js";import{ez as ee,ae as d,eA as l,aV as A,dh as te,aJ as N,aU as b,d as H,s as D,eB as oe,cl as ne,b as re}from"./vendor-vy-3b44047f.js";import{E as se}from"./vendor-reports-4bfebb26.js";import{u as ae}from"./use-search-with-search-params-c8e8c99a.js";import{u as ie}from"./use-report-filter-lines-70d35c99.js";import{R as le}from"./ReportEmptyOverlay-b95bb099.js";import{T as v}from"./report-table-consts-5838d7f7.js";import{c as f,l as E,b9 as I,d as F,$ as ue,ba as de,o as me}from"./vendor-vy-ds-885d80cb.js";import{bb as ce,u as ge}from"./vendor-mui-238acd1e.js";import{R as pe}from"./RevenueReportToolbar-f189599e.js";import{u as Te}from"./use-labels-4d4a7529.js";import{u as _e}from"./use-subsidiaries-9438a3e5.js";import{c as he,M as k,g as Oe,f as fe,m as B,j as Ae}from"./filter-field-builders-a40bb915.js";import{c as w}from"./option-collectors-1da5e27e.js";import{a as Ce}from"./apply-report-filters-2e4b1165.js";import"./AiCopilotProvider-1e5fad19.js";import"./VyLayout-ff2cba0b.js";import"./use-account-7dbbc7a8.js";import"./reference-data-query-options-consts-5095626a.js";import"./use-descope-sdk-b1412f07.js";import"./breadcrumbs-context-d114e5ee.js";import"./breadcrumb-routes-3b4ebb59.js";import"./app-router-a49381be.js";import"./use-jobs-5c611126.js";import"./VyPageLayoutTabs-1805ce94.js";import"./page-frame-cbbf18ba.js";import"./use-state-params-0f9cef64.js";import"./picker-value-utils-370a432f.js";import"./use-trigger-report-job-d4493d34.js";import"./use-catalog-products-list-5f86cccb.js";import"./use-meters-44d1580c.js";(function(){try{var o=typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:typeof self<"u"?self:{};o.SENTRY_RELEASE={id:"2cd2d3d210c4"};var n=new o.Error().stack;
1n&&(o._sentryDebugIds=o._sentryDebugIds||{},o._sentryDebugIds[n]="3e6e0f92-6d3b-4dcf-9817-a9b4083a2407",o._sentryDebugIdIdentifier="sentry-dbid-3e6e0f92-6d3b-4dcf-9817-a9b4083a2407")}catch{}})();function Ee(o){return L({queryKey:Y.arAgingReport(),queryFn:async()=>{const e=await z().getArAgingReport();return V(ee,e)},...o})}const y=Object.freeze({CUSTOMER_NAME:"customerName",TOTAL_GROSS_AMOUNT:"totalGrossAmount",UP_TO_ONE_MONTH:"upToOneMonth",ONE_TO_TWO_MONTHS:"oneToTwoMonths",TWO_TO_THREE_MONTHS:"twoToThreeMonths",MORE_THAN_THREE_MONTHS:"moreThanThreeMonths"});function ye(o){return[{field:y.CUSTOMER_NAME,headerName:"Customer breakdown",valueGetter:(n,e)=>e.customer.name,minWidth:240,flex:1,headerClassName:"pl-6 border-r",cellClassName:"border-r",renderCell:({row:{customer:{id:n,name:e}}})=>r.jsx(f,{sx:{pl:2,width:"100%",minWidth:0},children:n===v?r.jsx(E,{value:r.jsx("strong",{children:e}),align:"left"}):r.jsx(q,{customerId:n,name:e})})},{field:y.UP_TO_ONE_MONTH,headerName:"1-30",headerAlign:"left",valueGetter:(n,e)=>{var t;return d((t=e.outstandingBreakdown[l.UP_TO_ONE_MONTH])==null?void 0:t.grossAmount,0)},minWidth:240,flex:1,renderCell:({row:{outstandingBreakdown:n}})=>{var e;return r.jsx(E,{value:A(d((e=n[l.UP_TO_ONE_MONTH])==null?void 0:e.grossAmount,0),o,2,0),align:"left"})}},{field:y.ONE_TO_TWO_MONTHS,headerName:"31-60",headerAlign:"left",valueGetter:(n,e)=>{var t;return d((t=e.outstandingBreakdown[l.ONE_TO_TWO_MONTHS])==null?void 0:t.grossAmount,0)},minWidth:240,flex:1,renderCell:({row:{outstandingBreakdown:n}})=>{var e;return r.jsx(E,{value:A(d((e=n[l.ONE_TO_TWO_MONTHS])==null?void 0:e.grossAmount,0),o,2,0),align:"left"})}},{field:y.TWO_TO_THREE_MONTHS,headerName:"61-90",headerAlign:"left",valueGetter:(n,e)=>{var t;return d((t=e.outstandingBreakdown[l.TWO_TO_THREE_MONTHS])==null?void 0:t.grossAmount,0)},minWidth:240,flex:1,renderCell:({row:{outstandingBreakdown:n}})=>{var e;return r.jsx(E,{value:A(d((e=n[l.TWO_TO_THREE_MONTHS])==null?void 0:e.grossAmount,0),o,2,0),align:"left"})}},{field:y.MORE_THAN_THREE_MONTHS,headerName:"91+",headerAlign:"left",valueGetter:(n,e)=>{var t;return d((t=e.outstandingBreakdown[l.MORE_THAN_THREE_MONTHS])==null?void 0:t.grossAmount,0)},minWidth:240,flex:1,renderCell:({row:{outstandingBreakdown:n}})=>{var e;return r.jsx(E,{value:A(d((e=n[l.MORE_THAN_THREE_MONTHS])==null?void 0:e.grossAmount,0),o,2,0),align:"left"})}},{field:y.TOTAL_GROSS_AMOUNT,headerName:"Total",headerAlign:"left",headerClassName:I,cellClassName:I,valueGetter:(n,e)=>{var s,a,i,c;const t=e.outstandingBreakdown;return d((s=t[l.UP_TO_ONE_MONTH])==null?void 0:s.grossAmount,0)+d((a=t[l.ONE_TO_TWO_MONTHS])==null?void 0:a.grossAmount,0)+d((i=t[l.TWO_TO_THREE_MONTHS])==null?void 0:i.grossAmount,0)+d((c=t[l.MORE_THAN_THREE_MONTHS])==null?void 0:c.grossAmount,0)},minWidth:240,flex:1,renderCell:n=>r.jsx(E,{value:A(n.value,o,2,0),align:"left"})}]}function Ne(o){return o<=30?0:o<=60?1:o<=90?2:3}const Se=4,W={display:"flex",justifyContent:"center",fontSize:"13px"},Me=({invoices:o,currency:n})=>r.jsx(f,{sx:{py:1,backgroundColor:"grey.50"},children:o.map(e=>{const t=Ne(e.daysOutstanding);return r.jsxs(f,{sx:{display:"grid",gridTemplateColumns:"40px 1fr 1fr 1fr 1fr 1fr 1fr",alignItems:"center",py:.75,borderBottom:"1px solid",borderColor:"divider","&:last-child":{borderBottom:"none"},"&:hover":{backgroundColor:"action.selected"}},children:[r.jsx(f,{}),r.jsx(f,{sx:{pl:4,position:"sticky",left:40,backgroundColor:"inherit",zIndex:1},children:r.jsx(f,{component:U,to:`/invoices/${e.invoice.id}/customer/${e.customer.id}/dynamic`,target:"_blank",rel:"noopener noreferrer",sx:{color:"primary.main",textDecoration:"none","&:hover":{textDecoration:"underline"}},children:r.jsx(F,{variant:"subtitle2",sx:{color:"inherit"},children:e.invoice.invoiceNumber})})}),Array.from({length:Se},(s,a)=>r.jsx(f,{sx:W,children:a===t?A(e.remainingAmount,n,2,0):r.jsx(F,{variant:"body2",sx:{color:"text.disabled"},children:"-"})},a)),r.jsx(f,{sx:W,children:A(e.remainingAmount,n,2,0)})]},e.invoice.id)})}),xe=({items:o,breakdownItems:n,currency:e,loadingItems:t,pinnedRows:s,apiRef:a})=>{const[i,c]=O.useState(new Set),p=O.useMemo(()=>{const m=new Map;for(const g of n){const h=g.customer.id,M=m.get(h)??[];M.push(g),m.set(h,M)}return m},[n]),u=O.useCallback(m=>{const g=p.get(m.row.customer.id)??[];return g.length===0?null:r.jsx(Me,{invoices:g,currency:e})},[p,e]),T=O.useCallback(()=>"auto",[]),_=O.useCallback(m=>{c(m)},[]),C=O.useMemo(()=>[{...ce,width:40,minWidth:40,renderCell:m=>{if(m.row.customer.id===v)return null;const g=!!m.value;return r.jsx(ue,{size:"small",onClick:h=>{h.stopPropagation(),a==null||a.current.toggleDetailPanel(m.id)},children:r.jsx(de,{expanded:g})})}},...ye(e)],[a,e]);return r.jsx(f,{sx:{display:"flex",flexDirection:"column",height:"100%","& .MuiDataGrid-pinnedRows--bottom":{marginTop:"-1px"}}
1,children:r.jsx(me,{apiRef:a,rows:o,columns:C,getRowId:m=>m.customer.id,loading:t,pinnedRows:s,pinnedColumns:{left:["__detail_panel_toggle__","customerName"],right:["totalGrossAmount"]},pagination:!0,paginationMode:"client",detailPanelExpandedRowIds:i,onDetailPanelExpandedRowIdsChange:_,getDetailPanelContent:u,getDetailPanelHeight:T,sx:{'& .MuiDataGrid-cell[data-field="__detail_panel_toggle__"], & .MuiDataGrid-columnHeader[data-field="__detail_panel_toggle__"]':{padding:0,textOverflow:"clip"},"& .MuiDataGrid-detailPanel":{overflow:"visible"}},slots:{noRowsOverlay:le,loadingOverlay:K},initialState:{pagination:{paginationModel:{pageSize:25}}}})})},Re=({searchText:o,onSearchTextChange:n,filterFields:e,filters:t,onFiltersChange:s})=>r.jsx(pe,{search:{value:o,onChange:n},filters:{fields:e,lines:t,onChange:s}}),S=Object.freeze({CUSTOMER:"customerId",LABEL:"labels",SUBSIDIARY:"subsidiaryName"});function be(o){const{data:n}=Te({suspense:!1}),{data:e}=_e({suspense:!1});return O.useMemo(()=>{const t=w(o.map(u=>u.customer.name)),s=new Set(o.flatMap(u=>{var T;return((T=u.labels)==null?void 0:T.map(_=>_.id))??[]})),a=he(n==null?void 0:n.labels,s),i=w(o.map(u=>u.subsidiaryName??null)),c=i.length>0?i:w(((e==null?void 0:e.subsidiaries)??[]).map(u=>u.name)),p=[{id:S.CUSTOMER,label:"Customer",inputType:"select",operators:k,options:t},Oe(a),{id:S.SUBSIDIARY,label:"Subsidiary",inputType:"select",operators:k,options:c}];return fe(p)},[o,n,e])}const we={[S.CUSTOMER]:(o,n,e)=>B(o.customer.name,n,e),[S.LABEL]:(o,n,e)=>{var t;return Ae(((t=o.labels)==null?void 0:t.map(s=>s.id))??[],n,e)},[S.SUBSIDIARY]:(o,n,e)=>B(o.subsidiaryName??null,n,e)};function ve(o,n){return Ce(o,n,we)}function x(o,n,e){return o.filter(t=>t.daysOutstanding>n&&t.daysOutstanding<=e)}function He(o){const n=te(o,t=>t.customer.id);return Array.from(n.entries()).map(([,t])=>{const{customer:s}=t[0],a=x(t,0,30),i=x(t,30,60),c=x(t,60,90),p=x(t,90,1/0);return{customer:{id:s.id,name:s.name},outstandingBreakdown:{[l.UP_TO_ONE_MONTH]:{grossAmount:N(a,u=>u.remainingAmount),invoicesCount:a.length},[l.ONE_TO_TWO_MONTHS]:{grossAmount:N(i,u=>u.remainingAmount),invoicesCount:i.length},[l.TWO_TO_THREE_MONTHS]:{grossAmount:N(c,u=>u.remainingAmount),invoicesCount:c.length},[l.MORE_THAN_THREE_MONTHS]:{grossAmount:N(p,u=>u.remainingAmount),invoicesCount:p.length}}}})}function R(o,n){return{grossAmount:N(o,e=>{var t;return((t=e.outstandingBreakdown[n])==null?void 0:t.grossAmount)??0}),invoicesCount:N(o,e=>{var t;return((t=e.outstandingBreakdown[n])==null?void 0:t.invoicesCount)??0})}}function De(o){return{customer:{id:v,name:"Total"},outstandingBreakdown:{[l.UP_TO_ONE_MONTH]:R(o,l.UP_TO_ONE_MONTH),[l.ONE_TO_TWO_MONTHS]:R(o,l.ONE_TO_TWO_MONTHS),[l.TWO_TO_THREE_MONTHS]:R(o,l.TWO_TO_THREE_MONTHS),[l.MORE_THAN_THREE_MONTHS]:R(o,l.MORE_THAN_THREE_MONTHS)}}}function Ie(o,n){const e=o.addWorksheet("Invoices",{properties:{defaultColWidth:24},views:[{zoomScale:110}]});e.columns=[{header:"Invoice number",key:"invoiceNumber"},{header:"Customer",key:"customerName"},{header:"Issue date",key:"issueDate"},{header:"Due date",key:"dueDate"},{header:"Gross amount",key:"grossAmount",width:30,style:{numFmt:`${b(n.settings.currency)}#,##0.00`}},{header:"Days outstanding",key:"daysOutstanding",width:30,style:{numFmt:"#,##0"}}],e.autoFilter="A1:F1",["A1","B1","C1","D1","E1","F1"].map(t=>{e.getCell(t).fill={type:"pattern",pattern:"solid",fgColor:{argb:"46BDC6"}},e.getCell(t).font={color:{argb:"FFFFFF"}},e.getCell(t).alignment={vertical:"middle",horizontal:"center"}}),n.summaryBreakdown.forEach(({invoice:t,customer:s,daysOutstanding:a})=>{const i=e.addRow({invoiceNumber:t.invoiceNumber,customerName:s.name,issueDate:H(t.issuedAt)?D(t.issuedAt).format("D MMMM YYYY"):"",dueDate:H(t.dueDate)?D(t.dueDate).format("D MMMM YYYY"):"",grossAmount:t.revenueBreakdown.total,daysOutstanding:a});i.getCell(1).value={text:t.invoiceNumber,hyperlink:`${window.location.origin}/invoices/overview/light/${t.id}`,tooltip:`Open invoice ${t.id.slice(-5)} in Vayu`},i.getCell(2).value={text:s.name,hyperlink:`${window.location.origin}/customers/${s.id}`,tooltip:`Open ${s.name} in Vayu`},i.commit()})}function Fe(o,n){const e=o.addWorksheet("AR health as of date",{properties:{defaultColWidth:24,tabColor:{argb:"46BDC6"}},views:[{zoomScale:110}]});e.addRows([["AR health"],[""],["Currency",n.settings.currency],[""],[""],[""],["Days outstanding","Total gross amount","Number of invoices"],["1-30"],["31-60"],["61-90"],["91+"],["Total"],[""],[""],["Customer name","Customer ID","Total gross amount","1-30","31-60","61-90","91+"]]),n.summary.forEac
1h(({customer:t,outstandingBreakdown:s})=>{var i,c,p,u,T,_,C,m;const a=e.addRow([t.name,t.id.slice(-5),oe(d((i=s.upToOneMonth)==null?void 0:i.grossAmount,0),d((c=s.oneToTwoMonths)==null?void 0:c.grossAmount,0),d((p=s.twoToThreeMonths)==null?void 0:p.grossAmount,0),d((u=s.moreThanThreeMonths)==null?void 0:u.grossAmount,0)),d((T=s.upToOneMonth)==null?void 0:T.grossAmount,0),d((_=s.oneToTwoMonths)==null?void 0:_.grossAmount,0),d((C=s.twoToThreeMonths)==null?void 0:C.grossAmount,0),d((m=s.moreThanThreeMonths)==null?void 0:m.grossAmount,0)]);a.getCell(1).value={text:t.name,hyperlink:`${window.location.origin}/customers/${t.id}`,tooltip:`Open ${t.name} in Vayu`},a.getCell(3).numFmt=`${b(n.settings.currency)}#,##0.00`,a.getCell(4).numFmt="#,##0",a.getCell(5).numFmt="#,##0",a.getCell(6).numFmt="#,##0",a.getCell(7).numFmt="#,##0",a.commit()}),["B8","B9","B10","B11"].map((t,s)=>{const i=String.fromCharCode(68+s);e.getCell(t).value={formula:`SUM(${i}16:${i}${16+n.summary.length-1})`},e.getCell(t).numFmt=`${b(n.settings.currency)}#,##0.00`}),["C8","C9","C10","C11"].map((t,s)=>{const i=String.fromCharCode(68+s);e.getCell(t).value={formula:`COUNTIF(${i}16:${i}${16+n.summary.length-1}, ">0")`},e.getCell(t).numFmt="#,##0"}),e.getCell("B12").value={formula:"SUM(B8:B11)"},e.getCell("C12").value={formula:"SUM(C8:C11)"},e.getCell("B12").numFmt=`${b(n.settings.currency)}#,##0.00`,e.getCell("C12").numFmt="#,##0",["A3","B3","A7","B7","C7","A15","B15","C15","D15","E15","F15","G15"].map(t=>{e.getCell(t).fill={type:"pattern",pattern:"solid",fgColor:{argb:"46BDC6"}},e.getCell(t).font={color:{argb:"FFFFFF"}},e.getCell(t).alignment={vertical:"middle",horizontal:"center"}}),e.autoFilter="A15:G15"}const ke=()=>{const{data:o,isLoading:n}=Ee(),[e,t]=ae(),s=ge(),a=d(o==null?void 0:o.summaryBreakdown,[]),i=(o==null?void 0:o.settings.currency)??ne,{filters:c,handleFilterChange:p}=ie(),u=be(a),T=O.useMemo(()=>{let g=ve(a,c);const h=(e==null?void 0:e.toLowerCase())??"";return h&&(g=g.filter(M=>M.customer.name.toLowerCase().includes(h))),g},[a,c,e]),_=O.useMemo(()=>He(T),[T]),C=O.useMemo(()=>({bottom:[De(_)]}),[_]),m=async()=>{if(re(o))return;const g={...o,summary:_,summaryBreakdown:T};await J((async()=>{const h=new se.Workbook;Ie(h,g),Fe(h,g),await Q(h,"AR Aging Report")})(),{pending:"Downloading...",success:"Download complete",error:"Failed to download"})};return r.jsx(P,{title:"AR aging",export:{onExport:m},toolbar:r.jsx(Re,{searchText:e??"",onSearchTextChange:t,filterFields:u,filters:c,onFiltersChange:p}),children:r.jsx(xe,{apiRef:s,items:_,breakdownItems:T,currency:i,pinnedRows:C,loadingItems:n})})},Be=["How is my AR aging trending vs last quarter?","Show me the oldest outstanding invoices","Which customers are consistently paying late?","What's my bad debt risk exposure?","Who should I prioritize for collections today?"],We=()=>{const{isOpen:o,drawerWidth:n}=j();return r.jsx(f,{sx:{transition:e=>e.transitions.create("margin-right",{easing:o?e.transitions.easing.easeOut:e.transitions.easing.sharp,duration:o?e.transitions.duration.enteringScreen:e.transitions.duration.leavingScreen}),marginRight:o?`${n}px`:0},children:r.jsx($,{children:r.jsx(ke,{})})})},je=()=>{const{setSuggestions:o}=j();return O.useEffect(()=>(o(Be),()=>o(void 0)),[o]),r.jsxs(r.Fragment,{children:[r.jsx(G,{children:r.jsx("title",{children:"AR aging report"})}),r.jsx(We,{})]})},Ct=X(Z(je));export{Ct as ArAgingReportPage};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.