import{u as V,ci as G,a as R,d as I,f5 as B,p as l,ah as M,f4 as z,F as y,r as O,e as Q,ak as T,j as s,C as H,c2 as c,Q as v,_ as q,cz as h,ba as g,c_ as D,bO as L,a3 as S,Z as U}from"./bundle.CHKSNOJF.js";import{b as X}from"./payments-C1sjF6lG.js";import{u as Z}from"./usePaymentPageSaveAction-BqBYiwW1.js";function Y(){const[o]=V(),{id:_}=G(),d=R(),{data:a}=X({id:_}),w=I(),{data:f}=B({id:a?.company_gateway_id,queryParams:"include=gateway",enabled:!!a?.company_gateway_id}),[r,b]=l.useState(),[m,j]=l.useState([]),[p,$]=l.useState(!1),[C,k]=l.useState(!1),[x,F]=l.useState(!1),N=M(),t=z({enableReinitialize:!0,initialValues:{id:a?.id,date:a?.date,invoices:[]},onSubmit:i=>{y.processing(),b(void 0);let e="/api/v1/payments/refund?&email_receipt=:email";x&&(e+="&gateway_refund=true"),O("POST",Q(e,{email:p}),i).then(()=>{y.success("refunded_payment"),N("/payments")}).catch(n=>{n.response?.status===422&&(b(n.response.data),y.dismiss())}).finally(()=>{t.setSubmitting(!1),T(["payments"])})}}),A=i=>{const e=a?.paymentables.find(({invoice_id:n})=>n===i.id);return e?e.amount-e.refunded:0},E=i=>{const e=a?.paymentables.find(({invoice_id:n})=>n===i.id);return e?`${o("invoice")} #${i.number} - ${o("refundable")} (${w(e.amount-e.refunded,a?.client?.country_id,a?.client?.settings.currency_id)})`:""};l.useEffect(()=>{a&&Array.isArray(a.invoices)&&m.map(i=>{const e=a.invoices.find(n=>n.id==i);e&&t.setFieldValue("invoices",[...t.values.invoices,{amount:A(e),invoice_id:e?.id,id:""}])})},[m]),l.useEffect(()=>{let i=0;t.values.invoices.map(e=>{i=i+Number(e.amount),j(m.filter(n=>n!=e.invoice_id))})},[t.values.invoices]),l.useEffect(()=>{if(f){const i=f.data.data.gateway,e=Object.values(i.options).some(n=>n.refund);k(e)}},[f]);const P=()=>{const i=U(t?.values.invoices).pluck("invoice_id");return a?.invoices?.filter(e=>!i.contains(e.id))};return Z({onClick:()=>t.handleSubmit(),disabled:t.isSubmitting||!t.values.invoices.length},[t.values,t.isSubmitting]),s.jsxs(H,{title:o("refund_payment"),className:"shadow-sm",style:{borderColor:d.$24},headerStyle:{borderColor:d.$20},children:[s.jsx(c,{leftSide:o("number"),children:s.jsx(v,{disabled:!0,value:a?.number})}),a&&s.jsx(c,{leftSide:o("amount"),children:s.jsx(v,{disabled:!0,value:a?.amount-a?.refunded})}),s.jsx(c,{leftSide:o("applied"),children:s.jsx(v,{disabled:!0,value:a?.applied})}),s.jsx(c,{leftSide:o("date"),children:s.jsx(v,{type:"date",value:t.values.date,onValueChange:i=>t.setFieldValue("date",i)})}),s.jsxs(c,{leftSide:o("invoices"),children:[s.jsx(q,{onValueChange:i=>{t.values.invoices.filter(e=>e.invoice_id==i).length<1&&j([...m,i])},withBlank:!0,customSelector:!0,clearAfterSelection:!0,children:P()?.map((i,e)=>s.jsx("option",{value:i.id,children:E(i)},e))}),r?.errors.invoices&&s.jsx("div",{className:"py-2",children:s.jsx(h,{children:r.errors.invoices})})]}),s.jsx(g,{}),a&&Array.isArray(a.invoices)&&t.values.invoices.map((i,e)=>{const n=a.invoices.find(u=>u.id==i.invoice_id);if(n)return s.jsxs("div",{className:"flex flex-col",children:[s.jsx(c,{leftSide:`${o("invoice")}: ${n?.number}`,children:s.jsxs("div",{className:"flex items-center space-x-2",children:[s.jsx(D,{value:t.values.invoices[e].amount||"",onValueChange:u=>t.setFieldValue(`invoices.${e}.amount`,parseFloat(u))}),s.jsx("div",{className:"cursor-pointer focus:outline-none focus:ring-0",onClick:()=>{t.setFieldValue("invoices",t.values.invoices.filter(u=>u.invoice_id!=i.invoice_id))},children:s.jsx(L,{color:d.$16,hoverColor:d.$3,borderColor:d.$5,hoverBorderColor:d.$17,size:"1.6rem"})})]})}),(r?.errors[`invoices.${[e]}.invoice_id`]||r?.errors[`invoices.${[e]}.amount`])&&s.jsx("div",{className:"px-6",children:s.jsx(h,{className:"mt-2 break-all",children:r?.errors[`invoices.${[e]}.invoice_id`]||r?.errors[`invoices.${[e]}.amount`]})})]},e)}),!!(a&&Array.isArray(a.invoices)&&t.values.invoices.length)&&s.jsx(g,{className:"pt-4",withoutPadding:!0}),s.jsx(c,{leftSide:o("send_email"),leftSideHelp:o("email_receipt"),children:s.jsx(S,{checked:p,onChange:()=>{$(!p)}})}),C&&s.jsx(c,{leftSide:o("gateway_refund"),leftSideHelp:o("gateway_refund_help"),children:s.jsx(S,{checked:x,onChange:i=>F(i)})}),r?.errors.id&&s.jsx(h,{children:r.errors.id})]})}export{Y as default};
