import{o as e}from"./rolldown-runtime-GyhWBpbT.js";import{F as t,I as n,L as r,O as i,P as a,s as o}from"./react-vendor-DoSylNpz.js";import{$ as s,Fn as c,Fr as l,Gr as u,Hr as d,In as f,Kr as p,Ln as m,Mn as h,Nr as g,Rn as _,X as ee,Xt as v,ii as y,ni as b,r as x,yr as S,zn as C,zr as te}from"./mui-vendor-1IGyn9nW.js";import"./state-vendor-bhJgZEQG.js";import{dt as ne,k as w,n as re,ot as T,r as E,w as D}from"./index-BD_w4_v3.js";import{t as O}from"./html2canvas-DZsOdvuV.js";import{t as ie}from"./jspdf.es.min-lBH2rfpm.js";var k=e(y(),1),A=e(O(),1),j=b(),M=()=>{let{id:e}=n(),y=t(),b=i(e=>e.auth.user);D(`Print Invoice`);let O=a(),[M]=r(),N=M.get(`download`)===`pdf`,P=M.get(`print`)===`1`,F=w(b,`download_invoice`),I=w(b,`print_invoice`);(0,k.useEffect)(()=>{!F&&!I&&y(`/unauthorized`,{replace:!0})},[F,I,y]);let L=(0,k.useMemo)(()=>O.state?.from||`/sales/invoice`,[O.state]),{success:R,error:z}=ne(),[B,V]=(0,k.useState)(null),[H,U]=(0,k.useState)(!1),W=(0,k.useRef)(null),{data:G,isLoading:K}=o({queryKey:[`invoice`,e],queryFn:()=>re.getById(e),enabled:!!e});(0,k.useEffect)(()=>{(async()=>{if(G?.company)try{V((await T.get(`/api/masters/companies/${G.company}/`)).data)}catch{}})()},[G?.company]);let q=e=>e?x(new Date(e),`dd-MM-yyyy`):`-`,J=e=>`₹${Number(e||0).toLocaleString(`en-IN`,{minimumFractionDigits:2,maximumFractionDigits:2})}`,ae=e=>e!=null&&e!==``?Number(e).toLocaleString(`en-IN`,{minimumFractionDigits:2,maximumFractionDigits:2}):`-`,oe=G?.created_by_name||(G?.created_by_type&&G?.created_by_identifier?`${G.created_by_type}: ${G.created_by_identifier}`:G?.created_by_identifier)||`-`,Y=B?.logo,X=e=>{if(!e)return``;let t=e.split(`,`).map(e=>e.trim()).filter(Boolean),n=[],r=``;return t.forEach(e=>{let t=r?`${r}, ${e}`:e;t.length>50&&r?(n.push(r),r=e):r=t}),r&&n.push(r),n.slice(0,2).join(`
`)};(0,k.useEffect)(()=>{let e=document.createElement(`style`);return e.setAttribute(`data-print-style`,`invoice-print`),e.innerHTML=`
      .print-container {
        max-width: 410mm;
        width: 100%;
        margin: 0 auto;
      }
      .print-container .MuiTableCell-root {
        padding: 6px 8px;
        font-size: 10px;
        line-height: 1.25;
        white-space: normal;
        word-break: break-word;
        vertical-align: top;
      }
      @media print {
        body { background: white; -webkit-print-color-adjust: exact; }
        body * { visibility: hidden !important; }
        .print-container, .print-container * { visibility: visible !important; }
        .print-container {
          position: absolute;
          left: 0;
          right: 0;
          margin: 0 auto !important;
          max-width: 410mm !important;
          box-shadow: none !important;
          border: 0 !important;
          padding: 8mm 6mm;
        }
        .no-print { display: none !important; }
        @page { size: A3 landscape; margin: 8mm; }
        .print-container table { border-collapse: collapse !important; }
        .print-container .MuiTableCell-root {
          padding: 4px 6px !important;
          font-size: 9px !important;
          line-height: 1.2 !important;
        }
      }
    `,document.head.appendChild(e),()=>{document.head.removeChild(e)}},[]);let Z=(0,k.useCallback)(()=>{y(L)},[L,y]),se=(0,k.useCallback)(()=>{window.print(),Z()},[Z]),Q=(0,k.useCallback)(async(e=!1)=>{if(!(!W.current||!G))try{U(!0);let t=await(0,A.default)(W.current,{scale:2,useCORS:!0,backgroundColor:`#ffffff`}),n=t.toDataURL(`image/png`),r=new ie(`l`,`mm`,`a3`),i=r.internal.pageSize.getWidth(),a=r.internal.pageSize.getHeight(),o=t.height*i/t.width,s=a-8-10,c=o,l=8;for(r.addImage(n,`PNG`,0,l,i,o),c-=s;c>0;)l=c-o+8,r.addPage(),r.addImage(n,`PNG`,0,l,i,o),c-=s;r.save(`invoice-${G.invoice_number||G.id||`print`}.pdf`),e||R(`PDF downloaded`)}catch{e||z(`Failed to download PDF`)}finally{U(!1),e&&Z()}},[Z,G,z,R]);if((0,k.useEffect)(()=>{if(N&&!F){z(`You do not have permission to download Invoice PDF`),Z();return}if(P&&!I){z(`You do not have permission to print Invoice`),Z();return}if(!N||!G||K||H)return;let e=setTimeout(()=>void Q(!0),400);return()=>clearTimeout(e)},[N,P,F,I,Z,Q,G,H,K,z]),(0,k.useEffect)(()=>{if(P&&!I||!P||!G||K||H)return;let e=setTimeout(()=>{window.print(),Z()},500);return()=>clearTimeout(e)},[P,I,Z,G,H,K]),K)return(0,j.jsx)(l,{sx:{display:`flex`,alignItems:`center`,justifyContent:`center`,height:`100vh`},children:(0,j.jsx)(u,{})});if(!G)return(0,j.jsx)(l,{sx:{display:`flex`,alignItems:`center`,justifyContent:`center`,height:`100vh`},children:(0,j.jsx)(d,{variant:`h6`,color:`error`,children:`Invoice not found`})});let $=(()=>{let e={DRAFT:{bg:`#fff3cd`,color:`#856404`,label:`Draft`},PENDING:{bg:`#fff3cd`,color:`#856404`,label:`Pending`},CONFIRMED:{bg:`#d1ecf1`,color:`#0c5460`,label:`Confirmed`},PAID:{bg:`#d4edda`,color:`#155724`,label:`Paid`},PARTIALLY_PAID:{bg:`#fff3cd`,color:`#856404`,label:`Partially Paid`},CANCELLED:{bg:`#f8d7da`,color:`#721c24`,label:`Cancelled`}};return e[G.status]||e.DRAFT})();return(0,j.jsxs)(l,{sx:{minHeight:`100vh`,bgcolor:`#f5f6f8`,py:3,px:{xs:2,md:4}},children:[(0,j.jsxs)(l,{className:`no-print`,sx:{display:N||P?`none`:`flex`,justifyContent:`flex-end`,gap:1.5,mb:2},children:[(0,j.jsx)(g,{variant:`text`,color:`inherit`,startIcon:(0,j.jsx)(v,{}),onClick:Z,disabled:H,children:`Cancel`}),F&&(0,j.jsx)(g,{variant:`outlined`,startIcon:(0,j.jsx)(s,{}),onClick:()=>void Q(!1),disabled:H,children:H?`Preparing PDF...`:`Download PDF`}),I&&(0,j.jsx)(g,{variant:`contained`,startIcon:(0,j.jsx)(ee,{}),onClick:se,disabled:H,children:`Print`})]}),(0,j.jsxs)(p,{ref:W,className:`print-container`,sx:{maxWidth:`410mm`,width:`100%`,mx:`auto`,p:{xs:2,md:2.5},pt:{xs:3,md:3.5},pb:{xs:4,md:5},backgroundColor:`white`,boxShadow:`0 12px 30px rgba(0,0,0,0.12)`,borderRadius:2,"@media print":{boxShadow:`none`,borderRadius:0,maxWidth:`100%`,width:`100%`,p:`8mm 6mm`,m:0}},children:[(0,j.jsxs)(l,{sx:{display:`flex`,alignItems:`flex-start`,gap:2,mb:2},children:[(0,j.jsx)(l,{sx:{height:72,width:72,borderRadius:`50%`,border:`1px solid #d6d8df`,display:`flex`,alignItems:`center`,justifyContent:`center`,overflow:`hidden`,bgcolor:`#f9fafb`,flexShrink:0},children:Y?(0,j.jsx)(E,{src:Y,alt:B?.name||`Company`,sx:{height:`100%`,width:`100%`,objectFit:`contain`}}):(0,j.jsx)(d,{variant:`subtitle2`,color:`text.secondary`,children:`Logo`})}),(0,j.jsxs)(l,{sx:{flex:1,textAlign:`center`},children:[(0,j.jsx)(d,{variant:`h6`,sx:{fontWeight:800,letterSpacing:.4,color:`#1f2d3d`},children:B?.name||`Company Name`}),(0,j.jsx)(d,{variant:`body2`,color:`text.secondary`,sx:{whiteSpace:`pre-line`,mt:.5},children:X(B?.address)}),(B?.phone||B?.email)&&(0,j.jsxs)(d,{variant:`body2`,color:`text.secondary`,sx:{mt:.5},children:[B?.phone,` `,B?.phone&&B?.email?` | `:``,` `,B?.email]}),B?.gst_number&&(0,j.jsxs)(d,{variant:`body2`,color:`text.secondary`,sx:{mt:.5},children:[`GST: `,B.gst_number]})]}),(0,j.jsx)(l,{sx:{minWidth:130,display:`flex`,justifyContent:`flex-end`},children:(0,j.jsx)(te,{label:$.label,sx:{backgroundColor:$.bg,color:$.color,fontWeight:700,fontSize:`0.95rem`,height:36,px:1.5,textTransform:`uppercase`,letterSpacing:.4}})})]}),(0,j.jsx)(S,{sx:{my:2}}),(0,j.jsx)(d,{variant:`subtitle1`,sx:{textAlign:`center`,fontWeight:800,letterSpacing:1,color:`#263343`,mb:2,textTransform:`uppercase`},children:`Invoice`}),(0,j.jsx)(f,{component:p,elevation:0,sx:{border:`1px solid #e6e8f0`,mb:3},children:(0,j.jsx)(C,{size:`small`,children:(0,j.jsxs)(_,{children:[(0,j.jsxs)(h,{children:[(0,j.jsx)(m,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Invoice Number`}),(0,j.jsx)(m,{sx:{border:`1px solid #e6e8f0`,fontWeight:600},children:G.invoice_number}),(0,j.jsx)(m,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Source Type`}),(0,j.jsx)(m,{sx:{border:`1px solid #e6e8f0`,fontWeight:600},children:G.source_type}),(0,j.jsx)(m,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Order Number`}),(0,j.jsx)(m,{sx:{border:`1px solid #e6e8f0`,fontWeight:600},children:G.order_number})]}),(0,j.jsxs)(h,{children:[(0,j.jsx)(m,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Invoice Date`}),(0,j.jsx)(m,{sx:{border:`1px solid #e6e8f0`},children:q(G.invoice_date)}),(0,j.jsx)(m,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Due Date`}),(0,j.jsx)(m,{sx:{border:`1px solid #e6e8f0`},children:q(G.due_date)}),(0,j.jsx)(m,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Customer`}),(0,j.jsx)(m,{sx:{border:`1px solid #e6e8f0`},children:G.customer_name})]})]})})}),(0,j.jsx)(f,{component:p,elevation:0,sx:{border:`1px solid #e6e8f0`,mb:3},children:(0,j.jsx)(C,{size:`small`,children:(0,j.jsx)(_,{children:(0,j.jsxs)(h,{children:[(0,j.jsx)(m,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,width:`15%`,border:`1px solid #e6e8f0`},children:`Billing Address`}),(0,j.jsxs)(m,{sx:{width:`35%`,border:`1px solid #e6e8f0`},children:[X(G.billing_address)||`-`,(0,j.jsxs)(d,{variant:`body2`,sx:{color:`#666`,mt:.5},children:[`State: `,G.billing_state]})]}),(0,j.jsx)(m,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,width:`15%`,border:`1px solid #e6e8f0`},children:`Shipping Address`}),(0,j.jsxs)(m,{sx:{width:`35%`,border:`1px solid #e6e8f0`},children:[X(G.shipping_address)||`-`,(0,j.jsxs)(d,{variant:`body2`,sx:{color:`#666`,mt:.5},children:[`State: `,G.shipping_state]})]})]})})})}),(0,j.jsx)(d,{variant:`subtitle2`,sx:{textAlign:`center`,fontWeight:800,letterSpacing:1,color:`#263343`,mb:1,textTransform:`uppercase`},children:`Invoice Items`}),(0,j.jsx)(f,{component:p,elevation:0,sx:{border:`1px solid #e6e8f0`,mb:3},children:(0,j.jsxs)(C,{size:`small`,stickyHeader:!0,children:[(0,j.jsx)(c,{children:(0,j.jsxs)(h,{sx:{backgroundColor:`#f5f7fb`},children:[(0,j.jsx)(m,{sx:{fontWeight:700,border:`1px solid #e6e8f0`},children:`S.No`}),(0,j.jsx)(m,{sx:{fontWeight:700,border:`1px solid #e6e8f0`},children:`Item Code`}),(0,j.jsx)(m,{sx:{fontWeight:700,border:`1px solid #e6e8f0`},children:`Item Name`}),(0,j.jsx)(m,{sx:{fontWeight:700,border:`1px solid #e6e8f0`},children:`HSN Code`}),(0,j.jsx)(m,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`right`},children:`Quantity`}),(0,j.jsx)(m,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`right`},children:`Rate`}),(0,j.jsx)(m,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`right`},children:`Discount`}),(0,j.jsx)(m,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`right`},children:`Taxable Amount`}),(0,j.jsx)(m,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`right`},children:`Tax Amount`}),(0,j.jsx)(m,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`right`},children:`Line Total`})]})}),(0,j.jsx)(_,{children:!G.items||G.items.length===0?(0,j.jsx)(h,{children:(0,j.jsx)(m,{colSpan:10,align:`center`,sx:{border:`1px solid #e6e8f0`},children:`No invoice items`})}):G.items.map((e,t)=>(0,j.jsxs)(h,{children:[(0,j.jsx)(m,{sx:{border:`1px solid #e6e8f0`},children:t+1}),(0,j.jsx)(m,{sx:{border:`1px solid #e6e8f0`},children:e.item_code}),(0,j.jsx)(m,{sx:{border:`1px solid #e6e8f0`},children:e.item_name}),(0,j.jsx)(m,{sx:{border:`1px solid #e6e8f0`},children:e.hsn_code}),(0,j.jsx)(m,{sx:{border:`1px solid #e6e8f0`,textAlign:`right`},children:ae(e.quantity)}),(0,j.jsx)(m,{sx:{border:`1px solid #e6e8f0`,textAlign:`right`},children:J(e.rate)}),(0,j.jsx)(m,{sx:{border:`1px solid #e6e8f0`,textAlign:`right`},children:J(e.discount_amount||0)}),(0,j.jsx)(m,{sx:{border:`1px solid #e6e8f0`,textAlign:`right`},children:J(e.taxable_amount||0)}),(0,j.jsx)(m,{sx:{border:`1px solid #e6e8f0`,textAlign:`right`},children:J(e.tax_amount||0)}),(0,j.jsx)(m,{sx:{border:`1px solid #e6e8f0`,textAlign:`right`,fontWeight:600},children:J(e.line_total||0)})]},e.id||t))})]})}),(0,j.jsx)(l,{sx:{display:`flex`,justifyContent:`flex-end`,mb:3},children:(0,j.jsx)(l,{sx:{minWidth:320},children:(0,j.jsx)(C,{size:`small`,children:(0,j.jsxs)(_,{children:[(0,j.jsxs)(h,{children:[(0,j.jsx)(m,{sx:{fontWeight:600,border:`1px solid #e0e0e0`},children:`Subtotal`}),(0,j.jsx)(m,{sx:{textAlign:`right`,border:`1px solid #e0e0e0`},children:J(G.subtotal||0)})]}),(0,j.jsxs)(h,{children:[(0,j.jsx)(m,{sx:{fontWeight:600,border:`1px solid #e0e0e0`},children:`Discount`}),(0,j.jsx)(m,{sx:{textAlign:`right`,border:`1px solid #e0e0e0`},children:J(G.discount_amount||0)})]}),(0,j.jsxs)(h,{children:[(0,j.jsx)(m,{sx:{fontWeight:600,border:`1px solid #e0e0e0`},children:`Taxable Amount`}),(0,j.jsx)(m,{sx:{textAlign:`right`,border:`1px solid #e0e0e0`},children:J(G.taxable_amount||0)})]}),(0,j.jsxs)(h,{children:[(0,j.jsx)(m,{sx:{fontWeight:600,border:`1px solid #e0e0e0`},children:`Tax Amount`}),(0,j.jsx)(m,{sx:{textAlign:`right`,border:`1px solid #e0e0e0`},children:J(G.tax_amount||0)})]}),(0,j.jsxs)(h,{children:[(0,j.jsx)(m,{sx:{fontWeight:700,border:`1px solid #e0e0e0`,backgroundColor:`#f5f5f5`},children:`Grand Total`}),(0,j.jsx)(m,{sx:{textAlign:`right`,fontWeight:700,border:`1px solid #e0e0e0`,backgroundColor:`#f5f5f5`,color:`#006766`},children:J(G.grand_total||0)})]}),(0,j.jsxs)(h,{children:[(0,j.jsx)(m,{sx:{fontWeight:600,border:`1px solid #e0e0e0`},children:`Paid Amount`}),(0,j.jsx)(m,{sx:{textAlign:`right`,border:`1px solid #e0e0e0`,color:`success.main`},children:J(G.paid_amount||0)})]}),(0,j.jsxs)(h,{children:[(0,j.jsx)(m,{sx:{fontWeight:700,border:`1px solid #e0e0e0`,backgroundColor:`#fff3cd`},children:`Balance Amount`}),(0,j.jsx)(m,{sx:{textAlign:`right`,fontWeight:700,border:`1px solid #e0e0e0`,backgroundColor:`#fff3cd`,color:`error.main`},children:J(G.balance_amount||0)})]})]})})})}),(0,j.jsx)(S,{sx:{mt:2,mb:1}}),(0,j.jsxs)(l,{sx:{display:`flex`,justifyContent:`space-between`,alignItems:`center`},children:[(0,j.jsxs)(l,{children:[(0,j.jsxs)(d,{variant:`body2`,sx:{fontWeight:600,color:`#1f2d3d`},children:[`Created By: `,oe]}),(0,j.jsx)(d,{variant:`body2`,color:`text.secondary`,children:G.created_on?x(new Date(G.created_on),`dd-MM-yyyy hh:mm a`):`-`})]}),(0,j.jsxs)(d,{variant:`body2`,color:`text.secondary`,children:[`Generated on: `,x(new Date,`dd-MM-yyyy, hh:mm a`)]})]})]})]})};export{M as default};