Templates
Helper Functions
The following helper functions are available in the template.ejs
file of invoice and proposal templates.
#t
Returns a translated string for key
.
t (key: string): string
#formatDate
Returns a formatted date string, respecting the language of the invoice/proposal.
formatDate (date: string): string
#formatMoneyValue
Returns a formatted money value, respecting the language of the invoice/proposal and the currency of the money value.
formatMoneyValue (value: MoneyValue): string
#formatQuantity
Returns a string
containing the quantity
of an item and its unit
label in the correct language of the invoice or proposal.
If the language of the invoice or proposal is not part of the unit's labels, it will fallback to show the unit in another language.
formatQuantity (quantity: number, unit: Unit | null): string
#Example
// item.unit is a unit for "hours":
formatQuantity(1, item.unit) // "1 Hour"
// item.unit is a unit for "days":
formatQuantity(2, item.unit) // "2 Days"
// item.unit is null
formatQuantity(2, item.unit) // "2"
#formatUnit
Returns a unit's plural or singular label in the correct language of the invoice or proposal. If the language of the invoice or proposal is not part of the unit's labels, it will fallback to show the unit in another language.
formatUnit
is used internally by formatQuantity
and can be used to show only the label of a unit.
formatUnit (quantity: number, unit: Unit | null): string
#Example
// item.unit is a unit for "hours":
formatUnit(1, item.unit) // "Hour"
// item.unit is a unit for "days":
formatUnit(2, item.unit) // "Days"
// item.unit is null
formatUnit(2, item.unit) // ""
#join
Joins together an array of strings by a given separator, skipping falsey values.
join (strings: Array, separator: string): string
#Example
join(['Hello', '', 'World', undefined], ' • ') // "Hello • World"
#joinAddress
Joins together the components of an address by a given separator, skipping falsey values.
This helper handles several known address formats, such as German, US and UK addresses.
It can take an Address
or a Party
as the first argument. If a Party
is given, it will include the party's name in the address. A Party
is a type that can be either an InvoiceSeller, InvoiceBuyer, ProposalSeller or ProposalBuyer.
joinAddress = (addressOrParty: Address | Party, separator: string): string
#Example
// Specifying an address
joinAddress(invoice.seller.address, ' • ') // "Some street 76 • 99999 Berlin • Germany"
// Specifying an invoice party
joinAddress(invoice.seller, ' • ') // "Company Name • Some street 76 • 99999 Berlin • Germany"
#joinLegalInfo
Joins together a party's name, address and tax information by a given separator, skipping falsey values.
If the party is a seller and from Germany, it can include the Tax ID (Steuernummer).
joinLegalInfo = (party: Party, separator: string): string
#Example
joinLegalInfo(invoice.seller, ' • ') // "John Doe • Some street 76 • 99999 Berlin • Germany • VAT ID: DE123456789"
#joinTaxInfo
Joins together a party's tax information by a given separator, skipping falsey values.
If the party is a seller and from Germany, it can include the Tax ID (Steuernummer).
joinTaxInfo = (party: Party, separator: string): string
#Example
joinLegalInfo(invoice.seller, ' • ') // "VAT ID: DE123456789"
#getTaxFields
Returns an array of tax fields for a given party.
If the party is a seller and from Germany, it can include the Tax ID (Steuernummer).
The fields are objects with the following properties:
- key: The key of the field, e.g. "vatId"
- label: The label of the field, e.g. "VAT ID"
- value: The value of the field, e.g. "DE123456789".
getTaxFields = (party: Party): Field[]
#Example
getTaxFields(invoice.seller) // [{ key: 'vatId', label: 'VAT ID', value: 'DE123456789' }]
#countryCode
Returns the name of the country, respecting the language of the invoice/proposal.
countryCode (code: string): string
#field
Returns the value of the given custom field.
field (fieldName: string): any
#getSummaryItems
Returns an array of summary items, consisting of discounts, taxes and totals.
getSummaryItems (): SummaryItem[]
A summary item is an object with the following properties:
SummaryItem {
kind: 'subtotal' | 'discount' | 'afterDiscount' | 'taxSubtotal' | 'total'
title: string
value: string
}
#Example Usage
<table class="summary-table">
<tbody>
<% for (const summaryItem of getSummaryItems()) { %>
<tr>
<th><%= summaryItem.title %></th>
<td><span class="number"><%= summaryItem.value %></span></td>
</tr>
<% } %>
</tbody>
</table>
#clsx
The popular clsx function for generating class names.