1"use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[10993],{210993:(e,t,s)=>{s.r(t),s.d(t,{default:()=>eF});var a=s(695155),r=s(212115),n=s(979214),i=s(457527),o=s(638162),l=s(466498),c=s(669606),d=s(437177),u=s(212469),m=s(73312),x=s(733900),h=s(998986),p=s(537528),v=s(294831),g=s(989095),f=s(670488),y=s(984179),j=s(22130),_=s(757223),k=s(961594),b=s(948617),N=s(133473),w=s(821773),q=s(923950),E=s(614505),L=s(560719),A=s(126336),S=s(515800),C=s(598867),I=s(77504);let O=e=>u.Ik({product_id:u.Yj().min(1,e("inventory.stockLevels.form.validation.productRequired")),storage_id:u.Yj().min(1,e("inventory.stockLevels.form.validation.locationRequired")),quantity:u.Yj().min(1,e("inventory.stockLevels.form.validation.quantityRequired")).refine(e=>!Number.isNaN(Number(e)),e("inventory.stockLevels.form.validation.quantityNumber")).refine(e=>Number(e)>=0,e("inventory.stockLevels.form.validation.quantityNonNegative")),reserved_qty:u.Yj().optional().refine(e=>void 0===e||""===e.trim()||!Number.isNaN(Number(e))&&Number(e)>=0,e("inventory.stockLevels.form.validation.reservedNonNegative")),is_quarantine:u.zM(),quality_status:u.k5(["ok","qc_hold","reject"])}),T=e=>{var t,s;let{initialData:i,onSubmit:o,onCancel:l,loading:u=!1,mode:T,products:F,storageUnits:z,stockFieldsOnly:P=!1,existingStockLines:V=[],onReplace:D}=e,{t:R}=(0,n.Bd)(),B=(0,r.useMemo)(()=>O(R),[R]),{register:$,handleSubmit:J,watch:U,setValue:M,formState:{errors:H,isSubmitting:Q}}=(0,c.mN)({resolver:(0,d.u)(B),defaultValues:{product_id:(null==i?void 0:i.product_id)||"",storage_id:(null==i?void 0:i.storage_id)||"",quantity:(null==i?void 0:null===(t=i.quantity)||void 0===t?void 0:t.toString())||"",reserved_qty:(null==i?void 0:null===(s=i.reserved_qty)||void 0===s?void 0:s.toString())||"",is_quarantine:(null==i?void 0:i.is_quarantine)||!1,quality_status:(null==i?void 0:i.quality_status)||"ok"}}),W=U();(0,r.useEffect)(()=>{"create"!==T||null!=i&&i.storage_id||1!==z.length||M("storage_id",z[0].storage_id,{shouldValidate:!0,shouldDirty:!0})},[T,null==i?void 0:i.storage_id,z,M]);let[K,X]=(0,r.useState)(!1),[G,Z]=(0,r.useState)(""),Y=(0,r.useMemo)(()=>{var e;return P&&"create"===T&&W.storage_id&&null!==(e=V.find(e=>e.storage_id===W.storage_id))&&void 0!==e?e:null},[P,T,W.storage_id,V]),[ee,et]=(0,r.useState)("add");(0,r.useEffect)(()=>{et("add")},[W.storage_id]);let es=(0,r.useMemo)(()=>{let e=parseFloat(W.quantity||"");return Number.isFinite(e)?e:0},[W.quantity]),ea=(0,r.useMemo)(()=>Y?"replace"===ee?es:(Y.quantity||0)+es:null,[Y,ee,es]),er=async e=>{let t={product_id:e.product_id,storage_id:e.storage_id,quantity:parseFloat(e.quantity),reserved_qty:e.reserved_qty?parseFloat(e.reserved_qty):0,is_quarantine:e.is_quarantine,quality_status:e.quality_status};if(Y&&"replace"===ee&&D){await D(Y.stock_id,{quantity:t.quantity,reserved_qty:t.reserved_qty,is_quarantine:t.is_quarantine,quality_status:t.quality_status});return}await o(t)},en=u||Q,ei=F.find(e=>e.product_id===W.product_id),eo=e=>new Intl.NumberFormat(void 0,{maximumFractionDigits:4}).format(e);return P?(0,a.jsxs)("form",{onSubmit:J(er),className:"space-y-5",children:[ei?(0,a.jsxs)("div",{className:"rounded-lg border bg-muted/40 px-3 py-2 text-sm",children:[(0,a.jsx)("span",{className:"font-semibold text-foreground",children:ei.name}),(0,a.jsx)("code",{className:"ml-2 text-xs text-muted-foreground",children:ei.sku})]}):null,(0,a.jsxs)("div",{className:"space-y-2",children:[(0,a.jsxs)(h.J,{className:"text-sm font-semibold",children:[R("inventory.stockLevels.form.fields.location.label")," ",(0,a.jsx)("span",{className:"text-red-500",children:"*"})]}),"create"===T&&1===z.length?(0,a.jsxs)("div",{className:"flex items-center gap-2 rounded-md border border-purple-200 bg-purple-50/60 px-3 py-2 text-sm",children:[(0,a.jsx)(_.A,{className:"h-4 w-4 shrink-0 text-purple-600"}),(0,a.jsx)("span",{className:"font-medium",children:z[0].name}),(0,a.jsx)("code",{className:"text-xs bg-white px-1.5 py-0.5 rounded text-purple-800",children:z[0].code})]}):(0,a.jsxs)(p.l6,{value:W.storage_id,onValueChange:e=>M("storage_id",e),disabled:en,children:[(0,a.jsx)(p.bq,{className:H.storage_id?"border-red-500":"",children:(0,a.jsx)(p.yv,{placeholder:R("inventory.stockLevels.form.fields.location.placeholder")})}),(0,a.jsx)(p.gC,{children:z.map(e=>(0,a.jsxs)(p.eb,{value:e.storage_id,children:[(0,a.jsx)("span",{className:"font-medium",children:e.name}),(0,a.jsx)("code",{className:"ml-2 text-xs",children:e.code})]},e.storage_id))})]}),H.storage_id&&(0,a.jsxs)("p",{className:"text-sm text-red-600 flex items-center gap-1",children:[(0,a.jsx)(k.A,{className:"h-3 w-3"})," ",H.storage_id.message]})]}),Y?(0,a.jsxs)("div",{className:"space-y-3 rounded-lg border border-amber-200 bg-amber-50/60 p-3",children:[(0,a.jsxs)("div",{className:"flex items-start gap-2",children:[(0,a.jsx)(b.A,{className:"h-4 w-4 mt-0.5 shrink-0 text-amber-600"}),(0,a.jsxs)("div",{className:"flex-1 space-y-2",children:[(0,a.jsx)("p",{className:"text-sm font-semibold text-amber-900",children:R("inventory.stockLevels.form.fields.existingStock.title")}),(0,a.jsxs)("div",{className:"flex flex-wrap items-center gap-x-4 gap-y-1 text-sm",children:[(0,a.jsxs)("span",{className:"text-muted-foreground",children:[R("inventory.stockLevels.form.fields.existingStock.currentLabel")," : ",(0,a.jsx)("span",{className:"font-mono text-base font-semibold text-foreground",children:eo(Y.quantity)})]}),Y.is_quarantine?(0,a.jsx)("span",{className:"text-xs px-2 py-0.5 rounded-full bg-red-100 text-red-700 font-medium",children:R("inventory.stockLevels.form.fields.existingStock.quarantineBadge")}):null]})]})]}),D?(0,a.jsxs)(y.z,{value:ee,onValueChange:e=>et(e),className:"grid grid-cols-1 gap-2",disabled:en,children:[(0,a.jsxs)(h.J,{htmlFor:"op-add",className:"flex items-start gap-2 rounded-md border px-3 py-2 cursor-pointer transition-colors ".concat("add"===ee?"border-teal-400 bg-white":"border-transparent bg-white/60"),children:[(0,a.jsx)(y.C,{value:"add",id:"op-add",className:"mt-0.5"}),(0,a.jsx)("div",{className:"flex-1",children:(0,a.jsxs)("span",{className:"flex items-center gap-1.5 font-medium text-sm",children:[(0,a.jsx)(N.A,{className:"h-3.5 w-3.5 text-teal-600"}),R("inventory.stockLevels.form.fields.existingStock.modeAdd")]})})]}),(0,a.jsxs)(h.J,{htmlFor:"op-replace",className:"flex items-start gap-2 rounded-md border px-3 py-2 cursor-pointer transition-colors ".concat("replace"===ee?"border-orange-400 bg-white":"border-transparent bg-white/60"),children:[(0,a.jsx)(y.C,{value:"replace",id:"op-replace",className:"mt-0.5"}),(0,a.jsxs)("div",{className:"flex-1",children:[(0,a.jsxs)("span",{className:"flex items-center gap-1.5 font-medium text-sm",children:[(0,a.jsx)(w.A,{className:"h-3.5 w-3.5 text-orange-600"}),R("inventory.stockLevels.form.fields.existingStock.modeReplace")]}),"replace"===ee?(0,a.jsxs)("p",{className:"mt-1 flex items-center gap-1 text-xs text-orange-700",children:[(0,a.jsx)(q.A,{className:"h-3 w-3"}),R("inventory.stockLevels.form.fields.existingStock.modeReplaceWarning")]}):null]})]})]}):null]}):null,(0,a.jsxs)("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-4",children:[(0,a.jsxs)("div",{className:"space-y-2",children:[(0,a.jsxs)(h.J,{htmlFor:"quantity",className:"text-sm font-semibold",children:[Y?"replace"===ee?R("inventory.stockLevels.form.fields.quantity.labelReplace"):R("inventory.stockLevels.form.fields.quantity.labelAdd"):R("inventory.stockLevels.form.fields.quantity.label")," ",(0,a.jsx)("span",{className:"text-red-500",children:"*"})]}),(0,a.jsx)(x.p,{id:"quantity",type:"number",step:"1",placeholder:R("inventory.stockLevels.form.fields.quantity.placeholder"),...$("quantity"),disabled:en,className:H.quantity?"border-red-500":"",min:"0"}),Y?(0,a.jsx)("p",{className:"text-xs text-muted-foreground",children:"replace"===ee?R("inventory.stockLevels.form.fields.quantity.helperReplace"):R("inventory.stockLevels.form.fields.quantity.helperAdd")}):null,H.quantity&&(0,a.jsx)("p",{className:"text-sm text-red-600",children:H.quantity.message})]}),(0,a.jsxs)("div",{className:"space-y-2",children:[(0,a.jsx)(h.J,{htmlFor:"reserved_qty",className:"text-sm font-semibold",children:R("inventory.stockLevels.form.fields.reserved.label")}),(0,a.jsx)(x.p,{id:"reserved_qty",type:"number",step:"1",placeholder:R("inventory.stockLevels.form.fields.reserved.placeholder"),...$("reserved_qty"),disabled:en,className:H.reserved_qty?"border-red-500":"",min:"0"}),H.reserved_qty&&(0,a.jsx)("p",{className:"text-sm text-red-600",children:H.reserved_qty.message})]})]}),Y&&null!==ea?(0,a.jsxs)("div",{className:"rounded-lg border px-3 py-2.5 text-sm flex items-center justify-between ".concat("replace"===ee?"border-orange-200 bg-orange-50":"border-teal-200 bg-teal-50"),children:[(0,a.jsx)("span",{className:"text-muted-foreground",children:R("inventory.stockLevels.form.fields.existingStock.afterLabel")}),"add"===ee?(0,a.jsxs)("span",{className:"font-mono text-sm",children:[eo(Y.quantity),(0,a.jsx)("span",{className:"mx-1 text-muted-foreground",children:"+"}),eo(es),(0,a.jsx)("span",{className:"mx-1 text-muted-foreground",children:"="}),(0,a.jsx)("span",{className:"text-base font-bold text-teal-700",children:eo(ea)})]}):(0,a.jsxs)("span",{className:"font-mono",children:[(0,a.jsx)("span",{className:"text-muted-foreground line-through mr-2",children:eo(Y.quantity)}),(0,a.jsx)("span",{className:"text-base font-bold text-orange-700",children:eo(ea)})]})]}):null,(0,a.jsxs)("div",{className:"flex justify-end gap-2 pt-2",children:[(0,a.jsx)(m.$,{type:"button",variant:"outline",onClick:l,disabled:en,children:R("inventory.stockLevels.form.actions.cancel")}),(0,a.jsx)(m.$,{type:"submit",disabled:en,className:Y&&"replace"===ee?"bg-gradient-to-r from-orange-600 to-amber-600 hover:from-orange-700 hover:to-amber-700":"bg-gradient-to-r from-teal-600 to-cyan-600 hover:from-teal-700 hover:to-cyan-700",children:en?(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(E.A,{className:"h-4 w-4 mr-2 animate-spin"}),R("inventory.stockLevels.form.actions.saving")]}):Y?"replace"===ee?(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(w.A,{className:"h-4 w-4 mr-2"}),R("inventory.stockLevels.form.actions.replace")]}):(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(N.A,{className:"h-4 w-4 mr-2"}),R("inventory.stockLevels.form.actions.add")]}):R("inventory.stockLevels.form.actions.create")})]})]}):(0,a.jsxs)("form",{onSubmit:J(er),className:"space-y-6",children:[(0,a.jsxs)(j.Zp,{className:"border-blue-200 shadow-md hover:shadow-lg transition-shadow",children:[(0,a.jsxs)(j.aR,{className:"bg-gradient-to-r from-blue-50 to-transparent",children:[(0,a.jsxs)(j.ZB,{className:"flex items-center gap-2 text-lg",children:[(0,a.jsx)(L.A,{className:"h-5 w-5 text-blue-600"}),R("inventory.stockLevels.form.sections.productLocation.title")]}),(0,a.jsx)(j.BT,{children:R("inventory.stockLevels.form.sections.productLocation.description")})]}),(0,a.jsxs)(j.Wu,{className:"space-y-5 pt-6",children:[(0,a.jsxs)("div",{className:"space-y-2",children:[(0,a.jsxs)(h.J,{htmlFor:"product_id",className:"text-sm font-semibold flex items-center gap-1",children:[R("inventory.stockLevels.form.fields.product.label")," ",(0,a.jsx)("span",{className:"text-red-500",children:"*"}),W.product_id&&!H.product_id&&(0,a.jsx)(A.A,{className:"h-3 w-3 text-green-600"})]}),(0,a.jsxs)(g.AM,{open:K,onOpenChange:X,children:[(0,a.jsx)(g.Wv,{asChild:!0,children:(0,a.jsxs)(m.$,{variant:"outline",role:"combobox","aria-expanded":K,className:"w-full justify-between focus:ring-blue-500 ".concat(H.product_id?"border-red-500":""),disabled:en,children:[W.product_id?(()=>{let e=F.find(e=>e.product_id===W.product_id);return e?"".concat(e.name," (").concat(e.sku,")"):R("inventory.stockLevels.form.fields.product.placeholder")})():R("inventory.stockLevels.form.fields.product.placeholder"),(0,a.jsx)(S.A,{className:"ml-2 h-4 w-4 shrink-0 opacity-50"})]})}),(0,a.jsx)(g.hl,{className:"w-full p-0",align:"start",style:{width:"var(--radix-popover-trigger-width)"},children:(0,a.jsxs)(v.uB,{shouldFilter:!1,children:[(0,a.jsx)(v.G7,{placeholder:R("inventory.stockLevels.form.fields.product.searchPlaceholder"),value:G,onValueChange:Z}),(0,a.jsxs)(v.oI,{children:[(0,a.jsx)(v.xL,{children:R("inventory.stockLevels.form.fields.product.empty")}),(0,a.jsx)(v.L$,{children:F.filter(e=>{var t;if(!G)return!0;let s=G.toLowerCase();return e.name.toLowerCase().includes(s)||(null===(t=e.sku)||void 0===t?void 0:t.toLowerCase().includes(s))}).map(e=>(0,a.jsxs)(v.h_,{value:e.product_id,onSelect:e=>{M("product_id",e),X(!1),Z("")},children:[(0,a.jsx)(C.A,{className:"mr-2 h-4 w-4 ".concat(W.product_id===e.product_id?"opacity-100":"opacity-0")}),(0,a.jsxs)("div",{className:"flex flex-col",children:[(0,a.jsx)("span",{className:"font-medium",children:e.name}),(0,a.jsx)("span",{className:"text-xs text-muted-foreground",children:R("inventory.stockLevels.form.fields.product.skuLabel",{sku:e.sku})})]})]},e.product_id))})]})]})})]}),H.product_id&&(0,a.jsxs)("p",{className:"text-sm text-red-600 flex items-center gap-1",children:[(0,a.jsx)(k.A,{className:"h-3 w-3"})," ",H.product_id.message]})]}),(0,a.jsxs)("div",{className:"space-y-2",children:[(0,a.jsxs)(h.J,{htmlFor:"storage_id",className:"text-sm font-semibold flex items-center gap-1",children:[R("inventory.stockLevels.form.fields.location.label")," ",(0,a.jsx)("span",{className:"text-red-500",children:"*"}),W.storage_id&&!H.storage_id&&(0,a.jsx)(A.A,{className:"h-3 w-3 text-green-600"})]}),"create"===T&&1===z.length?(0,a.jsxs)("div",{className:"rounded-lg border border-purple-200 bg-purple-50/60 px-4 py-3 text-sm",children:[(0,a.jsxs)("div",{className:"flex items-center gap-2",children:[(0,a.jsx)(_.A,{className:"h-4 w-4 text-purple-600 shrink-0"}),(0,a.jsxs)("div",{children:[(0,a.jsx)("span",{className:"font-semibold",children:z[0].name}),(0,a.jsx)("code",{className:"ml-2 text-xs bg-white text-purple-800 px-2 py-0.5 rounded",children:z[0].code})]})]}),z[0].path?(0,a.jsx)("p",{className:"text-xs text-muted-foreground font-mono mt-2 pl-6",children:z[0].path}):null]}):(0,a.jsxs)(p.l6,{value:W.storage_id,onValueChange:e=>M("storage_id",e),disabled:en,children:[(0,a.jsx)(p.bq,{className:"focus:ring-blue-500 ".concat(H.storage_id?"border-red-500":""),children:(0,a.jsx)(p.yv,{placeholder:R("inventory.stockLevels.form.fields.location.placeholder")})}),(0,a.jsx)(p.gC,{children:z.map(e=>(0,a.jsx)(p.eb,{value:e.storage_id,children:(0,a.jsxs)("div",{className:"flex flex-col items-start",children:[(0,a.jsxs)("div",{className:"flex items-center gap-2",children:[(0,a.jsx)("code",{className:"text-xs bg-purple-50 text-purple-700 px-2 py-0.5 rounded",children:e.code}),(0,a.jsx)("span",{className:"font-medium",children:e.name})]}),e.path&&(0,a.jsx)("span",{className:"text-xs text-muted-foreground font-mono",children:e.path})]})},e.storage_id))})]}),H.storage_id&&(0,a.jsxs)("p",{className:"text-sm text-red-600 flex items-center gap-1",children:[(0,a.jsx)(k.A,{className:"h-3 w-3"})," ",H.storage_id.message]})]})]})]}),(0,a.jsxs)(j.Zp,{className:"border-green-200 shadow-md hover:shadow-lg transition-shadow",children:[(0,a.jsxs)(j.aR,{className:"bg-gradient-to-r from-green-50 to-transparent",children:[(0,a.jsx)(j.ZB,{className:"flex items-center gap-2 text-lg",children:R("inventory.stockLevels.form.sections.quantities.title")}),(0,a.jsx)(j.BT,{children:R("inventory.stockLevels.form.sections.quantities.description")})]}),(0,a.jsx)(j.Wu,{className:"space-y-5 pt-6",children:(0,a.jsxs)("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-5",children:[(0,a.jsxs)("div",{className:"space-y-2",children:[(0,a.jsxs)(h.J,{htmlFor:"quantity",className:"text-sm font-semibold flex items-center gap-1",children:[R("inventory.stockLevels.form.fields.quantity.label")," ",(0,a.jsx)("span",{className:"text-red-500",children:"*"})]}),(0,a.jsx)(x.p,{id:"quantity",type:"number",step:"1",placeholder:R("inventory.stockLevels.form.fields.quantity.placeholder"),...$("quantity"),disabled:en,className:"focus-visible:ring-green-500 ".concat(H.quantity?"border-red-500":""),min:"0"}),H.quantity&&(0,a.jsx)("p",{className:"text-sm text-red-600",children:H.quantity.message})]}),(0,a.jsxs)("div",{className:"space-y-2",children:[(0,a.jsx)(h.J,{htmlFor:"reserved_qty",className:"text-sm font-semibold",children:R("inventory.stockLevels.form.fields.reserved.label")}),(0,a.jsx)(x.p,{id:"reserved_qty",type:"number",step:"1",pla
1ceholder:R("inventory.stockLevels.form.fields.reserved.placeholder"),...$("reserved_qty"),disabled:en,className:"focus-visible:ring-green-500 ".concat(H.reserved_qty?"border-red-500":""),min:"0"}),(0,a.jsx)("p",{className:"text-xs text-muted-foreground",children:R("inventory.stockLevels.form.fields.reserved.helper")}),H.reserved_qty&&(0,a.jsx)("p",{className:"text-sm text-red-600",children:H.reserved_qty.message})]})]})})]}),(0,a.jsxs)(j.Zp,{className:"border-orange-200 shadow-md hover:shadow-lg transition-shadow",children:[(0,a.jsxs)(j.aR,{className:"bg-gradient-to-r from-orange-50 to-transparent",children:[(0,a.jsx)(j.ZB,{className:"flex items-center gap-2 text-lg",children:R("inventory.stockLevels.form.sections.quality.title")}),(0,a.jsx)(j.BT,{children:R("inventory.stockLevels.form.sections.quality.description")})]}),(0,a.jsxs)(j.Wu,{className:"space-y-5 pt-6",children:[(0,a.jsxs)("div",{className:"space-y-2",children:[(0,a.jsxs)(h.J,{htmlFor:"quality_status",className:"text-sm font-semibold",children:[R("inventory.stockLevels.form.fields.qualityStatus.label")," ",(0,a.jsx)("span",{className:"text-red-500",children:"*"})]}),(0,a.jsxs)(p.l6,{value:W.quality_status,onValueChange:e=>M("quality_status",e),disabled:en,children:[(0,a.jsx)(p.bq,{className:"focus:ring-orange-500",children:(0,a.jsx)(p.yv,{placeholder:R("inventory.stockLevels.form.fields.qualityStatus.placeholder")})}),(0,a.jsxs)(p.gC,{children:[(0,a.jsx)(p.eb,{value:"ok",children:(0,a.jsxs)("span",{className:"flex items-center gap-2",children:[(0,a.jsx)("span",{className:"text-green-600",children:"â"}),(0,a.jsx)("span",{children:R("inventory.stockLevels.qualityStatus.okOption")})]})}),(0,a.jsx)(p.eb,{value:"qc_hold",children:(0,a.jsxs)("span",{className:"flex items-center gap-2",children:[(0,a.jsx)("span",{className:"text-amber-600",children:"â¸"}),(0,a.jsx)("span",{children:R("inventory.stockLevels.qualityStatus.qcHoldOption")})]})}),(0,a.jsx)(p.eb,{value:"reject",children:(0,a.jsxs)("span",{className:"flex items-center gap-2",children:[(0,a.jsx)("span",{className:"text-red-600",children:"â"}),(0,a.jsx)("span",{children:R("inventory.stockLevels.qualityStatus.rejectOption")})]})})]})]})]}),(0,a.jsxs)(I.P.div,{whileHover:{scale:1.02},className:"flex items-center justify-between p-4 rounded-lg border border-red-200 bg-red-50/50 hover:bg-red-50 transition-colors",children:[(0,a.jsxs)("div",{className:"space-y-1 flex-1",children:[(0,a.jsx)(h.J,{htmlFor:"is_quarantine",className:"font-semibold text-red-900 cursor-pointer",children:R("inventory.stockLevels.form.fields.quarantine.label")}),(0,a.jsx)("p",{className:"text-sm text-red-700",children:R("inventory.stockLevels.form.fields.quarantine.description")})]}),(0,a.jsx)(f.d,{id:"is_quarantine",checked:W.is_quarantine,onCheckedChange:e=>M("is_quarantine",e),disabled:en,className:"data-[state=checked]:bg-red-500"})]})]})]}),(0,a.jsxs)(I.P.div,{initial:{opacity:0,y:20},animate:{opacity:1,y:0},transition:{delay:.3},className:"flex items-center justify-end gap-4 pt-4 border-t",children:[(0,a.jsx)(m.$,{type:"button",variant:"outline",onClick:l,disabled:en,size:"lg",children:R("inventory.stockLevels.form.actions.cancel")}),(0,a.jsx)(m.$,{type:"submit",disabled:en,size:"lg",className:"bg-gradient-to-r from-green-600 to-emerald-600 hover:from-green-700 hover:to-emerald-700 min-w-[180px]",children:en?(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(E.A,{className:"h-5 w-5 mr-2 animate-spin"}),R("inventory.stockLevels.form.actions.saving")]}):(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(A.A,{className:"h-5 w-5 mr-2"}),"create"===T?R("inventory.stockLevels.form.actions.create"):R("inventory.stockLevels.form.actions.save")]})})]})]})};var F=s(951344),z=s(138002),P=s(225121),V=s(309666),D=s(814081),R=s(949688),B=s(138684),$=s(306621),J=s(845350),U=s(930814),M=s(368050);let H=new Set(["name","sku","stock_quantity"]);function Q(e,t,s){let a=new Blob([t],{type:s}),r=window.URL.createObjectURL(a),n=document.createElement("a");n.href=r,n.download=e,document.body.appendChild(n),n.click(),document.body.removeChild(n),window.URL.revokeObjectURL(r)}function W(){return"SKU,Nom du produit,Quantit\xe9 en stock\nPROD-001,Extrait de Parfum Axe,50\nPROD-002,Sac \xe0 dos randonn\xe9e 30L,12\nPROD-003,Stylo bille noir,250"}function K(e){let{open:t,onOpenChange:s,onSuccess:i,warehouses:o=[]}=e,{t:l}=(0,n.Bd)(),{currentAccount:c}
1=(0,$.i)(),d=null==c?void 0:c.company_id,[u,x]=(0,r.useState)("upload"),[v,g]=(0,r.useState)(null),[f,y]=(0,r.useState)(!1),[j,b]=(0,r.useState)(null),[N,w]=(0,r.useState)(null),[q,L]=(0,r.useState)(null),[S,C]=(0,r.useState)(!1),I=(0,r.useMemo)(()=>{if(!o.length)return"";let e=o.find(e=>e.is_default);return(null!=e?e:o[0]).storage_id},[o]),[O,T]=(0,r.useState)("");(0,r.useEffect)(()=>{t&&T(I)},[t,I]);let K=(0,r.useRef)(null),X=(0,r.useRef)(0),G=(0,r.useRef)(0),Z=(0,r.useRef)(0),Y=(0,r.useRef)(0);(0,r.useEffect)(()=>()=>{K.current&&clearInterval(K.current)},[]);let ee=()=>{K.current&&(clearInterval(K.current),K.current=null)},et=e=>{Y.current=0,X.current=Date.now(),G.current=0,Z.current=Date.now(),setTimeout(()=>{K.current=setInterval(async()=>{Y.current++;let t=Date.now();if(t-X.current>M.pS){ee(),U.toast.error(l("inventory.stockImport.timeoutError",{defaultValue:"L'import a pris trop de temps. V\xe9rifiez que le worker Celery est actif."})),b({success:!1,error:"Timeout: le worker d'import ne r\xe9pond pas. Contactez l'administrateur."}),x("result"),C(!1);return}try{let s=await (0,J.T$)(e);if("success"===s.status&&s.data){let e=s.data;w(e);let a=e.current_row||0;if(a>G.current)G.current=a,Z.current=t;else if("running"===e.status&&t-Z.current>M.Bl){ee(),U.toast.error(l("inventory.stockImport.stagnationError",{defaultValue:"L'import semble bloqu\xe9. V\xe9rifiez que le service d'import est actif."})),b({success:!1,error:"Import bloqu\xe9 : aucune progression depuis plusieurs minutes."}),x("result"),C(!1);return}if("pending"===e.status&&t-X.current>M.rT){ee(),U.toast.error(l("inventory.stockImport.workerNotRunning",{defaultValue:"Le worker d'import (Celery) ne d\xe9marre pas. Contactez l'administrateur."})),b({success:!1,error:"Le worker d'import (Celery) ne r\xe9pond pas. La t\xe2che reste en attente."}),x("result"),C(!1);return}"completed"===e.status?(ee(),b(e.result),x("result"),C(!1),U.toast.success(l("inventory.stockImport.completed",{defaultValue:"Import du stock termin\xe9 !"})),setTimeout(()=>{null==i||i()},1200)):("failed"===e.status||"cancelled"===e.status)&&(ee(),b(e.result||{success:!1,error:e.error||"Import \xe9chou\xe9"}),x("result"),C(!1))}}catch(t){let e=t instanceof Error?t.message:"";if((e.includes("non trouv\xe9e")||e.includes("not found"))&&Y.current<10)return;Y.current>=20&&(ee(),U.toast.error(l("inventory.stockImport.statusError",{defaultValue:"Impossible de r\xe9cup\xe9rer le statut"})),x("upload"),C(!1))}},1e3)},500)},es=async()=>{if(v&&d)try{var e;C(!0),x("importing");let t=await (0,J.mq)(v);if("success"!==t.status||!t.data)throw Error(l("inventory.stockImport.analyzeError",{defaultValue:"Impossible d'analyser le fichier"}));let s=t.data,a={};for(let[e,t]of Object.entries(s.suggested_mapping||{}))H.has(t)&&(a[e]=t);let r=new Set(Object.values(a));if(!r.has("name"))throw Error(l("inventory.stockImport.missingName",{defaultValue:"Aucune colonne \xab Nom du produit \xbb d\xe9tect\xe9e dans le fichier."}));if(!r.has("stock_quantity"))throw Error(l("inventory.stockImport.missingQuantity",{defaultValue:"Aucune colonne \xab Quantit\xe9 \xbb d\xe9tect\xe9e dans le fichier."}));let n=await (0,J.importProductsWithMapping)(v,d,a,O||null);if("accepted"===n.status&&(null===(e=n.data)||void 0===e?void 0:e.task_id))L(n.data.task_id),w({task_id:n.data.task_id,company_id:d,user_id:"",file_name:n.data.file_name,status:"pending",total_rows:n.data.total_rows,current_row:0,percentage:0,created_at:new Date().toISOString(),started_at:null,completed_at:null,result:null,error:null}),et(n.data.task_id);else throw Error(l("inventory.stockImport.launchError",{defaultValue:"Erreur lors du lancement de l'import"}))}catch(t){let e=t instanceof Error?t.message:l("inventory.stockImport.launchError",{defaultValue:"Erreur lors du lancement de l'import"});U.toast.error(e),b({success:!1,error:e}),x("result"),C(!1)}},ea=async()=>{if(q&&S)try{await (0,J.rk)(q),U.toast.success(l("inventory.stockImport.cancelled",{defaultValue:"Import annul\xe9"})),ee(),C(!1),x("upload")}catch(e){U.toast.error(l("inventory.stockImport.cancelError",{defaultValue:"Erreur lors de l'annulation"}))}else er()},er=()=>{ee(),g(null),b(null),x("upload"),C(!1),w(null),L(null),s(!1)};return(0,a.jsx)(F.lG,{open:t,onOpenChange:er,children:(0,a.jsxs)(F.Cf,{className:"sm:max-w-[720px] max-h-[90vh] overflow-y-auto",children:[(0,a.jsxs)(F.c7,{children:[(0,a.jsxs)(F.L3,{className:"flex items-center gap-2",children:[(0,a.jsx)(V.A,{className:"h-5 w-5"}),l("inventory.stockImport.title",{defaultValue:"Importer du stock"})]}),(0,a.jsxs)(F.rr,{children:["importing"===u&&l("inventory.stockImport.inProgress",{defaultValue:"Import en cours..."}),"result"===u&&l("inventory.stockImport.result",{defaultValue:"R\xe9sultat de l'import"})]})]}),(0,a.jsxs)("div",{className:"space-y-6 py-4",children:["upload"===u&&(0,a.jsxs)(a.Fragment,{children:[(0,a.jsxs)("div",{className:"space-y-3",children:[(0,a.jsx)(h.J,{className:"text-base font-semibold",children:l("inventory.stockImport.downloadExampleTitle",{defaultValue:"1. T\xe9l\xe9chargez un exemple"})}),(0,a.jsx)("p",{className:"text-sm text-muted-foreground",children:l("inventory.stockImport.downloadExampleDesc",{defaultValue:"3 colonnes uniquement : SKU, Nom du produit, Quantit\xe9 en stock."})}),(0,a.jsxs)("div",{className:"flex gap-3",children:[(0,a.jsxs)(m.$,{variant:"outline",size:"sm",onClick:()=>Q("stock_import_example.csv",W(),"text/csv;charset=utf-8"),children:[(0,a.jsx)(D.A,{className:"h-4 w-4 mr-2"}),l("inventory.stockImport.downloadCSV",{defaultValue:"T\xe9l\xe9charger CSV"})]}),(0,a.jsxs)(m.$,{variant:"outline",size:"sm",onClick:()=>Q("stock_import_example.csv",W(),"text/csv;charset=utf-8"),title:l("inventory.stockImport.excelHint",{defaultValue:"Excel ouvre directement le fichier CSV : importez-le puis enregistrez-le en .xlsx si besoin."}),children:[(0,a.jsx)(R.A,{className:"h-4 w-4 mr-2"}),l("inventory.stockImport.downloadExcel",{defaultValue:"Mod\xe8le Excel"})]})]})]}),(0,a.jsxs)("div",{className:"space-y-3",children:[(0,a.jsx)(h.J,{className:"text-base font-semibold",children:l("inventory.stockImport.uploadTitle",{defaultValue:"2. Importez votre fichier"})}),(0,a.jsxs)("div",{className:"\n border-2 border-dashed rounded-lg p-8 text-center cursor-pointer\n transition-colors duration-200\n ".concat(f?"border-teal-500 bg-teal-50":"border-gray-300","\n ").concat(v?"bg-green-50 border-green-500":"hover:border-gray-400","\n "),onDrop:e=>{e.preventDefault(),y(!1);let t=e.dataTransfer.files[0];t&&(t.name.endsWith(".csv")||t.name.endsWith(".xlsx")||t.name.endsWith(".xls"))&&(g(t),b(null))},onDragOver:e=>{e.preventDefault(),y(!0)},onDragLeave:()=>y(!1),onClick:()=>{var e;return null===(e=document.getElementById("stock-file-upload"))||void 0===e?void 0:e.click()},children:[(0,a.jsx)("input",{id:"stock-file-upload",type:"file",className:"hidden",accept:".csv,.xlsx,.xls",onChange:e=>{var t;let s=null===(t=e.target.files)||void 0===t?void 0:t[0];
1s&&(g(s),b(null))}}),v?(0,a.jsxs)("div",{className:"flex flex-col items-center gap-2",children:[(0,a.jsx)(A.A,{className:"h-12 w-12 text-green-600"}),(0,a.jsx)("p",{className:"font-medium",children:v.name}),(0,a.jsxs)("p",{className:"text-sm text-muted-foreground",children:[(v.size/1024).toFixed(2)," KB"]}),(0,a.jsx)(m.$,{variant:"ghost",size:"sm",onClick:e=>{e.stopPropagation(),g(null)},children:l("inventory.stockImport.changeFile",{defaultValue:"Changer de fichier"})})]}):(0,a.jsxs)("div",{className:"flex flex-col items-center gap-2",children:[(0,a.jsx)(B.A,{className:"h-12 w-12 text-gray-400"}),(0,a.jsx)("p",{className:"font-medium",children:l("inventory.stockImport.dropFile",{defaultValue:"Glissez votre fichier ici ou cliquez pour s\xe9lectionner"})}),(0,a.jsx)("p",{className:"text-sm text-muted-foreground",children:l("inventory.stockImport.acceptedFormats",{defaultValue:"Formats accept\xe9s : CSV, XLSX, XLS"})})]})]})]}),o.length>1?(0,a.jsxs)("div",{className:"space-y-2",children:[(0,a.jsxs)(h.J,{className:"text-base font-semibold flex items-center gap-2",children:[(0,a.jsx)(_.A,{className:"h-4 w-4 text-teal-600"}),l("inventory.stockImport.warehouseSelectTitle",{defaultValue:"3. Entrep\xf4t cible"})]}),(0,a.jsxs)(p.l6,{value:O,onValueChange:T,children:[(0,a.jsx)(p.bq,{children:(0,a.jsx)(p.yv,{placeholder:l("inventory.stockImport.warehousePlaceholder",{defaultValue:"S\xe9lectionner un entrep\xf4t"})})}),(0,a.jsx)(p.gC,{children:o.map(e=>(0,a.jsx)(p.eb,{value:e.storage_id,children:(0,a.jsxs)("span",{className:"flex items-center gap-2",children:[(0,a.jsx)(_.A,{className:"h-3.5 w-3.5 text-teal-600"}),e.name,e.code?(0,a.jsxs)("span",{className:"text-xs text-muted-foreground",children:["(",e.code,")"]}):null,e.is_default?(0,a.jsx)(P.E,{variant:"secondary",className:"ml-1 text-[10px] py-0 h-4",children:l("inventory.stockImport.warehouseDefault",{defaultValue:"d\xe9faut"})}):null]})},e.storage_id))})]})]}):1===o.length?(0,a.jsxs)("div",{className:"rounded-md border bg-teal-50 border-teal-200 p-3 text-sm flex items-center gap-2 text-teal-900",children:[(0,a.jsx)(_.A,{className:"h-4 w-4 shrink-0"}),(0,a.jsxs)("span",{children:[l("inventory.stockImport.warehouseUniqueLabel",{defaultValue:"Entrep\xf4t :"})," ",(0,a.jsx)("span",{className:"font-semibold",children:o[0].name}),o[0].code?(0,a.jsxs)("span",{className:"text-xs text-teal-700",children:[" (",o[0].code,")"]}):null]})]}):null,(0,a.jsx)("div",{className:"flex justify-end pt-4 border-t",children:(0,a.jsx)(m.$,{onClick:es,disabled:!v||S||!d,className:"bg-gradient-to-r from-teal-600 to-cyan-600 hover:from-teal-700 hover:to-cyan-700",children:S?(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(E.A,{className:"h-4 w-4 mr-2 animate-spin"}),l("inventory.stockImport.starting",{defaultValue:"D\xe9marrage..."})]}):(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(B.A,{className:"h-4 w-4 mr-2"}),l("inventory.stockImport.startImport",{defaultValue:"Lancer l'import"})]})})})]}),"importing"===u&&(0,a.jsxs)("div",{className:"space-y-6",children:[N&&(0,a.jsxs)("div",{className:"space-y-3 p-4 bg-teal-50 rounded-lg border border-teal-200",children:[(0,a.jsxs)("div",{className:"flex items-center justify-between",children:[(0,a.jsxs)("div",{className:"flex items-center gap-2",children:[(0,a.jsx)(E.A,{className:"h-4 w-4 animate-spin text-teal-600"}),(0,a.jsx)("span",{className:"font-medium text-teal-900",children:l("inventory.stockImport.progressStatus",{percentage:N.percentage,defaultValue:"Import en cours... ".concat(N.percentage,"%")})})]}),(0,a.jsx)("span",{className:"text-sm text-teal-700",children:l("inventory.stockImport.progressRows",{current:N.current_row,total:N.total_rows,defaultValue:"".concat(N.current_row," / ").concat(N.total_rows," lignes")})})]}),(0,a.jsx)("div",{className:"w-full bg-teal-200 rounded-full h-2.5",children:(0,a.jsx)("div",{className:"bg-teal-600 h-2.5 rounded-full transition-all duration-300",style:{width:"".concat(N.percentage,"%")}})})]}),(0,a.jsx)("div",{className:"flex justify-center",children:(0,a.jsx)(m.$,{variant:"outline",onClick:ea,children:l("inventory.stockImport.cancel",{defaultValue:"Annuler l'import"})})})]}),"result"===u&&j&&(0,a.jsxs)("div",{className:"space-y-4",children:[(0,a.jsxs)(z.Fc,{variant:j.success?"default":"destructive",children:[j.success?(0,a.jsx)(A.A,{className:"h-4 w-4"}):(0,a.jsx)(k.A,{className:"h-4 w-4"}),(0,a.jsx)(z.TN,{children:j.success&&j.stats?(0,a.jsxs)("div",{className:"space-y-2",children:[(0,a.jsx)("p",{className:"font-semibold",children:l("inventory.stockImport.completed",{defaultValue:"Import du stock termin\xe9 !"})}),(0,a.jsxs)("div",{className:"grid grid-cols-2 gap-2 text-sm",children:[(0,a.jsxs)("div",{children:[l("inventory.stockImport.totalRows",{defaultValue:"Lignes totales"}),": ",j.stats.total_rows]}),(0,a.jsxs)("div",{children:[l("inventory.stockImport.processed",{defaultValue:"Produits/stocks trait\xe9s"}),": ",j.stats.products_created]}),(0,a.jsxs)("div",{children:[l("inventory.stockImport.skipped",{defaultValue:"Ignor\xe9es"}),": ",j.stats.skipped]}),(0,a.jsxs)("div",{children:[l("inventory.stockImport.errorsCount",{defaultValue:"Erreurs"}),": ",j.stats.errors_count]})]}),j.errors&&j.errors.length>0&&(0,a.jsxs)("details",{className:"mt-3",children:[(0,a.jsx)("summary",{className:"cursor-pointer text-sm text-amber-600 hover:text-amber-700",children:l("inventory.stockImport.viewErrors",{count:j.errors.length,defaultValue:"Voir les ".concat(j.errors.length," erreurs")})}),(0,a.jsx)("div",{className:"mt-2 text-xs space-y-1 max-h-40 overflow-y-auto",children:j.errors.map((e,t)=>(0,a.jsx)("div",{className:"text-red-600",children:e},t))})]})]}):(0,a.jsxs)("div",{children:[(0,a.jsx)("p",{className:"font-semibold",children:l("inventory.stockImport.failed",{defaultValue:"\xc9chec de l'import"})}),(0,a.jsx)("p",{className:"text-sm mt-1",children:j.error}),j.missing_columns&&j.missing_columns.length>0&&(0,a.jsxs)("div",{className:"mt-2",children:[(0,a.jsx)("p",{className:"text-sm font-medium",children:l("inventory.stockImport.missingColumns",{defaultValue:"Colonnes manquantes :"})}),(0,a.jsx)("div",{className:"flex flex-wrap gap-1 mt-1",children:j.missing_columns.map(e=>
1(0,a.jsx)(P.E,{variant:"destructive",className:"text-xs",children:e},e))})]})]})})]}),(0,a.jsxs)("div",{className:"flex justify-between pt-4 border-t",children:[(0,a.jsx)(m.$,{variant:"outline",onClick:()=>{x("upload"),b(null),w(null)},children:l("inventory.stockImport.importAnother",{defaultValue:"Importer un autre fichier"})}),(0,a.jsx)(m.$,{onClick:er,children:l("inventory.stockImport.close",{defaultValue:"Fermer"})})]})]})]})]})})}var X=s(634029),G=s(872508),Z=s(530098),Y=s(779715),ee=s(540767),et=s(545686),es=s(372288),ea=s(121276);let er=ea.A.OCR_API_URL;async function en(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=await (0,es.getAuthToken)();return{Authorization:t?"Bearer ".concat(t):"",...e}}async function ei(e){let t="Erreur";try{let s=await e.json();t=(null==s?void 0:s.detail)||(null==s?void 0:s.message)||JSON.stringify(s)}catch(s){t=await e.text().catch(()=>"")||"HTTP ".concat(e.status)}throw Error(t)}async function eo(e,t){if(!e.length)throw Error("Aucun fichier fourni");let s=new FormData;e.forEach(e=>s.append("files",e));let a="".concat(er,"/stock-ocr/extract?company_id=").concat(encodeURIComponent(t)),r=await fetch(a,{method:"POST",headers:await en(),body:s});return r.ok||await ei(r),r.json()}async function el(e){var t;let s=await fetch("".concat(er,"/stock-ocr/status/").concat(e),{method:"GET",headers:await en({"Content-Type":"application/json"})});s.ok||await ei(s);let a=await s.json();return null!==(t=null==a?void 0:a.data)&&void 0!==t?t:a}async function ec(e,t,s){let a="".concat(er,"/stock-ocr/validate/").concat(e,"?company_id=").concat(encodeURIComponent(t)),r=await fetch(a,{method:"POST",headers:await en({"Content-Type":"application/json"}),body:JSON.stringify(s)});return r.ok||await ei(r),r.json()}async function ed(e){let t=await fetch("".concat(er,"/stock-ocr/cancel/").concat(e),{method:"POST",headers:await en({"Content-Type":"application/json"})});return t.ok||await ei(t),t.json()}var eu=s(593474),em=s(803852);let ex=[".pdf",".png",".jpg",".jpeg",".webp",".bmp",".tiff"];function eh(){return"r-".concat(Date.now(),"-").concat(Math.random().toString(36).slice(2,8))}function ep(e){let t=e.lastIndexOf(".");return t>=0?e.slice(t).toLowerCase():""}function ev(e){var t,s;let{open:i,onOpenChange:o,companyId:l,warehouses:c=[],onSuccess:d}=e,{t:u}=(0,n.Bd)(),[v,g]=(0,r.useState)("upload"),[f,y]=(0,r.useState)([]),[j,b]=(0,r.useState)(!1),[w,L]=(0,r.useState)(null),[S,C]=(0,r.useState)(null),[I,O]=(0,r.useState)([]),T=(0,r.useMemo)(()=>{if(!c.length)return"";let e=c.find(e=>e.is_default);return(null!=e?e:c[0]).storage_id},[c]),[F,V]=(0,r.useState)(""),[R,$]=(0,r.useState)([]),[J,M]=(0,r.useState)([]),[H,Q]=(0,r.useState)(null),{quota:W,loading:K,recordSuccess:es,checkQuota:ea}=(0,eu.i)(l);(0,r.useEffect)(()=>{i&&l&&ea()},[i,l,ea]);let er=null!==W&&null!==W.limit&&(null!==(t=W.remaining)&&void 0!==t?t:0)===0,en=(null==W?void 0:W.limit)===null?1/0:null!==(s=null==W?void 0:W.remaining)&&void 0!==s?s:1/0,ei=f.length>en;(0,r.useEffect)(()=>{i&&V(T)},[i,T]);let ev=(0,r.useRef)(null),eg=(0,r.useCallback)(()=>{ev.current&&(clearTimeout(ev.current),ev.current=null)},[]),ef=(0,r.useCallback)(()=>{eg(),g("upload"),y([]),L(null),C(null),O([]),V(T),$([]),M([]),Q(null)},[T,eg]);(0,r.useEffect)(()=>{i||ef()},[i,ef]),(0,r.useEffect)(()=>()=>eg(),[eg]);let ey=(0,r.useCallback)(e=>{Q(null);let t=[],s=[];for(let a of e){let e=ep(a.name);if(!ex.includes(e)){s.push(u("inventory.stockOcrImport.unsupportedFormat",{name:a.name}));continue}if(a.size>0x1900000){s.push(u("inventory.stockOcrImport.fileTooLarge",{name:a.name}));continue}t.push(a)}y(e=>{let a=[...e,...t].slice(0,10);return e.length+t.length>10&&s.push(u("inventory.stockOcrImport.maxFiles",{max:10})),a}),s.length&&Q(s.join(" ⢠"))},[u]),ej=e=>{y(t=>t.filter((t,s)=>s!==e))},e_=(0,r.useCallback)(async e=>{let t=(e.items||[]).map(e=>({...e,_localKey:e.id||eh()}));O(t),g("review");let s=(e.files_report||[]).filter(e=>"ok"===e.ocr_status).length;for(let e=0;e<s;e++)await es();0===t.length?U.toast.warning(u("inventory.stockOcrImport.noProductDetected"),{description:u("inventory.stockOcrImport.noProductDetectedDesc")}):U.toast.success(u("inventory.stockOcrImport.productsDetected",{count:t.length}))},[es]),ek=(0,r.useCallback)(e=>{$(e.apply_results||[]),M(e.apply_errors||[]),g("result");let t=e.apply_created||0,s=e.apply_updated||0;t+s>0?(U.toast.success(u("inventory.stockOcrImport.stockFed"),{description:u("inventory.stockOcrImport.stockFedDesc",{created:t,updated:s})}),null==d||d()):U.toast.error(u("inventory.stockOcrImport.noneApplied"),{description:e.message||u("inventory.stockOcrImport.checkErrors")})},[d]),eb=(0,r.useCallback)(async e=>{try{let t=await el(e);if(C(t),"failed"===t.status||"cancelled"===t.status){eg(),Q(t.error||t.message||u("inventory.stockOcrImport.task
1Failed")),g("upload"),U.toast.error(t.error||t.message||u("inventory.stockOcrImport.ocrFailed"));return}if("extract"===t.phase&&"completed"===t.status){eg(),e_(t);return}if("apply"===t.phase&&"completed"===t.status){eg(),ek(t);return}ev.current=setTimeout(()=>void eb(e),2500)}catch(t){eg();let e=t instanceof Error?t.message:u("inventory.stockOcrImport.pollingError");Q(e),g("upload"),U.toast.error(e)}},[ek,e_,eg]),eN=async()=>{if(!l){Q(u("inventory.stockOcrImport.companyMissing"));return}if(0===f.length){Q(u("inventory.stockOcrImport.selectFile"));return}if(er){Q(u("inventory.stockOcrImport.quotaExhausted"));return}if(ei){Q(u("inventory.stockOcrImport.quotaInsufficient",{remaining:null==W?void 0:W.remaining,count:f.length}));return}Q(null),g("extracting");try{let e=(await eo(f,l)).data.task_id;L(e),eb(e)}catch(t){let e=t instanceof Error?t.message:u("inventory.stockOcrImport.launchError");Q(e),g("upload"),U.toast.error(e)}},ew=(e,t,s)=>{O(a=>a.map(a=>{if(a._localKey!==e)return a;if("quantity"===t){let e=Number(s.replace(",","."));return{...a,quantity:Number.isFinite(e)?e:0}}return{...a,[t]:s||null}}))},eq=e=>{O(t=>t.filter(t=>t._localKey!==e))},eE=(0,r.useMemo)(()=>{let e=[];return I.forEach((t,s)=>{t.name&&t.name.trim()||e.push(u("inventory.stockOcrImport.lineNameRequired",{line:s+1})),(!t.quantity||t.quantity<=0)&&e.push(u("inventory.stockOcrImport.lineQuantityRequired",{line:s+1}))}),e},[I,u]),eL=async()=>{if(w&&l){if(eE.length){Q(eE.slice(0,3).join(" ⢠"));return}Q(null),g("applying");try{await ec(w,l,{items:I.map(e=>{var t,s,a;return{name:e.name.trim(),sku:(null===(t=e.sku)||void 0===t?void 0:t.trim())||null,barcode:(null===(s=e.barcode)||void 0===s?void 0:s.trim())||null,quantity:Number(e.quantity),unit:(null===(a=e.unit)||void 0===a?void 0:a.trim())||null}}),storage_id:F||null}),eb(w)}catch(t){let e=t instanceof Error?t.message:u("inventory.stockOcrImport.applyError");Q(e),g("review"),U.toast.error(e)}}},eA=async()=>{if(w)try{await ed(w)}catch(e){}eg(),o(!1)},eS=(e,t)=>{let s=(null==S?void 0:S.total_files)||0,r=(null==S?void 0:S.processed_files)||0,n=(null==S?void 0:S.apply_total)||0,i=(null==S?void 0:S.apply_processed)||0;return(0,a.jsxs)("div",{className:"py-8 text-center space-y-4",children:[(0,a.jsx)(E.A,{className:"h-12 w-12 text-indigo-600 animate-spin mx-auto"}),(0,a.jsxs)("div",{children:[(0,a.jsx)("p",{className:"text-lg font-semibold",children:e}),t&&(0,a.jsx)("p",{className:"text-sm text-muted-foreground",children:t}),(null==S?void 0:S.message)&&(0,a.jsx)("p",{className:"text-sm text-muted-foreground mt-2",children:S.message})]}),s>0&&"extracting"===v&&(0,a.jsx)(P.E,{variant:"outline",children:u("inventory.stockOcrImport.filesProcessed",{current:r,total:s})}),n>0&&"applying"===v&&(0,a.jsx)(P.E,{variant:"outline",children:u("inventory.stockOcrImport.appliedProgress",{current:i,total:n})}),(0,a.jsx)(m.$,{variant:"ghost",size:"sm",onClick:eA,children:u("inventory.stockOcrImport.cancel")})]})};return(0,a.jsx)(X.cj,{open:i,onOpenChange:o,children:(0,a.jsxs)(X.h,{side:"right",className:"w-full sm:max-w-[50vw] sm:min-w-[640px] overflow-y-auto p-6",children:[(0,a.jsxs)(X.Fm,{className:"space-y-3 pb-5 border-b",children:[(0,a.jsxs)("div",{className:"flex items-start justify-between gap-3",children:[(0,a.jsxs)(X.qp,{className:"flex items-center gap-2 text-xl",children:[(0,a.jsx)("div",{className:"p-1.5 rounded-md bg-gradient-to-br from-indigo-500 to-purple-600 text-white",children:(0,a.jsx)(Z.A,{className:"h-5 w-5"})}),u("inventory.stockOcrImport.title")]}),(0,a.jsx)(em.q,{quota:W,loading:K})]}),(0,a.jsx)(X.Qs,{children:u("inventory.stockOcrImport.description")})]}),(0,a.jsxs)("div",{className:"pt-5",children:["upload"===v&&(0,a.jsxs)("div",{className:"space-y-4",children:[(0,a.jsxs)("div",{onDragOver:e=>{e.preventDefault(),b(!0)},onDragLeave:()=>b(!1),onDrop:e=>{var t;e.preventDefault(),b(!1),(null===(t=e.dataTransfer.files)||void 0===t?void 0:t.length)&&ey(Array.from(e.dataTransfer.files))},className:"border-2 border-dashed rounded-xl p-8 text-center transition-colors ".concat(j?"border-indigo-500 bg-indigo-50":"border-slate-300 hover:border-indigo-400"),children:[(0,a.jsx)(Z.A,{className:"h-10 w-10 text-indigo-500 mx-auto mb-3"}),(0,a.jsxs)(h.J,{htmlFor:"stock-ocr-input",className:"inline-flex items-center gap-2 cursor-pointer rounded-md border border-indigo-300 bg-white px-4 py-2 text-sm font-medium text-indigo-700 hover:bg-indigo-50",children:[(0,a.jsx)(B.A,{className:"h-4 w-4"}),u("inventory.stockOcrImport.chooseFiles")]}),(0,a.jsx)(x.p,{id:"stock-ocr-input",type:"file",multiple:!0,accept:ex.join(","),className:"hidden",onChange:e=>{e.target.files&&(ey(Array.from(e.target.files)),e.target.value="")}}),(0,a.jsx)("p",{className:"text-xs text-muted-foreground mt-3",children:u("inventory.stockOcrImport.acceptedFormats",{max:10})})]}),c.length>1?(0,a.jsxs)("div",{className:"space-y-1.5",children:[(0,a.jsxs)(h.J,{className:"text-sm font-semibold flex items-center gap-2",children:[(0,a.jsx)(_.A,{className:"h-4 w-4 text-indigo-600"}),u("inventory.stockOcrImport.targetWarehouse")]}),(0,a.jsxs)(p.l6,{value:F,onValueChange:V,children:[(0,a.jsx)(p.bq,{children:(0,a.jsx)(p.yv,{placeholder:u("inventory.stockOcrImport.warehousePlaceholder")})}),(0,a.jsx)(p.gC,{children:c.map(e=>(0,a.jsx)(p.eb,{value:e.storage_id,children:(0,a.jsxs)("span",{className:"flex items-center gap-2",children:[(0,a.jsx)(_.A,{className:"h-3.5 w-3.5 text-indigo-600"}),e.name,e.code?(0,a.jsxs)("span",{className:"text-xs text-muted-foreground",children:["(",e.code,")"]}
1):null,e.is_default?(0,a.jsx)(P.E,{variant:"secondary",className:"ml-1 text-[10px] py-0 h-4",children:u("company.treasury.achat.warehousePicker.defaultBadge")}):null]})},e.storage_id))})]})]}):1===c.length?(0,a.jsxs)("div",{className:"rounded-md border bg-indigo-50 border-indigo-200 p-3 text-sm flex items-center gap-2 text-indigo-900",children:[(0,a.jsx)(_.A,{className:"h-4 w-4 shrink-0"}),(0,a.jsxs)("span",{children:[u("inventory.stockOcrImport.warehouseLabel")," ",(0,a.jsx)("span",{className:"font-semibold",children:c[0].name}),c[0].code?(0,a.jsxs)("span",{className:"text-xs text-indigo-700",children:[" (",c[0].code,")"]}):null]})]}):null,f.length>0&&(0,a.jsxs)("div",{className:"space-y-2",children:[(0,a.jsx)(h.J,{className:"text-sm font-semibold",children:u("inventory.stockOcrImport.filesCount",{count:f.length})}),(0,a.jsx)("div",{className:"space-y-1.5 max-h-48 overflow-y-auto",children:f.map((e,t)=>(0,a.jsxs)("div",{className:"flex items-center justify-between gap-2 rounded-md border bg-white px-3 py-2",children:[(0,a.jsxs)("div",{className:"flex items-center gap-2 min-w-0",children:[".pdf"===ep(e.name)||"application/pdf"===e.type?(0,a.jsx)(D.A,{className:"h-4 w-4 text-red-500 shrink-0"}):(0,a.jsx)(Y.A,{className:"h-4 w-4 text-blue-500 shrink-0"}),(0,a.jsx)("span",{className:"text-sm truncate",children:e.name}),(0,a.jsx)("span",{className:"text-xs text-muted-foreground shrink-0",children:u("inventory.stockOcrImport.fileSizeKb",{size:(e.size/1024).toFixed(0)})})]}),(0,a.jsx)(m.$,{type:"button",variant:"ghost",size:"sm",onClick:()=>ej(t),className:"h-7 w-7 p-0",children:(0,a.jsx)(ee.A,{className:"h-4 w-4"})})]},"".concat(e.name,"-").concat(t)))})]}),er&&(0,a.jsxs)(z.Fc,{variant:"destructive",children:[(0,a.jsx)(q.A,{className:"h-4 w-4"}),(0,a.jsxs)(z.TN,{children:[(0,a.jsx)("p",{className:"font-semibold mb-1",children:u("inventory.stockOcrImport.quotaExhaustedTitle")}),(0,a.jsx)("p",{className:"text-xs",children:u("inventory.stockOcrImport.quotaExhaustedDesc",{used:null==W?void 0:W.used,limit:null==W?void 0:W.limit})})]})]}),!er&&ei&&(null==W?void 0:W.limit)!==null&&(0,a.jsxs)(z.Fc,{className:"border-amber-300 bg-amber-50",children:[(0,a.jsx)(q.A,{className:"h-4 w-4 text-amber-600"}),(0,a.jsx)(z.TN,{className:"text-amber-900 text-xs",children:u("inventory.stockOcrImport.quotaWouldExceed",{count:f.length,remaining:null==W?void 0:W.remaining})})]}),H&&(0,a.jsxs)(z.Fc,{variant:"destructive",children:[(0,a.jsx)(k.A,{className:"h-4 w-4"}),(0,a.jsx)(z.TN,{children:H})]}),(0,a.jsxs)("div",{className:"flex justify-end gap-2",children:[(0,a.jsx)(m.$,{variant:"outline",onClick:()=>o(!1),children:u("inventory.stockOcrImport.cancel")}),(0,a.jsxs)(m.$,{onClick:eN,disabled:0===f.length||er||ei,className:"bg-gradient-to-r from-indigo-600 to-purple-600 hover:from-indigo-700 hover:to-purple-700",children:[(0,a.jsx)(Z.A,{className:"h-4 w-4 mr-2"}),u("inventory.stockOcrImport.startExtraction")]})]})]}),"extracting"===v&&eS(u("inventory.stockOcrImport.extracting"),u("inventory.stockOcrImport.extractingHint")),"review"===v&&(0,a.jsxs)("div",{className:"space-y-4",children:[(0,a.jsxs)(z.Fc,{className:"bg-indigo-50 border-indigo-200",children:[(0,a.jsx)(Z.A,{className:"h-4 w-4 text-indigo-600"}),(0,a.jsx)(z.TN,{className:"text-indigo-900",children:u("inventory.stockOcrImport.reviewHint",{count:I.length})})]}),c.length>1?(0,a.jsxs)("div",{className:"space-y-1.5",children:[(0,a.jsxs)(h.J,{className:"text-sm font-semibold flex items-center gap-2",children:[(0,a.jsx)(_.A,{className:"h-4 w-4 text-indigo-600"}),u("inventory.stockOcrImport.targetWarehouse")]}),(0,a.jsxs)(p.l6,{value:F,onValueChange:V,children:[(0,a.jsx)(p.bq,{children:(0,a.jsx)(p.yv,{})}),(0,a.jsx)(p.gC,{children:c.map(e=>(0,a.jsx)(p.eb,{value:e.storage_id,children:(0,a.jsxs)("span",{className:"flex items-center gap-2",children:[(0,a.jsx)(_.A,{className:"h-3.5 w-3.5 text-indigo-600"}),e.name,e.code?(0,a.jsxs)("span",{className:"text-xs text-muted-foreground",children:["(",e.code,")"]}):null,e.is_default?(0,a.jsx)(P.E,{variant:"secondary",className:"ml-1 text-[10px] py-0 h-4",children:u("company.treasury.achat.warehousePicker.defaultBadge")}):null]})},e.storage_id))})]})]}):1===c.length?(0,a.jsxs)("div",{className:"rounded-md border bg-indigo-50 border-indigo-200 p-3 text-sm flex items-center gap-2 text-indigo-900",children:[(0,a.jsx)(_.A,{className:"h-4 w-4 shrink-0"}),(0,a.jsxs)("span",{children:[u("inventory.stockOcrImport.warehouseLabel")," ",(0,a.jsx)("span",{className:"font-semibold",children:c[0].name}
1),c[0].code?(0,a.jsxs)("span",{className:"text-xs text-indigo-700",children:[" (",c[0].code,")"]}):null]})]}):null,(0,a.jsx)("div",{className:"border rounded-lg overflow-hidden",children:(0,a.jsx)("div",{className:"max-h-[400px] overflow-y-auto",children:(0,a.jsxs)(G.XI,{children:[(0,a.jsx)(G.A0,{className:"sticky top-0 bg-slate-50 z-10",children:(0,a.jsxs)(G.Hj,{children:[(0,a.jsx)(G.nd,{className:"w-[40%]",children:u("inventory.stockOcrImport.productName")}),(0,a.jsx)(G.nd,{className:"w-[20%]",children:u("inventory.stockOcrImport.skuOptional")}),(0,a.jsx)(G.nd,{className:"w-[15%] text-center",children:u("inventory.stockOcrImport.quantityRequired")}),(0,a.jsx)(G.nd,{className:"w-[15%]",children:u("inventory.stockOcrImport.unit")}),(0,a.jsx)(G.nd,{className:"w-[10%] text-right",children:u("inventory.stockOcrImport.action")})]})}),(0,a.jsxs)(G.BF,{children:[I.map(e=>(0,a.jsxs)(G.Hj,{children:[(0,a.jsxs)(G.nA,{children:[(0,a.jsx)(x.p,{value:e.name,onChange:t=>ew(e._localKey,"name",t.target.value),placeholder:u("inventory.stockOcrImport.nameRequired"),className:"h-8"}),e.source_file&&(0,a.jsx)("p",{className:"text-xs text-muted-foreground mt-1 truncate",children:e.source_file})]}),(0,a.jsx)(G.nA,{children:(0,a.jsx)(x.p,{value:e.sku||"",onChange:t=>ew(e._localKey,"sku",t.target.value),placeholder:"â",className:"h-8 font-mono text-xs"})}),(0,a.jsx)(G.nA,{className:"text-center",children:(0,a.jsx)(x.p,{type:"number",min:"0",step:"0.01",value:e.quantity,onChange:t=>ew(e._localKey,"quantity",t.target.value),className:"h-8 text-right tabular-nums"})}),(0,a.jsx)(G.nA,{children:(0,a.jsx)(x.p,{value:e.unit||"",onChange:t=>ew(e._localKey,"unit",t.target.value),placeholder:u("inventory.stockOcrImport.unitPlaceholder"),className:"h-8"})}),(0,a.jsx)(G.nA,{className:"text-right",children:(0,a.jsx)(m.$,{type:"button",variant:"ghost",size:"sm",onClick:()=>eq(e._localKey),className:"h-7 w-7 p-0 text-red-600 hover:bg-red-50",children:(0,a.jsx)(et.A,{className:"h-4 w-4"})})})]},e._localKey)),0===I.length&&(0,a.jsx)(G.Hj,{children:(0,a.jsx)(G.nA,{colSpan:5,className:"text-center py-8 text-sm text-muted-foreground",children:u("inventory.stockOcrImport.emptyItems")})})]})]})})}),(0,a.jsxs)("div",{className:"flex items-center justify-between",children:[(0,a.jsxs)(m.$,{variant:"outline",size:"sm",onClick:()=>{O(e=>[...e,{_localKey:eh(),id:eh(),name:"",sku:null,barcode:null,quantity:1,unit:null,notes:null}])},className:"border-indigo-300 text-indigo-700 hover:bg-indigo-50",children:[(0,a.jsx)(N.A,{className:"h-4 w-4 mr-1"}),u("inventory.stockOcrImport.addRow")]}),I.length>0&&(0,a.jsxs)("p",{className:"text-xs text-muted-foreground",children:[u("inventory.stockOcrImport.totalQuantities")," ",(0,a.jsx)("span",{className:"font-semibold text-foreground",children:I.reduce((e,t)=>e+(Number(t.quantity)||0),0)})]})]}),H&&(0,a.jsxs)(z.Fc,{variant:"destructive",children:[(0,a.jsx)(k.A,{className:"h-4 w-4"}),(0,a.jsx)(z.TN,{children:H})]}),(0,a.jsxs)("div",{className:"flex justify-end gap-2",children:[(0,a.jsx)(m.$,{variant:"outline",onClick:()=>g("upload"),children:u("inventory.stockOcrImport.back")}),(0,a.jsxs)(m.$,{onClick:eL,disabled:0===I.length||eE.length>0,className:"bg-gradient-to-r from-emerald-600 to-teal-600 hover:from-emerald-700 hover:to-teal-700",children:[(0,a.jsx)(A.A,{className:"h-4 w-4 mr-2"}),u("inventory.stockOcrImport.validateAndApply")]})]})]}),"applying"===v&&eS(u("inventory.stockOcrImport.applying"),u("inventory.stockOcrImport.applyingHint")),"result"===v&&(()=>{let e=R.filter(e=>e.was_created),t=R.filter(e=>!e.was_created);return(0,a.jsxs)("div",{className:"space-y-4",children:[(0,a.jsxs)("div",{className:"grid grid-cols-3 gap-3",children:[(0,a.jsxs)("div",{className:"rounded-lg border bg-emerald-50 border-emerald-200 p-3 text-center",children:[(0,a.jsx)("p",{className:"text-2xl font-bold text-emerald-700",children:e.length}),(0,a.jsx)("p",{className:"text-xs text-emerald-700",children:u("inventory.stockOcrImport.productsCreated")})]}),(0,a.jsxs)("div",{className:"rounded-lg border bg-blue-50 border-blue-200 p-3 text-center",children:[(0,a.jsx)("p",{className:"text-2xl font-bold text-blue-700",children:t.length}),(0,a.jsx)("p",{className:"text-xs text-blue-700",children:u("inventory.stockOcrImport.stocksUpdated")})]}),(0,a.jsxs)("div",{className:"rounded-lg border bg-red-50 border-red-200 p-3 text-center",children:[(0,a.jsx)("p",{className:"text-2xl font-bold text-red-700",children:J.length}),(0,a.jsx)("p",{className:"text-xs text-red-700",children:u("inventory.stockOcrImport.errors")})]})]}),R.length>0&&(0,a.jsx)("div",{className:"border rounded-lg overflow-hidden",children:(0,a.jsx)("div",{className:"max-h-[300px] overflow-y-auto",children:(0,a.jsxs)(G.XI,{children:[(0,a.jsx)(G.A0,{className:"sticky top-0 bg-slate-50",children:(0,a.jsxs)(G.Hj,{children:[(0,a.jsx)(G.nd,{children:u("inventory.stockOcrImport.product")}),(0,a.jsx)(G.nd,{className:"text-center",children:u("inventory.stockOcrImport.stockBefore")}),(0,a.jsx)(G.nd,{className:"text-center",children:u("inventory.stockOcrImport.added")}),(0,a.jsx)(G.nd,{className:"text-center",children:u("inventory.stockOcrImport.newStock")}),(0,a.jsx)(G.nd,{className:"text-center",children:u("inventory.stockOcrImport.status")})]})}),(0,a.jsx)(G.BF,{children:R.map((e,t)=>(0,a.jsxs)(G.Hj,{children:[(0,a.jsxs)(G.nA,{children:[(0,a.jsx)("div",{className:"font-medium text-sm",children:e.name}),e.sku&&(0,a.jsx)("code",{className:"text-xs text-muted-foreground",children:e.sku})]}),(0,a.jsx)(G.nA,{className:"text-center tabular-nums",children:e.previous_qty}),(0,a.jsxs)(G.nA,{className:"text-center tabular-nums text-emerald-700",children:["+",e.added_qty]}),(0,a.jsx)(G.nA,{className:"text-center font-bold tabular-nums",children:e.new_qty}),(0,a.jsx)(G.nA,{className:"text-center",children:e.was_created?(0,a.jsx)(P.
1E,{className:"bg-emerald-100 text-emerald-800 border-emerald-200",children:u("inventory.stockOcrImport.created")}):(0,a.jsx)(P.E,{variant:"secondary",children:u("inventory.stockOcrImport.updated")})})]},"".concat(e.product_id||t,"-").concat(t)))})]})})}),J.length>0&&(0,a.jsxs)(z.Fc,{variant:"destructive",children:[(0,a.jsx)(k.A,{className:"h-4 w-4"}),(0,a.jsxs)(z.TN,{children:[(0,a.jsx)("p",{className:"font-semibold mb-1",children:u("inventory.stockOcrImport.errorsCount",{count:J.length})}),(0,a.jsx)("ul",{className:"text-xs space-y-0.5 max-h-32 overflow-y-auto",children:J.slice(0,20).map((e,t)=>(0,a.jsxs)("li",{children:["⢠",e]},t))})]})]}),(0,a.jsxs)("div",{className:"flex justify-end gap-2",children:[(0,a.jsx)(m.$,{variant:"outline",onClick:ef,children:u("inventory.stockOcrImport.newExtraction")}),(0,a.jsx)(m.$,{onClick:()=>{null==d||d(),o(!1)},className:"bg-gradient-to-r from-emerald-600 to-teal-600 hover:from-emerald-700 hover:to-teal-700",children:u("inventory.stockOcrImport.finish")})]})]})})()]})]})})}var eg=s(342914),ef=s(287081),ey=s(725782),ej=s(357485),e_=s(720853),ek=s(500196),eb=s(284267),eN=s(873239),ew=s(675340),eq=s(384858),eE=s(26764),eL=s(871406),eA=s(862937),eS=s(797476),eC=s(800590),eI=s(987454),eO=s(520861),eT=s(521567);function eF(e){var t,s,c,d,u;let{params:v,showHeaderRefreshButton:g=!0}=e,{t:f}=(0,n.Bd)(),{entrepriseId:y}=(0,r.use)(v),{session:b,user:E}=(0,eI.A)(),{createStockLevel:A,updateStockLevel:S}=(0,i.z)({autoLoad:!1}),[C,I]=(0,r.useState)({}),[O,F]=(0,r.useState)(1),[z,V]=(0,r.useState)(20),[D,R]=(0,r.useState)([]),[$,J]=(0,r.useState)(0),[M,H]=(0,r.useState)({}),[Q,W]=(0,r.useState)(!0),[Y,et]=(0,r.useState)(!1),[es,er]=(0,r.useState)(null),[en,ei]=(0,r.useState)(void 0),[eo,el]=(0,r.useState)([]),ec=(0,r.useCallback)(async e=>{if(y&&(null==E?void 0:E.id))try{await (0,eO.UI)(y,{recipient_user_ids:[E.id],...e})}catch(e){console.error("Failed to create notification:",e)}},[y,null==E?void 0:E.id]),{warehouses:ed}=(0,o.U)({pageSize:100}),{products:eu,loading:em,error:ex,refreshProducts:eh}=(0,l.Q)({excludeServices:!0,limit:1e4}),[ep,eF]=(0,r.useState)(!1),[ez,eP]=(0,r.useState)("create"),[eV,eD]=(0,r.useState)(null),[eR,eB]=(0,r.useState)(!1),[e$,eJ]=(0,r.useState)(!1),[eU,eM]=(0,r.useState)(!1),[eH,eQ]=(0,r.useState)(!1),[eW,eK]=(0,r.useState)([]),[eX,eG]=(0,r.useState)([]),[eZ,eY]=(0,r.useState)(!1),e0=(0,r.useMemo)(()=>!!(C.search||C.storage_id||C.product_id||C.quality_status||void 0!==C.is_quarantine||void 0!==C.low_stock||C.category_id||C.subcategory_id),[C.search,C.storage_id,C.product_id,C.quality_status,C.is_quarantine,C.low_stock,C.category_id,C.subcategory_id]);(0,r.useEffect)(()=>{(async()=>{if(y&&(null==b?void 0:b.access_token))try{eY(!0);let e=await (0,eA.mu)(y);eK(e||[])}catch(e){console.error("Error loading categories:",e),eK([])}finally{eY(!1)}})()},[y,null==b?void 0:b.access_token]),(0,r.useEffect)(()=>{(async()=>{if(!C.category_id||!y||!(null==b?void 0:b.access_token)){eG([]);return}try{let e=await (0,eA._$)(C.category_id,y);eG(e||[]),C.subcategory_id&&!e.find(e=>e.subcategory_id===C.subcategory_id)&&I({...C,subcategory_id:void 0})}catch(e){console.error("Error loading subcategories:",e),eG([])}})()},[C.category_id,C.subcategory_id,y,null==b?void 0:b.access_token,I]),(0,r.useEffect)(()=>{if(!y){J(0);return}let e=!0;return(0,eA.Wz)(y).then(t=>{e&&J(t.total)}).catch(()=>{e&&J(0)}),()=>{e=!1}},[y]);let e1=(0,r.useCallback)(e=>{for(let t of Object.values(M)){let s=t.find(t=>t.stock_id===e);if(s)return s}},[M]),e5=(0,r.useCallback)(async()=>{if(!y){R([]),J(0),H({}),W(!1),et(!1);return}W(!0),et(!0),er(null);try{var e;let t=[];if(C.product_id){let e=await (0,eA.v$)(y,C.product_id);t=e?[e]:[],J(t.length?1:0)}else t=(await (0,eA.EP)(y,{page:O,pageSize:z,search:C.search||void 0,category_id:C.category_id,subcategory_id:C.subcategory_id,is_service:!1})).products;if(R(t),!(null==b?void 0:b.access_token)||0===t.length){H({});return}let s=await fetch("".concat(ea.A.FINANCE_API_URL,"/v1/inventory/stock-levels/batch-lookup?entreprise_id=").concat(encodeURIComponent(y)),{method:"POST",headers:{Authorization:"Bearer ".concat(b.access_token),"Content-Type":"application/json"},body:JSON.stringify({product_ids:t.map(e=>e.product_id),storage_id:C.storage_id||void 0,quality_status:C.quality_status||void 0,is_quarantine:C.is_quarantine,low_stock:C.low_stock,search:C.search||void 0})});if(!s.ok){let e=await s.text();throw Error(e||"Erreur chargement des stocks (lot)")}let a=await s.json(),r=null!==(e=a.data)&&void 0!==e?e:[],n={};for(let e of t)n[e.product_id]=[];for(let e of r){let t=e.product_id;n[t]||(n[t]=[]),n[t].push(e)}H(n)}catch(e){er(e instanceof Error?e.message:"Erreur chargement des stocks"),R([]),H({})}finally{W(!1),et(!1)}},[y,null==b?void 0:b.access_token,O,z,C.search,C.storage_id,C.product_id,C.quality_status,C.is_quarantine,C.low_stock,C.category_id,C.subcategory_id]);(0,r.useEffect)(()=>{e5()},[e5]);let e2=(0,r.useCallback)(()=>{y&&(0,eA.Wz)(y).then(e=>J(e.total)).catch(()=>void 0),e5()},[y,e5]);(0,r.useEffect)(()=>{let e=!1;return(async()=>
1{if(!en||!(null==b?void 0:b.access_token)||!y){el([]);return}try{var t,s;let a=new URLSearchParams({entreprise_id:y,product_id:en,page:"1",page_size:"100"}),r=await fetch("".concat(ea.A.FINANCE_API_URL,"/v1/inventory/stock-levels?").concat(a.toString()),{headers:{Authorization:"Bearer ".concat(b.access_token),"Content-Type":"application/json"}});if(!r.ok){e||el([]);return}let n=await r.json(),i=null!==(s=null!==(t=n.data)&&void 0!==t?t:n.items)&&void 0!==s?s:[];e||el(i)}catch(t){console.error("Erreur chargement lignes existantes pour produit:",t),e||el([])}})(),()=>{e=!0}},[en,y,null==b?void 0:b.access_token]);let e4=(0,r.useMemo)(()=>D.map(e=>({...e,stockLines:M[e.product_id]||[]})),[D,M]),e3=(0,r.useCallback)(e=>(!C.quality_status||e.quality_status===C.quality_status)&&(!0!==C.is_quarantine||!!e.is_quarantine)&&(!0!==C.low_stock||!(e.available_qty>0)),[C.quality_status,C.is_quarantine,C.low_stock]),e6=(0,r.useMemo)(()=>!!C.quality_status||!0===C.is_quarantine||!0===C.low_stock,[C.quality_status,C.is_quarantine,C.low_stock]),e7=(0,r.useMemo)(()=>{let e=e4;return e6&&(e=e.filter(e=>e.stockLines.some(e3))),e},[e4,e6,e3]),e8=(0,r.useCallback)(e=>C.storage_id?e.stockLines.filter(e=>e.storage_id===C.storage_id):e.stockLines,[C.storage_id]),e9=Math.max(1,C.product_id?1:Math.ceil(($||0)/z)||1),te=C.product_id?D.length>0?1:0:$,tt=em||Q||Y,ts=ex||es,ta=async e=>{try{eB(!0),await A(e),await e2(),eF(!1),ei(void 0);let t=eu.find(t=>t.product_id===(null==e?void 0:e.product_id)),s=ed.find(t=>t.storage_id===(null==e?void 0:e.storage_id));await ec({type:"inventory_movement",title:f("inventory.stockLevels.notifications.createdTitle"),message:f("inventory.stockLevels.notifications.createdMessage",{productName:null==t?void 0:t.name,storageName:null==s?void 0:s.name}),related_entity_type:"stock_level",related_entity_id:(null==e?void 0:e.stock_id)||void 0,action_url:"/company/".concat(y,"/inventory/stock-levels"),metadata:{action:"created",product_id:null==e?void 0:e.product_id,storage_id:null==e?void 0:e.storage_id,quantity:null==e?void 0:e.quantity,reserved_qty:null==e?void 0:e.reserved_qty,is_quarantine:null==e?void 0:e.is_quarantine,quality_status:null==e?void 0:e.quality_status}}),U.toast.success(f("inventory.stockLevels.toasts.createSuccessTitle"),{description:f("inventory.stockLevels.toasts.createSuccessDescription"),icon:"â
"})}catch(s){let e=s instanceof Error?s.message:f("inventory.stockLevels.errors.generic"),t=/duplicate|already exists|existe déjà |unique|stock level/i.test(e)?f("inventory.stockLevels.errors.alreadyExists"):e;U.toast.error(f("inventory.stockLevels.toasts.createErrorTitle"),{description:t,icon:"â"})}finally{eB(!1)}},tr=async(e,t)=>{try{var s;eB(!0),await S(e,t),await e2(),eF(!1),ei(void 0);let a=null!==(s=e1(e))&&void 0!==s?s:eo.find(t=>t.stock_id===e);await ec({type:"inventory_movement",title:f("inventory.stockLevels.toasts.replaceSuccessTitle"),message:f("inventory.stockLevels.toasts.replaceSuccessDescription"),related_entity_type:"stock_level",related_entity_id:e,action_url:"/company/".concat(y,"/inventory/stock-levels"),metadata:{action:"replaced",stock_id:e,product_id:null==a?void 0:a.product_id,storage_id:null==a?void 0:a.storage_id,previous_quantity:null==a?void 0:a.quantity,new_quantity:t.quantity}}),U.toast.success(f("inventory.stockLevels.toasts.replaceSuccessTitle"),{description:f("inventory.stockLevels.toasts.replaceSuccessDescription"),icon:"\uD83D\uDD04"})}catch(e){U.toast.error(f("inventory.stockLevels.toasts.updateErrorTitle"),{description:e instanceof Error?e.message:f("inventory.stockLevels.errors.generic"),icon:"â"})}finally{eB(!1)}},tn=async e=>{if(eV)try{eB(!0),await S(eV.stock_id,e),await e2(),eF(!1),eD(null),await ec({type:"inventory_movement",title:f("inventory.stockLevels.notifications.updatedTitle"),message:f("inventory.stockLevels.notifications.updatedMessage",{productName:eV.product_name,storageName:eV.storage_name}),related_entity_type:"stock_level",related_entity_id:eV.stock_id,action_url:"/company/".concat(y,"/inventory/stock-levels"),metadata:{action:"updated",stock_id:eV.stock_id,product_id:eV.product_id,storage_id:eV.storage_id,patch:e}}),U.toast.success(f("inventory.stockLevels.toasts.updateSuccessTitle"),{description:f("inventory.stockLevels.toasts.updateSuccessDescription"),icon:"â
"})}catch(e){U.toast.error(f("inventory.stockLevels.toasts.updateErrorTitle"),{description:e instanceof Error?e.message:f("inventory.stockLevels.errors.generic"),icon:"â"})}finally{eB(!1)}},ti=(0,r.useCallback)(()=>{I({search:void 0,storage_id:void 0,product_id:void 0,quality_status:void 0,is_quarantine:void 0,low_stock:void 0,category_id:void 0,subcategory_id:void 0})},[]),to=e=>{ei(void 0),eD(e),eP("edit"),eF(!0)},tl=e=>{eD(null),ei(e),eP("create"),eF(!0)},tc=(0,r.useCallback)(e=>{F(e)},[]),td=(0,r.useCallback)(e=>{V(e),F(1)},[]);(0,r.useEffect)(()=>{F(1)},[C]);let tu="create"===ez&&!!en;return ts?(0,a.jsx)("div",{className:"min-h-screen bg-gradient-to-br from-slate-50 via-blue-50 to-indigo-50",children:(0,a.jsx)("div",{className:"container mx-auto p-4",children:(0,a.jsx)("div",{className:"w-full max-w-2xl mx-auto",children:(0,a.jsx)(j.Zp,{className:"border-red-200 bg-white/80 backdrop-blur-sm shadow-lg border-0",children:(0,a.jsx)(j.Wu,{className:"py-16 px-8",children:(0,a.jsxs)("div",{className:"text-center",children:[(0,a.jsx)("div",{className:"inline-block",children:(0,a.jsxs)("div",{className:"relative",children:[(0,a.jsx)("div",{className:"absolute inset-0 bg-red-400/20 blur-xl rounded-full"}),(0,a.jsx)(q.A,{className:"relative h-20 w-20 text-red-500 mx-auto mb-6"})]})}),(0,a.jsx)("h3",{className:"text-2xl font-bold mb-3 text-red-900",children:f("inventory.stockLevels.error.title")}),(0,a.jsx)("p",{className:"text-red-700 mb-8 text-lg max-w-md mx-auto leading-relaxed",children:ts}),(0,a.jsxs)(m.$,{onClick:()=>{e2(),eh()},size:"lg",className:"bg-gradient-to-r from-red-600 to-red-700 hover:from-red-700 hover:to-red-800 shadow-lg hover:shadow-xl transition-all",children:[(0,a.jsx)(w.A,{className:"h-5 w-5 mr-2"}),f("inventory.stockLevels.error.retry")]})]})})})})})}):(0,a.jsx)("div",{className:"min-h-screen bg-gradient-to-br from-slate-50 via-blue-50 to-indigo-50",children:(0,a.jsxs)("div",{className:"container mx-auto px-6 py-4 space-y-4",children:[(0,a.jsx)(eg.T,{title:f("inventory.stockLevels.pageTitle"),icon:L.A,colors:"teal-600",actions:(0,a.jsxs)(a.Fragment,{children:[g?(0,a.jsx)(m.$,{onClick:()=>{e2(),eh()},disabled:tt,variant:"outline",size:"sm",children:(0,a.jsx)(w.A,{className:"w-4 h-4 ".concat(tt?"animate-spin":"")})}):null,(0,a.jsxs)(m.$,{onClick:()=>eJ(!0),variant:"outline",size:"sm",className:"bg-gradient-to-br from-indigo-50 to-blue-50 border-indigo-200 text-indigo-600",children:[(0,a.jsx)(B.A,{className:"w-4 h-4 mr-1"}),(0,a.jsx)("span",{className:"hidden lg:inline",children:f("inventory.stockLevels.actions.import",{defaultValue:"Importer"})})]}),(0,a.jsxs)(m.$,{onClick:()=>eM(!0),variant:"outline",size:"sm",className:"bg-gradient-to-br from-purple-50 to-indigo-50 border-purple-200 text-purple-600",children:[(0,a.jsx)(Z.A,{className:"w-4 h-4 mr-1"}),(0,a.jsx)("span",{className:"hidden lg:inline",children:f("inventory.stockLevels.actions.importIa",{defaultValue:"Import IA"})})]}),(0,a.jsxs)(m.$,{onClick:()=>{eD(null),ei(void 0),eP("create"),eF(!0)},variant:"outline",size:"sm",className:"bg-gradient-to-br from-teal-50 to-cyan-50 border-teal-200 text-teal-600",children:[(0,a.jsx)(N.A,{className:"w-4 h-4 mr-1"}),(0,a.jsx)("span",{className:"hidden lg:inline",children:f("inventory.stockLevels.actions.add")})]})]})}),(0,a.jsx)(j.Zp,{className:"bg-white/80 backdrop-blur-sm shadow-lg border-0",children:(0,a.jsx)(j.Wu,{className:"p-4",children:(0,a.jsxs)("div",{className:"flex flex-col gap-4",children:[(0,a.jsxs)("div",{className:"flex flex-col md:flex-row gap-4",children:[(0,a.jsxs)("div",{className:"flex-1 relative",children:[(0,a.jsx)(e_.A,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),(0,a.jsx)(x.p,{placeholder:f("inventory.stockLevels.filters.searchPlaceholder"),value:C.search||"",onChange:e=>I({...C,search:e.target.value}),className:"pl-10"})]}),(0,a.jsxs)("div",{className:"flex gap-2 flex-wrap",children:[(0,a.jsxs)(m.$,{variant:!0===C.is_quarantine?"default":"outline",onClick:()=>I({...C,is_quarantine:!0!==C.is_quarantine||void 0}),size:"sm",className:!0===C.is_quarantine?"bg-red-600":"",children:[(0,a.jsx)(q.A,{className:"h-4 w-4 mr-1"}),f("inventory.stockLevels.filters.quarantine")]}),(0,a.jsx)(m.$,{variant:!0===C.low_stock?"default":"outline",onClick:()=>I({...C,low_stock:!0!==C.low_stock||void 0}),size:"sm",className:!0===C.low_stock?"bg-orange-600":"",children:f("inventory.stockLevels.filters.lowStock")}),(0,a.jsxs)(m.$,{variant:"outline",size:"sm",onClick:()=>eQ(!eH),className:"border-blue-300 text-blue-600 hover:bg-blue-50",children:[(0,a.jsx)(ek.A,{className:"h-4 w-4 mr-2"}),f("inventory.stockLevels.filters.advanced")]})]})]}),(C.storage_id||C.product_id||C.quality_status||C.category_id||C.subcategory_id)&&(0,a.jsxs)("div",{className:"flex flex-wrap gap-2 items-center pt-4 border-t",children:[(0,a.jsx)("span",{className:"text-sm text-muted-foreground",children:f("inventory.stockLevels.filters.active")}),C.storage_id&&(0,a.jsxs)(P.E,{variant:"secondary",className:"gap-1",children:[f("inventory.stockLevels.filters.locationLabel")," ",(null===(t=ed.find(e=>e.storage_id===C.storage_id))||void 0===t?void 0:t.name)||f("inventory.stockLevels.filters.unknown"),(0,a.jsx)(ee.A,{className:"h-3 w-3 cursor-pointer",onClick:()=>I({...C,storage_id:void 0})})]}),C.product_id&&(0,a.jsxs)(P.E,{variant:"secondary",className:"gap-1",children:[f("inventory.stockLevels.filters.productLabel")," ",(null===(s=eu.find(e=>e.product_id===C.product_id))||void 0===s?void 0:s.name)||f("inventory.stockLevels.filters.unknown"),(0,a.jsx)(ee.A,{className:"h-3 w-3 cursor-pointer",onClick:()=>I({...C,product_id:void 0})})]}),C.quality_status&&(0,a.jsxs)(P.E,{variant:"secondary",className:"gap-1",children:[f("inventory.stockLevels.filters.qualityLabel")," ","ok"===C.quality_status?f("inventory.stockLevels.qualityStatus.ok"):"qc_hold"===C.quality_status?f("inventory.stockLevels.qualityStatus.qcHold"):f("inventory.stockLevels.qualityStatus.reject"),(0,a.jsx)(ee.A,{className:"h-3 w-3 cursor-pointer",onClick:()=>I({...C,quality_status:void 0})})]}),C.category_id&&(0,a.jsxs)(P.E,{variant:"secondary",className:"gap-1",children:[f("inventory.stockLevels.filters.categoryLabel")," ",(null===(c=eW.find(e=>e.category_id===C.category_id))||void 0===c?void 0:c.name)||f("inventory.stockLevels.filters.unknown"),(0,a.jsx)(ee.A,{className:"h-3 w-3 cursor-pointer",onClick:()=>I({...C,category_id:void 0,subcategory_id:void 0})})]}),C.subcategory_id&&(0,a.jsxs)(P.E,{variant:"secondary",className:"gap-1",children:[f("inventory.stockLevels.filters.subcategoryLabel")," ",(null===(d=eX.find(e=>e.subcategory_id===C.subcategory_id))||void 0===d?void 0:d.name)||f("inventory.stockLevels.filters.unknown"),(0,a.jsx)(ee.A,{className:"h-3 w-3 cursor-pointer",onClick:()=>I({...C,subcategory_id:void 0})})]}),(0,a.jsxs)(m.$,{variant:"ghost",size:"sm",onClick:ti,className:"h-6 text-xs",children:[(0,a.jsx)(ee.A,{className:"h-3 w-3 mr-1"}),f("inventory.stockLevels.filters.clearAll")]})]}),eH&&(0,a.jsxs)("div",{className:"pt-4 mt-4 border-t space-y-4",children:[(0,a.jsxs)("div",{className:"flex items-center gap-2 mb-3",children:[(0,a.jsx)(eb.A,{className:"h-4 w-4 text-blue-600"}),(0,a.jsx)("span",{className:"text-sm font-semibold text-foreground",children:f("inventory.stockLevels.filters.advancedTitle")})]}),(0,a.jsxs)("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4",children:[(0,a.jsxs)("div",{className:"space-y-2",children:[(0,a.jsx)(h.J,{className:"text-sm font-semibold",children:f("inventory.stockLevels.filters.location")}),(0,a.jsxs)(p.l6,{value:C.storage_id||"__all__",onValueChange:e=>I({...C,storage_id:"__all__"===e?void 0:e}),children:[(0,a.jsx)(p.bq,{children:(0,a.jsx)(p.yv,{placeholder:f("inventory.stockLevels.filters.allLocations")})}),(0,a.jsxs)(p.gC,{children:[(0,a.jsx)(p.eb,{value:"__all__",children:f("inventory.stockLevels.filters.allLocations")}),ed.map(e=>(0,a.jsxs)(p.eb,{value:e.storage_id,children:[e.name," (",e.code,")",e.level?" - ".concat(e.level):""]},e.storage_id))]})]})]}),(0,a.jsxs)("div",{className:"space-y-2",children:[(0,a.jsx)(h.J,{className:"text-sm font-semibold",children:f("inventory.stockLevels.filters.product")}),(0,a.jsxs)(p.l6,{value:C.product_id||"__all__",onValueChange:e=>I({...C,product_id:"__all__"===e?void 0:e}),children:[(0,a.jsx)(p.bq,{children:(0,a.jsx)(p.yv,{placeholder:f("inventory.stockLevels.filters.allProducts")})}),(0,a.jsxs)(p.gC,{children:[(0,a.jsx)(p.eb,{value:"__all__",children:f("inventory.stockLevels.filters.allProducts")}),eu.map(e=>(0,a.jsxs)(p.eb,{value:e.product_id,children:[e.name," (",e.sku,")"]},e.product_id))]})]})]}),(0,a.jsxs)("div",{className:"space-y-2",children:[(0,a.jsx)(h.J,{className:"text-sm font-semibold",children:f("inventory.stockLevels.filters.qualityStatus")}
1),(0,a.jsxs)(p.l6,{value:C.quality_status||"__all__",onValueChange:e=>I({...C,quality_status:"__all__"===e?void 0:e}),children:[(0,a.jsx)(p.bq,{children:(0,a.jsx)(p.yv,{placeholder:f("inventory.stockLevels.filters.allStatuses")})}),(0,a.jsxs)(p.gC,{children:[(0,a.jsx)(p.eb,{value:"__all__",children:f("inventory.stockLevels.filters.allStatuses")}),(0,a.jsx)(p.eb,{value:"ok",children:f("inventory.stockLevels.qualityStatus.okOption")}),(0,a.jsx)(p.eb,{value:"qc_hold",children:f("inventory.stockLevels.qualityStatus.qcHoldOption")}),(0,a.jsx)(p.eb,{value:"reject",children:f("inventory.stockLevels.qualityStatus.rejectOption")})]})]})]}),(0,a.jsxs)("div",{className:"space-y-2",children:[(0,a.jsx)(h.J,{className:"text-sm font-semibold",children:f("inventory.stockLevels.filters.category")}),(0,a.jsx)(eS.R,{companyId:y,value:C.category_id||null,onChange:e=>{I({...C,category_id:e||void 0,subcategory_id:void 0})},placeholder:f("inventory.stockLevels.filters.allCategories"),allowClearLabel:f("inventory.stockLevels.filters.allCategories"),className:"h-10"})]}),(0,a.jsxs)("div",{className:"space-y-2",children:[(0,a.jsx)(h.J,{className:"text-sm font-semibold",children:f("inventory.stockLevels.filters.subcategory")}),(0,a.jsx)(eC.P,{companyId:y,categoryId:C.category_id||null,value:C.subcategory_id||null,onChange:e=>I({...C,subcategory_id:e||void 0}),disabled:!C.category_id,placeholder:C.category_id?f("inventory.stockLevels.filters.allSubcategories"):f("inventory.stockLevels.filters.selectCategoryFirst"),allowClearLabel:f("inventory.stockLevels.filters.allSubcategories"),className:"h-10"})]})]})]})]})})}),(0,a.jsx)(j.Zp,{className:"bg-white/80 backdrop-blur-sm shadow-lg border-0",children:(0,a.jsxs)(j.Wu,{className:"p-0",children:[tt?(0,a.jsx)("div",{className:"p-4 space-y-2",children:[...Array(10)].map((e,t)=>(0,a.jsxs)("div",{className:"flex items-center gap-4 p-4 border-b",children:[(0,a.jsx)(ef.E,{className:"h-10 w-10 rounded-lg"}),(0,a.jsx)(ef.E,{className:"h-4 flex-1"}),(0,a.jsx)(ef.E,{className:"h-4 w-32"}),(0,a.jsx)(ef.E,{className:"h-4 w-24"}),(0,a.jsx)(ef.E,{className:"h-4 w-24"}),(0,a.jsx)(ef.E,{className:"h-4 w-24"}),(0,a.jsx)(ef.E,{className:"h-4 w-24"}),(0,a.jsx)(ef.E,{className:"h-8 w-20"})]},t))}):0===eu.length?(0,a.jsx)("div",{className:"flex items-center justify-center py-24",children:(0,a.jsxs)("div",{className:"text-center max-w-lg",children:[(0,a.jsx)(L.A,{className:"relative h-24 w-24 text-teal-400/60 mx-auto mb-6"}),(0,a.jsx)("h3",{className:"text-2xl font-bold mb-3 text-foreground",children:f("inventory.stockLevels.catalog.emptyNoProductsTitle")}),(0,a.jsx)("p",{className:"text-muted-foreground mb-6",children:f("inventory.stockLevels.catalog.emptyNoProductsDescription")})]})}):0===e7.length?(0,a.jsx)("div",{className:"flex items-center justify-center py-24",children:(0,a.jsxs)("div",{className:"text-center max-w-lg",children:[(0,a.jsx)("div",{className:"inline-block",children:(0,a.jsxs)("div",{className:"relative",children:[(0,a.jsx)("div",{className:"absolute inset-0 bg-teal-400/30 blur-3xl rounded-full"}),(0,a.jsx)(L.A,{className:"relative h-32 w-32 text-teal-400/60 mx-auto mb-8"})]})}),e0?(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)("h3",{className:"text-3xl font-bold mb-4 text-foreground",children:f("inventory.stockLevels.empty.filteredTitle")}),(0,a.jsx)("p",{className:"text-muted-foreground mb-8 text-lg leading-relaxed",children:f("inventory.stockLevels.empty.filteredDescription")}),(0,a.jsxs)(m.$,{variant:"outline",onClick:ti,size:"lg",className:"shadow-md hover:shadow-lg transition-all border-2",children:[(0,a.jsx)(w.A,{className:"h-5 w-5 mr-2"}),f("inventory.stockLevels.empty.clearFilters")]})]}):(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)("h3",{className:"text-3xl font-bold mb-4 text-foreground",children:f("inventory.stockLevels.catalog.emptyFilteredTitle")}),(0,a.jsx)("p",{className:"text-muted-foreground mb-8 text-lg leading-relaxed",children:f("inventory.stockLevels.catalog.emptyFilteredDescription")})]})]})}):(0,a.jsx)("div",{className:"overflow-x-auto",children:(0,a.jsxs)(G.XI,{children:[(0,a.jsx)(G.A0,{children:(0,a.jsxs)(G.Hj,{className:"bg-gradient-to-r from-slate-50 to-slate-100/50 border-b border-slate-200",children:[(0,a.jsx)(G.nd,{className:"font-semibold text-slate-700",children:f("inventory.stockLevels.table.product")}),(0,a.jsx)(G.nd,{className:"font-semibold text-slate-700",children:f("inventory.stockLevels.table.code")}),(0,a.jsx)(G.nd,{className:"font-semibold text-slate-700 min-w-[200px]",children:f("inventory.stockLevels.catalog.stockByLocation")}),(0,a.jsx)(G.nd,{className:"font-semibold text-slate-700 text-center",children:f("inventory.stockLevels.catalog.columnStock")}),(0,a.jsx)(G.nd,{className:"font-semibold text-slate-700 text-center",children:f("inventory.stockLevels.table.reserved")}),(0,a.jsx)(G.nd,{className:"font-semibold text-slate-700 text-center",children:f("inventory.stockLevels.table.qualityStatus")}),(0,a.jsx)(G.nd,{className:"font-semibold text-slate-700 text-center w-[140px]",children:f("inventory.stockLevels.catalog.addStockColumn")}),(0,a.jsx)(G.nd,{className:"font-semibold text-slate-700 text-right w-14",children:f("inventory.stockLevels.table.actions")})]})}),(0,a.jsx)(G.BF,{children:e7.map(e=>{let t=e8(e),s={ok:"from-green-500 to-emerald-600",qc_hold:"from-amber-500 to-orange-600",reject:"from-red-500 to-red-600"},r={ok:(0,a.jsx)(eN.A,{className:"h-4 w-4"}),qc_hold:(0,a.jsx)(k.A,{className:"h-4 w-4"}),reject:(0,a.jsx)(ew.A,{className:"h-4 w-4"})},n={ok:f("inventory.stockLevels.qualityStatus.ok"),qc_hold:f("inventory.stockLevels.qualityStatus.qcHold"),reject:f("inventory.stockLevels.qualityStatus.reject")},i=t.some(e=>e.is_quarantine),o=[...new Set(t.map(e=>e.quality_status))],l=1===o.length?o[0]:null,c=t.reduce((e,t)=>
1e+t.quantity,0),d=t.reduce((e,t)=>e+t.reserved_qty,0);return(0,a.jsxs)(G.Hj,{className:"hover:bg-slate-50/50 ".concat(t.length?i?"bg-red-50/40":"":"bg-orange-50/25"),children:[(0,a.jsx)(G.nA,{className:"max-w-[240px]",children:(0,a.jsxs)("div",{className:"flex items-center gap-2",children:[(0,a.jsx)("div",{className:"p-2 rounded-lg bg-gradient-to-br ".concat(t.length?s[null!=l?l:"ok"]:"from-slate-400 to-slate-500"," text-white shadow-md flex-shrink-0"),children:(0,a.jsx)(L.A,{className:"h-4 w-4"})}),(0,a.jsxs)("div",{className:"flex flex-col gap-1 min-w-0 flex-1",children:[(0,a.jsx)("span",{className:"font-semibold text-sm break-words whitespace-normal",children:e.name}),i&&(0,a.jsxs)(P.E,{variant:"destructive",className:"text-xs w-fit",children:[(0,a.jsx)(q.A,{className:"h-3 w-3 mr-1"}),f("inventory.stockLevels.table.quarantine")]})]})]})}),(0,a.jsx)(G.nA,{children:(0,a.jsx)("code",{className:"text-xs bg-slate-100 text-slate-700 px-2 py-1 rounded font-mono",children:e.sku||f("inventory.stockLevels.table.na")})}),(0,a.jsx)(G.nA,{children:0===t.length?(0,a.jsx)("span",{className:"text-xs text-muted-foreground",children:"â"}):(0,a.jsxs)("div",{className:"text-sm space-y-1.5 max-w-[280px]",children:[t.slice(0,4).map(e=>(0,a.jsxs)("div",{className:"flex items-center gap-1.5 border-b border-border/40 pb-1 last:border-0 last:pb-0",children:[(0,a.jsx)(_.A,{className:"h-3 w-3 text-purple-600 shrink-0"}),(0,a.jsx)("span",{className:"truncate text-xs font-medium",children:e.storage_name||e.storage_code||f("inventory.stockLevels.table.na")})]},e.stock_id)),t.length>4&&(0,a.jsxs)("p",{className:"text-xs text-muted-foreground",children:["+",t.length-4," ",f("inventory.stockLevels.catalog.moreLocations")]})]})}),(0,a.jsx)(G.nA,{className:"text-center",children:0===t.length?(0,a.jsx)("span",{className:"text-xs text-muted-foreground",children:"â"}):(0,a.jsx)("span",{className:"text-lg font-bold text-green-600 tabular-nums",children:c})}),(0,a.jsx)(G.nA,{className:"text-center",children:0===t.length?(0,a.jsx)("span",{className:"text-xs text-muted-foreground",children:"â"}):(0,a.jsx)("span",{className:"text-lg font-bold text-amber-600 tabular-nums",children:d})}),(0,a.jsx)(G.nA,{className:"text-center",children:0===t.length?(0,a.jsx)("span",{className:"text-xs text-muted-foreground",children:"â"}):l?(0,a.jsxs)(P.E,{className:"bg-gradient-to-r ".concat(s[l]," text-white border-0 shadow-sm flex items-center gap-1 w-fit mx-auto"),children:[r[l],n[l]]}):(0,a.jsx)("span",{className:"text-xs text-muted-foreground",children:f("inventory.stockLevels.catalog.mixedQuality")})}),(0,a.jsx)(G.nA,{className:"text-center",children:(0,a.jsxs)(m.$,{type:"button",variant:"outline",size:"sm",className:"border-teal-200 text-teal-700 hover:bg-teal-50",onClick:()=>tl(e.product_id),children:[(0,a.jsx)(N.A,{className:"h-4 w-4 mr-1"}),f("inventory.stockLevels.actions.add")]})}),(0,a.jsx)(G.nA,{className:"text-right",children:e.stockLines.length>0?(0,a.jsxs)(ej.rI,{children:[(0,a.jsx)(ej.ty,{asChild:!0,children:(0,a.jsx)(m.$,{variant:"ghost",size:"sm",className:"h-8 w-8 p-0",children:(0,a.jsx)(eq.A,{className:"h-4 w-4"})})}),(0,a.jsxs)(ej.SQ,{align:"end",className:"w-56 shadow-xl",children:[(0,a.jsx)(ej.lp,{className:"font-semibold",children:f("inventory.stockLevels.actionsMenu.title")}),(0,a.jsx)(ej.mB,{}),e.stockLines.map(e=>(0,a.jsxs)(ej._2,{onClick:()=>to(e),className:"cursor-pointer py-2",children:[(0,a.jsx)(eE.A,{className:"h-4 w-4 mr-3 text-blue-600"}),(0,a.jsx)("span",{className:"truncate",children:f("inventory.stockLevels.catalog.editAt",{location:e.storage_name||e.storage_code||e.storage_id.slice(0,8)})})]},e.stock_id))]})]}):(0,a.jsx)("span",{className:"text-muted-foreground text-xs",children:"â"})})]},e.product_id)})})]})}),e7.length>0&&(0,a.jsx)("div",{className:"p-4 border-t",children:(0,a.jsx)(ey.d,{currentPage:O,totalPages:e9,pageSize:z,totalItems:te,onPageChange:tc,onPageSizeChange:td})})]})}),(0,a.jsx)(X.cj,{open:ep,onOpenChange:e=>{eF(e),e||(eD(null),ei(void 0))},children:(0,a.jsxs)(X.h,{className:(0,eT.cn)("w-full overflow-y-auto",tu?"sm:max-w-md":"sm:max-w-2xl"),children:[(0,a.jsxs)(X.Fm,{className:(0,eT.cn)("border-b",tu?"pb-4":"pb-6"),children:[(0,a.jsxs)(X.qp,{className:(0,eT.cn)("flex items-center gap-3",tu?"text-xl":"text-2xl"),children:[(0,a.jsx)("div",{className:"p-2 rounded-lg bg-gradient-to-br from-teal-500 to-cyan-600 text-white",children:"create"===ez?
1(0,a.jsx)(eL.A,{className:tu?"h-5 w-5":"h-6 w-6"}):(0,a.jsx)(eE.A,{className:"h-6 w-6"})}),tu?f("inventory.stockLevels.sheet.quickAddTitle"):"create"===ez?f("inventory.stockLevels.sheet.createTitle"):f("inventory.stockLevels.sheet.editTitle")]}),tu?null:(0,a.jsx)(X.Qs,{className:"text-base",children:"create"===ez?f("inventory.stockLevels.sheet.createDescription"):f("inventory.stockLevels.sheet.editDescription")})]}),(0,a.jsx)("div",{className:(0,eT.cn)(tu?"mt-4":"mt-6"),children:(0,a.jsx)(T,{stockFieldsOnly:tu,initialData:eV?{product_id:eV.product_id,storage_id:eV.storage_id,quantity:eV.quantity,reserved_qty:eV.reserved_qty,is_quarantine:eV.is_quarantine,quality_status:eV.quality_status}:en?{product_id:en}:void 0,existingStockLines:tu&&en?eo.map(e=>({stock_id:e.stock_id,storage_id:e.storage_id,quantity:e.quantity,reserved_qty:e.reserved_qty,is_quarantine:e.is_quarantine,quality_status:e.quality_status})):void 0,onReplace:tu?tr:void 0,onSubmit:"create"===ez?ta:tn,onCancel:()=>{eF(!1),eD(null),ei(void 0)},loading:eR,mode:ez,products:eu,storageUnits:ed.map(e=>({storage_id:e.storage_id,name:e.name,code:e.code,path:e.path}))},"".concat(ez,"-").concat(null!==(u=null==eV?void 0:eV.stock_id)&&void 0!==u?u:"","-").concat(null!=en?en:"","-").concat(ep))})]})}),(0,a.jsx)(K,{open:e$,onOpenChange:eJ,warehouses:ed.map(e=>({storage_id:e.storage_id,name:e.name,code:e.code,is_default:e.is_default})),onSuccess:()=>{e2(),eh()}}),(0,a.jsx)(ev,{open:eU,onOpenChange:eM,companyId:y,warehouses:ed.map(e=>({storage_id:e.storage_id,name:e.name,code:e.code,is_default:e.is_default})),onSuccess:()=>{e2(),eh()}})]})})}},803852:(e,t,s)=>{s.d(t,{q:()=>u});var a=s(695155),r=s(614505),n=s(814081),i=s(783509),o=s(923950),l=s(225121),c=s(659173),d=s(979214);function u(e){let{quota:t,loading:s,variant:u="compact"}=e,{t:m}=(0,d.Bd)();if(s)return(0,a.jsxs)("div",{className:"flex items-center gap-1.5 text-xs text-muted-foreground",children:[(0,a.jsx)(r.A,{className:"h-3 w-3 animate-spin"}),(0,a.jsx)("span",{children:m("common.ocrQuota.loading")})]});if(!t)return null;if(null===t.limit)return(0,a.jsxs)("div",{className:"flex items-center gap-1.5 text-xs text-muted-foreground",children:[(0,a.jsx)(n.A,{className:"h-3 w-3"}),(0,a.jsxs)("span",{children:["OCR : ",t.used," docs"]}),(0,a.jsx)(i.A,{className:"h-3 w-3"})]});if(0===t.limit)return(0,a.jsxs)(l.E,{variant:"destructive",className:"text-[10px] gap-1 px-1.5 py-0.5",children:[(0,a.jsx)(o.A,{className:"h-3 w-3"}),m("common.ocrQuota.unavailable")]});let x=Math.min(100,t.used/t.limit*100),h=x>=80,p=0===t.remaining;return"compact"===u?(0,a.jsxs)(l.E,{variant:p?"destructive":h?"outline":"secondary",className:"text-[10px] gap-1 px-1.5 py-0.5 ".concat(p?"":h?"border-amber-300 bg-amber-50 text-amber-700":"bg-blue-50 text-blue-700 border-blue-200"),children:[(0,a.jsx)(n.A,{className:"h-3 w-3"}),m("common.ocrQuota.badge",{used:t.used,limit:t.limit})]}):(0,a.jsxs)("div",{className:"space-y-1.5",children:[(0,a.jsxs)("div",{className:"flex items-center justify-between text-xs",children:[(0,a.jsxs)("div",{className:"flex items-center gap-1.5 font-medium",children:[(0,a.jsx)(n.A,{className:"h-3.5 w-3.5 text-blue-600"}),(0,a.jsx)("span",{children:m("common.ocrQuota.docsLabel")})]}),(0,a.jsxs)("span",{className:"font-semibold tabular-nums ".concat(p?"text-red-600":h?"text-amber-600":"text-gray-700"),children:[t.used," / ",t.limit]})]}),(0,a.jsx)(c.k,{value:x,className:"h-1.5 ".concat(p?"[&>div]:bg-red-500":h?"[&>div]:bg-amber-500":"[&>div]:bg-blue-500")}),(0,a.jsxs)("div",{className:"flex justify-between text-[10px] text-muted-foreground",children:[(0,a.jsx)("span",{children:m("common.ocrQuota.remaining",{remaining:t.remaining})}),(0,a.jsx)("span",{children:t.month})]})]})}},725782:(e,t,s)=>{s.d(t,{d:()=>u});var a=s(695155),r=s(73312),n=s(537528),i=s(317661),o=s(793518),l=s(746967),c=s(297054),d=s(979214);function u(e){let{currentPage:t,totalPages:s,pageSize:u,totalItems:m,onPageChange:x,onPageSizeChange:h,className:p="",disabled:v=!1}=e,{t:g}=(0,d.Bd)(),f=Math.min(t*u,m);return(0,a.jsxs)("div",{className:"flex items-center justify-between ".concat(p),children:[(0,a.jsx)("div",{className:"flex items-center space-x-2",children:(0,a.jsx)("p",{className:"text-sm text-muted-foreground",children:g("inventory.pagination.showing",{start:(t-1)*u+1,end:f,total:m})})}),(0,a.jsxs)("div",{className:"flex items-center space-x-2",children:[(0,a.jsxs)("div",{className:"flex items-center space-x-2",children:[(0,a.jsx)("p",{className:"text-sm text-muted-foreground",children:g("inventory.pagination.rowsPerPage")}),(0,a.jsxs)(n.l6,{value:"".concat(u),onValueChange:e=>h(Number(e)),disabled:v,children:[(0,a.jsx)(n.bq,{className:"h-8 w-[70px]",children:(0,a.jsx)(n.yv,{placeholder:u})}),(0,a.jsx)(n.gC,{side:"top",children:[10,20,30,40,50].map(e=>(0,a.jsx)(n.eb,{value:"".concat(e),children:e},e))})]})]}),(0,a.jsxs)("div",{className:"flex items-center space-x-2",children:[(0,a.jsx)(r.$,{variant:"outline",className:"h-8 w-8 p-0",onClick:()=>x(1),disabled:v||1===t,children:(0,a.jsx)(i.A,{className:"h-4 w-4"})}),(0,a.jsx)(r.$,{variant:"outline",className:"h-8 w-8 p-0",onClick:()=>x(t-1),disabled:v||1===t,children:(0,a.jsx)(o.A,{className:"h-4 w-4"})}),(0,a.jsx)("div",{className:"flex items-center gap-1",children:(0,a.jsx)("span",{className:"text-sm text-muted-foreground",children:g("inventory.pagination.pageOf",{current:t,total:s})})}),(0,a.jsx)(r.$,{variant:"outline",className:"h-8 w-8 p-0",onClick:()=>x(t+1),disabled:v||t===s,children:(0,a.jsx)(l.A,{className:"h-4 w-4"})}),(0,a.jsx)(r.$,{variant:"outline",className:"h-8 w-8 p-0",onClick:()=>x(s),disabled:v||t===s,children:(0,a.jsx)(c.A,{className:"h-4 w-4"})})]})]})]})}},287081:(e,t,s)=>{s.d(t,{E:()=>o});var a=s(695155),r=s(358803),n=s.n(r),i=s(521567);function o(e){let{className:t,...s}=e;return(0,a.jsxs)("div",{...s,className:"jsx-d373d32fac1e276a "+(s&&null!=s.className&&s.className||(0,i.cn)("relative overflow-hidden rounded-md","bg-gradient-to-r from-primary/10 via-primary/5 to-primary/10","background-animate",t)||""),children:[(0,a.jsx)("div",{className:"jsx-d373d32fac1e276a shimmer-wrapper",children:(0,a.jsx)("div",{className:"jsx-d373d32fac1e276a shimmer"})}),(0,a.jsx)(n(),{id:"d373d32fac1e276a",children:".background-animate.jsx-d373d32fac1e276a{-webkit-background-size:200%200%;-moz-background-size:200%200%;-o-background-size:200%200%;background-size:200%200%;-webkit-animation:gradientAnimation 2s ease infinite;-moz-animation:gradientAnimation 2s ease infinite;-o-animation:gradientAnimation 2s ease infinite;animation:gradientAnimation 2s ease infinite}@-webkit-keyframes gradientAnimation{0%{background-position:0%50%}50%{background-position:100%50%}
1100%{background-position:0%50%}}@-moz-keyframes gradientAnimation{0%{background-position:0%50%}50%{background-position:100%50%}100%{background-position:0%50%}}@-o-keyframes gradientAnimation{0%{background-position:0%50%}50%{background-position:100%50%}100%{background-position:0%50%}}@keyframes gradientAnimation{0%{background-position:0%50%}50%{background-position:100%50%}100%{background-position:0%50%}}.shimmer-wrapper.jsx-d373d32fac1e276a{position:absolute;top:0;left:0;width:100%;height:100%;overflow:hidden}.shimmer.jsx-d373d32fac1e276a{width:50%;height:100%;background:-webkit-linear-gradient(left,rgba(255,255,255,0)0%,rgba(255,255,255,.3)50%,rgba(255,255,255,0)100%);background:-moz-linear-gradient(left,rgba(255,255,255,0)0%,rgba(255,255,255,.3)50%,rgba(255,255,255,0)100%);background:-o-linear-gradient(left,rgba(255,255,255,0)0%,rgba(255,255,255,.3)50%,rgba(255,255,255,0)100%);background:linear-gradient(90deg,rgba(255,255,255,0)0%,rgba(255,255,255,.3)50%,rgba(255,255,255,0)100%);-webkit-animation:shimmerAnimation 1.5s infinite;-moz-animation:shimmerAnimation 1.5s infinite;-o-animation:shimmerAnimation 1.5s infinite;animation:shimmerAnimation 1.5s infinite;position:absolute;top:0;left:-150%}@-webkit-keyframes shimmerAnimation{to{left:150%}}@-moz-keyframes shimmerAnimation{to{left:150%}}@-o-keyframes shimmerAnimation{to{left:150%}}@keyframes shimmerAnimation{to{left:150%}}"})]})}s(212115)},593474:(e,t,s)=>{s.d(t,{i:()=>i});var a=s(212115),r=s(861086);let n="https://app.lynara.ai/api/finance";function i(e){let[t,s]=(0,a.useState)(null),[i,o]=(0,a.useState)(!1),[l,c]=(0,a.useState)(null),d=(0,a.useRef)(null),u=(0,a.useCallback)(async()=>{var e;let t=(0,r.createClient)(),{data:{session:s}}=await t.auth.getSession();return null!==(e=null==s?void 0:s.access_token)&&void 0!==e?e:null},[]),m=(0,a.useCallback)(async()=>{if(!e)return null;o(!0),c(null);try{let t=await u();if(!t)throw Error("Not authenticated");let a=await fetch("".concat(n,"/v1/ocr-quota/check/").concat(e),{headers:{Authorization:"Bearer ".concat(t)}});if(!a.ok)throw Error("HTTP ".concat(a.status));let r=await a.json();return s(r),r}catch(e){return c((null==e?void 0:e.message)||"Erreur quota OCR"),null}finally{o(!1)}},[e,u]),x=(0,a.useCallback)(async()=>{if(!e)return null;try{let a=await u();if(!a)throw Error("Not authenticated");let r=await fetch("".concat(n,"/v1/ocr-quota/increment/").concat(e),{method:"POST",headers:{Authorization:"Bearer ".concat(a)}});if(!r.ok){var t;let e=await r.json().catch(()=>({}));throw Error((null==e?void 0:null===(t=e.detail)||void 0===t?void 0:t.message)||"HTTP ".concat(r.status))}let i=await r.json();return s(i),i}catch(e){return c((null==e?void 0:e.message)||"Erreur incr\xe9ment OCR"),null}},[e,u]);(0,a.useEffect)(()=>{e&&e!==d.current&&(d.current=e,m())},[e,m]);let h=(null==t?void 0:t.limit)===null,p=null!==t&&0===t.limit;return{quota:t,loading:i,error:l,checkQuota:m,recordSuccess:x,isUnlimited:h,isBlocked:p}}},466498:(e,t,s)=>{s.d(t,{Q:()=>o});var a=s(212115),r=s(987454),n=s(76046);let i="https://app.lynara.ai/api/finance",o=e=>{let{session:t}=(0,r.A)(),s=(0,n.useParams)(),o=null==s?void 0:s.entrepriseId,[l,c]=(0,a.useState)([]),[d,u]=(0,a.useState)(!0),[m,x]=(0,a.useState)(null),h=(0,a.useCallback)(async()=>{try{var s;if(u(!0),x(null),!(null==t?void 0:t.access_token)||!o){c([]),u(!1);return}let a=null!==(s=null==e?void 0:e.limit)&&void 0!==s?s:1e4,r=new URLSearchParams({company_id:o,limit:String(a)});(null==e?void 0:e.search)&&r.append("search",e.search),(null==e?void 0:e.excludeServices)===!0&&r.append("is_service","false");let n=await fetch("".concat(i,"/v1/catalog/products?").concat(r.toString()),{method:"GET",headers:{Authorization:"Bearer ".concat(t.access_token),"Content-Type":"application/json"}});if(!n.ok){let e=await n.json().catch(()=>({detail:"Erreur r\xe9seau"}));throw Error(e.detail||"Erreur ".concat(n.status))}let l=await n.json();c(Array.isArray(l)?l:l.products||l||[])}catch(e){console.error("Error loading products:",e),x(e instanceof Error?e.message:"Failed to load products"),c([])}finally{u(!1)}},[null==t?void 0:t.access_token,o,null==e?void 0:e.search,null==e?void 0:e.excludeServices,null==e?void 0:e.limit]),p=(0,a.useCallback)(async e=>{if(!(null==t?void 0:t.access_token)||!o)throw Error("Session non disponible");let s=await fetch("".concat(i,"/v1/catalog/products?company_id=").concat(o),{method:"POST",headers:{Authorization:"Bearer ".concat(t.access_token),"Content-Type":"application/json"},body:JSON.stringify(e)});if(!s.ok)throw Error((await s.json().catch(()=>({detail:"Erreur r\xe9seau"}))).detail||"Erreur ".concat(s.status));let a=await s.json();return await h(),a},[null==t?void 0:t.access_token,o,h]),v=(0,a.useCallback)(()=>{h()}
1,[h]);return(0,a.useEffect)(()=>{h()},[h]),{products:l,loading:d,error:m,createProduct:p,refreshProducts:v}}},457527:(e,t,s)=>{s.d(t,{z:()=>o});var a=s(212115),r=s(987454),n=s(76046);let i="https://app.lynara.ai/api/finance",o=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},{session:t}=(0,r.A)(),s=(0,n.useParams)(),o=null==s?void 0:s.entrepriseId,[l,c]=(0,a.useState)([]),[d,u]=(0,a.useState)(!1!==e.autoLoad),[m,x]=(0,a.useState)(null),[h,p]=(0,a.useState)(e.initialFilters||{}),[v,g]=(0,a.useState)(0),[f,y]=(0,a.useState)(e.page||1),[j,_]=(0,a.useState)(e.pageSize||20),[k,b]=(0,a.useState)(null),N=(0,a.useCallback)(async()=>{if(!1!==e.autoLoad)try{if(u(!0),x(null),!(null==t?void 0:t.access_token)||!o){c([]),g(0),u(!1);return}let e=new URLSearchParams({entreprise_id:o,page:f.toString(),page_size:j.toString()});h.search&&e.append("search",h.search),h.storage_id&&e.append("storage_id",h.storage_id),h.product_id&&e.append("product_id",h.product_id),h.category_id&&e.append("category_id",h.category_id),h.subcategory_id&&e.append("subcategory_id",h.subcategory_id),h.quality_status&&e.append("quality_status",h.quality_status),void 0!==h.is_quarantine&&e.append("is_quarantine",h.is_quarantine.toString()),void 0!==h.low_stock&&e.append("low_stock",h.low_stock.toString());let s=await fetch("".concat(i,"/v1/inventory/stock-levels?").concat(e),{method:"GET",headers:{Authorization:"Bearer ".concat(t.access_token),"Content-Type":"application/json"}});if(!s.ok){let e="Erreur ".concat(s.status);try{let t=await s.json();"string"==typeof t?e=t:(null==t?void 0:t.detail)?e=t.detail:(null==t?void 0:t.message)?e=t.message:Array.isArray(null==t?void 0:t.errors)&&t.errors.length>0?e=t.errors.map(e=>e.msg||e.message||"Erreur de validation").join(", "):"object"==typeof t&&(e=JSON.stringify(t))}catch(t){e="Erreur r\xe9seau ".concat(s.status)}throw Error(e)}let a=await s.json();if(a.data&&a.pagination)c(a.data),g(a.pagination.total),b({page:a.pagination.page,page_size:a.pagination.limit,total:a.pagination.total,total_pages:a.pagination.total_pages});else if(a.items)c(a.items),g(a.total||a.items.length),b({page:a.page||f,page_size:a.page_size||j,total:a.total||a.items.length,total_pages:a.total_pages||Math.ceil((a.total||a.items.length)/(a.page_size||j))});else{let e=Array.isArray(a)?a:[];c(e),g(e.length),b(null)}}catch(e){console.error("Error loading stock levels:",e),x(e instanceof Error?e.message:"Failed to load stock levels"),c([]),g(0),b(null)}finally{u(!1)}},[null==t?void 0:t.access_token,o,h,f,j,e.autoLoad]),w=(0,a.useCallback)(async e=>{try{if(!(null==t?void 0:t.access_token)||!o)throw Error("Authentication required");let s=await fetch("".concat(i,"/v1/inventory/stock-levels?entreprise_id=").concat(o),{method:"POST",headers:{Authorization:"Bearer ".concat(t.access_token),"Content-Type":"application/json"},body:JSON.stringify(e)});if(!s.ok){let e="Error ".concat(s.status);try{let t=await s.json();"string"==typeof t?e=t:(null==t?void 0:t.detail)?e=t.detail:(null==t?void 0:t.message)?e=t.message:Array.isArray(null==t?void 0:t.errors)&&t.errors.length>0?e=t.errors.map(e=>e.msg||e.message||"Erreur de validation").join(", "):"object"==typeof t&&(e=JSON.stringify(t))}catch(t){e="Failed to create stock level"}throw Error(e)}let a=await s.json();return await N(),a}catch(e){throw console.error("Error creating stock level:",e),Error(e instanceof Error?e.message:"Failed to create stock level")}},[null==t?void 0:t.access_token,o,N]),q=(0,a.useCallback)(async(e,s)=>{try{if(!(null==t?void 0:t.access_token)||!o)throw Error("Authentication required");let a=await fetch("".concat(i,"/v1/inventory/stock-levels/").concat(e,"?entreprise_id=").concat(o),{method:"PUT",headers:{Authorization:"Bearer ".concat(t.access_token),"Content-Type":"application/json"},body:JSON.stringify(s)});if(!a.ok){let e="Error ".concat(a.status);try{let t=await a.json();"string"==typeof t?e=t:(null==t?void 0:t.detail)?e=t.detail:(null==t?void 0:t.message)?e=t.message:Array.isArray(null==t?void 0:t.errors)&&t.errors.length>0?e=t.errors.map(e=>e.msg||e.message||"Erreur de validation").join(", "):"object"==typeof t&&(e=JSON.stringify(t))}catch(t){e="Failed to update stock level"}throw Error(e)}let r=await a.json();return await N(),r}catch(e){throw console.error("Error updating stock level:",e),Error(e instanceof Error?e.message:"Failed to update stock level")}},[null==t?void 0:t.access_token,o,N]),E=(0,a.useCallback)(async e=>{try{if(!(null==t?void 0:t.access_token)||!o)throw Error("Authentication required");let s=await fetch("".concat(i,"/v1/inventory/stock-levels/adjust?entreprise_id=").concat(o),{method:"POST",headers:{Authorization:"Bearer ".concat(t.access_token),"Content-Type":"application/json"},body:JSON.stringify(e)});if(!s.ok){let e="Error ".concat(s.status);try{let t=await s.json();
1"string"==typeof t?e=t:(null==t?void 0:t.detail)?e=t.detail:(null==t?void 0:t.message)?e=t.message:Array.isArray(null==t?void 0:t.errors)&&t.errors.length>0?e=t.errors.map(e=>e.msg||e.message||"Erreur de validation").join(", "):"object"==typeof t&&(e=JSON.stringify(t))}catch(t){e="Failed to adjust stock"}throw Error(e)}await N()}catch(e){throw console.error("Error adjusting stock:",e),Error(e instanceof Error?e.message:"Failed to adjust stock")}},[null==t?void 0:t.access_token,o,N]),L=(0,a.useCallback)(async e=>{try{if(!(null==t?void 0:t.access_token)||!o)throw Error("Authentication required");let s=await fetch("".concat(i,"/v1/inventory/stock-levels/").concat(e,"?entreprise_id=").concat(o),{method:"DELETE",headers:{Authorization:"Bearer ".concat(t.access_token)}});if(!s.ok){let e="Error ".concat(s.status);try{let t=await s.json();"string"==typeof t?e=t:(null==t?void 0:t.detail)?e=t.detail:(null==t?void 0:t.message)?e=t.message:Array.isArray(null==t?void 0:t.errors)&&t.errors.length>0?e=t.errors.map(e=>e.msg||e.message||"Erreur de validation").join(", "):"object"==typeof t&&(e=JSON.stringify(t))}catch(t){e="Failed to delete stock level"}throw Error(e)}await N()}catch(e){throw console.error("Error deleting stock level:",e),Error(e instanceof Error?e.message:"Failed to delete stock level")}},[null==t?void 0:t.access_token,o,N]),A=(0,a.useCallback)(()=>{N()},[N]);return(0,a.useEffect)(()=>{!1!==e.autoLoad&&N()},[N,e.autoLoad]),{stockLevels:l,loading:d,error:m,filters:h,setFilters:p,createStockLevel:w,updateStockLevel:q,adjustStock:E,deleteStockLevel:L,refreshStockLevels:A,totalCount:v,page:f,setPage:y,pageSize:j,setPageSize:_,pagination:k}}},638162:(e,t,s)=>{s.d(t,{U:()=>o});var a=s(212115),r=s(76046),n=s(372288),i=s(121276);let o=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=(0,r.useParams)(),s=null==t?void 0:t.entrepriseId,[o,l]=(0,a.useState)([]),[c,d]=(0,a.useState)(!0),[u,m]=(0,a.useState)(null),[x,h]=(0,a.useState)(e.initialFilters||{}),[p,v]=(0,a.useState)(null),[g,f]=(0,a.useState)(0),[y,j]=(0,a.useState)(e.page||1),[_,k]=(0,a.useState)(e.pageSize||12),[b,N]=(0,a.useState)(null),w=async function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},s=await (0,n.getAuthToken)();if(!s)throw Error("Non authentifi\xe9");let a=await fetch("".concat(i.A.FINANCE_API_URL).concat(e),{...t,headers:{"Content-Type":"application/json",Authorization:"Bearer ".concat(s),...t.headers}});if(!a.ok)throw Error(function(e,t){var s;if(null==e||"object"!=typeof e)return"Erreur ".concat(t);let a=null!==(s=e.detail)&&void 0!==s?s:e.message;if("string"==typeof a)return a;if(Array.isArray(a))return a.map(e=>{if("string"==typeof e)return e;if(e&&"object"==typeof e&&"msg"in e)return String(e.msg);try{return JSON.stringify(e)}catch(t){return String(e)}}).filter(Boolean).join(", ");if(null!=a&&"object"==typeof a)try{return JSON.stringify(a)}catch(e){}return"Erreur ".concat(t)}(await a.json().catch(()=>({detail:"R\xe9ponse non JSON du serveur"})),a.status));return a.json()},q=e=>{if(!e)return[];if(Array.isArray(e))return e;if("object"==typeof e){if(Array.isArray(e.data))return e.data;if(Array.isArray(e.storage_units))return e.storage_units}return[]},E=(0,a.useCallback)(async e=>{if(!s)throw Error("Company ID requis");let t=await w("/v1/inventory/storage-units/".concat(e,"?entreprise_id=").concat(s)),a=Array.isArray(t)?t[0]:t;if(a&&"object"==typeof a&&a.storage_id)return a;throw Error("Emplacement introuvable")},[s]),L=(0,a.useCallback)(async e=>{if(!s||!e)return[];let t=new URLSearchParams({entreprise_id:s,parent_id:e,limit:String(100),offset:"0"});return q(await w("/v1/inventory/storage-units?".concat(t.toString())))},[s]),A=(0,a.useCallback)(async()=>{if(!s){d(!1);return}try{d(!0),m(null);let e=Math.min(Math.max(1,_),100),t=new URLSearchParams({entreprise_id:s,limit:e.toString(),offset:((y-1)*e).toString()}),a=x.level||"warehouse";t.append("level",a),x.search&&t.append("query",x.search),void 0!==x.is_active&&t.append("is_active",x.is_active.toString()),x.type&&t.append("type",x.type);let r=await w("/v1/inventory/storage-units?".concat(t.toString()));if(r.data&&r.pagination){let e=r.data||[];l(e),f(r.pagination.total),N({page:r.pagination.page,page_size:r.pagination.limit,total:r.pagination.total,total_pages:r.pagination.total_pages})}else if(r.storage_units){let e=r.storage_units||[];l(e),f(r.count||e.length);let t=(y-1)*_;N({page:Math.floor(t/_)+1,page_size:_,total:r.count||e.length,total_pages:Math.ceil((r.count||e.length)/_)})}else{let e=Array.isArray(r)?r:[];l(e),f(e.length),N(null)}}catch(e){m(e instanceof Error?e.message:"Erreur lors du chargement des entrep\xf4ts"),console.error("Error loading warehouses:",e)}finally{d(!1)}},[s,x,y,_]),S=(0,a.useCallback)(async()=>
1{if(s)try{let e=await w("/v1/inventory/stats?entreprise_id=".concat(s));v(e)}catch(e){console.error("Error loading stats:",e)}},[s]),C=(0,a.useCallback)(async e=>{if(!s)throw Error("Company ID requis");try{var t,a,r,n;d(!0);let i={...e,level:null!==(t=e.level)&&void 0!==t?t:"warehouse",type:e.type||"stockage",address_country:e.address_country,is_default:null!==(a=e.is_default)&&void 0!==a&&a,allow_negative_stock:null!==(r=e.allow_negative_stock)&&void 0!==r&&r,is_active:null===(n=e.is_active)||void 0===n||n,timezone:e.timezone||"Africa/Tunis"},o=await w("/v1/inventory/storage-units?entreprise_id=".concat(s),{method:"POST",body:JSON.stringify(i)});return await Promise.all([A(),S()]),o}catch(e){throw Error(e instanceof Error?e.message:"Erreur lors de la cr\xe9ation")}finally{d(!1)}},[s,A,S]),I=(0,a.useCallback)(async(e,t)=>{if(!s)throw Error("Company ID requis");try{d(!0),await w("/v1/inventory/storage-units/".concat(e,"?entreprise_id=").concat(s),{method:"PUT",body:JSON.stringify(t)}),await A()}catch(e){throw Error(e instanceof Error?e.message:"Erreur lors de la mise \xe0 jour")}finally{d(!1)}},[s,A]),O=(0,a.useCallback)(async e=>{if(!s)throw Error("Company ID requis");try{d(!0),await w("/inventory/storage-units/".concat(e,"?entreprise_id=").concat(s,"&soft=false"),{method:"DELETE"}),await Promise.all([A(),S()])}catch(e){throw Error(e instanceof Error?e.message:"Erreur lors de la suppression")}finally{d(!1)}},[s,A,S]),T=(0,a.useCallback)(async e=>{if(!s)throw Error("Company ID requis");try{d(!0);let t=o.find(t=>t.storage_id===e);if(!t)throw Error("Entrep\xf4t non trouv\xe9");await I(e,{is_active:!t.is_active})}catch(e){throw Error(e instanceof Error?e.message:"Erreur lors du changement de statut")}finally{d(!1)}},[s,o,I]),F=(0,a.useCallback)(async e=>{if(!s)throw Error("Company ID requis");try{d(!0),await w("/v1/inventory/storage-units/".concat(e,"/set-default?entreprise_id=").concat(s),{method:"POST"}),await A()}catch(e){throw Error(e instanceof Error?e.message:"Erreur lors de la d\xe9finition par d\xe9faut")}finally{d(!1)}},[s,A]),z=(0,a.useCallback)(async(e,t)=>{if(!s)throw Error("Company ID requis");try{d(!0),await w("/v1/inventory/storage-units/".concat(e,"/lock?entreprise_id=").concat(s,"&locked=").concat(t),{method:"POST"}),await A()}catch(e){throw Error(e instanceof Error?e.message:"Erreur lors du verrouillage")}finally{d(!1)}},[s,A]),P=(0,a.useCallback)(()=>{A(),S()},[A,S]);return(0,a.useEffect)(()=>{if(!s){l([]),d(!1);return}A(),S()},[s,x,y,_,A,S]),{warehouses:o,loading:c,error:u,filters:x,stats:p,setFilters:h,createWarehouse:C,updateWarehouse:I,deleteWarehouse:O,toggleWarehouseStatus:T,setDefaultWarehouse:F,lockWarehouse:z,refreshWarehouses:P,loadStats:S,getStorageUnit:E,listChildUnits:L,totalCount:g,page:y,setPage:j,pageSize:_,setPageSize:k,pagination:b}}},520861:(e,t,s)=>{s.d(t,{$M:()=>m,DH:()=>n,GQ:()=>i,MQ:()=>g,Tr:()=>u,UI:()=>v,X0:()=>h,aI:()=>x,fP:()=>p});var a=s(372288),r=s(121276);let n=function(e,t){if(null==e||""===e)return t;let s=String(e).trim().toLowerCase();return"true"===s||"1"===s||"yes"===s||"on"===s}((0,r.y)("NEXT_PUBLIC_NOTIFICATIONS_ENABLED","true"),!0);function i(e){if(!e||"string"!=typeof e)return!1;let t=e.trim();return/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(t)}let o="https://app.lynara.ai/api/finance",l=(r.A.ENV_MODE||r.A.NODE_ENV||"").toLowerCase(),c=("production"===l||"prod"===l)&&o.startsWith("http://")?o.replace(/^http:\/\//i,"https://"):o;async function d(){let e=await (0,a.getAuthToken)();return{"Content-Type":"application/json",...e&&{Authorization:"Bearer ".concat(e)}}}async function u(e,t){if(!n)return[];let s=new URLSearchParams;(null==t?void 0:t.is_read)!==void 0&&s.append("is_read",t.is_read.toString()),(null==t?void 0:t.type)&&s.append("type",t.type),(null==t?void 0:t.priority)&&s.append("priority",t.priority),(null==t?void 0:t.start_date)&&s.append("start_date",t.start_date),(null==t?void 0:t.end_date)&&s.append("end_date",t.end_date),(null==t?void 0:t.limit)&&s.append("limit",t.limit.toString()),(null==t?void 0:t.offset)&&s.append("offset",t.offset.toString());let a="".concat(c,"/v1/rh/notifications?company_id=").concat(e,"&").concat(s.toString()),r=await fetch(a,{method:"GET",headers:await d()});if(!r.ok)throw Error("Failed to fetch notifications: ".concat(r.statusText));let i=await r.json();return Array.isArray(i)?i.map(e=>({...e,notification_id:(null==e?void 0:e.notification_id)||(null==e?void 0:e.id)}
1)):[]}async function m(e){if(!n)return 0;let t="".concat(c,"/v1/rh/notifications/unread-count?company_id=").concat(e),s=await fetch(t,{method:"GET",headers:await d()});if(!s.ok)throw Error("Failed to fetch unread count: ".concat(s.statusText));return(await s.json()).count||0}async function x(e,t){if(!n)return;if(!t||"undefined"===t)throw Error("Invalid notification id");let s=String(t).trim();if(s.length<10)throw Error("Invalid notification id");let a="".concat(c,"/v1/rh/notifications/").concat(s,"/read?company_id=").concat(e),r=await fetch(a,{method:"POST",headers:await d()});if(!r.ok)throw Error("Failed to mark notification as read: ".concat(r.statusText))}async function h(e){if(!n)return;let t="".concat(c,"/v1/rh/notifications/read-all?company_id=").concat(e),s=await fetch(t,{method:"POST",headers:await d()});if(!s.ok)throw Error("Failed to mark all notifications as read: ".concat(s.statusText))}async function p(e,t){if(!n)return;let s="".concat(c,"/v1/rh/notifications/").concat(t,"?company_id=").concat(e),a=await fetch(s,{method:"DELETE",headers:await d()});if(!a.ok)throw Error("Failed to delete notification: ".concat(a.statusText))}async function v(e,t){if(!n){var s;let a=new Date().toISOString();return{notification_id:"disabled-".concat(crypto.randomUUID()),company_id:e,recipient_user_id:(null===(s=t.recipient_user_ids)||void 0===s?void 0:s[0])||"disabled",type:t.type,title:t.title,message:t.message,priority:t.priority||"medium",related_entity_type:t.related_entity_type,related_entity_id:t.related_entity_id,action_url:t.action_url,action_label:t.action_label,is_read:!1,created_at:a,metadata:t.metadata}}let a="".concat(c,"/v1/rh/notifications?company_id=").concat(e),r=await fetch(a,{method:"POST",headers:await d(),body:JSON.stringify(t)});if(!r.ok)throw Error("Failed to create notification: ".concat(r.statusText));let i=await r.json();return{...i,notification_id:(null==i?void 0:i.notification_id)||(null==i?void 0:i.id)}}async function g(e){if(!n)return;let t="".concat(c,"/v1/rh/notifications/clear-read?company_id=").concat(e),s=await fetch(t,{method:"DELETE",headers:await d()});if(!s.ok)throw Error("Failed to delete read notifications: ".concat(s.statusText))}}}]);
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.