1import{r as c,j as e,s as se,f8 as G,b7 as F,fu as Q,fv as U,bx as ae,X as k,b6 as j,fg as _,ag as K,L as te,p as q,aL as re,d$ as ie}from"./ui-vendor-Cm2ZkKHv.js";import{u as ne,a as oe}from"./table-vendor-C2kIG4hh.js";import{r as le,i as de,a3 as O,bR as b,aZ as ce,a_ as me,B as g,c as N,I as ue,S as xe,k as pe,l as he,m as fe,o as S,ap as u,aG as ge,bK as je,bL as be,bM as Ne,bN as ve,p as we,aq as R,ar as C}from"./index-DVu_w4ho.js";import{T as Te,a as ye,b as f}from"./tabs-oAd-VtsP.js";import{P as ke,a as Se,b as Ce}from"./popover-QQzRiqgZ.js";import{C as V}from"./calendar-BJoiCAY7.js";import{E as Fe}from"./ErrorEstado-CyWG3vtM.js";import{T as _e}from"./TourButton-BlXtTvkT.js";import{i as qe}from"./inventoryTraceabilityService-xV5WrQyi.js";import"./radix-vendor-CtEXKD-D.js";import"./chart-vendor-b_bTPWgQ.js";import"./react-vendor-DVzxkp4A.js";import"./addMonths-BlgZAcU2.js";import"./addWeeks-BhiivLsl.js";import"./normalizeInterval-CTYAF7TN.js";import"./endOfWeek-Brb8Q0-r.js";import"./endOfYear-CFEGJXjW.js";import"./getYear-F7sRW9ay.js";import"./isBefore-BTDkiDGL.js";import"./isSameDay-B7TuMg_-.js";import"./isSameMonth-DRNsUc-z.js";import"./startOfMonth-DuKQNzLK.js";const Re=[{element:'[data-tour="trazabilidad-raiz"]',popover:{title:"1 · La historia completa de un producto",description:'Cada entrada y cada salida de un producto, en orden, con su documento y su fecha. Es la pantalla que responde <strong>"¿por qué el sistema dice que hay tres?"</strong>.<br><br>Se lee de arriba hacia abajo: cada lÃnea explica el saldo que quedó después de ella.',side:"bottom",align:"center"}},{element:'[data-tour="trazabilidad-raiz"]',popover:{title:"2 · Lotes y vencimientos",description:"Para lo que vence âalimentos, medicamentos, quÃmicosâ el sistema sigue el <strong>lote</strong>: de qué compra vino y en qué venta salió.<br><br>Si un proveedor avisa de un lote con problema, esto es lo que dice a qué clientes se les vendió sin tener que revisar factura por factura.",side:"bottom",align:"center"}},{element:'[data-tour="trazabilidad-raiz"]',popover:{title:"3 · Cuando falta algo, se empieza por aquÃ",description:"Antes de registrar un ajuste conviene mirar esta pantalla: muchas veces el faltante no es un faltante sino un <strong>traslado que nadie registró</strong> o una venta que se anuló mal.<br><br>Ajustar sin mirar el historial esconde el problema en vez de resolverlo.",side:"bottom",align:"center"}}],ze={entrada:{label:"Entrada",color:"bg-emerald-500/15 text-emerald-700 border-emerald-200",icon:Q},salida:{label:"Salida",color:"bg-rose-500/15 text-rose-700 border-rose-200",icon:U},transferencia_entrada:{label:"Transf. Entrada",color:"bg-blue-500/15 text-blue-700 border-blue-200",icon:F},transferencia_salida:{label:"Transf. Salida",color:"bg-amber-500/15 text-amber-700 border-amber-200",icon:F},ajuste:{label:"Ajuste",color:"bg-purple-500/15 text-purple-700 border-purple-200",icon:ie}};function z({tipo:s}){const t=ze[s]||{label:s,color:"bg-muted text-muted-foreground border-border",icon:j},a=t.icon;return e.jsxs("span",{className:N("inline-flex items-center gap-1 rounded-full border px-2 py-0.5 text-xs font-medium",t.color),children:[e.jsx(a,{className:"w-3 h-3"}),t.label]})}function v(s){var t,a;return((t=s.producto)==null?void 0:t.nombre)||((a=s.insumo)==null?void 0:a.nombre)||"â"}function L(s){return s.producto?"producto":s.insumo?"insumo":"desconocido"}function I(s){var r,n,i,x;const t=((r=s.origen_sucursal)==null?void 0:r.nombre)||((n=s.origen_bodega)==null?void 0:n.nombre),a=((i=s.destino_sucursal)==null?void 0:i.nombre)||((x=s.destino_bodega)==null?void 0:x.nombre);return t&&a?{origen:t,destino:a}:a?{origen:null,destino:a}:t?{origen:t,destino:null}:{origen:null,destino:null}}const X={search:"",itemType:"all",dateFrom:void 0,dateTo:void 0};function Le({filters:s,onChange:t}){const[a,r]=c.useState(!1),n=s.itemType!=="all"||s.dateFrom||s.dateTo;return e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[e.jsxs("div",{className:"relative flex-1 min-w-[160px]",children:[e.jsx(ae,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground"}),e.jsx(ue,{placeholder:"Buscar producto, insumo, notaâ¦",value:s.search,onChange:i=>t({...s,search:i.target.value}),className:"pl-9 h-9"}),s.search&&e.jsx("button",{onClick:()=>t({...s,search:""}),className:"absolute right-2 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground",children:e.jsx(k,{className:"w-3.5 h-3.5"})})]}),e.jsxs(xe,{value:s.itemType,onValueChange:i=>t({...s,itemType:i}),children:[e.jsx(pe,{className:"h-9 w-36",children:e.jsx(he,{})}),e.jsxs(fe,{children:[e.jsx(S,{value:"all",children:"Todos los tipos"}),e.jsxs(S,{value:"producto",children:[e.jsx(j,{className:"inline w-3.5 h-3.5 mr-1"}),"Productos"]}),e.jsxs(S,{value:"insumo",children:[e.jsx(_,{className:"inline w-3.5 h-3.5 mr-1"}),"Insumos"]})]})]}),e.jsxs(ke,{open:a,onOpenChange:r,children:[e.jsx(Se,{asChild:!0,children:e.jsxs(g,{variant:"outline",size:"sm",className:N("h-9 gap-1.5",n&&"border-primary text-primary"),children:[e.jsx(K,{className:"w-3.5 h-3.5"}),s.dateFrom&&s.dateTo?`${u(s.dateFrom,"dd/MM")} â ${u(s.dateTo,"dd/MM")}`:s.dateFrom?`Desde ${u(s.dateFrom,"dd/MM")}`:"Fecha"]})}),e.jsxs(Ce,{className:"w-auto p-0",align:"end",children:[e.jsxs("div",{className:"flex gap-1 p-3 border-b text-xs text-muted-foreground items-center",children:[e.jsx(K,{className:"w-3.5 h-3.5"})," Selecciona el rango de fechas"]}),e.jsxs("div",{className:"flex gap-4 p-3",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium mb-1 text-muted-foreground",children:"Desde"}),e.jsx(V,{mode:"single",selected:s.dateFrom,onSelect:i=>t({...s,dateFrom:i}),className:"pointer-events-auto"})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium mb-1 text-muted-foreground",children:"Hasta"}),e.jsx(V,{mode:"single",selected:s.dateTo,onSelect:i=>t({...s,dateTo:i}),className:"pointer-events-auto"})]})]}),(s.dateFrom||s.dateTo)&&e.jsx("div",{className:"border-t px-3 py-2",children:e.jsxs(g,{variant:"ghost",size:"sm",className:"w-full h-8 text-xs",onClick:()=>{t({...s,dateFrom:void 0,dateTo:void 0}),r(!1)},children:[e.jsx(k,{className:"w-3 h-3 mr-1"})," Limpiar fechas"]})})]})]}),(s.search||n)&&e.jsx(g,{variant:"ghost",size:"sm",className:"h-9 px-2 text-muted-foreground",onClick:()=>t(X),children:e.jsx(k,{className:"w-3.5 h-3.5"})})]})}function Ie({entry:s,onClick:t}){var n;const a=I(s),r=L(s);return e.jsxs("tr",{className:"border-b hover:bg-muted/50 cursor-pointer transition-colors",onClick:t,children:[e.jsx("td",{className:"px-4 py-3 text-sm text-muted-foreground whitespace-nowrap",children:u(b(s.fecha),"dd/MM/yyyy HH:mm",{locale:R})}),e.jsx("td",{className:"px-4 py-3",children:e.jsx(z,{tipo:s.tipo})}),e.jsx("td",{className:"px-4 py-3",children:e.jsxs("div",{className:"flex items-center gap-1.5",children:[r==="producto"?e.jsx(j,{className:"w-3.5 h-3.5 text-muted-foreground shrink-0"}):e.jsx(_,{className:"w-3.5 h-3.5 text-muted-foreground shrink-0"}),e.jsx("span",{className:"text-sm font-medium",children:v(s)}),((n=s.variante)==null?void 0:n.nombre)&&e.jsxs("span",{className:"text-xs text-muted-foreground",children:["(",s.variante.nombre,")"]})]})}),e.jsx("td",{className:"px-4 py-3 text-sm font-semibold tabular-nums text-right",children:s.cantidad}),e.jsx("td",{className:"px-4 py-3",children:a.origen||a.destino?e.jsxs("div",{className:"flex items-center gap-1 text-xs text-muted-foreground",children:[a.origen&&e.jsx("span",{className:"bg-muted rounded px-1.5 py-0.5",children:a.origen}),a.origen&&a.destino&&e.jsx(q,{className:"w-3 h-3 shrink-0"}),a.destino&&e.jsx("span",{className:"bg-muted rounded px-1.5 py-0.5",children:a.destino})]}):e.jsx("span",{className:"text-xs text-muted-foreground",children:"â"})}),e.jsx("td",{className:"px-4 py-3 text-xs text-muted-foreground max-w-[160px] truncate",children:s.referencia_id||s.nota||"â"}),e.jsx("td",{className:"px-4 py-3 text-right",children:e.jsx(re,{className:"w-4 h-4 text-muted-foreground ml-auto"})})]})}function De({entry:s,onClick:t}){const a=I(s),r=L(s);return e.jsxs("div",{className:"bg-card border rounded-lg p-4 space-y-2.5 cursor-pointer active:scale-[0.99] transition-transform",onClick:t,children:[e.jsxs("div",{className:"flex items-start justify-between gap-2",children:[e.jsxs("div",{className:"flex items-center gap-1.5 min-w-0",children:[r==="producto"?e.jsx(j,{className:"w-4 h-4 text-muted-foreground shrink-0"}):e.jsx(_,{className:"w-4 h-4 text-muted-foreground shrink-0"}),e.jsx("span",{className:"font-semibold text-sm truncate",children:v(s)})]}),e.jsx(z,{tipo:s.tipo})]}),e.jsxs("div",{className:"flex items-center justify-between text-xs text-muted-foreground",children:[e.jsx("span",{children:u(b(s.fecha),"dd/MM/yyyy HH:mm",{locale:R})}),e.jsxs("span",{className:"font-semibold text-foreground tabular-nums",children:["Cant: ",s.cantidad]})]}),(a.origen||a.destino)&&e.jsxs("div",{className:"flex items-center gap-1 text-xs",children:[a.origen&&e.jsx("span",{className:"bg-muted rounded px-1.5 py-0.5",children:a.origen}),a.origen&&a.destino&&e.jsx(q,{className:"w-3 h-3 text-muted-foreground"}),a.destino&&e.jsx("span",{className:"bg-muted rounded px-1.5 py-0.5",children:a.destino})]}),(s.referencia_id||s.nota)&&e.jsx("p",{className:"text-xs text-muted-foreground truncate",children:s.referencia_id||s.nota})]})}function Me({entry:s,open:t,onClose:a}){if(!s)return null;const r=I(s);return e.jsx(je,{open:t,onOpenChange:n=>!n&&a(),children:e.jsxs(be,{className:"max-h-[85vh]",children:[e.jsx(Ne,{children:e.jsxs(ve,{className:"flex items-center gap-2",children:[e.jsx(z,{tipo:s.tipo}),e.jsx("span",{children:v(s)})]})}),e.jsx(we,{className:"flex-1 px-4 pb-8",children:e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsx(m,{label:"Fecha",value:u(b(s.fecha),"dd/MM/yyyy HH:mm",{locale:R})}),e.jsx(m,{label:"Cantidad",value:String(s.cantidad),highlight:!0}),s.saldo_resultante!==void 0&&e.jsx(m,{label:"Saldo resultante",value:String(s.saldo_resultante)}),e.jsx(m,{label:"Tipo Ãtem",value:L(s)==="producto"?"Producto":"Insumo"})]}),(r.origen||r.destino)&&e.jsxs(e.Fragment,{children:[e.jsx(C,{}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-semibold text-muted-foreground uppercase tracking-wide mb-2",children:"Ruta"}),e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[r.origen&&e.jsx("span",{className:"bg-muted rounded-lg px-3 py-1.5 text-sm font-medium",children:r.origen}),r.origen&&r.destino&&e.jsx(q,{className:"w-4 h-4 text-muted-foreground shrink-0"}),r.destino&&e.jsx("span",{className:"bg-muted rounded-lg px-3 py-1.5 text-sm font-medium",children:r.destino})]})]})]}),(s.referencia_tipo||s.referencia_id)&&e.jsxs(e.Fragment,{children:[e.jsx(C,{}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-semibold text-muted-foreground uppercase tracking-wide mb-2",children:"Referencia"}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[s.referencia_tipo&&e.jsx(m,{label:"Tipo referencia",value:s.referencia_tipo}),s.referencia_id&&e.jsx(m,{label:"ID referencia",value:s.referencia_id})]})]})]}),s.nota&&e.jsxs(e.Fragment,{children:[e.jsx(C,{}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-semibold text-muted-foreground uppercase tracking-wide mb-2",children:"Nota"}),e.jsx("p",{className:"text-sm text-foreground bg-muted/50 rounded-lg p-3",children:s.nota}
1)]})]})]})})]})})}function m({label:s,value:t,highlight:a}){return e.jsxs("div",{className:"bg-muted/50 rounded-lg p-3",children:[e.jsx("p",{className:"text-xs text-muted-foreground mb-0.5",children:s}),e.jsx("p",{className:N("text-sm font-semibold",a&&"text-primary text-base"),children:t})]})}function Ee({entries:s,loading:t,onSelect:a}){const r=ge();return t?e.jsxs("div",{className:"flex items-center justify-center py-16 text-muted-foreground gap-2",children:[e.jsx(te,{className:"w-5 h-5 animate-spin"})," Cargandoâ¦"]}):s.length===0?e.jsxs("div",{className:"flex flex-col items-center justify-center py-16 text-muted-foreground gap-2",children:[e.jsx(G,{className:"w-8 h-8 opacity-40"}),e.jsx("p",{className:"text-sm",children:"Sin registros para los filtros seleccionados"})]}):r?e.jsx("div",{className:"space-y-2 p-3",children:s.map(n=>e.jsx(De,{entry:n,onClick:()=>a(n)},n.id))}):e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-left",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b bg-muted/40",children:[e.jsx("th",{className:"px-4 py-2.5 text-xs font-semibold text-muted-foreground uppercase tracking-wide",children:"Fecha"}),e.jsx("th",{className:"px-4 py-2.5 text-xs font-semibold text-muted-foreground uppercase tracking-wide",children:"Tipo"}),e.jsx("th",{className:"px-4 py-2.5 text-xs font-semibold text-muted-foreground uppercase tracking-wide",children:"Ãtem"}),e.jsx("th",{className:"px-4 py-2.5 text-xs font-semibold text-muted-foreground uppercase tracking-wide text-right",children:"Cantidad"}),e.jsx("th",{className:"px-4 py-2.5 text-xs font-semibold text-muted-foreground uppercase tracking-wide",children:"Ruta"}),e.jsx("th",{className:"px-4 py-2.5 text-xs font-semibold text-muted-foreground uppercase tracking-wide",children:"Referencia"}),e.jsx("th",{className:"w-8"})]})}),e.jsx("tbody",{children:s.map(n=>e.jsx(Ie,{entry:n,onClick:()=>a(n)},n.id))})]})})}const Pe={all:"",transfers:"filters[tipo][$in][0]=transferencia_entrada&filters[tipo][$in][1]=transferencia_salida",ingresos:"filters[tipo][$eq]=entrada",egresos:"filters[tipo][$in][0]=salida&filters[tipo][$in][1]=transferencia_salida"};function Ae({active:s}){return s?e.jsxs("span",{className:"relative flex h-2 w-2 shrink-0",children:[e.jsx("span",{className:"animate-ping absolute inline-flex h-full w-full rounded-full bg-primary opacity-75"}),e.jsx("span",{className:"relative inline-flex rounded-full h-2 w-2 bg-primary"})]}):null}function ds(){const{startTour:s,hasSeenTour:t}=le("shop-trazabilidad",Re),{hasPermission:a}=de(),[r,n]=c.useState("all"),[i,x]=c.useState(X),[D,M]=c.useState(null),[Z,E]=c.useState(!1),w=c.useRef(),P=ne(),J=a("api::shop-kardex.shop-kardex.find")||a("api::shop-inventario.shop-inventario.find"),W=Pe[r],{data:p=[],isLoading:Y,isError:ee,refetch:A,isFetching:$}=oe({queryKey:["shop-kardex-traceability",r],queryFn:()=>qe.getKardex(W||void 0),staleTime:1e4,refetchInterval:3e4});c.useEffect(()=>{const o=["api::shop-kardex.shop-kardex","api::shop-movimiento-inventario.shop-movimiento-inventario","api::shop-movimiento-detalle.shop-movimiento-detalle"],l=()=>{P.invalidateQueries({queryKey:["shop-kardex-traceability"]}),E(!0),clearTimeout(w.current),w.current=setTimeout(()=>E(!1),2e3)},T=o.map(d=>O.onRealtimeUID(d,l)),y=O.onRealtime(d=>{o.some($e=>{var B,H;return((B=d.uid)==null?void 0:B.includes("kardex"))||((H=d.uid)==null?void 0:H.includes("movimiento"))})&&l()});return()=>{T.forEach(d=>d()),y(),clearTimeout(w.current)}},[P]);const h=c.useMemo(()=>p.filter(o=>{if(i.search){const l=i.search.toLowerCase(),T=v(o).toLowerCase(),y=(o.nota||"").toLowerCase(),d=(o.referencia_id||"").toLowerCase();if(!T.includes(l)&&!y.includes(l)&&!d.includes(l))return!1}if(i.itemType==="producto"&&!o.producto||i.itemType==="insumo"&&!o.insumo)return!1;if(i.dateFrom||i.dateTo)try{const l=b(o.fecha);if(i.dateFrom&&l<ce(i.dateFrom)||i.dateTo&&l>me(i.dateTo))return!1}catch{return!1}return!0}),[p,i]);return J?e.jsxs("div",{className:"flex flex-col h-full min-h-0 bg-background","data-tour":"trazabilidad-raiz",children:[e.jsx(_e,{onClick:s,hasSeenTour:t}),e.jsx("div",{className:"border-b px-4 md:px-6 py-4 bg-card",children:e.jsxs("div",{className:"flex items-center justify-between gap-3",children:[e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("h1",{className:"text-xl font-bold tracking-tight",children:"Trazabilidad de Inventario"}),e.jsx(Ae,{active:Z})]}),e.jsx("p",{className:"text-sm text-muted-foreground mt-0.5",children:"Movimientos, ingresos y egresos de bodega ⢠Tiempo real"})]}),e.jsxs(g,{variant:"outline",size:"sm",onClick:()=>A(),disabled:$,className:"gap-1.5 shrink-0",children:[e.jsx(se,{className:N("w-3.5 h-3.5",$&&"animate-spin")}),e.jsx("span",{className:"hidden sm:inline",children:"Actualizar"})]})]})}),e.jsx("div",{className:"px-4 md:px-6 pt-4",children:e.jsxs(Te,{value:r,onValueChange:o=>n(o),children:[e.jsxs(ye,{className:"w-full md:w-auto grid grid-cols-4 md:inline-flex h-9",children:[e.jsxs(f,{value:"all",className:"text-xs md:text-sm gap-1",children:[e.jsx(G,{className:"w-3.5 h-3.5"}),e.jsx("span",{children:"Todos"})]}),e.jsxs(f,{value:"transfers",className:"text-xs md:text-sm gap-1",children:[e.jsx(F,{className:"w-3.5 h-3.5"}),e.jsx("span",{className:"hidden sm:inline",children:"Transferencias"}),e.jsx("span",{className:"sm:hidden",children:"Transf."})]}),e.jsxs(f,{value:"ingresos",className:"text-xs md:text-sm gap-1",children:[e.jsx(Q,{className:"w-3.5 h-3.5"}),e.jsx("span",{children:"Ingresos"})]}),e.jsxs(f,{value:"egresos",className:"text-xs md:text-sm gap-1",children:[e.jsx(U,{className:"w-3.5 h-3.5"}),e.jsx("span",{children:"Egresos"})]})]}),e.jsx("div",{className:"mt-3 mb-4",children:e.jsx(Le,{filters:i,onChange:x})}),e.jsx("div",{className:"flex items-center justify-between mb-3",children:e.jsxs("p",{className:"text-xs text-muted-foreground",children:[h.length," registro",h.length!==1?"s":"",h.length!==p.length&&` de ${p.length}`]})}),e.jsx("div",{className:"bg-card border rounded-lg overflow-hidden",children:ee?e.jsx(Fe,{recurso:"el kardex",onReintentar:()=>A()}):e.jsx(Ee,{entries:h,loading:Y,onSelect:M})})]})}),e.jsx(Me,{entry:D,open:!!D,onClose:()=>M(null)})]}):e.jsx("div",{className:"p-6",children:e.jsx("div",{className:"text-sm text-muted-foreground",children:"No tienes permiso para ver la trazabilidad de inventario."})})}export{ds 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.