import{r as n,u as E,j as t,D as l}from"./index-DlcHsm52.js";import{F as s,g as F,C as k,B as f,a as A,T as R,S as u,b as m,I as j,r as o,s as b}from"./IdurarOs-j0IoqkpA.js";import{W as V}from"./shared-N8zi2N1N.js";import{c as O}from"./ErpApp-7VdZNpBd.js";import{c as D}from"./currencyList-mYH1Mcza.js";import{u as U}from"./useMoney-DK8w0tFT.js";import{M}from"./index-Clbw7Jrz.js";import{R as W}from"./ReloadOutlined-C37-gl6w.js";const B=D(),L={draft:"default",pending_approval:"gold",approved:"blue",processing:"gold",received:"green",returned:"orange",refunded:"purple",cancelled:"red"};function X(){const[g,v]=n.useState([]),[I,P]=n.useState([]),[S,c]=n.useState(!1),[C,p]=n.useState(!1),[d]=s.useForm(),h=E(F),{moneyFormatter:T}=U(),y=O(h,"purchaseorder","create"),w=O(h,"purchaseorder","update"),N=["draft","pending_approval","approved","processing","received","returned","refunded","cancelled"],i=async()=>{p(!0);const[e,r]=await Promise.all([o.list({entity:"purchaseorder",options:{page:1,items:100}}),o.list({entity:"vendor",options:{page:1,items:100}})]);v((e==null?void 0:e.result)||[]),P((r==null?void 0:r.result)||[]),p(!1)};n.useEffect(()=>{i()},[]);const _=async()=>{const e=await d.validateFields();await o.create({entity:"purchaseorder",jsonData:{vendor:e.vendor,paymentTerms:e.paymentTerms,number:e.number,items:[{itemName:e.itemName,quantity:e.quantity,price:e.price,total:e.quantity*e.price}],total:{amount:e.quantity*e.price,currency:e.currency||l}}}),b.success("Purchase order created"),c(!1),d.resetFields(),i()},q=async(e,r)=>{const a=await o.patch({entity:`purchaseorder/${e._id}/status`,jsonData:{status:r}});a!=null&&a.success&&(b.success("Purchase order status updated"),i())};return t.jsxs(V,{title:"Purchase Order Workbench",description:"Handle vendor purchase orders and surface procurement status.",permission:"vendors.purchaseorder.read",extra:t.jsxs(k,{children:[t.jsx(f,{icon:t.jsx(W,{}),onClick:i,children:"Refresh"}),t.jsx(f,{type:"primary",disabled:!y,onClick:()=>c(!0),children:"New Purchase Order"})]}),children:[t.jsx(A,{rowKey:"_id",loading:C,dataSource:g,pagination:!1,columns:[{title:"Number",dataIndex:"number"},{title:"Vendor",dataIndex:["vendor","name"]},{title:"Payment Terms",dataIndex:"paymentTerms"},{title:"Total",render:(e,r)=>{var a,x;return T({amount:((a=r.total)==null?void 0:a.amount)||r.total||0,currency_code:((x=r.total)==null?void 0:x.currency)||r.currency||l})}},{title:"Status",dataIndex:"status",render:e=>t.jsx(R,{color:L[e]||"default",children:e||"draft"})},{title:"Action",render:(e,r)=>t.jsx(u,{size:"small",value:r.status,disabled:!w,style:{width:170},onChange:a=>q(r,a),options:N.map(a=>({value:a,label:a}))})}]}),t.jsx(M,{title:"New Purchase Order",open:S,onCancel:()=>c(!1),onOk:_,okButtonProps:{disabled:!y},children:t.jsxs(s,{form:d,layout:"vertical",children:[t.jsx(s.Item,{name:"vendor",label:"Vendor",rules:[{required:!0}],children:t.jsx(u,{options:I.map(e=>({value:e._id,label:e.name}))})}),t.jsx(s.Item,{name:"number",label:"Number",rules:[{required:!0}],children:t.jsx(m,{min:1,style:{width:"100%"}})}),t.jsx(s.Item,{name:"itemName",label:"Item",rules:[{required:!0}],children:t.jsx(j,{})}),t.jsx(s.Item,{name:"quantity",label:"Quantity",initialValue:1,children:t.jsx(m,{min:1,style:{width:"100%"}})}),t.jsx(s.Item,{name:"price",label:"Price",initialValue:0,children:t.jsx(m,{min:0,style:{width:"100%"}})}),t.jsx(s.Item,{name:"currency",label:"Currency",initialValue:l,children:t.jsx(u,{options:B,showSearch:!0})}),t.jsx(s.Item,{name:"paymentTerms",label:"Payment Terms",initialValue:"Net 30",children:t.jsx(j,{})})]})})]})}export{X as default};
