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 _,Ur as ee,X as te,Xt as v,ii as y,ni as b,r as x,yr as S,zn as C,zr as w}from"./mui-vendor-1IGyn9nW.js";import"./state-vendor-bhJgZEQG.js";import{dt as T,k as E,ot as D,r as O,w as k}from"./index-BD_w4_v3.js";import{t as A}from"./html2canvas-DZsOdvuV.js";import{t as ne}from"./receipt.api-BPWlSLvI.js";import{t as re}from"./jspdf.es.min-lBH2rfpm.js";var j=e(y(),1),ie=e(A(),1),M=b(),N=()=>{let{id:e}=n(),y=t(),b=a(),[A]=r(),N=i(e=>e.auth.user);k(`Print Receipt`);let P=A.get(`download`)===`pdf`,F=A.get(`print`)===`1`,I=E(N,`download_receipt`),L=E(N,`print_receipt`);(0,j.useEffect)(()=>{!I&&!L&&y(`/unauthorized`,{replace:!0})},[I,L,y]);let R=(0,j.useMemo)(()=>b.state?.from||`/receipts`,[b.state]),{success:z,error:B}=T(),[V,H]=(0,j.useState)(null),[U,W]=(0,j.useState)(!1),G=(0,j.useRef)(null),{data:K,isLoading:q,isError:ae}=o({queryKey:[`receipt`,e,`print`],queryFn:()=>ne.getById(e),enabled:!!e});(0,j.useEffect)(()=>{(async()=>{if(K?.company)try{H((await D.get(`/api/masters/companies/${K.company}/`)).data)}catch{}})()},[K?.company]);let J=e=>e?x(new Date(e),`dd-MM-yyyy`):`-`,Y=e=>`₹${Number(e||0).toLocaleString(`en-IN`,{minimumFractionDigits:2,maximumFractionDigits:2})}`,oe=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(`
`)},X=(e=>{let t={0:{label:`Draft`,bg:`#fff3cd`,color:`#664d03`},1:{label:`Pending`,bg:`#d1ecf1`,color:`#0c5460`},2:{label:`Approved`,bg:`#d1e7dd`,color:`#0f5132`},3:{label:`Rejected`,bg:`#f8d7da`,color:`#842029`}};return t[e??1]||t[1]})(K?.authorized_status);(0,j.useEffect)(()=>{let e=document.createElement(`style`);return e.setAttribute(`data-print-style`,`receipt-print`),e.innerHTML=`
      .print-container {
        max-width: 210mm;
        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 {
        html, body {
          background: white;
          -webkit-print-color-adjust: exact;
          print-color-adjust: exact;
          margin: 0 !important;
          padding: 0 !important;
          width: 100% !important;
          overflow: visible !important;
        }
        body * { visibility: hidden !important; }
        .print-container, .print-container * { visibility: visible !important; }
        .print-container {
          position: fixed;
          top: 0;
          left: 0;
          right: 0;
          margin: 0 !important;
          max-width: none !important;
          width: 100% !important;
          box-sizing: border-box !important;
          box-shadow: none !important;
          border: 0 !important;
          padding-top: 6mm !important;
          padding-bottom: 8mm !important;
          padding-left: 6mm !important;
          padding-right: 6mm !important;
        }
        .no-print { display: none !important; }
        @page { size: A4 portrait; margin: 6mm; }
        .print-container table { border-collapse: collapse !important; }
        .print-container .MuiTableCell-root {
          padding: 4px 6px !important;
          font-size: 9px !important;
          line-height: 1.2 !important;
          white-space: normal !important;
          word-break: break-word !important;
          vertical-align: top !important;
        }
      }
    `,document.head.appendChild(e),()=>{document.head.querySelector(`[data-print-style="receipt-print"]`)?.remove()}},[]);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(!(!G.current||!K))try{W(!0);let t=await(0,ie.default)(G.current,{scale:2,useCORS:!0,backgroundColor:`#ffffff`}),n=t.toDataURL(`image/png`),r=new re(`p`,`mm`,`a4`),i=r.internal.pageSize.getWidth(),a=r.internal.pageSize.getHeight(),o=t.height*i/t.width,s=a-6-8,c=o,l=6;for(r.addImage(n,`PNG`,0,l,i,o),c-=s;c>0;)l=c-o+6,r.addPage(),r.addImage(n,`PNG`,0,l,i,o),c-=s;r.save(`receipt-${K.receipt_number||K.id||`print`}.pdf`),e||z(`PDF downloaded`)}catch{e||B(`Failed to download PDF`)}finally{W(!1),e&&Z()}},[Z,K,B,z]);if((0,j.useEffect)(()=>{if(!(!K||q||U)){if(P&&!I){B(`You do not have permission to download Receipt PDF`),Z();return}if(F&&!L){B(`You do not have permission to print Receipt`),Z();return}if(P){let e=setTimeout(()=>void Q(!0),400);return()=>clearTimeout(e)}if(F){if(!L)return;let e=setTimeout(()=>{window.print(),Z()},500);return()=>clearTimeout(e)}}},[P,F,I,L,Z,Q,U,q,K,B]),q)return(0,M.jsx)(l,{sx:{display:`flex`,justifyContent:`center`,alignItems:`center`,height:`100vh`},children:(0,M.jsx)(u,{})});if(ae||!K)return(0,M.jsxs)(l,{sx:{p:4},children:[(0,M.jsx)(ee,{severity:`error`,sx:{mb:2},children:`Failed to load Receipt`}),(0,M.jsx)(g,{variant:`contained`,startIcon:(0,M.jsx)(v,{}),onClick:Z,children:`Back`})]});let $=V?.logo;return(0,M.jsxs)(l,{sx:{minHeight:`100vh`,bgcolor:`#f5f6f8`,py:3,px:{xs:2,md:4}},children:[(0,M.jsxs)(l,{className:`no-print`,sx:{display:P||F?`none`:`flex`,justifyContent:`flex-end`,gap:1.5,mb:2},children:[(0,M.jsx)(g,{variant:`text`,color:`inherit`,startIcon:(0,M.jsx)(v,{}),onClick:Z,disabled:U,children:`Cancel`}),I&&(0,M.jsx)(g,{variant:`outlined`,startIcon:(0,M.jsx)(s,{}),onClick:()=>void Q(!1),disabled:U,children:U?`Preparing PDF...`:`Download PDF`}),L&&(0,M.jsx)(g,{variant:`contained`,startIcon:(0,M.jsx)(te,{}),onClick:se,disabled:U,children:`Print`})]}),(0,M.jsxs)(p,{ref:G,className:`print-container`,sx:{maxWidth:`210mm`,width:`100%`,mx:`auto`,p:{xs:2.5,md:3},pt:{xs:3.5,md:4},pb:{xs:4.5,md:5.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:0,m:0}},children:[(0,M.jsxs)(l,{sx:{display:`flex`,alignItems:`flex-start`,gap:2,mb:2},children:[(0,M.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:$?(0,M.jsx)(O,{src:$,alt:V?.name||K.company_name||`Company`,sx:{height:`100%`,width:`100%`,objectFit:`contain`}}):(0,M.jsx)(d,{variant:`subtitle2`,color:`text.secondary`,children:`Logo`})}),(0,M.jsxs)(l,{sx:{flex:1,textAlign:`center`},children:[(0,M.jsx)(d,{variant:`h6`,sx:{fontWeight:800,letterSpacing:.4,color:`#1f2d3d`},children:V?.name||K.company_name||`Company`}),(0,M.jsx)(d,{variant:`body2`,color:`text.secondary`,sx:{whiteSpace:`pre-line`,mt:.5},children:oe(V?.address)}),(V?.phone||V?.email)&&(0,M.jsxs)(d,{variant:`body2`,color:`text.secondary`,sx:{mt:.5},children:[V?.phone,` `,V?.phone&&V?.email?` | `:``,` `,V?.email]}),V?.gst_number&&(0,M.jsxs)(d,{variant:`body2`,color:`text.secondary`,sx:{mt:.5},children:[`GST: `,V.gst_number]})]}),(0,M.jsx)(l,{sx:{minWidth:130,display:`flex`,justifyContent:`flex-end`},children:(0,M.jsx)(w,{label:X.label,sx:{backgroundColor:X.bg,color:X.color,fontWeight:700,fontSize:`0.95rem`,height:36,px:1.5,textTransform:`uppercase`,letterSpacing:.4}})})]}),(0,M.jsx)(S,{sx:{my:2}}),(0,M.jsx)(d,{variant:`subtitle1`,sx:{textAlign:`center`,fontWeight:800,letterSpacing:1,color:`#263343`,mb:2,textTransform:`uppercase`},children:`Receipt`}),(0,M.jsxs)(l,{sx:{mb:3},children:[(0,M.jsx)(d,{variant:`subtitle2`,sx:{fontWeight:700,color:`#1f2d3d`,mb:1.2},children:`Receipt Details`}),(0,M.jsx)(f,{component:p,elevation:0,sx:{border:`1px solid #e2e8f0`,borderRadius:1.5},children:(0,M.jsx)(C,{size:`small`,children:(0,M.jsxs)(_,{children:[(0,M.jsxs)(h,{children:[(0,M.jsx)(m,{sx:{fontWeight:700,bgcolor:`#f8fafc`,width:`20%`,border:`1px solid #e2e8f0`},children:`Receipt Number`}),(0,M.jsx)(m,{sx:{width:`30%`,border:`1px solid #e2e8f0`},children:K.receipt_number||`-`}),(0,M.jsx)(m,{sx:{fontWeight:700,bgcolor:`#f8fafc`,width:`20%`,border:`1px solid #e2e8f0`},children:`Receipt Date`}),(0,M.jsx)(m,{sx:{width:`30%`,border:`1px solid #e2e8f0`},children:J(K.receipt_date)})]}),(0,M.jsxs)(h,{children:[(0,M.jsx)(m,{sx:{fontWeight:700,bgcolor:`#f8fafc`,border:`1px solid #e2e8f0`},children:`Payment Date`}),(0,M.jsx)(m,{sx:{border:`1px solid #e2e8f0`},children:J(K.payment_date)}),(0,M.jsx)(m,{sx:{fontWeight:700,bgcolor:`#f8fafc`,border:`1px solid #e2e8f0`},children:`Payment Mode`}),(0,M.jsx)(m,{sx:{border:`1px solid #e2e8f0`},children:K.payment_mode_display||K.payment_mode})]}),(K.reference_number||K.bank_name)&&(0,M.jsxs)(h,{children:[(0,M.jsx)(m,{sx:{fontWeight:700,bgcolor:`#f8fafc`,border:`1px solid #e2e8f0`},children:`Reference Number`}),(0,M.jsx)(m,{sx:{border:`1px solid #e2e8f0`},children:K.reference_number||`-`}),(0,M.jsx)(m,{sx:{fontWeight:700,bgcolor:`#f8fafc`,border:`1px solid #e2e8f0`},children:`Bank Name`}),(0,M.jsx)(m,{sx:{border:`1px solid #e2e8f0`},children:K.bank_name||`-`})]}),(0,M.jsxs)(h,{children:[(0,M.jsx)(m,{sx:{fontWeight:700,bgcolor:`#f8fafc`,border:`1px solid #e2e8f0`},children:`Customer Type`}),(0,M.jsx)(m,{sx:{border:`1px solid #e2e8f0`},children:K.customer_type}),(0,M.jsx)(m,{sx:{fontWeight:700,bgcolor:`#f8fafc`,border:`1px solid #e2e8f0`},children:`Customer Name`}),(0,M.jsx)(m,{sx:{border:`1px solid #e2e8f0`},children:K.customer_name||`-`})]}),(0,M.jsxs)(h,{children:[(0,M.jsx)(m,{sx:{fontWeight:700,bgcolor:`#f8fafc`,border:`1px solid #e2e8f0`},children:`Location`}),(0,M.jsx)(m,{sx:{border:`1px solid #e2e8f0`},children:K.location_name||`-`}),(0,M.jsx)(m,{sx:{fontWeight:700,bgcolor:`#f8fafc`,border:`1px solid #e2e8f0`},children:`Company`}),(0,M.jsx)(m,{sx:{border:`1px solid #e2e8f0`},children:K.company_name||`-`})]})]})})})]}),(0,M.jsxs)(l,{sx:{mb:3},children:[(0,M.jsx)(d,{variant:`subtitle2`,sx:{fontWeight:700,color:`#1f2d3d`,mb:1.2},children:`Invoice Allocations`}),(0,M.jsx)(f,{component:p,elevation:0,sx:{border:`1px solid #e2e8f0`,borderRadius:1.5},children:(0,M.jsxs)(C,{size:`small`,children:[(0,M.jsx)(c,{children:(0,M.jsxs)(h,{sx:{backgroundColor:`#006766`},children:[(0,M.jsx)(m,{sx:{color:`white`,fontWeight:700,border:`1px solid #e2e8f0`},children:`S.No`}),(0,M.jsx)(m,{sx:{color:`white`,fontWeight:700,border:`1px solid #e2e8f0`},children:`Invoice Number`}),(0,M.jsx)(m,{sx:{color:`white`,fontWeight:700,border:`1px solid #e2e8f0`},children:`Invoice Date`}),(0,M.jsx)(m,{sx:{color:`white`,fontWeight:700,border:`1px solid #e2e8f0`,textAlign:`right`},children:`Invoice Amount`}),(0,M.jsx)(m,{sx:{color:`white`,fontWeight:700,border:`1px solid #e2e8f0`,textAlign:`right`},children:`Invoice Balance`}),(0,M.jsx)(m,{sx:{color:`white`,fontWeight:700,border:`1px solid #e2e8f0`,textAlign:`right`},children:`Allocated Amount`})]})}),(0,M.jsx)(_,{children:K.allocations?.map((e,t)=>(0,M.jsxs)(h,{children:[(0,M.jsx)(m,{sx:{border:`1px solid #e2e8f0`},children:t+1}),(0,M.jsx)(m,{sx:{border:`1px solid #e2e8f0`},children:e.invoice_number||`-`}),(0,M.jsx)(m,{sx:{border:`1px solid #e2e8f0`},children:e.invoice_date?J(e.invoice_date):`-`}),(0,M.jsx)(m,{sx:{border:`1px solid #e2e8f0`,textAlign:`right`},children:Y(e.invoice_amount)}),(0,M.jsx)(m,{sx:{border:`1px solid #e2e8f0`,textAlign:`right`},children:Y(e.invoice_balance)}),(0,M.jsx)(m,{sx:{border:`1px solid #e2e8f0`,textAlign:`right`,fontWeight:700},children:Y(e.allocated_amount)})]},e.id||t))})]})})]}),(0,M.jsx)(l,{sx:{display:`flex`,justifyContent:`flex-end`,mb:3},children:(0,M.jsx)(l,{sx:{minWidth:320},children:(0,M.jsx)(C,{size:`small`,children:(0,M.jsxs)(_,{children:[(0,M.jsxs)(h,{children:[(0,M.jsx)(m,{sx:{fontWeight:700,border:`1px solid #e2e8f0`},children:`Total Amount`}),(0,M.jsx)(m,{sx:{textAlign:`right`,border:`1px solid #e2e8f0`},children:Y(K.total_amount)})]}),(0,M.jsxs)(h,{children:[(0,M.jsx)(m,{sx:{fontWeight:700,border:`1px solid #e2e8f0`},children:`Allocated Amount`}),(0,M.jsx)(m,{sx:{textAlign:`right`,border:`1px solid #e2e8f0`,color:`success.main`},children:Y(K.allocated_amount||0)})]}),(0,M.jsxs)(h,{children:[(0,M.jsx)(m,{sx:{fontWeight:700,border:`1px solid #e2e8f0`,backgroundColor:`#fff3cd`},children:`Adjustment Amount`}),(0,M.jsx)(m,{sx:{textAlign:`right`,fontWeight:700,border:`1px solid #e2e8f0`,backgroundColor:`#fff3cd`},children:Y(K.adjustment_amount||0)})]})]})})})}),K.remarks&&(0,M.jsxs)(l,{sx:{mb:3},children:[(0,M.jsx)(d,{variant:`subtitle2`,sx:{fontWeight:700,color:`#1f2d3d`,mb:1.2},children:`Remarks`}),(0,M.jsx)(p,{sx:{p:1.5,backgroundColor:`#f8fafc`,border:`1px solid #e2e8f0`,borderRadius:1.5},children:(0,M.jsx)(d,{variant:`body2`,children:K.remarks})})]}),(0,M.jsxs)(l,{sx:{mt:2,pt:1.5,borderTop:`1px solid #d6d8df`,display:`flex`,justifyContent:`space-between`,alignItems:`center`},children:[(0,M.jsxs)(d,{variant:`caption`,sx:{color:`#5f6b7a`},children:[`Generated on `,x(new Date,`dd-MM-yyyy, hh:mm a`)]}),(0,M.jsxs)(d,{variant:`caption`,sx:{color:`#5f6b7a`},children:[`Receipt: `,K.receipt_number||`-`]})]})]})]})};export{N as default};