import{e as b,q as V,i as Z,a as B,r as l,j as e,P as G,S as X,s as Y,d as p}from"./index-D4ZMoJFe.js";import{f as J,a as K}from"./formatters-jmx2pV9o.js";import{g as U,p as W}from"./mgxDepotFilter-Vtxq-a-5.js";import{g as ee,M as te}from"./MgxCompanyDepotFields-yc-AGAeU.js";import{T as se}from"./trash-2-CGotPSCR.js";/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const ae=b("ArrowDownToLine",[["path",{d:"M12 17V3",key:"1cwfxf"}],["path",{d:"m6 11 6 6 6-6",key:"12ii2o"}],["path",{d:"M19 21H5",key:"150jfl"}]]);/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const ne=b("ArrowLeftRight",[["path",{d:"M8 3 4 7l4 4",key:"9rb6wj"}],["path",{d:"M4 7h16",key:"6tx8e3"}],["path",{d:"m16 21 4-4-4-4",key:"siv7j2"}],["path",{d:"M20 17H4",key:"h6l3hr"}]]);/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const le=b("SlidersHorizontal",[["line",{x1:"21",x2:"14",y1:"4",y2:"4",key:"obuewd"}],["line",{x1:"10",x2:"3",y1:"4",y2:"4",key:"1q6298"}],["line",{x1:"21",x2:"12",y1:"12",y2:"12",key:"1iu8h1"}],["line",{x1:"8",x2:"3",y1:"12",y2:"12",key:"ntss68"}],["line",{x1:"21",x2:"16",y1:"20",y2:"20",key:"14d8ph"}],["line",{x1:"12",x2:"3",y1:"20",y2:"20",key:"m0wm8r"}],["line",{x1:"14",x2:"14",y1:"2",y2:"6",key:"14e1ph"}],["line",{x1:"8",x2:"8",y1:"10",y2:"14",key:"1i6ji0"}],["line",{x1:"16",x2:"16",y1:"18",y2:"22",key:"1lctlv"}]]),v=new Date,ie=new Date(v.getFullYear(),v.getMonth(),1).toISOString().slice(0,10),g=v.toISOString().slice(0,10),A={in:"text-accent2",out:"text-danger",adjustment:"text-gold",transfer:"text-accent"},oe={reception:"Réception",purchase_return:"Retour fournisseur",maintenance_intervention:"Entretien",transfer:"Transfert"},f={company_id:"",movement_type:"in",item_id:"",depot_id:"",destination_depot_id:"",movement_date:g,quantity:"",unit_price:"",reference_type:"",reference_id:"",notes:"",real_quantity:"",adjustment_reason:""};function xe(){const T=V(),{pushToast:L}=Z(),{user:_}=B(),c=ee(_),[r,N]=l.useState(c),[h,R]=l.useState([]),[y,z]=l.useState([]),[k,E]=l.useState([]),[i,d]=l.useState({from:ie,to:g,depot_id:U(),item_id:"",type:""}),[S,F]=l.useState([]),[q,C]=l.useState(!0),[w,j]=l.useState(""),[P,x]=l.useState(!1),[a,o]=l.useState({...f,company_id:c}),M=l.useMemo(()=>({company_id:r,...i,depot_id:i.depot_id||void 0,item_id:i.item_id||void 0,type:i.type||void 0}),[r,i]);l.useEffect(()=>{!r&&c&&N(c)},[r,c]);const D=async()=>{var t,s;if(r){C(!0),j("");try{const[n,m,Q,$]=await Promise.all([p.get("/mgx/consumable-movements",{params:M}),p.get("/mgx/consumable-items",{params:{company_id:r}}),p.get("/mgx/depots",{params:{all_companies:!0}}),p.get("/mgx/receptions",{params:{company_id:r}})]);F(n.data.items||[]),R(m.data.items||[]),z(Q.data.items||[]),E($.data.items||[])}catch(n){j(((s=(t=n==null?void 0:n.response)==null?void 0:t.data)==null?void 0:s.message)||"Impossible de charger les mouvements consommables.")}finally{C(!1)}}};l.useEffect(()=>{D()},[M]),l.useEffect(()=>{W(i.depot_id)},[i.depot_id]);const u=t=>{o({...f,company_id:r,movement_type:t,movement_date:g}),x(!0)},H=async t=>{var s,n;t.preventDefault();try{await p.post("/mgx/consumable-movements",{...a,company_id:a.company_id||r}),L({title:"Mouvement enregistré",tone:"success"}),x(!1),o({...f,company_id:r}),await D()}catch(m){j(((n=(s=m==null?void 0:m.response)==null?void 0:s.data)==null?void 0:n.message)||"Impossible d'enregistrer le mouvement.")}},O=async t=>{await T({title:"Suppression non disponible",description:"Les mouvements consommables sont historisés. La suppression directe n'est pas encore activée.",confirmLabel:"Fermer"})},I=h.find(t=>String(t.id)===String(a.item_id));return e.jsxs("div",{className:"space-y-6",children:[e.jsx("section",{className:"card-panel p-6 sm:p-8",children:e.jsxs("div",{className:"flex flex-col gap-4 lg:flex-row lg:items-end lg:justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs uppercase tracking-[0.2em] text-gold",children:"MGX · Consommables"}),e.jsx("h1",{className:"mt-3 text-3xl font-semibold text-text",children:"Mouvements Consommables"})]}),e.jsxs("div",{className:"flex flex-wrap gap-3",children:[e.jsxs("button",{type:"button",className:"btn-primary inline-flex items-center gap-2",onClick:()=>u("in"),children:[e.jsx(G,{size:16})," Entrée"]}),e.jsxs("button",{type:"button",className:"btn-secondary inline-flex items-center gap-2",onClick:()=>u("out"),children:[e.jsx(ae,{size:16})," Sortie"]}),e.jsxs("button",{type:"button",className:"btn-secondary inline-flex items-center gap-2",onClick:()=>u("adjustment"),children:[e.jsx(le,{size:16})," Ajustement"]}),e.jsxs("button",{type:"button",className:"btn-secondary inline-flex items-center gap-2",onClick:()=>u("transfer"),children:[e.jsx(ne,{size:16})," Transfert"]})]})]})}),e.jsx("section",{className:"card-panel p-5",children:e.jsxs("div",{className:"grid gap-3 xl:grid-cols-[150px_150px_220px_minmax(0,1fr)_220px]",children:[e.jsx("input",{type:"date",className:"input-dark",value:i.from,onChange:t=>d(s=>({...s,from:t.target.value}))}),e.jsx("input",{type:"date",className:"input-dark",value:i.to,onChange:t=>d(s=>({...s,to:t.target.value}))}),e.jsxs("select",{className:"input-dark",value:i.depot_id,onChange:t=>d(s=>({...s,depot_id:t.target.value})),children:[e.jsx("option",{value:"",children:"Tous dépôts"}),y.map(t=>e.jsx("option",{value:t.id,children:t.label},t.id))]}),e.jsxs("label",{className:"relative",children:[e.jsx(X,{className:"absolute left-3 top-1/2 -translate-y-1/2 text-textMuted",size:16}),e.jsxs("select",{className:"input-dark pl-10",value:i.item_id,onChange:t=>d(s=>({...s,item_id:t.target.value})),children:[e.jsx("option",{value:"",children:"Tous articles"}),h.map(t=>e.jsxs("option",{value:t.id,children:[t.code," · ",t.label]},t.id))]})]}),e.jsxs("select",{className:"input-dark",value:i.type,onChange:t=>d(s=>({...s,type:t.target.value})),children:[e.jsx("option",{value:"",children:"Tous types"}),e.jsx("option",{value:"in",children:"Entrée"}),e.jsx("option",{value:"out",children:"Sortie"}),e.jsx("option",{value:"adjustment",children:"Ajustement"}),e.jsx("option",{value:"transfer",children:"Transfert"})]})]})}),w?e.jsx("div",{className:"rounded-2xl border border-danger/20 bg-danger/5 p-4 text-sm text-danger",children:w}):null,e.jsx("section",{className:"table-shell overflow-x-auto",children:e.jsxs("table",{className:"min-w-[1280px] w-full",children:[e.jsx("thead",{className:"table-head",children:e.jsx("tr",{children:["Date","Article","Dépôt","Type","Quantité","Prix unit","Valeur","Référence","Créé par","Actions"].map(t=>e.jsx("th",{className:"table-cell",children:t},t))})}),e.jsx("tbody",{children:q?e.jsx("tr",{children:e.jsx("td",{colSpan:10,className:"table-cell text-textMuted",children:"Chargement..."})}):S.length?S.map(t=>{var s,n;return e.jsxs("tr",{className:"table-row",children:[e.jsx("td",{className:"table-cell",children:J(t.movement_date)}),e.jsxs("td",{className:"table-cell",children:[e.jsx("p",{className:"font-mono font-semibold text-text",children:t.item.code}),e.jsx("p",{className:"text-xs text-textMuted",children:t.item.label})]}),e.jsx("td",{className:"table-cell",children:((s=t.depot)==null?void 0:s.label)||"—"}),e.jsx("td",{className:`table-cell font-semibold ${A[t.movement_type]||"text-text"}`,children:t.movement_type==="in"?"Entrée":t.movement_type==="out"?"Sortie":t.movement_type==="adjustment"?"Ajustement":"Transfert"}),e.jsx("td",{className:`table-cell amount-cell font-semibold ${A[t.movement_type]||"text-text"}`,children:Number(t.quantity||0).toLocaleString("fr-DZ",{maximumFractionDigits:3})}),e.jsx("td",{className:"table-cell amount-cell",children:t.unit_price!==null?K(t.unit_price):"—"}),e.jsx("td",{className:"table-cell amount-cell",children:t.total_value_formatted}),e.jsx("td",{className:"table-cell",children:t.reference_type?e.jsxs("div",{children:[e.jsx("p",{className:"font-semibold text-text",children:oe[t.reference_type]||t.reference_type}),e.jsx("p",{className:"text-xs text-textMuted",children:t.reference_label||`#${t.reference_id||"—"}`})]}):"—"}),e.jsx("td",{className:"table-cell",children:((n=t.created_by)==null?void 0:n.full_name)||"—"}),e.jsx("td",{className:"table-cell",children:e.jsxs("div",{className:"flex gap-2",children:[e.jsx("button",{type:"button",className:"btn-secondary px-3 py-2",disabled:!0,children:e.jsx(Y,{size:15})}),e.jsx("button",{type:"button",className:"btn-secondary px-3 py-2 text-danger",onClick:()=>O(),children:e.jsx(se,{size:15})})]})})]},t.id)}):e.jsx("tr",{children:e.jsx("td",{colSpan:10,className:"table-cell text-textMuted",children:"Aucun mouvement consommable trouvé."})})})]})}),P?e.jsx("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-[#0f172a]/30 p-4",children:e.jsxs("form",{onSubmit:H,className:"card-panel w-full max-w-3xl p-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs uppercase tracking-[0.2em] text-gold",children:"Mouvement"}),e.jsx("h2",{className:"mt-2 text-2xl font-semibold text-text",children:a.movement_type==="in"?"Entrée":a.movement_type==="out"?"Sortie":a.movement_type==="adjustment"?"Ajustement":"Transfert"})]}),e.jsx("button",{type:"button",className:"btn-secondary",onClick:()=>x(!1),children:"Fermer"})]}),e.jsxs("div",{className:"mt-6 grid gap-5 md:grid-cols-2",children:[e.jsx(te,{user:_,companyId:a.company_id||r,depotId:a.depot_id,depots:y,onCompanyChange:t=>{N(t),o(s=>({...s,company_id:t,destination_depot_id:"",item_id:""}))},onDepotChange:t=>o(s=>({...s,depot_id:t})),depotLabel:a.movement_type==="transfer"?"Dépôt source":"Dépôt"}),e.jsxs("label",{className:"space-y-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:"Article"}),e.jsxs("select",{required:!0,className:"input-dark",value:a.item_id,onChange:t=>o(s=>({...s,item_id:t.target.value})),children:[e.jsx("option",{value:"",children:"Sélectionner"}),h.map(t=>e.jsxs("option",{value:t.id,children:[t.code," · ",t.label]},t.id))]}),I?e.jsxs("span",{className:"text-xs text-textMuted",children:["Stock actuel: ",Number(I.current_stock_qty||0).toLocaleString("fr-DZ",{maximumFractionDigits:3})]}):null]}),e.jsxs("label",{className:"space-y-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:"Date"}),e.jsx("input",{type:"date",className:"input-dark",value:a.movement_date,onChange:t=>o(s=>({...s,movement_date:t.target.value}))})]}),a.movement_type==="transfer"?e.jsxs("label",{className:"space-y-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:"Dépôt destination"}),e.jsxs("select",{className:"input-dark",value:a.destination_depot_id,onChange:t=>o(s=>({...s,destination_depot_id:t.target.value})),children:[e.jsx("option",{value:"",children:"Sélectionner"}),y.map(t=>e.jsx("option",{value:t.id,children:t.label},t.id))]})]}):null,e.jsxs("label",{className:"space-y-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:a.movement_type==="adjustment"?"Quantité réelle":"Quantité"}),e.jsx("input",{type:"number",step:"0.001",className:"input-dark",value:a.movement_type==="adjustment"?a.real_quantity:a.quantity,onChange:t=>o(s=>({...s,[a.movement_type==="adjustment"?"real_quantity":"quantity"]:t.target.value}))})]}),a.movement_type!=="adjustment"?e.jsxs("label",{className:"space-y-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:"Prix unitaire"}),e.jsx("input",{type:"number",step:"0.0001",className:"input-dark",value:a.unit_price,onChange:t=>o(s=>({...s,unit_price:t.target.value}))})]}):null,a.movement_type==="in"?e.jsxs("label",{className:"space-y-2 md:col-span-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:"Lier à une réception"}),e.jsxs("select",{className:"input-dark",value:a.reference_id,onChange:t=>{const s=k.find(n=>String(n.id)===String(t.target.value));o(n=>({...n,reference_type:t.target.value?"reception":"",reference_id:t.target.value,notes:n.notes||(s==null?void 0:s.reception_number)||""}))},children:[e.jsx("option",{value:"",children:"Aucune"}),k.map(t=>e.jsxs("option",{value:t.id,children:[t.reception_number," · ",t.order_number||"Sans BC"]},t.id))]})]}):null,a.movement_type==="adjustment"?e.jsxs("label",{className:"space-y-2 md:col-span-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:"Raison de l'ajustement"}),e.jsx("textarea",{className:"input-dark min-h-24",value:a.adjustment_reason,onChange:t=>o(s=>({...s,adjustment_reason:t.target.value}))})]}):null,e.jsxs("label",{className:"space-y-2 md:col-span-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:"Notes"}),e.jsx("textarea",{className:"input-dark min-h-24",value:a.notes,onChange:t=>o(s=>({...s,notes:t.target.value}))})]})]}),e.jsxs("div",{className:"mt-6 flex justify-end gap-3",children:[e.jsx("button",{type:"button",className:"btn-secondary",onClick:()=>x(!1),children:"Annuler"}),e.jsx("button",{type:"submit",className:"btn-primary",children:"Enregistrer"})]})]})}):null]})}export{xe as default};
