import{cg as V,aa as q,ch as L,F as v,r as M,e as H,ak as J,u as T,aA as P,b_ as D,ci as E,h as n,j as s,cj as K,ac as Q,ce as W,gA as $,p as c,ck as z,aS as G,cq as X,aF as Y,E as Z,bS as ee,ae as se,cm as re,cn as te,S as ae,aJ as oe,aK as ie,c7 as ce}from"./bundle.CHKSNOJF.js";import{C as ne}from"./CommonActions-CCjRlVnA.js";import{P as ue}from"./PreviousNextNavigation-vVnyKojr.js";import{u as de}from"./usePurchaseOrderUtilities-CdSMZmEe.js";import"./Actions-Cg-_a1t3.js";import"./invoice-status-C0SI5VYC.js";import"./useShouldDisplayClientGatewaysAndAutoBill-mN5p-ZyL.js";import"./hooks-9RDJr6hd.js";import"./CreditStatus-WHg8w3rC.js";import"./useInvoiceColumns-CGbyIb88.js";import"./InvoiceStatus-1S0ZEgN9.js";function me(d){const{setErrors:r,isDefaultFooter:a,isDefaultTerms:o,isFormBusy:t,setIsFormBusy:i}=d,m=V(),p=q(L);return f=>{if(t)return;i(!0),r(void 0),v.processing();let u="/api/v1/purchase_orders/:id?";o?(u+="save_default_terms=true",a&&(u+="&save_default_footer=true")):a&&(u+="save_default_footer=true"),M("PUT",H(u,{id:f.id}),f).then(async()=>{(o||a)&&await m(),v.success("updated_purchase_order")}).catch(l=>{if(l.response?.status===422){const h=l.response.data;h.errors.amount?v.error(h.errors.amount[0]):v.dismiss(),r(h)}}).finally(()=>{p(void 0),J(["purchase_orders"]),i(!1)})}}function le(d){const[r]=T(),a=P(),o=D(),{id:t}=E(),{purchaseOrder:i}=d,m=a("view_purchase_order")||a("edit_purchase_order")||o(i);return[{name:r("edit"),href:n("/purchase_orders/:id/edit",{id:t})},{name:r("documents"),href:n("/purchase_orders/:id/documents",{id:t}),enabled:m,formatName:()=>s.jsx(K,{numberOfDocuments:i?.documents?.length})},{name:r("settings"),href:n("/purchase_orders/:id/settings",{id:t})},{name:r("activity"),href:n("/purchase_orders/:id/activity",{id:t})},{name:r("history"),href:n("/purchase_orders/:id/history",{id:t})},{name:r("email_history"),href:n("/purchase_orders/:id/email_history",{id:t})}]}function Se(){const{documentTitle:d}=Q("edit_purchase_order"),[r]=T(),[a]=W(),{id:o}=E(),{data:t}=$({id:o}),i=P(),m=D(),p=[{name:r("purchase_orders"),href:"/purchase_orders"},{name:r("edit_purchase_order"),href:n("/purchase_orders/:id/edit",{id:o})}],[f,u]=c.useState(),[l,h]=c.useState(!1),[O,b]=c.useState(),[g,F]=c.useState(!1),[A,C]=c.useState(!1),[e,y]=z(G),I=X(),N=le({purchaseOrder:e}),{calculateInvoiceSum:B}=de({purchaseOrder:e,setPurchaseOrder:y,setInvoiceSum:b}),R=me({setErrors:u,isDefaultTerms:g,isDefaultFooter:A,isFormBusy:l,setIsFormBusy:h}),{changeTemplateVisible:k,setChangeTemplateVisible:U,changeTemplateResources:w}=Y();return c.useEffect(()=>{const S=a.get("action")&&e?e:t;if(S){const x=Z.cloneDeep(S);x.line_items.forEach(_=>_._id=ee()),x.invitations.forEach(_=>_.client_contact_id=_.client_contact_id||""),y(x)}},[t]),c.useEffect(()=>{e&&B(e)},[e]),s.jsxs(se,{title:d,breadcrumbs:p,...(i("edit_purchase_order")||m(e))&&e&&{navigationTopRight:s.jsx(ce,{resource:e,onSaveClick:()=>R(e),actions:I,cypressRef:"purchaseOrderActionDropdown",disableSaveButton:!e||l})},afterBreadcrumbs:s.jsx(ue,{entity:"purchase_order"}),children:[e?.id===o?s.jsxs("div",{className:"space-y-4",children:[s.jsx(re,{tabs:N,rightSide:e&&s.jsx("div",{className:"flex items-center",children:s.jsx(ne,{resource:e,entity:"purchase_order"})})}),s.jsx(te,{context:{purchaseOrder:e,setPurchaseOrder:y,errors:f,isDefaultTerms:g,setIsDefaultTerms:F,isDefaultFooter:A,setIsDefaultFooter:C,invoiceSum:O,setInvoiceSum:b}})]}):s.jsx("div",{className:"flex justify-center items-center",children:s.jsx(ae,{})}),s.jsx(oe,{entity:"purchase_order",entities:w,visible:k,setVisible:U,labelFn:j=>s.jsxs("div",{className:"flex flex-col space-y-1",children:[s.jsx(ie,{children:r("number")}),s.jsx("span",{children:j.number})]}),bulkUrl:"/api/v1/purchase_orders/bulk"})]})}export{Se as default};
