// Payment receipt: confirms a payment received and settles it against
// the tax invoices it pays, with any tax the client deducted at source
// (TDS) shown, so both sides' books agree on what is still owed.
//
// A receipt for payment against a tax invoice is not a GST document and
// carries no tax of its own: the GST was charged on the invoice.
//
// Not for advances. Money received before the service is supplied
// carries GST of its own, and needs a tax invoice for the advance or a
// GST receipt voucher (rule 50 of the CGST Rules) showing that tax. This
// receipt is neither. The kit bills advances with a tax invoice on the
// day they arrive (see proforma-invoice.typ); then this receipt settles
// that invoice like any other.
//
// The client and the figures below are examples; replace every one.
//
// How this template is laid out:
//   1. The import.
//   2. The data block: every fact the receipt states, and the only part
//      to edit. Unknown values stay `none` and print as marked blanks.
//   3. The layout, which reads only from the data block.

#import "../rivant.typ": *

// ---------------------------------------------------------------- data

#let data = (
  // true while it's still being checked: prints the DRAFT watermark.
  draft: false,
  // The next number in the receipt register (none prints a blank).
  seq: 9,
  // Date of issue, YYYY-MM-DD.
  date: "2026-09-10",
  // What it's for, set light after the title.
  subject: "for August 2026",
  // Who paid, as on the invoices.
  client: (
    name: "Sahyadri Foods Private Limited",
    address: ("Office 604, Pentagon P2, Magarpatta City,", "Hadapsar, Pune, Maharashtra 411013"),
    // As on the invoices (none prints a blank; remove the line to leave
    // it off).
    gstin: none,
  ),
  // How the money came in, from the bank statement.
  payment: (
    // The date it reached our account.
    date: "2026-09-10",
    // "NEFT", "RTGS", "IMPS", "UPI" or "Cheque".
    mode: "NEFT",
    // The UTR or cheque number, exactly as on the bank statement.
    reference: none,
    // The bank it was paid from (cheques), or none.
    drawn-on: none,
  ),
  // The tax invoices this payment settles. number: as in the register;
  // value: the invoice total; tds: the tax the client deducted at source
  // on it (0 if none), from their remittance advice; received: the part
  // of this payment set against it.
  invoices: (
    (
      number: "INV/2026-27/0014",
      date: "2026-09-01",
      value: 156940,
      tds: 2660,
      received: 154280,
    ),
  ),
  // Who signs: a finance document, so the Managing Director.
  signatory: (name: "Paras Deshpande", title: "Managing Director & CEO"),
)

// -------------------------------------------------------------- layout

#let k = document-kinds.at("receipt")

// Money as exact decimals, for sums.
#let dec(x) = if type(x) == decimal { x } else if type(x) == int { decimal(x) } else { decimal(str(x)) }
#let money(x) = inr(x, symbol: false)
#let total(key) = data.invoices.map(i => dec(i.at(key))).sum(default: decimal(0))
#let balance(i) = dec(i.value) - dec(i.tds) - dec(i.received)

#let received = total("received")
#let tds = total("tds")
#let cheque = lower(data.payment.mode) == "cheque"
#let mode = if cheque { "Cheque" } else if upper(data.payment.mode) == "UPI" { "UPI" } else {
  "Bank transfer (" + data.payment.mode + ")"
}

#show: rivant-document.with(
  kind: k.name,
  title: k.name,
  title-light: data.subject,
  reference: ref-no(k.code, data.seq, date: data.date),
  date: data.date,
  letterhead: k.letterhead,
  watermark: if data.draft { "draft" } else { none },
)

#parties(
  party("Received from", data.client),
  party("Payment", (
    name: mode,
    extra: (
      ("Paid on", long-date(data.payment.date, field: "Payment date")),
      (if cheque { "Cheque" } else { "UTR" }, data.payment.reference),
      ..if cheque { (("Bank", data.payment.drawn-on),) } else { () },
    ),
  )),
)

#lead-figure(
  "Amount received",
  received,
  note: [By #(if cheque { "cheque" } else { data.payment.mode }) on #long-date(data.payment.date, field: "Payment date"), with thanks.],
)

= Settlement

#data-table(
  columns: (1fr, 22mm, 24mm, 20mm, 24mm, 20mm),
  header: ("Invoice", "Date", "Value (₹)", "TDS (₹)", "Received (₹)", "Balance (₹)"),
  align: (left, left, right, right, right, right),
  rows: data.invoices.map(i => (
    i.number,
    short-date(i.date),
    money(i.value),
    money(i.tds),
    money(i.received),
    money(balance(i)),
  )),
  footer: (
    text(weight: 600)[Total],
    [],
    text(weight: 600, money(total("value"))),
    text(weight: 600, money(tds)),
    text(weight: 600, money(received)),
    text(weight: 600, money(data.invoices.map(balance).sum(default: decimal(0)))),
  ),
)

// Notes beside the signature.
#block(breakable: false, above: 9mm, grid(
  columns: (1fr, 70mm),
  column-gutter: 12mm,
  notes({
    set par(spacing: 0.9em)
    if tds > 0 [
      Tax deducted at source of #inr(tds) is credited against the
      invoices above once it appears in our annual tax statement. Please
      send us the TDS certificate for the quarter.

    ]
    if cheque [
      This receipt is subject to the cheque being honoured.

    ]
    [This receipt confirms payment against the invoices above. It is not a
    tax invoice.]
  }),
  signature-block(name: data.signatory.name, title: data.signatory.title, date: data.date, seal: true),
))
