1import{j as e}from"./vendor-motion-BYSGJZRG.js";import{u as se,r as c}from"./vendor-react-BAWrOFBJ.js";import{a1 as te,i as re,h as w,l as D,B as u,P as ae,C as ie,q as le,s as m,g as f}from"./index-BD6qdh5D.js";import{I as Q}from"./input-a2GrFMNb.js";import{S as _}from"./scroll-area-Db5VuWmi.js";import{h as ne}from"./helkos-logo-C0JpR9FH.js";import{U as T}from"./user-Btw4KzLD.js";import{S as $}from"./search-C_lzHtYP.js";import{P as C}from"./package-s8p4O8za.js";import{S as B}from"./shopping-cart-tQvz0V_R.js";import{M as oe}from"./minus-CtH1_2HY.js";import{P as ce}from"./plus-gav26jQM.js";import{T as de}from"./trash-2-Bk7EqU7C.js";import"./vendor-pdf-Cq2FS4Cb.js";import"./vendor-radix-Scc5zhsa.js";import"./vendor-charts-DWQnO0ie.js";function ke(){const{user:me,signOut:K}=te(),R=se(),V=re(),[U,N]=c.useState("customer"),[g,W]=c.useState(""),[o,S]=c.useState(null),[j,q]=c.useState(""),[h,v]=c.useState(null),[l,x]=c.useState([]),[k,P]=c.useState(!1),[H,y]=c.useState(new Map),{data:b=[]}=w({queryKey:["shop-customers-delivery"],queryFn:async()=>{const{data:s,error:t}=await m.from("shop_customers").select("id, company_name, contact_name, email, phone, address").order("company_name");if(t)throw t;return s}}),{data:G=[]}=w({queryKey:["shop-categories-delivery"],queryFn:async()=>{const{data:s,error:t}=await m.from("shop_categories").select("id, name, slug").eq("active",!0).order("sort_order");if(t)throw t;return s}}),{data:F=[]}=w({queryKey:["shop-products-delivery"],queryFn:async()=>{const{data:s,error:t}=await m.from("shop_products").select("id, code, name, selling_price, stock_quantity, category_id, image_url").eq("active",!0).order("name");if(t)throw t;const{data:r,error:a}=await m.from("shop_product_volumes").select("id, product_id, volume_liters, price, stock_quantity").eq("active",!0);if(a)throw a;return s.map(d=>({...d,volumes:r.filter(i=>i.product_id===d.id)}))}}),I=c.useMemo(()=>{if(!g.trim())return b.slice(0,20);const s=g.toLowerCase();return b.filter(t=>{var r,a,n;return(((r=t.company_name)==null?void 0:r.toLowerCase())||"").includes(s)||(((a=t.contact_name)==null?void 0:a.toLowerCase())||"").includes(s)||((n=t.phone)==null?void 0:n.includes(s))}).slice(0,20)},[b,g]),L=c.useMemo(()=>{let s=F;if(h&&(s=s.filter(t=>t.category_id===h)),j){const t=j.toLowerCase();s=s.filter(r=>r.name.toLowerCase().includes(t)||r.code.toLowerCase().includes(t))}return s.slice(0,40)},[F,j,h]),{subtotal:M,taxAmount:E,total:O}=c.useMemo(()=>{const s=l.reduce((a,n)=>a+n.unitPrice*n.quantity,0),r=s-s/(1+.255);return{subtotal:s,taxAmount:r,total:s}},[l]),J=l.reduce((s,t)=>s+t.quantity,0),X=async s=>{S(s),N("products");try{const{data:t,error:r}=await m.from("shop_customer_fixed_prices").select("product_id, fixed_price").eq("customer_id",s.id);if(!r&&t){const a=new Map;t.forEach(n=>a.set(n.product_id,Number(n.fixed_price))),y(a)}else y(new Map)}catch{y(new Map)}},z=(s,t)=>{const r=(t==null?void 0:t.id)||null,a=t?`${t.volume_liters}L`:"Base",n=(t==null?void 0:t.price)||s.selling_price,d=H.get(s.id)??n,i=l.findIndex(p=>p.productId===s.id&&p.volumeId===r);if(i>=0){const p=[...l];p[i].quantity+=1,x(p)}
1else x([...l,{productId:s.id,productCode:s.code,productName:s.name,volumeId:r,volumeLabel:a,quantity:1,unitPrice:d}]);f.success(`${s.name} added`)},A=(s,t)=>{const r=[...l],a=r[s].quantity+t;a<=0?x(l.filter((n,d)=>d!==s)):(r[s].quantity=a,x(r))},Y=s=>{x(l.filter((t,r)=>r!==s))},Z=async()=>{if(!o){f.error("Please select a customer");return}if(l.length===0){f.error("Cart is empty");return}P(!0);try{const{data:s,error:t}=await m.rpc("generate_shop_order_number");if(t)throw t;const{data:r,error:a}=await m.from("shop_orders").insert({order_number:s,customer_id:o.id,subtotal:M,tax_rate:25.5,tax_amount:E,total_amount:O,delivery_address:o.address||null,notes:"Created via Delivery POS",status:"pending",payment_status:"unpaid"}).select().single();if(a)throw a;const n=l.map(i=>({order_id:r.id,product_id:i.productId,product_code:i.productCode,product_name:i.productName,volume_id:i.volumeId,volume_label:i.volumeLabel,quantity:i.quantity,unit_price:i.unitPrice,total_price:i.unitPrice*i.quantity})),{error:d}=await m.from("shop_order_items").insert(n);if(d)throw d;for(const i of l)await m.rpc("reduce_order_stock",{p_product_id:i.productId,p_volume_id:i.volumeId||null,p_quantity:i.quantity});V.invalidateQueries({queryKey:["shop-orders"]}),f.success(`Order ${s} created!`),x([]),S(null),N("customer"),q(""),v(null),y(new Map)}catch(s){f.error(s.message||"Failed to create order")}finally{P(!1)}},ee=async()=>{await K(),R("/helkos/auth")};return e.jsxs("div",{className:"min-h-screen bg-background flex flex-col",children:[e.jsxs("header",{className:"bg-primary text-primary-foreground py-3 px-4 flex items-center justify-between shadow-md",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("img",{src:ne,alt:"Helkos",className:"h-8 w-auto"}),e.jsx("span",{className:"font-semibold hidden sm:inline",children:"Delivery Orders"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[o&&e.jsxs(D,{variant:"secondary",className:"hidden sm:flex",children:[e.jsx(T,{className:"h-3 w-3 mr-1"}),o.company_name]}),e.jsx(u,{variant:"ghost",size:"icon",onClick:ee,className:"text-primary-foreground hover:bg-primary-foreground/20",children:e.jsx(ae,{className:"h-5 w-5"})})]})]}),e.jsxs("div",{className:"flex-1 flex flex-col lg:flex-row overflow-hidden",children:[e.jsx("div",{className:"flex-1 flex flex-col overflow-hidden p-4",children:U==="customer"?e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"mb-4",children:[e.jsx("h2",{className:"text-xl font-bold mb-3",children:"Select Customer"}),e.jsxs("div",{className:"relative",children:[e.jsx($,{className:"absolute left-4 top-1/2 -translate-y-1/2 h-5 w-5 text-muted-foreground"}),e.jsx(Q,{placeholder:"Search by company, name, or phone...",value:g,onChange:s=>W(s.target.value),className:"pl-12 h-14 text-lg",autoFocus:!0})]})]}),e.jsx(_,{className:"flex-1",children:e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-3",children:[I.map(s=>e.jsx(ie,{className:"p-4 cursor-pointer hover:bg-accent transition-colors",onClick:()=>X(s),children:e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx("div",{className:"h-12 w-12 rounded-full bg-primary/10 flex items-center justify-center flex-shrink-0",children:e.jsx(T,{className:"h-6 w-6 text-primary"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"font-semibold text-lg truncate",children:s.company_name}),e.jsx("p",{className:"text-sm text-muted-foreground truncate",children:s.contact_name}),s.phone&&e.jsx("p",{className:"text-sm text-muted-foreground",children:s.phone})]})]})},s.id)),I.length===0&&e.jsx("div",{className:"col-span-full text-center py-12 text-muted-foreground",children:"No customers found"})]})})]}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"mb-4 flex items-center gap-3",children:[e.jsx(u,{variant:"outline",onClick:()=>N("customer"),className:"shrink-0",children:"â Change"}),e.jsxs("div",{className:"flex-1 bg-accent rounded-lg px-4 py-2",children:[e.jsx("span",{className:"font-medium",children:o==null?void 0:o.company_name}),e.jsx("span",{className:"text-muted-foreground ml-2 text-sm",children:o==null?void 0:o.contact_name})]})]}),e.jsxs("div",{className:"relative mb-3",children:[e.jsx($,{className:"absolute left-4 top-1/2 -translate-y-1/2 h-5 w-5 text-muted-foreground"}),e.jsx(Q,{placeholder:"Search products...",value:j,onChange:s=>q(s.target.value),className:"pl-12 h-12 text-lg"})]}),e.jsxs("div",{className:"flex gap-2 mb-4 overflow-x-auto pb-2",children:[e.jsx(u,{size:"lg",variant:h===null?"default":"outline",onClick:()=>v(null),className:"shrink-0 h-12 px-6",children:"All"}),G.map(s=>e.jsx(u,{size:"lg",variant:h===s.id?"default":"outline",onClick:()=>v(s.id),className:"shrink-0 h-12 px-6",children:s.name},s.id))]}),e.jsx(_,{className:"flex-1",children:e.jsxs("div",{className:"grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-3 xl:grid-cols-4 gap-3",children:[L.map(s=>s.volumes&&s.volumes.length>0?s.volumes.map(t=>e.jsxs("button",{onClick:()=>z(s,t),className:"bg-primary hover:bg-primary/90 text-primary-foreground rounded-xl p-3 text-center transition-all cursor-pointer active:scale-95 shadow-md flex flex-col items-center",children:[s.image_url?e.jsx("img",{src:s.image_url,alt:s.name,className:"w-16 h-16 object-contain bg-white rounded-lg mb-2"}
1):e.jsx("div",{className:"w-16 h-16 bg-primary-foreground/20 rounded-lg mb-2 flex items-center justify-center",children:e.jsx(C,{className:"h-8 w-8 opacity-50"})}),e.jsx("div",{className:"text-sm font-medium leading-tight line-clamp-2 min-h-[2.5rem]",children:s.name}),e.jsxs("div",{className:"text-xs opacity-80 mt-1 bg-primary-foreground/20 rounded px-2 py-0.5 inline-block",children:[t.volume_liters,"L"]}),e.jsxs("div",{className:"text-xl font-bold mt-1",children:["â¬",t.price.toFixed(0)]})]},`${s.id}-${t.id}`)):e.jsxs("button",{onClick:()=>z(s),className:"bg-primary hover:bg-primary/90 text-primary-foreground rounded-xl p-3 text-center transition-all cursor-pointer active:scale-95 shadow-md flex flex-col items-center",children:[s.image_url?e.jsx("img",{src:s.image_url,alt:s.name,className:"w-16 h-16 object-contain bg-white rounded-lg mb-2"}):e.jsx("div",{className:"w-16 h-16 bg-primary-foreground/20 rounded-lg mb-2 flex items-center justify-center",children:e.jsx(C,{className:"h-8 w-8 opacity-50"})}),e.jsx("div",{className:"text-sm font-medium leading-tight line-clamp-2 min-h-[2.5rem]",children:s.name}),e.jsxs("div",{className:"text-xl font-bold mt-2",children:["â¬",s.selling_price.toFixed(0)]})]},s.id)),L.length===0&&e.jsxs("div",{className:"col-span-full text-center py-12 text-muted-foreground",children:[e.jsx(C,{className:"h-12 w-12 mx-auto mb-3 opacity-50"}),"No products found"]})]})})]})}),e.jsxs("div",{className:"lg:w-96 bg-card border-t lg:border-t-0 lg:border-l flex flex-col",children:[e.jsx("div",{className:"p-4 border-b bg-muted/30",children:e.jsxs("h3",{className:"font-semibold flex items-center gap-2",children:[e.jsx(B,{className:"h-5 w-5"}),"Cart (",J," items)"]})}),e.jsx(_,{className:"flex-1 max-h-[200px] lg:max-h-none",children:e.jsx("div",{className:"p-3 space-y-2",children:l.length===0?e.jsxs("div",{className:"text-center py-8 text-muted-foreground",children:[e.jsx(B,{className:"h-10 w-10 mx-auto mb-2 opacity-30"}),e.jsx("p",{children:"Cart is empty"})]}):l.map((s,t)=>e.jsxs("div",{className:"flex items-center gap-2 p-3 bg-muted/50 rounded-lg",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"font-medium text-sm truncate",children:s.productName}),e.jsxs("div",{className:"flex items-center gap-2 text-xs text-muted-foreground",children:[e.jsx(D,{variant:"secondary",className:"text-xs",children:s.volumeLabel}),e.jsxs("span",{children:["â¬",s.unitPrice.toFixed(2)]})]})]}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(u,{size:"icon",variant:"outline",className:"h-9 w-9",onClick:()=>A(t,-1),children:e.jsx(oe,{className:"h-4 w-4"})}),e.jsx("span",{className:"w-8 text-center font-medium",children:s.quantity}),e.jsx(u,{size:"icon",variant:"outline",className:"h-9 w-9",onClick:()=>A(t,1),children:e.jsx(ce,{className:"h-4 w-4"})}),e.jsx(u,{size:"icon",variant:"ghost",className:"h-9 w-9 text-destructive",onClick:()=>Y(t),children:e.jsx(de,{className:"h-4 w-4"})})]})]},t))})}),e.jsxs("div",{className:"p-4 border-t bg-muted/30 space-y-3",children:[e.jsxs("div",{className:"flex justify-between text-lg",children:[e.jsx("span",{children:"Subtotal:"}),e.jsxs("span",{children:["â¬",M.toFixed(2)]})]}),e.jsxs("div",{className:"flex justify-between text-sm text-muted-foreground",children:[e.jsx("span",{children:"VAT (25.5%):"}),e.jsxs("span",{children:["â¬",E.toFixed(2)]})]}),e.jsxs("div",{className:"flex justify-between text-xl font-bold",children:[e.jsx("span",{children:"Total:"}),e.jsxs("span",{children:["â¬",O.toFixed(2)]})]}),e.jsx(u,{className:"w-full h-14 text-lg font-semibold",size:"lg",disabled:l.length===0||!o||k,onClick:Z,children:k?"Creating...":e.jsxs(e.Fragment,{children:[e.jsx(le,{className:"h-5 w-5 mr-2"}),"Create Order"]})})]})]})]})]})}export{ke 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.