PageSourceSearch

https://crewops.lovable.app/assets/Invoices-CvPy-f7V.js

js crewops.lovable.app collected 2026-10-03 03:22:15 UTC 41,296 bytes, 1 lines download raw bytes

1import{I as ce,r as u,ad as ee,am as ne,j as e,aO as ve,aP as be,aQ as _e,a0 as L,as as O,at as V,au as $,av as B,aw as c,m as te,a$ as os,a1 as ae,b2 as Ke,B as E,a6 as we,K as N,al as Se,b5 as Ve,Q as K,V as J,Y as H,aL as se,aM as xe,aN as Qe,$ as Q,ap as X,aV as ds,q as ze,C as Ye,aK as Je,aJ as ms,b$ as us,ah as $e,aq as xs,cR as hs,i as js,T as ps,b as gs,p as Ns,D as ys}from"./index-B01jGDSO.js";import{T as Be,a as Ue,b as Z,c as ge}from"./tabs-DXmrEda4.js";import{T as oe,a as de,b as k,c as h,d as me,e as d}from"./table-B_s0nExu.js";import{f as Ce}from"./automationBridge-VRapK3du.js";import{E as fs,d as vs,f as Ne}from"./csvExport-BwqVVIcg.js";import{I as bs}from"./InlineEmptyState-DpbsC1Nn.js";import{C as _s,I as ws,R as Ss}from"./RecordPaymentDialog-JSHrGb_A.js";import{R as fe}from"./refresh-cw-DwcaIqPh.js";import{P as Cs}from"./pause-D1A_4xln.js";import{P as Is}from"./play-Rd75rCly.js";import{C as Ds}from"./credit-card-C2oB-bLE.js";import{A as Es}from"./arrow-right-D4XSCGwT.js";import{R as Ts}from"./receipt-9mTRnGx4.js";import{E as qs}from"./external-link-Clh8aBfZ.js";import"./outboxEvents-D7dGyeAO.js";import"./jobCosting-DzJJ0mgv.js";import"./jobEvents-DelCzNIm.js";import"./PrintActionButton-Bugp6Wiq.js";import"./printAction-DMEO5AFs.js";import"./download-BRkpldLd.js";import"./printer-BUmnyIk3.js";import"./ban-DEk9KXxK.js";function Fs({onClose:T}){const{toast:b}=ce(),[j,w]=u.useState(""),[y,R]=u.useState(""),[q,S]=u.useState("Net 30"),[_,x]=u.useState(()=>{const r=new Date;return r.setDate(r.getDate()+30),r.toISOString().split("T")[0]}),{data:a=[]}=ee({queryKey:["estimates-for-invoice"],queryFn:async()=>{const{data:r,error:i}=await N.from("estimates").select("id, project_number, project_name, status, client_id, clients:client_id(name, company)").in("status",["accepted","sent","approved"]).order("created_at",{ascending:!1});if(i)throw i;return r}}),{data:l}=ee({queryKey:["estimate-version-latest",j],enabled:!!j,queryFn:async()=>{const{data:r,error:i}=await N.from("estimate_versions").select("*").eq("estimate_id",j).order("version_number",{ascending:!1}).limit(1).single();if(i)throw i;return r}}),f=a.find(r=>r.id===j),C=l==null?void 0:l.snapshot,n=(C==null?void 0:C.line_items)||(C==null?void 0:C.items)||[],p=r=>new Intl.NumberFormat("en-US",{style:"currency",currency:"USD"}).format(r),g=ne({mutationFn:async()=>{if(!l||!f)throw new Error("Select an estimate");const{data:{user:r}}=await N.auth.getUser(),i=Number(l.sell_price)||0,m=Number(l.sales_tax)||0,I=Number(l.grand_total)||i+m;let D=null;const{data:z}=await N.from("jobs").select("id, job_number").eq("client_id",f.client_id).limit(1);z&&z.length>0&&(D=z[0].id);const{data:F,error:Y}=await N.from("invoices").insert({job_id:D,client_id:f.client_id||null,estimate_id:j,subtotal:i,tax_amount:m,tax_rate:m>0&&i>0?m/i*100:0,total:I,balance_due:I,due_date:_,notes:y||null,terms:q||null,created_by:(r==null?void 0:r.id)||null}).select().single();if(Y)throw Y;if(n.length>0){const G=n.map((v,ue)=>({invoice_id:F.id,description:v.description||v.name||v.material_name||"Line item",quantity:Number(v.quantity)||Number(v.qty)||1,unit_price:Number(v.unit_price)||Number(v.price)||Number(v.rate)||0,total:Number(v.total)||Number(v.amount)||Number(v.quantity||1)*Number(v.unit_price||v.price||v.rate||0),category:v.category||v.type||"general",line_item_type:(v.type||v.category||"SERVICE").toUpperCase(),sort_order:ue})),{error:A}=await N.from("invoice_line_items").insert(G);A&&console.error("Line item error:",A)}else{const G=[{desc:"Direct Costs",amount:Number(l.direct_cost)||0,type:"SERVICE"},{desc:"Overhead",amount:Number(l.overhead_allocation)||0,type:"SERVICE"}].filter(A=>A.amount>0).map((A,v)=>({invoice_id:F.id,description:A.desc,quantity:1,unit_price:A.amount,total:A.amount,category:"general",line_item_type:A.type,sort_order:v}));G.length===0&&G.push({invoice_id:F.id,description:`Services per Estimate #${f.project_number}`,quantity:1,unit_price:i,total:i,category:"general",line_item_type:"SERVICE",sort_order:0}),await N.from("invoice_line_items").insert(G)}return await N.from("financial_journal").insert({entry_type:"revenue",description:`Invoice INV-${String(F.invoice_number).padStart(4,"0")} from Estimate #${f.project_number}`,debit:I,credit:0,job_id:D||null,invoice_id:F.id,created_by:(r==null?void 0:r.id)||null}),D&&await N.from("jobs").update({status:"invoiced"}).eq("id",D),Ce("InvoiceCreated","Invoice",F.id,{invoice_number:F.invoice_number,total:I,client_id:f.client_id,job_id:D,from_estimate:f.project_number}),await N.from("estimates").update({status:"invoiced"}).eq("id",j),F},onSuccess:()=>{b({title:"Invoice created from estimate"}),T()},onError:r=>{b({title:"Failed to create invoice",description:r.message,variant:"destructive"})}});return e.jsxs(ve,{className:"max-w-lg max-h-[85vh] overflow-y-auto",children:[e.jsx(be,{children:e.jsx(_e,{children:"Create Invoice from Estimate"})}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(L,{children:"Select Estimate"}),e.jsxs(O,{value:j,onValueChange:w,children:[e.jsx(V,{children:e.jsx($,{placeholder:"Choose an estimate..."})}),e.jsx(B,{children:a.map(r=>{var i,m;
1return e.jsxs(c,{value:r.id,children:["#",r.project_number," — ",r.project_name||((i=r.clients)==null?void 0:i.company)||((m=r.clients)==null?void 0:m.name)||"No customer"]},r.id)})})]})]}),l&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"rounded-lg border p-3 space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"text-sm font-medium",children:"Estimate Snapshot"}),e.jsxs(te,{variant:"outline",children:["v",l.version_number]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-2 text-sm",children:[e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Subtotal:"})," ",e.jsx("span",{className:"font-medium",children:p(Number(l.sell_price)||0)})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Tax:"})," ",e.jsx("span",{className:"font-medium",children:p(Number(l.sales_tax)||0)})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Margin:"})," ",e.jsxs("span",{className:"font-medium",children:[(Number(l.actual_margin)||0).toFixed(1),"%"]})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Items:"})," ",e.jsx("span",{className:"font-medium",children:l.item_count||n.length})]})]}),e.jsx(os,{}),e.jsxs("div",{className:"flex items-center justify-between text-lg font-bold",children:[e.jsx("span",{children:"Grand Total"}),e.jsx("span",{children:p(Number(l.grand_total)||Number(l.sell_price)||0)})]})]}),n.length>0&&e.jsx("div",{className:"max-h-48 overflow-y-auto border rounded-lg",children:e.jsxs(oe,{children:[e.jsx(de,{children:e.jsxs(k,{children:[e.jsx(h,{className:"text-xs",children:"Item"}),e.jsx(h,{className:"text-right text-xs",children:"Qty"}),e.jsx(h,{className:"text-right text-xs",children:"Price"}),e.jsx(h,{className:"text-right text-xs",children:"Total"})]})}),e.jsxs(me,{children:[n.slice(0,10).map((r,i)=>e.jsxs(k,{children:[e.jsx(d,{className:"text-xs",children:r.description||r.name||r.material_name}),e.jsx(d,{className:"text-right text-xs",children:r.quantity||r.qty||1}),e.jsx(d,{className:"text-right text-xs",children:p(Number(r.unit_price||r.price||r.rate||0))}),e.jsx(d,{className:"text-right text-xs font-medium",children:p(Number(r.total||r.amount||0))})]},i)),n.length>10&&e.jsx(k,{children:e.jsxs(d,{colSpan:4,className:"text-center text-xs text-muted-foreground",children:["+",n.length-10," more items"]})})]})]})})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(L,{children:"Due Date"}),e.jsx(ae,{type:"date",value:_,onChange:r=>x(r.target.value)})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(L,{children:"Terms"}),e.jsxs(O,{value:q,onValueChange:S,children:[e.jsx(V,{children:e.jsx($,{})}),e.jsxs(B,{children:[e.jsx(c,{value:"Due on Receipt",children:"Due on Receipt"}),e.jsx(c,{value:"Net 15",children:"Net 15"}),e.jsx(c,{value:"Net 30",children:"Net 30"}),e.jsx(c,{value:"Net 45",children:"Net 45"}),e.jsx(c,{value:"Net 60",children:"Net 60"})]})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(L,{children:"Notes"}),e.jsx(Ke,{value:y,onChange:r=>R(r.target.value),placeholder:"Optional notes...",rows:2})]}),e.jsxs("div",{className:"flex gap-2 pt-2",children:[e.jsx(E,{variant:"outline",onClick:T,className:"flex-1",children:"Cancel"}),e.jsx(E,{onClick:()=>g.mutate(),disabled:g.isPending||!j||!l,className:"flex-1",children:g.isPending?e.jsxs(e.Fragment,{children:[e.jsx(we,{className:"h-4 w-4 mr-2 animate-spin"}),"Creating..."]}):"Create Invoice"})]})]})]})}function ks({onClose:T}){const{toast:b}=ce(),j=Se(),[w,y]=u.useState([]),[R,q]=u.useState("Net 30"),{data:S=[],isLoading:_}=ee({queryKey:["batch-invoice-jobs"],queryFn:async()=>{const{data:n,error:p}=await N.from("jobs").select("id, job_number, project_name, sell_price, client_id, status, estimated_labor_cost, estimated_equipment_cost, estimated_material_cost, estimated_subs_cost, clients:client_id(name, company)").in("status",["completed"]).order("job_number",{ascending:!1});if(p)throw p;return n}}),x=n=>
1new Intl.NumberFormat("en-US",{style:"currency",currency:"USD"}).format(n),a=n=>{y(p=>p.includes(n)?p.filter(g=>g!==n):[...p,n])},l=()=>{w.length===S.length?y([]):y(S.map(n=>n.id))},f=S.filter(n=>w.includes(n.id)).reduce((n,p)=>n+Number(p.sell_price||0),0),C=ne({mutationFn:async()=>{const{data:{user:n}}=await N.auth.getUser(),p=S.filter(m=>w.includes(m.id)),g=new Date,r={"Due on Receipt":0,"Net 15":15,"Net 30":30,"Net 45":45,"Net 60":60};g.setDate(g.getDate()+(r[R]||30));let i=0;for(const m of p){const I=Number(m.sell_price)||0,{data:D,error:z}=await N.from("invoices").insert({job_id:m.id,client_id:m.client_id,subtotal:I,total:I,balance_due:I,due_date:g.toISOString().split("T")[0],terms:R,created_by:(n==null?void 0:n.id)||null}).select().single();if(z){console.error("Batch invoice error:",z);continue}const F=[{description:"Labor",quantity:1,unit_price:Number(m.estimated_labor_cost),total:Number(m.estimated_labor_cost),category:"labor",sort_order:0,line_item_type:"LABOR"},{description:"Equipment",quantity:1,unit_price:Number(m.estimated_equipment_cost),total:Number(m.estimated_equipment_cost),category:"equipment",sort_order:1,line_item_type:"EQUIPMENT"},{description:"Materials",quantity:1,unit_price:Number(m.estimated_material_cost),total:Number(m.estimated_material_cost),category:"material",sort_order:2,line_item_type:"MATERIAL"},{description:"Subcontractors",quantity:1,unit_price:Number(m.estimated_subs_cost),total:Number(m.estimated_subs_cost),category:"subs",sort_order:3,line_item_type:"SERVICE"}].filter(Y=>Y.unit_price>0).map(Y=>({...Y,invoice_id:D.id}));F.length>0&&await N.from("invoice_line_items").insert(F),await N.from("financial_journal").insert({entry_type:"revenue",description:`Invoice INV-${String(D.invoice_number).padStart(4,"0")} for Job #${m.job_number}`,debit:I,credit:0,job_id:m.id,invoice_id:D.id,created_by:(n==null?void 0:n.id)||null}),await N.from("jobs").update({status:"invoiced"}).eq("id",m.id),Ce("InvoiceCreated","Invoice",D.id,{invoice_number:D.invoice_number,total:I,client_id:m.client_id,job_id:m.id}),i++}return i},onSuccess:n=>{b({title:`${n} invoice${n!==1?"s":""} created`}),j.invalidateQueries({queryKey:["invoices"]}),T()},onError:n=>{b({title:"Batch invoicing failed",description:n.message,variant:"destructive"})}});return e.jsxs(ve,{className:"max-w-2xl max-h-[80vh] overflow-y-auto",children:[e.jsx(be,{children:e.jsx(_e,{children:"Batch Create Invoices"})}),e.jsxs("div",{className:"space-y-4",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Select completed jobs to generate invoices for."}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(L,{children:"Payment Terms"}),e.jsxs(O,{value:R,onValueChange:q,children:[e.jsx(V,{className:"w-40",children:e.jsx($,{})}),e.jsxs(B,{children:[e.jsx(c,{value:"Due on Receipt",children:"Due on Receipt"}),e.jsx(c,{value:"Net 15",children:"Net 15"}),e.jsx(c,{value:"Net 30",children:"Net 30"}),e.jsx(c,{value:"Net 45",children:"Net 45"}),e.jsx(c,{value:"Net 60",children:"Net 60"})]})]})]}),e.jsxs("div",{className:"text-right",children:[e.jsxs("p",{className:"text-sm text-muted-foreground",children:[w.length," selected"]}),e.jsx("p",{className:"text-lg font-bold",children:x(f)})]})]}),_?e.jsx("p",{className:"text-center py-8 text-muted-foreground",children:"Loading jobs..."}):S.length===0?e.jsx("p",{className:"text-center py-8 text-muted-foreground",children:"No completed jobs available for invoicing."}):e.jsxs(oe,{children:[e.jsx(de,{children:e.jsxs(k,{children:[e.jsx(h,{className:"w-10",children:e.jsx(Ve,{checked:w.length===S.length&&S.length>0,onCheckedChange:l})}),e.jsx(h,{children:"Job #"}),e.jsx(h,{children:"Project"}),e.jsx(h,{children:"Client"}),e.jsx(h,{className:"text-right",children:"Amount"})]})}),e.jsx(me,{children:S.map(n=>{var p,g;return e.jsxs(k,{className:"cursor-pointer",onClick:()=>a(n.id),children:[e.jsx(d,{children:e.jsx(Ve,{checked:w.includes(n.id),onCheckedChange:()=>a(n.id)})}),e.jsxs(d,{className:"font-mono font-medium",children:["#",n.job_number]}),e.jsx(d,{children:n.project_name}),e.jsx(d,{children:((p=n.clients)==null?void 0:p.company)||((g=n.clients)==null?void 0:g.name)||"—"}),e.jsx(d,{className:"text-right font-medium",children:x(Number(n.sell_price||0))})]},n.id)})})]}),e.jsxs("div",{className:"flex gap-2 pt-2",children:[e.jsx(E,{variant:"outline",onClick:T,className:"flex-1",children:"Cancel"}),e.jsx(E,{onClick:()=>C.mutate(),disabled:C.isPending||w.length===0,className:"flex-1",children:C.isPending?e.jsxs(e.Fragment,{children:[e.jsx(we,{className:"h-4 w-4 mr-2 animate-spin"}),"Creating..."]}):`Create ${w.length} Invoice${w.length!==1?"s":""}`})]})]})]})}const Ps={weekly:"Weekly",biweekly:"Bi-weekly",monthly:"Monthly",quarterly:"Quarterly",annually:"Annually"};function Rs(){const{toast:T}=ce(),b=Se(),[j,w]=u.useState(!1),{data:y=[],isLoading:R}=ee({queryKey:["recurring-templates"],queryFn:async()=>{const{data:x,error:a}=await N.from("recurring_invoice_templates").select("*, clients:client_id(name, company)").order("created_at",{ascending:!1});if(a)throw a;return x}}),q=ne({mutationFn:async({id:x,is_active:a})=>{const{error:l}=await N.from("recurring_invoice_templates").update({is_active:a}).eq("id",x);if(l)throw l},onSuccess:()=>{b.invalidateQueries({queryKey:["recurring-templates"]}),T({title:"Template updated"})}}),S=ne({mutationFn:async x=>{const{error:a}=await N.from("recurring_invoice_templates").delete().eq("id",x);if(a)throw a},onSuccess:()=>{b.invalidateQueries({queryKey:["recurring-templates"]}),T({title:"Template deleted"})}}),_=x=>new Intl.NumberFormat("en-US",{style:"currency",currency:"USD"}).format(x);return e.jsxs(K,{children:[e.jsxs(J,{className:"flex flex-row items-center justify-between",children:[e.jsxs(H,{
1className:"flex items-center gap-2",children:[e.jsx(fe,{className:"h-5 w-5"}),"Recurring Invoices"]}),e.jsxs(se,{open:j,onOpenChange:w,children:[e.jsx(xe,{asChild:!0,children:e.jsxs(E,{size:"sm",children:[e.jsx(Qe,{className:"h-4 w-4 mr-1"}),"New Template"]})}),e.jsx(Ms,{onClose:()=>{w(!1),b.invalidateQueries({queryKey:["recurring-templates"]})}})]})]}),e.jsx(Q,{children:R?e.jsx("p",{className:"text-center py-8 text-muted-foreground",children:"Loading..."}):y.length===0?e.jsxs("div",{className:"text-center py-8",children:[e.jsx(fe,{className:"h-8 w-8 mx-auto text-muted-foreground mb-2"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"No recurring templates yet. Create one to auto-generate invoices on a schedule."})]}):e.jsxs(oe,{children:[e.jsx(de,{children:e.jsxs(k,{children:[e.jsx(h,{children:"Template"}),e.jsx(h,{children:"Customer"}),e.jsx(h,{children:"Frequency"}),e.jsx(h,{className:"text-right",children:"Amount"}),e.jsx(h,{children:"Next Invoice"}),e.jsx(h,{children:"Status"}),e.jsx(h,{className:"w-[100px]",children:"Actions"})]})}),e.jsx(me,{children:y.map(x=>{var a,l;return e.jsxs(k,{children:[e.jsx(d,{className:"font-medium",children:x.template_name}),e.jsx(d,{children:((a=x.clients)==null?void 0:a.company)||((l=x.clients)==null?void 0:l.name)||"—"}),e.jsx(d,{children:Ps[x.frequency]||x.frequency}),e.jsx(d,{className:"text-right",children:_(Number(x.amount))}),e.jsx(d,{children:X(new Date(x.next_invoice_date),"MMM d, yyyy")}),e.jsx(d,{children:e.jsx(te,{variant:x.is_active?"default":"secondary",children:x.is_active?"Active":"Paused"})}),e.jsx(d,{children:e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(E,{size:"sm",variant:"ghost",className:"h-7 w-7 p-0",onClick:()=>q.mutate({id:x.id,is_active:!x.is_active}),title:x.is_active?"Pause":"Resume",children:x.is_active?e.jsx(Cs,{className:"h-3.5 w-3.5"}):e.jsx(Is,{className:"h-3.5 w-3.5"})}),e.jsx(E,{size:"sm",variant:"ghost",className:"h-7 w-7 p-0 text-destructive",onClick:()=>S.mutate(x.id),children:e.jsx(ds,{className:"h-3.5 w-3.5"})})]})})]},x.id)})})]})})]})}function Ms({onClose:T}){const{toast:b}=ce(),[j,w]=u.useState(""),[y,R]=u.useState(""),[q,S]=u.useState("monthly"),[_,x]=u.useState(""),[a,l]=u.useState(()=>{const i=new Date;return i.setMonth(i.getMonth()+1),i.toISOString().split("T")[0]}),[f,C]=u.useState("Net 30"),[n,p]=u.useState(""),{data:g=[]}=ee({queryKey:["recurring-clients"],queryFn:async()=>{const{data:i,error:m}=await N.from("clients").select("id, name, company").order("name");if(m)throw m;return i}}),r=ne({mutationFn:async()=>{const{data:{user:i}}=await N.auth.getUser(),m=[{description:j,quantity:1,unit_price:parseFloat(_),total:parseFloat(_)}],{error:I}=await N.from("recurring_invoice_templates").insert({template_name:j,client_id:y||null,frequency:q,amount:parseFloat(_)||0,next_invoice_date:a,terms:f,notes:n||null,line_items_json:m,created_by:(i==null?void 0:i.id)||null});if(I)throw I},onSuccess:()=>{b({title:"Recurring template created"}),T()},onError:i=>{b({title:"Failed to create template",description:i.message,variant:"destructive"})}});return e.jsxs(ve,{className:"max-w-md",children:[e.jsx(be,{children:e.jsx(_e,{children:"New Recurring Invoice Template"})}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(L,{children:"Template Name *"}),e.jsx(ae,{value:j,onChange:i=>w(i.target.value),placeholder:"e.g. Monthly Lawn Care"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(L,{children:"Customer"}),e.jsxs(O,{value:y,onValueChange:R,children:[e.jsx(V,{children:e.jsx($,{placeholder:"Select customer..."})}),e.jsx(B,{children:g.map(i=>e.jsx(c,{value:i.id,children:i.company||i.name},i.id))})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(L,{children:"Frequency"}),e.jsxs(O,{value:q,onValueChange:S,children:[e.jsx(V,{children:e.jsx($,{})}),e.jsxs(B,{children:[e.jsx(c,{value:"weekly",children:"Weekly"}),e.jsx(c,{value:"biweekly",children:"Bi-weekly"}),e.jsx(c,{value:"monthly",children:"Monthly"}),e.jsx(c,{value:"quarterly",children:"Quarterly"}),e.jsx(c,{value:"annually",children:"Annually"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(L,{children:"Amount ($)"}
1),e.jsx(ae,{type:"number",step:"0.01",value:_,onChange:i=>x(i.target.value),placeholder:"0.00"})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(L,{children:"Next Invoice Date"}),e.jsx(ae,{type:"date",value:a,onChange:i=>l(i.target.value)})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(L,{children:"Terms"}),e.jsxs(O,{value:f,onValueChange:C,children:[e.jsx(V,{children:e.jsx($,{})}),e.jsxs(B,{children:[e.jsx(c,{value:"Due on Receipt",children:"Due on Receipt"}),e.jsx(c,{value:"Net 15",children:"Net 15"}),e.jsx(c,{value:"Net 30",children:"Net 30"}),e.jsx(c,{value:"Net 45",children:"Net 45"}),e.jsx(c,{value:"Net 60",children:"Net 60"})]})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(L,{children:"Notes"}),e.jsx(Ke,{value:n,onChange:i=>p(i.target.value),placeholder:"Optional notes on each invoice...",rows:2})]}),e.jsxs("div",{className:"flex gap-2 pt-2",children:[e.jsx(E,{variant:"outline",onClick:T,className:"flex-1",children:"Cancel"}),e.jsx(E,{onClick:()=>r.mutate(),disabled:r.isPending||!j||!_,className:"flex-1",children:r.isPending?"Creating...":"Create Template"})]})]})]})}const As={SUCCEEDED:"bg-green-500/20 text-green-600",CLEARED:"bg-green-500/20 text-green-600",PENDING:"bg-yellow-500/20 text-yellow-600",FAILED:"bg-destructive/20 text-destructive",REFUNDED:"bg-muted text-muted-foreground"},Ls={STRIPE:"💳",SQUARE:"◼️",MANUAL:"📝",ACH:"🏦"};function Os(){const[T,b]=u.useState(""),[j,w]=u.useState("all"),{data:y=[],isLoading:R}=ee({queryKey:["all-payments"],queryFn:async()=>{const{data:a,error:l}=await N.from("payments").select("*, invoices:invoice_id(invoice_number, client_id, clients:client_id(name, company))").order("payment_date",{ascending:!1}).limit(200);if(l)throw l;return a}}),q=u.useMemo(()=>y.filter(a=>{var n,p,g,r,i,m,I,D;const l=T.toLowerCase(),f=!l||String((n=a.invoices)==null?void 0:n.invoice_number).includes(l)||((r=(g=(p=a.invoices)==null?void 0:p.clients)==null?void 0:g.name)==null?void 0:r.toLowerCase().includes(l))||((I=(m=(i=a.invoices)==null?void 0:i.clients)==null?void 0:m.company)==null?void 0:I.toLowerCase().includes(l))||((D=a.reference_number)==null?void 0:D.toLowerCase().includes(l)),C=j==="all"||a.status===j;return f&&C}),[y,T,j]),S=a=>new Intl.NumberFormat("en-US",{style:"currency",currency:"USD"}).format(a),_=y.filter(a=>["SUCCEEDED","CLEARED"].includes(a.status)).reduce((a,l)=>a+Number(l.amount),0),x=y.filter(a=>a.status==="PENDING").reduce((a,l)=>a+Number(l.amount),0);return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"grid gap-4 md:grid-cols-3",children:[e.jsxs(K,{children:[e.jsxs(J,{className:"flex flex-row items-center justify-between space-y-0 pb-2",children:[e.jsx(H,{className:"text-sm font-medium",children:"Total Received"}),e.jsx(ze,{className:"h-4 w-4 text-green-600"})]}),e.jsx(Q,{children:e.jsx("div",{className:"text-2xl font-bold text-green-600",children:S(_)})})]}),e.jsxs(K,{children:[e.jsxs(J,{className:"flex flex-row items-center justify-between space-y-0 pb-2",children:[e.jsx(H,{className:"text-sm font-medium",children:"Pending"}),e.jsx(Ye,{className:"h-4 w-4 text-yellow-600"})]}),e.jsx(Q,{children:e.jsx("div",{className:"text-2xl font-bold text-yellow-600",children:S(x)})})]}),e.jsxs(K,{children:[e.jsxs(J,{className:"flex flex-row items-center justify-between space-y-0 pb-2",children:[e.jsx(H,{className:"text-sm font-medium",children:"Total Payments"}),e.jsx(Ds,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(Q,{children:e.jsx("div",{className:"text-2xl font-bold",children:y.length})})]})]}),e.jsxs("div",{className:"flex flex-col gap-3 sm:flex-row sm:items-center",children:[e.jsxs("div",{className:"relative flex-1 max-w-sm",children:[e.jsx(Je,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(ae,{placeholder:"Search payments...",value:T,onChange:a=>b(a.target.value),className:"pl-9"})]}),e.jsxs(O,{value:j,onValueChange:w,children:[e.jsx(V,{className:"w-[150px]",children:e.jsx($,{placeholder:"Status"})}),e.jsxs(B,{children:[e.jsx(c,{value:"all",children:"All Statuses"}),e.jsx(c,{value:"SUCCEEDED",children:"Succeeded"}),e.jsx(c,{value:"CLEARED",children:"Cleared"}),e.jsx(c,{value:"PENDING",children:"Pending"}),e.jsx(c,{value:"FAILED",children:"Failed"})]})]})]}),e.jsx(K,{children:e.jsx(Q,{className:"p-0",children:e.jsxs(oe,{children:[e.jsx(de,{children:e.jsxs(k,{children:[e.jsx(h,{children:"Invoice"}),e.jsx(h,{children:"Customer"}),e.jsx(h,{children:"Provider"}),e.jsx(h,{children:"Method"}),e.jsx(h,{className:"text-right",children:"Amount"}),e.jsx(h,{children:"Status"}),e.jsx(h,{children:"Reference"}),e.jsx(h,{children:"Date"})]})}),e.jsx(me,{children:R?e.jsx(k,{children:e.jsx(d,{colSpan:8,className:"text-center py-8 text-muted-foreground",children:"Loading..."})}):q.length===0?e.jsx(k,{children:e.jsx(d,{colSpan:8,className:"text-center py-8 text-muted-foreground",children:"No payments found"})}):q.map(a=>{var l,f,C,n;return e.jsxs(k,{children:[e.jsx(d,{className:"font-mono text-sm",children:a.invoices?`INV-${String(a.invoices.invoice_number).padStart(4,"0")}`:"—"}),e.jsx(d,{className:"text-sm",children:((f=(l=a.invoices)==null?void 0:l.clients)==null?void 0:f.company)||((n=(C=a.invoices)==null?void 0:C.clients)==null?void 0:n.name)||"—"}),e.jsx(d,{children:e.jsxs("span",{className:"text-sm",children:[Ls[a.provider]||""," ",a.provider]})}),e.jsx(d,{className:"text-sm capitalize",children:a.payment_method||"—"}),e.jsx(d,{className:"text-right font-medium",children:S(Number(a.amount))}),e.jsx(d,{children:e.jsx(te,{className:As[a.status]||"",variant:"secondary",children:a.status})}),e.jsx(d,{className:"text-sm text-muted-foreground",children:a.reference_number||"—"}),e.jsx(d,{className:"text-sm",children:a.payment_date?X(new Date(a.payment_date),"MMM d, yyyy"):"—"})]},a.id)})})]})})})]})}const Vs={draft:"bg-muted text-muted-foreground",sent:"bg-blue-500/20 text-blue-600",viewed:"bg-purple-500/20 text-purple-600",partial:"bg-yellow-500/20 text-yellow-600",paid:"bg-green-500/20 text-green-600",overdue:"bg-destructive/20 text-destructive",cancelled:"bg-muted text-muted-foreground",void:"bg-muted text-muted-foreground
1/60"},ye={draft:"Draft",sent:"Sent",viewed:"Viewed",partial:"Partial",paid:"Paid",overdue:"Overdue",cancelled:"Cancelled",void:"Void"};function ot(){const{toast:T}=ce(),b=Se(),[j,w]=u.useState(""),[y,R]=u.useState("all"),[q,S]=u.useState("all"),[_,x]=u.useState("all"),[a,l]=u.useState(String(new Date().getFullYear())),[f,C]=u.useState("newest"),[n,p]=u.useState("all"),[g,r]=u.useState(1),i=25,[m,I]=u.useState(!1),[D,z]=u.useState(null),[F,Y]=u.useState(null),[G,A]=u.useState(!1),[v,ue]=u.useState(!1),[He,Ge]=u.useState("invoices"),{connection:We,syncPayments:Ze,isSyncing:Ie}=ms(),{data:he=[],isLoading:Xe}=ee({queryKey:["invoices"],queryFn:async()=>{const{data:s,error:t}=await N.from("invoices").select("*, clients:client_id(name, company, source), jobs:job_id(job_number, project_name)").order("created_at",{ascending:!1});if(t)throw t;return s}}),U=u.useMemo(()=>he.map(s=>{const t=["sent","viewed","partial"].includes(s.status)&&s.due_date&&us(new Date(s.due_date));return{...s,displayStatus:t?"overdue":s.status}}),[he]),es=u.useMemo(()=>{const s=new Set,t=new Date().getFullYear();for(let o=t+1;o>=2020;o--)s.add(String(o));return U.forEach(o=>{o.issue_date&&s.add(String(new Date(o.issue_date).getFullYear()))}),["all",...Array.from(s).sort((o,P)=>Number(P)-Number(o))]},[U]),De=U.filter(s=>{var Te,qe,Fe,ke,Pe,Re,Me,Ae,Le,Oe;const t=j.toLowerCase(),o=!t||String(s.invoice_number).includes(t)||((qe=(Te=s.clients)==null?void 0:Te.name)==null?void 0:qe.toLowerCase().includes(t))||((ke=(Fe=s.clients)==null?void 0:Fe.company)==null?void 0:ke.toLowerCase().includes(t))||((Re=(Pe=s.jobs)==null?void 0:Pe.project_name)==null?void 0:Re.toLowerCase().includes(t)),P=a==="all"||s.issue_date&&String(new Date(s.issue_date).getFullYear())===a,M=y==="all"||s.displayStatus===y,ie=q==="all"||((Me=s.clients)==null?void 0:Me.source)===q,cs=_==="all"||_==="residential"&&!((Ae=s.clients)!=null&&Ae.company)||_==="commercial"&&!!((Le=s.clients)!=null&&Le.company)||_==="from_leads"&&(((Oe=s.clients)==null?void 0:Oe.source)||"").toLowerCase().includes("lead"),le=o&&P&&M&&ie&&cs;return n==="all"?le:n==="outstanding"?le&&["sent","viewed","partial","overdue"].includes(s.displayStatus):n==="paid"?le&&s.displayStatus==="paid":n==="draft"?le&&s.displayStatus==="draft":le}),W=u.useMemo(()=>{const s=[...De];switch(f){case"oldest":s.sort((t,o)=>new Date(t.issue_date||t.created_at).getTime()-new Date(o.issue_date||o.created_at).getTime());break;case"num_asc":s.sort((t,o)=>Number(t.invoice_number)-Number(o.invoice_number));break;case"num_desc":s.sort((t,o)=>Number(o.invoice_number)-Number(t.invoice_number));break;case"amt_low":s.sort((t,o)=>Number(t.total)-Number(o.total));break;case"amt_high":s.sort((t,o)=>Number(o.total)-Number(t.total));break;default:s.sort((t,o)=>new Date(o.issue_date||o.created_at).getTime()-new Date(t.issue_date||t.created_at).getTime());break}return s},[De,f]),je=Math.max(1,Math.ceil(W.length/i)),ss=W.slice((g-1)*i,g*i),pe=`${j}|${y}|${q}|${a}|${n}|${f}|${_}`,Ee=u.useRef(pe);Ee.current!==pe&&(Ee.current=pe,g!==1&&r(1));const re=s=>new Intl.NumberFormat("en-US",{style:"currency",currency:"USD"}).format(s),ts=U.filter(s=>["sent","viewed","partial","overdue"].includes(s.displayStatus)).reduce((s,t)=>s+Number(t.balance_due),0),as=U.filter(s=>s.displayStatus==="paid").reduce((s,t)=>s+Number(t.total),0),ns=U.filter(s=>s.displayStatus==="overdue").reduce((s,t)=>s+Number(t.balance_due),0),rs=u.useMemo(()=>{const s=U.filter(o=>o.displayStatus==="paid"&&o.issue_date&&o.paid_at);if(s.length===0)return 0;const t=s.reduce((o,P)=>{const M=new Date(P.issue_date),ie=new Date(P.paid_at);return o+Math.max(0,$e(ie,M))},0);return Math.round(t/s.length)},[U]),is=ne({mutationFn:async({id:s,status:t})=>{var P;const{error:o}=await N.from("invoices").update({status:t}).eq("id",s);if(o)throw o;if(t==="sent"){const M=he.find(ie=>ie.id===s);Ce("EstimateSent","Invoice",s,{invoice_number:M==null?void 0:M.invoice_number,total:Number(M==null?void 0:M.total),client_name:(P=M==null?void 0:M.clients)==null?void 0:P.name})}},onSuccess:()=>{b.invalidateQueries({queryKey:["invoices"]}),T({title:"Invoice status updated"})}}),ls=s=>{if(!["sent","viewed","partial"].includes(s.status)||!s.due_date)return null;const t=$e(new Date(s.due_date),new Date);
1return t<0?e.jsxs(te,{variant:"destructive",className:"text-[10px] ml-1",children:[Math.abs(t),"d overdue"]}):t<=7?e.jsxs(te,{className:"bg-yellow-500/20 text-yellow-600 text-[10px] ml-1",children:["Due in ",t,"d"]}):null};return e.jsxs(xs,{children:[e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex flex-col gap-3 sm:gap-4 sm:flex-row sm:items-center sm:justify-between",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl sm:text-3xl font-bold tracking-tight",children:"Invoices"}),e.jsx("p",{className:"text-muted-foreground text-sm",children:"Create, send, and track invoices"})]}),e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[We&&e.jsxs(E,{variant:"outline",size:"sm",onClick:()=>{Ze("pull"),b.invalidateQueries({queryKey:["invoices"]})},disabled:Ie,children:[Ie?e.jsx(we,{className:"mr-2 h-4 w-4 animate-spin"}):e.jsx(fe,{className:"mr-2 h-4 w-4"}),"Sync QB"]}),e.jsx(fs,{label:"Export CSV",onExport:async()=>{if(!W.length)throw new Error("No invoices to export");vs(["Invoice #","Customer","Job #","Issue Date","Due Date","Total","Paid","Balance","Status"],W.map(s=>{var t,o,P;return[`INV-${String(s.invoice_number).padStart(4,"0")}`,((t=s.clients)==null?void 0:t.company)||((o=s.clients)==null?void 0:o.name)||"",((P=s.jobs)==null?void 0:P.job_number)||"",s.issue_date?X(new Date(s.issue_date),"MM/dd/yyyy"):"",s.due_date?X(new Date(s.due_date),"MM/dd/yyyy"):"",Ne(s.total),Ne(s.amount_paid),Ne(s.balance_due),ye[s.displayStatus]||s.displayStatus]}),`invoices_${X(new Date,"yyyy-MM-dd")}.csv`)}}),e.jsxs(se,{open:G,onOpenChange:A,children:[e.jsx(xe,{asChild:!0,children:e.jsxs(E,{variant:"outline",children:[e.jsx(hs,{className:"mr-2 h-4 w-4"}),"Batch Invoice"]})}),e.jsx(ks,{onClose:()=>{A(!1),b.invalidateQueries({queryKey:["invoices"]})}})]}),e.jsxs(se,{open:v,onOpenChange:ue,children:[e.jsx(xe,{asChild:!0,children:e.jsxs(E,{variant:"outline",children:[e.jsx(Es,{className:"mr-2 h-4 w-4"}),"From Estimate"]})}),e.jsx(Fs,{onClose:()=>{ue(!1),b.invalidateQueries({queryKey:["invoices"]})}})]}),e.jsxs(se,{open:m,onOpenChange:I,children:[e.jsx(xe,{asChild:!0,children:e.jsxs(E,{children:[e.jsx(Qe,{className:"mr-2 h-4 w-4"}),"New Invoice"]})}),e.jsx(_s,{onClose:()=>{I(!1),b.invalidateQueries({queryKey:["invoices"]})}})]})]})]}),e.jsxs(Be,{value:He,onValueChange:Ge,children:[e.jsxs(Ue,{children:[e.jsx(Z,{value:"invoices",children:"Invoices"}),e.jsx(Z,{value:"payments",children:"Payments"}),e.jsx(Z,{value:"recurring",children:"Recurring"})]}),e.jsx(ge,{value:"recurring",className:"mt-4",children:e.jsx(Rs,{})}),e.jsx(ge,{value:"payments",className:"mt-4",children:e.jsx(Os,{})}),e.jsxs(ge,{value:"invoices",className:"space-y-6 mt-4",children:[e.jsxs("div",{className:"grid gap-3 sm:gap-4 grid-cols-2 sm:grid-cols-3 md:grid-cols-5",children:[e.jsxs(K,{children:[e.jsxs(J,{className:"flex flex-row items-center justify-between space-y-0 pb-2 px-3 sm:px-6",children:[e.jsx(H,{className:"text-xs sm:text-sm font-medium",children:"Total Invoices"}),e.jsx(js,{className:"h-4 w-4 text-muted-foreground hidden sm:block"})]}),e.jsx(Q,{className:"px-3 sm:px-6",children:e.jsx("div",{className:"text-lg sm:text-2xl font-bold",children:U.length})})]}),e.jsxs(K,{children:[e.jsxs(J,{className:"flex flex-row items-center justify-between space-y-0 pb-2 px-3 sm:px-6",children:[e.jsx(H,{className:"text-xs sm:text-sm font-medium",children:"Outstanding"}),e.jsx(Ye,{className:"h-4 w-4 text-muted-foreground hidden sm:block"})]}),e.jsx(Q,{className:"px-3 sm:px-6",children:e.jsx("div",{className:"text-lg sm:text-2xl font-bold truncate",children:re(ts)})})]}),e.jsxs(K,{children:[e.jsxs(J,{className:"flex flex-row items-center justify-between space-y-0 pb-2 px-3 sm:px-6",children:[e.jsx(H,{className:"text-xs sm:text-sm font-medium",children:"Collected"}),e.jsx(ze,{className:"h-4 w-4 text-green-600 hidden sm:block"})]}),e.jsx(Q,{className:"px-3 sm:px-6",children:e.jsx("div",{className:"text-lg sm:text-2xl font-bold text-green-600 truncate",children:re(as)})})]}),e.jsxs(K,{children:[e.jsxs(J,{className:"flex flex-row items-center justify-between space-y-0 pb-2 px-3 sm:px-6",children:[e.jsx(H,{className:"text-xs sm:text-sm font-medium",children:"Overdue"}),e.jsx(ps,{className:"h-4 w-4 text-destructive hidden sm:block"})]}),e.jsx(Q,{className:"px-3 sm:px-6",children:e.jsx("div",{className:"text-lg sm:text-2xl font-bold text-destructive truncate",children:re(ns)})})]}),e.jsxs(K,{className:"col-span-2 sm:col-span-1",children:[e.jsxs(J,{className:"flex flex-row items-center justify-between space-y-0 pb-2 px-3 sm:px-6",children:[e.jsx(H,{className:"text-xs sm:text-sm font-medium",children:"Avg Days to Pay"}),e.jsx(gs,{className:"h-4 w-4 text-muted-foreground hidden sm:block"})]}),e.jsxs(Q,{className:"px-3 sm:px-6",children:[e.jsx("div",{className:"text-lg sm:text-2xl font-bold",children:rs}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"days"})]})]})]}),e.jsxs("div",{className:"flex flex-col gap-3 sm:flex-row sm:items-center sm:flex-wrap",children:[e.jsxs("div",{className:"relative flex-1 min-w-0 max-w-sm",children:[e.jsx(Je,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(ae,{placeholder:"Search invoices...",value:j,onChange:s=>w(s.target.value),className:"pl-9"})]}),e.jsxs(O,{value:_,onValueChange:x,children:[e.jsx(V,{className:"w-full sm:w-[160px]",children:e.jsx($,{placeholder:"Customer Type"})}),e.jsxs(B,{className:"bg-popover z-50",children:[e.jsx(c,{value:"all",children:"All Types"}),e.jsx(c,{value:"residential",children:"Residential"}),e.jsx(c,{value:"commercial",children:"Commercial"}),e.jsx(c,{value:"from_leads",children:"From Leads"})]})]}),e.jsxs(O,{value:a,onValueChange:l,children:[e.jsx(V,{className:"w-full sm:w-[120px]",children:e.jsx($,{placeholder:"Year"})}),e.jsx(B,{className:"bg-popover z-50",children:es.map(s=>e.jsx(c,{value:s,children:s==="all"?"All Years":s},s))})]}),e.jsxs(O,{value:y,onValueChange:R,children:[e.jsx(V,{className:"w-full sm:w-[140px]",children:e.jsx($,{placeholder:"Status"})}),e.jsxs(B,{className:"bg-popover z-50",children:[e.jsx(c,{value:"all",children:"All Statuses"}
1),Object.entries(ye).map(([s,t])=>e.jsx(c,{value:s,children:t},s))]})]}),e.jsxs(O,{value:q,onValueChange:S,children:[e.jsx(V,{className:"w-full sm:w-[160px]",children:e.jsx($,{placeholder:"Source"})}),e.jsxs(B,{className:"bg-popover z-50",children:[e.jsx(c,{value:"all",children:"All Sources"}),["BRF Chamber of Commerce","Facebook","Flyer Radio","Referral","Repeat Customer","Tri-County Advi$or","Web Search","Website","Yard Sign"].map(s=>e.jsx(c,{value:s,children:s},s))]})]}),e.jsxs("div",{className:"flex items-center gap-1 text-sm text-muted-foreground min-w-0",children:[e.jsx("span",{className:"shrink-0",children:"Sort:"}),e.jsxs(O,{value:f,onValueChange:C,children:[e.jsx(V,{className:"w-full sm:w-[140px]",children:e.jsx($,{})}),e.jsxs(B,{children:[e.jsx(c,{value:"newest",children:"Newest First"}),e.jsx(c,{value:"oldest",children:"Oldest First"}),e.jsx(c,{value:"num_asc",children:"Number: Asc"}),e.jsx(c,{value:"num_desc",children:"Number: Desc"}),e.jsx(c,{value:"amt_low",children:"Amount: Lowest"}),e.jsx(c,{value:"amt_high",children:"Amount: Highest"})]})]})]}),e.jsx("div",{className:"w-full overflow-x-auto -mx-1 px-1",children:e.jsx(Be,{value:n,onValueChange:p,children:e.jsxs(Ue,{className:"w-max",children:[e.jsxs(Z,{value:"all",className:"text-xs sm:text-sm",children:["All (",U.length,")"]}),e.jsx(Z,{value:"outstanding",className:"text-xs sm:text-sm",children:"Outstanding"}),e.jsx(Z,{value:"paid",className:"text-xs sm:text-sm",children:"Paid"}),e.jsx(Z,{value:"draft",className:"text-xs sm:text-sm",children:"Drafts"})]})})})]}),e.jsx(K,{children:e.jsx(Q,{className:"p-0 overflow-x-auto",children:e.jsxs(oe,{className:"min-w-[700px]",children:[e.jsx(de,{children:e.jsxs(k,{children:[e.jsx(h,{children:"Invoice #"}),e.jsx(h,{children:"Customer"}),e.jsx(h,{children:"Job"}),e.jsx(h,{children:"Issued"}),e.jsx(h,{children:"Due"}),e.jsx(h,{className:"text-right",children:"Total"}),e.jsx(h,{className:"text-right",children:"Balance"}),e.jsx(h,{children:"Status"}),e.jsx(h,{className:"w-[160px]",children:"Actions"})]})}),e.jsx(me,{children:Xe?e.jsx(k,{children:e.jsx(d,{colSpan:9,className:"text-center py-8 text-muted-foreground",children:"Loading..."})}):W.length===0?e.jsx(k,{children:e.jsx(d,{colSpan:9,children:e.jsx(bs,{icon:Ts,title:"No invoices found",description:"Adjust your filters or create a new invoice."})})}):ss.map(s=>{var t,o;return e.jsxs(k,{className:`cursor-pointer hover:bg-muted/50 ${s.displayStatus==="overdue"?"bg-destructive/5":""}`,onClick:()=>z(s.id),children:[e.jsxs(d,{className:"font-medium font-mono",children:["INV-",String(s.invoice_number).padStart(4,"0")]}),e.jsx(d,{children:((t=s.clients)==null?void 0:t.company)||((o=s.clients)==null?void 0:o.name)||"—"}),e.jsx(d,{className:"text-muted-foreground",children:s.jobs?`#${s.jobs.job_number}`:"—"}),e.jsx(d,{className:"text-sm",children:X(new Date(s.issue_date),"MMM d, yyyy")}),e.jsxs(d,{className:"text-sm",children:[X(new Date(s.due_date),"MMM d, yyyy"),ls(s)]}),e.jsx(d,{className:"text-right font-medium",children:re(Number(s.total))}),e.jsx(d,{className:"text-right",children:e.jsx("span",{className:Number(s.balance_due)>0?"text-destructive font-medium":"text-green-600",children:re(Number(s.balance_due))})}),e.jsx(d,{children:e.jsx(te,{className:Vs[s.displayStatus]||"",variant:"secondary",children:ye[s.displayStatus]||s.displayStatus})}),e.jsx(d,{onClick:P=>P.stopPropagation(),children:e.jsxs("div",{className:"flex items-center gap-1",children:[s.status==="draft"&&e.jsxs(E,{size:"sm",variant:"outline",className:"h-7 text-xs",onClick:()=>is.mutate({id:s.id,status:"sent"}),children:[e.jsx(Ns,{className:"h-3 w-3 mr-1"}),"Send"]}),["sent","viewed","partial","overdue"].includes(s.displayStatus)&&e.jsxs(e.Fragment,{children:[e.jsxs(E,{size:"sm",variant:"outline",className:"h-7 text-xs",onClick:()=>Y(s.id),children:[e.jsx(ys,{className:"h-3 w-3 mr-1"}),"Pay"]}),s.token&&e.jsx(E,{size:"sm",variant:"ghost",className:"h-7 w-7 p-0",onClick:()=>window.open(`/pay/${s.token}`,"_blank"),title:"Open customer pay link",children:e.jsx(qs,{className:"h-3.5 w-3.5"})})]})]})})]},s.id)})})]})})}),W.length>0&&e.jsxs("div",{className:"flex flex-col sm:flex-row items-start sm:items-center justify-between gap-2",children:[e.jsxs("div",{className:"flex items-center gap-1 flex-wrap",children:[(()=>{const s=[];for(let t=1;t<=je;t++)t<=10||t===je||t>=g-1&&t<=g+1?s.push(t):s[s.length-1]!=="..."&&s.push("...");return s.map((t,o)=>t==="..."?e.jsx("span",{className:"px-1 text-sm text-muted-foreground",children:"..."},`ellipsis-${o}`):e.jsx(E,{variant:g===t?"default":"ghost",size:"sm",className:"h-7 w-7 p-0 text-xs",onClick:()=>r(t),children:t},t))})(),e.jsx(E,{variant:"ghost",size:"sm",className:"h-7 px-2 text-xs",disabled:g>=je,onClick:()=>r(s=>s+1),children:"Next »"})]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Showing ",W.length," of ",U.length," invoices"]})]})]})]})]}),D&&e.jsx(se,{open:!!D,onOpenChange:()=>z(null),children:e.jsx(ws,{invoiceId:D,onClose:()=>z(null)})}),F&&e.jsx(se,{open:!!F,onOpenChange:()=>Y(null),children:e.jsx(Ss,{invoiceId:F,onClose:()=>{Y(null),b.invalidateQueries({queryKey:["invoices"]})}})})]})}export{ot as default};

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.