1const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["assets/react-pdf.browser-NFXtIbJ4.js","assets/index-DelWp-6b.js","assets/index-CEmMupnr.css","assets/browser-Mn9bJN7n.js","assets/PaymentReceiptPdf-Bce0SEQ7.js","assets/companyConfig-B7zyE5gL.js"])))=>i.map(i=>d[i]); 2import{u as O,r as B,v as $,j as e,L as j,W as q,g as U,at as D,_ as f,aA as i,y as P,ai as V}from"./index-DelWp-6b.js";import{c as W}from"./usePurchaseOrders-CuilBAdv.js";import{u as z,a as G,b as H,c as K}from"./usePaymentMethods-BW9CRr9m.js";import{u as Q}from"./useQuery-B9eAwr7I.js";import{b as J}from"./finance.service-SNgILG24.js";import{a as X,o as Y,s as c,d as Z,n as ee}from"./schemas-VY_rVlns.js";import{F as N}from"./FormField-Dxd-zNjA.js";import{f as A}from"./enums-Cc21cdqO.js";import{D as te}from"./dollar-sign-BE3VKhn-.js";import{C as g}from"./circle-check-big-BNy0DKH2.js";import{D as se}from"./download-B0-Qxbs8.js";import"./useMutation-CwYdRQCa.js";import"./purchase.service-B2GUOycb.js";import"./axios-B9iLQH_W.js";import"./standard-masters.service-5cGfGpYm.js";import"./crud.service-B2C_PX1U.js";import"./paging-BO6Zj7Ff.js";import"./bulkPost.types-OO5AaCPP.js";import"./clsx-B-dksMZM.js";import"./bundle-mjs-BvL7hA86.js";const ae=Y({paymentType:ee(A).default(A.Advance),paymentDate:c(),amount:Z().min(.01,"Must be positive"),currency:c(),paymentMethodId:c().min(1,"Select a payment method"),bankAccountId:c().optional(),bankReference:c().optional(),notes:c().optional()}),m="w-full px-3 py-2 border border-slate-300 rounded-lg text-sm bg-white focus:outline-none focus:ring-2 focus:ring-[var(--primary-500)] focus:border-transparent shadow-sm";function ke(){const{id:R}=O(),{data:s,isLoading:I}=W(R),x=z(),S=G(),p=H(),{data:T}=K(),u=T??[],{data:v=[]}=Q({queryKey:["bank-accounts","active"],queryFn:()=>J.getAll(!0)}),[n,o]=B.useState(null),{register:l,handleSubmit:M,reset:C,watch:w}=$({resolver:X(ae),defaultValues:{paymentDate:new Date().toISOString().split("T")[0],currency:(s==null?void 0:s.currency)||"SAR",amount:(s==null?void 0:s.balanceDue)||0,paymentMethodId:""}}),L=w("paymentMethodId"),r=u.find(t=>t.id===L),_=w("bankAccountId");if(I)return e.jsx("div",{className:"p-10 flex justify-center",children:e.jsx(j,{className:"w-8 h-8 animate-spin text-[var(--primary-500)]"})});if(!s)return e.jsx("div",{className:"p-10 text-center text-red-500",children:"Error loading PO"});const E=t=>{var d;if(t.amount>s.balanceDue)return i.error(`Cannot pay more than balance due (${s.balanceDue})`);const a=u.find(b=>b.id===t.paymentMethodId);if((a!=null&&a.bankRequired||a!=null&&a.referenceRequired)&&!((d=t.bankReference)!=null&&d.trim()))return i.error(`"${a==null?void 0:a.name}" requires a bank / reference number`);const h={purchaseOrderId:s.id,paymentType:t.paymentType,paymentMethodId:t.paymentMethodId,paymentDate:t.paymentDate,amount:t.amount,currency:t.currency,bankReference:t.bankReference||void 0,notes:t.notes||void 0,bankAccountId:t.bankAccountId||void 0};x.mutate(h,{onSuccess:()=>{i.success("Payment recorded"),C({amount:s.balanceDue-t.amount,paymentDate:new Date().toISOString().split("T")[0],currency:s.currency,paymentMethodId:t.paymentMethodId})}})};return e.jsxs("div",{className:"max-w-5xl mx-auto space-y-6",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx(q,{to:`/purchasing/purchase-orders/${s.id}`,className:"w-10 h-10 flex items-center justify-center bg-white rounded-full shadow-sm text-slate-500 hover:text-slate-900 transition-colors",children:e.jsx(U,{className:"w-5 h-5"})}),e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold text-slate-900",children:"Manage Payments"}),e.jsxs("p",{className:"text-sm text-slate-500 mt-1",children:["PO ",s.poNumber," ⢠",s.vendorName]})]})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[e.jsxs("div",{className:"bg-white p-5 rounded-xl shadow-card border-t-4 border-slate-300",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(te,{className:"w-4 h-4 text-slate-400"}),e.jsx("p",{className:"text-xs font-medium text-slate-500 uppercase tracking-wide",children:"Grand Total"})]}),e.jsx("p",{className:"text-2xl font-bold text-slate-900",children:s.grandTotal.toLocaleStr
2ing("en-US",{minimumFractionDigits:2})}),e.jsx("p",{className:"text-sm text-slate-500 mt-0.5",children:s.currency})]}),e.jsxs("div",{className:"bg-white p-5 rounded-xl shadow-card border-t-4 border-green-400",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(g,{className:"w-4 h-4 text-green-500"}),e.jsx("p",{className:"text-xs font-medium text-slate-500 uppercase tracking-wide",children:"Total Paid"})]}),e.jsx("p",{className:"text-2xl font-bold text-green-600",children:s.totalPaid.toLocaleString("en-US",{minimumFractionDigits:2})}),e.jsx("p",{className:"text-sm text-slate-500 mt-0.5",children:s.currency})]}),e.jsxs("div",{className:"bg-white p-5 rounded-xl shadow-card border-t-4 border-red-400",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx(D,{className:"w-4 h-4 text-red-400"}),e.jsx("p",{className:"text-xs font-medium text-slate-500 uppercase tracking-wide",children:"Balance Due"})]}),e.jsx("p",{className:`text-2xl font-bold ${s.balanceDue>0?"text-red-500":"text-green-600"}`,children:s.balanceDue.toLocaleString("en-US",{minimumFractionDigits:2})}),e.jsx("p",{className:"text-sm text-slate-500 mt-0.5",children:s.currency})]})]}),e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-3 gap-6",children:[e.jsxs("div",{className:"lg:col-span-2 bg-white rounded-xl shadow-card overflow-hidden",children:[e.jsx("div",{className:"px-6 py-4 border-b border-slate-100",children:e.jsx("h3",{className:"font-semibold text-slate-800",children:"Payment History"})}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm text-left",children:[e.jsx("thead",{className:"bg-slate-50 text-slate-500 text-xs uppercase",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-6 py-3 font-medium",children:"Date"}),e.jsx("th",{className:"px-6 py-3 font-medium",children:"Type"}),e.jsx("th",{className:"px-6 py-3 font-medium",children:"Method"}),e.jsx("th",{className:"px-6 py-3 font-medium",children:"Reference"}),e.jsx("th",{className:"px-6 py-3 font-medium text-right",children:"Amount"}),e.jsx("th",{className:"px-6 py-3 font-medium",children:"In the books"}),e.jsx("th",{className:"px-6 py-3"})]})}),e.jsxs("tbody",{className:"divide-y divide-slate-100",children:[s.payments.map(t=>e.jsxs("tr",{className:"hover:bg-[rgba(var(--primary-rgb),0.07)] transition-colors",children:[e.jsx("td",{className:"px-6 py-4",children:new Date(t.paymentDate).toLocaleDateString()}),e.jsx("td",{className:"px-6 py-4 text-slate-600",children:t.paymentTypeDisplay}),e.jsx("td",{className:"px-6 py-4 text-slate-600",children:t.paymentMethodDisplay}),e.jsx("td",{className:"px-6 py-4 text-slate-500 text-xs",children:t.bankReference||"â"}),e.jsxs("td",{className:"px-6 py-4 text-right font-semibold text-slate-800",children:[t.amount.toLocaleString("en-US",{minimumFractionDigits:2})," ",t.currency]}),e.jsx("td",{className:"px-6 py-4",children:t.isBooked?e.jsxs("span",{className:"inline-flex items-center gap-1 text-xs font-semibold text-green-700",children:[e.jsx(g,{className:"w-3.5 h-3.5"}),t.advanceNumber||t.bankVoucherNumber||"Posted"]}):e.jsxs("button",{onClick:()=>o({id:t.id,reference:t.paymentReference,bankAccountId:""}),className:"inline-flex items-center gap-1 text-xs font-semibold text-amber-700 hover:text-amber-900",title:"This payment is not in the ledger yet",children:[e.jsx(D,{className:"w-3.5 h-3.5"})," Book it"]})}),e.jsx("td",{className:"px-6 py-4 text-right",children:e.jsxs("div",{className:"flex items-center justify-end gap-1",children:[e.jsx("button",{onClick:async()=>{try{const[{pdf:a},{default:h},{createElement:d}]=await Promise.all([f(()=>import("./react-pdf.browser-NFXtIbJ4.js"),__vite__mapDeps([0,1,2,3])),f(()=>import("./PaymentReceiptPdf-Bce0SEQ7.js"),__vite__mapDeps([4,1,2,0,3,5])),f(()=>import("./index-DelWp-6b.js").then(F=>F.R),__vite__mapDeps([1,2]))]),b=await a(d(h,{payment:t,poNumber:s.poNumber,vendorName:s.vendorName})).toBlob(),k=URL.createObjectURL(b),y=document.createElement("a");y.href=k,y.download=`${t.paymentReference}.pdf`,y.click(),URL.revokeObjectURL(k)}catch(a){i.error(P(a,"Failed to generate PDF"))}},className:"p-1.5 rounded-lg text-slate-400 hover:text-[var(--primary-500)] hover:bg-[var(--primary-50)] transition-colors",title:"Download Receipt",children:e.jsx(se,{className:"w-4 h-4"})}),e.jsx("button",{onClick:()=>S.mutate(t.id),className:"p-1.5 rounded-lg text-slate-400 hover:text-red-600 hover:bg-red-50 transition-colors",title:"Delete",children:e.jsx(V,{className:"w-4 h-4"})})]})})]},t.id)),s.payments.length===0&&e.jsx("tr",{children:e.jsx("td",{colSpan:6,className:"px-6 py-10 text-center text-slate-400 text-sm",children:"No payments recorded yet"})})]})]})})]}),e.jsxs("div",{className:"lg:col-span-1 bg-white rounded-xl shadow-card overflow-hidden",children:[e.jsx("div",{className:"px-6 py-4 border-b border-slate-100",children:e.jsx("h3",{className:"font-semibold text-slate-800",children:"Record New Payment"})}),e.jsxs("form",{onSubmit:M(E),className:"p-6 space-y-4",children:[e.jsxs("div",{className:"flex flex-col gap-1",children:[e.jsx("label",{className:"text-sm font-medium text-slate-700",children:"Payment Type"}),e.jsxs("select",{...l("paymentType",{valueAsNumber:!0}),className:m,children:[e.jsx("option",{value:"Advance",children:"Advance Payment"}),e.jsx("option",{value:"Milestone",children:"Milestone"}),e.jsx("option",{value:"Final",children:"Final"}),e.jsx("option",{value:"Retention",children:"Retention"})]})]}),e.jsx(N,{label:"Amount",type:"number",step:"0.01",...l("amount",{valueAsNumber:!0})}),e.jsxs("div",{className:"flex flex-col gap-1",children:[e.jsx("label",{className:"text-sm font-medium text-slate-700",children:"Payment Method"}),e.jsxs("select",{...l("paymentMethodId"),className:m,children:[e.jsx("option",{value:"",children:"â Select method â"}),u.map(t=>e.jsxs("option",{value:t.id,children:[t.name,t.methodType?` · ${t.methodType}`:""]},t.id))]}),((r==null?void 0:r.bankRequired)||(r==null?void 0:r.referenceRequired))&&e.jsxs("p",{className:"text-xs text-amber-600 font-medium",children:['"',r.name,'" requires a reference number.']})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx("label",{className:"block text-sm font-medium text-slate-700",children:"Bank account"}),e.jsxs("select",{...l("bankAccountId"),className:m,children:[e.jsx("option",{value:"",children:"â Not now (record only) â"}),v.filter(t=>t.isActive).map(t=>e.jsxs("option",{value:t.id,children:[t.bankName," · ",t.accountNumber,t.currency?` · ${t.currency}`:""]},t.id))]}),e.jsx("p",{className:"text-xs text-slate-400",children:_?"The supplier advance, its journal and the bank voucher are posted with this payment â all of it, or none.":"Without a bank the payment is recorded but not in the books, and waits on the posting-failures list."})]}),e.jsx(N,{label:"Date",type:"date",...l("paymentDate")}),e.jsx(N,{label:"Reference #",...l("bankReference")}),s.balanceDue===0&&e.jsxs("div",{className:"flex items-center gap-2 bg-green-50 border border-green-200 rounded-lg px-3 py-2 text-xs text-green-700",children:[e.jsx(g,{className:"w-3.5 h-3.5 shrink-0"}),"This PO is fully paid."]}),e.jsxs("button",{type:"submit",disabled:x.isPending||s.balanceDue===0,className:"w-full bg-[var(--primary-500)] hover:bg-[var(--primary-600)] text-white rounded-lg py-2.5 font-medium text-sm transition-colors shadow-sm disabled:opacity-50 flex items-center justify-center gap-2",children:[x.isPending&&e.jsx(j,{className:"w-4 h-4 animate-spin"}),"Record Payment"]})]})]})]}),n&&e.jsx("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-slate-900/50 backdrop-blur-sm p-4",onClick:()=>o(null),children:e.jsxs("div",{className:"w-full max-w-md bg-white rounded-2xl shadow-2xl overflow-hidden",onClick:t=>t.stopPropagation(),children:[e.jsxs("div",{className:"px-6 py-4 border-b border-slate-100",children:[e.jsxs("h3",{className:"font-semibold text-slate-800",children:["Put ",n.reference," into the books"]}),e.jsx("p",{className:"text-xs text-slate-500 mt-1",children:"Dr Supplier Advance / Cr the bank below, on the payment's own date."})]}),e.jsxs("div",{className:"px-6 py-5 space-y-2",children:[e.jsx("label",{className:"block text-sm font-medium text-slate-700",children:"Bank the money left"}),e.jsxs("select",{value:n.bankAccountId,onChange:t=>o({...n,bankAccountId:t.target.value}),className:m,children:[e.jsx("option",{value:"",children:"â Select a bank â"}),v.filter(t=>t.isActive).map(t=>e.jsxs("option",{value:t.id,children:[t.bankName," · ",t.accountNumber,t.currency?` · ${t.currency}`:""]},t.id))]})]}),e.jsxs("div",{className:"px-6 py-4 bg-slate-50 border-t border-slate-100 flex justify-end gap-3",children:[e.jsx("button",{onClick:()=>o(null),className:"px-4 py-2 text-sm font-medium text-slate-600 bg-white border border-slate-200 rounded-lg hover:bg-slate-50",children:"Cancel"}),e.jsxs("button",{disabled:!n.bankAccountId||p.isPending,onClick:()=>p.mutate({id:n.id,dto:{bankAccountId:n.bankAccountId}},{onSuccess:()=>{i.success(`${n.reference} is in the books`),o(null)},onError:t=>i.error(P(t,"Could not book the payment"))}),className:"px-4 py-2 text-sm font-medium text-white bg-[var(--primary-500)] rounded-lg hover:bg-[var(--primary-600)] disabled:opacity-50 flex items-center gap-2",children:[p.isPending&&e.jsx(j,{className:"w-4 h-4 animate-spin"}),"Book it"]})]})]})})]})}export{ke as default};
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.