import{b as e,H as he,r as w,a as F}from"./app-DTeOhhdM.js";import{w as ee}from"./index-BOZ6ExPe.js";import{D as xe,a as fe,B as te}from"./AuthenticatedLayout-DNj-hWy1.js";import{A as se}from"./autocomplete.esm-wKYwH1zm.js";import{u as ye,C as Y}from"./index.esm-Bufnuwsb.js";import{c as be}from"./customerSearchClientTemplate-DwaSlkBQ.js";function qe(){return e.jsx("div",{class:"p-4  w-full",children:e.jsx("div",{class:"animate-pulse flex space-x-4",children:e.jsx("div",{class:"flex-1 space-y-6 py-1",children:e.jsx("div",{class:"space-y-3",children:e.jsxs("div",{class:"grid grid-cols-12 gap-4",children:[e.jsxs("div",{className:"col-span-6 flex  gap-2",children:[e.jsx("div",{className:"w-[4rem]",children:e.jsx("div",{class:" bg-slate-200 h-[4rem] w-[4rem] "})}),e.jsxs("div",{className:"w-full space-y-2.5",children:[e.jsx("div",{class:"h-2 bg-slate-200 rounded "}),e.jsx("div",{class:"h-2 bg-slate-200 rounded "}),e.jsx("div",{class:"h-2 bg-slate-200 rounded "}),e.jsx("div",{class:"h-2 bg-slate-200 rounded "})]})]}),e.jsxs("div",{className:"col-span-6 flex  gap-2",children:[e.jsx("div",{className:"w-[4rem]",children:e.jsx("div",{class:" bg-slate-200 h-[4rem] w-[4rem] "})}),e.jsxs("div",{className:"w-full space-y-2.5",children:[e.jsx("div",{class:"h-2 bg-slate-200 rounded "}),e.jsx("div",{class:"h-2 bg-slate-200 rounded "}),e.jsx("div",{class:"h-2 bg-slate-200 rounded "}),e.jsx("div",{class:"h-2 bg-slate-200 rounded "})]})]}),e.jsxs("div",{className:"col-span-6 flex  gap-2",children:[e.jsx("div",{className:"w-[4rem]",children:e.jsx("div",{class:" bg-slate-200 h-[4rem] w-[4rem] "})}),e.jsxs("div",{className:"w-full space-y-2.5",children:[e.jsx("div",{class:"h-2 bg-slate-200 rounded "}),e.jsx("div",{class:"h-2 bg-slate-200 rounded "}),e.jsx("div",{class:"h-2 bg-slate-200 rounded "}),e.jsx("div",{class:"h-2 bg-slate-200 rounded "})]})]}),e.jsxs("div",{className:"col-span-6 flex  gap-2",children:[e.jsx("div",{className:"w-[4rem]",children:e.jsx("div",{class:" bg-slate-200 h-[4rem] w-[4rem] "})}),e.jsxs("div",{className:"w-full space-y-2.5",children:[e.jsx("div",{class:"h-2 bg-slate-200 rounded "}),e.jsx("div",{class:"h-2 bg-slate-200 rounded "}),e.jsx("div",{class:"h-2 bg-slate-200 rounded "}),e.jsx("div",{class:"h-2 bg-slate-200 rounded "})]})]})]})})})})})}(function(){function d(t){const n=window.getComputedStyle(t);return!(t.hidden||t.disabled||n.display==="none"||n.visibility==="hidden")}function m(){const t=document.querySelectorAll('td input[type="number"], td input.lg\\:w-16');return Array.from(t).filter(n=>d(n))}function h(){const t=[".pos-search input",'input[aria-label="Product"]',".p-autocomplete input",'input[placeholder*="search"], input[placeholder*="Search"]'];for(const n of t){const p=document.querySelector(n);if(p&&d(p))return p}return null}function g(){const t=['button[label="Process"]','button:contains("Process")','input[type="submit"]','button[type="submit"]'];for(const n of t){const p=document.querySelector(n);if(p&&d(p))return p}return null}let r=-1,l=0,u=0,k=0;const U=60,K=3,M=1e3;function V(){const t=h();return t?(t.focus(),r=-1,!0):!1}function Q(){const t=m();if(t.length===0)return!1;const n=t[t.length-1];return n?(n.focus(),n.select(),r=t.length-1,!0):!1}function j(){const t=g();return t?(t.focus(),!0):!1}function T(){const t=m();if(t.length===0)return!1;r===-1?r=t.length-1:r=r>0?r-1:t.length-1;const n=t[r];return n?(n.focus(),n.select(),!0):!1}function R(){const t=m();if(t.length===0)return!1;r===-1?r=0:r=r<t.length-1?r+1:0;const n=t[r];return n?(n.focus(),n.select(),!0):!1}window.posControlFunctions={focusProductSearch:V,focusLastQuantity:Q,focusQuantityUp:T,focusQuantityDown:R,focusSubmitButton:j};function v(t){document.readyState==="loading"?document.addEventListener("DOMContentLoaded",t,{once:!0}):t()}v(()=>{document.addEventListener("keydown",t=>{let n=!1;const p=t.timeStamp||Date.now();if(t.key.length===1){l===0?(l=p,u=1):p-k<=U?u+=1:(l=p,u=1),k=p;return}if(t.key==="Enter"&&!t.shiftKey){const _=l?p-l:1/0,P=u>=K&&_<=M;if(l=0,u=0,k=0,P)return;n=V(),n&&t.preventDefault()}else if(t.key==="Enter"&&t.shiftKey)t.preventDefault(),n=j();else if(t.key==="Tab")n=Q(),n&&t.preventDefault();else if(t.key==="ArrowUp"){const _=h();document.activeElement!==_&&(n=T(),n&&t.preventDefault())}else if(t.key==="ArrowDown"){const _=h();document.activeElement!==_&&(n=R(),n&&t.preventDefault())}else{if(t.key.length===1)return;if(["Backspace","Delete","ArrowLeft","ArrowRight","ArrowUp","ArrowDown","Home","End"].includes(t.key)){if(document.activeElement&&document.activeElement.tagName==="INPUT")return}else t.preventDefault()}},!0)})})();function ge(){return"ontouchstart"in window||navigator.maxTouchPoints>0||navigator.msMaxTouchPoints>0}function we(){var l;(l=document.querySelector(".virtual-pos-controls"))==null||l.remove();const o=document.createElement("div");o.className="virtual-pos-controls",o.style.cssText=`
        position: fixed;
        right: 20px;
        top: 50%;
        transform: translateY(-50%);
        z-index: 1000;
        display: flex;
        flex-direction: column;
        gap: 8px;
        background: rgba(0, 0, 0, 0.8);
        padding: 10px;
        border-radius: 8px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    `;const d=`
        width: 45px;
        height: 45px;
        border: none;
        border-radius: 6px;
        background: #007bff;
        color: white;
        font-size: 18px;
        font-weight: bold;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all 0.2s;
        user-select: none;
    `,m=document.createElement("button");m.innerHTML="↑",m.style.cssText=d,m.onclick=()=>{var u;(u=window.posControlFunctions)!=null&&u.focusQuantityUp&&(window.posControlFunctions.focusQuantityUp(),m.style.background="#0056b3",setTimeout(()=>m.style.background="#007bff",150))},m.title="Previous Quantity";const h=document.createElement("button");h.innerHTML="↓",h.style.cssText=d,h.onclick=()=>{var u;(u=window.posControlFunctions)!=null&&u.focusQuantityDown&&(window.posControlFunctions.focusQuantityDown(),h.style.background="#0056b3",setTimeout(()=>h.style.background="#007bff",150))},h.title="Next Quantity";const g=document.createElement("button");g.innerHTML="🔍",g.style.cssText=d+"background: #28a745;",g.onclick=()=>{var u;(u=window.posControlFunctions)!=null&&u.focusProductSearch&&(window.posControlFunctions.focusProductSearch(),g.style.background="#1e7e34",setTimeout(()=>g.style.background="#28a745",150))},g.title="Focus Search";const r=document.createElement("button");r.innerHTML="#",r.style.cssText=d+"background: #ffc107; color: #000;",r.onclick=()=>{var u;(u=window.posControlFunctions)!=null&&u.focusLastQuantity&&(window.posControlFunctions.focusLastQuantity(),r.style.background="#e0a800",setTimeout(()=>r.style.background="#ffc107",150))},r.title="Focus Last Quantity",o.append(m,h,g,r),document.body.appendChild(o)}function ve(){if(ge()){we();const o=document.createElement("style");o.textContent=`
            .virtual-pos-controls button:hover {
                transform: scale(1.05);
            }
            .virtual-pos-controls button:active {
                transform: scale(0.95);
            }
            @media (max-width: 768px) {
                .virtual-pos-controls {
                    right: 10px !important;
                }
            }
        `,document.head.appendChild(o)}}const Qe=()=>{he.useEffect(()=>(ve(),()=>{var o;(o=document.querySelector(".virtual-pos-controls"))==null||o.remove()}),[]),ee("f1",o=>{var d;o.preventDefault(),(d=document.querySelector(".customer-search-input input"))==null||d.focus()}),ee("f2",o=>{var d;o.preventDefault(),(d=document.querySelector(".pos-search-input input"))==null||d.focus()})};function Pe({image:o}){const d=window.location.origin;return e.jsx("img",{src:o?`${d}/upload/${o}`:"https://www.primefaces.org/wp-content/uploads/2020/05/placeholder.png",alt:"Product image",className:"block h-[68px] w-[68px] object-cover",loading:"lazy"})}const _e="sales.return.store",je="customer-search",Ne="invoice.search";function Ae({visible:o,onHide:d,warehouse:m=[],permission:h=[],defaultWarehouseId:g=null,initialCustomer:r=null,toastRef:l,onSuccess:u,onExchangePending:k,storeRoute:U=_e,clientSearchRoute:K=je,invoiceSearchRoute:M=Ne,locationId:V=null}){const{control:Q,watch:j,setValue:T}=ye({defaultValues:{sales_return_warehouse_id:null,sales_return_customer_id:null,sales_return_invoice_no:null}}),[R,v]=w.useState([]),[t,n]=w.useState([]),[p,_]=w.useState({}),[P,z]=w.useState("cash"),[G,O]=w.useState(!1),[ne,X]=w.useState([]),[re,D]=w.useState(""),[ae,I]=w.useState(""),[le,oe]=w.useState(0),[H,W]=w.useState(null),ce=h.includes("sales_admin_access"),J=j("sales_return_customer_id"),ue=j("sales_return_invoice_no"),ie=a=>{const x=a.query||"";setTimeout(()=>{F.post(route(K),{data:x}).then(s=>{var c;(c=s.data)!=null&&c.customer?X(s.data.customer):X([])}).catch(()=>X([]))},250)},de=a=>{setTimeout(()=>{const x={data:a.query||""};J&&(x.customer_id=J),F.post(route(M),x).then(s=>{v(s.data||[])}).catch(()=>v([]))},250)};w.useEffect(()=>{o&&(z("cash"),n([]),_({}),I(""),v([]),W(null),T("sales_return_invoice_no",null),r!=null&&r.id?(T("sales_return_customer_id",r.id),D(r.displayName||""),F.post(route(M),{data:"",customer_id:r.id}).then(a=>v(a.data||[])).catch(()=>v([]))):(T("sales_return_customer_id",null),D("")),oe(a=>a+1))},[o,r==null?void 0:r.id,r==null?void 0:r.displayName]);const B=()=>{n([]),_({}),v([]),D(""),I(""),d()},pe=async()=>{var y,f,A;if(H!=null&&H.created_at)try{const{data:i}=await F.get(route("setting.rule.value"),{params:{name:"sales_return_days"}}),N=i==null?void 0:i.value;if(N!=null&&N!==""){const C=parseInt(N,10)||0,b=new Date(H.created_at),q=new Date;b.setHours(0,0,0,0),q.setHours(0,0,0,0);const L=q-b;if(Math.floor(L/(1440*60*1e3))>C){(y=l==null?void 0:l.current)==null||y.show({severity:"error",summary:"Return period exceeded",detail:`Return is allowed within ${C} day(s) of sale. Sale date: ${b.toLocaleDateString()}.`,life:5e3});return}}}catch{}let a=!1;const x=t.map((i,N)=>{var Z;const C=i.attribute_id??N,b=Number(p[C]??0);if(!b)return null;const q=Number(i.price??0),L=Number(i.after_discount??0),E=q-L,S=Number(i.qty??i.sales_quantity??0),$=Number(((Z=i.salesReturn)==null?void 0:Z.total_qty)??0),me=S-$;return(b>me||b<0)&&(a=!0),{product_id:Number(i.product_id),attribute_id:Number(i.attribute_id),sales_quantity:S,return_quantity:b,return_prv:$,sales:E,return_price:E,total:E*b}}).filter(Boolean);if(a||x.length===0){(f=l==null?void 0:l.current)==null||f.show({severity:"warn",summary:"Invalid return quantity",detail:"Return quantity is required and cannot be greater than sold quantity minus previous returns."});return}const c={reference_no:"",warehouse_id:j("sales_return_warehouse_id")??g,return_date:new Date().toISOString().slice(0,10),invoice_no:j("sales_return_invoice_no"),costomer_id:j("sales_return_customer_id"),list:x};if(P==="exchange"){k==null||k(c),B(),(A=l==null?void 0:l.current)==null||A.show({severity:"info",summary:"Exchange Return",detail:"Add replacement items and process invoice to complete exchange."});return}O(!0),F.post(route(U),c).then(()=>{var i;O(!1),B(),(i=l==null?void 0:l.current)==null||i.show({severity:"success",summary:"Sales Return",detail:"Quick return applied successfully"}),u==null||u()}).catch(()=>{var i;O(!1),(i=l==null?void 0:l.current)==null||i.show({severity:"error",summary:"Sales Return",detail:"Failed to apply return"})})};return e.jsx(xe,{header:"Quick Sales Return",visible:o,style:{width:"80vw"},modal:!0,onHide:B,children:e.jsxs("div",{className:"grid grid-cols-12 gap-4",children:[e.jsxs("div",{className:"col-span-12 md:col-span-4 space-y-4",children:[ce&&e.jsx("div",{className:"field",children:e.jsx(Y,{name:"sales_return_warehouse_id",control:Q,render:({field:a})=>e.jsx(fe,{...a,optionLabel:"name",optionValue:"id",className:"w-full !h-9 text-xs",options:m||[],placeholder:"Select warehouse"})})}),e.jsxs("div",{className:"field",children:[e.jsx("label",{className:"block text-xs font-semibold mb-1",children:"Customer"}),e.jsx(Y,{name:"sales_return_customer_id",control:Q,render:({field:a})=>e.jsx(se,{dropdown:!0,value:re,suggestions:ne,completeMethod:ie,"aria-label":"Customer",itemTemplate:be,onChange:x=>{const s=x.value;if(s&&typeof s=="object"&&s.id){a.onChange(s.id);const c=s.name||s.number||"",y=s.number&&s.name?` (${s.number})`:"",f=s.user_type?` (${s.user_type})`:"";D(c+y+f),I(""),v([]),n([])}else typeof s=="string"?D(s):s==null&&(a.onChange(null),D(""),I(""),v([]),n([]))},className:"w-full customer-search-input"},le)})]}),e.jsxs("div",{className:"field",children:[e.jsx("label",{className:"block text-xs font-semibold mb-1",children:"Invoice No"}),e.jsx(Y,{name:"sales_return_invoice_no",control:Q,render:({field:a})=>e.jsx(se,{value:ae,suggestions:R,completeMethod:de,dropdown:!0,field:"invoice_no",onChange:x=>{const s=x.value;if(typeof s=="string"){I(s);return}if(s==null){W(null),a.onChange(null),n([]),I("");return}if(a.onChange((s==null?void 0:s.id)??null),W(s||null),s!=null&&s.client_id&&(s!=null&&s.customer)){T("sales_return_customer_id",s.client_id);const c=s.customer,y=c.name||c.number||"",f=c.number&&c.name?` (${c.number})`:"",A=c.user_type?` (${c.user_type})`:"";D(y+f+A)}s!=null&&s.details?n(s.details.map(c=>{const y=(s.sales_return||[]).find(f=>f.product_id===c.product_id&&f.attribute_id===c.attribute_id)??null;return{...c,salesReturn:y}})):n([]),I((s==null?void 0:s.invoice_no)??"")},className:"w-full"})})]}),e.jsx("div",{className:"field",children:e.jsxs("div",{className:"col-span-12 flex items-center gap-4",children:[e.jsx("label",{className:"text-xs font-semibold text-slate-700",children:"Return type:"}),e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs("label",{className:"flex items-center gap-2 cursor-pointer",children:[e.jsx("input",{type:"radio",name:"salesReturnType",checked:P==="cash",onChange:()=>z("cash"),className:"h-3.5 w-3.5"}),e.jsx("span",{className:"text-xs",children:"Cash"})]}),e.jsxs("label",{className:"flex items-center gap-2 cursor-pointer",children:[e.jsx("input",{type:"radio",name:"salesReturnType",checked:P==="exchange",onChange:()=>z("exchange"),className:"h-3.5 w-3.5"}),e.jsx("span",{className:"text-xs",children:"Exchange"})]})]}),P==="exchange"&&e.jsx("small",{className:"text-amber-600 text-xs",children:"Add replacement items and process invoice to complete exchange."})]})})]}),e.jsxs("div",{className:"col-span-12 md:col-span-8",children:[e.jsxs("div",{className:"mb-2 flex items-center justify-between",children:[e.jsx("h3",{className:"text-sm font-semibold text-slate-800",children:"Invoice Items"}),e.jsx("small",{className:"text-xs text-slate-500",children:"Select invoice to load items and apply quick return."})]}),e.jsx("div",{className:"border rounded-md max-h-[55vh] overflow-y-auto",children:e.jsxs("table",{className:"min-w-full text-xs",children:[e.jsx("thead",{className:"bg-slate-50",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-2 py-2 text-left",children:"Product"}),e.jsx("th",{className:"px-2 py-2 text-right",children:"Sold"}),e.jsx("th",{className:"px-2 py-2 text-right",children:"Prev. Return"}),e.jsx("th",{className:"px-2 py-2 text-right",children:"Return Qty"}),e.jsx("th",{className:"px-2 py-2 text-right",children:"Price"})]})}),e.jsxs("tbody",{children:[t.length===0&&e.jsx("tr",{children:e.jsx("td",{colSpan:5,className:"px-2 py-4 text-center text-slate-400",children:"No items loaded. Choose customer and invoice."})}),t.map((a,x)=>{var i,N,C,b,q,L;const s=Number(a.qty??a.sales_quantity??0),c=Number(((i=a.salesReturn)==null?void 0:i.total_qty)??0),y=s-c,f=a.attribute_id??x,A=p[f]??"";return e.jsxs("tr",{className:"border-t",children:[e.jsxs("td",{className:"px-2 py-2",children:[((N=a.product)==null?void 0:N.name)||a.product_name||"",(((C=a==null?void 0:a.attribute)==null?void 0:C.sku)||((b=a==null?void 0:a.arribute)==null?void 0:b.sku))&&e.jsxs("span",{className:"ml-1 text-[10px] text-slate-500",children:["(",((q=a.attribute)==null?void 0:q.sku)||((L=a.arribute)==null?void 0:L.sku),")"]})]}),e.jsx("td",{className:"px-2 py-2 text-right",children:s}),e.jsx("td",{className:"px-2 py-2 text-right",children:c}),e.jsx("td",{className:"px-2 py-2 text-right",children:e.jsx("input",{type:"number",min:0,max:y,value:A,onChange:E=>{let S=Number(E.target.value||0);S<0&&(S=0),S>y&&(S=y),_($=>({...$,[f]:S}))},className:"w-16 border border-slate-300 rounded px-1 py-0.5 text-right"})}),e.jsx("td",{className:"px-2 py-2 text-right",children:Number(a.price??0).toFixed(2)})]},f)})]})]})}),e.jsxs("div",{className:"mt-4 flex justify-end gap-2",children:[e.jsx(te,{className:"p-button-text",label:"Close",icon:"pi pi-times",onClick:B}),e.jsx(te,{label:"Apply Return",icon:"pi pi-check",loading:G,disabled:G||t.length===0||!ue,onClick:pe})]})]})]})})}export{qe as P,Ae as Q,Pe as p,Qe as u};
