Invoices and bills: the invoice line sync and the tax computation
An invoice is an account.move whose move_type is out_invoice (customer invoice), out_refund (customer credit
note), in_invoice (vendor bill) or in_refund (vendor credit note). Code: erp_addon_account::invoice and
erp_addon_account::tax::compute.
Fields
Section titled “Fields”| Model | Field | Meaning |
|---|---|---|
account.move | invoice_date | invoice or bill date |
account.move | invoice_date_due | due date: the last instalment of the payment terms, else the date set by hand, else the invoice date (see Payment terms) |
account.move | invoice_line_ids | the line_ids whose display_type is product, section, subsection or note (a one2many with a domain on the same inverse) |
account.move | amount_untaxed, amount_tax, amount_total | totals in the document currency, positive on an ordinary invoice |
account.move | amount_residual, payment_state | the total and not_paid until payments are available |
account.move.line | product_id, product_uom_id, quantity, price_unit, discount, tax_ids | what the user edits on a product line |
account.move.line | tax_repartition_line_id, tax_line_id | on a tax line: the repartition line it was made from, and its tax |
account.move.line | price_subtotal, price_total | the line amount without and with taxes |
display_type gained tax and payment_term (the receivable or payable line).
The totals, the amount due and the payment status (moves::SYNC_OWNED_FIELDS) are written by the line sync only. A
caller value for one of them is refused with a UserError that names the field, on create and on write, on a draft
as well as on a posted document, and so is the context key default_<field> for one of them: change the lines, the
totals follow. A write on the item model itself (account.move.line) does not run the sync; posting runs it once
more for every draft invoice before its checks, so a posted invoice always carries the figures of its lines.
The invoice line sync
Section titled “The invoice line sync”invoice::sync_lines(tx, env, move_ids) runs after every create of an entry and after every write of a draft invoice
that changes its lines, partner, currency, type, journal, company or dates. For each draft invoice it:
- computes the taxes of each product line (
tax::compute::compute) and writes the line’samount_currency(subtotal timesinvoice::direction_sign: credit on a customer invoice, debit on a vendor bill, reversed on credit notes),price_subtotalandprice_total; - splits each tax amount over the tax’s
taxrepartition lines of the document kind (invoice or refund) and keeps one tax line per repartition line and account (the repartition line’s account, else the product line’s account); a zero amount makes no line; - keeps the receivable (customer documents) or payable (vendor documents) lines that balance the entry in both currencies: one line, due at the due date, or one per instalment of the payment terms (Payment terms). Its account is chosen as described under The receivable or payable account;
- writes the header totals, the amount due and the payment state when one of them changed.
New product lines without an account take the journal’s default account; without one the save is refused (product
accounts are not available yet). When creating a draft without a journal, the addon searches sale or purchase journals for the company and its parents in journal order. If the draft supplies a foreign currency_id (directly or through default_currency_id), a journal in that currency takes priority. Without a match it takes the first eligible journal and keeps the requested document currency.
A currency change moves every product, tax and receivable or payable line to the new currency: the amount in
currency is written with it, so the balance is converted at the new currency’s rate on the accounting date (a
currency without a rate is refused naming it).
The balances of a draft invoice in another currency than its company’s are converted at the entry’s accounting date.
When that date changes on the draft, the write runs invoice::sync_lines_redated(tx, env, move_ids), which converts
the product, tax and receivable or payable balances again at the new date; the amounts in currency do not change.
The re-dating at posting (an accounting date moved by a lock date) keeps the balances as they are.
What the sync costs: reads are one query per field for all the invoices together; new lines are created in one call and stale lines deleted in one call; each changed line is written with its own write (a bulk-write API is a follow-up) and the header once per invoice whose totals changed. Lines and headers that already match are not written. A cell of an unexpected kind or a short column in one of these reads is refused naming the field; it is never read as an empty value.
The receivable or payable account
Section titled “The receivable or payable account”The receivable line of a customer document takes an account of type asset_receivable, the payable line of a vendor
document one of type liability_payable:
- a line that already has an account of the right type keeps it;
- otherwise the candidates are the accounts of that type shared with the invoice’s company or one of its parent companies (a branch uses the accounts of its parent);
- no candidate: the save is refused naming what to create;
- one candidate: it is used;
- several candidates: the one whose code is entered in the company’s Accounting settings, block “Invoices”, is
used. The settings are
account.invoice_receivable_account_code(“Invoices: receivable account of customer invoices (account code)”) andaccount.invoice_payable_account_code(“Invoices: payable account of vendor bills (account code)”), one value per company, empty by default, written by accounting managers. With an empty setting or a code that none of the candidates has, the save is refused listing the candidate codes and naming the setting. An account is never picked by its position among several.
The tax computation
Section titled “The tax computation”tax::compute::compute(&BaseLine) is pure (no database). It takes the unit price, quantity, discount, the line’s
TaxDefs and the document currency increment, and returns the subtotal, the total and one TaxAmount (base and amount)
per evaluated tax. Supported: percent, fixed and division taxes, groups of taxes (their children are evaluated at the
group’s place), price-included taxes, taxes that affect the base of later ones, ordering by sequence then id, and
rounding per line (half away from zero, ADR 0046). Python code taxes and reverse charge are refused with a message that they are not available yet. Groups are supported by the module but the database has no child taxes yet, so a group tax on a line
is refused with a message saying so. The company has no “round globally” choice yet.
Posting checks
Section titled “Posting checks”The posting service calls invoice::post::check_postable(tx, env, move_ids) and
invoice::post::before_post(tx, env, move_ids); both do nothing for plain journal entries.
check_postablerefuses, in one error listing every problem: an invoice without customer or vendor, without a product line, with a negative total, or a vendor document without its bill date.before_postgives customer documents without an invoice date today’s date (in the user’s timezone); vendor documents keep the date printed by the vendor.
Not yet
Section titled “Not yet”Receipts, early payment discount and cash rounding of payment terms, product accounts, fiscal positions, cash rounding, payments, the overdue search filter (the client cannot compare with today’s date in a domain yet), unit conversion of the product price.