1import{fO as A,g0 as E,es as r,e9 as e,K as j,bT as D,P as b,Q as N,S as T,T as P,bc as _,E as v,cO as $,f as S,i as F,l as M,m as B,j as L,b1 as O,k as H,h as I,g as z,eN as w}from"./index-D2yE0WFs.js";import{A as K}from"./AdminLayout-CuVddbA7.js";import{B as Q}from"./banknote-v5qEX3yY.js";import"./chart-column-Bi7_7fla.js";import"./trending-up-Bks0iuZo.js";import"./activity-D50GHC8x.js";import"./users-314H31wg.js";const l=i=>`$${Number(i).toFixed(2)}`;function X(){A("Expert Payouts");const{toast:i}=E(),[o,y]=r.useState([]),[n,x]=r.useState(!0),[p,u]=r.useState(null),[a,c]=r.useState(null),[f,m]=r.useState(""),[d,g]=r.useState(!1),h=async()=>{x(!0),u(null);const{data:t,error:s}=await w.rpc("admin_expert_payouts");s?u(s.message):y(t??[]),x(!1)};r.useEffect(()=>{h()},[]);const k=async()=>{if(!(!a||d)){g(!0);try{const{data:t,error:s}=await w.rpc("admin_mark_earnings_paid",{p_creator_id:a.creator_id,p_home_workplace_id:a.home_workplace_id,p_note:f||null,p_expected_total:Number(a.available_total)});if(s)throw s;i({title:`Marked paid: ${l((t==null?void 0:t.total_paid)??0)}`,description:`${(t==null?void 0:t.rows_paid)??0} earnings row(s). Record the actual transfer reference in your books.`}),c(null),m(""),await h()}catch(t){i({title:"Error",description:(t==null?void 0:t.message)??String(t),variant:"destructive"})}finally{g(!1)}}},C=o.reduce((t,s)=>t+Number(s.available_total),0);return e.jsx(K,{title:"Expert Payouts",children:e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold",children:"Expert Payouts"}),e.jsx("p",{className:"text-muted-foreground",children:"Payable balances by creator à home workplace (default routing collects at the workplace). Mark as paid AFTER the manual transfer; the note is the audit trail."})]}),e.jsxs(j,{variant:"outline",onClick:h,disabled:n,children:[e.jsx(D,{className:`h-4 w-4 mr-2 ${n?"animate-spin":""}`}),"Refresh"]})]}),p&&e.jsx(b,{className:"border-destructive/50",children:e.jsxs(N,{className:"p-4 text-sm text-destructive",children:[p,". If this mentions a missing function, paste migrations 20260726100000 and 20260726110000 first."]})}),e.jsxs(b,{children:[e.jsx(T,{className:"flex flex-row items-center justify-between space-y-0",children:e.jsxs(P,{className:"flex items-center gap-2 text-lg",children:[e.jsx(Q,{className:"h-5 w-5 text-emerald-600"}),n?"Loadingâ¦":`${l(C)} payable across ${o.filter(t=>t.available_total>0).length} balance(s)`]})}),e.jsx(N,{className:"p-0",children:n?e.jsx("div",{className:"flex items-center justify-center p-12",children:e.jsx(_,{className:"h-6 w-6 animate-spin text-muted-foreground"})}):o.length===0?e.jsx("div",{className:"p-12 text-center text-muted-foreground",children:"No earnings yet."}):e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b bg-muted/40 text-left",children:[e.jsx("th",{className:"p-3 font-semibold",children:"Creator"}),e.jsx("th",{className:"p-3 font-semibold",children:"Collects at"}),e.jsx("th",{className:"p-3 font-semibold text-right",children:"Available"}),e.jsx("th",{className:"p-3 font-semibold text-right",children:"On the way"}),e.jsx("th",{className:"p-3 font-semibold text-right",children:"Paid lifetime"}),e.jsx("th",{className:"p-3 font-semibold",children:"Flags"}),e.jsx("th",{className:"p-3"})]})}),e.jsx("tbody",{children:o.map((t,s)=>e.jsxs("tr",{className:"border-b last:border-b-0 hover:bg-muted/20",children:[e.jsxs("td",{className:"p-3",children:[e.jsx("div",{className:"font-medium",children:t.creator_name??"â"}),e.jsx("div",{className:"text-xs text-muted-foreground",children:t.creator_email??""})]}),e.jsx("td",{className:"p-3",children:t.home_workplace_name??"â"}),e.jsx("td",{className:"p-3 text-right font-mono font-semibold",children:l(t.available_total)}),e.jsx("td",{className:"p-3 text-right font-mono text-muted-foreground",children:l(t.pending_total)}),e.jsx("td",{className:"p-3 text-right font-mono text-muted-foreground",children:l(t.paid_total)}),e.jsx("td",{className:"p-3",children:e.jsxs("div",{className:"flex gap-1",children:[t.floored_rows>0&&e.jsxs(v,{variant:"destructive",title:"Months where running the worker cost more than it earned â pricing/fair-use alarm",children:[e.jsx($,{className:"h-3 w-3 mr-1"}),t.floored_rows," floored"]}),t.reversed_rows>0&&e.jsxs(v,{variant:"outline",className:"text-destructive border-destructive/40",children:[t.reversed_rows," reversed"]})]})}),e.jsx("td",{className:"p-3 text-right",children:e.jsx(j,{size:"sm",disabled:t.available_total<=0||!t.creator_id,title:t.creator_id?void 0:"Unattributed earnings (no creator on file) â investigate the rows before any payout",onClick:()=>{c(t),m("")},className:"bg-emerald-600 hover:bg-emerald-700 text-white",children:"Mark paid"})})]},`${t.creator_id}-${t.home_workplace_id}-${s}`))})]})})})]}),e.jsx(S,{open:!!a,onOpenChange:t=>{t||c(null)},children:e.jsxs(F,{children:[e.jsxs(M,{children:[e.jsxs(B,{children:["Mark ",l((a==null?void 0:a.available_total)??0)," as paid?"]}),e.jsxs(L,{children:[(a==null?void 0:a.creator_name)??"This creator"," · collects at ",(a==null?void 0:a.home_workplace_name)??"â"," ·"," ",(a==null?void 0:a.available_rows)??0," available row(s). Do this AFTER the transfer went out â it is the ledger's record that the money left."]})]}),e.jsx(O,{placeholder:"Transfer reference / note (recommended)",value:f,onChange:t=>m(t.target.value)}
1),e.jsxs(H,{children:[e.jsx(I,{disabled:d,children:"Cancel"}),e.jsxs(z,{onClick:t=>{t.preventDefault(),k()},disabled:d,children:[d?e.jsx(_,{className:"h-4 w-4 animate-spin mr-2"}):null,"Confirm paid"]})]})]})})]})})}export{X 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.