1import{r as o,j as e,t as p}from"./index-DSugmHUS.js";import{C as q,d as I,a as se,b as te,c as ae}from"./card-Di9E2Ks8.js";import{B as D}from"./button-m1DExL1R.js";import{B as C}from"./badge-B8atV_Tq.js";import{i as P,m as re,o as le,p as y,q as m,r as ne,s as l}from"./Index-BPe71JiC.js";import{D as U,a as V,b as H,c as $,d as W,e as ie}from"./dialog-CIarVAiX.js";import{D as de,a as ce,E as oe,b as me,e as T}from"./use-permissions-DY9JGyl_.js";import{S as z}from"./use-forms-ul9uhWcB.js";import{T as xe}from"./textarea-l-vHJGTB.js";import{L as n}from"./label-vTM0e4c2.js";import{supabase as A}from"./client-ChDlC2U2.js";import{a as G}from"./auth-identity-DzeYk6GR.js";import{a as ue,l as J}from"./alert-dialog-B3LhExJT.js";import{g as he}from"./app-settings-store-xfeo64yl.js";import{o as pe,k as je}from"./use-roles-D4XvQNKE.js";import{F as ge}from"./filter-jg2JGHy4.js";import{E as ve}from"./eye-DKj9YflC.js";import{C as K}from"./circle-check-VuDJsLoV.js";import"./UIPreferencesProvider-z_xzDbof.js";import"./auth-signout-intent-Cwv85joy.js";import"./AppFooter-5aU6EpQT.js";import"./SniperLogo-CIrxtFra.js";import"./use-states-CkirmXom.js";import"./index-PNcq8FZ2.js";import"./index-Be40SeTS.js";import"./use-theme-3WYHr7HG.js";import"./minus-CS-1NInk.js";import"./use-countries-3rmBWzkp.js";import"./products-data-Cr26dmrZ.js";import"./input-D9uL_PFp.js";import"./format-date-OQ-J4T8_.js";import"./schemas-dF-40InO.js";import"./orders-data-BfL7ogtZ.js";import"./forms-data-C22rYxwX.js";import"./conversion-api-DgR-Bv2N.js";import"./forms-sync-AQymZ2vX.js";import"./duplicate-block-message-3iGWS-XC.js";const fe=[{value:"all",label:"All open + closed"},{value:"discrepancy",label:"Open"},{value:"acknowledged",label:"Acknowledged"},{value:"resolved",label:"Resolved"}];function as(){const{branches:B}=ue(),[x,F]=o.useState([]),[Q,_]=o.useState(!0),[j,X]=P("StockDiscrepanciesPage","statusFilter","discrepancy"),[g,Y]=P("StockDiscrepanciesPage","branchFilter","all"),[t,R]=o.useState(null),[r,v]=o.useState(null),[f,u]=o.useState(""),[k,w]=o.useState(null),S=async()=>{_(!0);const s=await he();if(!s){F([]),_(!1);return}const{data:a}=await A.from("stock_audits").select("*").eq("owner_id",s).in("status",["discrepancy","acknowledged","resolved"]).order("performed_at",{ascending:!1}).limit(500);F(a||[]),_(!1)};o.useEffect(()=>{S()},[]);const E=o.useMemo(()=>x.filter(s=>!(j!=="all"&&s.status!==j||g!=="all"&&s.branch_id!==g)),[x,j,g]),N=o.useMemo(()=>{const s=Date.now()-2592e6;return{open:x.filter(a=>a.status==="discrepancy").length,acknowledged:x.filter(a=>a.status==="acknowledged").length,resolved30:x.filter(a=>a.status==="resolved"&&new Date(a.resolved_at||a.performed_at).getTime()>=s).length,varianceUnits:x.filter(a=>a.status!=="resolved").reduce((a,d)=>a+Math.abs(d.variance||0),0)}},[x]),Z=async s=>{var a,d,h,i;w(s.id);try{const{data:c}=await G(),b=((d=(a=c.user)==null?void 0:a.user_metadata)==null?void 0:d.name)||((h=c.user)==null?void 0:h.email)||null,{error:L}=await A.from("stock_audits").update({status:"acknowledged",acknowledged_by:((i=c.user)==null?void 0:i.id)||null,acknowledged_by_name:b,acknowledged_at:new Date().toISOString()}).eq("id",s.id);if(L)throw L;await J("Acknowledge Discrepancy","inventory",s.id,{product:s.product_name,variance:s.variance}),p({title:"Acknowledged"}),await S()}catch(c){p({title:"Failed to acknowledge",description:c==null?void 0:c.message,variant:"destructive"})}finally{w(null)}},ee=async()=>{var s,a,d,h;if(r){if(!f.trim()){p({title:"Add a resolution note",variant:"destructive"});return}w(r.id);try{const{data:i}=await G(),c=((a=(s=i.user)==null?void 0:s.user_metadata)==null?void 0:a.name)||((d=i.user)==null?void 0:d.email)||null,{error:b}=await A.from("stock_audits").update({status:"resolved",resolved_by:((h=i.user)==null?void 0:h.id)||null,resolved_by_name:c,resolved_at:new Date().toISOString(),resolution_notes:f.trim()}).eq("id",r.id);if(b)throw b;await J("Resolve Discrepancy","inventory",r.id,{product:r.product_name,variance:r.variance,resolution:f.trim()}),p({title:"Resolved"}),v(null),u(""),await S()}catch(i){p({title:"Failed to resolve",description:i==null?void 0:i.message,variant:"destructive"})}finally{w(null)}}},M=s=>
1{var a;return((a=B.find(d=>d.id===s))==null?void 0:a.name)||"â"},O=s=>s==="resolved"?e.jsxs(C,{variant:"secondary",className:"gap-1",children:[e.jsx(K,{className:"h-3 w-3"})," Resolved"]}):s==="acknowledged"?e.jsx(C,{variant:"default",className:"gap-1",children:"Acknowledged"}):e.jsxs(C,{variant:"destructive",className:"gap-1",children:[e.jsx(je,{className:"h-3 w-3"})," Open"]});return e.jsxs("div",{className:"flex-1 overflow-y-auto",children:[e.jsxs("div",{className:"max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8 space-y-6",children:[e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx("div",{className:"h-10 w-10 rounded-md bg-primary/10 flex items-center justify-center",children:e.jsx(pe,{className:"h-5 w-5 text-primary"})}),e.jsxs("div",{children:[e.jsx("p",{className:"mono text-[10px] uppercase tracking-[0.2em] text-muted-foreground mb-2",children:"Fraud Check · Discrepancies"}),e.jsx("h1",{className:"text-3xl font-semibold tracking-tight text-gradient-steel",children:"Stock Discrepancies"}),e.jsx("p",{className:"text-sm text-muted-foreground mt-2 max-w-2xl",children:"Variances from blind stock audits land here. Acknowledge to show you're investigating, resolve once the count is reconciled or written off."})]})]}),e.jsx("div",{className:"grid grid-cols-2 lg:grid-cols-4 gap-3",children:[{label:"Open",value:N.open,tone:"destructive"},{label:"Acknowledged",value:N.acknowledged,tone:"default"},{label:"Resolved (30d)",value:N.resolved30,tone:"muted"},{label:"Unresolved variance (units)",value:N.varianceUnits,tone:"muted"}].map(s=>e.jsx(q,{children:e.jsxs(I,{className:"py-4",children:[e.jsx("p",{className:"text-[11px] uppercase tracking-widest text-muted-foreground",children:s.label}),e.jsx("p",{className:`text-2xl font-semibold mono mt-1 ${s.tone==="destructive"?"text-destructive":""}`,children:s.value})]})},s.label))}),e.jsxs(q,{children:[e.jsxs(se,{className:"flex flex-row items-center justify-between gap-3 flex-wrap",children:[e.jsxs("div",{children:[e.jsx(te,{className:"text-base",children:"Discrepancy log"}),e.jsx(ae,{children:"Latest 500 audits across all branches."})]}),e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx(ge,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("div",{className:"w-48",children:e.jsx(z,{value:j,onChange:X,options:fe,placeholder:"Status"})}),e.jsx("div",{className:"w-56",children:e.jsx(z,{value:g,onChange:Y,options:[{value:"all",label:"All branches"},...B.map(s=>({value:s.id,label:s.name}))],placeholder:"Branch"})})]})]}),e.jsx(I,{className:"overflow-x-auto",children:e.jsxs(re,{children:[e.jsx(le,{children:e.jsxs(y,{children:[e.jsx(m,{children:"When"}),e.jsx(m,{children:"By"}),e.jsx(m,{children:"Branch"}),e.jsx(m,{children:"Product"}),e.jsx(m,{className:"text-right",children:"Counted"}),e.jsx(m,{className:"text-right",children:"Expected"}),e.jsx(m,{className:"text-right",children:"Variance"}),e.jsx(m,{children:"Status"}),e.jsx(m,{className:"w-10"})]})}),e.jsx(ne,{children:Q?e.jsx(y,{children:e.jsx(l,{colSpan:9,className:"text-center text-muted-foreground py-6",children:"Loadingâ¦"})}):E.length===0?e.jsx(y,{children:e.jsx(l,{colSpan:9,className:"text-center text-muted-foreground py-6",children:"No discrepancies in this view."})}):E.map(s=>e.jsxs(y,{className:s.status==="discrepancy"?"bg-destructive/5":"",children:[e.jsx(l,{className:"whitespace-nowrap text-xs",children:new Date(s.performed_at).toLocaleString()}),e.jsx(l,{className:"text-xs",children:s.performed_by_name||"â"}),e.jsx(l,{className:"text-xs",children:M(s.branch_id)}),e.jsx(l,{className:"text-xs",children:s.product_name||s.product_id||"â"}),e.jsx(l,{className:"text-right mono",children:s.counted_qty}),e.jsx(l,{className:"text-right mono",children:s.expected_qty}),e.jsx(l,{className:`text-right mono font-semibold ${s.status!=="resolved"?"text-destructive":""}`,children:s.variance>0?`+${s.variance}`:s.variance}),e.jsx(l,{children:O(s.status)}),e.jsx(l,{children:e.jsxs(de,{children:[e.jsx(ce,{asChild:!0,children:e.jsx(D,{variant:"ghost",size:"icon",className:"h-8 w-8",disabled:k===s.id,children:e.jsx(oe,{className:"h-4 w-4"})})}),e.jsxs(me,{align:"end",children:[e.jsxs(T,{onClick:()=>R(s),children:[e.jsx(ve,{className:"h-4 w-4 mr-2"})," View details"]}
1),s.status==="discrepancy"&&e.jsx(T,{onClick:()=>Z(s),children:"Acknowledge"}),s.status!=="resolved"&&e.jsxs(T,{onClick:()=>{v(s),u("")},children:[e.jsx(K,{className:"h-4 w-4 mr-2"})," Mark resolved"]})]})]})})]},s.id))})]})})]})]}),e.jsx(U,{open:!!t,onOpenChange:s=>!s&&R(null),children:e.jsxs(V,{className:"max-w-lg",children:[e.jsxs(H,{children:[e.jsx($,{children:"Discrepancy details"}),e.jsx(W,{children:"Full audit record."})]}),t&&e.jsxs("div",{className:"space-y-2 text-sm",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx(n,{className:"text-muted-foreground text-xs",children:"When"}),e.jsx("p",{children:new Date(t.performed_at).toLocaleString()})]}),e.jsxs("div",{children:[e.jsx(n,{className:"text-muted-foreground text-xs",children:"By"}),e.jsx("p",{children:t.performed_by_name||"â"})]}),e.jsxs("div",{children:[e.jsx(n,{className:"text-muted-foreground text-xs",children:"Branch"}),e.jsx("p",{children:M(t.branch_id)})]}),e.jsxs("div",{children:[e.jsx(n,{className:"text-muted-foreground text-xs",children:"Product"}),e.jsx("p",{children:t.product_name||"â"})]}),e.jsxs("div",{children:[e.jsx(n,{className:"text-muted-foreground text-xs",children:"Counted"}),e.jsx("p",{className:"mono",children:t.counted_qty})]}),e.jsxs("div",{children:[e.jsx(n,{className:"text-muted-foreground text-xs",children:"Expected"}),e.jsx("p",{className:"mono",children:t.expected_qty})]}),e.jsxs("div",{children:[e.jsx(n,{className:"text-muted-foreground text-xs",children:"Variance"}),e.jsx("p",{className:"mono text-destructive font-semibold",children:t.variance>0?`+${t.variance}`:t.variance})]}),e.jsxs("div",{children:[e.jsx(n,{className:"text-muted-foreground text-xs",children:"Status"}),e.jsx("p",{children:O(t.status)})]})]}),t.notes&&e.jsxs("div",{children:[e.jsx(n,{className:"text-muted-foreground text-xs",children:"Staff notes"}),e.jsx("p",{children:t.notes})]}),t.acknowledged_at&&e.jsxs("div",{className:"border-t pt-2",children:[e.jsx(n,{className:"text-muted-foreground text-xs",children:"Acknowledged"}),e.jsxs("p",{children:[t.acknowledged_by_name||"â"," · ",new Date(t.acknowledged_at).toLocaleString()]})]}),t.resolved_at&&e.jsxs("div",{className:"border-t pt-2",children:[e.jsx(n,{className:"text-muted-foreground text-xs",children:"Resolved"}),e.jsxs("p",{children:[t.resolved_by_name||"â"," · ",new Date(t.resolved_at).toLocaleString()]}),t.resolution_notes&&e.jsxs("p",{className:"mt-1 text-muted-foreground italic",children:['"',t.resolution_notes,'"']})]})]})]})}),e.jsx(U,{open:!!r,onOpenChange:s=>{s||(v(null),u(""))}
1,children:e.jsxs(V,{children:[e.jsxs(H,{children:[e.jsx($,{children:"Mark resolved"}),e.jsx(W,{children:"Briefly note how this variance was reconciled (e.g. recount matched, wrote off as damage, transferred from another branch)."})]}),e.jsx(xe,{value:f,onChange:s=>u(s.target.value),rows:4,placeholder:"Resolution noteâ¦"}),e.jsxs(ie,{children:[e.jsx(D,{variant:"outline",onClick:()=>{v(null),u("")},children:"Cancel"}),e.jsx(D,{onClick:ee,disabled:k===(r==null?void 0:r.id),children:k===(r==null?void 0:r.id)?"Savingâ¦":"Mark resolved"})]})]})})]})}export{as as StockDiscrepanciesPage};
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.