-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtemplate.typ
357 lines (311 loc) · 9.12 KB
/
template.typ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#import "@preview/tablex:0.0.4": tablex, cellx, rowspanx
#let default-currency = state("currency-state", "$")
#let default-hundreds-separator = state("separator-state", ",")
#let default-decimal = state("decimal-state", ".")
#let date-to-str(date, format: "[day padding:none] [month repr:long] [year]") = {
if type(date) == "string" {
let pieces = date.split("-").map(int)
date = datetime(year: pieces.at(0), month: pieces.at(1), day: pieces.at(2))
}
let to-format = if date == none { datetime.today() } else { date }
if type(to-format) == "datetime" {
to-format.display(format)
} else {
to-format
}
}
#let check-dict-keys(dict, ..keys) = {
assert(type(dict) == "dictionary", message: "dict must be a dictionary")
for key in keys.pos() {
assert(key in dict, message: "dict must contain key: " + repr(key))
}
}
#let format-company-info(info, title: none) = [
#check-dict-keys(info, "company-name", "address", "name", "email", "phone")
#title\
#info.company-name\
#info.address
Attn: #info.name\
#link("mailto:" + info.email)\
#info.phone
]
#let format-doc-info(info) = [
#check-dict-keys(info, "title", "id", "date")
#text(size: 2.75em, weight: "extrabold")[#info.title]
#v(-2em)
ID: #info.id\
Date: #date-to-str(info.date)
#if "valid-through" in info [
#linebreak()
Valid through #date-to-str(info.valid-through)
]
]
#let format-frontmatter(preparer-info, client-info, doc-info) = [
#grid(columns: 2, column-gutter: 1fr)[
#format-doc-info(doc-info)
][
#set align(bottom)
#doc-info.at("logo", default: none)
]
#line(length: 100%)
#v(1em)
#grid(columns: 2, column-gutter: 1fr)[
#format-company-info(client-info, title: [*TO:*])
][
#format-company-info(preparer-info, title: [*FROM:*])
]
#v(1em)
]
#let format-payment-info(payment-info) = {
check-dict-keys(payment-info, "payment-window", "account-details")
[
#v(1em)
*Due within #payment-info.payment-window days of receipt.*
#payment-info.account-details
]
}
#let price-formatter(number, currency: auto, separator: auto, decimal: auto, digits: 2) ={
// Adds commas after each 3 digits to make
// pricing more readable
if currency == auto {
currency = default-currency.display()
}
if separator == auto {
separator = default-hundreds-separator.display()
}
if decimal == auto {
decimal = default-decimal.display()
}
let integer-portion = str(calc.abs(calc.trunc(number)))
let num-length = integer-portion.len()
let num-with-commas = ""
for ii in range(num-length) {
if calc.rem(ii, 3) == 0 and ii > 0 {
num-with-commas = separator + num-with-commas
}
num-with-commas = integer-portion.at(-ii - 1) + num-with-commas
}
// Another "round" is needed to offset float imprecision
let fraction = calc.round(calc.fract(number), digits: digits + 1)
let fraction-int = calc.round(fraction * calc.pow(10, digits))
if fraction-int == 0 {
fraction-int = ""
} else {
fraction-int = decimal + str(fraction-int)
}
let formatted = currency + num-with-commas + fraction-int
if number < 0 {
formatted = "(" + formatted + ")"
}
formatted
}
#let c(body, ..args) = cellx(inset: 1.25em, ..args, text(weight: "bold", body))
#let total-bill(amount) = {
grid(columns: (auto, auto))[
][
#tablex(
columns: (auto, auto),
align: (auto, right),
auto-vlines: false,
c[TOTAL],
c[#price-formatter(amount)],
)
]
}
#let _format-charge-value(value, info, row-total, row-number) = {
// TODO: Account for other helpful types like datetime
if value == none {
return (value, row-total, false)
}
let typ = info.at("type")
let did-multiply = false
if typ not in ("string", "index") {
let multiplier = value
if info.at("negative", default: false) {
multiplier *= -1
}
if typ == "percent" {
multiplier = 1 + multiplier/100
}
if row-total == none {
row-total = 1
}
row-total *= multiplier
did-multiply = true
}
let out-value = value
if typ == "currency" {
out-value = price-formatter(value)
} else if typ == "percent" {
out-value = value
} else if typ == "string" {
out-value = eval(value, mode: "markup")
} else if typ == "index" and value == "" {
out-value = row-number
}
if "suffix" in info {
out-value = [#out-value#info.at("suffix")]
}
(out-value, row-total, did-multiply)
}
#let _format-charge-columns(charge-info) = {
let get-eval(dict, key, default) = {
let value = dict.at(key, default: default)
if type(value) == "string" {
eval(value)
}
else {
value
}
}
let (names, aligns, widths) = ((), (), ())
for (key, info) in charge-info.pairs() {
key = upper(key.at(0)) + key.slice(1)
names.push(c(key))
let default-align = if info.at("type") == "string" { left } else { right }
aligns.push(get-eval(info, "align", default-align))
widths.push(get-eval(info, "width", auto))
}
// Keys correspodn to tablex specs other than "names" which is positional
(names: names, align: aligns, columns: widths)
}
#let bill-table(..items, charge-info: auto) = {
if charge-info == auto {
charge-info = (:)
}
if items.pos().len() == 0 {
return (table: none, amount: 0)
}
let out = ()
let total-amount = 0
let columns = ()
// A separate "Total" column is only needed if there are >1 multipliers
let has-multiplier = false
let found-infos = (:)
// Initial scan finds all possible fields, and whether a "total"
// field is needed
for item in items.pos() {
let mult-count = 0
for (key, value) in item.pairs() {
if key not in charge-info {
let fallback = (type: type(value))
charge-info.insert(key, fallback)
}
found-infos.insert(key, charge-info.at(key))
let (_, _, did-multiply) = _format-charge-value(value, charge-info.at(key), 0, 0)
if did-multiply {
mult-count += 1
}
has-multiplier = has-multiplier or mult-count > 1
}
}
// Now that all needed keys are guaranteed to exist, we can start to format output values
for (ii, item) in items.pos().enumerate() {
let row-number = ii + 1
let row-total = none
for (key, info) in found-infos.pairs() {
let default-value = info.at("default", default: none)
let value = item.at(key, default: default-value)
let (display-value, new-row-total, _) = _format-charge-value(
value, info, row-total, row-number
)
out.push(display-value)
row-total = new-row-total
}
if row-total == none {
row-total = 0
}
if has-multiplier {
out.push(price-formatter(row-total))
}
total-amount += row-total
}
if has-multiplier {
found-infos.insert("total", (type: "currency"))
}
let col-spec = _format-charge-columns(found-infos)
let names = col-spec.remove("names")
let tbl = tablex(
..col-spec,
auto-vlines: false,
inset: 1em,
..names,
..out,
)
(table: tbl, amount: total-amount)
}
#let invoice(
body,
preparer-info: none,
client-info: none,
payment-info: none,
doc-info: none,
apply-default-style: true,
) = {
set text(font: "Arial", hyphenate: false) if apply-default-style
set page(paper: "us-letter", margin: 0.8in, number-align: top + right) if apply-default-style
// conditional "set" rules are tricky due to scoping
show link: content => {
if apply-default-style {
set text(fill: blue.darken(20%))
underline(content)
} else {
content
}
}
let frontmatter = format-frontmatter(preparer-info, client-info, doc-info)
frontmatter
body
if payment-info != none {
format-payment-info(payment-info)
}
}
#let create-bill-tables(headings-and-charges, charge-info: auto, price-locale: (:)) = {
if "currency" in price-locale {
default-currency.update(price-locale.at("currency"))
}
if "separator" in price-locale {
default-hundreds-separator.update(price-locale.at("separator"))
}
if "decimal" in price-locale {
default-decimal.update(price-locale.at("decimal"))
}
let needs-heading = headings-and-charges.len() > 1
let running-total = 0
for (key, charge-list) in headings-and-charges.pairs() {
if needs-heading {
[= #key]
}
let bill = bill-table(..charge-list, charge-info: charge-info)
bill.table
running-total += bill.amount
}
h(1fr)
set align(right) if not needs-heading
total-bill(running-total)
}
#let remove-or-default(dict, key, default) = {
// Self assignment allows mutability
let dict = dict
if key in dict {
let value = dict.remove(key)
(dict, value)
} else {
(dict, default)
}
}
#let invoice-from-metadata(metadata-dict, pre-table-body: [], ..extra-invoice-args) = {
let meta = metadata-dict
let charges = (:)
for key in meta.keys() {
if lower(key).ends-with("charges") {
let opts = meta.remove(key)
charges.insert(key, opts)
}
}
let (meta, info) = remove-or-default(meta, "charge-info", auto)
let (meta, price-locale) = remove-or-default(meta, "locale", ())
show: invoice.with(..meta, ..extra-invoice-args)
pre-table-body
create-bill-tables(charges, charge-info: info, price-locale: price-locale)
}