1import{r as m,j as e}from"./react-vendor-CblAmzNW.js";import{u as Q,N as B,O as Y,G as f,Q as _,d as u,R as k,E as S,b as P,U as G,V as U,i as R,X as V,Y as A,Z as E,_ as z,$ as H,f as $,M as X,a0 as Z,a1 as J,a2 as L,a3 as W,a4 as ee,a5 as ae,a6 as se,D as q,A as M,C as I,F,a7 as te,a8 as ne,a9 as le}from"./index-D4FJNhFp.js";import{a as ie,C as re,p as D,L as oe}from"./payouts-CE6IiFsB.js";import{u as g,A as de,P as w,F as N,C as ce}from"./ui-CY0uQJqe.js";import{F as me}from"./file-pen-line-D7NkAQbl.js";import{R as ue}from"./refresh-cw-CMSrG8re.js";import{P as xe}from"./pencil-BvEdWSg-.js";import{R as pe}from"./rotate-ccw-CEVRvs_M.js";import"./clock-DcLnzABQ.js";import"./circle-C4ScT7qT.js";function C(){const a=R();return()=>Promise.all([a.invalidateQueries({queryKey:V()}),a.invalidateQueries({queryKey:A().slice(0,1)}),a.invalidateQueries({queryKey:E(0).slice(0,1)})])}function T({lines:a,showCollaborator:d,selectable:l,selected:i,onToggle:c,onRowAction:r}){return a.length?e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full min-w-[820px] text-sm",children:[e.jsx("thead",{className:"t-meta text-left text-ink-3",children:e.jsxs("tr",{className:"border-b border-line",children:[l&&e.jsx("th",{className:"w-8 px-2 py-2"}),[...d?["Collaborator"]:[],"Sale","Beat / tier","Split","Base","Share","Reversed","Status","Reference",""].map((s,n)=>e.jsx("th",{className:"px-3 py-2 font-normal",children:s},n))]})}),e.jsx("tbody",{className:"divide-y divide-line",children:a.map(s=>e.jsxs("tr",{"data-testid":`row-ledger-${s.id}`,className:"align-top",children:[l&&e.jsx("td",{className:"px-2 py-2.5",children:(s.status==="manual_pending"||s.status==="owed")&&e.jsx("input",{type:"checkbox","aria-label":`Select ledger line ${s.id}`,checked:i?.has(s.id)??!1,onChange:()=>c?.(s.id),"data-testid":`checkbox-ledger-${s.id}`})}),d&&e.jsxs("td",{className:"px-3 py-2.5",children:[e.jsx("span",{className:"font-medium",children:s.collaboratorName}),e.jsx("span",{className:"block t-small text-ink-3",children:s.collaboratorEmail})]}),e.jsxs("td",{className:"px-3 py-2.5 whitespace-nowrap",children:[e.jsxs("a",{className:"font-mono hover:text-gold",href:`/admin/orders/${s.orderId}`,children:["#",s.orderId]}),e.jsx("span",{className:"block t-small text-ink-3",children:$(s.paidAt)})]}),e.jsxs("td",{className:"px-3 py-2.5",children:[s.beatTitle??"â",e.jsx("span",{className:"block t-small text-ink-3",children:s.tier??""})]}),e.jsx("td",{className:"px-3 py-2.5 font-mono",children:D(s.percent)}),e.jsx("td",{className:"px-3 py-2.5 font-mono text-ink-2",children:u(s.baseCents)}),e.jsx("td",{className:"px-3 py-2.5 font-mono font-semibold",children:u(s.amountCents)}),e.jsx("td",{className:"px-3 py-2.5 font-mono text-ink-2",children:s.reversalCents?`â${u(s.reversalCents)}`:"â"}),e.jsxs("td",{className:"px-3 py-2.5",children:[e.jsx(oe,{status:s.status}),s.failureReason&&e.jsx("span",{className:"mt-1 block max-w-[260px] t-small text-bad","data-testid":`text-ledger-failure-${s.id}`,children:s.failureReason})]}),e.jsxs("td",{className:"px-3 py-2.5 max-w-[200px] break-all t-small text-ink-3",children:[s.stripeTransferId??s.manualReference??"â",s.stripeReversalId&&e.jsx("span",{className:"block",children:s.stripeReversalId})]}),e.jsx("td",{className:"px-3 py-2.5 text-right",children:r?.(s)})]},s.id))})]})}):e.jsx(P,{title:"No ledger lines",body:"Shares appear here after a paid sale of a beat with collaborators."})}function K({l:a}){const d=g(),l=C(),i=ne(),c=le(),[r,s]=m.useState("");return a.status!=="reversal_failed"?null:e.jsxs("div",{className:"flex flex-wrap justify-end gap-1.5",children:[a.stripeTransferId&&e.jsxs("button",{className:"btn btn-line btn-sm",disabled:i.isPending,"data-testid":`button-retry-reversal-${a.id}`,onClick:()=>i.mutate({id:a.id},{onSuccess:async n=>{await l(),n.status==="reversed"?d.ok("Reversal succeeded"):d.fail(new Error(n.failureReason??"Still failing"),"Reversal still failing")},onError:n=>d.fail(n)}),children:[i.isPending?e.jsx(f,{className:"animate-spin"}):e.jsx(pe,{})," Retry reversal"]}),e.jsx(ce,{title:"Mark recovered",confirmLabel:"Mark recovered",body:e.jsxs("div",{className:"space-y-3",children:[e.jsxs("p",{children:["Confirm you recovered ",u(a.reversalCents)," from ",a.collaboratorName," outside Str
1ipe."]}),e.jsx("input",{className:"input",placeholder:"Reference (e.g. PayPal txn, bank ref)",value:r,onChange:n=>s(n.target.value),"data-testid":"input-recovered-reference"})]}),onConfirm:()=>{if(!r.trim()){d.fail(new Error("Enter a reference."));return}c.mutate({id:a.id,data:{reference:r.trim()}},{onSuccess:async()=>{await l(),d.ok("Marked recovered")},onError:n=>d.fail(n)})},trigger:n=>e.jsxs("button",{className:"btn btn-quiet btn-sm",onClick:()=>{s(""),n()},"data-testid":`button-mark-recovered-${a.id}`,children:[e.jsx(L,{})," Mark recovered"]})})]})}function he({c:a,onClose:d}){const l=se(a?.id??0,{query:{enabled:!!a,queryKey:E(a?.id??0)}});return e.jsx(q,{open:!!a,onOpenChange:i=>!i&&d(),children:e.jsxs(M,{className:"max-w-5xl border-line-strong bg-surface-2",children:[e.jsx(I,{children:e.jsxs(F,{children:["Ledger â ",a?.name]})}),a&&e.jsxs("p",{className:"t-small text-ink-2",children:["Earned ",u(a.totals.earnedCents)," · Paid ",u(a.totals.paidCents)," · Owed ",u(a.totals.owedCents),a.totals.toRecoverCents?` · To recover ${u(a.totals.toRecoverCents)}`:""]}),e.jsx("div",{className:"max-h-[65vh] overflow-y-auto",children:l.isLoading?e.jsx(k,{count:4}):l.isError?e.jsx(S,{onRetry:()=>l.refetch()}):e.jsx(T,{lines:l.data??[],onRowAction:i=>e.jsx(K,{l:i})})})]})})}function be({c:a,onClose:d}){const l=g(),i=C(),c=te(),[r,s]=m.useState(""),[n,p]=m.useState(""),[h,b]=m.useState("stripe");if(m.useEffect(()=>{a&&(s(a.name),p(a.notes??""),b(a.status==="manual"?"manual":"stripe"))},[a]),!a)return null;const v=()=>c.mutate({id:a.id,data:{name:r.trim(),notes:n.trim()||null,payoutMethod:h}},{onSuccess:async()=>{await i(),l.ok("Collaborator saved"),d()},onError:x=>l.fail(x)});return e.jsx(q,{open:!0,onOpenChange:x=>!x&&d(),children:e.jsxs(M,{className:"border-line-strong bg-surface-2",children:[e.jsx(I,{children:e.jsxs(F,{children:["Edit ",a.email]})}),e.jsxs("div",{className:"space-y-4",children:[e.jsx(N,{label:"Name",htmlFor:"collab-name",children:e.jsx("input",{id:"collab-name",className:"input",value:r,onChange:x=>s(x.target.value),"data-testid":"input-collab-name"})}),e.jsx(N,{label:"Payout method",hint:"Manual moves unpaid shares to the manual-pay list. Switching back to Stripe re-queues unpaid Stripe-order shares as owed.",children:e.jsxs("select",{className:"input",value:h,onChange:x=>b(x.target.value),"data-testid":"select-payout-method",children:[e.jsx("option",{value:"stripe",children:"Stripe Connect (automatic transfers)"}),e.jsx("option",{value:"manual",children:"Manual (paid outside Stripe)"})]})}),e.jsx(N,{label:"Private notes",htmlFor:"collab-notes",children:e.jsx("textarea",{id:"collab-notes",className:"input min-h-[90px]",value:n,onChange:x=>p(x.target.value),"data-testid":"input-collab-notes"})}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx("button",{className:"btn btn-line",onClick:d,children:"Cancel"}),e.jsxs("button",{className:"btn btn-gold",disabled:c.isPending||!r.trim(),onClick:v,"data-testid":"button-save-collab",children:[c.isPending&&e.jsx(f,{className:"animate-spin"}),"Save"]})]})]})]})})}function je({c:a,onLedger:d,onEdit:l}){const i=g(),c=C(),r=z(),s=H();return e.jsxs("tr",{"data-testid":`row-collab-${a.id}`,className:"align-top",children:[e.jsxs("td",{className:"px-3 py-3",children:[e.jsx("span",{className:"font-medium",children:a.name}),a.notes&&e.jsx("span",{className:"block max-w-[200px] truncate t-small text-ink-3",title:a.notes,children:a.notes})]}),e.jsxs("td",{className:"px-3 py-3 text-ink-2 break-all",children:[a.email,e.jsx("span",{className:"block t-small text-ink-3",children:a.invitedAt?`Invited ${$(a.invitedAt)}`:"Not invited yet"})]}),e.jsxs("td",{className:"px-3 py-3",children:[e.jsx(re,{status:a.status}),a.country&&e.jsx("span",{className:"ml-1 t-small text-ink-3",children:a.country}),a.stripeStatusDetail&&e.jsx("span",{className:"mt-1 block max-w-[220px] t-small text-ink-3",children:a.stripeStatusDetail})]}),e.jsx("td",{className:"px-3 py-3",children:a.beats.length?e.jsx("ul",{className:"space-y-0.5",children:a.beats.map(n=>e.jsxs("li",{className:"t-small",children:[e.jsx("a",{className:"hover:text-gold",href:`/admin/beats/${n.beatId}`,children:n.title})," ",e.jsx("span",{className:"font-mono text-ink-2",children:D(n.percent)}
1)," ",e.jsx("span",{className:n.agreementAccepted?"text-ok":"text-ink-3",children:n.agreementAccepted?"· signed":"· unsigned"})]},n.beatId))}):e.jsx("span",{className:"t-small text-ink-3",children:"No beats"})}),e.jsx("td",{className:"px-3 py-3 font-mono",children:u(a.totals.earnedCents)}),e.jsx("td",{className:"px-3 py-3 font-mono",children:u(a.totals.paidCents)}),e.jsxs("td",{className:"px-3 py-3 font-mono","data-testid":`text-owed-${a.id}`,children:[u(a.totals.owedCents),a.totals.toRecoverCents>0&&e.jsxs("span",{className:"block t-small text-bad",children:["Recover ",u(a.totals.toRecoverCents)]})]}),e.jsx("td",{className:"px-3 py-3",children:e.jsxs("div",{className:"flex flex-wrap justify-end gap-1.5",children:[e.jsxs("button",{className:"btn btn-line btn-sm",onClick:d,"data-testid":`button-ledger-${a.id}`,children:[e.jsx(me,{})," Ledger"]}),e.jsxs("button",{className:"btn btn-quiet btn-sm",disabled:r.isPending,"data-testid":`button-resend-invite-${a.id}`,onClick:()=>r.mutate({id:a.id},{onSuccess:async()=>{await c(),i.ok("Invite sent",a.email)},onError:n=>i.fail(n,"Invite not sent")}),children:[r.isPending?e.jsx(f,{className:"animate-spin"}):e.jsx(X,{})," Resend invite"]}),a.stripeAccountId&&a.status!=="manual"&&e.jsxs("button",{className:"btn btn-quiet btn-sm",disabled:s.isPending,"data-testid":`button-refresh-${a.id}`,onClick:()=>s.mutate({id:a.id},{onSuccess:async n=>{await c(),i.ok("Stripe status refreshed",n.status)},onError:n=>i.fail(n,"Refresh failed")}),children:[s.isPending?e.jsx(f,{className:"animate-spin"}):e.jsx(ue,{})," Refresh"]}),e.jsx("button",{className:"btn btn-quiet btn-sm",onClick:l,"aria-label":`Edit ${a.name}`,"data-testid":`button-edit-${a.id}`,children:e.jsx(xe,{})})]})})]})}const ve=[{v:"attention",label:"Needs attention"},{v:"manual_pending",label:"Manual payouts due"},{v:"owed",label:"Owed (waiting for Stripe)"},{v:"reversal_failed",label:"Failed reversals"},{v:"transferred",label:"Paid via Stripe"},{v:"paid_manually",label:"Paid manually"},{v:"reversed",label:"Reversed"},{v:"all",label:"All"}];function fe(){const a=g(),d=C(),[l,i]=m.useState("attention"),c=Z({status:l},{query:{queryKey:A({status:l})}}),[r,s]=m.useState(new Set),[n,p]=m.useState(""),h=J();m.useEffect(()=>s(new Set),[l]);const b=c.data??[],v=m.useMemo(()=>b.filter(o=>r.has(o.id)).reduce((o,j)=>o+j.amountCents-j.reversalCents,0),[b,r]),x=o=>s(j=>{const t=new Set(j);return t.has(o)?t.delete(o):t.add(o),t});return e.jsxs(w,{title:"Payout ledger",desc:"Manual-pay list, owed shares and failed reversals. Tick manual payouts you've sent, then Mark paid with your reference.",action:e.jsx("select",{className:"input w-auto",value:l,onChange:o=>i(o.target.value),"aria-label":"Filter ledger","data-testid":"select-ledger-filter",children:ve.map(o=>e.jsx("option",{value:o.v,children:o.label},o.v))}),children:[r.size>0&&e.jsxs("div",{className:"mb-3 flex flex-wrap items-center gap-2 rounded-lg border border-gold/40 bg-gold/5 p-3",children:[e.jsxs("span",{className:"t-small",children:[r.size," selected · ",u(v)]}),e.jsx("input",{className:"input w-64",placeholder:"Payment reference (required)",value:n,onChange:o=>p(o.target.value),"data-testid":"input-mark-paid-reference"}),e.jsxs("button",{className:"btn btn-gold btn-sm",disabled:!n.trim()||h.isPending,"data-testid":"button-mark-paid",onClick:()=>h.mutate({data:{ids:[...r],reference:n.trim()}},{onSuccess:async o=>{await d(),s(new Set),p(""),a.ok(`Marked ${o.count} paid`)},onError:o=>a.fail(o)}),children:[h.isPending?e.jsx(f,{className:"animate-spin"}):e.jsx(L,{})," Mark paid"]})]}),c.isLoading?e.jsx(k,{count:4}):c.isError?e.jsx(S,{onRetry:()=>c.refetch()}):e.jsx(T,{lines:b,showCollaborator:!0,selectable:!0,selected:r,onToggle:x,onRowAction:o=>e.jsx(K,{l:o})})]})}function ge(){const a=g(),d=R(),l=W(),i=ee(),[c,r]=m.useState("");return m.useEffect(()=>{l.data&&r(l.data.body)},[l.data]),e.jsx(w,{title:"Split agreement template",desc:e.jsxs(e.Fragment,{children:["Version ",e.jsx("span",{className:"font-mono","data-testid":"text-agreement-version",children:l.data?.version??"â¦"}),". Each beat's agreement adds the beat, collaborator, percentage, calculation method and payout timing above this text. Saving a change creates a new version; collaborators who accepted an older version are asked to accept again."]}),children:l.isLoading?e.jsx(k,{count:2}):l.isError?e.jsx(S,{onRetry:()=>l.refetch()}):e.jsxs("div",{className:"space-y-3",children:[e.jsx("textarea",{className:"input min-h-[220px] font-mono text-xs",value:c,onChange:s=>r(s.target.value),"aria-label":"Agreement template","data-testid":"input-agreement-body"}),e.jsx("div",{className:"flex justify-end",children:e.jsxs("button",{className:"btn btn-gold btn-sm",disabled:i.isPending||c.trim()===l.data?.body.trim()||c.trim().length<20,"data-testid":"button-save-agreement",onClick:()=>i.mutate({data:{body:c}},{onSuccess:async s=>{await d.invalidateQueries({queryKey:ae()}),a.ok("Agreement saved",`Version ${s.version}`)},onError:s=>a.fail(s)}),children:[i.isPending&&e.jsx(f,{className:"animate-spin"}),"Save new version"]})})]})})}function $e(){Q("Collaborators & Payouts â Admin");const a=g(),d=C(),l=B(),i=Y(),[c,r]=m.useState(null),[s,n]=m.useState(null),p=new Date().getFullYear(),[h,b]=m.useState(p),[v,x]=m.useState(""),o=l.data??[],j=t=>o.reduce((y,O)=>y+O.totals[t],0);return e.jsxs("div",{children:[e.jsx(de,{title:"Collaborators & Payouts",desc:"Splits are paid from the net of each sale by Stripe Connect transfer, or manually where Stripe can't pay out.",action:e.jsxs("button",{className:"btn btn-line btn-sm",disabled:i.isPending,"data-testid":"button-process-owed",onClick:()=>i.mutate(void 0,{onSuccess:async t=>{await d(),a.ok("Owed shares processed",`${t.transferred} transferred · ${t.failed} failed · ${t.cancelled} closed`)},onError:t=>a.fail(t)}),children:[i.isPending?e.jsx(f,{className:"animate-spin"}):e.jsx(_,{})," Process owed now"]})}),e.jsx("div",{className:"mb-6 grid gap-3 sm:grid-cols-3",children:[["Earned",j("earnedCents")],["Paid",j("paidCents")],["Owed",j("owedCents")]].map(([t,
1y])=>e.jsxs("div",{className:"card p-4",children:[e.jsxs("p",{className:"t-meta text-ink-3",children:[t," (all collaborators)"]}),e.jsx("p",{className:"mt-1 font-mono text-xl font-semibold","data-testid":`text-total-${t.toLowerCase()}`,children:u(y)})]},t))}),e.jsx(w,{title:"Collaborators",className:"mb-6",children:l.isLoading?e.jsx(k,{count:3}):l.isError?e.jsx(S,{onRetry:()=>l.refetch()}):o.length===0?e.jsx(P,{icon:e.jsx(G,{className:"size-5"}),title:"No collaborators yet",body:"Add a collaborator and their split in a beat's editor (Pricing & publish) or with the bulk âAdd collaboratorâ action. They're invited by email when the beat is published."}):e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full min-w-[980px] text-sm",children:[e.jsx("thead",{className:"t-meta text-left text-ink-3",children:e.jsx("tr",{className:"border-b border-line",children:["Name","Email","Status","Beats","Earned","Paid","Owed",""].map((t,y)=>e.jsx("th",{className:"px-3 py-2 font-normal",children:t},y))})}),e.jsx("tbody",{className:"divide-y divide-line",children:o.map(t=>e.jsx(je,{c:t,onLedger:()=>r(t),onEdit:()=>n(t)},t.id))})]})})}),e.jsx("div",{className:"mb-6",children:e.jsx(fe,{})}),e.jsx(w,{title:"Year-end export",desc:"One CSV line per share, by sale date in the store timezone. Includes Stripe transfer/reversal IDs and manual references.",className:"mb-6",children:e.jsxs("div",{className:"flex flex-wrap items-end gap-3",children:[e.jsx(N,{label:"Year",htmlFor:"csv-year",children:e.jsx("select",{id:"csv-year",className:"input w-28",value:h,onChange:t=>b(Number(t.target.value)),"data-testid":"select-csv-year",children:[0,1,2,3].map(t=>e.jsx("option",{value:p-t,children:p-t},t))})}),e.jsx(N,{label:"Collaborator",htmlFor:"csv-collab",children:e.jsxs("select",{id:"csv-collab",className:"input w-64",value:v,onChange:t=>x(t.target.value),"data-testid":"select-csv-collab",children:[e.jsx("option",{value:"",children:"All collaborators"}),o.map(t=>e.jsxs("option",{value:t.id,children:[t.name," (",t.email,")"]},t.id))]})}),e.jsxs("a",{className:"btn btn-line btn-sm",href:ie(`admin/payouts/year-end.csv?year=${h}${v?`&collaboratorId=${v}`:""}`),download:!0,"data-testid":"link-year-end-csv",children:[e.jsx(U,{})," Download CSV"]})]})}),e.jsx(ge,{}),e.jsx(he,{c,onClose:()=>r(null)}),e.jsx(be,{c:s,onClose:()=>n(null)})]})}export{$e 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.