1import{u as J,r as x,j as e}from"./vendor-react-zwv87yMD.js";import{u as W,a as b,b as K}from"./vendor-query-DYq8g7OI.js";import{B as u}from"./button-DlBDuKbo.js";import{A as X,a as Z,b as ee,c as te,d as se,e as ae,f as ie,g as re}from"./alert-dialog-P2RRIvov.js";import{d as A}from"./delivery-tickets-T0DTAAqI.js";import{c as le}from"./contacts-DebKsgyA.js";import{d as de}from"./drivers-DMmSfnK_.js";import{m as ne}from"./menu-items-B9YVhy5i.js";import{c as oe}from"./customer-pricing-BilO6PTR.js";import{u as ce}from"./authStore-Bv6p2Ne2.js";import{g as me}from"./timezoneFormatters-Lg56xFv0.js";import{u as ue}from"./useOrgTimezone-C1UagyKl.js";import{e as xe,b as he,c as pe,L as fe}from"./vendor-router-DimZLiZb.js";import"./vendor-charts-CtrTLS4K.js";import"./vendor-i18n-BdYkS11p.js";import"./vendor-ui-CfDMlrKu.js";import"./vendor-pdf-DQuePnfN.js";import"./vendor-state-ByI9XJey.js";import"./vendor-editor-C_dhmuey.js";import"./utils-BXSzAfFa.js";import"./api-client-wl3SCNAO.js";import"./i18n-CwjSIy0a.js";import"./vendor-icons-bLSWzydc.js";import"./employeeAuthStore-lYSKMuhw.js";function Be(){const{t:s}=J("delivery"),{id:h}=xe(),[P]=he(),p=pe(),j=W(),{permissions:M,currentOrganizationId:f}=ce(),z=ue(),L=M.includes("MANAGE_DELIVERY_TICKETS"),n=!!h,[i,o]=x.useState({contactId:P.get("contactId")||"",contractId:P.get("contractId")||"",driverId:"",scheduledDate:me(z),scheduledTime:"",deliveryAddress:"",notes:"",internalNotes:""}),[c,v]=x.useState([]),[O,w]=x.useState(!1),[y,N]=x.useState(null),[r,m]=x.useState({menuItemId:"",description:"",sku:"",category:"",quantity:1,unit:"each",unitPrice:0,taxable:!0,notes:""}),[I,k]=x.useState(null),{data:l,isLoading:Q}=b({queryKey:["delivery-ticket",f,h],queryFn:()=>A.getOne(h),enabled:n}),{data:D=[]}=b({queryKey:["contacts",f],queryFn:()=>le.getAll(),staleTime:300*1e3}),{data:B=[]}=b({queryKey:["drivers",f],queryFn:()=>de.getAll(),staleTime:300*1e3}),{data:S=[]}=b({queryKey:["menuItems",f,{includeInactive:!1}],queryFn:()=>ne.getAll(),staleTime:300*1e3}),{data:$=[]}=b({queryKey:["customer-pricing",f,i.contactId],queryFn:()=>oe.getByContact(i.contactId),enabled:!!i.contactId,staleTime:300*1e3}),R=D.filter(t=>t.type==="customer");x.useEffect(()=>{var t;l&&(o({contactId:l.contactId,contractId:l.contractId||"",driverId:l.driverId||"",scheduledDate:l.scheduledDate.split("T")[0],scheduledTime:l.scheduledTime||"",deliveryAddress:l.deliveryAddress||"",notes:l.notes||"",internalNotes:l.internalNotes||""}),v(((t=l.lineItems)==null?void 0:t.map(a=>({id:a.id,menuItemId:a.menuItemId||"",description:a.description,sku:a.sku||"",category:a.category||"",quantity:Number(a.quantity),unit:a.unit||"each",unitPrice:a.unitPrice,taxable:a.taxable,notes:a.notes||""})))||[]))},[l]),x.useEffect(()=>{if(!n&&i.contactId){const t=D.find(a=>a.id===i.contactId);if(t&&t.address){const a=[t.address,t.city,t.state,t.zipCode].filter(Boolean).join(", ");o(d=>({...d,deliveryAddress:a}))}}},[i.contactId,D,n]);const T=K({mutationFn:A.create,onSuccess:t=>{j.invalidateQueries({queryKey:["delivery-tickets"]}),p(`/dashboard/delivery-tickets/${t.id}`)}}),C=K({mutationFn:({ticketId:t,data:a})=>A.update(t,a),onSuccess:()=>{j.invalidateQueries({queryKey:["delivery-tickets"]}),j.invalidateQueries({queryKey:["delivery-ticket",h]}),p(`/dashboard/delivery-tickets/${h}`)}}),_=t=>{if(t.preventDefault(),!i.contactId||!i.scheduledDate)return;const a={...i,contractId:i.contractId||void 0,driverId:i.driverId||void 0,scheduledTime:i.scheduledTime||void 0,deliveryAddress:i.deliveryAddress||void 0,notes:i.notes||void 0,internalNotes:i.internalNotes||void 0,items:c.map(d=>({menuItemId:d.menuItemId||void 0,description:d.description,sku:d.sku||void 0,category:d.category||void 0,quantity:d.quantity,unit:d.unit||void 0,unitPrice:d.unitPrice,taxable:d.taxable,notes:d.notes||void 0}))};n?C.mutate({ticketId:h,data:a}):T.mutate(a)},F=t=>{t!==void 0?(N(t),m(c[t])):(N(null),m({menuItemId:"",description:"",sku:"",category:"",quantity:1,unit:"each",unitPrice:0,taxable:!0,notes:""})),w(!0)},E=()=>{w(!1),N(null)},H=t=>{if(t.preventDefault(),!(!r.description||r.quantity<1)){if(y!==null){const a=[...c];a[y]=r,v(a)}else v([...c,r]);E()}},U=()=>{I!==null&&(v(c.filter((t,a)=>a!==I)),k(null))},G=t=>{const a=S.find(q=>q.id===t);if(!a)return;const d=$.find(q=>q.menuItemId===t),Y=Math.round(d?Number(d.price)*100:Number(a.price)*100);m({...r,menuItemId:t,description:a.englishName,sku:a.sku||"",category:a.category||"",unit:a.unit||"each",unitPrice:Y,taxable:a.taxable})},g=t=>new Intl.NumberFormat("en-US",{style:"currency",currency:"USD"}).format(t/100),V=()=>c.reduce((t,a)=>t+a.quantity*a.unitPrice,0);return n&&Q?e.jsx("div",{className:"py-8 text-center text-muted-foreground",children:s("form.loadingTicket")}):n&&l&&l.status!=="DRAFT"?e.jsxs("div",{className:"py-12 text-center",children:[e.jsx("p",{className:"text-muted-foreground mb-4",children:s("form.onlyDraftEditable")}),e.jsx(u,{onClick:()=>p(`/dashboard/delivery-tickets/${h}`),children:s("form.viewTicket")})]}
1):L?e.jsxs("div",{className:"space-y-6 max-w-4xl",children:[e.jsxs("div",{children:[e.jsxs(fe,{to:"/dashboard/delivery-tickets",className:"text-muted-foreground hover:text-foreground",children:["â ",s("list.title")]}),e.jsx("h2",{className:"text-3xl font-bold mt-2",children:n?s("form.editTitle",{number:l==null?void 0:l.ticketNumber}):s("form.newTitle")})]}),e.jsxs("form",{onSubmit:_,className:"space-y-6",children:[e.jsxs("div",{className:"border rounded-lg p-4 bg-card space-y-4",children:[e.jsx("h3",{className:"font-semibold",children:s("form.ticketDetails")}),e.jsxs("div",{className:"grid md:grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium mb-1",children:s("form.customer")}),e.jsxs("select",{value:i.contactId,onChange:t=>o({...i,contactId:t.target.value}),className:"w-full px-3 py-2 border rounded-md",required:!0,disabled:n,children:[e.jsx("option",{value:"",children:s("form.selectCustomer")}),R.map(t=>e.jsxs("option",{value:t.id,children:[t.name,t.company?` (${t.company})`:""]},t.id))]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium mb-1",children:s("form.assignedDriver")}),e.jsxs("select",{value:i.driverId,onChange:t=>o({...i,driverId:t.target.value}),className:"w-full px-3 py-2 border rounded-md",children:[e.jsx("option",{value:"",children:s("table.unassigned")}),B.filter(t=>t.isActive).map(t=>e.jsxs("option",{value:t.id,children:[t.firstName," ",t.lastName]},t.id))]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium mb-1",children:s("form.scheduledDate")}),e.jsx("input",{type:"date",value:i.scheduledDate,onChange:t=>o({...i,scheduledDate:t.target.value}),className:"w-full px-3 py-2 border rounded-md",required:!0})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium mb-1",children:s("form.scheduledTime")}),e.jsx("input",{type:"text",value:i.scheduledTime||"",onChange:t=>o({...i,scheduledTime:t.target.value}),className:"w-full px-3 py-2 border rounded-md",placeholder:s("form.scheduledTimePlaceholder")})]}),e.jsxs("div",{className:"md:col-span-2",children:[e.jsx("label",{className:"block text-sm font-medium mb-1",children:s("detail.deliveryAddress")}),e.jsx("textarea",{value:i.deliveryAddress||"",onChange:t=>o({...i,deliveryAddress:t.target.value}),className:"w-full px-3 py-2 border rounded-md",rows:2})]})]})]}),e.jsxs("div",{className:"border rounded-lg p-4 bg-card space-y-4",children:[e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx("h3",{className:"font-semibold",children:s("form.items")}),e.jsx(u,{type:"button",variant:"outline",onClick:()=>F(),children:s("form.addItem")})]}),c.length===0?e.jsx("div",{className:"py-8 text-center text-muted-foreground border rounded-lg",children:s("form.noItems")}):e.jsxs("table",{className:"w-full",children:[e.jsx("thead",{className:"border-b",children:e.jsxs("tr",{className:"text-left",children:[e.jsx("th",{className:"pb-2 font-medium",children:s("form.itemHeader")}),e.jsx("th",{className:"pb-2 font-medium text-right",children:s("detail.qty")}),e.jsx("th",{className:"pb-2 font-medium text-right",children:s("detail.unitPrice")}),e.jsx("th",{className:"pb-2 font-medium text-right",children:s("table.total")}),e.jsx("th",{className:"pb-2 font-medium"})]})}),e.jsx("tbody",{children:c.map((t,a)=>e.jsxs("tr",{className:"border-b",children:[e.jsxs("td",{className:"py-2",children:[e.jsx("div",{children:t.description}),t.sku&&e.jsx("div",{className:"text-xs text-muted-foreground",children:s("detail.sku",{sku:t.sku})}),t.notes&&e.jsx("div",{className:"text-xs text-muted-foreground",children:t.notes})]}),e.jsxs("td",{className:"py-2 text-right",children:[t.quantity," ",t.unit]}),e.jsx("td",{className:"py-2 text-right",children:g(t.unitPrice)}),e.jsx("td",{className:"py-2 text-right font-medium",children:g(t.quantity*t.unitPrice)}),e.jsx("td",{className:"py-2",children:e.jsxs("div",{className:"flex gap-1 justify-end",children:[e.jsx(u,{type:"button",variant:"ghost",size:"sm",onClick:()=>F(a),children:s("actions.edit")}),e.jsx(u,{type:"button",variant:"ghost",size:"sm",className:"text-destructive",onClick:()=>k(a),children:s("form.remove")})]})})]},a))}),e.jsx("tfoot",{children:e.jsxs("tr",{className:"font-semibold",children:[e.jsx("td",{colSpan:3,className:"pt-4 text-right",children:s("form.subtotalLabel")}),e.jsx("td",{className:"pt-4 text-right",children:g(V())}),e.jsx("td",{})]})})]})]}),e.jsxs("div",{className:"border rounded-lg p-4 bg-card grid md:grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium mb-1",children:s("detail.customerNotes")}),e.jsx("textarea",{value:i.notes||"",onChange:t=>o({...i,notes:t.target.value}),className:"w-full px-3 py-2 border rounded-md",rows:3,placeholder:s("form.customerNotesPlaceholder")})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium mb-1",children:s("detail.internalNotes")}),e.jsx("textarea",{value:i.internalNotes||"",onChange:t=>o({...i,internalNotes:t.target.value}),className:"w-full px-3 py-2 border rounded-md",rows:3,placeholder:s("form.internalNotesPlaceholder")})]})]}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(u,{type:"button",variant:"outline",onClick:()=>p("/dashboard/delivery-tickets"),children:s("actions.cancel")}),e.jsx(u,{type:"submit",disabled:!i.contactId||!i.scheduledDate||c.length===0||T.isPending||C.isPending,children:T.isPending||C.isPending?s("form.saving"):s(n?"form.updateTicket":"form.createTicket")})]})]}),O&&e.jsx("div",{className:"fixed inset-0 bg-black/50 flex items-center justify-center z-50",children:e.jsxs("div",{className:"bg-background rounded-lg p-6 w-full max-w-md
1shadow-lg max-h-[90vh] overflow-y-auto",children:[e.jsx("h3",{className:"text-lg font-semibold mb-4",children:s(y!==null?"form.itemDialog.editTitle":"form.itemDialog.addTitle")}),e.jsxs("form",{onSubmit:H,className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium mb-1",children:s("form.itemDialog.selectCatalog")}),e.jsxs("select",{value:r.menuItemId,onChange:t=>G(t.target.value),className:"w-full px-3 py-2 border rounded-md",children:[e.jsx("option",{value:"",children:s("form.itemDialog.customItem")}),S.map(t=>e.jsxs("option",{value:t.id,children:[t.englishName," - ",g(Math.round(Number(t.price)*100))]},t.id))]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium mb-1",children:s("form.itemDialog.description")}),e.jsx("input",{type:"text",value:r.description,onChange:t=>m({...r,description:t.target.value}),className:"w-full px-3 py-2 border rounded-md",required:!0})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium mb-1",children:s("form.itemDialog.quantity")}),e.jsx("input",{type:"number",min:"1",step:"0.01",value:r.quantity,onChange:t=>m({...r,quantity:parseFloat(t.target.value)||1}),className:"w-full px-3 py-2 border rounded-md",required:!0})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium mb-1",children:s("form.itemDialog.unit")}),e.jsx("input",{type:"text",value:r.unit,onChange:t=>m({...r,unit:t.target.value}),className:"w-full px-3 py-2 border rounded-md",placeholder:s("form.itemDialog.unitPlaceholder")})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium mb-1",children:s("form.itemDialog.unitPrice")}),e.jsx("input",{type:"number",min:"0",value:r.unitPrice,onChange:t=>m({...r,unitPrice:parseInt(t.target.value)||0}),className:"w-full px-3 py-2 border rounded-md",required:!0}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:g(r.unitPrice)})]}),e.jsx("div",{children:e.jsxs("label",{className:"flex items-center gap-2",children:[e.jsx("input",{type:"checkbox",checked:r.taxable,onChange:t=>m({...r,taxable:t.target.checked}),className:"rounded"}),e.jsx("span",{className:"text-sm font-medium",children:s("form.itemDialog.taxable")})]})}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium mb-1",children:s("detail.notes")}),e.jsx("input",{type:"text",value:r.notes,onChange:t=>m({...r,notes:t.target.value}),className:"w-full px-3 py-2 border rounded-md",placeholder:s("form.itemDialog.notesPlaceholder")})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[e.jsx(u,{type:"button",variant:"outline",onClick:E,children:s("actions.cancel")}),e.jsx(u,{type:"submit",disabled:!r.description,children:s(y!==null?"form.itemDialog.update":"form.itemDialog.add")})]})]})]})}),e.jsx(X,{open:I!==null,onOpenChange:()=>k(null),children:e.jsxs(Z,{children:[e.jsxs(ee,{children:[e.jsx(te,{children:s("form.removeDialog.title")}),e.jsx(se,{children:s("form.removeDialog.description")})]}),e.jsxs(ae,{children:[e.jsx(ie,{children:s("actions.cancel")}),e.jsx(re,{onClick:U,className:"bg-destructive text-destructive-foreground hover:bg-destructive/90",children:s("form.remove")})]})]})})]}):e.jsxs("div",{className:"py-12 text-center",children:[e.jsx("p",{className:"text-muted-foreground mb-4",children:s("form.noPermission")}),e.jsx(u,{onClick:()=>p("/dashboard/delivery-tickets"),children:s("detail.backToTickets")})]})}export{Be 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.