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-BD_NkAgt.js";import{Bn as s,Fr as c,Jr as l,Ln as ee,Lr as u,Pn as d,Rn as f,Vn as p,Vr as m,Wr as h,Z as g,Zt as _,et as v,ii as y,oi as b,qr as x,r as S,xr as C,zn as w}from"./mui-vendor-DsoH2_ot.js";import"./state-vendor-Cw2RdylD.js";import{ft as T,k as E,n as te,r as ne,st as D,w as O}from"./index-Bo9BrBsx.js";import{t as k}from"./html2canvas-DZsOdvuV.js";import{t as A}from"./jspdf.es.min-BJ2LNVDK.js";var j=e(b(),1),re=e(k(),1),M=y(),N=()=>{let{id:e}=n(),y=t(),b=i(e=>e.auth.user);O(`Print Invoice`);let k=a(),[N]=r(),P=N.get(`download`)===`pdf`,F=N.get(`print`)===`1`,I=E(b,`download_invoice`),L=E(b,`print_invoice`);(0,j.useEffect)(()=>{!I&&!L&&y(`/unauthorized`,{replace:!0})},[I,L,y]);let R=(0,j.useMemo)(()=>k.state?.from||`/sales/invoice`,[k.state]),{success:z,error:B}=T(),[V,ie]=(0,j.useState)(null),[H,U]=(0,j.useState)(!1),W=(0,j.useRef)(null),{data:G,isLoading:K}=o({queryKey:[`invoice`,e],queryFn:()=>te.getById(e),enabled:!!e});(0,j.useEffect)(()=>{(async()=>{if(G?.company)try{ie((await D.get(`/api/masters/companies/${G.company}/`)).data)}catch{}})()},[G?.company]);let q=e=>e?S(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=V?.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,j.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,j.useCallback)(()=>{y(R)},[R,y]),se=(0,j.useCallback)(()=>{window.print(),Z()},[Z]),Q=(0,j.useCallback)(async(e=!1)=>{if(!(!W.current||!G))try{U(!0);let t=await(0,re.default)(W.current,{scale:2,useCORS:!0,backgroundColor:`#ffffff`}),n=t.toDataURL(`image/png`),r=new A(`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||z(`PDF downloaded`)}catch{e||B(`Failed to download PDF`)}finally{U(!1),e&&Z()}},[Z,G,B,z]);if((0,j.useEffect)(()=>{if(P&&!I){B(`You do not have permission to download Invoice PDF`),Z();return}if(F&&!L){B(`You do not have permission to print Invoice`),Z();return}if(!P||!G||K||H)return;let e=setTimeout(()=>void Q(!0),400);return()=>clearTimeout(e)},[P,F,I,L,Z,Q,G,H,K,B]),(0,j.useEffect)(()=>{if(F&&!L||!F||!G||K||H)return;let e=setTimeout(()=>{window.print(),Z()},500);return()=>clearTimeout(e)},[F,L,Z,G,H,K]),K)return(0,M.jsx)(u,{sx:{display:`flex`,alignItems:`center`,justifyContent:`center`,height:`100vh`},children:(0,M.jsx)(x,{})});if(!G)return(0,M.jsx)(u,{sx:{display:`flex`,alignItems:`center`,justifyContent:`center`,height:`100vh`},children:(0,M.jsx)(h,{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,M.jsxs)(u,{sx:{minHeight:`100vh`,bgcolor:`#f5f6f8`,py:3,px:{xs:2,md:4}},children:[(0,M.jsxs)(u,{className:`no-print`,sx:{display:P||F?`none`:`flex`,justifyContent:`flex-end`,gap:1.5,mb:2},children:[(0,M.jsx)(c,{variant:`text`,color:`inherit`,startIcon:(0,M.jsx)(_,{}),onClick:Z,disabled:H,children:`Cancel`}),I&&(0,M.jsx)(c,{variant:`outlined`,startIcon:(0,M.jsx)(v,{}),onClick:()=>void Q(!1),disabled:H,children:H?`Preparing PDF...`:`Download PDF`}),L&&(0,M.jsx)(c,{variant:`contained`,startIcon:(0,M.jsx)(g,{}),onClick:se,disabled:H,children:`Print`})]}),(0,M.jsxs)(l,{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,M.jsxs)(u,{sx:{display:`flex`,alignItems:`flex-start`,gap:2,mb:2},children:[(0,M.jsx)(u,{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,M.jsx)(ne,{src:Y,alt:V?.name||`Company`,sx:{height:`100%`,width:`100%`,objectFit:`contain`}}):(0,M.jsx)(h,{variant:`subtitle2`,color:`text.secondary`,children:`Logo`})}),(0,M.jsxs)(u,{sx:{flex:1,textAlign:`center`},children:[(0,M.jsx)(h,{variant:`h6`,sx:{fontWeight:800,letterSpacing:.4,color:`#1f2d3d`},children:V?.name||`Company Name`}),(0,M.jsx)(h,{variant:`body2`,color:`text.secondary`,sx:{whiteSpace:`pre-line`,mt:.5},children:X(V?.address)}),(V?.phone||V?.email)&&(0,M.jsxs)(h,{variant:`body2`,color:`text.secondary`,sx:{mt:.5},children:[V?.phone,` `,V?.phone&&V?.email?` | `:``,` `,V?.email]}),V?.gst_number&&(0,M.jsxs)(h,{variant:`body2`,color:`text.secondary`,sx:{mt:.5},children:[`GST: `,V.gst_number]})]}),(0,M.jsx)(u,{sx:{minWidth:130,display:`flex`,justifyContent:`flex-end`},children:(0,M.jsx)(m,{label:$.label,sx:{backgroundColor:$.bg,color:$.color,fontWeight:700,fontSize:`0.95rem`,height:36,px:1.5,textTransform:`uppercase`,letterSpacing:.4}})})]}),(0,M.jsx)(C,{sx:{my:2}}),(0,M.jsx)(h,{variant:`subtitle1`,sx:{textAlign:`center`,fontWeight:800,letterSpacing:1,color:`#263343`,mb:2,textTransform:`uppercase`},children:`Invoice`}),(0,M.jsx)(f,{component:l,elevation:0,sx:{border:`1px solid #e6e8f0`,mb:3},children:(0,M.jsx)(p,{size:`small`,children:(0,M.jsxs)(s,{children:[(0,M.jsxs)(d,{children:[(0,M.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Invoice Number`}),(0,M.jsx)(w,{sx:{border:`1px solid #e6e8f0`,fontWeight:600},children:G.invoice_number}),(0,M.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Source Type`}),(0,M.jsx)(w,{sx:{border:`1px solid #e6e8f0`,fontWeight:600},children:G.source_type}),(0,M.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Order Number`}),(0,M.jsx)(w,{sx:{border:`1px solid #e6e8f0`,fontWeight:600},children:G.order_number})]}),(0,M.jsxs)(d,{children:[(0,M.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Invoice Date`}),(0,M.jsx)(w,{sx:{border:`1px solid #e6e8f0`},children:q(G.invoice_date)}),(0,M.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Due Date`}),(0,M.jsx)(w,{sx:{border:`1px solid #e6e8f0`},children:q(G.due_date)}),(0,M.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Customer`}),(0,M.jsx)(w,{sx:{border:`1px solid #e6e8f0`},children:G.customer_name})]})]})})}),(0,M.jsx)(f,{component:l,elevation:0,sx:{border:`1px solid #e6e8f0`,mb:3},children:(0,M.jsx)(p,{size:`small`,children:(0,M.jsx)(s,{children:(0,M.jsxs)(d,{children:[(0,M.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,width:`15%`,border:`1px solid #e6e8f0`},children:`Billing Address`}),(0,M.jsxs)(w,{sx:{width:`35%`,border:`1px solid #e6e8f0`},children:[X(G.billing_address)||`-`,(0,M.jsxs)(h,{variant:`body2`,sx:{color:`#666`,mt:.5},children:[`State: `,G.billing_state]})]}),(0,M.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,width:`15%`,border:`1px solid #e6e8f0`},children:`Shipping Address`}),(0,M.jsxs)(w,{sx:{width:`35%`,border:`1px solid #e6e8f0`},children:[X(G.shipping_address)||`-`,(0,M.jsxs)(h,{variant:`body2`,sx:{color:`#666`,mt:.5},children:[`State: `,G.shipping_state]})]})]})})})}),(0,M.jsx)(h,{variant:`subtitle2`,sx:{textAlign:`center`,fontWeight:800,letterSpacing:1,color:`#263343`,mb:1,textTransform:`uppercase`},children:`Invoice Items`}),(0,M.jsx)(f,{component:l,elevation:0,sx:{border:`1px solid #e6e8f0`,mb:3},children:(0,M.jsxs)(p,{size:`small`,stickyHeader:!0,children:[(0,M.jsx)(ee,{children:(0,M.jsxs)(d,{sx:{backgroundColor:`#f5f7fb`},children:[(0,M.jsx)(w,{sx:{fontWeight:700,border:`1px solid #e6e8f0`},children:`S.No`}),(0,M.jsx)(w,{sx:{fontWeight:700,border:`1px solid #e6e8f0`},children:`Item Code`}),(0,M.jsx)(w,{sx:{fontWeight:700,border:`1px solid #e6e8f0`},children:`Item Name`}),(0,M.jsx)(w,{sx:{fontWeight:700,border:`1px solid #e6e8f0`},children:`HSN Code`}),(0,M.jsx)(w,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`right`},children:`Quantity`}),(0,M.jsx)(w,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`right`},children:`Rate`}),(0,M.jsx)(w,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`right`},children:`Discount`}),(0,M.jsx)(w,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`right`},children:`Taxable Amount`}),(0,M.jsx)(w,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`right`},children:`Tax Amount`}),(0,M.jsx)(w,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`right`},children:`Line Total`})]})}),(0,M.jsx)(s,{children:!G.items||G.items.length===0?(0,M.jsx)(d,{children:(0,M.jsx)(w,{colSpan:10,align:`center`,sx:{border:`1px solid #e6e8f0`},children:`No invoice items`})}):G.items.map((e,t)=>(0,M.jsxs)(d,{children:[(0,M.jsx)(w,{sx:{border:`1px solid #e6e8f0`},children:t+1}),(0,M.jsx)(w,{sx:{border:`1px solid #e6e8f0`},children:e.item_code}),(0,M.jsx)(w,{sx:{border:`1px solid #e6e8f0`},children:e.item_name}),(0,M.jsx)(w,{sx:{border:`1px solid #e6e8f0`},children:e.hsn_code}),(0,M.jsx)(w,{sx:{border:`1px solid #e6e8f0`,textAlign:`right`},children:ae(e.quantity)}),(0,M.jsx)(w,{sx:{border:`1px solid #e6e8f0`,textAlign:`right`},children:J(e.rate)}),(0,M.jsx)(w,{sx:{border:`1px solid #e6e8f0`,textAlign:`right`},children:J(e.discount_amount||0)}),(0,M.jsx)(w,{sx:{border:`1px solid #e6e8f0`,textAlign:`right`},children:J(e.taxable_amount||0)}),(0,M.jsx)(w,{sx:{border:`1px solid #e6e8f0`,textAlign:`right`},children:J(e.tax_amount||0)}),(0,M.jsx)(w,{sx:{border:`1px solid #e6e8f0`,textAlign:`right`,fontWeight:600},children:J(e.line_total||0)})]},e.id||t))})]})}),(0,M.jsx)(u,{sx:{display:`flex`,justifyContent:`flex-end`,mb:3},children:(0,M.jsx)(u,{sx:{minWidth:320},children:(0,M.jsx)(p,{size:`small`,children:(0,M.jsxs)(s,{children:[(0,M.jsxs)(d,{children:[(0,M.jsx)(w,{sx:{fontWeight:600,border:`1px solid #e0e0e0`},children:`Subtotal`}),(0,M.jsx)(w,{sx:{textAlign:`right`,border:`1px solid #e0e0e0`},children:J(G.subtotal||0)})]}),(0,M.jsxs)(d,{children:[(0,M.jsx)(w,{sx:{fontWeight:600,border:`1px solid #e0e0e0`},children:`Discount`}),(0,M.jsx)(w,{sx:{textAlign:`right`,border:`1px solid #e0e0e0`},children:J(G.discount_amount||0)})]}),(0,M.jsxs)(d,{children:[(0,M.jsx)(w,{sx:{fontWeight:600,border:`1px solid #e0e0e0`},children:`Taxable Amount`}),(0,M.jsx)(w,{sx:{textAlign:`right`,border:`1px solid #e0e0e0`},children:J(G.taxable_amount||0)})]}),(0,M.jsxs)(d,{children:[(0,M.jsx)(w,{sx:{fontWeight:600,border:`1px solid #e0e0e0`},children:`Tax Amount`}),(0,M.jsx)(w,{sx:{textAlign:`right`,border:`1px solid #e0e0e0`},children:J(G.tax_amount||0)})]}),(0,M.jsxs)(d,{children:[(0,M.jsx)(w,{sx:{fontWeight:700,border:`1px solid #e0e0e0`,backgroundColor:`#f5f5f5`},children:`Grand Total`}),(0,M.jsx)(w,{sx:{textAlign:`right`,fontWeight:700,border:`1px solid #e0e0e0`,backgroundColor:`#f5f5f5`,color:`#006766`},children:J(G.grand_total||0)})]}),(0,M.jsxs)(d,{children:[(0,M.jsx)(w,{sx:{fontWeight:600,border:`1px solid #e0e0e0`},children:`Paid Amount`}),(0,M.jsx)(w,{sx:{textAlign:`right`,border:`1px solid #e0e0e0`,color:`success.main`},children:J(G.paid_amount||0)})]}),(0,M.jsxs)(d,{children:[(0,M.jsx)(w,{sx:{fontWeight:700,border:`1px solid #e0e0e0`,backgroundColor:`#fff3cd`},children:`Balance Amount`}),(0,M.jsx)(w,{sx:{textAlign:`right`,fontWeight:700,border:`1px solid #e0e0e0`,backgroundColor:`#fff3cd`,color:`error.main`},children:J(G.balance_amount||0)})]})]})})})}),(0,M.jsx)(C,{sx:{mt:2,mb:1}}),(0,M.jsxs)(u,{sx:{display:`flex`,justifyContent:`space-between`,alignItems:`center`},children:[(0,M.jsxs)(u,{children:[(0,M.jsxs)(h,{variant:`body2`,sx:{fontWeight:600,color:`#1f2d3d`},children:[`Created By: `,oe]}),(0,M.jsx)(h,{variant:`body2`,color:`text.secondary`,children:G.created_on?S(new Date(G.created_on),`dd-MM-yyyy hh:mm a`):`-`})]}),(0,M.jsxs)(h,{variant:`body2`,color:`text.secondary`,children:[`Generated on: `,S(new Date,`dd-MM-yyyy, hh:mm a`)]})]})]})]})};export{N as default};