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