1import{r as i,b as ie,j as e,t as _}from"./index-DdU35jPJ.js";import{S as ve,a as ye,b as we,c as Se,d as Ce}from"./sheet-BXj4TthU.js";import{B as z}from"./button-BfxMpK8P.js";import{I as j}from"./input-cpzcpc5y.js";import{L as u}from"./label-Bnj3FxlL.js";import{S as qe}from"./ShopCustomerCreateDrawer-BGFGhEHb.js";import{S as $e,a as Te,b as ke}from"./ShopOrderSummary-X5_sHyOt.js";import{S as ne}from"./shopping-cart-vZlzvnWl.js";import{U as le}from"./user-plus-CQvFDEfi.js";import{S as oe}from"./shield-check-CvVcm_Fy.js";import{S as ce}from"./shield-alert-ZSvoZ3Lw.js";import{X as Le}from"./x-DAruoHhn.js";import{S as Pe}from"./search-8uT820HJ.js";import{L as me}from"./loader-circle-B5Ydjp3O.js";import{T as Fe}from"./trash-2-Bi9M4bD6.js";import{P as Me}from"./plus-Bux0BJlB.js";import{P as Ae}from"./percent-D18g7D9L.js";import{C as Oe}from"./credit-card-BsmBHB--.js";import"./index-BhZ8ej-y.js";import"./index-12QbP6l0.js";import"./index-gN4drEn4.js";import"./index-BsvT06Lq.js";import"./index-9bdZCgcr.js";import"./index-BvvUEucZ.js";import"./index-Bwgqm7vL.js";import"./index-Dp3B9jqt.js";import"./clsx-B-dksMZM.js";import"./utils-DCmAa5as.js";import"./bundle-mjs-BTmSotT-.js";import"./textarea-RCbb6gBZ.js";import"./select-CicXJDZx.js";import"./index-B1Cc5OXO.js";import"./index-7px4L8n5.js";import"./index-CkK8lBUI.js";import"./index-DGERrSaP.js";import"./chevron-down-DcLGbHop.js";import"./createLucideIcon-DMxicyrC.js";import"./check-BECSssz0.js";import"./chevron-up-w1zviHLx.js";import"./user-TFtzTQfw.js";import"./briefcase-sHV214ND.js";import"./building-DrEGAp5Q.js";import"./phone-BiN9hL5_.js";import"./mail-DvcgW5d2.js";import"./truck-BQgQtOKT.js";import"./map-pin-CKcF86VS.js";import"./file-text-BIInLmy-.js";import"./upload-tj7WXC4r.js";import"./wrench-B9LFfIyi.js";import"./package-DYJogmeO.js";import"./trending-up-D6tLkDJM.js";function Mt({open:O,onClose:f,items:B,onSuccess:D,actAsCustomerId:v}){const[N,U]=i.useState([]),[de,V]=i.useState(!1),[T,E]=i.useState(""),[y,k]=i.useState(""),[ue,L]=i.useState(!1),[h,H]=i.useState(!1),[w,Q]=i.useState(8.25),[g,X]=i.useState(!1),[S,J]=i.useState(3),[K,W]=i.useState(""),[C,P]=i.useState([]),[F,Y]=i.useState([]),[M,Z]=i.useState([]),[G,ee]=i.useState(!1);i.useEffect(()=>{O&&(k(""),E(""),W(""),H(!1),Q(8.25),X(!1),J(3),P([]),Y([]),Z([]),pe())},[O]);const pe=async()=>{V(!0);try{const t=await ie.functions.invoke("manageShopCustomer",{action:"list",...v?{act_as_customer_id:v}:{}}),r=(t==null?void 0:t.data)||t;r!=null&&r.customers&&U(r.customers)}catch{}V(!1)},l=i.useMemo(()=>N.find(t=>t.id===y)||null,[N,y]),b=i.useMemo(()=>(B||[]).filter(t=>Number(t.quantity_on_hand)>0),[B]),I=(t,r,n)=>{P(o=>o.map((c,x)=>{if(x!==t)return c;const m={...c,[r]:n};if(r==="shop_inventory_item_id"){const a=b.find(s=>s.id===n);if(a){const s=Number(a.average_cost)||0;m.selling_price_per_tire=Math.round(s*1.3*100)/100,m.quantity=1}}if(r==="quantity"){const a=b.find(s=>s.id===c.shop_inventory_item_id);if(a){const s=Number(a.quantity_on_hand)||0;let p=parseInt(n,10);(isNaN(p)||p<1)&&(p=1),p>s&&(p=s,_.warning(`Only ${s} in stock â quantity clamped`,{id:`clamp-${t}`})),m.quantity=p}}return m}))},xe=()=>P(t=>[...t,{shop_inventory_item_id:"",quantity:1,selling_price_per_tire:0}]),he=t=>P(r=>r.filter((n,o)=>o!==t)),_e=t=>{U(r=>[t,...r]),k(t.id),L(!1),_.success(`"${t.company_name}" added and selected`)},q=i.useMemo(()=>{let t=0,r=0,n=0;for(const d of C){const re=b.find(je=>je.id===d.shop_inventory_item_id);if(!re)continue;const R=Number(d.quantity)||0,Ne=Number(d.selling_price_per_tire)||0,ge=Number(re.average_cost)||0;t+=Ne*R,r+=ge*R,n+=R}let o=0,c=0;for(const d of F)o+=Number(d.total_price)||0,c+=(Number(d.cost_per_unit)||0)*(Number(d.quantity)||0);let x=0,m=0;for(const d of M)x+=(Number(d.selling_price_per_unit)||0)*(Number(d.quantity)||0),m+=(Number(d.cost_per_unit)||0)*(Number(d.quantity)||0);const a=Math.round((t+o+x)*100)/100,s=Math.round((r+c+m)*100)/100,p=Math.round((a-s)*100)/100,$=h,A=$?Math.round(a*(Number(w)||0))/100:0,se=g,ae=se?Math.round(a*(Number(S)||0))/100:0,fe=Math.round((a+A+ae)*100)/100;return{tire_revenue:Math.round(t*100)/100,services_revenue:Math.round(o*100)/100,parts_revenue:Math.round(x*100)/100,total_revenue:a,total_cogs:s,total_gp:p,total_tires:n,tax_exempt:!1,tax_applied:$,sales_tax_amount:A,fee_applied:se,transaction_fee_amount:ae,grand_total:fe}},[C,F,M,b,l,h,w,g,S]),be=async()=>{var c,x,m;if(!y){_.error("Please select a customer");return}const t=C.filter(a=>a.shop_inventory_item_id&&Number(a.quantity)>0),r=F.filter(a=>Number(a.quantity)>0&&Number(a.price_per_unit)>=0),n=M.filter(a=>(a.description||"").trim()&&Number(a.quantity)>0&&Number(a.selling_price_per_unit)>=0);if(t.length===0&&r.length===0&&n.length===0){_.error("Add at least one tire, service, or part line");return}if(r.find(a=>a.service_type==="other"&&!(a.description||"").trim())){_.error('Add a description for the "Other" service line');return}for(let a=0;a<t.length;a++){const s=b.find(A=>
1A.id===t[a].shop_inventory_item_id);if(!s)continue;const p=Number(s.quantity_on_hand)||0,$=Number(t[a].quantity)||0;if($>p){_.error(`${s.brand} ${s.size}: only ${p} available, requested ${$}`);return}}ee(!0);try{const a=await ie.functions.invoke("createShopCustomerOrder",{shop_customer_id:y,sales_tax_applied:q.tax_applied,sales_tax_percent:Number(w),transaction_fee_applied:q.fee_applied,transaction_fee_percent:Number(S),notes:K.trim(),items:t.map(s=>({shop_inventory_item_id:s.shop_inventory_item_id,quantity:Number(s.quantity),selling_price_per_tire:Number(s.selling_price_per_tire)})),services:r.map(s=>({service_type:s.service_type,description:(s.description||"").trim(),quantity:Number(s.quantity),price_per_unit:Number(s.price_per_unit),cost_per_unit:Number(s.cost_per_unit)||0})),parts:n.map(s=>({description:(s.description||"").trim(),quantity:Number(s.quantity),selling_price_per_unit:Number(s.selling_price_per_unit),cost_per_unit:Number(s.cost_per_unit)||0})),...v?{act_as_customer_id:v}:{}});_.success(((c=a.data)==null?void 0:c.message)||"Order created"),D==null||D(),f==null||f()}catch(a){const s=((m=(x=a==null?void 0:a.response)==null?void 0:x.data)==null?void 0:m.error)||(a==null?void 0:a.message)||"Failed to create order";_.error(s)}finally{ee(!1)}},te=i.useMemo(()=>{if(!T)return N;const t=T.toLowerCase();return N.filter(r=>`${r.company_name||""} ${r.contact_name||""} ${r.phone_number||""} ${r.email||""}`.toLowerCase().includes(t))},[N,T]);return e.jsxs(e.Fragment,{children:[e.jsx(ve,{open:O,onOpenChange:t=>!t&&(f==null?void 0:f()),children:e.jsxs(ye,{side:"right",className:"w-full sm:max-w-lg overflow-y-auto",children:[e.jsxs(we,{children:[e.jsxs(Se,{className:"flex items-center gap-2",children:[e.jsx(ne,{className:"w-5 h-5"})," Create Customer Order"]}),e.jsx(Ce,{children:"Select a customer, sell tires, services, and parts. Cost, GP, tax, and fees are frozen at sale time."})]}),e.jsxs("div",{className:"space-y-4 px-4 pb-8",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(u,{className:"text-sm font-semibold text-slate-700",children:"Customer *"}),e.jsxs("button",{onClick:()=>L(!0),className:"flex items-center gap-1 text-xs font-medium text-blue-600 hover:text-blue-700",children:[e.jsx(le,{className:"w-3.5 h-3.5"})," New Customer"]})]}),l?e.jsxs("div",{className:"rounded-lg border border-blue-200 bg-blue-50/50 p-3",children:[e.jsxs("div",{className:"flex items-start justify-between gap-2",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"font-semibold text-slate-900 truncate",children:l.company_name}),e.jsx("p",{className:"text-sm text-slate-600",children:l.contact_name||"â"}),l.phone_number&&e.jsx("p",{className:"text-xs text-slate-500",children:l.phone_number})]}),e.jsxs("div",{className:"flex flex-col items-end gap-1 shrink-0",children:[e.jsx("span",{className:`px-2 py-0.5 rounded-full text-xs font-semibold ${l.sales_tax_exempt?"bg-green-100 text-green-700":"bg-amber-100 text-amber-700"}`,children:l.sales_tax_exempt?e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(oe,{className:"w-3 h-3"})," Tax Exempt"]}):e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(ce,{className:"w-3 h-3"})," Taxable"]})}),e.jsx("button",{onClick:()=>k(""),className:"text-xs text-slate-400 hover:text-red-500",children:e.jsx(Le,{className:"w-3.5 h-3.5"})})]})]}),l.dot_number&&e.jsxs("p",{className:"text-xs text-slate-500 mt-1",children:["DOT: ",l.dot_number]})]}):e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"relative",children:[e.jsx(Pe,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-slate-400"}),e.jsx(j,{value:T,onChange:t=>E(t.target.value),placeholder:"Search customers by name, company, phoneâ¦",className:"pl-9"})]}),de?e.jsx("div",{className:"flex items-center justify-center py-4",children:e.jsx(me,{className:"w-4 h-4 animate-spin text-blue-600"})}):te.length===0?e.jsxs("div",{className:"text-center py-4",children:[e.jsx("p",{className:"text-sm text-slate-500 mb-2",children:N.length===0?"N
1o customers yet.":"No matching customers."}),e.jsxs(z,{onClick:()=>L(!0),size:"sm",variant:"outline",className:"border-blue-300 text-blue-600 hover:bg-blue-50",children:[e.jsx(le,{className:"w-4 h-4 mr-1.5"})," Create New Customer"]})]}):e.jsx("div",{className:"max-h-48 overflow-y-auto rounded-lg border border-slate-200 divide-y divide-slate-100",children:te.map(t=>e.jsx("button",{onClick:()=>{k(t.id),E("")},className:"w-full text-left px-3 py-2 hover:bg-blue-50 transition-colors",children:e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"text-sm font-medium text-slate-900 truncate",children:t.company_name}),e.jsxs("p",{className:"text-xs text-slate-500 truncate",children:[t.contact_name||"â"," ",t.phone_number?`· ${t.phone_number}`:""]})]}),t.sales_tax_exempt&&e.jsx(oe,{className:"w-4 h-4 text-green-500 shrink-0"})]})},t.id))})]})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsx(u,{className:"text-sm font-semibold text-slate-700",children:"Line Items"}),C.map((t,r)=>{const n=b.find(s=>s.id===t.shop_inventory_item_id),o=Number(n==null?void 0:n.average_cost)||0,c=Number(t.selling_price_per_tire)||0,x=Number(t.quantity)||0,m=Number(n==null?void 0:n.quantity_on_hand)||0,a=Math.round((c-o)*x*100)/100;return e.jsxs("div",{className:"rounded-lg border border-slate-200 p-3 space-y-2 bg-slate-50",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("span",{className:"text-xs font-medium text-slate-500",children:["Line ",r+1]}),e.jsx("button",{onClick:()=>he(r),className:"text-red-500 hover:text-red-700",children:e.jsx(Fe,{className:"w-4 h-4"})})]}),e.jsxs("select",{value:t.shop_inventory_item_id,onChange:s=>I(r,"shop_inventory_item_id",s.target.value),className:"w-full rounded-lg border border-slate-200 bg-white px-3 py-2 text-sm",children:[e.jsx("option",{value:"",children:"Select tireâ¦"}),b.map(s=>e.jsxs("option",{value:s.id,children:[s.brand," ",s.model||""," ",s.size," ",s.position||""," (",s.quantity_on_hand," in stock)"]},s.id))]}),n&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-3 gap-2",children:[e.jsxs("div",{children:[e.jsx(u,{className:"text-xs text-slate-500",children:"Qty"}),e.jsx(j,{type:"number",min:"1",max:m,value:t.quantity,onChange:s=>I(r,"quantity",s.target.value),className:"h-9"})]}),e.jsxs("div",{children:[e.jsx(u,{className:"text-xs text-slate-500",children:"Sell $/tire"}),e.jsx(j,{type:"number",min:"0",step:"0.01",value:t.selling_price_per_tire,onChange:s=>I(r,"selling_price_per_tire",s.target.value),className:"h-9"})]}),e.jsxs("div",{children:[e.jsx(u,{className:"text-xs text-slate-500",children:"Cost $/tire"}),e.jsxs("div",{className:"h-9 flex items-center px-3 rounded-lg bg-slate-100 text-sm text-slate-600",children:["$",o.toFixed(2)]})]})]}),e.jsxs("div",{className:"flex items-center justify-between text-xs",children:[e.jsxs("span",{className:"text-blue-600 font-medium",children:[m," available"]}),e.jsxs("span",{className:"text-slate-500",children:["GP/tire: ",e.jsxs("span",{className:c-o>=0?"text-green-600 font-medium":"text-red-600 font-medium",children:["$",(c-o).toFixed(2)]})]}),e.jsxs("span",{className:"text-slate-500",children:["Line GP: ",e.jsxs("span",{className:a>=0?"text-green-600 font-medium":"text-red-600 font-medium",children:["$",a.toFixed(2)]})]})]})]})]},r)}),e.jsxs(z,{onClick:xe,variant:"outline",className:"w-full border-blue-300 text-blue-600 hover:bg-blue-50",children:[e.jsx(Me,{className:"w-4 h-4 mr-1.5"})," ",C.length===0?"Add Tire":"Add Another Tire"]})]}),e.jsx($e,{services:F,onChange:Y}),e.jsx(Te,{parts:M,onChange:Z}),e.jsxs("div",{className:"rounded-lg border border-slate-200 p-3 space-y-2 bg-white",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs(u,{className:"text-sm font-semibold text-slate-700 flex items-center gap-1.5",children:[e.jsx(Ae,{className:"w-4 h-4 text-blue-500"})," Sales Tax"]}),e.jsx("button",{onClick:()=>H(!h),className:`relative w-11 h-6 rounded-full transition-colors ${h?"bg-blue-600":"bg-slate-300"}`,"aria-label":h?"Turn sales tax off":"Turn sales tax on",children:e.jsx("span",{className:`absolute top-0.5 left-0.5 w-
15 h-5 rounded-full bg-white shadow transition-transform ${h?"translate-x-5":""}`})})]}),h&&e.jsxs("div",{className:"grid grid-cols-2 gap-3 pt-1",children:[e.jsxs("div",{children:[e.jsx(u,{className:"text-xs text-slate-500",children:"Tax %"}),e.jsx(j,{type:"number",min:"0",step:"0.01",value:w,onChange:t=>Q(t.target.value),className:"h-9",placeholder:"e.g. 8.25"})]}),e.jsxs("div",{children:[e.jsx(u,{className:"text-xs text-slate-500",children:"Tax Amount"}),e.jsxs("div",{className:"h-9 flex items-center px-3 rounded-lg bg-slate-100 text-sm text-slate-600",children:["$",q.sales_tax_amount.toFixed(2)]})]})]}),h&&(l==null?void 0:l.sales_tax_exempt)&&e.jsxs("div",{className:"flex items-center gap-1.5 text-xs text-amber-700 bg-amber-50 border border-amber-200 rounded-md px-2 py-1.5",children:[e.jsx(ce,{className:"w-3.5 h-3.5 shrink-0"}),"Customer is marked Tax Exempt â tax is being applied manually"]})]}),e.jsxs("div",{className:"rounded-lg border border-slate-200 p-3 space-y-2 bg-white",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs(u,{className:"text-sm font-semibold text-slate-700 flex items-center gap-1.5",children:[e.jsx(Oe,{className:"w-4 h-4 text-indigo-500"})," Transaction Fee"]}),e.jsx("button",{onClick:()=>X(!g),className:`relative w-11 h-6 rounded-full transition-colors ${g?"bg-blue-600":"bg-slate-300"}`,children:e.jsx("span",{className:`absolute top-0.5 left-0.5 w-5 h-5 rounded-full bg-white shadow transition-transform ${g?"translate-x-5":""}`})})]}),g&&e.jsxs("div",{className:"grid grid-cols-2 gap-3 pt-1",children:[e.jsxs("div",{children:[e.jsx(u,{className:"text-xs text-slate-500",children:"Fee %"}),e.jsx(j,{type:"number",min:"0",step:"0.01",value:S,onChange:t=>J(t.target.value),className:"h-9"})]}),e.jsxs("div",{children:[e.jsx(u,{className:"text-xs text-slate-500",children:"Fee Amount"}),e.jsxs("div",{className:"h-9 flex items-center px-3 rounded-lg bg-slate-100 text-sm text-slate-600",children:["$",q.transaction_fee_amount.toFixed(2)]})]})]})]}),e.jsxs("div",{children:[e.jsx(u,{htmlFor:"notes",children:"Notes"}),e.jsx(j,{id:"notes",value:K,onChange:t=>W(t.target.value),placeholder:"Optional order notes"})]}),e.jsx(ke,{preview:q,salesTaxPercent:w,transactionFeePercent:S}),e.jsxs(z,{onClick:be,disabled:G||!y,className:"w-full bg-blue-600 hover:bg-blue-700 text-white",children:[G?e.jsx(me,{className:"w-4 h-4 mr-2 animate-spin"}):e.jsx(ne,{className:"w-4 h-4 mr-2"}),G?"Creatingâ¦":"Create Order"]})]})]})}),e.jsx(qe,{open:ue,onClose:()=>L(!1),actAsCustomerId:v,onCreated:_e})]})}export{Mt as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.