1import{j as e}from"./emotion-element-f0de968e.browser.esm-CkAcib9J.js";import{r as N,f as se,L as oe}from"./chunk-KS7C4IRE-D5dXzlLW.js";import{c as ae}from"./useSlotProps-Br-fPZ-p.js";import{S as ie}from"./StoreSelect-BYE64C-x.js";import{M as V}from"./react-moment-interop-Cejh4knc.js";import{h as Y}from"./moment-BLMuvzoS.js";import{D as ne}from"./Download-RwrDOK54.js";import{B as k,T as b}from"./Box-Bu2Z2trI.js";import{F as _,S as U}from"./index-Ba4xS3j9.js";import{I as F,T as X}from"./TextField-CwHKLY54.js";import{M as w,B as R,C as le}from"./MenuItem-Cbws_f4j.js";import{D as ce}from"./DataGrid-C0l7V7_J.js";import{T as z}from"./Tooltip-V5Adzz76.js";import{L as de}from"./Link-Cdtiv1NY.js";import{C as te}from"./Chip-DBy7n3Mp.js";import{L as ue}from"./LineChart-Cl9AnwvO.js";import{bI as me,aw as pe,bJ as he,bK as xe,bL as ge,bM as fe,bN as ye}from"./api-qiY2smlF.js";import{C as be}from"./Card-C8fRb0iU.js";import{C as je}from"./CardContent-BK55dnAK.js";import"./identifier-DCmdaooV.js";import"./imageUtils-BWiJvnWv.js";import"./Autocomplete-kxPez9UZ.js";import"./useAddToCart-CB7Sdaa6.js";import"./Snackbar-Fo0d5dST.js";import"./useTheme-C_8o6ItC.js";import"./useId-D-VxNTxp.js";import"./usePreviousProps-DZbJ-Faz.js";import"./IconButton-C2igLTSk.js";import"./index-CIibP7VT.js";import"./SearchResults-CnjpWGbS.js";import"./OriginLink-NZs5VyL2.js";import"./seo-D5RPFqEx.js";import"./env-CHruRPRK.js";import"./appPaths-DLtVD8gm.js";import"./Star-VcYUfXEv.js";import"./mapLinks-BXFXGuFX.js";import"./Photo-BTaLaYYc.js";import"./useThemeProps-CUeDNTrx.js";import"./ArrowDropDown-Dc3_CX8Q.js";import"./LocationOn-RbsU8tml.js";import"./Divider-jVf1TKX1.js";import"./InputAdornment-DPRfKtOn.js";import"./Checkbox-Bf-KPp_e.js";import"./FormControlLabel-BgXUbgq2.js";import"./Badge-eGs3R1-L.js";import"./KeyboardArrowRight-D_vrZmoo.js";import"./LastPage-JNFuzFiN.js";import"./TableCell-C8PZLZCf.js";import"./CustomerWrapper-mJMo-Beg.js";import"./Favorite-OhcX1HPf.js";import"./Message-b6OAa9gy.js";import"./ColorModeContext-Db2jY2gh.js";import"./Search-DYAWCHnA.js";import"./AtlasSearchProvider-C0Krahge.js";import"./CustomSearchBox-B1Pjk2eZ.js";import"./AnimatedLink-DLgq7h-R.js";import"./index-CaKTCWaW.js";import"./KeyboardDoubleArrowLeft-urr1X-Ug.js";import"./Skeleton-Cz64fv2Z.js";import"./cssUtils-BDukCBHC.js";import"./useThemeProps-B0DemRAW.js";const ve=ae(e.jsx("path",{d:"M12 5V2L8 6l4 4V7c3.31 0 6 2.69 6 6 0 2.97-2.17 5.43-5 5.91v2.02c3.95-.49 7-3.85 7-7.93 0-4.42-3.58-8-8-8m-6 8c0-1.65.67-3.15 1.76-4.24L6.34 7.34C4.9 8.79 4 10.79 4 13c0 4.08 3.05 7.44 7 7.93v-2.02c-2.83-.48-5-2.94-5-5.91"})),ke=["all","tuneportals","square","lightspeed","lightspeed_x","shopify","manual_file","manual_ui","order","automated"],Se=["all","qty","price"],we=[{value:"all",label:"All time"},{value:"7",label:"Last 7 days"},{value:"30",label:"Last 30 days"},{value:"90",label:"Last 90 days"}],Ce=.25;function Ie(o){var f;const p=(f=o.meta)==null?void 0:f.kind;return p==="qty"&&o.value===0&&(o.oldValue??0)>0?"stockout":p==="price"&&o.oldValue&&Math.abs((o.value-o.oldValue)/o.oldValue)>=Ce?"price-swing":null}function J({x:o,y:p,label:f,yLabel:j,color:c,valueFormatter:n}){return p.length<2?e.jsxs(k,{sx:{flex:1,minWidth:320},children:[e.jsx(b,{variant:"subtitle2",children:f}),e.jsx(b,{variant:"caption",color:"text.secondary",children:"Not enough data to chart"})]}):e.jsxs(k,{sx:{flex:1,minWidth:340},children:[e.jsx(b,{variant:"subtitle2",sx:{mb:.5},children:f}),e.jsx(ue,{height:220,xAxis:[{data:o,scaleType:"point"}],yAxis:[{label:j}],series:[{data:p,color:c,showMark:p.length<=30,valueFormatter:n}],slotProps:{legend:{hidden:!0}}})]})}function De({data:o,isLoading:p,isFetching:f,isError:j,kind:c,onKindChange:n,source:l,onSourceChange:x,page:g,setPage:P,title:C,description:I,onUpcClick:S,period:E,onPeriodChange:L}){const u=(o==null?void 0:o.docs)??[],{qtyX:$,qtyY:M,priceX:v,priceY:H,singleSeries:D}=N.useMemo(()=>{const t=[...u].sort((m,s)=>new Date(m.ts).getTime()-new Date(s.ts).getTime()),r=t.filter(m=>{var s;return((s=m.meta)==null?void 0:s.kind)==="qty"}),a=t.filter(m=>{var s;return((s=m.meta)==null?void 0:s.kind)==="price"}),h=u[0],W=u.length>0&&u.every(m=>{var s,d,i,y;return String((s=m.meta)==null?void 0:s.skuId)===String((d=h==null?void 0:h.meta)==null?void 0:d.skuId)&&String((i=m.meta)==null?void 0:i.storeId)===String((y=h==null?void 0:h.meta)==null?void 0:y.storeId)});return{qtyX:r.map(m=>Y(m.ts).format("MMM D HH:mm")),qtyY:r.map(m=>m.value),priceX:a.map(m=>Y(m.ts).format("MMM D HH:mm")),priceY:a.map(m=>m.value),singleSeries:W}},[u]),O=t=>{var h;if(t.delta===void 0||t.delta===null)return"";const r=t.delta>0?"+":t.delta<0?"â":"",a=Math.abs(t.delta);return((h=t.meta)==null?void 0:h.kind)==="price"?`${r}$${a.toFixed(2)}`:`${r}${a}`},A=(t,r)=>{var a;return r==null?"":((a=t.meta)==null?void 0:a.kind)==="price"?`$${Number(r).toFixed(2)}`:String(r)},T={overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},q=[{field:"ts",headerName:"When",width:150,sortable:!1,renderCell:t=>e.jsx(V,{local:!0,format:"MM/DD/YY hh:mm A",children:t.row.ts})},{field:"store",headerName:"Store",width:160,sortable:!1,valueGetter:(t,r)=>r.storeName??"",renderCell:t=>e.jsx(z,{title:t.row.storeName??"",children:e.jsx("span",{style:T,children:t.row.storeName??"â"})})},{field:"upc",headerName:"UPC",width:140,sortable:!1,valueGetter:(t,r)=>{var a;return((a=r.product)==null?void 0:a.upc)??""},renderCell:t=>{var r;return(r=t.row.product)!=null&&r.upc?e.jsx(de,{component:"button",type:"button",underline:"hover",title:"Show this UPC across all stores",onClick:()=>
1S==null?void 0:S(t.row.product.upc),sx:{cursor:S?"pointer":"default"},children:t.row.product.upc}):"â"}},{field:"product",headerName:"Product",flex:1.4,minWidth:160,sortable:!1,valueGetter:(t,r)=>{var a,h;return[(a=r.product)==null?void 0:a.artist,(h=r.product)==null?void 0:h.title].filter(Boolean).join(" â ")},renderCell:t=>{var a,h;const r=[(a=t.row.product)==null?void 0:a.artist,(h=t.row.product)==null?void 0:h.title].filter(Boolean).join(" â ")||"â";return e.jsx(z,{title:r==="â"?"":r,children:e.jsx("span",{style:T,children:r})})}},{field:"kind",headerName:"Kind",width:80,sortable:!1,valueGetter:(t,r)=>{var a;return((a=r.meta)==null?void 0:a.kind)??""}},{field:"change",headerName:"Change",width:150,sortable:!1,align:"center",headerAlign:"center",renderCell:t=>{const r=t.row;return r.oldValue!==void 0&&r.oldValue!==null?`${A(r,r.oldValue)} â ${A(r,r.value)}`:A(r,r.value)}},{field:"delta",headerName:"Î",width:90,sortable:!1,align:"center",headerAlign:"center",renderCell:t=>e.jsx("span",{style:{color:(t.row.delta??0)<0?"#d32f2f":"#2e7d32"},children:O(t.row)})},{field:"source",headerName:"Source",width:130,sortable:!1,align:"center",headerAlign:"center",valueGetter:(t,r)=>{var a;return((a=r.meta)==null?void 0:a.source)??""},renderCell:t=>{var r;return e.jsx(te,{size:"small",label:(r=t.row.meta)==null?void 0:r.source,sx:{height:20,fontSize:11}})}},{field:"userEmail",headerName:"By",width:170,sortable:!1,valueGetter:(t,r)=>r.userEmail??"â"},{field:"note",headerName:"Reason",flex:1.2,minWidth:180,sortable:!1,renderCell:t=>e.jsx(z,{title:t.row.note??"",children:e.jsx("span",{style:T,children:t.row.note??""})})}],K=()=>{const t=d=>String(d??"").replace(/"/g,'""'),r=["timestamp","store","kind","upc","artist","title","oldValue","value","delta","source","user","orderId","dataloadId","note"],a=u.map(d=>{var i,y,B,Q,G;return[new Date(d.ts).toISOString(),t(d.storeName),((i=d.meta)==null?void 0:i.kind)??"",t((y=d.product)==null?void 0:y.upc),t((B=d.product)==null?void 0:B.artist),t((Q=d.product)==null?void 0:Q.title),d.oldValue??"",d.value??"",d.delta??"",((G=d.meta)==null?void 0:G.source)??"",d.userEmail??"",d.orderId??"",d.dataloadId??"",t(d.note)]}),h=[r,...a].map(d=>d.map(i=>`"${i}"`).join(",")).join(` 2`),W=new Blob([h],{type:"text/csv"}),m=URL.createObjectURL(W),s=document.createElement("a");s.href=m,s.download=`sku-history-${Date.now()}.csv`,document.body.appendChild(s),s.click(),URL.revokeObjectURL(m),document.body.removeChild(s)};return e.jsxs(k,{children:[C&&e.jsx(b,{variant:"h5",sx:{mb:I?.5:1},children:C}),I&&e.jsx(b,{variant:"body2",color:"text.secondary",sx:{mb:2},children:I}),e.jsxs(k,{sx:{display:"flex",gap:2,alignItems:"center",flexWrap:"wrap",mb:2},children:[e.jsxs(_,{size:"small",sx:{minWidth:120},children:[e.jsx(F,{id:"sku-history-kind-label",children:"Kind"}),e.jsx(U,{labelId:"sku-history-kind-label",value:c,label:"Kind",onChange:t=>n(t.target.value),children:Se.map(t=>e.jsx(w,{value:t,children:t==="all"?"All":t},t))})]}),e.jsxs(_,{size:"small",sx:{minWidth:160},children:[e.jsx(F,{id:"sku-history-source-label",children:"Source"}),e.jsx(U,{labelId:"sku-history-source-label",value:l,label:"Source",onChange:t=>x(t.target.value),children:ke.map(t=>e.jsx(w,{value:t,children:t==="all"?"All":t},t))})]}),L&&e.jsxs(_,{size:"small",sx:{minWidth:150},children:[e.jsx(F,{id:"sku-history-period-label",children:"Period"}),e.jsx(U,{labelId:"sku-history-period-label",value:E??"all",label:"Period",onChange:t=>L(t.target.value),children:we.map(t=>e.jsx(w,{value:t.value,children:t.label},t.value))})]}),e.jsx(k,{sx:{flex:1}}),e.jsx(R,{size:"small",variant:"outlined",startIcon:e.jsx(ne,{}),onClick:K,disabled:!u.length,children:"Export CSV"})]}),D&&e.jsxs(k,{sx:{display:"flex",gap:3,mb:2,flexWrap:"wrap"},children:[e.jsx(J,{x:$,y:M,label:"Quantity over time",yLabel:"Quantity",color:"#1976d2",valueFormatter:t=>t==null?"":String(t)}),e.jsx(J,{x:v,y:H,label:"Price over time",yLabel:"Price ($)",color:"#2e7d32",valueFormatter:t=>t==null?"":`$${Number(t).toFixed(2)}`})]}),!D&&!!u.length&&e.jsx(b,{variant:"caption",color:"text.secondary",sx:{dis
2play:"block",mb:2},children:"Select a single SKU, or a UPC at one store, to see its quantity & price trend over time."}),j&&e.jsx(b,{color:"error",sx:{mb:1},children:"Error fetching history"}),e.jsx(ce,{autoHeight:!0,rows:u,getRowId:t=>{var r,a;return t._id??`${t.ts}-${(r=t.meta)==null?void 0:r.skuId}-${(a=t.meta)==null?void 0:a.kind}`},columns:q,loading:p||f,disableRowSelectionOnClick:!0,disableColumnMenu:!0,disableColumnFilter:!0,disableColumnSelector:!0,disableDensitySelector:!0,getRowClassName:t=>{const r=Ie(t.row);return r?`row-${r}`:""},paginationMode:"server",rowCount:(o==null?void 0:o.totalDocs)??0,paginationModel:{page:Math.max((g??1)-1,0),pageSize:(o==null?void 0:o.limit)??50},onPaginationModelChange:t=>P(t.page+1),pageSizeOptions:[(o==null?void 0:o.limit)??50],sx:{"& .row-stockout":{bgcolor:"rgba(211,47,47,0.12)"},"& .row-price-swing":{bgcolor:"rgba(237,108,2,0.12)"}}})]})}const Z=1440*60*1e3;function re(o){if(!o)return"error";const p=Date.now()-new Date(o).getTime();return p<=Z?"success":p<=7*Z?"warning":"error"}function Ne(o){return`${re(o)}.main`}function ee({label:o,ts:p}){return e.jsxs(k,{sx:{minWidth:160},children:[e.jsx(b,{variant:"caption",color:"text.secondary",display:"block",children:o}),e.jsx(b,{variant:"body2",sx:{fontWeight:600,color:Ne(p)},children:p?e.jsx(V,{fromNow:!0,children:p}):"never"})]})}function Pe({storeId:o}){const{data:p,isLoading:f,isError:j}=me({storeId:o},{skip:!o}),c=p,n=(()=>{const l=new Map;return((c==null?void 0:c.recentBySource)??[]).forEach(x=>{const g=l.get(x.source)??{count:0,last:x.last,kinds:new Set};g.count+=x.count,new Date(x.last).getTime()>new Date(g.last).getTime()&&(g.last=x.last),g.kinds.add(x.kind),l.set(x.source,g)}),Array.from(l.entries()).map(([x,g])=>({source:x,count:g.count,last:g.last,kinds:Array.from(g.kinds)})).sort((x,g)=>new Date(g.last).getTime()-new Date(x.last).getTime())})();return e.jsx(be,{variant:"outlined",sx:{mb:3},children:e.jsxs(je,{children:[e.jsxs(b,{variant:"subtitle1",sx:{fontWeight:600,mb:1},children:["Store health"," ",e.jsx(b,{component:"span",variant:"caption",color:"text.secondary",children:"· sources active in the last 30 days"})]}),f?e.jsx(le,{size:20}):j?e.jsx(b,{color:"text.secondary",children:"Could not load summary"}):e.jsxs(e.Fragment,{children:[e.jsxs(k,{sx:{display:"flex",gap:4,mb:2,flexWrap:"wrap"},children:[e.jsx(ee,{label:"Last quantity change",ts:c==null?void 0:c.lastQtyChange}),e.jsx(ee,{label:"Last price change",ts:c==null?void 0:c.lastPriceChange})]}),e.jsx(k,{sx:{display:"flex",gap:1,flexWrap:"wrap",alignItems:"center"},children:n.length===0?e.jsx(b,{variant:"caption",color:"text.secondary",children:"No changes recorded in the last 30 days â this store may not be syncing."}):n.map(l=>e.jsx(z,{title:`kinds: ${l.kinds.join(", ")}`,children:e.jsx(te,{size:"small",variant:"outlined",color:re(l.last),label:e.jsxs("span",{children:[l.source,": ",l.count," ·"," ",e.jsx(V,{fromNow:!0,children:l.last})]})})},l.source))})]})]})})}const Le=1440*60*1e3;function Ut(){const[o,p]=se(),f=o.get("dataloadId")||"",j=o.get("upc")||"",c=o.get("skuId")||"",n=o.get("mode")||(f?"dataload":j?"upc":c?"sku":"feed"),l=o.get("storeId")||"",x=o.get("kind")||"all",g=o.get("source")||"all",P=o.get("period")||"all",C=Number(o.get("page"))||1,[I,S]=N.useState(j),[E,L]=N.useState(c),[u,$]=N.useState(null);N.useEffect(()=>S(j),[j]),N.useEffect(()=>L(c),[c]);const{data:M}=pe({storeId:l},{skip:!l||(u==null?void 0:u._id)===l});N.useEffect(()=>{if(!l){u&&$(null);return}const s=M==null?void 0:M.store;(s==null?void 0:s._id)===l&&(u==null?void 0:u._id)!==s._id&&$({_id:s._id,name:s.name})},[l,M,u]);const v=(s,d=!1)=>{const i={mode:n,upc:j,skuId:c,storeId:l,kind:x,source:g,period:P,page:C,dataloadId:f,...s},y={};i.mode!=="feed"&&(y.mode=i.mode),i.mode==="dataload"&&i.dataloadId&&(y.dataloadId=i.dataloadId),i.mode==="upc"&&i.upc&&(y.upc=i.upc),i.mode==="sku"&&i.skuId&&(y.skuId=i.skuId),i.storeId&&(y.storeId=i.storeId),i.kind!=="all"&&(y.kind=i.kind),i.source!=="all"&&(y.source=i.source),i.period!=="all"&&(y.period=i.period),Number(i.page)>1&&(y.page=String(i.page)),p(y,{replace:!d})},H=P==="all"?void 0:new Date(Date.now()-Number(P)*Le).toISOString(),D={kind:x,source:g,from:H,page:C,limit:50},O=he({storeId:l,...D,limit:100},{skip:n!=="feed"}),A=xe({upc:j,storeId:l,...D},{skip:n!=="upc"||!j}),T=ge({skuId:c,storeId:l,...D},{skip:n!=="sku"||!c}),q=fe({storeId:l,...D},{skip:n!=="store"||!l}),K=ye({dataloadId:f,page:C,limit:50},{skip:n!=="dataload"||!f}),t=n==="feed"?O:n==="upc"?A:n==="sku"?T:n==="store"?q:K,r=(u==null?void 0:u.name)||(l?"selected store":null),a=(()=>{switch(n){case"feed":return{title:r?`Recent changes â ${r}`:"Recent changes â all stores",description:r?"Most recent quantity & price changes for this store, newest first. The health panel above shows when each source last wrote â use it to spot a stalled integration.":"The 100 most recent quantity & price changes across every store and sync source. Confirm data is flowing, then filter by store, kind, source, or period to narrow down."};case"upc":return{title:j?`Changes for UPC ${j}`:"Search by UPC",description:"Every recorded quantity & price change for this UPC across stores. Select a store to chart its trend over time."};case"sku":return{title:c?`Changes for SKU ${c}`:"Search by SKU id",description:"Full quantity & price timeline for one SKU at its store, with trend charts."};case"store":return{title:r?`Change feed â ${r}`:"Store change feed",description:"All recorded changes for the selected store, newest first. Check the health panel above for per-source freshness."};case"dataload":return{title:`Changes from sync run ${f}`,description:"Every SKU quantity & price change attributed to this dataload/sync run."};default:return{title:"",description:""}}})(),h=s=>{v({mode:"upc",upc:s,storeId:"",page:1},!0)},W=()=>{S(""),L(""),$(null),p({},{replace:!0})},m=(n==="feed"||n==="store")&&!!l;return e.jsxs(k,{sx:{p:4,boxSizing:"border-box"},children:[e.jsxs(k,{sx:{display:"flex",mb:2,gap:.5},children:[e.jsx(oe,{to:"/admin/dashboard",style:{textDecoration:"none"},children:e.jsx(b,{color:"text.secondary",sx:{fontWeight:600},children:"Admin"})}),"/",e.jsx(b,{color:"text.secondary",sx:{fontWeight:600},children:"Inventory History"})]}),e.jsxs(k,{sx:{display:"flex",alignItems:"center",justifyContent:"space-between",gap:2,mb:2},children:[e.jsx(b,{variant:"h1",color:"text.primary",children:"Inventory History"}),e.jsx(R,{variant:"outlined",size:"small",startIcon:e.jsx(ve,{}),onClick:W,children:"Reset"})]}),e.jsxs(k,{sx:{display:"flex",gap:2,flexWrap:"wrap",alignItems:"center",mb:3},children:[e.jsxs(_,{size:"small",sx:{minWidth:160},children:[e.jsx(F,{id:"sku-history-mode-label",children:"Search by"}),e.jsxs(U,{labelId:"sku-history-mode-label",value:n,label:"Search by",onChange:s=>v({mode:s.target.value,page:1}),children:[e.jsx(w,{value:"feed",children:"Recent (all stores)"}),e.jsx(w,{value:"upc",children:"UPC"}
2),e.jsx(w,{value:"sku",children:"SKU id"}),e.jsx(w,{value:"store",children:"Store feed"}),f&&e.jsx(w,{value:"dataload",children:"Dataload run"})]})]}),n==="upc"&&e.jsxs(e.Fragment,{children:[e.jsx(X,{size:"small",label:"UPC",value:I,onChange:s=>S(s.target.value),onKeyDown:s=>{s.key==="Enter"&&v({mode:"upc",upc:I.trim(),page:1})}}),e.jsx(R,{variant:"contained",size:"small",onClick:()=>v({mode:"upc",upc:I.trim(),page:1}),children:"Search"})]}),n==="sku"&&e.jsxs(e.Fragment,{children:[e.jsx(X,{size:"small",label:"SKU id",value:E,sx:{minWidth:280},onChange:s=>L(s.target.value),onKeyDown:s=>{s.key==="Enter"&&v({mode:"sku",skuId:E.trim(),page:1})}}),e.jsx(R,{variant:"contained",size:"small",onClick:()=>v({mode:"sku",skuId:E.trim(),page:1}),children:"Search"})]}),(n==="feed"||n==="upc"||n==="sku"||n==="store")&&e.jsx(_,{size:"small",sx:{minWidth:250},children:e.jsx(ie,{value:u,onChange:s=>{$(s),v({storeId:(s==null?void 0:s._id)||"",page:1})},label:n==="store"?"Store":"Filter by Store (optional)"})})]}),m&&e.jsx(Pe,{storeId:l}),e.jsx(De,{data:t.data,isLoading:t.isLoading,isFetching:t.isFetching,isError:t.isError,kind:x,onKindChange:s=>v({kind:s,page:1}),source:g,onSourceChange:s=>v({source:s,page:1}),period:P,onPeriodChange:s=>v({period:s,page:1}),page:C,setPage:s=>v({page:s}),title:a.title,description:a.description,onUpcClick:h})]})}export{Ut 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.