Skip to content

Commit

Permalink
[IMP/REF] accounting-pocalypse yeaaahh
Browse files Browse the repository at this point in the history
This commit merges the following models
 * account.invoice and account.move
 * account.invoice.line and account.move.line
 * account.voucher and account.move
 * account.voucher.line and account.move.line

It was the opportunity for a big cleanup of the code, so it also restructures the whole account module, its different models/fields, the tests etc. for a better world and a better code readability.

==== Rationale ====
The rationale of this huge change is that we want journal entries / invoices to be easily edited, and changes reflected in the other model. It's a HUGE feature and very strategic for the fiduciary companies. For example, changing the account of a journal entry needs to be automatically reflected on the related invoice.

The same reasoning applies to sale/purchase vouchers.

==== Changes made in features =====
When creating an invoice, you are now creating a journal entry directly.
--> The object account.invoice no longer exists.
In the same fashion when creating an invoice line, you're now adding journal items directly in the journal entry representing the invoice. If this invoice line has some tax, it may create additional journal items as well.
--> The models account.invoice.line & account.invoice.tax no longer exist

Identically, when creating a sale/purchase receipt with its lines, you are now creating a journal entry directly and there's no more usability difference between encoding a receipt or an invoice.
--> The object account.voucher no longer exists.
--> The object account.voucher.line no longer exists.
--> The whole account_voucher module no longer exists.

Positive side-effects coming from these changes are
* draft invoices/bills/sale or purchase receipts now create a draft accounting entry. Validate these objects now simply post its journal entry. That means that draft invoices/bills/sale or purchase receipt can straightforwardly be included in reporting or budgets.
* opening a journal entry in form view will now always open the correct view: if it's a sale/purchase journal entry we will have a customer invoice/vendor bill view or a sale/purchase receipt view, whatever the menu we're coming from.
* code & business logic simplification. It is also condensed in a single place instead of being partially duplicated on invoices, vouchers and journal entries.

There should be no feature loss, except the one allowing to group multiple journal items together based on the same product during the invoice validation.

==== Changes made in models =====
* account.invoice: model removed. Instead, now use account.move with following mapping

field (account.invoice) 		field (account.move)
-----------------------			--------------------
name 					invoice_payment_ref
number 					name
reference 				ref
comment 				narration
user_id 				invoice_user_id
amount_					total_company_signed amount_total_signed
residual 				amount_residual
state 					state + invoice_payment_state 		/!\ selection changed
date_invoice 				invoice_date
date_due 				invoice_date_due
sent 					invoice_sent
origin 					invoice_origin
payment_term_id 			invoice_payment_term_id
partner_bank_id 			invoice_partner_bank_id
incoterm_id 				invoice_incoterm_id
vendor_bill_id 				invoice_vendor_bill_id
source_email 				invoice_source_email
vendor_display_name 			invoice_vendor_display_name
invoice_icon 				invoice_vendor_icon
cash_rounding_id 			invoice_cash_rounding_id
sequence_number_next 			invoice_sequence_number_next
sequence_number_next_prefix 		invoice_sequence_number_next_prefix

'invoices' subset of account.move can be accessed by using the selection field 'type' or one of the many helpers like is_invoice()

* account.move: now has a valid state 'cancel' that has to be excluded from all business logic
* account.move: field 'amount' renamed into 'amount_total'
* account.move: field 'reverse_entry_id' renamed into 'reversed_entry_id'
* account.move.line: now has a field 'display_type' that has to be excluded from all business logic, in order to support invoice layouting
* account.invoice.line: model removed. Instead, now use account.move.line with following mapping

field (account.invoice.line) 		field (account.move.line)
----------------------------		-------------------------
invoice_id 				move_id
uom_id 					product_uom_id
invoice_line_tax_ids 			tax_ids
account_analytic_id 			analytic_account_id

'invoice lines' subset of all account.move.line from a journal entry can be accessed by using the boolean field 'exclude_from_invoice_tab'

* account.invoice.tax: model removed. Instead, now use account.move.line with following mapping

field (account.invoice.tax) 		field (account.move.line)
---------------------------		-------------------------
invoice_id 				move_id
account_analytic_id 			analytic_account_id
amount 					price_unit
base 					tax_base_amount

'tax lines' subset of all account.move.line from a journal entry can be accessed by using the relational field 'tax_line_id'

* account.invoice.confirm: model removed. Instead, now use the 'post()' function of account.move
* account.invoice.refund: model removed. Instead, now use account.move.reversal to reverse the entries with the same options as we had for invoices
* account.voucher: model removed. Instead, now use account.move of type in ['out_receipt', 'in_receipt]
* account.voucher.line: model removed. Instead, now use account.move.line

==== Changes made in functions ====
* on account.move, method _run_post_draft_to_post() renamed into _autopost_draft_entries()
* on account.move, method action_account_invoice_payment() renamed into action_invoice_register_payment()
* on account.move, method action_invoice_reconcile_to_check() renamed into action_open_matching_suspense_moves()
* on account.move, method _get_domain_edition_mode_available() renamed into _get_domain_matching_supsense_moves()
* on account.move, method _get_intrastat_country_id() renamed into _get_invoice_intrastat_country_id()
* on account.move.line, method _get_domain_for_edition_mode() renamed into _get_suspense_moves_domain()
* in account.bank.statement, contextual key 'edition_mode' renamed into 'suspense_moves_mode'

Was task 1917430
  • Loading branch information
smetl authored and qdp-odoo committed Jun 28, 2019
1 parent 0bd61b5 commit beaa30a
Show file tree
Hide file tree
Showing 319 changed files with 12,218 additions and 59,894 deletions.
3 changes: 0 additions & 3 deletions addons/account/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@
'views/account_view.xml',
'views/account_report.xml',
'data/mail_template_data.xml',
'wizard/account_invoice_refund_view.xml',
'wizard/account_validate_move_view.xml',
'wizard/account_invoice_state_view.xml',
'wizard/pos_box.xml',
'views/account_end_fy.xml',
'views/account_invoice_view.xml',
'views/partner_view.xml',
'views/product_view.xml',
'views/account_analytic_view.xml',
Expand Down
18 changes: 10 additions & 8 deletions addons/account/controllers/portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class PortalAccount(CustomerPortal):

def _prepare_portal_layout_values(self):
values = super(PortalAccount, self)._prepare_portal_layout_values()
invoice_count = request.env['account.invoice'].search_count([])
invoice_count = request.env['account.move'].search_count([
('type', 'in', ('out_invoice', 'in_invoice', 'out_refund', 'in_refund', 'out_receipt', 'in_receipt')),
])
values['invoice_count'] = invoice_count
return values

Expand All @@ -29,13 +31,13 @@ def _invoice_get_page_view_values(self, invoice, access_token, **kwargs):
@http.route(['/my/invoices', '/my/invoices/page/<int:page>'], type='http', auth="user", website=True)
def portal_my_invoices(self, page=1, date_begin=None, date_end=None, sortby=None, **kw):
values = self._prepare_portal_layout_values()
AccountInvoice = request.env['account.invoice']
AccountInvoice = request.env['account.move']

domain = []
domain = [('type', 'in', ('out_invoice', 'out_refund', 'in_invoice', 'in_refund', 'out_receipt', 'in_receipt'))]

searchbar_sortings = {
'date': {'label': _('Invoice Date'), 'order': 'date_invoice desc'},
'duedate': {'label': _('Due Date'), 'order': 'date_due desc'},
'date': {'label': _('Invoice Date'), 'order': 'invoice_date desc'},
'duedate': {'label': _('Due Date'), 'order': 'invoice_date_due desc'},
'name': {'label': _('Reference'), 'order': 'name desc'},
'state': {'label': _('Status'), 'order': 'state'},
}
Expand All @@ -44,7 +46,7 @@ def portal_my_invoices(self, page=1, date_begin=None, date_end=None, sortby=None
sortby = 'date'
order = searchbar_sortings[sortby]['order']

archive_groups = self._get_archive_groups('account.invoice', domain)
archive_groups = self._get_archive_groups('account.move', domain)
if date_begin and date_end:
domain += [('create_date', '>', date_begin), ('create_date', '<=', date_end)]

Expand Down Expand Up @@ -77,7 +79,7 @@ def portal_my_invoices(self, page=1, date_begin=None, date_end=None, sortby=None
@http.route(['/my/invoices/<int:invoice_id>'], type='http', auth="public", website=True)
def portal_my_invoice_detail(self, invoice_id, access_token=None, report_type=None, download=False, **kw):
try:
invoice_sudo = self._document_check_access('account.invoice', invoice_id, access_token)
invoice_sudo = self._document_check_access('account.move', invoice_id, access_token)
except (AccessError, MissingError):
return request.redirect('/my')

Expand All @@ -88,7 +90,7 @@ def portal_my_invoice_detail(self, invoice_id, access_token=None, report_type=No
acquirers = values.get('acquirers')
if acquirers:
country_id = values.get('partner_id') and values.get('partner_id')[0].country_id.id
values['acq_extra_fees'] = acquirers.get_acquirer_extra_fees(invoice_sudo.residual, invoice_sudo.currency_id, country_id)
values['acq_extra_fees'] = acquirers.get_acquirer_extra_fees(invoice_sudo.amount_residual, invoice_sudo.currency_id, country_id)

return request.render("account.portal_invoice_page", values)

Expand Down
12 changes: 6 additions & 6 deletions addons/account/data/account_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,19 @@
<!-- Account-related subtypes for messaging / Chatter -->
<record id="mt_invoice_validated" model="mail.message.subtype">
<field name="name">Validated</field>
<field name="res_model">account.invoice</field>
<field name="res_model">account.move</field>
<field name="default" eval="False"/>
<field name="description">Invoice validated</field>
</record>
<record id="mt_invoice_paid" model="mail.message.subtype">
<field name="name">Paid</field>
<field name="res_model">account.invoice</field>
<field name="res_model">account.move</field>
<field name="default" eval="False"/>
<field name="description">Invoice paid</field>
</record>
<record id="mt_invoice_created" model="mail.message.subtype">
<field name="name">Invoice Created</field>
<field name="res_model">account.invoice</field>
<field name="res_model">account.move</field>
<field name="default" eval="False"/>
<field name="hidden" eval="True"/>
<field name="description">Invoice Created</field>
Expand Down Expand Up @@ -175,10 +175,10 @@
</record>

<!-- Share Button in action menu -->
<record id="model_account_invoice_action_share" model="ir.actions.server">
<record id="model_account_move_action_share" model="ir.actions.server">
<field name="name">Share</field>
<field name="model_id" ref="account.model_account_invoice"/>
<field name="binding_model_id" ref="account.model_account_invoice"/>
<field name="model_id" ref="account.model_account_move"/>
<field name="binding_model_id" ref="account.model_account_move"/>
<field name="state">code</field>
<field name="code">action = records.action_share()</field>
</record>
Expand Down
16 changes: 8 additions & 8 deletions addons/account/data/mail_template_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
<!--Email template -->
<record id="email_template_edi_invoice" model="mail.template">
<field name="name">Invoice: Send by email</field>
<field name="model_id" ref="account.model_account_invoice"/>
<field name="model_id" ref="account.model_account_move"/>
<field name="email_from">${(object.user_id.email_formatted or user.email_formatted) |safe}</field>
<field name="partner_to">${object.partner_id.id}</field>
<field name="subject">${object.company_id.name} Invoice (Ref ${object.number or 'n/a'})</field>
<field name="subject">${object.company_id.name} Invoice (Ref ${object.name or 'n/a'})</field>
<field name="body_html" type="html">
<div style="margin: 0px; padding: 0px;">
<p style="margin: 0px; padding: 0px; font-size: 13px;">
Expand All @@ -22,17 +22,17 @@
% endif
<br /><br />
Here is your
% if object.number:
invoice <strong>${object.number}</strong>
% if object.name:
invoice <strong>${object.name}</strong>
% else:
invoice
%endif
% if object.origin:
(with reference: ${object.origin})
% if object.invoice_origin:
(with reference: ${object.invoice_origin})
% endif
amounting in <strong>${format_amount(object.amount_total, object.currency_id)}</strong>
from ${object.company_id.name}.
% if object.state=='paid':
% if object.invoice_payment_state == 'paid':
This invoice is already paid.
% else:
Please remit payment at your earliest convenience.
Expand All @@ -43,7 +43,7 @@
</div>
</field>
<field name="report_template" ref="account_invoices"/>
<field name="report_name">Invoice_${(object.number or '').replace('/','_')}${object.state == 'draft' and '_draft' or ''}</field>
<field name="report_name">Invoice_${(object.name or '').replace('/','_')}${object.state == 'draft' and '_draft' or ''}</field>
<field name="lang">${object.partner_id.lang}</field>
<field name="user_signature" eval="False"/>
<field name="auto_delete" eval="True"/>
Expand Down
2 changes: 1 addition & 1 deletion addons/account/data/service_cron.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<field name="nextcall" eval="(DateTime.now().replace(hour=2, minute=0) + timedelta(days=1)).strftime('%Y-%m-%d %H:%M:%S')" />
<field name="doall" eval="False"/>
<field name="model_id" ref="model_account_move"/>
<field name="code">model._run_post_draft_to_post()</field>
<field name="code">model._autopost_draft_entries()</field>
<field name="state">code</field>
</record>
</odoo>
3 changes: 2 additions & 1 deletion addons/account/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from . import account
from . import account_reconcile_model
from . import account_payment
from . import account_invoice
from . import account_payment_term
from . import account_bank_statement
from . import account_move
from . import account_payment_term
from . import account_bank_statement
from . import chart_template
from . import account_analytic_line
from . import account_journal_dashboard
Expand Down
71 changes: 31 additions & 40 deletions addons/account/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,6 @@ def _get_bank_statements_available_sources(self):
domain=[('deprecated', '=', False)], help="It acts as a default account for debit amount")
update_posted = fields.Boolean(string='Allow Cancelling Entries',
help="Check this box if you want to allow the cancellation the entries related to this journal or of the invoice related to this journal")
group_invoice_lines = fields.Boolean(string='Group Invoice Lines',
help="If this box is checked, the system will try to group the accounting lines when generating them from invoices.")
sequence_id = fields.Many2one('ir.sequence', string='Entry Sequence',
help="This field contains the information related to the numbering of the journal entries of this journal.", required=True, copy=False)
refund_sequence_id = fields.Many2one('ir.sequence', string='Credit Note Entry Sequence',
Expand Down Expand Up @@ -738,7 +736,7 @@ def _update_mail_alias(self, vals):
if self.alias_id:
self.alias_id.write(alias_values)
else:
self.alias_id = self.env['mail.alias'].with_context(alias_model_name='account.invoice',
self.alias_id = self.env['mail.alias'].with_context(alias_model_name='account.move',
alias_parent_model_name='account.journal').create(alias_values)

if vals.get('alias_name'):
Expand Down Expand Up @@ -1179,17 +1177,6 @@ def onchange_price_include(self):
if self.price_include:
self.include_base_amount = True

def get_grouping_key(self, invoice_tax_val):
""" Returns a string that will be used to group account.invoice.tax sharing the same properties"""
self.ensure_one()
return str(invoice_tax_val['tax_id']) + '-' + \
str(invoice_tax_val.get('tax_repartition_line_id')) + '-' + \
str(invoice_tax_val['account_id']) + '-' + \
str(invoice_tax_val['account_analytic_id']) + '-' + \
str(invoice_tax_val.get('analytic_tag_ids', [])) + '-' + \
str(invoice_tax_val.get('tax_ids') or []) + '-' + \
str(invoice_tax_val.get('tag_ids') or [])

def _compute_amount(self, base_amount, price_unit, quantity=1.0, product=None, partner=None):
""" Returns the amount of a single tax. base_amount is the actual amount on which the tax is applied, which is
price_unit * quantity eventually affected by previous taxes (if tax is include_base_amount XOR price_include)
Expand All @@ -1210,7 +1197,7 @@ def _compute_amount(self, base_amount, price_unit, quantity=1.0, product=None, p
else:
return quantity * self.amount

price_include = self._context['force_price_include'] if 'force_price_include' in self._context else self.price_include
price_include = self._context.get('force_price_include', self.price_include)

# base * (1 + tax_amount) = new_base
if self.amount_type == 'percent' and not price_include:
Expand Down Expand Up @@ -1254,12 +1241,15 @@ def get_tax_tags(self, is_refund, repartition_type):
return rep_lines.filtered(lambda x: x.repartition_type == repartition_type).mapped('tag_ids')

@api.multi
def compute_all(self, price_unit, currency=None, quantity=1.0, product=None, partner=None, is_refund=False):
def compute_all(self, price_unit, currency=None, quantity=1.0, product=None, partner=None, is_refund=False, handle_price_include=True):
""" Returns all information required to apply taxes (in self + their children in case of a tax group).
We consider the sequence of the parent for group of taxes.
Eg. considering letters as taxes and alphabetic order as sequence :
[G, B([A, D, F]), E, C] will be computed as [A, D, F, C, E, G]
'handle_price_include' is used when we need to ignore all tax included in price. If False, it means the
amount passed to this method will be considered as the base of all computations.
RETURN: {
'total_excluded': 0.0, # Total without taxes
'total_included': 0.0, # Total with taxes
Expand Down Expand Up @@ -1362,28 +1352,29 @@ def recompute_base(base_amount, fixed_amount, percent_amount, division_amount):
incl_fixed_amount = incl_percent_amount = incl_division_amount = 0
# Store the tax amounts we compute while searching for the total_excluded
cached_tax_amounts = {}
for tax in reversed(taxes):
if tax.include_base_amount:
base = recompute_base(base, incl_fixed_amount, incl_percent_amount, incl_division_amount)
incl_fixed_amount = incl_percent_amount = incl_division_amount = 0
store_included_tax_total = True
if tax.price_include:
if tax.amount_type == 'percent':
incl_percent_amount += tax.amount
elif tax.amount_type == 'division':
incl_division_amount += tax.amount
elif tax.amount_type == 'fixed':
incl_fixed_amount += quantity * tax.amount
else:
# tax.amount_type == other (python)
tax_amount = tax._compute_amount(base, price_unit, quantity, product, partner)
incl_fixed_amount += tax_amount
# Avoid unecessary re-computation
cached_tax_amounts[i] = tax_amount
if store_included_tax_total:
total_included_checkpoints[i] = base
store_included_tax_total = False
i -= 1
if handle_price_include:
for tax in reversed(taxes):
if tax.include_base_amount:
base = recompute_base(base, incl_fixed_amount, incl_percent_amount, incl_division_amount)
incl_fixed_amount = incl_percent_amount = incl_division_amount = 0
store_included_tax_total = True
if tax.price_include:
if tax.amount_type == 'percent':
incl_percent_amount += tax.amount
elif tax.amount_type == 'division':
incl_division_amount += tax.amount
elif tax.amount_type == 'fixed':
incl_fixed_amount += quantity * tax.amount
else:
# tax.amount_type == other (python)
tax_amount = tax._compute_amount(base, price_unit, quantity, product, partner)
incl_fixed_amount += tax_amount
# Avoid unecessary re-computation
cached_tax_amounts[i] = tax_amount
if store_included_tax_total:
total_included_checkpoints[i] = base
store_included_tax_total = False
i -= 1

total_excluded = recompute_base(base, incl_fixed_amount, incl_percent_amount, incl_division_amount)

Expand Down Expand Up @@ -1438,8 +1429,8 @@ def recompute_base(base_amount, fixed_amount, percent_amount, division_amount):
'price_include': tax.price_include,
'tax_exigibility': tax.tax_exigibility,
'tax_repartition_line_id': repartition_line.id,
'tag_ids': [(6, False, (repartition_line.tag_ids + subsequent_tags).ids)],
'tax_ids': [(6, False, subsequent_taxes.ids)]
'tag_ids': (repartition_line.tag_ids + subsequent_tags).ids,
'tax_ids': subsequent_taxes.ids,
})

total_amount += line_amount
Expand Down
Loading

0 comments on commit beaa30a

Please sign in to comment.