1import{dd as I,s as u}from"./index-B3NVYGCC.js";function k(m){const{invoice:a,client:t,company:n,lineItems:d,branding:i,taxSettings:e,payments:f}=m,p=d.map(o=>({date:o.service_date||void 0,name:o.product_service_name||o.description||"â",description:o.description||void 0,quantity:Number(o.quantity||1),unit_price:Number(o.unit_price||0),subtotal:Number(o.subtotal??Number(o.quantity||1)*Number(o.unit_price||0)),tax_rate:Number(o.tax_rate||0)})),c=Number((e==null?void 0:e.federal_tax_rate)||0),s=Number((e==null?void 0:e.provincial_tax_rate)||0),g=c+s,D=c>0&&s>0,l=new Map;d.forEach(o=>{const r=Number(o.tax_rate||0);if(r<=0)return;const w=Number(o.subtotal??Number(o.quantity||1)*Number(o.unit_price||0));if(D&&Math.abs(r-g)<.01){const v=w*c/100,y=w*s/100,_=(e==null?void 0:e.federal_tax_label)||I(c),$=(e==null?void 0:e.provincial_tax_label)||I(s),E=`${_}-${c}`,A=`${$}-${s}`,q=l.get(E);l.set(E,{label:_,rate:c,amount:((q==null?void 0:q.amount)||0)+v,number:e==null?void 0:e.federal_tax_number});const x=l.get(A);l.set(A,{label:$,rate:s,amount:((x==null?void 0:x.amount)||0)+y,number:e==null?void 0:e.provincial_tax_number})}else{const v=I(r),y=`${v}-${r}`,_=l.get(y);l.set(y,{label:v,rate:r,amount:((_==null?void 0:_.amount)||0)+w*r/100})}});const R=Array.from(l.values()).sort((o,r)=>o.rate-r.rate),h=p.reduce((o,r)=>o+r.subtotal,0),N=a.discount_type,b=Number(a.discount_value||0),L=N==="percentage"?h*b/100:N==="fixed"?b:0,B=Math.max(0,h-L),C=R.reduce((o,r)=>o+r.amount,0),M=B+C,O=[t.street||t.address,[t.city,t.province].filter(Boolean).join(", "),t.postal_code].filter(Boolean).join(", "),T=(t.first_name&&t.last_name?`${t.first_name} ${t.last_name}`:t.name)||"Client",j={invoice_number:a.invoice_number||"BROUILLON",title:a.title||void 0,issue_date:a.issue_date,due_date:a.due_date||new Date().toISOString(),status:a.status||"draft",subtotal:h,discount_type:N,discount_value:b>0?b:void 0,discount_description:a.discount_description||void 0,tax_lines:R,total_amount:M,payment_terms:a.payment_terms||void 0,notes_public:a.notes_public||void 0,lineItems:p,client:{name:T,email:t.email,phone:t.phone,address:O||void 0},branding:i?{primary_color:i.primary_color,secondary_color:i.secondary_color,portal_logo_url:i.portal_logo_url,invoice_logo_url:i.invoice_logo_url,invoice_header_color:i.invoice_header_color,show_company_name_under_logo:i.show_company_name_under_logo}:void 0,taxNumbers:e?{federal_label:e.federal_tax_label||void 0,federal_number:e.federal_tax_number||void 0,provincial_label:e.provincial_tax_label||void 0,provincial_number:e.provincial_tax_number||void 0}:void 0},F={id:n.id,name:n.name||"Entreprise",email:n.email,phone:n.phone,street:n.street,city:n.city,province:n.province,postal_code:n.postal_code,country:n.country,website:n.website,business_number:n.business_number,logo_url:n.logo_url,logo_invoice_url:n.logo_invoice_url||n.logo_url||void 0};return{data:j,company:F}}async function G(m,a){const{data:t,error:n}=await u.from("invoices").select("*").eq("id",m).single();if(n||!t)throw new Error("Facture introuvable");const d=a||t.company_id,i=t.client_id,[e,f,p,c,s,g]=await Promise.all([u.from("clients").select("*").eq("id",i).single(),u.from("companies").select("*").eq("id",d).single(),u.from("line_items").select("*").eq("invoice_id",m).order("item_order"),u.from("company_branding").select("*").eq("company_id",d).single(),u.from("company_tax_settings").select("*").eq("company_id",d).single(),u.from("payment_records").select("*").eq("invoice_id",m).order("payment_date")]);if(!e.data)throw new Error("Client introuvable");if(!f.data)throw new Error("Entreprise introuvable");return{invoice:t,client:e.data,company:f.data,lineItems:p.data||[],branding:c.data||null,taxSettings:s.data||null,payments:g.data||[]}}export{k as buildInvoicePDFData,G as fetchInvoiceContextFromDB};
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.