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 u,Lr as d,Pn as f,Rn as p,Vn as m,Vr as h,Wr as g,Z as _,Zt as v,et as y,h as b,ii as x,oi as S,qr as C,xr as w,zn as T}from"./mui-vendor-DsoH2_ot.js";import"./state-vendor-Cw2RdylD.js";import{ft as ee,k as E,r as te,st as D,w as ne}from"./index-Bo9BrBsx.js";import{t as O}from"./html2canvas-DZsOdvuV.js";import{t as re}from"./dispatch.api-BmxiRZ-P.js";import{t as k}from"./jspdf.es.min-BJ2LNVDK.js";var A=e(S(),1),ie=e(O(),1),j=e(b(),1),M=x(),N=()=>{let{id:e}=n(),b=t(),x=i(e=>e.auth.user);ne(`Print Dispatch Plan`);let S=a(),[O]=r(),N=O.get(`download`)===`pdf`,P=O.get(`print`)===`1`,F=E(x,`download_dispatchplan`),I=E(x,`print_dispatchplan`);(0,A.useEffect)(()=>{!F&&!I&&b(`/unauthorized`,{replace:!0})},[F,I,b]);let L=(0,A.useMemo)(()=>S.state?.from||`/sales/dispatch`,[S.state]),{success:R,error:z}=ee(),[B,V]=(0,A.useState)(null),[H,U]=(0,A.useState)(!1),W=(0,A.useRef)(null),{data:G,isLoading:K}=o({queryKey:[`dispatchPlan`,e],queryFn:()=>re.getById(e),enabled:!!e});(0,A.useEffect)(()=>{(async()=>{if(G?.company)try{V((await D.get(`/api/masters/companies/${G.company}/`)).data)}catch{}})()},[G?.company]);let q=e=>e?(0,j.default)(e).format(`DD-MM-YYYY`):`-`,J=e=>e?e.replace(/_/g,` `):`-`,ae=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=>e!=null&&e!==``?Number(e).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(`
`)},se=e=>{let t=e?.remaining_quantity,n=t==null?null:Number(t);if(n!==null&&!Number.isNaN(n)&&n>=0)return X(n);let r=Number(e?.quantity_ordered),i=Number(e?.quantity_dispatched);return!Number.isNaN(r)&&!Number.isNaN(i)?X(Math.max(0,r-i)):`-`};(0,A.useEffect)(()=>{let e=document.createElement(`style`);return e.setAttribute(`data-print-style`,`dispatch-plan-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; /* A3 width for landscape */
          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: A3 landscape; margin: 8mm; }
        .print-container table { border-collapse: collapse !important; }
        .print-container .MuiTableCell-root {
          padding: 4px 6px !important;
          font-size: 8.5px !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.removeChild(e)}},[]);let Z=(0,A.useCallback)(()=>{b(L)},[L,b]),ce=(0,A.useCallback)(()=>{window.print(),Z()},[Z]),Q=(0,A.useCallback)(async(e=!1)=>{if(!(!W.current||!G))try{U(!0);let t=await(0,ie.default)(W.current,{scale:2,useCORS:!0,backgroundColor:`#ffffff`}),n=t.toDataURL(`image/png`),r=new k(`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(`dispatch-plan-${G.dispatch_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,A.useEffect)(()=>{if(N&&!F){z(`You do not have permission to download Dispatch Plan PDF`),Z();return}if(P&&!I){z(`You do not have permission to print Dispatch Plan`),Z();return}if(!N||!G||K||H)return;let e=setTimeout(()=>void Q(!0),400);return()=>clearTimeout(e)},[N,P,F,I,Z,Q,H,K,G,z]),(0,A.useEffect)(()=>{if(P&&!I||!P||!G||K||H)return;let e=setTimeout(()=>{window.print(),Z()},500);return()=>clearTimeout(e)},[P,I,Z,H,K,G]),K)return(0,M.jsx)(d,{sx:{display:`flex`,alignItems:`center`,justifyContent:`center`,height:`100vh`},children:(0,M.jsx)(C,{})});if(!G)return(0,M.jsx)(d,{sx:{display:`flex`,alignItems:`center`,justifyContent:`center`,height:`100vh`},children:(0,M.jsx)(g,{variant:`h6`,color:`error`,children:`Dispatch plan not found`})});let $=G.items||[];return(0,M.jsxs)(d,{sx:{minHeight:`100vh`,bgcolor:`#f5f6f8`,py:3,px:{xs:2,md:4}},children:[(0,M.jsxs)(d,{className:`no-print`,sx:{display:N||P?`none`:`flex`,justifyContent:`flex-end`,gap:1.5,mb:2},children:[(0,M.jsx)(c,{variant:`text`,color:`inherit`,startIcon:(0,M.jsx)(v,{}),onClick:Z,disabled:H,children:`Cancel`}),F&&(0,M.jsx)(c,{variant:`outlined`,startIcon:(0,M.jsx)(y,{}),onClick:()=>void Q(!1),disabled:H,children:H?`Preparing PDF...`:`Download PDF`}),I&&(0,M.jsx)(c,{variant:`contained`,startIcon:(0,M.jsx)(_,{}),onClick:ce,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)(d,{sx:{display:`flex`,alignItems:`flex-start`,gap:2,mb:2},children:[(0,M.jsx)(d,{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)(te,{src:Y,alt:B?.name||`Company`,sx:{height:`100%`,width:`100%`,objectFit:`contain`}}):(0,M.jsx)(g,{variant:`subtitle2`,color:`text.secondary`,children:`Logo`})}),(0,M.jsxs)(d,{sx:{flex:1,textAlign:`center`},children:[(0,M.jsx)(g,{variant:`h6`,sx:{fontWeight:800,letterSpacing:.4,color:`#1f2d3d`},children:B?.name||`Company Name`}),(0,M.jsx)(g,{variant:`body2`,color:`text.secondary`,sx:{whiteSpace:`pre-line`,mt:.5},children:oe(B?.address)}),(B?.phone||B?.email)&&(0,M.jsxs)(g,{variant:`body2`,color:`text.secondary`,sx:{mt:.5},children:[B?.phone,` `,B?.phone&&B?.email?` | `:``,` `,B?.email]}),B?.gst_number&&(0,M.jsxs)(g,{variant:`body2`,color:`text.secondary`,sx:{mt:.5},children:[`GST: `,B.gst_number]})]}),(0,M.jsx)(d,{sx:{minWidth:130,display:`flex`,justifyContent:`flex-end`},children:(0,M.jsx)(h,{label:J(G.status),sx:{backgroundColor:`#e6f4ea`,color:`#0f5132`,fontWeight:700,fontSize:`0.95rem`,height:36,px:1.5,textTransform:`uppercase`,letterSpacing:.4}})})]}),(0,M.jsx)(w,{sx:{my:2}}),(0,M.jsx)(g,{variant:`subtitle1`,sx:{textAlign:`center`,fontWeight:800,letterSpacing:1,color:`#263343`,mb:2,textTransform:`uppercase`},children:`Dispatch Plan Summary`}),(0,M.jsx)(p,{component:l,elevation:0,sx:{border:`1px solid #e6e8f0`,mb:3},children:(0,M.jsx)(m,{size:`small`,children:(0,M.jsxs)(s,{children:[(0,M.jsxs)(f,{children:[(0,M.jsx)(T,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Dispatch Number`}),(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`,fontWeight:600},children:G.dispatch_number}),(0,M.jsx)(T,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Dispatch Date`}),(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`,fontWeight:600},children:q(G.dispatch_date)})]}),(0,M.jsxs)(f,{children:[(0,M.jsx)(T,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Planned Dispatch Date`}),(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`},children:q(G?.planned_dispatch_date)}),(0,M.jsx)(T,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Company`}),(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`},children:G.company_name||`-`})]}),(0,M.jsxs)(f,{children:[(0,M.jsx)(T,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Location`}),(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`},children:G.location_name||G?.source_location_name||`-`}),(0,M.jsx)(T,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Route`}),(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`},children:G?.route_name||`-`})]}),(0,M.jsxs)(f,{children:[(0,M.jsx)(T,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Vehicle Number`}),(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`},children:G?.vehicle_number||`-`}),(0,M.jsx)(T,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Vehicle Type / Capacity`}),(0,M.jsxs)(T,{sx:{border:`1px solid #e6e8f0`},children:[G?.vehicle_type||`-`,G?.vehicle_capacity?` | ${G?.vehicle_capacity}`:``]})]}),(0,M.jsxs)(f,{children:[(0,M.jsx)(T,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Driver Name`}),(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`},children:G?.driver_name||`-`}),(0,M.jsx)(T,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Driver Phone / License`}),(0,M.jsxs)(T,{sx:{border:`1px solid #e6e8f0`},children:[G?.driver_phone||`-`,G?.driver_license?` | ${G?.driver_license}`:``]})]})]})})}),(0,M.jsx)(g,{variant:`subtitle2`,sx:{textAlign:`center`,fontWeight:800,letterSpacing:1,color:`#263343`,mb:1,textTransform:`uppercase`},children:`Dispatch Items`}),(0,M.jsx)(p,{component:l,elevation:0,sx:{border:`1px solid #e6e8f0`,mb:3},children:(0,M.jsxs)(m,{size:`small`,stickyHeader:!0,children:[(0,M.jsx)(u,{children:(0,M.jsxs)(f,{sx:{backgroundColor:`#f5f7fb`},children:[(0,M.jsx)(T,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`center`,width:60},children:`S.No`}),(0,M.jsx)(T,{sx:{fontWeight:700,border:`1px solid #e6e8f0`},children:`Order Date`}),(0,M.jsx)(T,{sx:{fontWeight:700,border:`1px solid #e6e8f0`},children:`Order No`}),(0,M.jsx)(T,{sx:{fontWeight:700,border:`1px solid #e6e8f0`},children:`Item Name`}),(0,M.jsx)(T,{sx:{fontWeight:700,border:`1px solid #e6e8f0`},children:`Customer`}),(0,M.jsx)(T,{sx:{fontWeight:700,border:`1px solid #e6e8f0`},children:`Shipping Address`}),(0,M.jsx)(T,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`right`},children:`Qty Ordered`}),(0,M.jsx)(T,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`right`},children:`Qty Dispatched`}),(0,M.jsx)(T,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`right`},children:`Remaining Qty`}),(0,M.jsx)(T,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`center`},children:`Delivery Seq`}),(0,M.jsx)(T,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`center`},children:`Loading Seq`}),(0,M.jsx)(T,{sx:{fontWeight:700,border:`1px solid #e6e8f0`,textAlign:`center`},children:`Unloading Seq`})]})}),(0,M.jsx)(s,{children:$.length===0?(0,M.jsx)(f,{children:(0,M.jsx)(T,{colSpan:12,align:`center`,sx:{border:`1px solid #e6e8f0`},children:`No dispatch items added`})}):$.map((e,t)=>(0,M.jsxs)(f,{children:[(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`,textAlign:`center`},children:t+1}),(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`,whiteSpace:`nowrap`},children:e.order_date?q(e.order_date):`-`}),(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`,whiteSpace:`nowrap`},children:e.sales_order_number||e.sales_order||`-`}),(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`},children:(()=>e.item_name||e.order_items?.map(e=>e.item_name).filter(Boolean).join(`, `)||`--`)()}),(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`},children:e.customer_name||`-`}),(0,M.jsxs)(T,{sx:{border:`1px solid #e6e8f0`},children:[e.shipping_address||`-`,e.shipping_city_name?`, ${e.shipping_city_name}`:``,e.shipping_state_name?`, ${e.shipping_state_name}`:``]}),(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`,textAlign:`right`},children:X(e.quantity_ordered)}),(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`,textAlign:`right`},children:X(e.quantity_dispatched)}),(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`,textAlign:`right`},children:se(e)}),(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`,textAlign:`center`},children:e.delivery_sequence??`-`}),(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`,textAlign:`center`},children:e.loading_sequence??`-`}),(0,M.jsx)(T,{sx:{border:`1px solid #e6e8f0`,textAlign:`center`},children:e?.unloading_sequence??`-`})]},t))})]})}),(0,M.jsx)(w,{sx:{mt:2,mb:1}}),(0,M.jsxs)(d,{sx:{display:`flex`,justifyContent:`space-between`,alignItems:`center`},children:[(0,M.jsxs)(g,{variant:`body2`,sx:{fontWeight:600,color:`#1f2d3d`},children:[`Created By: `,ae]}),(0,M.jsxs)(g,{variant:`body2`,color:`text.secondary`,children:[`Generated on: `,(0,j.default)().format(`DD/MM/YYYY HH:mm`)]})]})]})]})};export{N as default};