// Credit note: reduces a tax invoice we issued, for services not
// delivered, a discount agreed after invoicing, or an overcharge.
//
// It carries what rule 53 of the CGST Rules asks of a credit note: the
// words "Credit note"; our name, address and GSTIN; a serial number of at
// most 16 characters, unique for the financial year; the date; the
// recipient's name, address and GSTIN (and their state and its code); the
// number and date of the invoice it reduces; the taxable value, the tax
// rate and the tax credited; and an authorised signature. The GST rates
// and the place of supply must be the original invoice's.
//
// For the client's CA to confirm (not verified here):
//   - Time limit (section 34 of the CGST Act): issued no later than 30
//     November after the end of the financial year of the invoice, or the
//     date of the annual return if that is earlier.
//   - Our GST is reduced only if a registered client reverses the input
//     tax credit it took on the credited tax, as the note asks it to.
//
// 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 note 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 credit note register (none prints a blank).
  seq: 2,
  // Date of issue, YYYY-MM-DD.
  date: "2026-09-12",
  // Why, in a few words, set light after the title.
  subject: "for images not delivered",
  // Our GSTIN, from the GST registration certificate (none prints a
  // blank). Never type one from memory.
  gstin: none,
  client: (
    name: "Sahyadri Foods Private Limited",
    attn: "Mr Rohan Kulkarni, Head of Marketing",
    address: ("Office 604, Pentagon P2, Magarpatta City,", "Hadapsar, Pune, Maharashtra 411013"),
    gstin: none,
    // The place of supply on the original invoice.
    state: "27",
  ),
  // The invoice this note reduces: its number and date exactly as in the
  // register, and its figures as issued (before any earlier credit note).
  invoice: (
    number: "INV/2026-27/0014",
    date: "2026-09-01",
    taxable: 133000,
    tax: 23940,
    total: 156940,
  ),
  // The reason, in plain words.
  reason: [Invoice line 03 billed forty product images. You asked us on
    4 September 2026 to leave ten of them out, so thirty were delivered.
    This note credits the ten images and the GST charged on them.],
  // What is credited, before GST, at the original invoice's rates.
  items: (
    (
      description: "Product photography, images not delivered",
      detail: "Invoice line 03: 40 images billed, 30 delivered",
      sac: "998382",
      qty: 10,
      unit: "images",
      rate: 950,
    ),
  ),
  // "adjust": set against our next invoice. "refund": paid back to the
  // client's bank account.
  settlement: "adjust",
  // Who signs: a finance document, so the Managing Director.
  signatory: (name: "Paras Deshpande", title: "Managing Director & CEO"),
)

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

#let k = document-kinds.at("credit-note")
#let place = data.client.state
#let t = compute-totals(data.items, place-of-supply: place)
#let money(x) = if x == none { blank("Amount", width: 16mm) } else { inr(x, symbol: false) }
#let minus(x) = if x == none { blank("Amount", width: 16mm) } else { "−" + inr(x, symbol: false) }
#let less(a, b) = if a == none or b == none { none } else { decimal(str(a)) - b }

#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,
  gstin: data.gstin,
  letterhead: k.letterhead,
  watermark: if data.draft { "draft" } else { none },
  meta: (
    ("Place of supply", place-of-supply(place)),
    // Our invoices don't use reverse charge (see tax-invoice.typ). A note
    // against one that did needs the CA's advice, not this template.
    ("Reverse charge", "No"),
    // Fifth, so it opens the second row, in the wide column references fit.
    ("Against invoice", data.invoice.number),
    ("Invoice date", long-date(data.invoice.date, field: "Invoice date")),
  ),
)

#parties(company-party("Supplier", require-gstin: true), party("Issued to", data.client))

= Reason

#data.reason

// Kept whole with its totals while the list is short: without the
// wrapper, kit 2.1.0's line-items() can push its last rows and totals to
// the next page when they would fit. A long list breaks between rows.
#block(breakable: data.items.len() > 8, line-items(data.items, place-of-supply: place, total-label: "Total credit"))

= Effect on the invoice

#block(breakable: false, data-table(
  columns: (1fr, 30mm, 26mm, 30mm),
  header: ([], "Taxable (₹)", "GST (₹)", "Total (₹)"),
  align: (left, right, right, right),
  rows: (
    ([Invoice #data.invoice.number], money(data.invoice.taxable), money(data.invoice.tax), money(data.invoice.total)),
    ([Less this credit note], minus(t.taxable), minus(t.tax), minus(t.total)),
  ),
  footer: (
    text(weight: 600)[Revised invoice value],
    text(weight: 600, money(less(data.invoice.taxable, t.taxable))),
    text(weight: 600, money(less(data.invoice.tax, t.tax))),
    text(weight: 600, money(less(data.invoice.total, t.total))),
  ),
))

#callout(title: if data.settlement == "refund" { "Refund" } else { "Adjustment" })[
  #if data.settlement == "refund" [
    We will refund #inr(t.total) to your bank account within 14 business
    days of this note.
  ] else [
    We will deduct #inr(t.total) from our next invoice to you.
  ]
  As a registered recipient, please reduce the input tax credit you
  claimed on the invoice by the GST on this note, #inr(t.tax), in your
  return for the month you receive it.
]

// Closing note beside the signature, as on the tax invoice.
#block(breakable: false, above: 9mm, grid(
  columns: (1fr, 70mm),
  column-gutter: 12mm,
  notes[This is a computer-generated credit note. It is valid when signed
    by an authorised signatory, by hand or digitally. Subject to the
    jurisdiction of the courts at Navi Mumbai, Maharashtra.],
  signature-block(name: data.signatory.name, title: data.signatory.title, date: data.date, seal: true),
))
