1import{a as R,j as e,c as B,u as F}from"./chunk-Cl_8PWDN.js";import{r,u as V}from"./chunk-Bzo9Tunx.js";import{u as g,A as K}from"./chunk-DUErHPP3.js";import"./chunk-DHVgiofg.js";import"./chunk-x6H3mB14.js";import{n as E,o as A,D as I,k as q,t as N,av as P,B as u}from"./index-5yA9CUzc.js";import{S as C,a as $,b as T,c as D,d as m}from"./chunk-Y6PacCAK.js";import{I as v}from"./chunk-BIJpGMWs.js";import{C as Q}from"./chunk-DWy7ntbN.js";import{T as L}from"./chunk-YrvLZ4fv.js";import{T as z,a as H,b as M,c as O}from"./chunk-HgkD2_Xl.js";import"./chunk-DaCLkmJg.js";import"./chunk-Dwamq1GJ.js";import"./chunk-BNWfZVZZ.js";import"./chunk-ClMkenV-.js";import"./chunk-me0vpg0v.js";import"./chunk-CD0_quAU.js";import"./chunk-C7gcEKhI.js";import"./chunk-mAyixyGg.js";import"./chunk-Cswqrut6.js";import"./chunk-DBTYFX8p.js";import"./chunk-CbG3gslG.js";import"./chunk-DOYwYJoF.js";import"./chunk-Cpk34r19.js";import"./chunk-sOoSo9Bm.js";import"./chunk-BeHpvERE.js";import"./chunk-PeZDclay.js";const j="/api/admin/commission-reconciliation";async function G(s,t={},n){return s.get(`${j}/bookings/`,t,void 0,{signal:n})}async function W(s,t){return s.get(`${j}/statements/`,{},void 0,{signal:t})}async function U(s,t,n){return s.get(`${j}/statements/${t}/line-items/`,{},void 0,{signal:n})}async function J(s,t,n,a){return s.post(`${j}/bookings/${t}/resolve/`,n,void 0,{headers:{"If-Match":a}})}async function X(s,t,n,a){return s.patch(`${j}/bookings/${t}/expected-commission/`,n,void 0,{headers:{"If-Match":a}})}async function Y(s,t,n,a){return s.post(`${j}/bookings/${t}/dispute/`,n,void 0,{headers:{"If-Match":a}})}function Z(s={}){const t=g();return R({queryKey:["admin","commission-reconciliation","bookings",s],queryFn:({signal:n})=>G(t,s,n)})}function w({status:s}){const t={unflagged:"bg-gray-100 text-gray-700",flagged:"bg-yellow-100 text-yellow-800",investigating:"bg-blue-100 text-blue-800",resolved:"bg-emerald-100 text-emerald-800",disputed:"bg-purple-100 text-purple-800"};return e.jsx("span",{className:`rounded-full px-2 py-0.5 text-xs font-medium ${t[s]}`,children:s})}function k(s){if(s==null)return"â";const t=Number(s);return isFinite(t)?`${t<0?"-":""}$${Math.abs(t).toFixed(2)}`:s}function ee({onRowClick:s}){const[t,n]=r.useState({status:"needs_attention"}),{data:a,isLoading:o,error:c}=Z(t);return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[e.jsxs(C,{value:t.status??"needs_attention",onValueChange:i=>n(l=>({...l,status:i})),children:[e.jsx($,{className:"w-[180px]",children:e.jsx(T,{})}),e.jsxs(D,{children:[e.jsx(m,{value:"needs_attention",children:"Needs attention"}),e.jsx(m,{value:"resolved",children:"Resolved"}),e.jsx(m,{value:"disputed",children:"Disputed"}),e.jsx(m,{value:"all",children:"All"})]})]}),e.jsxs(C,{value:t.discrepancy_type??"all_types",onValueChange:i=>n(l=>({...l,discrepancy_type:i==="all_types"?void 0:i})),children:[e.jsx($,{className:"w-[200px]",children:e.jsx(T,{placeholder:"Discrepancy type"})}),e.jsxs(D,{children:[e.jsx(m,{value:"all_types",children:"All types"}),e.jsx(m,{value:"amount_mismatch",children:"Amount mismatch"}),e.jsx(m,{value:"missing_commission",children:"Missing commission"}),e.jsx(m,{value:"none",children:"No discrepancy"})]})]}),e.jsx(v,{placeholder:"Search booking ref / advisor",value:t.search??"",onChange:i=>n(l=>({...l,search:i.target.value})),className:"w-[260px]"}),e.jsxs("label",{className:"flex items-center gap-2 text-sm",children:[e.jsx(Q,{checked:!!t.include_resolved,onCheckedChange:i=>n(l=>({...l,include_resolved:!!i}))}),"Include resolved/disputed"]})]}),o&&e.jsx("div",{className:"text-sm text-gray-500",children:"Loadingâ¦"}),c&&e.jsx("div",{className:"text-sm text-red-600",children:"Failed to load."}),a&&a.results.length===0&&e.jsx("div",{className:"rounded border bg-gray-50 p-6 text-center text-sm text-gray-500",children:"No bookings need attention."}),a&&a.results.length>0&&e.jsx("div",{className:"overflow-x-auto rounded border",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-gray-50 text-left",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-3 py-2",children:"Booking"}),e.jsx("th",{className:"px-3 py-2",children:"Cruise line"}
1),e.jsx("th",{className:"px-3 py-2",children:"Advisor"}),e.jsx("th",{className:"px-3 py-2",children:"Sailed"}),e.jsx("th",{className:"px-3 py-2",children:"Expected"}),e.jsx("th",{className:"px-3 py-2",children:"Received"}),e.jsx("th",{className:"px-3 py-2",children:"Î"}),e.jsx("th",{className:"px-3 py-2",children:"Type"}),e.jsx("th",{className:"px-3 py-2",children:"Status"}),e.jsx("th",{className:"px-3 py-2",children:"Assignee"})]})}),e.jsx("tbody",{children:a.results.map(i=>e.jsxs("tr",{className:"cursor-pointer border-t hover:bg-gray-50",onClick:()=>s(i),children:[e.jsx("td",{className:"px-3 py-2 font-mono",children:i.supplier_booking_ref}),e.jsx("td",{className:"px-3 py-2",children:i.supplier_name||"â"}),e.jsx("td",{className:"px-3 py-2",children:i.advisor?.email??"â"}),e.jsx("td",{className:"px-3 py-2",children:i.departure_date??"â"}),e.jsx("td",{className:"px-3 py-2",children:k(i.expected_commission)}),e.jsx("td",{className:"px-3 py-2",children:k(i.received_commission)}),e.jsx("td",{className:`px-3 py-2 ${i.diff&&Number(i.diff)<0?"text-red-600":i.diff&&Number(i.diff)>0?"text-amber-700":i.diff?"text-emerald-700":""}`,children:k(i.diff)}),e.jsx("td",{className:"px-3 py-2 text-xs",children:i.discrepancy_type??"â"}),e.jsx("td",{className:"px-3 py-2",children:e.jsx(w,{status:i.reconciliation_status})}),e.jsx("td",{className:"px-3 py-2",children:i.reconciliation_assignee?.email??"â"})]},i.booking_id))})]})})]})}function se(){const s=g();return R({queryKey:["admin","commission-reconciliation","statements"],queryFn:({signal:t})=>W(s,t)})}function te(s){const t=g();return R({queryKey:["admin","commission-reconciliation","statements",s,"line-items"],queryFn:({signal:n})=>s?U(t,s,n):Promise.reject("no id"),enabled:!!s})}function b(s){return s?`$${Number(s).toFixed(2)}`:"â"}function ie(){const[s,t]=r.useState(null),{data:n,isLoading:a,error:o}=se(),c=te(s);return e.jsxs("div",{className:"space-y-4",children:[a&&e.jsx("div",{className:"text-sm text-gray-500",children:"Loadingâ¦"}),o&&e.jsx("div",{className:"text-sm text-red-600",children:"Failed to load."}),n&&n.results.length===0&&e.jsx("div",{className:"rounded border bg-gray-50 p-6 text-center text-sm text-gray-500",children:"No statements yet."}),n&&n.results.length>0&&e.jsx("div",{className:"overflow-x-auto rounded border",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-gray-50 text-left",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-3 py-2",children:"Statement"}),e.jsx("th",{className:"px-3 py-2",children:"Cruise line"}),e.jsx("th",{className:"px-3 py-2",children:"Date"}),e.jsx("th",{className:"px-3 py-2",children:"Total"}),e.jsx("th",{className:"px-3 py-2",children:"Reconciled"}),e.jsx("th",{className:"px-3 py-2",children:"Discrepancies"})]})}),e.jsx("tbody",{children:n.results.map(i=>e.jsxs("tr",{className:"cursor-pointer border-t hover:bg-gray-50",onClick:()=>t(i.remittance_id),children:[e.jsx("td",{className:"px-3 py-2 font-mono text-xs",children:i.message_id}),e.jsx("td",{className:"px-3 py-2",children:i.cruise_line}),e.jsx("td",{className:"px-3 py-2",children:i.statement_date??"â"}),e.jsx("td",{className:"px-3 py-2",children:b(i.deposit_amount)}),e.jsx("td",{className:"px-3 py-2",children:b(i.reconciled_total)}),e.jsx("td",{className:"px-3 py-2",children:i.discrepancy_count>0?e.jsx("span",{className:"rounded bg-yellow-100 px-2 py-0.5 text-xs text-yellow-800",children:i.discrepancy_count}):"â"})]},i.remittance_id))})]})}),e.jsx(E,{open:!!s,onOpenChange:i=>!i&&t(null),children:e.jsxs(A,{className:"max-w-3xl",children:[e.jsx(I,{children:e.jsx(q,{children:"Statement line items"})}),c.isLoading&&e.jsx("div",{className:"text-sm text-gray-500",children:"Loadingâ¦"}),c.data&&e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-gray-50 text-left",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-3 py-2",children:"Booking ref"}),e.jsx("th",{className:"px-3 py-2",children:"Found?"}),e.jsx("th",{className:"px-3 py-2",children:"Expected"}),e.jsx("th",{className:"px-3 py-2",children:"Received"}),e.jsx("th",{className:"px-3 py-2",children:"Î"}),e.jsx("th",{className:"px-3 py-2",children:"Status"})]})}),e.jsx("tbody",{children:c.data.line_items.map((i,l)=>e.jsxs("tr",{className:"border-t",children:[e.jsx("td",{className:"px-3 py-2 font-mono",children:i.booking_ref}),e.jsx("td",{className:"px-3 py-2",children:i.booking_found?"â":"â orphan"}),e.jsx("td",{className:"px-3 py-2",children:b(i.expected_commission)}),e.jsx("td",{className:"px-3 py-2",children:b(i.received_commission)}),e.jsx("td",{className:"px-3 py-2",children:b(i.diff)}),e.jsx("td",{className:"px-3 py-2 text-xs",children:i.status})]},`${i.booking_ref}-${l}`))})]})})]})})]})}
1function ne(){const s=g(),t=B();return F({mutationFn:({bookingId:n,status:a,note:o,ifMatch:c})=>J(s,n,{status:a,note:o},c),onSuccess:()=>t.invalidateQueries({queryKey:["admin","commission-reconciliation"]}),onError:n=>{n?.response?.status===409?N.error("This booking changed since you opened it. Refresh and try again."):N.error(n?.message??"Failed to update")}})}function ae(){const s=g(),t=B();return F({mutationFn:({bookingId:n,to:a,cc:o,subject:c,body:i,ifMatch:l})=>Y(s,n,{to:a,cc:o,subject:c,body:i},l),onSuccess:()=>t.invalidateQueries({queryKey:["admin","commission-reconciliation"]})})}function ce(s){const t=s.expected_commission??"n/a",n=s.received_commission??"n/a",a=s.diff??"n/a";return s.discrepancy_type==="missing_commission"?`Hello ${s.supplier_name||"team"}, 2 3We're still awaiting commission payment for the following booking. The sail date has passed but no remittance has appeared: 4 5 ⢠Booking ref: ${s.supplier_booking_ref} 6 ⢠Guest: ${s.guest_name||"â"} 7 ⢠Sail date: ${s.departure_date??"n/a"} 8 ⢠Expected: $${t} 9 10Could you confirm whether this booking is queued for an upcoming statement, or let us know if there's an issue we should address? 11 12Thank you, 13Cruise Pirate Reconciliation Team 14`:`Hello ${s.supplier_name||"team"}, 15 16We're following up on a commission discrepancy for the following booking on the agent statement ${s.remittance_message_id?`(${s.remittance_message_id})`:""}: 17 18 ⢠Booking ref: ${s.supplier_booking_ref} 19 ⢠Guest: ${s.guest_name||"â"} 20 ⢠Sail date: ${s.departure_date??"n/a"} 21 ⢠Expected: $${t} 22 ⢠Received: $${n} 23 ⢠Difference: $${a} 24 25Could you confirm the correct commission and let us know whether a top-up will be issued, or send any context that explains the adjustment? 26 27Thank you, 28Cruise Pirate Reconciliation Team 29`}function le({row:s,open:t,onClose:n}){const[a,o]=r.useState(""),[c,i]=r.useState(""),[l,h]=r.useState(""),[x,y]=r.useState(""),p=ae();r.useEffect(()=>{if(t){o("");const d=s.advisor?.email??"";i(d);const _=s.discrepancy_type==="missing_commission"?"Awaiting commission":"Commission discrepancy";h(`${_}: booking ${s.supplier_booking_ref}`),y(ce(s))}},[t,s]);const f=!!a.trim()&&!!l.trim()&&!!x.trim()&&!p.isPending;return e.jsx(E,{open:t,onOpenChange:d=>!d&&n(),children:e.jsxs(A,{className:"max-w-2xl",children:[e.jsx(I,{children:e.jsxs(q,{children:["Send dispute email â ",s.supplier_booking_ref]})}),e.jsxs("div",{className:"space-y-3 text-sm",children:[e.jsxs("label",{className:"block",children:[e.jsx("span",{className:"mb-1 block",children:"To"}),e.jsx(v,{value:a,onChange:d=>o(d.target.value),placeholder:"[email protected]"})]}),!a.trim()&&e.jsx("p",{className:"-mt-2 text-xs text-amber-700",children:"No dispute email known for this cruise line â enter manually."}),e.jsxs("label",{className:"block",children:[e.jsx("span",{className:"mb-1 block",children:"CC"}),e.jsx(v,{value:c,onChange:d=>i(d.target.value),placeholder:"[email protected]"}),e.jsx("p",{className:"mt-1 text-xs text-gray-500",children:"Comma-separated. Default = booking's advisor."})]}),e.jsxs("label",{className:"block",children:[e.jsx("span",{className:"mb-1 block",children:"Subject"}),e.jsx(v,{value:l,onChange:d=>h(d.target.value)})]}),e.jsxs("label",{className:"block",children:[e.jsx("span",{className:"mb-1 block",children:"Body"}),e.jsx(L,{value:x,onChange:d=>y(d.target.value),rows:14,className:"font-mono text-xs"})]}),p.isError&&e.jsxs("div",{className:"rounded border border-red-300 bg-red-50 p-3 text-sm text-red-800",children:["Failed to send: ",p.error?.message??"unknown error",". The discrepancy is still flagged."]})]}),e.jsxs(P,{children:[e.jsx(u,{variant:"outline",onClick:n,children:"Cancel"}),e.jsx(u,{disabled:!f,onClick:()=>{const d=c.split(",").map(_=>_.trim()).filter(Boolean);p.mutate({bookingId:s.booking_id,to:a.trim(),cc:d,subject:l.trim(),body:x,ifMatch:s.updated_at},{onSuccess:()=>n()})},children:p.isPending?"Sendingâ¦":"Send"})]})]})})}function oe(){const s=g(),t=B();return F({mutationFn:({bookingId:n,newValue:a,reason:o,ifMatch:c})=>X(s,n,{new_value:a,reason:o},c),onSuccess:()=>
29t.invalidateQueries({queryKey:["admin","commission-reconciliation"]}),onError:n=>{n?.response?.status===409?N.error("Booking changed since you opened it. Refresh and try again."):N.error(n?.message??"Failed to override")}})}function re({row:s,open:t,onClose:n}){const[a,o]=r.useState(s.expected_commission??""),[c,i]=r.useState(""),l=oe(),h=Number(a)>0&&c.trim().length>=10&&!l.isPending;return e.jsx(E,{open:t,onOpenChange:x=>!x&&n(),children:e.jsxs(A,{children:[e.jsx(I,{children:e.jsxs(q,{children:["Override expected commission â ",s.supplier_booking_ref]})}),e.jsxs("div",{className:"space-y-4",children:[e.jsx("div",{className:"text-sm",children:e.jsxs("div",{children:["Current expected: ",e.jsxs("span",{className:"font-mono",children:["$",s.expected_commission??"â"]})]})}),e.jsxs("label",{className:"block text-sm",children:[e.jsx("span",{className:"mb-1 block",children:"New expected ($)"}),e.jsx(v,{type:"number",step:"0.01",min:"0.01",value:a,onChange:x=>o(x.target.value)})]}),e.jsxs("label",{className:"block text-sm",children:[e.jsx("span",{className:"mb-1 block",children:"Reason (min 10 chars)"}),e.jsx(L,{value:c,onChange:x=>i(x.target.value),rows:3})]})]}),e.jsxs(P,{children:[e.jsx(u,{variant:"outline",onClick:n,children:"Cancel"}),e.jsx(u,{disabled:!h,onClick:()=>l.mutate({bookingId:s.booking_id,newValue:a,reason:c.trim(),ifMatch:s.updated_at},{onSuccess:()=>n()}),children:l.isPending?"Savingâ¦":"Save override"})]})]})})}function S(s){if(s==null)return"â";const t=Number(s);return`${t<0?"-":""}$${Math.abs(t).toFixed(2)}`}function de({row:s,onClose:t}){const n=V(),[a,o]=r.useState(s.reconciliation_status),[c,i]=r.useState(""),[l,h]=r.useState(!1),[x,y]=r.useState(!1),p=ne();return e.jsxs("aside",{className:"fixed right-0 top-0 z-50 flex h-full w-full max-w-[480px] flex-col overflow-y-auto border-l bg-white shadow-xl",children:[e.jsxs("header",{className:"flex items-center justify-between border-b px-4 py-3",children:[e.jsxs("div",{children:[e.jsxs("h2",{className:"text-base font-semibold",children:[s.supplier_name||"â"," ",s.supplier_booking_ref]}),e.jsxs("p",{className:"text-xs text-gray-500",children:[s.guest_name||"â"," · ",s.advisor?.email??"â"," · sailed ",s.departure_date??"â"]})]}),e.jsx(u,{variant:"ghost",size:"sm",onClick:t,children:"â"})]}),e.jsxs("section",{className:"space-y-2 border-b px-4 py-3 text-sm",children:[e.jsx("div",{className:"text-xs uppercase tracking-wide text-gray-500",children:"Discrepancy"}),e.jsxs("div",{children:["Type: ",e.jsx("span",{className:"font-mono",children:s.discrepancy_type??"â"})]}),e.jsxs("div",{className:"flex items-center justify-between",children:["Expected: ",e.jsx("span",{className:"font-mono",children:S(s.expected_commission)}),e.jsx(u,{size:"sm",variant:"outline",onClick:()=>h(!0),children:"Override"})]}),e.jsxs("div",{children:["Received: ",e.jsx("span",{className:"font-mono",children:S(s.received_commission)})]}),e.jsxs("div",{children:["Difference: ",e.jsx("span",{className:"font-mono",children:S(s.diff)})]}),s.remittance_message_id&&e.jsxs("div",{className:"text-xs text-gray-500",children:["Source: ",s.remittance_message_id]})]}),e.jsxs("section",{className:"space-y-3 border-b px-4 py-3 text-sm",children:[e.jsx("div",{className:"text-xs uppercase tracking-wide text-gray-500",children:"Reconciliation"}),e.jsxs(C,{value:a,onValueChange:f=>o(f),children:[e.jsx($,{children:e.jsx(T,{})}),e.jsxs(D,{children:[e.jsx(m,{value:"flagged",children:"Flagged"}),e.jsx(m,{value:"investigating",children:"Investigating"}),e.jsx(m,{value:"resolved",children:"Resolved"}),e.jsx(m,{value:"disputed",children:"Disputed"})]})]}),e.jsx(L,{placeholder:"Resolution note (optional unless resolving)",value:c,onChange:f=>i(f.target.value),rows:3}),e.jsx(u,{disabled:p.isPending,onClick:()=>p.mutate({bookingId:s.booking_id,status:a,note:c,ifMatch:s.updated_at},{onSuccess:()=>t()}),children:p.isPending?"Savingâ¦":"Save changes"})]}),e.jsxs("section",{className:"space-y-2 border-b px-4 py-3",children:[e.jsx("div",{className:"text-xs uppercase tracking-wide text-gray-500",children:"Actions"}),e.jsx(u,{variant:"outline",disabled:s.dispute_within_dedupe_window,title:s.dispute_within_dedupe_window?"A dispute was sent for this booking in the last 30 days":void 0,onClick:()=>
29y(!0),children:"ð§ Send dispute email"}),e.jsx(u,{variant:"outline",onClick:()=>n(`/bookings?bookingId=${s.booking_id}`),children:"ð Open booking page"})]}),e.jsx(re,{row:s,open:l,onClose:()=>h(!1)}),e.jsx(le,{row:s,open:x,onClose:()=>y(!1)})]})}function me(){const[s,t]=r.useState("bookings"),[n,a]=r.useState(null);return e.jsxs("div",{className:"space-y-4 p-4",children:[e.jsx("h1",{className:"text-xl font-semibold",children:"Commission Reconciliation"}),e.jsxs(z,{value:s,onValueChange:o=>t(o),children:[e.jsxs(H,{children:[e.jsx(M,{value:"bookings",children:"Bookings"}),e.jsx(M,{value:"statements",children:"Statements"})]}),e.jsx(O,{value:"bookings",children:e.jsx(ee,{onRowClick:a})}),e.jsx(O,{value:"statements",children:e.jsx(ie,{})})]}),n&&e.jsx(de,{row:n,onClose:()=>a(null)})]})}function Oe(){return e.jsx(K,{children:e.jsx(me,{})})}export{Oe 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.