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