sales : Product invoices

trade document

A voucher that has a partner as recipient and one or several "rows" or "items", each of which usually refers to a product.

Snippets in this document are tested on the lino_book.projects.pierre demo project.

>>> from lino import startup
>>> startup('lino_book.projects.pierre.settings.doctests')
>>> from lino.api.doctest import *
>>> ses = rt.login('robin')

The plugin

Lino implements product invoices in the lino_xl.lib.sales plugin. The internal codename "sales" is for historical reasons, you might generate product invoices for other trade types as well.

The plugin needs and automatically installs the lino_xl.lib.products plugin.

It also needs and installs lino_xl.lib.vat (and not lino_xl.lib.vatless). Which means that if you want product invoices, you cannot not also install the VAT framework. If the site operator is not subject to VAT, you might add lino_xl.lib.bevats which hides most of the VAT functionality.

>>> dd.plugins.sales.needs_plugins
['lino.modlib.memo', 'lino_xl.lib.products', 'lino_xl.lib.vat']

This plugin may be combined with the lino_xl.lib.invoicing plugin which adds automatic generation of such product invoices.

Product invoices

A product invoice is a legal document which describes that something (the invoice items) has been sold to a given business partner (called the customer). The partner can be either a private person or an organization.

class VatProductInvoice

The Django model representing a product invoice.

Inherits from lino_xl.lib.ledger.Voucher.

Virtual fields:


The balance of previous payments or debts. On a printed invoice, this amount should be mentioned and added to the invoice's amount in order to get the total amount to pay.


The balance of all movements matching this invoice.


get_print_items(self, ar):

For usage in an appy template:

do text
from table(obj.get_print_items(ar))
class InvoiceItem

The Django model representing an item of a product invoice.

class InvoiceDetail

The Lino layout representing the detail view of a product invoice.

class Invoices
class InvoicesByJournal
Shows all invoices of a given journal (whose `voucher_type` must be
class DueInvoices

Shows all due product invoices.

class ProductDocItem

Mixin for voucher items which potentially refer to a product.


The product that is being sold or purchased.


A multi-line rich text to be printed in the resulting printable document.

class ItemsByInvoicePrint

The table used to render items in a printable document.


TODO: write more about it.

class ItemsByInvoicePrintNoQtyColumn

Alternative column layout to be used when printing an invoice.

class SalesDocument

Common base class for lino_xl.lib.orders.Order and VatProductInvoice.

Inherits from lino_xl.lib.vat.mixins.VatDocument and ino_xl.lib.excerpts.mixinsCertifiable.

Subclasses must either add themselves a date field (as does Order) or inherit it from Voucher (as does VatProductInvoice).

Note that this class sets edit_totals to False.

print_items_table = None

The table (column layout) to use in the printed document.

ItemsByInvoicePrint ItemsByInvoicePrintNoQtyColumn

Paper types

class PaperType

Describes a paper type (document template) to be used when printing an invoice.

A sample use case is to differentiate between invoices to get printed either on a company letterpaper for expedition via paper mail or into an email-friendly pdf file.

Inherits from lino.utils.mldbc.mixins.BabelNamed.

templates_group = 'sales/VatProductInvoice'

A class attribute.


Trade types

The plugin updates your lino_xl.lib.ledger.TradeTypes.sales, causing two additional database fields to be injected to lino_xl.lib.products.Product.

The first injected field is the sales price of a product:

>>> translation.activate('en')
>>> print(ledger.TradeTypes.sales.price_field_name)
>>> print(ledger.TradeTypes.sales.price_field_label)
Sales price
>>> products.Product._meta.get_field('sales_price')
<lino.core.fields.PriceField: sales_price>

The other injected field is the sales base account of a product:

>>> print(ledger.TradeTypes.sales.base_account_field_name)
>>> print(ledger.TradeTypes.sales.base_account_field_label)
Sales account
>>> products.Product._meta.get_field('sales_account')
<django.db.models.fields.related.ForeignKey: sales_account>

The sales journal

The pierre demo site has no VAT declarations, no purchase journals, no financial journals, just a single sales journal.

>>>'ledger.Journals', column_names="ref name trade_type")
=========== ================ ================== ============
 Reference   Designation      Designation (en)   Trade type
----------- ---------------- ------------------ ------------
 SLS         Factures vente   Sales invoices     Sales
=========== ================ ================== ============

Invoices are sorted by number and year. The entry date should normally never "go back". Lino supports exceptional situations, e.g. starting to issue invoices at a given number and entering a series of sales invoices from a legacy system afterwards.

>>> jnl = rt.models.ledger.Journal.get_by_ref("SLS")
>>>'sales.InvoicesByJournal', jnl)
===================== ============ ============ =================================== =============== ============== ================
 No.                   Entry date   Due date     Partner                             Total to pay    Subject line   Workflow
--------------------- ------------ ------------ ----------------------------------- --------------- -------------- ----------------
 15/2017               12/03/2017   18/03/2017   van Veen Vincent                    1 110,16                       **Registered**
 14/2017               11/03/2017   17/03/2017   van Veen Vincent                    535,00                         **Registered**
 4/2017                09/02/2017   08/02/2017   Radermacher Edgard                  21,00                          **Registered**
 3/2017                08/02/2017   09/03/2017   Radermacher Daniela                 719,60                         **Registered**
 2/2017                07/02/2017   28/02/2017   Radermacher Christian               645,00                         **Registered**
 1/2017                07/01/2017   06/04/2017   Radermacher Berta                   31,92                          **Registered**
 57/2016               10/12/2016   07/02/2017   Radermacher Alfons                  3 149,71                       **Registered**
 56/2016               09/12/2016   07/01/2017   Emonts-Gast Erna                    1 613,92                       **Registered**
 55/2016               08/12/2016   17/12/2016   Emontspool Erwin                    448,50                         **Registered**
 5/2016                11/01/2016   10/03/2016   Garage Mergelsberg                  535,00                         **Registered**
 4/2016                10/01/2016   08/02/2016   Bäckerei Schmitz                    280,00                         **Registered**
 3/2016                09/01/2016   18/01/2016   Bäckerei Mießen                     679,81                         **Registered**
 2/2016                08/01/2016   14/01/2016   Bäckerei Ausdemwald                 2 039,82                       **Registered**
 1/2016                07/01/2016   06/01/2016   Rumma & Ko OÜ                       2 999,85                       **Registered**
 **Total (72 rows)**                                                                 **82 597,39**
===================== ============ ============ =================================== =============== ============== ================
>>> mt = contenttypes.ContentType.objects.get_for_model(sales.VatProductInvoice).id
>>> obj = sales.VatProductInvoice.objects.get(journal__ref="SLS", number=20)
>>> url = '/api/sales/InvoicesByJournal/{0}'.format(
>>> url += '?mt={0}&mk={1}&an=detail&fmt=json'.format(mt,
>>> test_client.force_login(rt.login('robin').user)
>>> res = test_client.get(url, REMOTE_USER='robin')
>>> # res.content
>>> r = check_json_result(res, "navinfo data disable_delete id param_values title")
>>> print(r['title']) 
<a ...>Sales invoices (SLS)</a> » SLS 20/2016

IllegalText: The <text:section> element does not allow text

The following reproduces a situation which caused above error until 2015-11-11.

TODO: it is currently disabled for different reasons: leaves dangling temporary directories, does not reproduce the problem (probably because we must clear the cache).

>> obj = rt.models.sales.VatProductInvoice.objects.all()[0] >> obj VatProductInvoice #1 ('SLS#1') >> from lino.modlib.appypod.appy_renderer import AppyRenderer >> tplfile = rt.find_config_file('sales/VatProductInvoice/Default.odt') >> context = dict() >> outfile = "tmp.odt" >> renderer = AppyRenderer(ses, tplfile, context, outfile) >> ar = rt.models.sales.ItemsByInvoicePrint.request(obj) >> print(renderer.insert_table(ar)) #doctest: +ELLIPSIS <table:table ...</table:table-rows></table:table>

>> item = obj.items.all()[0] >> item.description = """ ... <p>intro:</p><ol><li>first</li><li>second</li></ol> ... <p></p> ... """ >> >> print(renderer.insert_table(ar)) #doctest: +ELLIPSIS Traceback (most recent call last): ... IllegalText: The <text:section> element does not allow text

The language of an invoice

The language of an invoice not necessary that of the user who enters the invoice. It is either the partner's language or (if this is empty) the Site's get_default_language.