Creating an invoice with percentage and fixed discounts in Cakedesk.

March 11, 2025

Using Cakedesk's Discounts in Custom Templates

In Cakedesk version 1.17.0, we're introducing a new feature: discounts.

If you created a custom invoice/proposal template before this version, you will need to update your template in order to display discounts correctly.

You want to make sure that your custom template shows discounts (if the invoice/proposal has any) at the end of the items table, before the tax subtotals:

Showing discounts in an invoice

To do this is simple.

#❌ Before: Manually Rendering Subtotals, Taxes and Totals

Before, your code might have looked something like this:

<table>
	<tbody>
		<% if (invoice.hasVat) { %>
			<tr>
				<th><%= t('subtotal') %></th>
				<td><%= formatMoneyValue(invoice.subtotal) %></td>
			</tr>
			<% for (const taxSubtotal of invoice.taxSubtotals) { %>
				<tr>
					<th><%= t('vat') %> (<%= taxSubtotal.vatPercentage %>%)</th>
					<td><%= formatMoneyValue(taxSubtotal.subtotal) %></td>
				</tr>
			<% } %>
			<tr>
				<th><%= t('invoiceTotal') %></th>
				<td><%= formatMoneyValue(invoice.total) %></td>
			</tr>
		<% } else { %>
			<tr>
				<th><%= t('invoiceTotal') %></th>
				<td><%= formatMoneyValue(invoice.total) %></td>
			</tr>
		<% } %>
	</tbody>
</table>

#✅ After: Using 'getSummaryItems' to Render Subtotals, Taxes and Totals

Thanks to the new helper function getSummaryItems, we can replace the code above with the following (much simpler) code:

<table>
	<tbody>
		<% for (const summaryItem of getSummaryItems()) { %>
			<tr>
				<th><%= summaryItem.title %></th>
				<td><%= summaryItem.value %></td>
			</tr>
		<% } %>
	</tbody>
</table>

This will automatically show discounts in your template if they are present.

You can read up about the SummaryItems in the docs. Basically, they will include the subtotal, discounts (if there are any), the subtotal after discounts (if applicable), tax subtotals (if there are any), and the total (after discounts and taxes).

And that's pretty much all there is to do! Don't forget to apply this update for both your invoice and proposal templates.

#Further Reading

The following things are worth knowing but probably not necessary if you choose to use the getSummaryItems function which is highly recommended.

  • Invoice and Proposal objects now have a discounts: Discount[] property
  • Invoice and Proposal objects now have a subtotalAfterDiscounts property
  • Invoice and Proposal objects now have a totalDiscounts property
  • Invoice.taxSubtotals and Proposal.taxSubtotals are now deprecated. Use {Invoice,Proposal}.taxSubtotalsV2 instead
  • TaxSubtotal objects include a few new fields: baseAmount, baseAmountAfterDiscounts and discountAmount
  • Docs: Discount