diff --git a/addons/account/__manifest__.py b/addons/account/__manifest__.py index 9d042afbd3c50..f7ab4fcac794d 100644 --- a/addons/account/__manifest__.py +++ b/addons/account/__manifest__.py @@ -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', diff --git a/addons/account/controllers/portal.py b/addons/account/controllers/portal.py index 99d44428f29d6..b72d3415bcff2 100644 --- a/addons/account/controllers/portal.py +++ b/addons/account/controllers/portal.py @@ -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 @@ -29,13 +31,13 @@ def _invoice_get_page_view_values(self, invoice, access_token, **kwargs): @http.route(['/my/invoices', '/my/invoices/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'}, } @@ -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)] @@ -77,7 +79,7 @@ def portal_my_invoices(self, page=1, date_begin=None, date_end=None, sortby=None @http.route(['/my/invoices/'], 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') @@ -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) diff --git a/addons/account/data/account_data.xml b/addons/account/data/account_data.xml index a8c1d83daef65..ed3478c21abf0 100644 --- a/addons/account/data/account_data.xml +++ b/addons/account/data/account_data.xml @@ -78,19 +78,19 @@ Validated - account.invoice + account.move Invoice validated Paid - account.invoice + account.move Invoice paid Invoice Created - account.invoice + account.move Invoice Created @@ -175,10 +175,10 @@ - + Share - - + + code action = records.action_share() diff --git a/addons/account/data/mail_template_data.xml b/addons/account/data/mail_template_data.xml index def7a705654c1..cc2d2c07907a5 100644 --- a/addons/account/data/mail_template_data.xml +++ b/addons/account/data/mail_template_data.xml @@ -7,10 +7,10 @@ Invoice: Send by email - + ${(object.user_id.email_formatted or user.email_formatted) |safe} ${object.partner_id.id} - ${object.company_id.name} Invoice (Ref ${object.number or 'n/a'}) + ${object.company_id.name} Invoice (Ref ${object.name or 'n/a'})

@@ -22,17 +22,17 @@ % endif

Here is your - % if object.number: - invoice ${object.number} + % if object.name: + invoice ${object.name} % else: invoice %endif - % if object.origin: - (with reference: ${object.origin}) + % if object.invoice_origin: + (with reference: ${object.invoice_origin}) % endif amounting in ${format_amount(object.amount_total, object.currency_id)} 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. @@ -43,7 +43,7 @@

- Invoice_${(object.number or '').replace('/','_')}${object.state == 'draft' and '_draft' or ''} + Invoice_${(object.name or '').replace('/','_')}${object.state == 'draft' and '_draft' or ''} ${object.partner_id.lang} diff --git a/addons/account/data/service_cron.xml b/addons/account/data/service_cron.xml index 6436692b2d57d..231e9ff7ed6b6 100644 --- a/addons/account/data/service_cron.xml +++ b/addons/account/data/service_cron.xml @@ -8,7 +8,7 @@ - model._run_post_draft_to_post() + model._autopost_draft_entries() code
diff --git a/addons/account/models/__init__.py b/addons/account/models/__init__.py index 6f178fd2665d7..f52625112da7f 100644 --- a/addons/account/models/__init__.py +++ b/addons/account/models/__init__.py @@ -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 diff --git a/addons/account/models/account.py b/addons/account/models/account.py index da381195dd2dd..cb7ce2dd8aca0 100644 --- a/addons/account/models/account.py +++ b/addons/account/models/account.py @@ -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', @@ -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'): @@ -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) @@ -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: @@ -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 @@ -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) @@ -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 diff --git a/addons/account/models/account_bank_statement.py b/addons/account/models/account_bank_statement.py index 4718efa059901..7d0a9e16926de 100644 --- a/addons/account/models/account_bank_statement.py +++ b/addons/account/models/account_bank_statement.py @@ -397,6 +397,7 @@ def button_cancel_reconciliation(self): aml_to_cancel.remove_move_reconcile() moves_to_cancel = aml_to_cancel.mapped('move_id') moves_to_cancel.button_cancel() + moves_to_cancel.button_draft() moves_to_cancel.unlink() if payment_to_cancel: payment_to_cancel.unlink() @@ -434,7 +435,9 @@ def _prepare_reconciliation_move(self, move_ref): if self.ref: ref = move_ref + ' - ' + self.ref if move_ref else self.ref data = { + 'type': 'entry', 'journal_id': self.statement_id.journal_id.id, + 'currency_id': self.statement_id.currency_id.id, 'date': self.statement_id.accounting_date or self.date, 'ref': ref, } @@ -624,7 +627,7 @@ def process_reconciliation(self, counterpart_aml_dicts=None, payment_aml_rec=Non """ payable_account_type = self.env.ref('account.data_account_type_payable') receivable_account_type = self.env.ref('account.data_account_type_receivable') - edition_mode = self._context.get('edition_mode') + suspense_moves_mode = self._context.get('suspense_moves_mode') counterpart_aml_dicts = counterpart_aml_dicts or [] payment_aml_rec = payment_aml_rec or self.env['account.move.line'] new_aml_dicts = new_aml_dicts or [] @@ -641,7 +644,7 @@ def process_reconciliation(self, counterpart_aml_dicts=None, payment_aml_rec=Non if any(rec.statement_id for rec in payment_aml_rec): raise UserError(_('A selected move line was already reconciled.')) for aml_dict in counterpart_aml_dicts: - if aml_dict['move_line'].reconciled and not edition_mode: + if aml_dict['move_line'].reconciled and not suspense_moves_mode: raise UserError(_('A selected move line was already reconciled.')) if isinstance(aml_dict['move_line'], int): aml_dict['move_line'] = aml_obj.browse(aml_dict['move_line']) @@ -655,7 +658,7 @@ def process_reconciliation(self, counterpart_aml_dicts=None, payment_aml_rec=Non user_type_id = self.env['account.account'].browse(aml_dict.get('account_id')).user_type_id if user_type_id in [payable_account_type, receivable_account_type] and user_type_id not in account_types: account_types |= user_type_id - if edition_mode: + if suspense_moves_mode: if any(not line.journal_entry_ids for line in self): raise UserError(_('Some selected statement line were not already reconciled with an account move.')) else: @@ -688,9 +691,9 @@ def process_reconciliation(self, counterpart_aml_dicts=None, payment_aml_rec=Non # Create the move self.sequence = self.statement_id.line_ids.ids.index(self.id) + 1 move_vals = self._prepare_reconciliation_move(self.statement_id.name) - if edition_mode: + if suspense_moves_mode: self.button_cancel_reconciliation() - move = self.env['account.move'].create(move_vals) + move = self.env['account.move'].with_context(default_journal_id=move_vals['journal_id']).create(move_vals) counterpart_moves = (counterpart_moves | move) # Create The payment @@ -732,7 +735,7 @@ def process_reconciliation(self, counterpart_aml_dicts=None, payment_aml_rec=Non (new_aml | counterpart_move_line).reconcile() - self._check_invoice_state(counterpart_move_line.invoice_id) + self._check_invoice_state(counterpart_move_line.move_id) # Balance the move st_line_amount = -sum([x.balance for x in move.line_ids]) @@ -758,7 +761,7 @@ def process_reconciliation(self, counterpart_aml_dicts=None, payment_aml_rec=Non }) self.bank_account_id = bank_account - counterpart_moves.assert_balanced() + counterpart_moves._check_balanced() return counterpart_moves @api.multi @@ -792,5 +795,4 @@ def _prepare_move_line_for_currency(self, aml_dict, date): aml_dict['currency_id'] = statement_currency.id def _check_invoice_state(self, invoice): - if invoice.state == 'in_payment' and all([payment.state == 'reconciled' for payment in invoice.mapped('payment_move_line_ids.payment_id')]): - invoice.write({'state': 'paid'}) + invoice._compute_amount() diff --git a/addons/account/models/account_invoice.py b/addons/account/models/account_invoice.py deleted file mode 100644 index 8e0744403e4f4..0000000000000 --- a/addons/account/models/account_invoice.py +++ /dev/null @@ -1,2131 +0,0 @@ -# -*- coding: utf-8 -*- - -from collections import OrderedDict -import json -import re -import uuid -from functools import partial -from stdnum.iso7064 import mod_97_10 -from itertools import zip_longest - -from lxml import etree - -from odoo import api, exceptions, fields, models, _ -from odoo.tools import email_re, email_split, email_escape_char, float_is_zero, float_compare, \ - pycompat, date_utils -from odoo.tools.misc import formatLang - -from odoo.exceptions import UserError, RedirectWarning, ValidationError, Warning - -from odoo.addons import decimal_precision as dp -import logging - -_logger = logging.getLogger(__name__) - -# mapping invoice type to journal type -TYPE2JOURNAL = { - 'out_invoice': 'sale', - 'in_invoice': 'purchase', - 'out_refund': 'sale', - 'in_refund': 'purchase', -} - -# mapping invoice type to refund type -TYPE2REFUND = { - 'out_invoice': 'out_refund', # Customer Invoice - 'in_invoice': 'in_refund', # Vendor Bill - 'out_refund': 'out_invoice', # Customer Credit Note - 'in_refund': 'in_invoice', # Vendor Credit Note -} - -MAGIC_COLUMNS = ('id', 'create_uid', 'create_date', 'write_uid', 'write_date') - - -class AccountInvoice(models.Model): - _name = "account.invoice" - _inherit = ['portal.mixin', 'mail.thread', 'mail.activity.mixin'] - _description = "Invoice" - _order = "date_invoice desc, number desc, id desc" - - - def _get_default_incoterm(self): - return self.env.company.incoterm_id - - @api.one - @api.depends('invoice_line_ids.price_subtotal', 'tax_line_ids.amount', 'tax_line_ids.amount_rounding', - 'currency_id', 'company_id', 'date_invoice', 'type') - def _compute_amount(self): - round_curr = self.currency_id.round - self.amount_untaxed = sum(line.price_subtotal for line in self.invoice_line_ids) - self.amount_tax = sum(round_curr(line.amount_total) for line in self.tax_line_ids) - self.amount_total = self.amount_untaxed + self.amount_tax - amount_total_company_signed = self.amount_total - amount_untaxed_signed = self.amount_untaxed - if self.currency_id and self.company_id and self.currency_id != self.company_id.currency_id: - currency_id = self.currency_id - amount_total_company_signed = currency_id._convert(self.amount_total, self.company_id.currency_id, self.company_id, self.date_invoice or fields.Date.today()) - amount_untaxed_signed = currency_id._convert(self.amount_untaxed, self.company_id.currency_id, self.company_id, self.date_invoice or fields.Date.today()) - sign = self.type in ['in_refund', 'out_refund'] and -1 or 1 - self.amount_total_company_signed = amount_total_company_signed * sign - self.amount_total_signed = self.amount_total * sign - self.amount_untaxed_signed = amount_untaxed_signed * sign - - def _compute_sign_taxes(self): - for invoice in self: - sign = invoice.type in ['in_refund', 'out_refund'] and -1 or 1 - invoice.amount_untaxed_invoice_signed = invoice.amount_untaxed * sign - invoice.amount_tax_signed = invoice.amount_tax * sign - - @api.onchange('amount_total') - def _onchange_amount_total(self): - for inv in self: - if float_compare(inv.amount_total, 0.0, precision_rounding=inv.currency_id.rounding) == -1: - raise Warning(_('You cannot validate an invoice with a negative total amount. You should create a credit note instead.')) - - @api.model - def _default_journal(self): - if self._context.get('default_journal_id', False): - return self.env['account.journal'].browse(self._context.get('default_journal_id')) - inv_type = self._context.get('type', 'out_invoice') - inv_types = inv_type if isinstance(inv_type, list) else [inv_type] - company_id = self._context.get('company_id', self.env.company.id) - domain = [ - ('type', 'in', [TYPE2JOURNAL[ty] for ty in inv_types if ty in TYPE2JOURNAL]), - ('company_id', '=', company_id), - ] - company_currency_id = self.env['res.company'].browse(company_id).currency_id.id - currency_id = self._context.get('default_currency_id') or company_currency_id - currency_clause = [('currency_id', '=', currency_id)] - if currency_id == company_currency_id: - currency_clause = ['|', ('currency_id', '=', False)] + currency_clause - return ( - self.env['account.journal'].search(domain + currency_clause, limit=1) - or self.env['account.journal'].search(domain, limit=1) - ) - - @api.model - def _default_currency(self): - journal = self._default_journal() - return journal.currency_id or journal.company_id.currency_id or self.env.company.currency_id - - def _default_comment(self): - invoice_type = self.env.context.get('type', 'out_invoice') - if invoice_type == 'out_invoice' and self.env['ir.config_parameter'].sudo().get_param('account.use_invoice_terms'): - return self.env.company.invoice_terms - - def _get_aml_for_amount_residual(self): - """ Get the aml to consider to compute the amount residual of invoices """ - self.ensure_one() - return self.sudo().move_id.line_ids.filtered(lambda l: l.account_id == self.account_id) - - @api.one - @api.depends( - 'state', 'currency_id', 'invoice_line_ids.price_subtotal', - 'move_id.line_ids.amount_residual', - 'move_id.line_ids.currency_id') - def _compute_residual(self): - residual = 0.0 - residual_company_signed = 0.0 - sign = self.type in ['in_refund', 'out_refund'] and -1 or 1 - for line in self._get_aml_for_amount_residual(): - residual_company_signed += line.amount_residual - if line.currency_id == self.currency_id: - residual += line.amount_residual_currency if line.currency_id else line.amount_residual - else: - from_currency = line.currency_id or line.company_id.currency_id - residual += from_currency._convert(line.amount_residual, self.currency_id, line.company_id, line.date or fields.Date.today()) - self.residual_company_signed = abs(residual_company_signed) * sign - self.residual_signed = abs(residual) * sign - self.residual = abs(residual) - digits_rounding_precision = self.currency_id.rounding - if self.move_id and float_is_zero(self.residual, precision_rounding=digits_rounding_precision): - self.reconciled = True - else: - self.reconciled = False - - @api.multi - def _get_domain_edition_mode_available(self): - self.ensure_one() - domain = self.env['account.move.line']._get_domain_for_edition_mode() - domain += ['|',('move_id.partner_id', '=?', self.partner_id.id),('move_id.partner_id', '=', False)] - if self.type in ('out_invoice', 'in_refund'): - domain.append(('balance', '=', -self.residual)) - else: - domain.append(('balance', '=', self.residual)) - return domain - - @api.multi - def _get_edition_mode_available(self): - for r in self: - domain = r._get_domain_edition_mode_available() - domain2 = [('state', '=', 'open'),('residual', '=', r.residual),('type', '=', r.type)] - r.edition_mode_available = (0 < self.env['account.move.line'].search_count(domain) < 5) and self.env['account.invoice'].search_count(domain2) < 5 and r.state == 'open' - - @api.one - def _get_outstanding_info_JSON(self): - self.outstanding_credits_debits_widget = json.dumps(False) - if self.state == 'open': - domain = [('account_id', '=', self.account_id.id), - ('partner_id', '=', self.env['res.partner']._find_accounting_partner(self.partner_id).id), - ('reconciled', '=', False), - '|', - '&', ('amount_residual_currency', '!=', 0.0), ('currency_id','!=', None), - '&', ('amount_residual_currency', '=', 0.0), '&', ('currency_id','=', None), ('amount_residual', '!=', 0.0)] - if self.type in ('out_invoice', 'in_refund'): - domain.extend([('credit', '>', 0), ('debit', '=', 0)]) - type_payment = _('Outstanding credits') - else: - domain.extend([('credit', '=', 0), ('debit', '>', 0)]) - type_payment = _('Outstanding debits') - info = {'title': '', 'outstanding': True, 'content': [], 'invoice_id': self.id} - lines = self.env['account.move.line'].search(domain) - currency_id = self.currency_id - if len(lines) != 0: - for line in lines: - # get the outstanding residual value in invoice currency - if line.currency_id and line.currency_id == self.currency_id: - amount_to_show = abs(line.amount_residual_currency) - else: - currency = line.company_id.currency_id - amount_to_show = currency._convert(abs(line.amount_residual), self.currency_id, self.company_id, line.date or fields.Date.today()) - if float_is_zero(amount_to_show, precision_rounding=self.currency_id.rounding): - continue - if line.ref : - title = '%s : %s' % (line.move_id.name, line.ref) - else: - title = line.move_id.name - info['content'].append({ - 'journal_name': line.ref or line.move_id.name, - 'title': title, - 'amount': amount_to_show, - 'currency': currency_id.symbol, - 'id': line.id, - 'position': currency_id.position, - 'digits': [69, self.currency_id.decimal_places], - }) - info['title'] = type_payment - self.outstanding_credits_debits_widget = json.dumps(info) - self.has_outstanding = True - - @api.model - def _get_payments_vals(self): - if not self.payment_move_line_ids: - return [] - payment_vals = [] - currency_id = self.currency_id - for payment in self.payment_move_line_ids: - payment_currency_id = False - if self.type in ('out_invoice', 'in_refund'): - amount = sum([p.amount for p in payment.matched_debit_ids if p.debit_move_id in self.move_id.line_ids]) - amount_currency = sum( - [p.amount_currency for p in payment.matched_debit_ids if p.debit_move_id in self.move_id.line_ids]) - if payment.matched_debit_ids: - payment_currency_id = all([p.currency_id == payment.matched_debit_ids[0].currency_id for p in - payment.matched_debit_ids]) and payment.matched_debit_ids[ - 0].currency_id or False - elif self.type in ('in_invoice', 'out_refund'): - amount = sum( - [p.amount for p in payment.matched_credit_ids if p.credit_move_id in self.move_id.line_ids]) - amount_currency = sum([p.amount_currency for p in payment.matched_credit_ids if - p.credit_move_id in self.move_id.line_ids]) - if payment.matched_credit_ids: - payment_currency_id = all([p.currency_id == payment.matched_credit_ids[0].currency_id for p in - payment.matched_credit_ids]) and payment.matched_credit_ids[ - 0].currency_id or False - # get the payment value in invoice currency - if payment_currency_id and payment_currency_id == self.currency_id: - amount_to_show = amount_currency - else: - currency = payment.company_id.currency_id - amount_to_show = currency._convert(amount, self.currency_id, payment.company_id, self.date or fields.Date.today()) - if float_is_zero(amount_to_show, precision_rounding=self.currency_id.rounding): - continue - payment_ref = payment.move_id.name - if payment.move_id.ref: - payment_ref += ' (' + payment.move_id.ref + ')' - payment_vals.append({ - 'name': payment.name, - 'journal_name': payment.journal_id.name, - 'amount': amount_to_show, - 'currency': currency_id.symbol, - 'digits': [69, currency_id.decimal_places], - 'position': currency_id.position, - 'date': payment.date, - 'payment_id': payment.id, - 'account_payment_id': payment.payment_id.id, - 'invoice_id': payment.invoice_id.id, - 'move_id': payment.move_id.id, - 'ref': payment_ref, - }) - return payment_vals - - @api.one - @api.depends('payment_move_line_ids.amount_residual') - def _get_payment_info_JSON(self): - self.payments_widget = json.dumps(False) - if self.payment_move_line_ids: - info = {'title': _('Less Payment'), 'outstanding': False, 'content': self._get_payments_vals()} - self.payments_widget = json.dumps(info, default=date_utils.json_default) - - @api.one - @api.depends('move_id.line_ids.amount_residual') - def _compute_payments(self): - payment_lines = set() - for line in self.move_id.line_ids.filtered(lambda l: l.account_id.id == self.account_id.id): - payment_lines.update(line.mapped('matched_credit_ids.credit_move_id.id')) - payment_lines.update(line.mapped('matched_debit_ids.debit_move_id.id')) - self.payment_move_line_ids = self.env['account.move.line'].browse(list(payment_lines)).sorted() - - name = fields.Char(string='Reference/Description', index=True, - readonly=True, states={'draft': [('readonly', False)]}, copy=False, help='The name that will be used on account move lines') - - origin = fields.Char(string='Source Document', - help="Reference of the document that produced this invoice.", - readonly=True, states={'draft': [('readonly', False)]}) - type = fields.Selection([ - ('out_invoice','Customer Invoice'), - ('in_invoice','Vendor Bill'), - ('out_refund','Customer Credit Note'), - ('in_refund','Vendor Credit Note'), - ], readonly=True, states={'draft': [('readonly', False)]}, index=True, change_default=True, - default=lambda self: self._context.get('type', 'out_invoice'), - tracking=True) - - refund_invoice_id = fields.Many2one('account.invoice', string="Invoice for which this invoice is the credit note") - number = fields.Char(related='move_id.name', store=True, readonly=True, copy=False) - move_name = fields.Char(string='Journal Entry Name', readonly=False, - default=False, copy=False, - help="Technical field holding the number given to the invoice, automatically set when the invoice is validated then stored to set the same number again if the invoice is cancelled, set to draft and re-validated.") - reference = fields.Char(string='Payment Ref.', copy=False, readonly=True, states={'draft': [('readonly', False)]}, - help='Automatically generated once the invoice is confirmed. You can also write a free communication.') - comment = fields.Text('Additional Information', readonly=True, states={'draft': [('readonly', False)]}, default=_default_comment) - - state = fields.Selection([ - ('draft','Draft'), - ('open', 'Open'), - ('in_payment', 'In Payment'), - ('paid', 'Paid'), - ('cancel', 'Cancelled'), - ], string='Status', index=True, readonly=True, default='draft', - tracking=True, copy=False, - help=" * The 'Draft' status is used when a user is encoding a new and unconfirmed Invoice.\n" - " * The 'Open' status is used when user creates invoice, an invoice number is generated. It stays in the open status till the user pays the invoice.\n" - " * The 'In Payment' status is used when payments have been registered for the entirety of the invoice in a journal configured to post entries at bank reconciliation only, and some of them haven't been reconciled with a bank statement line yet.\n" - " * The 'Paid' status is set automatically when the invoice is paid. Its related journal entries may or may not be reconciled.\n" - " * The 'Cancelled' status is used when user cancel invoice.") - sent = fields.Boolean(readonly=True, default=False, copy=False, - help="It indicates that the invoice has been sent.") - date_invoice = fields.Date(string='Invoice Date', - readonly=True, states={'draft': [('readonly', False)]}, index=True, - help="Keep empty to use the current date", copy=False) - date_due = fields.Date(string='Due Date', - readonly=True, states={'draft': [('readonly', False)]}, index=True, copy=False, - help="If you use payment terms, the due date will be computed automatically at the generation " - "of accounting entries. The Payment terms may compute several due dates, for example 50% " - "now and 50% in one month, but if you want to force a due date, make sure that the payment " - "term is not set on the invoice. If you keep the Payment terms and the due date empty, it " - "means direct payment.") - partner_id = fields.Many2one('res.partner', string='Partner', change_default=True, - readonly=True, states={'draft': [('readonly', False)]}, - tracking=True, help="You can find a contact by its Name, TIN, Email or Internal Reference.") - vendor_bill_id = fields.Many2one('account.invoice', string='Vendor Bill', - help="Auto-complete from a past bill.") - payment_term_id = fields.Many2one('account.payment.term', string='Payment Terms', oldname='payment_term', - readonly=True, states={'draft': [('readonly', False)]}, - help="If you use payment terms, the due date will be computed automatically at the generation " - "of accounting entries. If you keep the payment terms and the due date empty, it means direct payment. " - "The payment terms may compute several due dates, for example 50% now, 50% in one month.") - date = fields.Date(string='Accounting Date', - copy=False, - help="Keep empty to use the invoice date.", - readonly=True, states={'draft': [('readonly', False)]}) - - account_id = fields.Many2one('account.account', string='Account', - readonly=True, states={'draft': [('readonly', False)]}, - domain=[('deprecated', '=', False)], help="The partner account used for this invoice.") - invoice_line_ids = fields.One2many('account.invoice.line', 'invoice_id', string='Invoice Lines', oldname='invoice_line', - readonly=True, states={'draft': [('readonly', False)]}, copy=True) - tax_line_ids = fields.One2many('account.invoice.tax', 'invoice_id', string='Tax Lines', oldname='tax_line', - readonly=True, states={'draft': [('readonly', False)]}, copy=True) - refund_invoice_ids = fields.One2many('account.invoice', 'refund_invoice_id', string='Refund Invoices', readonly=True) - move_id = fields.Many2one('account.move', string='Journal Entry', - readonly=True, index=True, ondelete='restrict', copy=False, - help="Link to the automatically generated Journal Items.") - - amount_by_group = fields.Binary(string="Tax amount by group", compute='_amount_by_group', help="type: [(name, amount, base, formated amount, formated base)]") - amount_untaxed = fields.Monetary(string='Untaxed Amount', - store=True, readonly=True, compute='_compute_amount', tracking=True) - amount_untaxed_signed = fields.Monetary(string='Untaxed Amount in Company Currency', currency_field='company_currency_id', - store=True, readonly=True, compute='_compute_amount') - amount_untaxed_invoice_signed = fields.Monetary(string='Untaxed Amount in Invoice Currency', currency_field='currency_id', - readonly=True, compute='_compute_sign_taxes') - amount_tax = fields.Monetary(string='Tax', - store=True, readonly=True, compute='_compute_amount') - amount_tax_signed = fields.Monetary(string='Tax in Invoice Currency', currency_field='currency_id', - readonly=True, compute='_compute_sign_taxes') - amount_total = fields.Monetary(string='Total', - store=True, readonly=True, compute='_compute_amount') - amount_total_signed = fields.Monetary(string='Total in Invoice Currency', currency_field='currency_id', - store=True, readonly=True, compute='_compute_amount', - help="Total amount in the currency of the invoice, negative for credit notes.") - amount_total_company_signed = fields.Monetary(string='Total in Company Currency', currency_field='company_currency_id', - store=True, readonly=True, compute='_compute_amount', - help="Total amount in the currency of the company, negative for credit notes.") - currency_id = fields.Many2one('res.currency', string='Currency', - required=True, readonly=True, states={'draft': [('readonly', False)]}, - default=_default_currency, tracking=True) - company_currency_id = fields.Many2one('res.currency', related='company_id.currency_id', string="Company Currency", readonly=True) - journal_id = fields.Many2one('account.journal', string='Journal', - required=True, readonly=True, states={'draft': [('readonly', False)]}, - default=_default_journal, - domain="[('type', 'in', {'out_invoice': ['sale'], 'out_refund': ['sale'], 'in_refund': ['purchase'], 'in_invoice': ['purchase']}.get(type, [])), ('company_id', '=', company_id)]") - company_id = fields.Many2one('res.company', string='Company', change_default=True, - required=True, readonly=True, states={'draft': [('readonly', False)]}, - default=lambda self: self.env.company) - - reconciled = fields.Boolean(string='Paid/Reconciled', store=True, readonly=True, compute='_compute_residual', - help="It indicates that the invoice has been paid and the journal entry of the invoice has been reconciled with one or several journal entries of payment.") - partner_bank_id = fields.Many2one('res.partner.bank', string='Bank Account', - help='Bank Account Number to which the invoice will be paid. A Company bank account if this is a Customer Invoice or Vendor Credit Note, otherwise a Partner bank account number.', - readonly=True, states={'draft': [('readonly', False)]}) #Default value computed in default_get for out_invoices - - residual = fields.Monetary(string='Amount Due', - compute='_compute_residual', store=True, help="Remaining amount due.") - residual_signed = fields.Monetary(string='Amount Due in Invoice Currency', currency_field='currency_id', - compute='_compute_residual', store=True, help="Remaining amount due in the currency of the invoice.") - residual_company_signed = fields.Monetary(string='Amount Due in Company Currency', currency_field='company_currency_id', - compute='_compute_residual', store=True, help="Remaining amount due in the currency of the company.") - payment_ids = fields.Many2many('account.payment', 'account_invoice_payment_rel', 'invoice_id', 'payment_id', string="Payments", copy=False, readonly=True) - payment_move_line_ids = fields.Many2many('account.move.line', string='Payment Move Lines', compute='_compute_payments', store=True) - user_id = fields.Many2one('res.users', string='Salesperson', tracking=True, - readonly=True, states={'draft': [('readonly', False)]}, - default=lambda self: self.env.user, copy=False) - fiscal_position_id = fields.Many2one('account.fiscal.position', string='Fiscal Position', oldname='fiscal_position', - readonly=True, states={'draft': [('readonly', False)]}, - help="""Fiscal positions are used to adapt taxes and accounts for particular customers or sales orders/invoices. - The default value comes from the customer.""") - commercial_partner_id = fields.Many2one('res.partner', string='Commercial Entity', compute_sudo=True, - related='partner_id.commercial_partner_id', store=True, readonly=True, - help="The commercial entity that will be used on Journal Entries for this invoice") - - edition_mode_available = fields.Boolean(compute='_get_edition_mode_available', groups='account.group_account_invoice') - outstanding_credits_debits_widget = fields.Text(compute='_get_outstanding_info_JSON', groups="account.group_account_invoice") - payments_widget = fields.Text(compute='_get_payment_info_JSON', groups="account.group_account_invoice") - has_outstanding = fields.Boolean(compute='_get_outstanding_info_JSON', groups="account.group_account_invoice") - cash_rounding_id = fields.Many2one('account.cash.rounding', string='Cash Rounding Method', - readonly=True, states={'draft': [('readonly', False)]}, - help='Defines the smallest coinage of the currency that can be used to pay by cash.') - - #fields use to set the sequence, on the first invoice of the journal - sequence_number_next = fields.Char(string='Next Number', compute="_get_sequence_number_next", inverse="_set_sequence_next") - sequence_number_next_prefix = fields.Char(string='Next Number Prefix', compute="_get_sequence_prefix") - incoterm_id = fields.Many2one('account.incoterms', string='Incoterm', - default=_get_default_incoterm, - help='International Commercial Terms are a series of predefined commercial terms used in international transactions.') - - #fields related to vendor bills automated creation by email - source_email = fields.Char(string='Source Email', tracking=True) - vendor_display_name = fields.Char(compute='_get_vendor_display_info', store=True) # store=True to enable sorting on that column - invoice_icon = fields.Char(compute='_get_vendor_display_info', store=False) - - _sql_constraints = [ - ('number_uniq', 'unique(number, company_id, journal_id, type)', 'Invoice Number must be unique per Company!'), - ] - - @api.depends('partner_id', 'source_email') - def _get_vendor_display_info(self): - for invoice in self: - vendor_display_name = invoice.partner_id.name - invoice.invoice_icon = '' - if not vendor_display_name: - if invoice.source_email: - vendor_display_name = _('From: ') + invoice.source_email - invoice.invoice_icon = '@' - else: - vendor_display_name = ('Created by: ') + invoice.create_uid.name - invoice.invoice_icon = '#' - invoice.vendor_display_name = vendor_display_name - - def _get_reference_euro_invoice(self): - """ This computes the reference based on the RF Creditor Reference. - The data of the reference is the database id number of the invoice. - For instance, if an invoice is issued with id 43, the check number - is 07 so the reference will be 'RF07 43'. - """ - self.ensure_one() - base = self.id - check_digits = mod_97_10.calc_check_digits('{}RF'.format(base)) - reference = 'RF{} {}'.format(check_digits, " ".join(["".join(x) for x in zip_longest(*[iter(str(base))]*4, fillvalue="")])) - return reference - - def _get_reference_euro_partner(self): - """ This computes the reference based on the RF Creditor Reference. - The data of the reference is the user defined reference of the - partner or the database id number of the parter. - For instance, if an invoice is issued for the partner with internal - reference 'food buyer 654', the digits will be extracted and used as - the data. This will lead to a check number equal to 00 and the - reference will be 'RF00 654'. - If no reference is set for the partner, its id in the database will - be used. - """ - self.ensure_one() - partner_ref = self.partner_id.ref - partner_ref_nr = re.sub('\D', '', partner_ref or '')[-21:] or str(self.partner_id.id)[-21:] - partner_ref_nr = partner_ref_nr[-21:] - check_digits = mod_97_10.calc_check_digits('{}RF'.format(partner_ref_nr)) - reference = 'RF{} {}'.format(check_digits, " ".join(["".join(x) for x in zip_longest(*[iter(partner_ref_nr)]*4, fillvalue="")])) - return reference - - def _get_reference_odoo_invoice(self): - """ This computes the reference based on the Odoo format. - We simply return the number of the invoice, defined on the journal - sequence. - """ - self.ensure_one() - return self.number - - def _get_reference_odoo_partner(self): - """ This computes the reference based on the Odoo format. - The data used is the reference set on the partner or its database - id otherwise. For instance if the reference of the customer is - 'dumb customer 97', the reference will be 'CUST/dumb customer 97'. - """ - ref = self.partner_id.ref or str(self.partner_id.id) - prefix = _('CUST') - return '%s/%s' % (prefix, ref) - - @api.multi - def _get_computed_reference(self): - self.ensure_one() - if self.journal_id.invoice_reference_type == 'none': - return '' - else: - ref_function = getattr(self, '_get_reference_{}_{}'.format(self.journal_id.invoice_reference_model, self.journal_id.invoice_reference_type)) - if ref_function: - return ref_function() - else: - raise UserError(_('The combination of reference model and reference type on the journal is not implemented')) - - # Load all Vendor Bill lines - @api.onchange('vendor_bill_id') - def _onchange_vendor_bill(self): - if not self.vendor_bill_id: - return {} - self.currency_id = self.vendor_bill_id.currency_id - new_lines = self.env['account.invoice.line'] - for line in self.vendor_bill_id.invoice_line_ids: - new_lines += new_lines.new(line._prepare_invoice_line()) - self.invoice_line_ids += new_lines - self.payment_term_id = self.vendor_bill_id.payment_term_id - self.vendor_bill_id = False - return {} - - def _get_seq_number_next_stuff(self): - self.ensure_one() - journal_sequence = self.journal_id.sequence_id - if self.journal_id.refund_sequence: - domain = [('type', '=', self.type)] - journal_sequence = self.type in ['in_refund', 'out_refund'] and self.journal_id.refund_sequence_id or self.journal_id.sequence_id - elif self.type in ['in_invoice', 'in_refund']: - domain = [('type', 'in', ['in_invoice', 'in_refund'])] - else: - domain = [('type', 'in', ['out_invoice', 'out_refund'])] - if self.id: - domain += [('id', '<>', self.id)] - domain += [('journal_id', '=', self.journal_id.id), ('state', 'not in', ['draft', 'cancel'])] - return journal_sequence, domain - - def _compute_access_url(self): - super(AccountInvoice, self)._compute_access_url() - for invoice in self: - invoice.access_url = '/my/invoices/%s' % (invoice.id) - - @api.depends('state', 'journal_id', 'date_invoice') - def _get_sequence_prefix(self): - """ computes the prefix of the number that will be assigned to the first invoice/bill/refund of a journal, in order to - let the user manually change it. - """ - if not self.env.user._is_system(): - for invoice in self: - invoice.sequence_number_next_prefix = False - invoice.sequence_number_next = '' - return - for invoice in self: - journal_sequence, domain = invoice._get_seq_number_next_stuff() - if (invoice.state == 'draft') and not self.search(domain, limit=1): - prefix, dummy = journal_sequence.with_context(ir_sequence_date=invoice.date_invoice, - ir_sequence_date_range=invoice.date_invoice)._get_prefix_suffix() - invoice.sequence_number_next_prefix = prefix - else: - invoice.sequence_number_next_prefix = False - - @api.depends('state', 'journal_id') - def _get_sequence_number_next(self): - """ computes the number that will be assigned to the first invoice/bill/refund of a journal, in order to - let the user manually change it. - """ - for invoice in self: - journal_sequence, domain = invoice._get_seq_number_next_stuff() - if (invoice.state == 'draft') and not self.search(domain, limit=1): - number_next = journal_sequence._get_current_sequence().number_next_actual - invoice.sequence_number_next = '%%0%sd' % journal_sequence.padding % number_next - else: - invoice.sequence_number_next = '' - - @api.multi - def _set_sequence_next(self): - ''' Set the number_next on the sequence related to the invoice/bill/refund''' - self.ensure_one() - journal_sequence, domain = self._get_seq_number_next_stuff() - if not self.env.user._is_admin() or not self.sequence_number_next or self.search_count(domain): - return - nxt = re.sub("[^0-9]", '', self.sequence_number_next) - result = re.match("(0*)([0-9]+)", nxt) - if result and journal_sequence: - # use _get_current_sequence to manage the date range sequences - sequence = journal_sequence._get_current_sequence() - sequence.number_next = int(result.group(2)) - - @api.multi - def _get_report_base_filename(self): - self.ensure_one() - return self.type == 'out_invoice' and self.state == 'draft' and _('Draft Invoice') or \ - self.type == 'out_invoice' and self.state in ('open','in_payment','paid') and _('Invoice - %s') % (self.number) or \ - self.type == 'out_refund' and self.state == 'draft' and _('Credit Note') or \ - self.type == 'out_refund' and _('Credit Note - %s') % (self.number) or \ - self.type == 'in_invoice' and self.state == 'draft' and _('Vendor Bill') or \ - self.type == 'in_invoice' and self.state in ('open','in_payment','paid') and _('Vendor Bill - %s') % (self.number) or \ - self.type == 'in_refund' and self.state == 'draft' and _('Vendor Credit Note') or \ - self.type == 'in_refund' and _('Vendor Credit Note - %s') % (self.number) - - @api.model - def create(self, vals): - if not vals.get('journal_id') and vals.get('type'): - vals['journal_id'] = self.with_context(type=vals.get('type'))._default_journal().id - - onchanges = self._get_onchange_create() - for onchange_method, changed_fields in onchanges.items(): - if any(f not in vals for f in changed_fields): - invoice = self.new(vals) - getattr(invoice, onchange_method)() - for field in changed_fields: - if field not in vals and invoice[field]: - vals[field] = invoice._fields[field].convert_to_write(invoice[field], invoice) - - invoice = super(AccountInvoice, self).create(vals) - - if any(line.invoice_line_tax_ids for line in invoice.invoice_line_ids) and not invoice.tax_line_ids: - invoice.compute_taxes() - - return invoice - - @api.constrains('partner_id', 'partner_bank_id') - def validate_partner_bank_id(self): - for record in self: - if record.partner_bank_id: - if record.type in ('in_invoice', 'out_refund') and record.partner_bank_id.partner_id != record.partner_id.commercial_partner_id: - raise ValidationError(_("Commercial partner and vendor account owners must be identical.")) - elif record.type in ('out_invoice', 'in_refund') and not record.company_id in record.partner_bank_id.partner_id.ref_company_ids: - raise ValidationError(_("The account selected for payment does not belong to the same company as this invoice.")) - - @api.multi - def _write(self, vals): - pre_not_reconciled = self.filtered(lambda invoice: not invoice.reconciled) - pre_reconciled = self - pre_not_reconciled - res = super(AccountInvoice, self)._write(vals) - reconciled = self.filtered(lambda invoice: invoice.reconciled) - not_reconciled = self - reconciled - (reconciled & pre_reconciled).filtered(lambda invoice: invoice.state == 'open').action_invoice_paid() - (not_reconciled & pre_not_reconciled).filtered(lambda invoice: invoice.state in ('in_payment', 'paid')).action_invoice_re_open() - return res - - @api.model - def default_get(self,default_fields): - """ Compute default partner_bank_id field for 'out_invoice' type, - using the default values computed for the other fields. - """ - res = super(AccountInvoice, self).default_get(default_fields) - - if res.get('type', False) not in ('out_invoice', 'in_refund') or not 'company_id' in res: - return res - - partner_bank_result = self._get_partner_bank_id(res['company_id']) - if partner_bank_result: - res['partner_bank_id'] = partner_bank_result.id - return res - - def _get_partner_bank_id(self, company_id): - company = self.env['res.company'].browse(company_id) - if company.partner_id: - return self.env['res.partner.bank'].search([('partner_id', '=', company.partner_id.id)], limit=1) - - @api.model - def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False): - def get_view_id(xid, name): - try: - return self.env.ref('account.' + xid) - except ValueError: - view = self.env['ir.ui.view'].search([('name', '=', name)], limit=1) - if not view: - return False - return view.id - - context = self._context - supplier_form_view_id = get_view_id('invoice_supplier_form', 'account.invoice.supplier.form').id - if context.get('active_model') == 'res.partner' and context.get('active_ids'): - partner = self.env['res.partner'].browse(context['active_ids'])[0] - if not view_type: - view_id = get_view_id('invoice_tree', 'account.invoice.tree') - view_type = 'tree' - elif view_type == 'form': - if partner.supplier and not partner.customer: - view_id = supplier_form_view_id - elif partner.customer and not partner.supplier: - view_id = get_view_id('invoice_form', 'account.invoice.form').id - - return super(AccountInvoice, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu) - - @api.multi - def invoice_print(self): - """ Print the invoice and mark it as sent, so that we can see more - easily the next step of the workflow - """ - self.filtered(lambda inv: not inv.sent).write({'sent': True}) - if self.user_has_groups('account.group_account_invoice'): - return self.env.ref('account.account_invoices').report_action(self) - else: - return self.env.ref('account.account_invoices_without_payment').report_action(self) - - @api.multi - def action_reconcile_to_check(self): - self.ensure_one() - domain = self._get_domain_edition_mode_available() - ids = self.env['account.move.line'].search(domain).mapped('statement_line_id').ids - action_context = {'show_mode_selector': False, 'company_ids': self.mapped('company_id').ids} - action_context.update({'edition_mode': True}) - action_context.update({'statement_line_ids': ids}) - action_context.update({'partner_id': self.partner_id.id}) - action_context.update({'partner_name': self.partner_id.name}) - return { - 'type': 'ir.actions.client', - 'tag': 'bank_statement_reconciliation_view', - 'context': action_context, - } - - @api.multi - def action_invoice_sent(self): - """ Open a window to compose an email, with the edi invoice template - message loaded by default - """ - self.ensure_one() - template = self.env.ref('account.email_template_edi_invoice', False) - compose_form = self.env.ref('account.account_invoice_send_wizard_form', False) - ctx = dict( - default_model='account.invoice', - default_res_id=self.id, - default_use_template=bool(template), - default_template_id=template and template.id or False, - default_composition_mode='comment', - mark_invoice_as_sent=True, - custom_layout="mail.mail_notification_paynow", - force_email=True - ) - return { - 'name': _('Send Invoice'), - 'type': 'ir.actions.act_window', - 'view_type': 'form', - 'view_mode': 'form', - 'res_model': 'account.invoice.send', - 'views': [(compose_form.id, 'form')], - 'view_id': compose_form.id, - 'target': 'new', - 'context': ctx, - } - - @api.multi - @api.returns('mail.message', lambda value: value.id) - def message_post(self, **kwargs): - if self.env.context.get('mark_invoice_as_sent'): - self.filtered(lambda inv: not inv.sent).write({'sent': True}) - self.env.company.set_onboarding_step_done('account_onboarding_sample_invoice_state') - return super(AccountInvoice, self.with_context(mail_post_autofollow=True)).message_post(**kwargs) - - @api.model - def message_new(self, msg_dict, custom_values=None): - """ Overrides mail_thread message_new(), called by the mailgateway through message_process, - to complete values for vendor bills created by mails. - """ - # Split `From` and `CC` email address from received email to look for related partners to subscribe on the invoice - subscribed_emails = email_split((msg_dict.get('from') or '') + ',' + (msg_dict.get('cc') or '')) - seen_partner_ids = [partner.id for partner in self.env['mail.thread']._mail_find_partner_from_emails(subscribed_emails, records=self) if partner] - - # Detection of the partner_id of the invoice: - # 1) check if the email_from correspond to a supplier - email_from = msg_dict.get('from') or '' - email_from = email_escape_char(email_split(email_from)[0]) - partners = self.env['mail.thread'].sudo()._mail_search_on_partner(email_from, extra_domain=[('supplier', '=', True)]).id - partner_id = partners.ids[0] if partners else False - - # 2) otherwise, if the email sender is from odoo internal users then it is likely that the vendor sent the bill - # by mail to the internal user who, inturn, forwarded that email to the alias to automatically generate the bill - # on behalf of the vendor. - if not partner_id: - user_partners = self.env['mail.thread'].sudo()._mail_search_on_user(email_from).id - user_partner_id = user_partners.ids[0] if user_partners else False - if user_partner_id and user_partner_id in self.env.ref('base.group_user').users.mapped('partner_id').ids: - # In this case, we will look for the vendor's email address in email's body and assume if will come first - email_addresses = email_re.findall(msg_dict.get('body')) - if email_addresses: - partner_ids = [partner.id for partner in self.env['mail.thread']._mail_find_partner_from_emails([email_addresses[0]], records=self, force_create=False) if partner] - partner_id = partner_ids and partner_ids[0] - # otherwise, there's no fallback on the partner_id found for the regular author of the mail.message as we want - # the partner_id to stay empty - - # If the partner_id can be found, subscribe it to the bill, otherwise it's left empty to be manually filled - if partner_id: - seen_partner_ids.append(partner_id) - - # Find the right purchase journal based on the "TO" email address - destination_emails = email_split((msg_dict.get('to') or '') + ',' + (msg_dict.get('cc') or '')) - alias_names = [mail_to.split('@')[0] for mail_to in destination_emails] - journal = self.env['account.journal'].search([ - ('type', '=', 'purchase'), ('alias_name', 'in', alias_names) - ], limit=1) - - # Create the message and the bill. - values = dict(custom_values or {}, partner_id=partner_id, source_email=email_from) - if journal: - values['journal_id'] = journal.id - # Passing `type` in context so that _default_journal(...) can correctly set journal for new vendor bill - invoice = super(AccountInvoice, self.with_context(type=values.get('type'))).message_new(msg_dict, values) - - # Subscribe internal users on the newly created bill - partners = self.env['res.partner'].browse(seen_partner_ids) - is_internal = lambda p: (p.user_ids and - all(p.user_ids.mapped(lambda u: u.has_group('base.group_user')))) - partners_to_subscribe = partners.filtered(is_internal) - if partners_to_subscribe: - invoice.message_subscribe([p.id for p in partners_to_subscribe]) - return invoice - - @api.model - def complete_empty_list_help(self): - # add help message about email alias in vendor bills empty lists - Journal = self.env['account.journal'] - journals = Journal.browse(self._context.get('default_journal_id')) or Journal.search([('type', '=', 'purchase')]) - - if journals: - links = '' - alias_count = 0 - for journal in journals.filtered(lambda j: j.alias_domain and j.alias_id.alias_name): - email = format(journal.alias_id.alias_name) + "@" + format(journal.alias_domain) - links += "{}".format(email, email) + ", " - alias_count += 1 - if links and alias_count == 1: - help_message = _('Or share the email %s to your vendors: bills will be created automatically upon mail reception.') % (links[:-2]) - elif links: - help_message = _('Or share the emails %s to your vendors: bills will be created automatically upon mail reception.') % (links[:-2]) - else: - help_message = _('''Or set an email alias ''' - '''to allow draft vendor bills to be created upon reception of an email.''') % (journals[0].id, journals[0].id) - else: - help_message = _('

You can control the invoice from your vendor based on what you purchased or received.

') - return help_message - - @api.multi - def compute_taxes(self): - """Function used in other module to compute the taxes on a fresh invoice created (onchanges did not applied)""" - account_invoice_tax = self.env['account.invoice.tax'] - ctx = dict(self._context) - for invoice in self: - # Delete non-manual tax lines - self._cr.execute("DELETE FROM account_invoice_tax WHERE invoice_id=%s AND manual is False", (invoice.id,)) - if self._cr.rowcount: - self.invalidate_cache() - - # Generate one tax line per tax, however many invoice lines it's applied to - tax_grouped = invoice.get_taxes_values() - - # Create new tax lines - for tax in tax_grouped.values(): - account_invoice_tax.create(tax) - - # dummy write on self to trigger recomputations - return self.with_context(ctx).write({'invoice_line_ids': []}) - - @api.multi - def unlink(self): - for invoice in self: - if invoice.state not in ('draft', 'cancel'): - raise UserError(_('You cannot delete an invoice which is not draft or cancelled. You should create a credit note instead.')) - elif invoice.move_name: - raise UserError(_('You cannot delete an invoice after it has been validated (and received a number). You can set it back to "Draft" state and modify its content, then re-confirm it.')) - return super(AccountInvoice, self).unlink() - - @api.onchange('invoice_line_ids') - def _onchange_invoice_line_ids(self): - taxes_grouped = self.get_taxes_values() - tax_lines = self.tax_line_ids.filtered('manual') - for tax in taxes_grouped.values(): - # ATTENTION: due to this, fields in tax have to be in the view (possibly invisible), as they won't be saved otherwise (when hitting "save") - tax_lines += tax_lines.new(tax) - self.tax_line_ids = tax_lines - return - - @api.onchange('partner_id', 'company_id') - def _onchange_partner_id(self): - account_id = False - payment_term_id = False - fiscal_position = False - bank_id = False - warning = {} - domain = {} - company_id = self.company_id.id - p = self.partner_id if not company_id else self.partner_id.with_context(force_company=company_id) - type = self.type or self.env.context.get('type', 'out_invoice') - if p: - rec_account = p.property_account_receivable_id - pay_account = p.property_account_payable_id - if not rec_account and not pay_account: - action = self.env.ref('account.action_account_config') - msg = _('Cannot find a chart of accounts for this company, You should configure it. \nPlease go to Account Configuration.') - raise RedirectWarning(msg, action.id, _('Go to the configuration panel')) - - if type in ('in_invoice', 'in_refund'): - account_id = pay_account.id - payment_term_id = p.property_supplier_payment_term_id.id - else: - account_id = rec_account.id - payment_term_id = p.property_payment_term_id.id - - delivery_partner_id = self.get_delivery_partner_id() - fiscal_position = self.env['account.fiscal.position'].get_fiscal_position(self.partner_id.id, delivery_id=delivery_partner_id) - - # If partner has no warning, check its company - if p.invoice_warn == 'no-message' and p.parent_id: - p = p.parent_id - if p.invoice_warn and p.invoice_warn != 'no-message': - # Block if partner only has warning but parent company is blocked - if p.invoice_warn != 'block' and p.parent_id and p.parent_id.invoice_warn == 'block': - p = p.parent_id - warning = { - 'title': _("Warning for %s") % p.name, - 'message': p.invoice_warn_msg - } - if p.invoice_warn == 'block': - self.partner_id = False - - self.account_id = account_id - self.payment_term_id = payment_term_id - self.date_due = False - self.fiscal_position_id = fiscal_position - - if type in ('in_invoice', 'out_refund'): - bank_ids = p.commercial_partner_id.bank_ids - bank_id = bank_ids[0].id if bank_ids else False - self.partner_bank_id = bank_id - domain = {'partner_bank_id': [('id', 'in', bank_ids.ids)]} - elif type == 'out_invoice': - domain = {'partner_bank_id': [('partner_id.ref_company_ids', 'in', [self.company_id.id])]} - - res = {} - if warning: - res['warning'] = warning - if domain: - res['domain'] = domain - return res - - @api.multi - def get_delivery_partner_id(self): - self.ensure_one() - return self.partner_id.address_get(['delivery'])['delivery'] - - @api.onchange('journal_id') - def _onchange_journal_id(self): - if self.journal_id and not self._context.get('default_currency_id'): - self.currency_id = self.journal_id.currency_id.id or self.journal_id.company_id.currency_id.id - - @api.onchange('payment_term_id', 'date_invoice') - def _onchange_payment_term_date_invoice(self): - date_invoice = self.date_invoice - if not date_invoice: - date_invoice = fields.Date.context_today(self) - if self.payment_term_id: - pterm = self.payment_term_id - pterm_list = pterm.with_context(currency_id=self.company_id.currency_id.id).compute(value=1, date_ref=date_invoice) - self.date_due = max(line[0] for line in pterm_list) - elif self.date_due and (date_invoice > self.date_due): - self.date_due = date_invoice - - @api.onchange('cash_rounding_id', 'invoice_line_ids', 'tax_line_ids') - def _onchange_cash_rounding(self): - # Drop previous cash rounding lines - lines_to_remove = self.invoice_line_ids.filtered(lambda l: l.is_rounding_line) - if lines_to_remove: - self.invoice_line_ids -= lines_to_remove - - # Clear previous rounded amounts - for tax_line in self.tax_line_ids: - if tax_line.amount_rounding != 0.0: - tax_line.amount_rounding = 0.0 - - if self.cash_rounding_id and self.type in ('out_invoice', 'out_refund'): - rounding_amount = self.cash_rounding_id.compute_difference(self.currency_id, self.amount_total) - if not self.currency_id.is_zero(rounding_amount): - if self.cash_rounding_id.strategy == 'biggest_tax': - # Search for the biggest tax line and add the rounding amount to it. - # If no tax found, an error will be raised by the _check_cash_rounding method. - if not self.tax_line_ids: - return - biggest_tax_line = None - for tax_line in self.tax_line_ids: - if not biggest_tax_line or tax_line.amount > biggest_tax_line.amount: - biggest_tax_line = tax_line - biggest_tax_line.amount_rounding += rounding_amount - elif self.cash_rounding_id.strategy == 'add_invoice_line': - # Create a new invoice line to perform the rounding - rounding_line = self.env['account.invoice.line'].new({ - 'name': self.cash_rounding_id.name, - 'invoice_id': self.id, - 'account_id': self.cash_rounding_id.account_id.id, - 'price_unit': rounding_amount, - 'quantity': 1, - 'is_rounding_line': True, - 'sequence': 9999 # always last line - }) - - # To be able to call this onchange manually from the tests, - # ensure the inverse field is updated on account.invoice. - if not rounding_line in self.invoice_line_ids: - self.invoice_line_ids += rounding_line - - @api.multi - def action_invoice_draft(self): - if self.filtered(lambda inv: inv.state != 'cancel'): - raise UserError(_("Invoice must be cancelled in order to reset it to draft.")) - # go from canceled state to draft state - self.write({'state': 'draft', 'date': False}) - # Delete former printed invoice - try: - report_invoice = self.env['ir.actions.report']._get_report_from_name('account.report_invoice') - except IndexError: - report_invoice = False - if report_invoice and report_invoice.attachment: - for invoice in self: - with invoice.env.do_in_draft(): - invoice.number, invoice.state = invoice.move_name, 'open' - attachment = self.env.ref('account.account_invoices').retrieve_attachment(invoice) - if attachment: - attachment.unlink() - return True - - @api.multi - def action_invoice_open(self): - # lots of duplicate calls to action_invoice_open, so we remove those already open - to_open_invoices = self.filtered(lambda inv: inv.state != 'open') - if to_open_invoices.filtered(lambda inv: not inv.partner_id): - raise UserError(_("The field Vendor/Customer is required, please complete it to validate the Vendor Bill/Customer Invoice.")) - if to_open_invoices.filtered(lambda inv: inv.state != 'draft'): - raise UserError(_("Invoice must be in draft state in order to validate it.")) - if to_open_invoices.filtered(lambda inv: float_compare(inv.amount_total, 0.0, precision_rounding=inv.currency_id.rounding) == -1): - raise UserError(_("You cannot validate an invoice with a negative total amount. You should create a credit note instead.")) - if to_open_invoices.filtered(lambda inv: not inv.account_id): - raise UserError(_('No account was found to create the invoice, be sure you have installed a chart of account.')) - to_open_invoices.action_date_assign() - to_open_invoices._set_name_and_date_invoice() - res = to_open_invoices.action_move_create() - to_open_invoices.invoice_validate() - return res - - @api.multi - def action_invoice_paid(self): - # lots of duplicate calls to action_invoice_paid, so we remove those already paid - to_pay_invoices = self.filtered(lambda inv: inv.state != 'paid') - if to_pay_invoices.filtered(lambda inv: inv.state not in ('open', 'in_payment')): - raise UserError(_('Invoice must be validated in order to set it to register payment.')) - if to_pay_invoices.filtered(lambda inv: not inv.reconciled): - raise UserError(_('You cannot pay an invoice which is partially paid. You need to reconcile payment entries first.')) - - for invoice in to_pay_invoices: - if any([move.journal_id.post_at_bank_rec and move.state == 'draft' for move in invoice.payment_move_line_ids.mapped('move_id')]): - invoice.write({'state': 'in_payment'}) - else: - invoice.write({'state': 'paid'}) - - @api.multi - def action_invoice_re_open(self): - if self.filtered(lambda inv: inv.state not in ('in_payment', 'paid')): - raise UserError(_('Invoice must be paid in order to set it to register payment.')) - return self.write({'state': 'open'}) - - @api.multi - def action_register_payment(self): - return self.env['account.payment'].with_context(active_ids=self.ids, active_model='account.invoice', active_id=self.id).action_register_payment() - - @api.multi - def action_invoice_cancel(self): - return self.filtered(lambda inv: inv.state != 'cancel').action_cancel() - - @api.multi - def _notify_get_groups(self): - """ Give access button to users and portal customer as portal is integrated - in account. Customer and portal group have probably no right to see - the document so they don't have the access button. """ - groups = super(AccountInvoice, self)._notify_get_groups() - - if self.state not in ('draft', 'cancel'): - for group_name, group_method, group_data in groups: - if group_name not in ('customer', 'portal'): - group_data['has_button_access'] = True - - return groups - - @api.multi - def get_formview_id(self, access_uid=None): - """ Update form view id of action to open the invoice """ - if self.type in ('in_invoice', 'in_refund'): - return self.env.ref('account.invoice_supplier_form').id - else: - return self.env.ref('account.invoice_form').id - - def _prepare_tax_line_vals(self, line, tax): - ''' Prepare values to create an account.invoice.tax line. - :param line: An account.invoice.line record. - :param tax: Tax values outputted by compute_all() in account.tax. - :param taxes: A list of account.tax ids affecting the tax base amount. - :return: The account.invoice.tax values to create a new record. - ''' - vals = { - 'invoice_id': self.id, - 'name': tax['name'], - 'tax_id': tax['id'], - 'amount': tax['amount'], - 'base': tax['base'], - 'manual': False, - 'sequence': tax['sequence'], - 'account_analytic_id': tax['analytic'] and line.account_analytic_id.id or False, - 'account_id': tax['account_id'] or line.account_id.id, - 'analytic_tag_ids': tax['analytic'] and line.analytic_tag_ids.ids or False, - 'tax_repartition_line_id': tax.get('tax_repartition_line_id'), # For base amount, we let this field empty - 'tag_ids': tax['tag_ids'], - 'tax_ids': tax['tax_ids'], - } - - # If the taxes generate moves on the same financial account as the invoice line, - # propagate the analytic account from the invoice line to the tax line. - # This is necessary in situations were (part of) the taxes cannot be reclaimed, - # to ensure the tax move is allocated to the proper analytic account. - if not vals.get('account_analytic_id') and line.account_analytic_id and vals['account_id'] == line.account_id.id: - vals['account_analytic_id'] = line.account_analytic_id.id - return vals - - @api.multi - def get_taxes_values(self, tax_group_fields=False): - def is_tax_affecting_base_amount(tax): - return (tax.amount_type not in ('group', 'division') and tax.include_base_amount and tax.price_include)\ - or (tax.amount_type == 'division' and tax.include_base_amount and not tax.price_include) - # Avoid redundant browsing. - tax_map = dict((t.id, t) for t in self.invoice_line_ids.invoice_line_tax_ids._origin) - default_tax_group_fields = set(['amount', 'base']) - if tax_group_fields: - default_tax_group_fields |= set(tax_group_fields) - tax_grouped = {} - round_curr = self.currency_id.round - for line in self.invoice_line_ids: - if not line.account_id: - continue - - price_unit = line.price_unit * (1 - (line.discount or 0.0) / 100.0) - taxes = line.invoice_line_tax_ids._origin.compute_all(price_unit, self.currency_id, line.quantity, line.product_id, self.partner_id, is_refund=self.type in ('in_refund', 'out_refund'))['taxes'] - - for tax_vals in taxes: - # Retrieve the tax record (not in tax_vals when dealing with group of taxes). - if tax_map.get(tax_vals['id']): - tax = tax_map[tax_vals['id']] - else: - tax = tax_map[tax_vals['id']] = self.env['account.tax'].browse(tax_vals['id']) - - val = self._prepare_tax_line_vals(line, tax_vals) - key = tax.get_grouping_key(val) - - if key not in tax_grouped: - tax_grouped[key] = val - tax_grouped[key]['base'] = round_curr(val['base']) - else: - for field in default_tax_group_fields: - tax_grouped[key][field] += round_curr(val.get(field)) or 0 - - return tax_grouped - - @api.multi - def _get_aml_for_register_payment(self): - """ Get the aml to consider to reconcile in register payment """ - self.ensure_one() - return self.move_id.line_ids.filtered(lambda r: not r.reconciled and r.account_id.internal_type in ('payable', 'receivable')) - - @api.multi - def register_payment(self, payment_line, writeoff_acc_id=False, writeoff_journal_id=False): - """ Reconcile payable/receivable lines from the invoice with payment_line """ - line_to_reconcile = self.env['account.move.line'] - for inv in self: - line_to_reconcile += inv._get_aml_for_register_payment() - return (line_to_reconcile + payment_line).reconcile(writeoff_acc_id, writeoff_journal_id) - - @api.multi - def assign_outstanding_credit(self, credit_aml_id): - self.ensure_one() - credit_aml = self.env['account.move.line'].browse(credit_aml_id) - if not credit_aml.currency_id and self.currency_id != self.company_id.currency_id: - amount_currency = self.company_id.currency_id._convert(credit_aml.balance, self.currency_id, self.company_id, credit_aml.date or fields.Date.today()) - credit_aml.with_context(allow_amount_currency=True, check_move_validity=False).write({ - 'amount_currency': amount_currency, - 'currency_id': self.currency_id.id}) - if credit_aml.payment_id: - credit_aml.payment_id.write({'invoice_ids': [(4, self.id, None)]}) - return self.register_payment(credit_aml) - - @api.multi - def action_date_assign(self): - for inv in self: - # Here the onchange will automatically write to the database - inv._onchange_payment_term_date_invoice() - return True - - @api.multi - def finalize_invoice_move_lines(self, move_lines): - """ finalize_invoice_move_lines(move_lines) -> move_lines - - Hook method to be overridden in additional modules to verify and - possibly alter the move lines to be created by an invoice, for - special cases. - :param move_lines: list of dictionaries with the account.move.lines (as for create()) - :return: the (possibly updated) final move_lines to create for this invoice - """ - return move_lines - - @api.multi - def compute_invoice_totals(self, company_currency, invoice_move_lines): - total = 0 - total_currency = 0 - for line in invoice_move_lines: - if self.currency_id != company_currency: - currency = self.currency_id - date = self._get_currency_rate_date() or fields.Date.context_today(self) - if not (line.get('currency_id') and line.get('amount_currency')): - line['currency_id'] = currency.id - line['amount_currency'] = currency.round(line['price']) - line['price'] = currency._convert(line['price'], company_currency, self.company_id, date) - else: - line['currency_id'] = False - line['amount_currency'] = False - line['price'] = self.currency_id.round(line['price']) - if self.type in ('out_invoice', 'in_refund'): - total += line['price'] - total_currency += line['amount_currency'] or line['price'] - line['price'] = - line['price'] - else: - total -= line['price'] - total_currency -= line['amount_currency'] or line['price'] - return total, total_currency, invoice_move_lines - - @api.model - def invoice_line_move_line_get(self): - res = [] - for line in self.invoice_line_ids: - if not line.account_id: - continue - if line.quantity==0: - continue - - taxes_to_flatten = line.invoice_line_tax_ids - flattened_taxes = self.env['account.tax'] - while taxes_to_flatten: - tax = taxes_to_flatten[0] - taxes_to_flatten -= tax - flattened_taxes += tax - taxes_to_flatten += tax.children_tax_ids - - analytic_tag_ids = [(4, analytic_tag.id, None) for analytic_tag in line.analytic_tag_ids] - - tax_repartition_field_name = 'invoice_repartition_line_ids' if line.invoice_type in ('in_invoice', 'out_invoice') else 'refund_repartition_line_ids' - tag_ids = flattened_taxes.mapped(tax_repartition_field_name).filtered(lambda x: x.repartition_type == 'base').mapped('tag_ids.id') - move_line_dict = { - 'invl_id': line.id, - 'type': 'src', - 'name': line.name, - 'price_unit': line.price_unit, - 'quantity': line.quantity, - 'price': line.price_subtotal, - 'account_id': line.account_id.id, - 'product_id': line.product_id.id, - 'uom_id': line.uom_id.id, - 'account_analytic_id': line.account_analytic_id.id, - 'analytic_tag_ids': analytic_tag_ids, - 'tax_ids': [(6, 0, flattened_taxes.ids)], - 'invoice_id': self.id, - 'tag_ids': [(6, 0, tag_ids)], - } - res.append(move_line_dict) - return res - - def tax_line_move_line_get(self): - self.ensure_one() - res = [] - # loop the invoice.tax.line in reversal sequence - for tax_line in sorted(self.tax_line_ids, key=lambda x: -x.sequence): - if tax_line.amount_total: - analytic_tag_ids = [(4, analytic_tag.id, None) for analytic_tag in tax_line.analytic_tag_ids] - res.append({ - 'invoice_tax_line_id': tax_line.id, - 'tax_line_id': tax_line.tax_id.id, - 'type': 'tax', - 'name': tax_line.name, - 'price_unit': tax_line.amount_total, - 'quantity': 1, - 'price': tax_line.amount_total, - 'account_id': tax_line.account_id.id, - 'account_analytic_id': tax_line.account_analytic_id.id, - 'analytic_tag_ids': analytic_tag_ids, - 'invoice_id': self.id, - 'tax_ids': tax_line.tax_ids and [(6, 0, tax_line.tax_ids.ids)] or False, # We don't pass an empty recordset here, as it would reset the tax_exibility of the line, due to the condition in account.move.line's create - 'tax_repartition_line_id': tax_line.tax_repartition_line_id.id, - 'tag_ids': [(6, 0, tax_line.tag_ids.ids)], - 'tax_base_amount': tax_line.base, - }) - return res - - def inv_line_characteristic_hashcode(self, invoice_line): - """Overridable hashcode generation for invoice lines. Lines having the same hashcode - will be grouped together if the journal has the 'group line' option. Of course a module - can add fields to invoice lines that would need to be tested too before merging lines - or not.""" - return "%s-%s-%s-%s-%s-%s-%s" % ( - invoice_line['account_id'], - invoice_line.get('tax_ids', 'False'), - invoice_line.get('tax_line_id', 'False'), - invoice_line.get('product_id', 'False'), - invoice_line.get('analytic_account_id', 'False'), - invoice_line.get('date_maturity', 'False'), - invoice_line.get('analytic_tag_ids', 'False'), - ) - - def group_lines(self, iml, line): - """Merge account move lines (and hence analytic lines) if invoice line hashcodes are equals""" - if self.journal_id.group_invoice_lines: - line2 = {} - for x, y, l in line: - tmp = self.inv_line_characteristic_hashcode(l) - if tmp in line2: - am = line2[tmp]['debit'] - line2[tmp]['credit'] + (l['debit'] - l['credit']) - line2[tmp]['debit'] = (am > 0) and am or 0.0 - line2[tmp]['credit'] = (am < 0) and -am or 0.0 - line2[tmp]['amount_currency'] += l['amount_currency'] - line2[tmp]['analytic_line_ids'] += l['analytic_line_ids'] - qty = l.get('quantity') - if qty: - line2[tmp]['quantity'] = line2[tmp].get('quantity', 0.0) + qty - else: - line2[tmp] = l - line = [] - for key, val in line2.items(): - line.append((0, 0, val)) - return line - - @api.multi - def action_move_create(self): - """ Creates invoice related analytics and financial move lines """ - account_move = self.env['account.move'] - - for inv in self: - if not inv.journal_id.sequence_id: - raise UserError(_('Please define sequence on the journal related to this invoice.')) - if not inv.invoice_line_ids.filtered(lambda line: line.account_id): - raise UserError(_('Please add at least one invoice line.')) - if inv.move_id: - continue - company_currency = inv.company_id.currency_id - - # create move lines (one per invoice line + eventual taxes and analytic lines) - iml = inv.invoice_line_move_line_get() - iml += inv.tax_line_move_line_get() - - diff_currency = inv.currency_id != company_currency - # create one move line for the total and possibly adjust the other lines amount - total, total_currency, iml = inv.compute_invoice_totals(company_currency, iml) - - name = inv.name or '' - if inv.payment_term_id: - totlines = inv.payment_term_id.with_context(currency_id=company_currency.id).compute(total, inv.date_invoice) - res_amount_currency = total_currency - for i, t in enumerate(totlines): - if inv.currency_id != company_currency: - amount_currency = company_currency._convert(t[1], inv.currency_id, inv.company_id, inv._get_currency_rate_date() or fields.Date.today()) - else: - amount_currency = False - - # last line: add the diff - res_amount_currency -= amount_currency or 0 - if i + 1 == len(totlines): - amount_currency += res_amount_currency - - iml.append({ - 'type': 'dest', - 'name': name, - 'price': t[1], - 'account_id': inv.account_id.id, - 'date_maturity': t[0], - 'amount_currency': diff_currency and amount_currency, - 'currency_id': diff_currency and inv.currency_id.id, - 'invoice_id': inv.id - }) - else: - iml.append({ - 'type': 'dest', - 'name': name, - 'price': total, - 'account_id': inv.account_id.id, - 'date_maturity': inv.date_due, - 'amount_currency': diff_currency and total_currency, - 'currency_id': diff_currency and inv.currency_id.id, - 'invoice_id': inv.id - }) - part = self.env['res.partner']._find_accounting_partner(inv.partner_id) - line = [(0, 0, self.line_get_convert(l, part.id)) for l in iml] - line = inv.group_lines(iml, line) - - line = inv.finalize_invoice_move_lines(line) - - date = inv.date or inv.date_invoice - move_ref = inv.reference - if inv.origin: - if move_ref: - move_ref += ' (%s)' % inv.origin - else: - move_ref = inv.origin - move_vals = { - 'ref': move_ref, - 'line_ids': line, - 'journal_id': inv.journal_id.id, - 'date': date, - 'narration': inv.comment, - 'name': inv.number, - } - move = account_move.create(move_vals) - move.post() - # make the invoice point to that move - vals = { - 'move_id': move.id, - 'date': date, - 'move_name': move.name, - } - inv.write(vals) - return True - - @api.constrains('cash_rounding_id', 'tax_line_ids') - def _check_cash_rounding(self): - for inv in self: - if inv.cash_rounding_id: - rounding_amount = inv.cash_rounding_id.compute_difference(inv.currency_id, inv.amount_total) - if rounding_amount != 0.0: - raise UserError(_('The cash rounding cannot be computed because the difference must ' - 'be added on the biggest tax found and no tax are specified.\n' - 'Please set up a tax or change the cash rounding method.')) - - @api.multi - def _check_duplicate_supplier_reference(self): - for invoice in self: - # refuse to validate a vendor bill/credit note if there already exists one with the same reference for the same partner, - # because it's probably a double encoding of the same bill/credit note - if invoice.type in ('in_invoice', 'in_refund') and invoice.reference: - if self.search([('type', '=', invoice.type), ('reference', '=', invoice.reference), ('company_id', '=', invoice.company_id.id), ('commercial_partner_id', '=', invoice.commercial_partner_id.id), ('id', '!=', invoice.id)]): - raise UserError(_("Duplicated vendor reference detected. You probably encoded twice the same vendor bill/credit note.")) - - @api.multi - def invoice_validate(self): - for invoice in self.filtered(lambda invoice: invoice.partner_id not in invoice.message_partner_ids): - invoice.message_subscribe([invoice.partner_id.id]) - - for invoice in self: - vals = {'state': 'open'} - if not invoice.date_due: - vals['date_due'] = vals.get('date_invoice', invoice.date_invoice) - - - # Auto-compute reference, if not already existing and if configured on company - if not invoice.reference and invoice.type == 'out_invoice': - vals['reference'] = invoice._get_computed_reference() - - invoice.write(vals) - - self._check_duplicate_supplier_reference() - return True - - @api.model - def line_get_convert(self, line, part): - return self.env['product.product']._convert_prepared_anglosaxon_line(line, part) - - @api.multi - def action_cancel(self): - moves = self.env['account.move'] - for inv in self: - if inv.move_id: - moves += inv.move_id - #unreconcile all journal items of the invoice, since the cancellation will unlink them anyway - inv.move_id.line_ids.filtered(lambda x: x.account_id.reconcile).remove_move_reconcile() - - # First, set the invoices as cancelled and detach the move ids - self.write({'state': 'cancel', 'move_id': False}) - if moves: - # second, invalidate the move(s) - moves.button_cancel() - # delete the move this invoice was pointing to - # Note that the corresponding move_lines and move_reconciles - # will be automatically deleted too - moves.unlink() - return True - - ################### - - @api.multi - def name_get(self): - TYPES = { - 'out_invoice': _('Invoice'), - 'in_invoice': _('Vendor Bill'), - 'out_refund': _('Credit Note'), - 'in_refund': _('Vendor Credit note'), - } - result = [] - for inv in self: - result.append((inv.id, "%s %s" % (inv.number or TYPES[inv.type], inv.name or ''))) - return result - - @api.model - def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): - args = args or [] - invoice_ids = [] - if name: - invoice_ids = self._search([('number', '=', name)] + args, limit=limit, access_rights_uid=name_get_uid) - if not invoice_ids: - invoice_ids = self._search([('name', operator, name)] + args, limit=limit, access_rights_uid=name_get_uid) - return self.browse(invoice_ids).name_get() - - @api.model - def _refund_cleanup_lines(self, lines): - """ Convert records to dict of values suitable for one2many line creation - - :param recordset lines: records to convert - :return: list of command tuple for one2many line creation [(0, 0, dict of valueis), ...] - """ - result = [] - for line in lines: - values = {} - for name, field in line._fields.items(): - if name in MAGIC_COLUMNS: - continue - elif field.type == 'many2one': - values[name] = line[name].id - elif field.type not in ['many2many', 'one2many']: - values[name] = line[name] - elif name == 'invoice_line_tax_ids': - values[name] = [(6, 0, line[name].ids)] - elif name == 'analytic_tag_ids': - values[name] = [(6, 0, line[name].ids)] - result.append((0, 0, values)) - return result - - def _get_refund_common_fields(self): - return ['partner_id', 'payment_term_id', 'account_id', 'currency_id', 'journal_id'] - - @api.model - def _get_refund_prepare_fields(self): - return ['name', 'reference', 'comment', 'date_due'] - - @api.model - def _get_refund_modify_read_fields(self): - read_fields = ['type', 'number', 'invoice_line_ids', 'tax_line_ids', - 'date'] - return self._get_refund_common_fields() + self._get_refund_prepare_fields() + read_fields - - @api.model - def _get_refund_copy_fields(self): - copy_fields = ['company_id', 'user_id', 'fiscal_position_id'] - return self._get_refund_common_fields() + self._get_refund_prepare_fields() + copy_fields - - def _get_currency_rate_date(self): - return self.date or self.date_invoice - - @api.model - def _create_refund_repartition_mapping(self, taxes): - """ Creates a mapping between tax and refund repartition lines of the - provided taxes, using the sequence of repartition lines to match them. - This function is used in order to fix account.invoice.tax objects generated - for refunds when tax amounts have been modified manually. - - :return: A dictionnary, with invoice repartition line ids as keys, and refund - repartition lines as values - """ - rslt = {} - for tax in taxes: - index = 0 - while(index < len(tax.invoice_repartition_line_ids)): - # _validate_repartition_lines constraint on taxes ensure invoice and refund repartition are equal, and in the same order - inv_rep_ln = tax.invoice_repartition_line_ids[index] - ref_rep_ln = tax.refund_repartition_line_ids[index] - if inv_rep_ln.repartition_type == 'tax': - rslt[inv_rep_ln.id] = ref_rep_ln - index += 1 - - return rslt - - @api.model - def _prepare_refund(self, invoice, date_invoice=None, date=None, description=None, journal_id=None): - """ Prepare the dict of values to create the new credit note from the invoice. - This method may be overridden to implement custom - credit note generation (making sure to call super() to establish - a clean extension chain). - - :param record invoice: invoice as credit note - :param string date_invoice: credit note creation date from the wizard - :param integer date: force date from the wizard - :param string description: description of the credit note from the wizard - :param integer journal_id: account.journal from the wizard - :return: dict of value to create() the credit note - """ - values = {} - for field in self._get_refund_copy_fields(): - if invoice._fields[field].type == 'many2one': - values[field] = invoice[field].id - else: - values[field] = invoice[field] or False - - values['invoice_line_ids'] = self._refund_cleanup_lines(invoice.invoice_line_ids) - - if journal_id: - journal = self.env['account.journal'].browse(journal_id) - elif invoice['type'] == 'in_invoice': - journal = self.env['account.journal'].search([('type', '=', 'purchase')], limit=1) - else: - journal = self.env['account.journal'].search([('type', '=', 'sale')], limit=1) - values['journal_id'] = journal.id - - values['type'] = TYPE2REFUND[invoice['type']] - values['date_invoice'] = date_invoice or fields.Date.context_today(invoice) - values['state'] = 'draft' - values['number'] = False - values['origin'] = invoice.number - values['payment_term_id'] = False - values['refund_invoice_id'] = invoice.id - - if values['type'] == 'in_refund': - partner_bank_result = self._get_partner_bank_id(values['company_id']) - if partner_bank_result: - values['partner_bank_id'] = partner_bank_result.id - - if date: - values['date'] = date - if description: - values['name'] = description - - # Treat refund tax lines. - # We copy them from the invoice and replace their account_id and - # tag_ids based on the refund repartition of the corresponding taxes. - tax_rep_ln_mapping = {} - for invoice_tax_entry in invoice.tax_line_ids: - if not invoice_tax_entry.tax_repartition_line_id.id in tax_rep_ln_mapping: - tax_rep_ln_mapping[invoice_tax_entry.tax_id.id] = self._create_refund_repartition_mapping(invoice_tax_entry.tax_id) - - tax_line_vals = [] - for invoice_tax_entry in invoice.tax_line_ids: - ref_rep_ln = tax_rep_ln_mapping[invoice_tax_entry.tax_id.id][invoice_tax_entry.tax_repartition_line_id.id] - tax_line_vals.append({ - 'name': invoice_tax_entry.name, - 'tax_id': invoice_tax_entry.tax_id.id, - 'tax_repartition_line_id': ref_rep_ln.id, - 'account_id': ref_rep_ln.account_id.id or invoice_tax_entry.account_id.id, # If the refund repartition line has no account set, we use the one from the original invoice - 'account_analytic_id': invoice_tax_entry.account_analytic_id.id, - 'analytic_tag_ids': [(6, 0, invoice_tax_entry.analytic_tag_ids.ids)], - 'amount': invoice_tax_entry.amount, - 'amount_rounding': invoice_tax_entry.amount_rounding, - 'manual': invoice_tax_entry.manual, - 'sequence': invoice_tax_entry.sequence, - 'base': invoice_tax_entry.base, - 'tax_ids': [(6, 0, invoice_tax_entry.tax_ids.ids)], - 'tag_ids': [(6, 0, ref_rep_ln.tag_ids.ids)], - }) - values['tax_line_ids'] = [(0, 0, tax_line_val) for tax_line_val in tax_line_vals] - - return values - - @api.multi - @api.returns('self') - def refund(self, date_invoice=None, date=None, description=None, journal_id=None): - new_invoices = self.browse() - for invoice in self: - # create the new invoice - values = self._prepare_refund(invoice, date_invoice=date_invoice, date=date, - description=description, journal_id=journal_id) - refund_invoice = self.create(values) - if invoice.type == 'out_invoice': - message = _("This customer invoice credit note has been created from: %s
Reason: %s") % (invoice.id, invoice.number, description) - else: - message = _("This vendor bill credit note has been created from: %s
Reason: %s") % (invoice.id, invoice.number, description) - - refund_invoice.message_post(body=message) - new_invoices += refund_invoice - return new_invoices - - def _prepare_payment_vals(self, pay_journal, pay_amount=None, date=None, writeoff_acc=None, communication=None): - payment_type = self.type in ('out_invoice', 'in_refund') and 'inbound' or 'outbound' - if payment_type == 'inbound': - payment_method = self.env.ref('account.account_payment_method_manual_in') - journal_payment_methods = pay_journal.inbound_payment_method_ids - else: - payment_method = self.env.ref('account.account_payment_method_manual_out') - journal_payment_methods = pay_journal.outbound_payment_method_ids - - if not communication: - communication = self.type in ('in_invoice', 'in_refund') and self.reference or self.number - if self.origin: - communication = '%s (%s)' % (communication, self.origin) - - payment_vals = { - 'invoice_ids': [(6, 0, self.ids)], - 'amount': pay_amount or self.residual, - 'payment_date': date or fields.Date.context_today(self), - 'communication': communication, - 'partner_id': self.partner_id.id, - 'partner_type': self.type in ('out_invoice', 'out_refund') and 'customer' or 'supplier', - 'journal_id': pay_journal.id, - 'payment_type': payment_type, - 'payment_method_id': payment_method.id, - 'payment_difference_handling': writeoff_acc and 'reconcile' or 'open', - 'writeoff_account_id': writeoff_acc and writeoff_acc.id or False, - } - return payment_vals - - @api.multi - def pay_and_reconcile(self, pay_journal, pay_amount=None, date=None, writeoff_acc=None): - """ Create and post an account.payment for the invoice self, which creates a journal entry that reconciles the invoice. - - :param pay_journal: journal in which the payment entry will be created - :param pay_amount: amount of the payment to register, defaults to the residual of the invoice - :param date: payment date, defaults to fields.Date.context_today(self) - :param writeoff_acc: account in which to create a writeoff if pay_amount < self.residual, so that the invoice is fully paid - """ - if isinstance(pay_journal, int): - pay_journal = self.env['account.journal'].browse([pay_journal]) - assert len(self) == 1, "Can only pay one invoice at a time." - - payment_vals = self._prepare_payment_vals(pay_journal, pay_amount=pay_amount, date=date, writeoff_acc=writeoff_acc) - payment = self.env['account.payment'].create(payment_vals) - payment.post() - - return True - - @api.multi - def _creation_subtype(self): - if self.type in ('out_invoice', 'out_refund'): - return self.env.ref('account.mt_invoice_created') - return super(AccountInvoice, self)._creation_subtype() - - @api.multi - def _track_subtype(self, init_values): - self.ensure_one() - if 'state' in init_values and self.state == 'paid' and self.type in ('out_invoice', 'out_refund'): - return self.env.ref('account.mt_invoice_paid') - elif 'state' in init_values and self.state == 'open' and self.type in ('out_invoice', 'out_refund'): - return self.env.ref('account.mt_invoice_validated') - return super(AccountInvoice, self)._track_subtype(init_values) - - def _amount_by_group(self): - for invoice in self: - currency = invoice.currency_id or invoice.company_id.currency_id - fmt = partial(formatLang, invoice.with_context(lang=invoice.partner_id.lang).env, currency_obj=currency) - res = {} - for line in invoice.tax_line_ids: - tax = line.tax_id - group_key = (tax.tax_group_id, tax.amount_type, tax.amount) - res.setdefault(group_key, {'base': 0.0, 'amount': 0.0}) - res[group_key]['amount'] += line.amount_total - res[group_key]['base'] += line.base - res = sorted(res.items(), key=lambda l: l[0][0].sequence) - invoice.amount_by_group = [( - r[0][0].name, r[1]['amount'], r[1]['base'], - fmt(r[1]['amount']), fmt(r[1]['base']), - len(res), - ) for r in res] - - @api.multi - def preview_invoice(self): - self.ensure_one() - return { - 'type': 'ir.actions.act_url', - 'target': 'self', - 'url': self.get_portal_url(), - } - - def _get_intrastat_country_id(self): - return self.partner_id.country_id.id - - def _get_onchange_create(self): - return OrderedDict([ - ('_onchange_partner_id', ['account_id', 'payment_term_id', 'fiscal_position_id', 'partner_bank_id']), - ('_onchange_journal_id', ['currency_id']), - ]) - - def _set_name_and_date_invoice(self): - for invoice in self: - if (invoice.move_name and invoice.move_name != '/'): - new_name = invoice.move_name - else: - new_name = False - journal = invoice.journal_id - if journal.sequence_id: - # If invoice is actually refund and journal has a refund_sequence then use that one or use the regular one - sequence = journal.sequence_id - if invoice.type in ['out_refund', 'in_refund'] and journal.refund_sequence: - if not journal.refund_sequence_id: - raise UserError(_('Please define a sequence for the credit notes')) - sequence = journal.refund_sequence_id - - new_name = sequence.with_context(ir_sequence_date=invoice.date or invoice.date_invoice).next_by_id() - else: - raise UserError(_('Please define a sequence on the journal.')) - #give the invoice its number directly as it's needed in _get_computed_reference() - invoice.number = new_name - if not invoice.date_invoice: - invoice.date_invoice = fields.Date.context_today(self) - - -class AccountInvoiceLine(models.Model): - _name = "account.invoice.line" - _description = "Invoice Line" - _order = "invoice_id,sequence,id" - - @api.one - @api.depends('price_unit', 'discount', 'invoice_line_tax_ids', 'quantity', - 'product_id', 'invoice_id.partner_id', 'invoice_id.currency_id', 'invoice_id.company_id', - 'invoice_id.date_invoice', 'invoice_id.date') - def _compute_price(self): - currency = self.invoice_id and self.invoice_id.currency_id or None - price = self.price_unit * (1 - (self.discount or 0.0) / 100.0) - taxes = False - if self.invoice_line_tax_ids: - taxes = self.invoice_line_tax_ids.compute_all(price, currency, self.quantity, product=self.product_id, partner=self.invoice_id.partner_id, is_refund=self.invoice_id.type in ('in_refund', 'out_refund')) - self.price_subtotal = price_subtotal_signed = taxes['total_excluded'] if taxes else self.quantity * price - self.price_total = taxes['total_included'] if taxes else self.price_subtotal - if self.invoice_id.currency_id and self.invoice_id.currency_id != self.invoice_id.company_id.currency_id: - currency = self.invoice_id.currency_id - date = self.invoice_id._get_currency_rate_date() - price_subtotal_signed = currency._convert(price_subtotal_signed, self.invoice_id.company_id.currency_id, self.company_id or self.env.company, date or fields.Date.today()) - sign = self.invoice_id.type in ['in_refund', 'out_refund'] and -1 or 1 - self.price_subtotal_signed = price_subtotal_signed * sign - - @api.model - def _default_account(self): - if self._context.get('journal_id'): - journal = self.env['account.journal'].browse(self._context.get('journal_id')) - if self._context.get('type') in ('out_invoice', 'in_refund'): - return journal.default_credit_account_id.id - return journal.default_debit_account_id.id - - def _get_price_tax(self): - for l in self: - l.price_tax = l.price_total - l.price_subtotal - - name = fields.Text(string='Description', required=True) - origin = fields.Char(string='Source Document', - help="Reference of the document that produced this invoice.") - sequence = fields.Integer(default=10, - help="Gives the sequence of this line when displaying the invoice.") - invoice_id = fields.Many2one('account.invoice', string='Invoice Reference', - ondelete='cascade', index=True) - invoice_type = fields.Selection(related='invoice_id.type', readonly=True) - uom_id = fields.Many2one('uom.uom', string='Unit of Measure', - ondelete='set null', index=True, oldname='uos_id') - product_id = fields.Many2one('product.product', string='Product', - ondelete='restrict', index=True) - account_id = fields.Many2one('account.account', string='Account', domain=[('deprecated', '=', False)], - default=_default_account, - help="The income or expense account related to the selected product.") - price_unit = fields.Float(string='Unit Price', required=True, digits=dp.get_precision('Product Price')) - price_subtotal = fields.Monetary(string='Amount (without Taxes)', - store=True, readonly=True, compute='_compute_price', help="Total amount without taxes") - price_total = fields.Monetary(string='Amount (with Taxes)', - store=True, readonly=True, compute='_compute_price', help="Total amount with taxes") - price_subtotal_signed = fields.Monetary(string='Amount Signed', currency_field='company_currency_id', - store=True, readonly=True, compute='_compute_price', - help="Total amount in the currency of the company, negative for credit note.") - price_tax = fields.Monetary(string='Tax Amount', compute='_get_price_tax', store=False) - quantity = fields.Float(string='Quantity', digits=dp.get_precision('Product Unit of Measure'), - required=True, default=1) - discount = fields.Float(string='Discount (%)', digits=dp.get_precision('Discount'), - default=0.0) - invoice_line_tax_ids = fields.Many2many('account.tax', - 'account_invoice_line_tax', 'invoice_line_id', 'tax_id', - string='Taxes', domain=[('type_tax_use','!=','none'), '|', ('active', '=', False), ('active', '=', True)], oldname='invoice_line_tax_id') - account_analytic_id = fields.Many2one('account.analytic.account', - string='Analytic Account') - analytic_tag_ids = fields.Many2many('account.analytic.tag', string='Analytic Tags') - company_id = fields.Many2one('res.company', string='Company', - related='invoice_id.company_id', store=True, readonly=True, related_sudo=False) - partner_id = fields.Many2one('res.partner', string='Partner', - related='invoice_id.partner_id', store=True, readonly=True, related_sudo=False) - currency_id = fields.Many2one('res.currency', related='invoice_id.currency_id', store=True, related_sudo=False, readonly=False) - company_currency_id = fields.Many2one('res.currency', related='invoice_id.company_currency_id', readonly=True, related_sudo=False) - is_rounding_line = fields.Boolean(string='Rounding Line', help='Is a rounding line in case of cash rounding.') - - display_type = fields.Selection([ - ('line_section', "Section"), - ('line_note', "Note")], default=False, help="Technical field for UX purpose.") - - @api.model - def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False): - res = super(AccountInvoiceLine, self).fields_view_get( - view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu) - if self._context.get('type'): - doc = etree.XML(res['arch']) - for node in doc.xpath("//field[@name='product_id']"): - if self._context['type'] in ('in_invoice', 'in_refund'): - # Hack to fix the stable version 8.0 -> saas-12 - # purchase_ok will be moved from purchase to product in master #13271 - if 'purchase_ok' in self.env['product.template']._fields: - node.set('domain', "[('purchase_ok', '=', True)]") - else: - node.set('domain', "[('sale_ok', '=', True)]") - res['arch'] = etree.tostring(doc, encoding='unicode') - return res - - @api.v8 - def get_invoice_line_account(self, type, product, fpos, company): - accounts = product.product_tmpl_id.get_product_accounts(fpos) - if type in ('out_invoice', 'out_refund'): - return accounts['income'] - return accounts['expense'] - - def _set_currency(self): - company = self.invoice_id.company_id - currency = self.invoice_id.currency_id - if company and currency: - if company.currency_id != currency: - self.price_unit = self.price_unit * currency.with_context(dict(self._context or {}, date=self.invoice_id.date_invoice)).rate - - def _set_taxes(self): - """ Used in on_change to set taxes and price""" - self.ensure_one() - - # Keep only taxes of the company - company_id = self.company_id or self.env.company - - if self.invoice_id.type in ('out_invoice', 'out_refund'): - taxes = self.product_id.taxes_id.filtered(lambda r: r.company_id == company_id) or self.account_id.tax_ids or self.invoice_id.company_id.account_sale_tax_id - else: - taxes = self.product_id.supplier_taxes_id.filtered(lambda r: r.company_id == company_id) or self.account_id.tax_ids or self.invoice_id.company_id.account_purchase_tax_id - - self.invoice_line_tax_ids = fp_taxes = self.invoice_id.fiscal_position_id.map_tax(taxes, self.product_id, self.invoice_id.partner_id) - - fix_price = self.env['account.tax']._fix_tax_included_price - if self.invoice_id.type in ('in_invoice', 'in_refund'): - prec = self.env['decimal.precision'].precision_get('Product Price') - if not self.price_unit or float_compare(self.price_unit, self.product_id.standard_price, precision_digits=prec) == 0: - self.price_unit = fix_price(self.product_id.standard_price, taxes, fp_taxes) - self._set_currency() - else: - self.price_unit = fix_price(self.product_id.lst_price, taxes, fp_taxes) - self._set_currency() - - @api.onchange('product_id') - def _onchange_product_id(self): - domain = {} - if not self.invoice_id: - return - - part = self.invoice_id.partner_id - fpos = self.invoice_id.fiscal_position_id - company = self.invoice_id.company_id - currency = self.invoice_id.currency_id - type = self.invoice_id.type - - if not part: - warning = { - 'title': _('Warning!'), - 'message': _('You must first select a partner.'), - } - return {'warning': warning} - - if not self.product_id: - if type not in ('in_invoice', 'in_refund'): - self.price_unit = 0.0 - domain['uom_id'] = [] - else: - self_lang = self - if part.lang: - self_lang = self.with_context(lang=part.lang) - - product = self_lang.product_id - account = self.get_invoice_line_account(type, product, fpos, company) - if account: - self.account_id = account.id - self._set_taxes() - - product_name = self_lang._get_invoice_line_name_from_product() - if product_name != None: - self.name = product_name - - if not self.uom_id or product.uom_id.category_id.id != self.uom_id.category_id.id: - self.uom_id = product.uom_id.id - domain['uom_id'] = [('category_id', '=', product.uom_id.category_id.id)] - - if company and currency: - - if self.uom_id and self.uom_id.id != product.uom_id.id: - self.price_unit = product.uom_id._compute_price(self.price_unit, self.uom_id) - return {'domain': domain} - - def _get_invoice_line_name_from_product(self): - """ Returns the automatic name to give to the invoice line depending on - the product it is linked to. - """ - self.ensure_one() - if not self.product_id: - return '' - invoice_type = self.invoice_id.type - rslt = self.product_id.partner_ref - if invoice_type in ('in_invoice', 'in_refund'): - if self.product_id.description_purchase: - rslt += '\n' + self.product_id.description_purchase - else: - if self.product_id.description_sale: - rslt += '\n' + self.product_id.description_sale - - return rslt - - @api.onchange('account_id') - def _onchange_account_id(self): - if not self.account_id: - return - if not self.product_id: - fpos = self.invoice_id.fiscal_position_id - default_tax = self.invoice_id.type in ('out_invoice', 'out_refund') and self.invoice_id.company_id.account_sale_tax_id or self.invoice_id.company_id.account_purchase_tax_id - self.invoice_line_tax_ids = fpos.map_tax(self.account_id.tax_ids or default_tax, partner=self.partner_id) - elif not self.price_unit: - self._set_taxes() - - - @api.onchange('uom_id') - def _onchange_uom_id(self): - warning = {} - result = {} - if not self.uom_id: - self.price_unit = 0.0 - - if self.product_id and self.uom_id: - if self.invoice_id.type in ('in_invoice', 'in_refund'): - price_unit = self.product_id.standard_price - else: - price_unit = self.product_id.lst_price - self.price_unit = self.product_id.uom_id._compute_price(price_unit, self.uom_id) - self._set_currency() - - if self.product_id.uom_id.category_id.id != self.uom_id.category_id.id: - warning = { - 'title': _('Warning!'), - 'message': _('The selected unit of measure has to be in the same category as the product unit of measure.'), - } - self.uom_id = self.product_id.uom_id.id - if warning: - result['warning'] = warning - return result - - def _set_additional_fields(self): - """ Some modules, such as Purchase, provide a feature to add automatically pre-filled - invoice lines. However, these modules might not be aware of extra fields which are - added by extensions of the accounting module. - This method is intended to be overridden by these extensions, so that any new field can - easily be auto-filled as well. - """ - pass - - @api.multi - def unlink(self): - if self.filtered(lambda r: r.invoice_id and r.invoice_id.state != 'draft'): - raise UserError(_('You can only delete an invoice line if the invoice is in draft state.')) - return super(AccountInvoiceLine, self).unlink() - - def _prepare_invoice_line(self): - data = { - 'name': self.name, - 'origin': self.origin, - 'uom_id': self.uom_id.id, - 'product_id': self.product_id.id, - 'account_id': self.account_id.id, - 'price_unit': self.price_unit, - 'quantity': self.quantity, - 'discount': self.discount, - 'account_analytic_id': self.account_analytic_id.id, - 'analytic_tag_ids': self.analytic_tag_ids.ids, - 'invoice_line_tax_ids': self.invoice_line_tax_ids.ids - } - return data - - @api.model_create_multi - def create(self, vals_list): - for vals in vals_list: - if vals.get('display_type', self.default_get(['display_type'])['display_type']): - vals.update(price_unit=0, account_id=False, quantity=0) - return super(AccountInvoiceLine, self).create(vals_list) - - @api.multi - def write(self, values): - if 'display_type' in values and self.filtered(lambda line: line.display_type != values.get('display_type')): - raise UserError("You cannot change the type of an invoice line. Instead you should delete the current line and create a new line of the proper type.") - return super(AccountInvoiceLine, self).write(values) - - _sql_constraints = [ - ('accountable_required_fields', - "CHECK(display_type IS NOT NULL OR account_id IS NOT NULL)", - "Missing required account on accountable invoice line."), - - ('non_accountable_fields_null', - "CHECK(display_type IS NULL OR (price_unit = 0 AND account_id IS NULL and quantity = 0))", - "Forbidden unit price, account and quantity on non-accountable invoice line"), - ] - - -class AccountInvoiceTax(models.Model): - _name = "account.invoice.tax" - _description = "Invoice Tax" - _order = 'sequence, id desc' - - def _prepare_invoice_tax_val(self): - self.ensure_one() - return { - 'tax_id': self.tax_id.id, - 'account_id': self.account_id.id, - 'account_analytic_id': self.account_analytic_id.id, - 'analytic_tag_ids': self.analytic_tag_ids.ids or False, - } - - invoice_id = fields.Many2one('account.invoice', string='Invoice', ondelete='cascade', index=True) - name = fields.Char(string='Tax Description', required=True) - tax_id = fields.Many2one('account.tax', string='Tax', ondelete='restrict') - tax_repartition_line_id = fields.Many2one(string="Originating Repartition Line", comodel_name='account.tax.repartition.line') - account_id = fields.Many2one('account.account', string='Tax Account', required=True, domain=[('deprecated', '=', False)]) - account_analytic_id = fields.Many2one('account.analytic.account', string='Analytic account') - analytic_tag_ids = fields.Many2many('account.analytic.tag', string='Analytic Tags') - amount = fields.Monetary('Tax Amount') - amount_rounding = fields.Monetary('Amount Delta') - amount_total = fields.Monetary(string="Amount Total", compute='_compute_amount_total') - manual = fields.Boolean(default=True) - sequence = fields.Integer(help="Gives the sequence order when displaying a list of invoice tax.") - company_id = fields.Many2one('res.company', string='Company', related='account_id.company_id', store=True, readonly=True) - currency_id = fields.Many2one('res.currency', related='invoice_id.currency_id', store=True, readonly=True) - base = fields.Monetary(string='Base') - tax_ids = fields.Many2many('account.tax', string='Affecting Base Taxes', help='Taxes whose base amount needs has to be affected by this tax line.') - tag_ids = fields.Many2many(string="Tags", comodel_name='account.account.tag', help="The taxes that will be applied on the move line generated for this tax entry") - - @api.depends('amount', 'amount_rounding') - def _compute_amount_total(self): - for tax_line in self: - tax_line.amount_total = tax_line.amount + tax_line.amount_rounding diff --git a/addons/account/models/account_journal_dashboard.py b/addons/account/models/account_journal_dashboard.py index 08b119405b4a9..5f68241000fb8 100644 --- a/addons/account/models/account_journal_dashboard.py +++ b/addons/account/models/account_journal_dashboard.py @@ -9,6 +9,9 @@ from odoo.tools.misc import formatLang, format_date as odoo_format_date import random +import ast + + class account_journal(models.Model): _inherit = "account.journal" @@ -157,12 +160,12 @@ def get_bar_graph_datas(self): start_date = (first_day_of_week + timedelta(days=-7)) for i in range(0,6): if i == 0: - query += "("+select_sql_clause+" and date_due < '"+start_date.strftime(DF)+"')" + query += "("+select_sql_clause+" and invoice_date_due < '"+start_date.strftime(DF)+"')" elif i == 5: - query += " UNION ALL ("+select_sql_clause+" and date_due >= '"+start_date.strftime(DF)+"')" + query += " UNION ALL ("+select_sql_clause+" and invoice_date_due >= '"+start_date.strftime(DF)+"')" else: next_date = start_date + timedelta(days=7) - query += " UNION ALL ("+select_sql_clause+" and date_due >= '"+start_date.strftime(DF)+"' and date_due < '"+next_date.strftime(DF)+"')" + query += " UNION ALL ("+select_sql_clause+" and invoice_date_due >= '"+start_date.strftime(DF)+"' and invoice_date_due < '"+next_date.strftime(DF)+"')" start_date = next_date self.env.cr.execute(query, query_args) @@ -190,9 +193,17 @@ def _get_bar_graph_select_query(self): the bar graph's data as its first element, and the arguments dictionary for it as its second. """ - return ("""SELECT sum(residual_company_signed) as total, min(date_due) as aggr_date - FROM account_invoice - WHERE journal_id = %(journal_id)s and state = 'open'""", {'journal_id':self.id}) + return (''' + SELECT + SUM((CASE WHEN move.type IN ('out_refund', 'in_refund') THEN -1 else 1 END) * line.amount_residual) AS total, + MIN(invoice_date_due) AS aggr_date + FROM account_move_line line + JOIN account_move move ON move.id = line.move_id + WHERE move.journal_id = %(journal_id)s + AND move.state = 'posted' + AND move.invoice_payment_state = 'not_paid' + AND move.type IN ('out_invoice', 'out_refund', 'in_invoice', 'in_refund', 'out_receipt', 'in_receipt') + ''', {'journal_id': self.id}) @api.multi def get_journal_dashboard_datas(self): @@ -241,7 +252,20 @@ def get_journal_dashboard_datas(self): query_results_drafts = self.env.cr.dictfetchall() today = fields.Date.today() - query = """SELECT residual_signed as amount_total, currency_id AS currency, type, date_invoice, company_id FROM account_invoice WHERE journal_id = %s AND date <= %s AND state = 'open';""" + query = ''' + SELECT + (CASE WHEN type IN ('out_refund', 'in_refund') THEN -1 ELSE 1 END) * amount_residual AS amount_total, + currency_id AS currency, + type, + invoice_date, + company_id + FROM account_move move + WHERE journal_id = %s + AND date <= %s + AND state = 'posted' + AND invoice_payment_state = 'not_paid' + AND type IN ('out_invoice', 'out_refund', 'in_invoice', 'in_refund', 'out_receipt', 'in_receipt'); + ''' self.env.cr.execute(query, (self.id, today)) late_query_results = self.env.cr.dictfetchall() curr_cache = {} @@ -282,9 +306,19 @@ def _get_open_bills_to_pay_query(self): data as its first element, and the arguments dictionary to use to run it as its second. """ - return ("""SELECT state, residual_signed as amount_total, currency_id AS currency, type, date_invoice, company_id - FROM account_invoice - WHERE journal_id = %(journal_id)s AND state = 'open';""", {'journal_id':self.id}) + return (''' + SELECT + (CASE WHEN move.type IN ('out_refund', 'in_refund') THEN -1 ELSE 1 END) * move.amount_residual AS amount_total, + move.currency_id AS currency, + move.type, + move.invoice_date, + move.company_id + FROM account_move move + WHERE move.journal_id = %(journal_id)s + AND move.state = 'posted' + AND move.invoice_payment_state = 'not_paid' + AND move.type IN ('out_invoice', 'out_refund', 'in_invoice', 'in_refund', 'out_receipt', 'in_receipt'); + ''', {'journal_id': self.id}) def _get_draft_bills_query(self): """ @@ -292,18 +326,19 @@ def _get_draft_bills_query(self): gather the bills in draft state data, and the arguments dictionary to use to run it as its second. """ - # there is no account_move_lines for draft invoices, so no relevant residual_signed value - return ("""SELECT state, - (CASE WHEN inv.type in ('out_invoice', 'in_invoice') - THEN inv.amount_total - ELSE (-1 * inv.amount_total) - END) AS amount_total, - inv.currency_id AS currency, - inv.type, - inv.date_invoice, - inv.company_id - FROM account_invoice inv - WHERE journal_id = %(journal_id)s AND state = 'draft';""", {'journal_id':self.id}) + return (''' + SELECT + (CASE WHEN move.type IN ('out_refund', 'in_refund') THEN -1 ELSE 1 END) * move.amount_total AS amount_total, + move.currency_id AS currency, + move.type, + move.invoice_date, + move.company_id + FROM account_move move + WHERE move.journal_id = %(journal_id)s + AND move.state = 'draft' + AND move.invoice_payment_state = 'not_paid' + AND move.type IN ('out_invoice', 'out_refund', 'in_invoice', 'in_refund', 'out_receipt', 'in_receipt'); + ''', {'journal_id': self.id}) def _count_results_and_sum_amounts(self, results_dict, target_currency, curr_cache=None): """ Loops on a query result to count the total number of invoices and sum @@ -320,7 +355,7 @@ def _count_results_and_sum_amounts(self, results_dict, target_currency, curr_cac cur = self.env['res.currency'].browse(result.get('currency')) company = self.env['res.company'].browse(result.get('company_id')) or self.env.company rslt_count += 1 - date = result.get('date_invoice') or fields.Date.today() + date = result.get('invoice_date') or fields.Date.today() amount = result.get('amount_total', 0) or 0 if cur != target_currency: @@ -336,28 +371,21 @@ def _count_results_and_sum_amounts(self, results_dict, target_currency, curr_cac @api.multi def action_create_new(self): ctx = self._context.copy() - model = 'account.invoice' + ctx['default_journal_id'] = self.id if self.type == 'sale': - ctx.update({'journal_type': self.type, 'default_type': 'out_invoice', 'type': 'out_invoice', 'default_journal_id': self.id}) - if ctx.get('refund'): - ctx.update({'default_type':'out_refund', 'type':'out_refund'}) - view_id = self.env.ref('account.invoice_form').id + ctx['default_type'] = 'out_refund' if ctx.get('refund') else 'out_invoice' elif self.type == 'purchase': - ctx.update({'journal_type': self.type, 'default_type': 'in_invoice', 'type': 'in_invoice', 'default_journal_id': self.id}) - if ctx.get('refund'): - ctx.update({'default_type': 'in_refund', 'type': 'in_refund'}) - view_id = self.env.ref('account.invoice_supplier_form').id + ctx['default_type'] = 'in_refund' if ctx.get('refund') else 'in_invoice' else: - ctx.update({'default_journal_id': self.id, 'view_no_maturity': True}) - view_id = self.env.ref('account.view_move_form').id - model = 'account.move' + ctx['default_type'] = 'entry' + ctx['view_no_maturity'] = True return { 'name': _('Create invoice/bill'), 'type': 'ir.actions.act_window', 'view_type': 'form', 'view_mode': 'form', - 'res_model': model, - 'view_id': view_id, + 'res_model': 'account.move', + 'view_id': self.env.ref('account.view_move_form').id, 'context': ctx, } @@ -402,7 +430,7 @@ def action_open_to_check(self): self.ensure_one() ids = self.to_check_ids().ids action_context = {'show_mode_selector': False, 'company_ids': self.mapped('company_id').ids} - action_context.update({'edition_mode': True}) + action_context.update({'suspense_moves_mode': True}) action_context.update({'statement_line_ids': ids}) return { 'type': 'ir.actions.client', @@ -412,7 +440,7 @@ def action_open_to_check(self): def to_check_ids(self): self.ensure_one() - domain = self.env['account.move.line']._get_domain_for_edition_mode() + domain = self.env['account.move.line']._get_suspense_moves_domain() domain.append(('journal_id', '=', self.id)) statement_line_ids = self.env['account.move.line'].search(domain).mapped('statement_line_id') return statement_line_ids @@ -420,60 +448,42 @@ def to_check_ids(self): @api.multi def open_action(self): """return action based on type for related journals""" - action_name = self._context.get('action_name', False) + action_name = self._context.get('action_name') + + # Find action based on journal. if not action_name: if self.type == 'bank': action_name = 'action_bank_statement_tree' elif self.type == 'cash': action_name = 'action_view_bank_statement_tree' elif self.type == 'sale': - action_name = 'action_invoice_tree1' - use_domain = expression.AND( - [self.env.context.get('use_domain', []), [('journal_id', '=', self.id)]] - ) - self = self.with_context(use_domain=use_domain) + action_name = 'action_move_out_invoice_type' elif self.type == 'purchase': - action_name = 'action_vendor_bill_template' - use_domain = expression.AND( - [self.env.context.get('use_domain', []), [('journal_id', '=', self.id)]] - ) - self = self.with_context(use_domain=use_domain) + action_name = 'action_move_in_invoice_type' else: action_name = 'action_move_journal_line' - _journal_invoice_type_map = { - ('sale', None): 'out_invoice', - ('purchase', None): 'in_invoice', - ('sale', 'refund'): 'out_refund', - ('purchase', 'refund'): 'in_refund', - ('bank', None): 'bank', - ('cash', None): 'cash', - ('general', None): 'general', - } - invoice_type = _journal_invoice_type_map[(self.type, self._context.get('invoice_type'))] + # Set 'account.' prefix if missing. + if '.' not in action_name: + action_name = 'account.%s' % action_name - ctx = self._context.copy() - ctx.pop('group_by', None) - ctx.update({ - 'journal_type': self.type, + action = self.env.ref(action_name).read()[0] + context = self._context.copy() + if 'context' in action and type(action['context']) == str: + context.update(ast.literal_eval(action['context'])) + else: + context.update(action.get('context', {})) + action['context'] = context + action['context'].update({ 'default_journal_id': self.id, - 'default_type': invoice_type, - 'type': invoice_type, 'search_default_journal_id': self.id, }) - [action] = self.env.ref('account.%s' % action_name).read() - action['context'] = ctx - action['domain'] = self._context.get('use_domain', []) - account_invoice_filter = self.env.ref('account.view_account_invoice_filter', False) - if action_name in ['action_invoice_tree1', 'action_vendor_bill_template']: - action['search_view_id'] = account_invoice_filter and account_invoice_filter.id or False - if action_name in ['action_bank_statement_tree', 'action_view_bank_statement_tree']: - action['views'] = False - action['view_id'] = False - if self.type == 'purchase': - new_help = self.env['account.invoice'].with_context(ctx).complete_empty_list_help() - action.update({'help': (action.get('help') or '') + new_help}) + if self.type == 'sale': + action['domain'] = [('type', 'in', ('out_invoice', 'out_refund', 'out_receipt'))] + elif self.type == 'purchase': + action['domain'] = [('type', 'in', ('in_invoice', 'in_refund', 'in_receipt'))] + return action @api.multi diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index 3c181f3357b32..040f2bbeceb64 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -1,349 +1,1903 @@ # -*- coding: utf-8 -*- -import time -from datetime import date -from collections import OrderedDict from odoo import api, fields, models, _ -from odoo.osv import expression from odoo.exceptions import RedirectWarning, UserError, ValidationError +from odoo.tools import float_is_zero, float_compare, safe_eval, date_utils from odoo.tools.misc import formatLang, format_date -from odoo.tools import float_is_zero, float_compare -from odoo.tools.safe_eval import safe_eval from odoo.addons import decimal_precision as dp -from lxml import etree -#---------------------------------------------------------- -# Entries -#---------------------------------------------------------- +from collections import OrderedDict +from datetime import date +from itertools import groupby +from stdnum.iso7064 import mod_97_10 +from itertools import zip_longest + +import json +import re +import logging + +_logger = logging.getLogger(__name__) + class AccountMove(models.Model): _name = "account.move" + _inherit = ['portal.mixin', 'mail.thread', 'mail.activity.mixin'] _description = "Journal Entries" _order = 'date desc, name desc, id desc' - _inherit = ['mail.thread', 'mail.activity.mixin'] @api.model - def default_get(self, fields): - rec = super(AccountMove, self).default_get(fields) - if not rec.get('journal_id'): - rec.update({'journal_id': self.env['account.journal'].search([('type', '=', 'general'), ('company_id', '=', self.env.company.id)], limit=1).id}) - return rec + def _get_default_journal(self): + ''' Get the default journal. + It could either be passed through the context using the 'default_journal_id' key containing its id, + either be determined by the default type. + ''' + move_type = self._context.get('default_type', 'entry') + journal_type = 'general' + if move_type in self.get_sale_types(include_receipts=True): + journal_type = 'sale' + elif move_type in self.get_purchase_types(include_receipts=True): + journal_type = 'purchase' + + if self._context.get('default_journal_id'): + journal = self.env['account.journal'].browse(self._context['default_journal_id']) + + if move_type != 'entry' and journal.type != journal_type: + raise UserError(_("Cannot create an invoice of type %s with a journal having %s as type.") % (move_type, journal_type)) + else: + company_id = self._context.get('default_company_id', self.env.company.id) + domain = [('company_id', '=', company_id), ('type', '=', journal_type)] + + journal = None + if self._context.get('default_currency_id'): + currency_domain = domain + [('currency_id', '=', self._context['default_currency_id'])] + journal = self.env['account.journal'].search(currency_domain, limit=1) + + if not journal: + journal = self.env['account.journal'].search(domain, limit=1) + + if not journal: + error_msg = _('Please define an accounting miscellaneous journal in your company') + if journal_type == 'sale': + error_msg = _('Please define an accounting sale journal in your company') + elif journal_type == 'purchase': + error_msg = _('Please define an accounting purchase journal in your company') + raise UserError(error_msg) + return journal + + @api.model + def _get_default_currency(self): + ''' Get the default currency from either the journal, either the default journal's company. ''' + journal = self._get_default_journal() + return journal.currency_id or journal.company_id.currency_id + + @api.model + def _get_default_invoice_incoterm(self): + ''' Get the default incoterm for invoice. ''' + return self.env.company.incoterm_id + + # ==== Business fields ==== + name = fields.Char(string='Number', required=True, readonly=True, copy=False, default='/') + date = fields.Date(string='Date', required=True, index=True, readonly=True, + states={'draft': [('readonly', False)]}, + default=fields.Date.context_today) + ref = fields.Char(string='Reference', copy=False, readonly=True, + states={'draft': [('readonly', False)]}) + narration = fields.Text(string='Internal Note') + state = fields.Selection(selection=[ + ('draft', 'Unposted'), + ('posted', 'Posted'), + ('cancel', 'Cancelled') + ], string='Status', required=True, readonly=True, copy=False, tracking=True, + default='draft') + type = fields.Selection(selection=[ + ('entry', 'Journal Entry'), + ('out_invoice', 'Customer Invoice'), + ('out_refund', 'Customer Credit Note'), + ('in_invoice', 'Vendor Bill'), + ('in_refund', 'Vendor Credit Note'), + ('out_receipt', 'Sales Receipt'), + ('in_receipt', 'Purchase Receipt'), + ], String='Type', required=True, store=True, index=True, readonly=True, tracking=True, + default="entry") + to_check = fields.Boolean(string='To Check', default=False, + help='If this checkbox is ticked, it means that the user was not sure of all the related informations at the time of the creation of the move and that the move needs to be checked again.') + journal_id = fields.Many2one('account.journal', string='Journal', required=True, readonly=True, + states={'draft': [('readonly', False)]}, + domain=lambda self: [('company_id', '=', self.env.company.id)], + default=_get_default_journal) + company_id = fields.Many2one(string='Company', store=True, readonly=True, + related='journal_id.company_id') + company_currency_id = fields.Many2one(string='Company Currency', readonly=True, + related='journal_id.company_id.currency_id') + currency_id = fields.Many2one('res.currency', store=True, readonly=True, tracking=True, required=True, + states={'draft': [('readonly', False)]}, + string='Currency', + default=_get_default_currency) + line_ids = fields.One2many('account.move.line', 'move_id', string='Journal Items', copy=True, readonly=True, + states={'draft': [('readonly', False)]}) + partner_id = fields.Many2one('res.partner', readonly=True, tracking=True, + states={'draft': [('readonly', False)]}, + string='Customer/Vendor') + commercial_partner_id = fields.Many2one('res.partner', string='Commercial Entity', store=True, readonly=True, + compute='_compute_commercial_partner_id') + + # === Amount fields === + amount_untaxed = fields.Monetary(string='Untaxed Amount', store=True, readonly=True, tracking=True, + compute='_compute_amount') + amount_tax = fields.Monetary(string='Tax', store=True, readonly=True, + compute='_compute_amount') + amount_total = fields.Monetary(string='Total', store=True, readonly=True, + compute='_compute_amount', + inverse='_inverse_amount_total') + amount_residual = fields.Monetary(string='Amount Due', store=True, + compute='_compute_amount') + amount_untaxed_signed = fields.Monetary(string='Untaxed Amount Signed', store=True, readonly=True, + compute='_compute_amount') + amount_tax_signed = fields.Monetary(string='Tax Signed', store=True, readonly=True, + compute='_compute_amount') + amount_total_signed = fields.Monetary(string='Total Signed', store=True, readonly=True, + compute='_compute_amount') + amount_residual_signed = fields.Monetary(string='Amount Due Signed', store=True, + compute='_compute_amount') + amount_by_group = fields.Binary(string="Tax amount by group", + compute='_compute_invoice_taxes_by_group', + help="Technical field used by web_studio to allow an easy edition of the invoice report by drag/drop of the field. Return type: [(name, amount, base, formated amount, formated base)]") + + # ==== Cash basis feature fields ==== + tax_cash_basis_rec_id = fields.Many2one( + 'account.partial.reconcile', + string='Tax Cash Basis Entry of', + help="Technical field used to keep track of the tax cash basis reconciliation. " + "This is needed when cancelling the source: it will post the inverse journal entry to cancel that part too.") + + # ==== Auto-post feature fields ==== + auto_post = fields.Boolean(string='Post Automatically', default=False, + help='If this checkbox is ticked, this entry will be automatically posted at its date.') + + # ==== Reverse feature fields ==== + reversed_entry_id = fields.Many2one('account.move', string="Reverse entry", readonly=True, copy=False) + + # ========================================================= + # Invoice related fields + # ========================================================= + + # ==== Business fields ==== + fiscal_position_id = fields.Many2one('account.fiscal.position', string='Fiscal Position', readonly=True, + states={'draft': [('readonly', False)]}, + help="Fiscal positions are used to adapt taxes and accounts for particular customers or sales orders/invoices. " + "The default value comes from the customer.") + invoice_user_id = fields.Many2one('res.users', readonly=True, copy=False, tracking=True, + states={'draft': [('readonly', False)]}, + string='Salesperson', + default=lambda self: self.env.user) + invoice_payment_state = fields.Selection(selection=[ + ('not_paid', 'Not Paid'), + ('in_payment', 'In Payment'), + ('paid', 'paid') + ], string='Payment Status', store=True, readonly=True, copy=False, tracking=True, + compute='_compute_amount') + invoice_date = fields.Date(string='Invoice/Bill Date', readonly=True, index=True, copy=False, + states={'draft': [('readonly', False)]}, + help="Keep empty to use the current date") + invoice_date_due = fields.Date(string='Due Date', readonly=True, index=True, copy=False, + states={'draft': [('readonly', False)]}, + help="If you use payment terms, the due date will be computed automatically at the generation " + "of accounting entries. The Payment terms may compute several due dates, for example 50% " + "now and 50% in one month, but if you want to force a due date, make sure that the payment " + "term is not set on the invoice. If you keep the Payment terms and the due date empty, it " + "means direct payment.") + invoice_payment_ref = fields.Char(string='Payment Reference', index=True, copy=False, readonly=True, + states={'draft': [('readonly', False)]}, + help="The payment reference to set on journal items.") + invoice_sent = fields.Boolean(readonly=True, default=False, copy=False, + help="It indicates that the invoice has been sent.") + invoice_origin = fields.Char(string='Origin', readonly=True, tracking=True, + help="The document(s) that generated the invoice.") + invoice_payment_term_id = fields.Many2one('account.payment.term', string='Payment Terms', + readonly=True, states={'draft': [('readonly', False)]}, + help="If you use payment terms, the due date will be computed automatically at the generation " + "of accounting entries. If you keep the payment terms and the due date empty, it means direct payment. " + "The payment terms may compute several due dates, for example 50% now, 50% in one month.") + # /!\ invoice_line_ids is just a subset of line_ids. + invoice_line_ids = fields.One2many('account.move.line', 'move_id', string='Invoice lines', + copy=False, readonly=True, + domain=[('exclude_from_invoice_tab', '=', False)], + states={'draft': [('readonly', False)]}) + invoice_partner_bank_id = fields.Many2one('res.partner.bank', string='Bank Account', + help='Bank Account Number to which the invoice will be paid. A Company bank account if this is a Customer Invoice or Vendor Credit Note, otherwise a Partner bank account number.', + readonly=True, states={'draft': [('readonly', False)]}) + invoice_incoterm_id = fields.Many2one('account.incoterms', string='Incoterm', + default=_get_default_invoice_incoterm, + help='International Commercial Terms are a series of predefined commercial terms used in international transactions.') + + # ==== Payment widget fields ==== + invoice_outstanding_credits_debits_widget = fields.Text(groups="account.group_account_invoice", + compute='_compute_payments_widget_to_reconcile_info') + invoice_payments_widget = fields.Text(groups="account.group_account_invoice", + compute='_compute_payments_widget_reconciled_info') + invoice_has_outstanding = fields.Boolean(groups="account.group_account_invoice", + compute='_compute_payments_widget_to_reconcile_info') + + # ==== Vendor bill fields ==== + invoice_vendor_bill_id = fields.Many2one('account.move', store=False, + string='Vendor Bill', + help="Auto-complete from a past bill.") + invoice_source_email = fields.Char(string='Source Email', tracking=True) + invoice_vendor_display_name = fields.Char(compute='_compute_invoice_vendor_display_info', store=True) + invoice_vendor_icon = fields.Char(compute='_compute_invoice_vendor_display_info', store=False) + + # ==== Cash rounding fields ==== + invoice_cash_rounding_id = fields.Many2one('account.cash.rounding', string='Cash Rounding Method', + readonly=True, states={'draft': [('readonly', False)]}, + help='Defines the smallest coinage of the currency that can be used to pay by cash.') + + # ==== Fields to set the sequence, on the first invoice of the journal ==== + invoice_sequence_number_next = fields.Char(string='Next Number', + compute='_compute_invoice_sequence_number_next', + inverse='_inverse_invoice_sequence_number_next') + invoice_sequence_number_next_prefix = fields.Char(string='Next Number Prefix', + compute="_compute_invoice_sequence_number_next") + + # ==== Display purpose fields ==== + invoice_filter_type_domain = fields.Char(compute='_compute_invoice_filter_type_domain', + help="Technical field used to have a dynamic domain on journal / taxes in the form view.") + bank_partner_id = fields.Many2one('res.partner', help='Technical field to get the domain on the bank', compute='_compute_bank_partner_id') + invoice_has_matching_supsense_amount = fields.Boolean(compute='_compute_has_matching_suspense_amount', + groups='account.group_account_invoice', + help="Technical field used to display an alert on invoices if there is at least a matching amount in any supsense account.") + + # ------------------------------------------------------------------------- + # ONCHANGE METHODS + # ------------------------------------------------------------------------- + + @api.onchange('invoice_date') + def _onchange_invoice_date(self): + if self.invoice_date: + self.invoice_date_due = self.date = self.invoice_date + self._onchange_currency() + + @api.onchange('invoice_date_due') + def _onchange_invoice_date_due(self): + self._recompute_dynamic_lines() + + @api.onchange('journal_id') + def _onchange_journal(self): + if self.journal_id and self.journal_id.currency_id: + new_currency = self.journal_id.currency_id + if new_currency != self.currency_id: + self.currency_id = new_currency + self._onchange_currency() + + @api.onchange('partner_id') + def _onchange_partner_id(self): + warning = {} + if self.partner_id: + rec_account = self.partner_id.property_account_receivable_id + pay_account = self.partner_id.property_account_payable_id + if not rec_account and not pay_account: + action = self.env.ref('account.action_account_config') + msg = _('Cannot find a chart of accounts for this company, You should configure it. \nPlease go to Account Configuration.') + raise RedirectWarning(msg, action.id, _('Go to the configuration panel')) + p = self.partner_id + if p.invoice_warn == 'no-message' and p.parent_id: + p = p.parent_id + if p.invoice_warn and p.invoice_warn != 'no-message': + # Block if partner only has warning but parent company is blocked + if p.invoice_warn != 'block' and p.parent_id and p.parent_id.invoice_warn == 'block': + p = p.parent_id + warning = { + 'title': _("Warning for %s") % p.name, + 'message': p.invoice_warn_msg + } + if p.invoice_warn == 'block': + self.partner_id = False + return {'warning': warning} + for line in self.line_ids: + line.partner_id = self.partner_id.commercial_partner_id + if self.is_sale_document(include_receipts=True): + self.invoice_payment_term_id = self.partner_id.property_payment_term_id + elif self.is_purchase_document(include_receipts=True): + self.invoice_payment_term_id = self.partner_id.property_supplier_payment_term_id + + self._compute_bank_partner_id() + self.invoice_partner_bank_id = self.bank_partner_id.bank_ids and self.bank_partner_id.bank_ids[0] + + # Find the new fiscal position. + delivery_partner_id = self._get_invoice_delivery_partner_id() + new_fiscal_position_id = self.env['account.fiscal.position'].get_fiscal_position( + self.partner_id.id, delivery_id=delivery_partner_id) + self.fiscal_position_id = self.env['account.fiscal.position'].browse(new_fiscal_position_id) + self._recompute_dynamic_lines() + if warning: + return {'warning': warning} + + @api.onchange('date', 'currency_id') + def _onchange_currency(self): + company_currency = self.company_id.currency_id + has_foreign_currency = self.currency_id and self.currency_id != company_currency + + for line in self.line_ids: + new_currency = has_foreign_currency and self.currency_id + line.currency_id = new_currency + line._onchange_currency() + self._recompute_dynamic_lines() + + @api.onchange('invoice_payment_ref') + def _onchange_invoice_payment_ref(self): + for line in self.line_ids.filtered(lambda line: line.account_id.user_type_id.type in ('receivable', 'payable')): + line.name = self.invoice_payment_ref + + @api.onchange('invoice_vendor_bill_id') + def _onchange_invoice_vendor_bill(self): + if self.invoice_vendor_bill_id: + # Copy invoice lines. + for line in self.invoice_vendor_bill_id.invoice_line_ids: + copied_vals = line.copy_data()[0] + copied_vals['move_id'] = self.id + new_line = self.env['account.move.line'].new(copied_vals) + new_line.recompute_tax_line = True + + # Copy payment terms. + self.invoice_payment_term_id = self.invoice_vendor_bill_id.invoice_payment_term_id + + # Copy currency. + if self.currency_id != self.invoice_vendor_bill_id.currency_id: + self.currency_id = self.invoice_vendor_bill_id.currency_id + + # Reset + self.invoice_vendor_bill_id = False + self._recompute_dynamic_lines() + + @api.onchange('type') + def _onchange_type(self): + ''' Onchange made to filter the partners depending of the type. ''' + if self.is_sale_document(include_receipts=True): + if self.env['ir.config_parameter'].sudo().get_param('account.use_invoice_terms'): + self.narration = self.company_id.invoice_terms or self.env.company.invoice_terms + return {'domain': {'partner_id': [('customer', '=', True)]}} + elif self.is_purchase_document(include_receipts=True): + return {'domain': {'partner_id': [('supplier', '=', True)]}} + + @api.onchange('invoice_line_ids') + def _onchange_invoice_line_ids(self): + current_invoice_lines = self.line_ids.filtered(lambda line: not line.exclude_from_invoice_tab) + others_lines = self.line_ids - current_invoice_lines + if others_lines and current_invoice_lines - self.invoice_line_ids: + others_lines[0].recompute_tax_line = True + self.line_ids = others_lines + self.invoice_line_ids + self._onchange_recompute_dynamic_lines() + + @api.onchange('line_ids', 'invoice_payment_term_id', 'invoice_date_due', 'invoice_cash_rounding_id', 'invoice_vendor_bill_id') + def _onchange_recompute_dynamic_lines(self): + self._recompute_dynamic_lines() + + @api.model + def _get_tax_grouping_key_from_tax_line(self, tax_line): + ''' Create the dictionary based on a tax line that will be used as key to group taxes together. + /!\ Must be consistent with '_get_tax_grouping_key_from_base_line'. + :param tax_line: An account.move.line being a tax line (with 'tax_repartition_line_id' set then). + :return: A dictionary containing all fields on which the tax will be grouped. + ''' + return { + 'tax_repartition_line_id': tax_line.tax_repartition_line_id.id, + 'account_id': tax_line.account_id.id, + 'currency_id': tax_line.currency_id.id, + 'analytic_tag_ids': [(6, 0, tax_line.tax_line_id.analytic and tax_line.analytic_tag_ids.ids or [])], + 'analytic_account_id': tax_line.tax_line_id.analytic and tax_line.analytic_account_id.id, + 'tax_ids': [(6, 0, tax_line.tax_ids.ids)], + 'tag_ids': [(6, 0, tax_line.tag_ids.ids)], + } + + @api.model + def _get_tax_grouping_key_from_base_line(self, base_line, tax_vals): + ''' Create the dictionary based on a base line that will be used as key to group taxes together. + /!\ Must be consistent with '_get_tax_grouping_key_from_tax_line'. + :param base_line: An account.move.line being a base line (that could contains something in 'tax_ids'). + :param tax_vals: An element of compute_all(...)['taxes']. + :return: A dictionary containing all fields on which the tax will be grouped. + ''' + tax_repartition_line = self.env['account.tax.repartition.line'].browse(tax_vals['tax_repartition_line_id']) + account = base_line._get_default_tax_account(tax_repartition_line) or base_line.account_id + return { + 'tax_repartition_line_id': tax_vals['tax_repartition_line_id'], + 'account_id': account.id, + 'currency_id': base_line.currency_id.id, + 'analytic_tag_ids': [(6, 0, base_line.analytic_tag_ids.ids)], + 'analytic_account_id': base_line.analytic_account_id.id, + 'tax_ids': [(6, 0, tax_vals['tax_ids'])], + 'tag_ids': [(6, 0, tax_vals['tag_ids'])], + } @api.multi - @api.depends('name', 'state') - def name_get(self): - result = [] + def _recompute_tax_lines(self): + ''' Compute the dynamic tax lines of the journal entry. + + :param lines_map: The line_ids dispatched by type containing: + * base_lines: The lines having a tax_ids set. + * tax_lines: The lines having a tax_line_id set. + * terms_lines: The lines generated by the payment terms of the invoice. + * rounding_lines: The cash rounding lines of the invoice. + ''' + self.ensure_one() + in_draft_mode = self != self._origin + + def _serialize_tax_grouping_key(grouping_dict): + ''' Serialize the dictionary values to be used in the taxes_map. + :param grouping_dict: The values returned by '_get_tax_grouping_key_from_tax_line' or '_get_tax_grouping_key_from_base_line'. + :return: A string representing the values. + ''' + return '-'.join(str(v) for v in grouping_dict.values()) + + def _compute_base_line_taxes(base_line): + ''' Compute taxes amounts both in company currency / foreign currency as the ratio between + amount_currency & balance could not be the same as the expected currency rate. + The 'amount_currency' value will be set on compute_all(...)['taxes'] in multi-currency. + :param base_line: The account.move.line owning the taxes. + :return: The result of the compute_all method. + ''' + balance_taxes_res = base_line.tax_ids._origin.compute_all( + base_line.balance, + currency=base_line.company_currency_id, + partner=base_line.partner_id, + is_refund=self.type in ('out_refund', 'in_refund'), + handle_price_include=False, + ) + + if base_line.currency_id: + # Multi-currencies mode: Taxes are computed both in company's currency / foreign currency. + amount_currency_taxes_res = base_line.tax_ids._origin.compute_all( + base_line.amount_currency, + currency=base_line.currency_id, + partner=base_line.partner_id, + is_refund=self.type in ('out_refund', 'in_refund'), + handle_price_include=False, + ) + for b_tax_res, ac_tax_res in zip(balance_taxes_res['taxes'], amount_currency_taxes_res['taxes']): + b_tax_res['amount_currency'] = ac_tax_res['amount'] + return balance_taxes_res + + taxes_map = {} + + # ==== Add tax lines ==== + for line in self.line_ids.filtered('tax_repartition_line_id'): + grouping_dict = self._get_tax_grouping_key_from_tax_line(line) + grouping_key = _serialize_tax_grouping_key(grouping_dict) + taxes_map[grouping_key] = { + 'tax_line': line, + 'balance': 0.0, + 'amount_currency': 0.0, + 'tax_base_amount': 0.0, + 'grouping_dict': False, + } + + # ==== Mount base lines ==== + for line in self.line_ids.filtered(lambda line: not line.exclude_from_invoice_tab): + # Don't call compute_all if there is no tax. + if not line.tax_ids: + line.tag_ids = [(5, 0, 0)] + continue + + compute_all_vals = _compute_base_line_taxes(line) + + # Assign tags on base line + line.tag_ids = compute_all_vals['base_tags'] + + tax_exigible = True + for tax_vals in compute_all_vals['taxes']: + grouping_dict = self._get_tax_grouping_key_from_base_line(line, tax_vals) + grouping_key = _serialize_tax_grouping_key(grouping_dict) + + tax_repartition_line = self.env['account.tax.repartition.line'].browse(tax_vals['tax_repartition_line_id']) + tax = tax_repartition_line.invoice_tax_id or tax_repartition_line.refund_tax_id + + if tax.tax_exigibility == 'on_payment': + tax_exigible = False + + taxes_map_entry = taxes_map.setdefault(grouping_key, { + 'tax_line': None, + 'balance': 0.0, + 'amount_currency': 0.0, + 'tax_base_amount': 0.0, + 'grouping_dict': False, + }) + taxes_map_entry['balance'] += tax_vals['amount'] + taxes_map_entry['amount_currency'] += tax_vals.get('amount_currency', 0.0) + taxes_map_entry['tax_base_amount'] += tax_vals['base'] + taxes_map_entry['grouping_dict'] = grouping_dict + line.tax_exigible = tax_exigible + + # ==== Process taxes_map ==== + for taxes_map_entry in taxes_map.values(): + # Don't create tax lines with zero balance. + if self.currency_id.is_zero(taxes_map_entry['balance']) and self.currency_id.is_zero(taxes_map_entry['amount_currency']): + taxes_map_entry['grouping_dict'] = False + + tax_line = taxes_map_entry['tax_line'] + tax_base_amount = -taxes_map_entry['tax_base_amount'] if self.is_inbound() else taxes_map_entry['tax_base_amount'] + + if not tax_line and not taxes_map_entry['grouping_dict']: + continue + elif tax_line and not taxes_map_entry['grouping_dict']: + # The tax line is no longer used, drop it. + self.line_ids -= tax_line + elif tax_line: + tax_line.update({ + 'amount_currency': taxes_map_entry['amount_currency'], + 'debit': taxes_map_entry['balance'] > 0.0 and taxes_map_entry['balance'] or 0.0, + 'credit': taxes_map_entry['balance'] < 0.0 and -taxes_map_entry['balance'] or 0.0, + 'tax_base_amount': tax_base_amount, + }) + else: + create_method = in_draft_mode and self.env['account.move.line'].new or self.env['account.move.line'].create + tax_repartition_line_id = taxes_map_entry['grouping_dict']['tax_repartition_line_id'] + tax_repartition_line = self.env['account.tax.repartition.line'].browse(tax_repartition_line_id) + tax = tax_repartition_line.invoice_tax_id or tax_repartition_line.refund_tax_id + tax_line = create_method({ + 'name': tax.name, + 'move_id': self.id, + 'partner_id': line.partner_id.id, + 'company_id': line.company_id.id, + 'company_currency_id': line.company_currency_id.id, + 'quantity': 1.0, + 'date_maturity': False, + 'amount_currency': taxes_map_entry['amount_currency'], + 'debit': taxes_map_entry['balance'] > 0.0 and taxes_map_entry['balance'] or 0.0, + 'credit': taxes_map_entry['balance'] < 0.0 and -taxes_map_entry['balance'] or 0.0, + 'tax_base_amount': tax_base_amount, + 'exclude_from_invoice_tab': True, + 'tax_exigible': tax.tax_exigibility == 'on_invoice', + **taxes_map_entry['grouping_dict'], + }) + + if in_draft_mode: + tax_line._onchange_amount_currency() + tax_line._onchange_balance() + + @api.multi + def _recompute_cash_rounding_lines(self): + ''' Handle the cash rounding feature on invoices. + + In some countries, the smallest coins do not exist. For example, in Switzerland, there is no coin for 0.01 CHF. + For this reason, if invoices are paid in cash, you have to round their total amount to the smallest coin that + exists in the currency. For the CHF, the smallest coin is 0.05 CHF. + + There are two strategies for the rounding: + + 1) Add a line on the invoice for the rounding: The cash rounding line is added as a new invoice line. + 2) Add the rounding in the biggest tax amount: The cash rounding line is added as a new tax line on the tax + having the biggest balance. + ''' + self.ensure_one() + in_draft_mode = self != self._origin + + def _compute_cash_rounding(self, total_balance, total_amount_currency): + ''' Compute the amount differences due to the cash rounding. + :param self: The current account.move record. + :param total_balance: The invoice's total in company's currency. + :param total_amount_currency: The invoice's total in invoice's currency. + :return: The amount differences both in company's currency & invoice's currency. + ''' + if self.currency_id == self.company_id.currency_id: + diff_balance = self.invoice_cash_rounding_id.compute_difference(self.currency_id, total_balance) + diff_amount_currency = 0.0 + else: + diff_amount_currency = self.invoice_cash_rounding_id.compute_difference(self.currency_id, total_amount_currency) + diff_balance = self.currency_id._convert(diff_amount_currency, self.company_id.currency_id, self.company_id, self.date) + return diff_balance, diff_amount_currency + + def _apply_cash_rounding(self, diff_balance, diff_amount_currency, cash_rounding_line): + ''' Apply the cash rounding. + :param self: The current account.move record. + :param diff_balance: The computed balance to set on the new rounding line. + :param diff_amount_currency: The computed amount in invoice's currency to set on the new rounding line. + :param cash_rounding_line: The existing cash rounding line. + :return: The newly created rounding line. + ''' + rounding_line_vals = { + 'debit': diff_balance > 0.0 and diff_balance or 0.0, + 'credit': diff_balance < 0.0 and -diff_balance or 0.0, + 'quantity': 1.0, + 'amount_currency': diff_amount_currency, + 'partner_id': self.partner_id.id, + 'move_id': self.id, + 'currency_id': self.currency_id if self.currency_id != self.company_id.currency_id else False, + 'company_id': self.company_id.id, + 'company_currency_id': self.company_id.currency_id.id, + 'is_rounding_line': True, + 'sequence': 9999, + } + + if self.invoice_cash_rounding_id.strategy == 'biggest_tax': + biggest_tax_line = None + for tax_line in self.line_ids.filtered('tax_repartition_line_id'): + if not biggest_tax_line or tax_line.price_subtotal > biggest_tax_line.price_subtotal: + biggest_tax_line = tax_line + + # No tax found. + if not biggest_tax_line: + return + + rounding_line_vals.update({ + 'name': _('%s (rounding)') % biggest_tax_line.name, + 'account_id': biggest_tax_line.account_id.id, + 'tax_repartition_line_id': biggest_tax_line.tax_repartition_line_id.id, + 'tax_exigible': biggest_tax_line.tax_exigible, + 'exclude_from_invoice_tab': True, + }) + + elif self.invoice_cash_rounding_id.strategy == 'add_invoice_line': + rounding_line_vals.update({ + 'name': self.invoice_cash_rounding_id.name, + 'account_id': self.invoice_cash_rounding_id.account_id.id, + }) + + # Create or update the cash rounding line. + if cash_rounding_line: + cash_rounding_line.update({ + 'amount_currency': rounding_line_vals['amount_currency'], + 'debit': rounding_line_vals['debit'], + 'credit': rounding_line_vals['credit'], + }) + else: + create_method = in_draft_mode and self.env['account.move.line'].new or self.env['account.move.line'].create + cash_rounding_line = create_method(rounding_line_vals) + + if in_draft_mode: + cash_rounding_line._onchange_amount_currency() + cash_rounding_line._onchange_balance() + + existing_cash_rounding_line = self.line_ids.filtered(lambda line: line.is_rounding_line) + + # The cash rounding has been removed. + if not self.invoice_cash_rounding_id: + self.line_ids -= existing_cash_rounding_line + return + + # The cash rounding strategy has changed. + if self.invoice_cash_rounding_id and existing_cash_rounding_line: + strategy = self.invoice_cash_rounding_id.strategy + old_strategy = 'biggest_tax' if existing_cash_rounding_line.tax_line_id else 'add_invoice_line' + if strategy != old_strategy: + self.line_ids -= existing_cash_rounding_line + existing_cash_rounding_line = self.env['account.move.line'] + + others_lines = self.line_ids.filtered(lambda line: line.account_id.user_type_id.type not in ('receivable', 'payable')) + others_lines -= existing_cash_rounding_line + total_balance = sum(others_lines.mapped('balance')) + total_amount_currency = sum(others_lines.mapped('amount_currency')) + + diff_balance, diff_amount_currency = _compute_cash_rounding(self, total_balance, total_amount_currency) + + # The invoice is already rounded. + if self.currency_id.is_zero(diff_balance) and self.currency_id.is_zero(diff_amount_currency): + self.line_ids -= existing_cash_rounding_line + return + + _apply_cash_rounding(self, diff_balance, diff_amount_currency, existing_cash_rounding_line) + + @api.multi + def _recompute_payment_terms_lines(self): + ''' Compute the dynamic payment term lines of the journal entry.''' + self.ensure_one() + in_draft_mode = self != self._origin + today = fields.Date.context_today(self) + + def _get_payment_terms_computation_date(self): + ''' Get the date from invoice that will be used to compute the payment terms. + :param self: The current account.move record. + :return: A datetime.date object. + ''' + if self.invoice_payment_term_id: + return self.invoice_date or today + else: + return self.invoice_date_due or self.invoice_date or today + + def _get_payment_terms_account(self, payment_terms_lines): + ''' Get the account from invoice that will be set as receivable / payable account. + :param self: The current account.move record. + :param payment_terms_lines: The current payment terms lines. + :return: An account.account record. + ''' + if payment_terms_lines: + # Retrieve account from previous payment terms lines in order to allow the user to set a custom one. + return payment_terms_lines[0].account_id + elif self.partner_id: + # Retrieve account from partner. + if self.is_sale_document(include_receipts=True): + return self.partner_id.property_account_receivable_id + else: + return self.partner_id.property_account_payable_id + else: + # Search new account. + domain = [ + ('company_id', '=', self.company_id.id), + ('internal_type', '=', 'receivable' if self.type in ('out_invoice', 'out_refund', 'out_receipt') else 'payable'), + ] + return self.env['account.account'].search(domain, limit=1) + + def _compute_payment_terms(self, date, total_balance, total_amount_currency): + ''' Compute the payment terms. + :param self: The current account.move record. + :param date: The date computed by '_get_payment_terms_computation_date'. + :param total_balance: The invoice's total in company's currency. + :param total_amount_currency: The invoice's total in invoice's currency. + :return: A list . + ''' + if self.invoice_payment_term_id: + to_compute = self.invoice_payment_term_id.compute(total_balance, date_ref=date, currency=self.currency_id) + if self.currency_id != self.company_id.currency_id: + # Multi-currencies. + to_compute_currency = self.invoice_payment_term_id.compute(total_amount_currency, date_ref=date, currency=self.currency_id) + return [(b[0], b[1], ac[1]) for b, ac in zip(to_compute, to_compute_currency)] + else: + # Single-currency. + return [(b[0], b[1], 0.0) for b in to_compute] + else: + return [(fields.Date.to_string(date), total_balance, total_amount_currency)] + + def _compute_diff_payment_terms_lines(self, existing_terms_lines, account, to_compute): + ''' Process the result of the '_compute_payment_terms' method and creates/updates corresponding invoice lines. + :param self: The current account.move record. + :param existing_terms_lines: The current payment terms lines. + :param account: The account.account record returned by '_get_payment_terms_account'. + :param to_compute: The list returned by '_compute_payment_terms'. + ''' + # As we try to update existing lines, sort them by due date. + existing_terms_lines = existing_terms_lines.sorted(lambda line: line.date_maturity or today) + existing_terms_lines_index = 0 + + # Recompute amls: update existing line or create new one for each payment term. + new_terms_lines = self.env['account.move.line'] + for date_maturity, balance, amount_currency in to_compute: + if existing_terms_lines_index < len(existing_terms_lines): + # Update existing line. + candidate = existing_terms_lines[existing_terms_lines_index] + existing_terms_lines_index += 1 + candidate.update({ + 'date_maturity': date_maturity, + 'amount_currency': -amount_currency, + 'debit': balance < 0.0 and -balance or 0.0, + 'credit': balance > 0.0 and balance or 0.0, + }) + else: + # Create new line. + create_method = in_draft_mode and self.env['account.move.line'].new or self.env['account.move.line'].create + candidate = create_method({ + 'name': self.invoice_payment_ref or '', + 'debit': balance < 0.0 and -balance or 0.0, + 'credit': balance > 0.0 and balance or 0.0, + 'quantity': 1.0, + 'amount_currency': -amount_currency, + 'date_maturity': date_maturity, + 'move_id': self.id, + 'currency_id': self.currency_id.id if self.currency_id != self.company_id.currency_id else False, + 'account_id': account.id, + 'partner_id': self.commercial_partner_id.id, + 'exclude_from_invoice_tab': True, + }) + new_terms_lines += candidate + if in_draft_mode: + candidate._onchange_amount_currency() + candidate._onchange_balance() + return new_terms_lines + + existing_terms_lines = self.line_ids.filtered(lambda line: line.account_id.user_type_id.type in ('receivable', 'payable')) + others_lines = self.line_ids.filtered(lambda line: line.account_id.user_type_id.type not in ('receivable', 'payable')) + total_balance = sum(others_lines.mapped('balance')) + total_amount_currency = sum(others_lines.mapped('amount_currency')) + + if not others_lines: + self.line_ids -= existing_terms_lines + return + + computation_date = _get_payment_terms_computation_date(self) + account = _get_payment_terms_account(self, existing_terms_lines) + to_compute = _compute_payment_terms(self, computation_date, total_balance, total_amount_currency) + new_terms_lines = _compute_diff_payment_terms_lines(self, existing_terms_lines, account, to_compute) + + # Remove old terms lines that are no longer needed. + self.line_ids -= existing_terms_lines - new_terms_lines + + if new_terms_lines: + self.invoice_payment_ref = new_terms_lines[-1].name or '' + self.invoice_date_due = new_terms_lines[-1].date_maturity + + @api.multi + def _recompute_dynamic_lines(self, recompute_all_taxes=False): + ''' Recompute all lines that depend of others. + + For example, tax lines depends of base lines (lines having tax_ids set). This is also the case of cash rounding + lines that depend of base lines or tax lines depending the cash rounding strategy. When a payment term is set, + this method will auto-balance the move with payment term lines. + + :param recompute_all_taxes: Force the computation of taxes. If set to False, the computation will be done + or not depending of the field 'recompute_tax_line' in lines. + ''' + self.ensure_one() + + # Dispatch lines and pre-compute some aggregated values like taxes. + for line in self.line_ids: + if line.recompute_tax_line: + recompute_all_taxes = True + line.recompute_tax_line = False + + # Compute taxes. + if recompute_all_taxes: + self._recompute_tax_lines() + + if self.is_invoice(include_receipts=True): + + # Compute cash rounding. + self._recompute_cash_rounding_lines() + + # Compute payment terms. + self._recompute_payment_terms_lines() + + # Only synchronize one2many in onchange. + if self != self._origin: + self.invoice_line_ids = self.line_ids.filtered(lambda line: not line.exclude_from_invoice_tab) + + @api.multi + def onchange(self, values, field_name, field_onchange): + # OVERRIDE + # As the dynamic lines in this model are quite complex, we need to ensure some computations are done exactly + # at the beginning / at the end of the onchange mechanism. So, the onchange recursivity is disabled. + return super(AccountMove, self.with_context(recursive_onchanges=False)).onchange(values, field_name, field_onchange) + + # ------------------------------------------------------------------------- + # COMPUTE METHODS + # ------------------------------------------------------------------------- + + @api.depends('type') + def _compute_invoice_filter_type_domain(self): for move in self: - name = move.name - if self.env.context.get('name_groupby'): - name = "**{date}**, {name} {partner}".format(date=format_date(self.env, move.date), name=move.name, partner=move.partner_id.name or "") - if move.state == 'draft': - name = '* ' + str(move.id) - result.append((move.id, name)) - return result + if move.is_sale_document(include_receipts=True): + move.invoice_filter_type_domain = 'sale' + elif move.is_purchase_document(include_receipts=True): + move.invoice_filter_type_domain = 'purchase' + else: + move.invoice_filter_type_domain = False + + @api.depends('partner_id') + def _compute_commercial_partner_id(self): + for move in self: + move.commercial_partner_id = move.partner_id.commercial_partner_id + + @api.depends('commercial_partner_id') + def _compute_bank_partner_id(self): + for move in self: + if move.is_outbound(): + move.bank_partner_id = move.commercial_partner_id + else: + move.bank_partner_id = move.company_id.partner_id + + @api.depends( + 'line_ids.debit', + 'line_ids.credit', + 'line_ids.currency_id', + 'line_ids.amount_currency', + 'line_ids.amount_residual', + 'line_ids.amount_residual_currency', + 'line_ids.payment_id.state') + def _compute_amount(self): + invoice_ids = [move.id for move in self if move.id and move.is_invoice(include_receipts=True)] + if invoice_ids: + self._cr.execute( + ''' + SELECT move.id + FROM account_move move + JOIN account_move_line line ON line.move_id = move.id + JOIN account_partial_reconcile part ON part.debit_move_id = line.id OR part.credit_move_id = line.id + JOIN account_move_line rec_line ON + (rec_line.id = part.credit_move_id AND line.id = part.debit_move_id) + OR + (rec_line.id = part.debit_move_id AND line.id = part.credit_move_id) + JOIN account_payment payment ON payment.id = rec_line.payment_id + JOIN account_journal journal ON journal.id = rec_line.journal_id + WHERE payment.state IN ('posted', 'sent') + AND journal.post_at_bank_rec IS TRUE + AND move.id IN %s + ''', [tuple(invoice_ids)] + ) + in_payment_set = set(res[0] for res in self._cr.fetchall()) + else: + in_payment_set = {} + + for move in self: + total_untaxed = 0.0 + total_untaxed_currency = 0.0 + total_tax = 0.0 + total_tax_currency = 0.0 + total_residual = 0.0 + total_residual_currency = 0.0 + currencies = set() + + for line in move.line_ids: + if line.currency_id: + currencies.add(line.currency_id) + + # Untaxed amount. + if (move.is_invoice(include_receipts=True) and not line.exclude_from_invoice_tab)\ + or (move.type == 'entry' and line.debit and not line.tax_line_id): + total_untaxed += line.balance + total_untaxed_currency += line.amount_currency + + # Tax amount. + if line.tax_line_id: + total_tax += line.balance + total_tax_currency += line.amount_currency + + # Residual amount. + if move.type == 'entry' or line.account_id.user_type_id.type in ('receivable', 'payable'): + total_residual += line.amount_residual + total_residual_currency += line.amount_residual_currency + + total = total_untaxed + total_tax + total_currency = total_untaxed_currency + total_tax_currency + + if (move.type == 'entry' and total < 0.0) or move.is_inbound(): + sign = -1 + else: + sign = 1 + + move.amount_untaxed = sign * (total_untaxed_currency if len(currencies) == 1 else total_untaxed) + move.amount_tax = sign * (total_tax_currency if len(currencies) == 1 else total_tax) + move.amount_total = sign * (total_currency if len(currencies) == 1 else total) + move.amount_residual = -sign * (total_residual_currency if len(currencies) == 1 else total_residual) + move.amount_untaxed_signed = -total_untaxed + move.amount_tax_signed = -total_tax + move.amount_total_signed = -total + move.amount_residual_signed = total_residual + + currency = len(currencies) == 1 and currencies.pop() or move.company_id.currency_id + is_paid = currency and currency.is_zero(move.amount_residual) or not move.amount_residual + + # Compute 'invoice_payment_state'. + if move.state == 'posted' and is_paid: + if move.id in in_payment_set: + move.invoice_payment_state = 'in_payment' + else: + move.invoice_payment_state = 'paid' + else: + move.invoice_payment_state = 'not_paid' @api.multi - @api.depends('line_ids.debit', 'line_ids.credit', 'line_ids.amount_currency', 'line_ids.currency_id') - def _amount_compute(self): + def _inverse_amount_total(self): for move in self: - total = 0.0 - total_currency = 0.0 - currency_id = move.line_ids and move.line_ids[0].currency_id.id or False - for line in move.line_ids.filtered(lambda l: l.debit): - total += line.debit - if currency_id and line.currency_id.id == currency_id: - total_currency += line.amount_currency - elif currency_id: - currency_id = False - - if currency_id and total_currency: - move.amount = total_currency - move.currency_id = currency_id + if len(move.line_ids) != 2 or move.type != 'entry': + continue + + to_write = [] + + if move.currency_id != move.company_id.currency_id: + amount_currency = abs(move.amount_total) + balance = move.currency_id._convert(amount_currency, move.currency_id, move.company_id, move.date) else: - move.currency_id = move.company_id.currency_id or self.env.user.company_id.currency_id - move.amount = total + balance = abs(move.amount_total) + amount_currency = 0.0 + + for line in move.line_ids: + to_write.append((1, line.id, { + 'debit': line.balance > 0.0 and balance or 0.0, + 'credit': line.balance < 0.0 and balance or 0.0, + 'amount_currency': line.balance > 0.0 and amount_currency or -amount_currency, + })) + + move.write({'line_ids': to_write}) @api.multi - def _set_amount(self): + def _get_domain_matching_supsense_moves(self): + self.ensure_one() + domain = self.env['account.move.line']._get_suspense_moves_domain() + domain += ['|', ('partner_id', '=?', self.partner_id.id), ('partner_id', '=', False)] + if self.is_inbound(): + domain.append(('balance', '=', -self.amount_residual)) + else: + domain.append(('balance', '=', self.amount_residual)) + return domain + + def _compute_has_matching_suspense_amount(self): + for r in self: + res = False + if r.state == 'posted' and r.is_invoice() and r.invoice_payment_state == 'not_paid': + domain = r._get_domain_matching_supsense_moves() + #there are more than one but less than 5 suspense moves matching the residual amount + if (0 < self.env['account.move.line'].search_count(domain) < 5): + domain2 = [ + ('invoice_payment_state', '=', 'not_paid'), + ('state', '=', 'open'), + ('amount_residual', '=', r.amount_residual), + ('type', '=', r.type)] + #there are less than 5 other open invoices of the same type with the same residual + if self.env['account.move'].search_count(domain2) < 5: + res = True + r.invoice_has_matching_supsense_amount = res + + @api.depends('partner_id', 'invoice_source_email') + def _compute_invoice_vendor_display_info(self): for move in self: - if len(move.line_ids) == 2 and move.amount != 0: - amount_in_company_currency = move.amount - if move.currency_id and move.currency_id != move.company_id.currency_id: - amount_in_company_currency = move.currency_id._convert(move.amount, move.company_id.currency_id, move.company_id, move.date) - for line in move.line_ids: - line.amount_currency = line.debit and move.amount or -move.amount - for line in move.with_context(check_move_validity=False).line_ids: - line.debit = line.debit and amount_in_company_currency or 0.0 - line.credit = line.credit and amount_in_company_currency or 0.0 - - @api.depends('line_ids.debit', 'line_ids.credit', 'line_ids.matched_debit_ids.amount', 'line_ids.matched_credit_ids.amount', 'line_ids.account_id.user_type_id.type') - def _compute_matched_percentage(self): - """Compute the percentage to apply for cash basis method. This value is relevant only for moves that - involve journal items on receivable or payable accounts. + vendor_display_name = move.partner_id.name + move.invoice_icon = '' + if not vendor_display_name: + if move.invoice_source_email: + vendor_display_name = _('From: ') + move.invoice_source_email + move.invoice_vendor_icon = '@' + else: + vendor_display_name = ('Created by: ') + move.create_uid.name + move.invoice_vendor_icon = '#' + move.invoice_vendor_display_name = vendor_display_name + + @api.depends('state', 'journal_id', 'invoice_date') + def _compute_invoice_sequence_number_next(self): + """ computes the prefix of the number that will be assigned to the first invoice/bill/refund of a journal, in order to + let the user manually change it. """ + # Check user group. + system_user = self.env.user._is_system() + if not system_user: + return + + # Check moves being candidates to set a custom number next. + moves = self.filtered(lambda move: move.is_invoice() and move.name == '/') + if not moves: + return + + for key, group in groupby(moves, key=lambda move: (move.journal_id, move._get_sequence())): + journal, sequence = key + domain = [('journal_id', '=', journal.id), ('state', '=', 'posted')] + if not isinstance(self.id, models.NewId): + domain.append(('id', '!=', self.id)) + if journal.type == 'sale': + domain.append(('type', 'in', ('out_invoice', 'out_refund'))) + elif journal.type == 'purchase': + domain.append(('type', 'in', ('in_invoice', 'in_refund'))) + else: + continue + if self.search_count(domain): + continue + + for move in group: + prefix, dummy = sequence._get_prefix_suffix(date=move.invoice_date or fields.Date.today(), date_range=move.invoice_date) + number_next = sequence._get_current_sequence().number_next_actual + move.invoice_sequence_number_next_prefix = prefix + move.invoice_sequence_number_next = '%%0%sd' % sequence.padding % number_next + + @api.multi + def _inverse_invoice_sequence_number_next(self): + ''' Set the number_next on the sequence related to the invoice/bill/refund''' + # Check user group. + if not self.env.user._is_admin(): + return + + # Set the next number in the sequence. for move in self: - total_amount = 0.0 - total_reconciled = 0.0 - for line in move.line_ids: - if line.account_id.user_type_id.type in ('receivable', 'payable'): - amount = abs(line.debit - line.credit) - total_amount += amount - precision_currency = move.currency_id or move.company_id.currency_id - if float_is_zero(total_amount, precision_rounding=precision_currency.rounding): - move.matched_percentage = 1.0 + if not move.invoice_sequence_number_next: + continue + sequence = move._get_sequence() + nxt = re.sub("[^0-9]", '', move.invoice_sequence_number_next) + result = re.match("(0*)([0-9]+)", nxt) + if result and sequence: + date_sequence = sequence._get_current_sequence() + date_sequence.number_next_actual = int(result.group(2)) + + @api.multi + def _compute_payments_widget_to_reconcile_info(self): + for move in self: + move.invoice_outstanding_credits_debits_widget = json.dumps(False) + + if move.state != 'posted' or move.invoice_payment_state != 'not_paid' or not move.is_invoice(include_receipts=True): + continue + pay_term_line_ids = move.line_ids.filtered(lambda line: line.account_id.user_type_id.type in ('receivable', 'payable')) + + domain = [('account_id', 'in', pay_term_line_ids.mapped('account_id').ids), + ('move_id.state', '=', 'posted'), + ('partner_id', '=', move.commercial_partner_id.id), + ('reconciled', '=', False), '|', ('amount_residual', '!=', 0.0), + ('amount_residual_currency', '!=', 0.0)] + + if move.is_inbound(): + domain.extend([('credit', '>', 0), ('debit', '=', 0)]) + type_payment = _('Outstanding credits') else: - for line in move.line_ids: - if line.account_id.user_type_id.type in ('receivable', 'payable'): - for partial_line in (line.matched_debit_ids + line.matched_credit_ids): - total_reconciled += partial_line.amount - move.matched_percentage = total_reconciled / total_amount + domain.extend([('credit', '=', 0), ('debit', '>', 0)]) + type_payment = _('Outstanding debits') + info = {'title': '', 'outstanding': True, 'content': [], 'move_id': move.id} + lines = self.env['account.move.line'].search(domain) + currency_id = move.currency_id + if len(lines) != 0: + for line in lines: + # get the outstanding residual value in invoice currency + if line.currency_id and line.currency_id == move.currency_id: + amount_to_show = abs(line.amount_residual_currency) + else: + currency = line.company_id.currency_id + amount_to_show = currency._convert(abs(line.amount_residual), move.currency_id, move.company_id, + line.date or fields.Date.today()) + if float_is_zero(amount_to_show, precision_rounding=move.currency_id.rounding): + continue + info['content'].append({ + 'journal_name': line.ref or line.move_id.name, + 'amount': amount_to_show, + 'currency': currency_id.symbol, + 'id': line.id, + 'position': currency_id.position, + 'digits': [69, move.currency_id.decimal_places], + }) + info['title'] = type_payment + move.invoice_outstanding_credits_debits_widget = json.dumps(info) + move.invoice_has_outstanding = True @api.multi - @api.depends('line_ids.reconcile_model_id') - def _compute_reconcile_model(self): + def _get_reconciled_info_JSON_values(self): + self.ensure_one() + foreign_currency = self.currency_id if self.currency_id != self.company_id.currency_id else False + + reconciled_vals = [] + pay_term_line_ids = self.line_ids.filtered(lambda line: line.account_id.user_type_id.type in ('receivable', 'payable')) + partials = pay_term_line_ids.mapped('matched_debit_ids') + pay_term_line_ids.mapped('matched_credit_ids') + for partial in partials: + counterpart_lines = partial.debit_move_id + partial.credit_move_id + counterpart_line = counterpart_lines.filtered(lambda line: line not in self.line_ids) + + if foreign_currency and partial.currency_id == foreign_currency: + amount = partial.amount_currency + else: + amount = partial.company_currency_id._convert(partial.amount, self.currency_id, self.company_id, self.date) + + if float_is_zero(amount, precision_rounding=self.currency_id.rounding): + continue + + ref = counterpart_line.move_id.name + if counterpart_line.move_id.ref: + ref += ' (' + counterpart_line.move_id.ref + ')' + + reconciled_vals.append({ + 'name': counterpart_line.name, + 'journal_name': counterpart_line.journal_id.name, + 'amount': amount, + 'currency': self.currency_id.symbol, + 'digits': [69, self.currency_id.decimal_places], + 'position': self.currency_id.position, + 'date': counterpart_line.date, + 'payment_id': counterpart_line.id, + 'account_payment_id': counterpart_line.payment_id.id, + 'move_id': counterpart_line.move_id.id, + 'ref': ref, + }) + return reconciled_vals + + @api.depends('type', 'line_ids.amount_residual') + def _compute_payments_widget_reconciled_info(self): for move in self: - move.reconcile_model_id = move.line_ids.mapped('reconcile_model_id') + if move.state != 'posted' or not move.is_invoice(include_receipts=True): + continue + reconciled_vals = move._get_reconciled_info_JSON_values() + if reconciled_vals: + info = { + 'title': _('Less Payment'), + 'outstanding': False, + 'content': reconciled_vals, + } + move.invoice_payments_widget = json.dumps(info, default=date_utils.json_default) + else: + move.invoice_payments_widget = json.dumps(False) + + @api.depends('line_ids.price_subtotal', 'line_ids.tax_base_amount', 'line_ids.tax_line_id', 'partner_id', 'currency_id') + def _compute_invoice_taxes_by_group(self): + ''' Helper to get the taxes grouped according their account.tax.group. + This method is only used when printing the invoice. + ''' + for move in self: + lang_env = move.with_context(lang=move.partner_id.lang).env + tax_lines = move.line_ids.filtered(lambda line: line.tax_line_id) + res = {} + for line in tax_lines: + res.setdefault(line.tax_line_id.tax_group_id, {'base': 0.0, 'amount': 0.0}) + res[line.tax_line_id.tax_group_id]['amount'] += line.price_subtotal + res[line.tax_line_id.tax_group_id]['base'] += line.tax_base_amount + res = sorted(res.items(), key=lambda l: l[0].sequence) + move.amount_by_group = [( + group.name, amounts['amount'], + amounts['base'], + formatLang(lang_env, amounts['amount'], currency_obj=move.currency_id), + formatLang(lang_env, amounts['base'], currency_obj=move.currency_id), + len(res), + ) for group, amounts in res] + + # ------------------------------------------------------------------------- + # CONSTRAINS METHODS + # ------------------------------------------------------------------------- + + @api.constrains('line_ids', 'journal_id') + def _validate_move_modification(self): + if 'posted' in self.mapped('line_ids.payment_id.state'): + raise ValidationError(_("You cannot modify a journal entry linked to a posted payment.")) + + @api.constrains('name', 'journal_id') + def _check_unique_sequence_number(self): + if not self: + return + + self._cr.execute(''' + SELECT move.id + FROM account_move move + INNER JOIN account_move move2 ON + move2.name = move.name + AND move2.company_id = move.company_id + AND move2.journal_id = move.journal_id + AND move2.type = move.type + AND move2.id != move.id + WHERE move.id IN %s + AND move.state = 'posted' + AND move2.state = 'posted' + ''', [tuple(self.ids)]) + res = self._cr.fetchone() + if res: + raise ValidationError(_('Posted journal entry must have an unique sequence number per company.')) + + @api.constrains('ref') + def _check_duplicate_supplier_reference(self): + moves = self.filtered(lambda move: move.is_purchase_document() and move.ref) + if not moves: + return + + self._cr.execute(''' + SELECT move.id + FROM account_move move + INNER JOIN account_move move2 ON + move2.ref = move.ref + AND move2.company_id = move.company_id + AND move2.commercial_partner_id = move.commercial_partner_id + AND move2.type = move.type + AND move2.id != move.id + WHERE move.id IN %s + AND move.type in ('in_invoice', 'in_refund') + AND move.ref IS NOT NULL + ''', [tuple(self.ids)]) + if self._cr.fetchone(): + raise ValidationError(_('Duplicated vendor reference detected. You probably encoded twice the same vendor bill/credit note.')) + + @api.multi + def _check_balanced(self): + ''' Assert the move is fully balanced debit = credit. + An error is raised if it's not the case. + ''' + moves = self.filtered(lambda move: move.line_ids) + if not moves: + return + + # /!\ As this method is called in create / write, we can't make the assumption the computed stored fields + # are already done. Then, this query MUST NOT depend of computed stored fields (e.g. balance). + # It happens as the ORM makes the create with the 'no_recompute' statement. + self._cr.execute(''' + SELECT line.move_id + FROM account_move_line line + JOIN account_move move ON move.id = line.move_id + JOIN account_journal journal ON journal.id = move.journal_id + JOIN res_company company ON company.id = journal.company_id + JOIN res_currency currency ON currency.id = company.currency_id + WHERE line.move_id IN %s + GROUP BY line.move_id, currency.decimal_places + HAVING ROUND(SUM(debit - credit), currency.decimal_places) != 0.0; + ''', [tuple(self.ids)]) + + query_res = self._cr.fetchall() + if query_res: + ids = [res[0] for res in query_res] + raise UserError(_("Cannot create unbalanced journal entry. Ids: %s") % str(ids)) + + @api.multi + def _check_fiscalyear_lock_date(self): + for move in self: + lock_date = max(move.company_id.period_lock_date or date.min, move.company_id.fiscalyear_lock_date or date.min) + if self.user_has_groups('account.group_account_manager'): + lock_date = move.company_id.fiscalyear_lock_date + if move.date <= (lock_date or date.min): + if self.user_has_groups('account.group_account_manager'): + message = _("You cannot add/modify entries prior to and inclusive of the lock date %s") % lock_date + else: + message = _("You cannot add/modify entries prior to and inclusive of the lock date %s. Check the company settings or ask someone with the 'Adviser' role") % lock_date + raise UserError(message) + return True + + @api.multi + def _check_tax_lock_date(self): + if not self: + return + + self._cr.execute(''' + SELECT move.id, company.tax_lock_date + FROM account_move move + JOIN account_journal journal ON journal.id = move.journal_id + JOIN res_company company ON company.id = journal.company_id + WHERE move.id IN %s + AND move.date < company.tax_lock_date + ''', [tuple(self.ids)]) + + query_res = self._cr.fetchone() + if query_res: + raise UserError(_(''' + The operation is refused as it would impact an already issued tax statement. + Please change the journal entry date or the tax lock date set in the settings (%s) to proceed + ''' % query_res[1])) + + @api.multi + def _check_move_consistency(self): + for move in self: + if move.line_ids: + if not all([x.company_id.id == move.company_id.id for x in move.line_ids]): + raise UserError(_("Cannot create moves for different companies.")) + + self._check_balanced() + self._check_fiscalyear_lock_date() + + # ------------------------------------------------------------------------- + # LOW-LEVEL METHODS + # ------------------------------------------------------------------------- + + @api.multi + def _move_autocomplete_invoice_lines_values(self): + ''' This method recomputes dynamic lines on the current journal entry that include taxes, cash rounding + and payment terms lines. + ''' + self.ensure_one() + + line_currency = self.currency_id if self.currency_id != self.company_id.currency_id else False + for line in self.line_ids: + # Do something only on invoice lines. + if line.exclude_from_invoice_tab: + continue + + # Ensure related fields are well copied. + line.partner_id = self.partner_id + line.date = self.date + line.recompute_tax_line = True + line.currency_id = line_currency + + # Shortcut to load the demo data. + if not line.account_id: + line.account_id = line._get_computed_account() + if not line.account_id: + if self.is_sale_document(include_receipts=True): + line.account_id = self.journal_id.default_credit_account_id + elif self.is_purchase_document(include_receipts=True): + line.account_id = self.journal_id.default_debit_account_id + + self.line_ids._onchange_price_subtotal() + self._recompute_dynamic_lines(recompute_all_taxes=True) + + values = self._convert_to_write(self._cache) + values.pop('invoice_line_ids', None) + return values + + @api.model + def _move_autocomplete_invoice_lines_create(self, vals_list): + ''' During the create of an account.move with only 'invoice_line_ids' set and not 'line_ids', this method is called + to auto compute accounting lines of the invoice. In that case, accounts will be retrieved and taxes, cash rounding + and payment terms will be computed. At the end, the values will contains all accounting lines in 'line_ids' + and the moves should be balanced. + + :param vals_list: The list of values passed to the 'create' method. + :return: Modified list of values. + ''' + new_vals_list = [] + for vals in vals_list: + if not vals.get('invoice_line_ids'): + new_vals_list.append(vals) + continue + if vals.get('line_ids'): + vals.pop('invoice_line_ids', None) + new_vals_list.append(vals) + continue + if not vals.get('type') and not self._context.get('default_type'): + vals.pop('invoice_line_ids', None) + new_vals_list.append(vals) + continue + vals['type'] = vals.get('type', self._context.get('default_type', 'entry')) + if not vals['type'] in self.get_invoice_types(include_receipts=True): + new_vals_list.append(vals) + continue + + vals['line_ids'] = vals.pop('invoice_line_ids') + + if vals.get('invoice_date') and not vals.get('date'): + vals['date'] = vals['invoice_date'] + + ctx_vals = {'default_type': vals.get('type') or self._context.get('default_type')} + if vals.get('journal_id'): + ctx_vals['default_journal_id'] = vals['journal_id'] + self_ctx = self.with_context(**ctx_vals) + new_vals = self_ctx._add_missing_default_values(vals) + + move = self_ctx.new(new_vals) + new_vals_list.append(move._move_autocomplete_invoice_lines_values()) + return new_vals_list + + @api.multi + def _move_autocomplete_invoice_lines_write(self, vals): + ''' During the write of an account.move with only 'invoice_line_ids' set and not 'line_ids', this method is called + to auto compute accounting lines of the invoice. In that case, accounts will be retrieved and taxes, cash rounding + and payment terms will be computed. At the end, the values will contains all accounting lines in 'line_ids' + and the moves should be balanced. + + :param vals_list: A python dict representing the values to write. + :return: True if the auto-completion did something, False otherwise. + ''' + enable_autocomplete = 'invoice_line_ids' in vals and 'line_ids' not in vals and True or False + + if not enable_autocomplete: + return False + + vals['line_ids'] = vals.pop('invoice_line_ids') + for invoice in self: + invoice_new = invoice.with_context(default_type=invoice.type, default_journal_id=invoice.journal_id.id).new(origin=invoice) + invoice_new.update(vals) + values = invoice_new._move_autocomplete_invoice_lines_values() + values.pop('invoice_line_ids', None) + invoice.write(values) + return True + + @api.model_create_multi + def create(self, vals_list): + # OVERRIDE + vals_list = self._move_autocomplete_invoice_lines_create(vals_list) + + moves = super(AccountMove, self).create(vals_list) + + # Trigger 'action_invoice_paid' when the invoice is directly paid at its creation. + moves.filtered(lambda move: move.is_invoice(include_receipts=True) and move.invoice_payment_state in ('paid', 'in_payment')).action_invoice_paid() + + return moves + + @api.multi + def write(self, vals): + not_paid_invoices = self.filtered(lambda move: move.is_invoice(include_receipts=True) and move.invoice_payment_state not in ('paid', 'in_payment')) + + if self._move_autocomplete_invoice_lines_write(vals): + res = True + else: + vals.pop('invoice_line_ids', None) + res = super(AccountMove, self.with_context(check_move_validity=False)).write(vals) + + # Ensure the move is still well balanced. + if 'line_ids' in vals and self._context.get('check_move_validity', True): + # 'check_move_validity' is needed as the write will be done line per line. + self._check_move_consistency() + + # Trigger 'action_invoice_paid' when the invoice becomes paid after a write. + not_paid_invoices.filtered(lambda move: move.invoice_payment_state in ('paid', 'in_payment')).action_invoice_paid() + + return res + + @api.multi + def unlink(self): + for move in self: + # check the lock date + check if some entries are reconciled + move.line_ids._update_check() + move.line_ids.unlink() + return super(AccountMove, self).unlink() + + @api.multi + @api.depends('name', 'state') + def name_get(self): + result = [] + for move in self: + if self._context.get('name_groupby') and not (move.type == 'entry' and move.state == 'draft'): + name = '**%s**, %s %s' % (format_date(self.env, move.date), move.name, move.partner_id.name or '') + elif move.type == 'entry': + # Miscellaneous operation. + if move.state == 'draft': + name = '* %s' % str(move.id) + else: + name = move.name + else: + # Invoice. + name = move._get_invoice_display_name(show_ref=True) + result.append((move.id, name)) + return result + + @api.multi + def _track_subtype(self, init_values): + # OVERRIDE to add custom subtype depending of the state. + self.ensure_one() + + if not self.is_invoice(include_receipts=True): + return super(AccountMove, self)._track_subtype(init_values) + + if 'invoice_payment_state' in init_values and self.invoice_payment_state == 'paid': + return self.env.ref('account.mt_invoice_paid') + elif 'state' in init_values and self.state == 'posted' and self.is_sale_document(include_receipts=True): + return self.env.ref('account.mt_invoice_validated') + elif 'state' in init_values and self.state == 'draft' and self.is_sale_document(include_receipts=True): + return self.env.ref('account.mt_invoice_created') + return super(AccountMove, self)._track_subtype(init_values) + + # ------------------------------------------------------------------------- + # BUSINESS METHODS + # ------------------------------------------------------------------------- @api.model - @api.depends('reconcile_model_id') - def _search_reconcile_model(self, operator, operand): - if operand: - rmi = self.search([('line_ids.reconcile_model_id', operator, operand)]) + def get_invoice_types(self, include_receipts=False): + return ['out_invoice', 'out_refund', 'in_refund', 'in_invoice'] + (include_receipts and ['out_receipt', 'in_receipt'] or []) + + def is_invoice(self, include_receipts=False): + return self.type in self.get_invoice_types(include_receipts) + + @api.model + def get_sale_types(self, include_receipts=False): + return ['out_invoice', 'out_refund'] + (include_receipts and ['out_receipt'] or []) + + def is_sale_document(self, include_receipts=False): + return self.type in self.get_sale_types(include_receipts) + + @api.model + def get_purchase_types(self, include_receipts=False): + return ['in_invoice', 'in_refund'] + (include_receipts and ['in_receipt'] or []) + + def is_purchase_document(self, include_receipts=False): + return self.type in self.get_purchase_types(include_receipts) + + @api.model + def get_inbound_types(self, include_receipts=True): + return ['out_invoice', 'in_refund'] + (include_receipts and ['out_receipt'] or []) + + def is_inbound(self, include_receipts=True): + return self.type in self.get_inbound_types(include_receipts) + + @api.model + def get_outbound_types(self, include_receipts=True): + return ['in_invoice', 'out_refund'] + (include_receipts and ['in_receipt'] or []) + + def is_outbound(self, include_receipts=True): + return self.type in self.get_outbound_types(include_receipts) + + @api.multi + def _get_invoice_reference_euro_invoice(self): + """ This computes the reference based on the RF Creditor Reference. + The data of the reference is the database id number of the invoice. + For instance, if an invoice is issued with id 43, the check number + is 07 so the reference will be 'RF07 43'. + """ + self.ensure_one() + base = self.id + check_digits = mod_97_10.calc_check_digits('{}RF'.format(base)) + reference = 'RF{} {}'.format(check_digits, " ".join(["".join(x) for x in zip_longest(*[iter(str(base))]*4, fillvalue="")])) + return reference + + @api.multi + def _get_invoice_reference_euro_partner(self): + """ This computes the reference based on the RF Creditor Reference. + The data of the reference is the user defined reference of the + partner or the database id number of the parter. + For instance, if an invoice is issued for the partner with internal + reference 'food buyer 654', the digits will be extracted and used as + the data. This will lead to a check number equal to 00 and the + reference will be 'RF00 654'. + If no reference is set for the partner, its id in the database will + be used. + """ + self.ensure_one() + partner_ref = self.partner_id.ref + partner_ref_nr = re.sub('\D', '', partner_ref or '')[-21:] or str(self.partner_id.id)[-21:] + partner_ref_nr = partner_ref_nr[-21:] + check_digits = mod_97_10.calc_check_digits('{}RF'.format(partner_ref_nr)) + reference = 'RF{} {}'.format(check_digits, " ".join(["".join(x) for x in zip_longest(*[iter(partner_ref_nr)]*4, fillvalue="")])) + return reference + + @api.multi + def _get_invoice_reference_odoo_invoice(self): + """ This computes the reference based on the Odoo format. + We simply return the number of the invoice, defined on the journal + sequence. + """ + self.ensure_one() + return self.name + + @api.multi + def _get_invoice_reference_odoo_partner(self): + """ This computes the reference based on the Odoo format. + The data used is the reference set on the partner or its database + id otherwise. For instance if the reference of the customer is + 'dumb customer 97', the reference will be 'CUST/dumb customer 97'. + """ + ref = self.partner_id.ref or str(self.partner_id.id) + prefix = _('CUST') + return '%s/%s' % (prefix, ref) + + @api.multi + def _get_invoice_computed_reference(self): + self.ensure_one() + if self.journal_id.invoice_reference_type == 'none': + return '' else: - rmi = self.search([('line_ids', operator, operand)]) - if rmi: - return [('id', 'in', rmi.ids)] - return [('id', '=', False)] + ref_function = getattr(self, '_get_invoice_reference_{}_{}'.format(self.journal_id.invoice_reference_model, self.journal_id.invoice_reference_type)) + if ref_function: + return ref_function() + else: + raise UserError(_('The combination of reference model and reference type on the journal is not implemented')) @api.multi - def _get_default_journal(self): - if self.env.context.get('default_journal_type'): - return self.env['account.journal'].search([('company_id', '=', self.env.company.id), ('type', '=', self.env.context['default_journal_type'])], limit=1).id + def _get_sequence(self): + ''' Return the sequence to be used during the post of the current move. + :return: An ir.sequence record or False. + ''' + self.ensure_one() + + journal = self.journal_id + if self.type in ('entry', 'out_invoice', 'in_invoice') or not journal.refund_sequence: + return journal.sequence_id + if not journal.refund_sequence_id: + return + return journal.refund_sequence_id + + @api.multi + def _get_invoice_display_name(self, show_ref=False): + ''' Helper to get the display name of an invoice depending of its type. + :param show_ref: A flag indicating of the display name must include or not the journal entry reference. + :return: A string representing the invoice. + ''' + self.ensure_one() + if self.state == 'draft': + return { + 'out_invoice': _('Draft Invoice'), + 'out_refund': _('Credit Note'), + 'in_invoice': _('Vendor Bill'), + 'in_refund': _('Vendor Credit Note'), + 'out_receipt': _('Sales Receipt'), + 'in_receipt': _('Purchase Receipt'), + }[self.type] + else: + return ('%s' % self.name) + (show_ref and self.ref and '(%s)' % self.ref or '') + + @api.multi + def _get_invoice_delivery_partner_id(self): + ''' Hook allowing to retrieve the right delivery address depending of installed modules. + :return: A res.partner record's id representing the delivery address. + ''' + self.ensure_one() + return self.partner_id.address_get(['delivery'])['delivery'] + + @api.multi + def _get_invoice_intrastat_country_id(self): + ''' Hook allowing to retrieve the intrastat country depending of installed modules. + :return: A res.country record's id. + ''' + self.ensure_one() + return self.partner_id.country_id.id + + @api.multi + def _get_cash_basis_matched_percentage(self): + """Compute the percentage to apply for cash basis method. This value is relevant only for moves that + involve journal items on receivable or payable accounts. + """ + self.ensure_one() + query = ''' + SELECT + ( + SELECT COALESCE(SUM(line.balance), 0.0) + FROM account_move_line line + JOIN account_account account ON account.id = line.account_id + JOIN account_account_type account_type ON account_type.id = account.user_type_id + WHERE line.move_id = %s AND account_type.type IN ('receivable', 'payable') + ) AS total_amount, + ( + SELECT COALESCE(SUM(partial.amount), 0.0) + FROM account_move_line line + JOIN account_account account ON account.id = line.account_id + JOIN account_account_type account_type ON account_type.id = account.user_type_id + LEFT JOIN account_partial_reconcile partial ON + partial.debit_move_id = line.id + OR + partial.credit_move_id = line.id + WHERE line.move_id = %s AND account_type.type IN ('receivable', 'payable') + ) AS total_reconciled + ''' + params = [self.id, self.id] + self._cr.execute(query, params) + total_amount, total_reconciled = self._cr.fetchone() + if float_is_zero(total_amount, precision_rounding=self.company_id.currency_id.rounding): + return 1.0 + else: + return abs(total_reconciled / total_amount) @api.multi - @api.depends('line_ids.partner_id') - def _compute_partner_id(self): - for move in self: - partner = move.line_ids.mapped('partner_id') - move.partner_id = partner.id if len(partner) == 1 else False - - @api.onchange('date') - def _onchange_date(self): - '''On the form view, a change on the date will trigger onchange() on account.move - but not on account.move.line even the date field is related to account.move. - Then, trigger the _onchange_amount_currency manually. + def _reverse_move_vals(self, default_values, cancel=True): + ''' Reverse values passed as parameter being the copied values of the original journal entry. + For example, debit / credit must be switched. The tax lines must be edited in case of refunds. + + :param default_values: A copy_date of the original journal entry. + :param cancel: A flag indicating the reverse is made to cancel the original journal entry. + :return: The updated default_values. ''' - self.line_ids._onchange_amount_currency() - - name = fields.Char(string='Number', required=True, copy=False, default='/') - ref = fields.Char(string='Reference', copy=False) - date = fields.Date(required=True, states={'posted': [('readonly', True)]}, index=True, default=fields.Date.context_today) - journal_id = fields.Many2one('account.journal', string='Journal', required=True, states={'posted': [('readonly', True)]}, default=_get_default_journal) - state = fields.Selection([('draft', 'Unposted'), ('posted', 'Posted')], string='Status', - required=True, readonly=True, copy=False, default='draft', - help='All manually created new journal entries are usually in the status \'Unposted\', ' - 'but you can set the option to skip that status on the related journal. ' - 'In that case, they will behave as journal entries automatically created by the ' - 'system on document validation (invoices, bank statements...) and will be created ' - 'in \'Posted\' status.') - line_ids = fields.One2many('account.move.line', 'move_id', string='Journal Items', - states={'posted': [('readonly', True)]}, copy=True) - partner_id = fields.Many2one('res.partner', compute='_compute_partner_id', string="Partner", store=True, readonly=True) - amount = fields.Monetary(compute='_amount_compute', inverse="_set_amount", store=True) - currency_id = fields.Many2one('res.currency', compute='_amount_compute', store=True, string="Currency") - narration = fields.Text(string='Internal Note') - company_id = fields.Many2one('res.company', related='journal_id.company_id', string='Company', store=True, readonly=True) - matched_percentage = fields.Float('Percentage Matched', compute='_compute_matched_percentage', digits=0, store=True, readonly=True, help="Technical field used in cash basis method") - reconcile_model_id = fields.Many2many('account.reconcile.model', compute='_compute_reconcile_model', search='_search_reconcile_model', string="Reconciliation Model", readonly=True) - # Dummy Account field to search on account.move by account_id - dummy_account_id = fields.Many2one('account.account', related='line_ids.account_id', string='Account', store=False, readonly=True) - tax_cash_basis_rec_id = fields.Many2one( - 'account.partial.reconcile', - string='Tax Cash Basis Entry of', - help="Technical field used to keep track of the tax cash basis reconciliation. " - "This is needed when cancelling the source: it will post the inverse journal entry to cancel that part too.") - auto_post = fields.Boolean(string='Post Automatically', default=False, help='If this checkbox is ticked, this entry will be automatically posted at its date.') - reverse_entry_id = fields.Many2one('account.move', String="Reverse entry", store=True, readonly=True) - to_check = fields.Boolean(string='To Check', default=False, help='If this checkbox is ticked, it means that the user was not sure of all the related informations at the time of the creation of the move and that the move needs to be checked again.') - tax_type_domain = fields.Char(store=False, help='Technical field used to have a dynamic taxes domain on the form view.') + self.ensure_one() - @api.constrains('line_ids', 'journal_id') - def _validate_move_modification(self): - if 'posted' in self.mapped('line_ids.payment_id.state'): - raise ValidationError(_("You cannot modify a journal entry linked to a posted payment.")) + def compute_tax_repartition_lines_mapping(move_vals): + ''' Computes and returns a mapping between the current repartition lines to the new expected one. + :param move_vals: The newly created invoice as a python dictionary to be passed to the 'create' method. + :return: A map invoice_repartition_line => refund_repartition_line. + ''' + # invoice_repartition_line => refund_repartition_line + mapping = {} + + # Do nothing if the move is not a credit note. + if move_vals['type'] not in ('out_refund', 'in_refund'): + return mapping + + for line_command in move_vals.get('line_ids', []): + line_vals = line_command[2] # (0, 0, {...}) + + if line_vals.get('tax_ids') and line_vals['tax_ids'][0][2]: + # Base line. + tax_ids = line_vals['tax_ids'][0][2] + elif line_vals.get('tax_line_id'): + # Tax line. + tax_ids = [line_vals['tax_line_id']] + else: + continue - @api.onchange('journal_id') - def _onchange_journal_id(self): - self.tax_type_domain = self.journal_id.type if self.journal_id.type in ('sale', 'purchase') else None + for tax in self.env['account.tax'].browse(tax_ids): + for inv_rep_line, ref_rep_line in zip(tax.invoice_repartition_line_ids, tax.refund_repartition_line_ids): + mapping[inv_rep_line] = ref_rep_line + return mapping - @api.onchange('line_ids') - def _onchange_line_ids(self): - '''Compute additional lines corresponding to the taxes set on the line_ids. + move_vals = self.with_context(include_business_fields=True).copy_data(default=default_values)[0] - For example, add a line with 1000 debit and 15% tax, this onchange will add a new - line with 150 debit. - ''' - def _str_to_list(string): - #remove heading and trailing brackets and return a list of int. This avoid calling safe_eval on untrusted field content - string = string[1:-1] - if string: - return [int(x) for x in string.split(',')] - return [] - - def _build_grouping_key(line): - #build a string containing all values used to create the tax line - return str(line.tax_ids.ids) + '-' + str(line.analytic_tag_ids.ids) + '-' + (line.analytic_account_id and str(line.analytic_account_id.id) or '') - - def _parse_grouping_key(line): - # Retrieve values computed the last time this method has been run. - if not line.tax_line_grouping_key: - return {'tax_ids': [], 'tag_ids': [], 'analytic_account_id': False} - tax_str, tags_str, analytic_account_str = line.tax_line_grouping_key.split('-') - return { - 'tax_ids': _str_to_list(tax_str), - 'tag_ids': _str_to_list(tags_str), - 'analytic_account_id': analytic_account_str and int(analytic_account_str) or False, - } + tax_repartition_lines_mapping = compute_tax_repartition_lines_mapping(move_vals) - def _find_existing_tax_line(line_ids, tax_repartition_line_id, analytic_tag_ids, analytic_account_id): - # tax_repartition_line_id, tag_ids and analytic_account_id are real ids - if tax.analytic: - return line_ids.filtered(lambda x: x.tax_repartition_line_id.id == tax_repartition_line_id and x.analytic_tag_ids.ids == analytic_tag_ids and x.analytic_account_id.id == analytic_account_id) - return line_ids.filtered(lambda x: x.tax_repartition_line_id.id == tax_repartition_line_id) + for line_command in move_vals.get('line_ids', []): + line_vals = line_command[2] # (0, 0, {...}) - def _get_lines_to_sum(line_ids, tax, tag_ids, analytic_account_id): - # tax is a real record; tag_ids and analytic_account_id are real ids - if tax.analytic: - return line_ids.filtered(lambda x: tax in x.tax_ids._origin and x.analytic_tag_ids.ids == tag_ids and x.analytic_account_id.id == analytic_account_id) - return line_ids.filtered(lambda x: tax in x.tax_ids._origin) + # ==== Inverse debit / credit / amount_currency ==== + amount_currency = -line_vals.get('amount_currency', 0.0) + balance = line_vals['credit'] - line_vals['debit'] - # Cache the already computed tax to avoid useless recalculation (real records) - processed_taxes = self.env['account.tax'] + line_vals.update({ + 'amount_currency': amount_currency, + 'debit': balance > 0.0 and balance or 0.0, + 'credit': balance < 0.0 and -balance or 0.0, + }) - self.ensure_one() - for line in self.line_ids.filtered(lambda x: x.recompute_tax_line): - # Retrieve old field values. - parsed_key = _parse_grouping_key(line) - - # Unmark the line. - line.recompute_tax_line = False - - # Manage group of taxes (new records) - group_taxes = line.tax_ids.filtered(lambda t: t.amount_type == 'group') - children_taxes = group_taxes.mapped('children_tax_ids') - if children_taxes: - line.tax_ids |= children_taxes - # Because the taxes on the line changed, we need to recompute them. - processed_taxes -= children_taxes._origin - - # Get the taxes to process (actual records) - taxes = self.env['account.tax'].browse(parsed_key['tax_ids']) - taxes |= line.tax_ids._origin - taxes |= children_taxes._origin - to_process_taxes = (taxes - processed_taxes).filtered(lambda t: t.amount_type != 'group') - processed_taxes += to_process_taxes - - # Apply tags on base line - line.tag_ids = taxes.mapped('invoice_repartition_line_ids').filtered(lambda x: x.repartition_type == 'base').mapped('tag_ids') - - # Process taxes (real records) - for tax in to_process_taxes: - lines_to_sum = _get_lines_to_sum(self.line_ids, tax, parsed_key['tag_ids'], parsed_key['analytic_account_id']) - - balance = sum([l.balance for l in lines_to_sum]) - - # Compute the tax amount one by one. - quantity = len(lines_to_sum) if tax.amount_type == 'fixed' else 1 - taxes_vals = tax.compute_all(balance, quantity=quantity, currency=line.currency_id, product=line.product_id, partner=line.partner_id) - - if taxes_vals.get('taxes'): - for line_vals in taxes_vals['taxes']: - tax_line = _find_existing_tax_line(self.line_ids, line_vals['tax_repartition_line_id'], parsed_key['tag_ids'], parsed_key['analytic_account_id']) - if tax_line: - if not lines_to_sum: - # Drop tax line because the originator tax is no longer used. - self.line_ids -= tax_line - elif balance: - # Update the existing tax_line. - # Update the debit/credit amount according to the new balance. - amount = line_vals['amount'] - account = line_vals['account_id'] or line.account_id - tax_line.debit = amount > 0 and amount or 0.0 - tax_line.credit = amount < 0 and -amount or 0.0 - tax_line.account_id = account - else: - # Reset debit/credit in case of the originator line is temporary set to 0 in both debit/credit. - tax_line.debit = tax_line.credit = 0.0 - else: - # Create a new tax_line. - amount = line_vals['amount'] - to_create_vals = { - 'account_id': line_vals['account_id'] or line.account_id.id, - 'name': line_vals['name'], - 'partner_id': line.partner_id.id, - 'debit': amount > 0 and amount or 0.0, - 'credit': amount < 0 and -amount or 0.0, - 'analytic_account_id': line.analytic_account_id.id if tax.analytic else False, - 'analytic_tag_ids': line.analytic_tag_ids.ids if tax.analytic else False, - 'move_id': self.id, - 'tax_exigible': tax.tax_exigibility == 'on_invoice', - 'company_id': self.company_id.id, - 'company_currency_id': self.company_id.currency_id.id, - 'tax_repartition_line_id': line_vals['tax_repartition_line_id'], - 'tag_ids': line_vals['tag_ids'], - 'tax_base_amount': line_vals['base'], - } - # N.B. currency_id/amount_currency are not set because if we have two lines with the same tax - # and different currencies, we have no idea which currency set on this line. - self.env['account.move.line'].new(to_create_vals) - - # Keep record of the values used as taxes the last time this method has been run. - line.tax_line_grouping_key = _build_grouping_key(line) + if move_vals['type'] not in ('out_refund', 'in_refund'): + continue - @api.model - def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False): - res = super(AccountMove, self).fields_view_get( - view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu) - if self._context.get('vat_domain'): - res['fields']['line_ids']['views']['tree']['fields']['tax_line_id']['domain'] = [('tag_ids', 'in', [self.env.ref(self._context.get('vat_domain')).id])] - return res + # ==== Map tax repartition lines ==== + if line_vals.get('tax_ids') and line_vals['tax_ids'][0][2]: + # Base line. + invoice_repartition_lines = self.env['account.tax'].browse(line_vals['tax_ids'][0][2])\ + .mapped('invoice_repartition_line_ids')\ + .filtered(lambda line: line.repartition_type == 'base') + refund_repartition_lines = invoice_repartition_lines\ + .mapped(lambda line: tax_repartition_lines_mapping[line]) + + line_vals['tag_ids'] = [(6, 0, refund_repartition_lines.mapped('tag_ids').ids)] + elif line_vals.get('tax_repartition_line_id'): + # Tax line. + invoice_repartition_line = self.env['account.tax.repartition.line'].browse(line_vals['tax_repartition_line_id']) + refund_repartition_line = tax_repartition_lines_mapping[invoice_repartition_line] + + # Find the right account. + account_id = self.env['account.move.line']._get_default_tax_account(refund_repartition_line).id + if not account_id: + if not invoice_repartition_line.account_id: + # Keep the current account as the current one comes from the base line. + account_id = line_vals['account_id'] + else: + tax = invoice_repartition_line.invoice_tax_id + base_line = self.line_ids.filtered(lambda line: tax in line.tax_ids)[0] + account_id = base_line.account_id.id - @api.model - def create(self, vals): - move = super(AccountMove, self.with_context(mail_create_nolog=True, check_move_validity=False, partner_id=vals.get('partner_id'))).create(vals) - move.assert_balanced() - return move + line_vals.update({ + 'tax_repartition_line_id': refund_repartition_line.id, + 'account_id': account_id, + 'tag_ids': [(6, 0, refund_repartition_line.tag_ids.ids)], + }) + return move_vals @api.multi - def write(self, vals): - if 'line_ids' in vals: - res = super(AccountMove, self.with_context(check_move_validity=False)).write(vals) - self.assert_balanced() - else: - res = super(AccountMove, self).write(vals) - return res + def _reverse_moves(self, default_values_list=None, cancel=False): + ''' Reverse a recordset of account.move. + If cancel parameter is true, the reconcilable or liquidity lines + of each original move will be reconciled with its reverse's. + + :param default_values_list: A list of default values to consider per move. + ('type' & 'reversed_entry_id' are computed in the method). + :return: An account.move recordset, reverse of the current self. + ''' + if not default_values_list: + default_values_list = [{} for move in self] + + if cancel: + lines = self.mapped('line_ids') + # Avoid maximum recursion depth. + if lines: + lines.remove_move_reconcile() + + reverse_type_map = { + 'entry': 'entry', + 'out_invoice': 'out_refund', + 'in_invoice': 'in_refund', + 'in_refund': 'in_invoice', + 'out_receipt': 'in_receipt', + 'in_receipt': 'out_receipt', + } + + move_vals_list = [] + for move, default_values in zip(self, default_values_list): + default_values.update({ + 'type': reverse_type_map[self.type], + 'reversed_entry_id': self.id, + }) + move_vals_list.append(move._reverse_move_vals(default_values, cancel=cancel)) + reverse_moves = self.env['account.move'].create(move_vals_list) + + # Reconcile moves together to cancel the previous one. + if cancel: + reverse_moves.with_context(move_reverse_cancel=cancel).post() + for move, reverse_move in zip(self, reverse_moves): + accounts = move.mapped('line_ids.account_id') \ + .filtered(lambda account: account.reconcile or account.internal_type == 'liquidity') + for account in accounts: + (move.line_ids + reverse_move.line_ids)\ + .filtered(lambda line: line.account_id == account and line.balance)\ + .reconcile() + + return reverse_moves + + @api.multi + def open_reconcile_view(self): + return self.line_ids.open_reconcile_view() @api.multi def post(self): - self._post_validate() + for move in self: + if move.auto_post and move.date > fields.Date.today(): + date_msg = move.date.strftime(self.env['res.lang']._lang_get(self.env.user.lang).date_format) + raise UserError(_("This move is configured to be auto-posted on %s" % date_msg)) + + if not move.partner_id: + if move.is_sale_document(): + raise UserError(_("The field 'Customer' is required, please complete it to validate the Customer Invoice.")) + elif move.is_purchase_document(): + raise UserError(_("The field 'Vendor' is required, please complete it to validate the Vendor Bill.")) + + if move.is_invoice(include_receipts=True) and float_compare(move.amount_total, 0.0, precision_rounding=move.currency_id.rounding) < 0: + raise UserError(_("You cannot validate an invoice with a negative total amount. You should create a credit note instead.")) + + # Handle case when the invoice_date is not set. In that case, the invoice_date is set at today and then, + # lines are recomputed accordingly. + if not move.invoice_date and move.is_invoice(include_receipts=True): + move.invoice_date = fields.Date.context_today(self) + move.with_context(check_move_validity=False)._onchange_invoice_date() + + self._check_tax_lock_date() + self._check_move_consistency() # Create the analytic lines in batch is faster as it leads to less cache invalidation. self.mapped('line_ids').create_analytic_lines() for move in self: if move.auto_post and move.date > fields.Date.today(): raise UserError(_("This move is configured to be auto-posted on {}".format(move.date.strftime(self.env['res.lang']._lang_get(self.env.user.lang).date_format)))) + to_write = {'state': 'posted'} if move.name == '/': - new_name = False - journal = move.journal_id + # Get the journal's sequence. + sequence = move._get_sequence() + if not sequence: + raise UserError(_('Please define a sequence on your journal.')) - if journal.sequence_id: - sequence = journal.sequence_id - new_name = sequence.with_context(ir_sequence_date=move.date).next_by_id() - else: - raise UserError(_('Please define a sequence on the journal.')) + # Consume a new number. + to_write['name'] = sequence.next_by_id(sequence_date=move.date) + + move.write(to_write) - if new_name: - move.name = new_name + # Compute 'ref' for 'out_invoice'. + if move.type == 'out_invoice' and not move.invoice_payment_ref: + to_write = { + 'invoice_payment_ref': move._get_invoice_computed_reference(), + 'line_ids': [] + } + for line in move.line_ids.filtered(lambda line: line.account_id.user_type_id.type in ('receivable', 'payable')): + to_write['line_ids'].append((1, line.id, {'name': to_write['invoice_payment_ref']})) + move.write(to_write) if move == move.company_id.account_opening_move_id and not move.company_id.account_bank_reconciliation_start: # For opening moves, we set the reconciliation date threshold @@ -352,22 +1906,41 @@ def post(self): # installing Accounting- with bank statements) move.company_id.account_bank_reconciliation_start = move.date - return self.write({'state': 'posted'}) + @api.multi + def action_reverse(self): + action = self.env.ref('account.action_view_account_move_reversal').read()[0] + + if self.is_invoice(): + action['name'] = _('Credit Note') + + return action @api.multi def action_post(self): - if self.mapped('line_ids.payment_id'): - if any(self.mapped('journal_id.post_at_bank_rec')): - raise UserError(_("A payment journal entry generated in a journal configured to post entries only when payments are reconciled with a bank statement cannot be manually posted. Those will be posted automatically after performing the bank reconciliation.")) + if self.mapped('line_ids.payment_id') and any(self.mapped('journal_id.post_at_bank_rec')): + raise UserError(_("A payment journal entry generated in a journal configured to post entries only when payments are reconciled with a bank statement cannot be manually posted. Those will be posted automatically after performing the bank reconciliation.")) return self.post() + @api.multi + def js_assign_outstanding_line(self, line_id): + self.ensure_one() + lines = self.env['account.move.line'].browse(line_id) + lines += self.line_ids.filtered(lambda line: line.account_id == lines[0].account_id and not line.reconciled) + return lines.reconcile() + + @api.multi + def button_draft(self): + if any(move.state != 'cancel' for move in self): + raise UserError(_('Only cancelled journal entries can be reset to draft.')) + self.write({'state': 'draft'}) + @api.multi def button_cancel(self): AccountMoveLine = self.env['account.move.line'] excluded_move_ids = [] - if self._context.get('edition_mode'): - excluded_move_ids = AccountMoveLine.search(AccountMoveLine._get_domain_for_edition_mode() + [('move_id', 'in', self.ids)]).mapped('move_id').ids + if self._context.get('suspense_moves_mode'): + excluded_move_ids = AccountMoveLine.search(AccountMoveLine._get_suspense_moves_domain() + [('move_id', 'in', self.ids)]).mapped('move_id').ids for move in self: if not move.journal_id.update_posted and move.id not in excluded_move_ids: @@ -377,122 +1950,130 @@ def button_cancel(self): if self.ids: self.check_access_rights('write') self.check_access_rule('write') - self._check_lock_date() - self._cr.execute('UPDATE account_move '\ - 'SET state=%s '\ - 'WHERE id IN %s', ('draft', tuple(self.ids),)) + self._check_fiscalyear_lock_date() + self._cr.execute('UPDATE account_move SET state=%s WHERE id IN %s', ('cancel', tuple(self.ids))) self.invalidate_cache() - self._check_lock_date() + self._check_fiscalyear_lock_date() + self.mapped('line_ids').remove_move_reconcile() return True @api.multi - def unlink(self): - for move in self: - #check the lock date + check if some entries are reconciled - move.line_ids._update_check() - move.line_ids.unlink() - return super(AccountMove, self).unlink() + def action_invoice_sent(self): + """ Open a window to compose an email, with the edi invoice template + message loaded by default + """ + self.ensure_one() + template = self.env.ref('account.email_template_edi_invoice', raise_if_not_found=False) + compose_form = self.env.ref('account.account_invoice_send_wizard_form', raise_if_not_found=False) + ctx = dict( + default_model='account.move', + default_res_id=self.id, + default_use_template=bool(template), + default_template_id=template and template.id or False, + default_composition_mode='comment', + mark_invoice_as_sent=True, + custom_layout="mail.mail_notification_paynow", + force_email=True + ) + return { + 'name': _('Send Invoice'), + 'type': 'ir.actions.act_window', + 'view_type': 'form', + 'view_mode': 'form', + 'res_model': 'account.invoice.send', + 'views': [(compose_form.id, 'form')], + 'view_id': compose_form.id, + 'target': 'new', + 'context': ctx, + } @api.multi - def _post_validate(self): - for move in self: - if move.line_ids: - if not all([x.company_id.id == move.company_id.id for x in move.line_ids]): - raise UserError(_("Cannot create moves for different companies.")) - self.assert_balanced() - return self._check_lock_date() + def action_invoice_print(self): + """ Print the invoice and mark it as sent, so that we can see more + easily the next step of the workflow + """ + if any(not move.is_invoice(include_receipts=True) for move in self): + raise UserError(_("Only invoices could be printed.")) + + self.filtered(lambda inv: not inv.invoice_sent).write({'invoice_sent': True}) + if self.user_has_groups('account.group_account_invoice'): + return self.env.ref('account.account_invoices').report_action(self) + else: + return self.env.ref('account.account_invoices_without_payment').report_action(self) @api.multi - def _check_lock_date(self): - for move in self: - lock_date = max(move.company_id.period_lock_date or date.min, move.company_id.fiscalyear_lock_date or date.min) - if self.user_has_groups('account.group_account_manager'): - lock_date = move.company_id.fiscalyear_lock_date - if move.date <= (lock_date or date.min): - if self.user_has_groups('account.group_account_manager'): - message = _("You cannot add/modify entries prior to and inclusive of the lock date %s") % (lock_date) - else: - message = _("You cannot add/modify entries prior to and inclusive of the lock date %s. Check the company settings or ask someone with the 'Adviser' role") % (lock_date) - raise UserError(message) - return True + def action_invoice_paid(self): + ''' Hook to be overrided called when the invoice moves to the paid state. ''' + pass @api.multi - def assert_balanced(self): - if not self.ids: - return True - prec = self.env.company.currency_id.decimal_places - - self._cr.execute("""\ - SELECT move_id - FROM account_move_line - WHERE move_id in %s - GROUP BY move_id - HAVING abs(sum(debit) - sum(credit)) > %s - """, (tuple(self.ids), 10 ** (-max(5, prec)))) - if len(self._cr.fetchall()) != 0: - raise UserError(_("Cannot create unbalanced journal entry.")) - return True + def action_open_matching_suspense_moves(self): + self.ensure_one() + domain = self._get_domain_matching_supsense_moves() + ids = self.env['account.move.line'].search(domain).mapped('statement_line_id').ids + action_context = {'show_mode_selector': False, 'company_ids': self.mapped('company_id').ids} + action_context.update({'suspense_moves_mode': True}) + action_context.update({'statement_line_ids': ids}) + action_context.update({'partner_id': self.partner_id.id}) + action_context.update({'partner_name': self.partner_id.name}) + return { + 'type': 'ir.actions.client', + 'tag': 'bank_statement_reconciliation_view', + 'context': action_context, + } + + @api.multi + def action_invoice_register_payment(self): + return self.env['account.payment']\ + .with_context(active_ids=self.ids, active_model='account.move', active_id=self.id)\ + .action_register_payment() + + @api.multi + def _get_report_base_filename(self): + if any(not move.is_invoice() for move in self): + raise UserError(_("Only invoices could be printed.")) + return self._get_invoice_display_name() @api.multi - def _reverse_move(self, date=None, journal_id=None): + def preview_invoice(self): self.ensure_one() - date = date or fields.Date.today() - with self.env.norecompute(): - reversed_move = self.copy(default={ - 'date': date, - 'journal_id': journal_id.id if journal_id else self.journal_id.id, - 'ref': _('Reversal of: %s') % (self.name), - }) - for acm_line in reversed_move.line_ids.with_context(check_move_validity=False): - acm_line.write({ - 'debit': acm_line.credit, - 'credit': acm_line.debit, - 'amount_currency': -acm_line.amount_currency - }) - self.reverse_entry_id = reversed_move - self.recompute() - return reversed_move - - @api.multi - def reverse_moves(self, date=None, journal_id=None): - date = date or fields.Date.today() - reversed_moves = self.env['account.move'] - for ac_move in self: - #unreconcile all lines reversed - aml = ac_move.line_ids.filtered(lambda x: x.account_id.reconcile or x.account_id.internal_type == 'liquidity') - aml.remove_move_reconcile() - reversed_move = ac_move._reverse_move(date=date, journal_id=journal_id) - reversed_moves |= reversed_move - #reconcile together the reconcilable (or the liquidity aml) and their newly created counterpart - for account in set([x.account_id for x in aml]): - to_rec = aml.filtered(lambda y: y.account_id == account) - to_rec |= reversed_move.line_ids.filtered(lambda y: y.account_id == account) - #reconciliation will be full, so speed up the computation by using skip_full_reconcile_check in the context - to_rec.reconcile() - if reversed_moves: - reversed_moves._post_validate() - reversed_moves.post() - return [x.id for x in reversed_moves] - return [] + return { + 'type': 'ir.actions.act_url', + 'target': 'self', + 'url': self.get_portal_url(), + } @api.multi - def open_reconcile_view(self): - return self.line_ids.open_reconcile_view() + def _compute_access_url(self): + super(AccountMove, self)._compute_access_url() + for move in self.filtered(lambda move: move.is_invoice()): + move.access_url = '/my/invoices/%s' % (move.id) - # FIXME: Clarify me and change me in master @api.multi - def action_duplicate(self): + def action_view_reverse_entry(self): self.ensure_one() - action = self.env.ref('account.action_move_journal_line').read()[0] - action['context'] = dict(self.env.context) - action['context']['form_view_initial_mode'] = 'edit' - action['context']['view_no_maturity'] = False - action['views'] = [(self.env.ref('account.view_move_form').id, 'form')] - action['res_id'] = self.copy().id + + # Create action. + action = { + 'name': _('Reverse Moves'), + 'type': 'ir.actions.act_window', + 'res_model': 'account.move', + } + reverse_entries = self.env['account.move'].search([('reversed_entry_id', '=', self.id)]) + if len(reverse_entries) == 1: + action.update({ + 'view_mode': 'form', + 'res_id': reverse_entries.id, + }) + else: + action.update({ + 'view_mode': 'tree', + 'domain': [('id', 'in', reverse_entries.ids)], + }) return action @api.model - def _run_post_draft_to_post(self): + def _autopost_draft_entries(self): ''' This method is called from a cron job. It is used to post entries such as those created by the module account_asset. @@ -504,46 +2085,546 @@ def _run_post_draft_to_post(self): ]) records.post() - @api.multi - def action_view_reverse_entry(self): - action = self.env.ref('account.action_move_journal_line').read()[0] - action['views'] = [(self.env.ref('account.view_move_form').id, 'form')] - action['res_id'] = self.reverse_entry_id.id - return action class AccountMoveLine(models.Model): _name = "account.move.line" _description = "Journal Item" - _order = "date desc, move_name desc, id desc" + _order = "sequence, date desc, move_name desc, id" + + # ==== Business fields ==== + move_id = fields.Many2one('account.move', string='Journal Entry', + index=True, required=True, auto_join=True, ondelete="cascade", + help="The move of this entry line.") + move_name = fields.Char(string='Number', related='move_id.name', store=True, index=True) + date = fields.Date(related='move_id.date', store=True, readonly=True, index=True, copy=False) + ref = fields.Char(related='move_id.ref', store=True, copy=False, index=True, readonly=False) + parent_state = fields.Selection(related='move_id.state', store=True, readonly=True) + journal_id = fields.Many2one(related='move_id.journal_id', store=True, readonly=False, index=True, copy=False) + company_id = fields.Many2one(related='move_id.company_id', store=True, readonly=True) + company_currency_id = fields.Many2one(related='company_id.currency_id', string='Company Currency', + readonly=True, store=True, + help='Utility field to express amount currency') + account_id = fields.Many2one('account.account', string='Account', + index=True, ondelete="cascade", + domain=[('deprecated', '=', False)]) + account_internal_type = fields.Selection(related='account_id.user_type_id.type', string="Internal Type", store=True, readonly=True) + sequence = fields.Integer(default=10) + name = fields.Char(string='Label') + quantity = fields.Float(string='Quantity', + default=1.0, digits=dp.get_precision('Product Unit of Measure'), + help="The optional quantity expressed by this line, eg: number of product sold." + "The quantity is not a legal requirement but is very useful for some reports.") + price_unit = fields.Float(string='Unit Price', digits=dp.get_precision('Product Price')) + discount = fields.Float(string='Discount (%)', digits=dp.get_precision('Discount'), default=0.0) + debit = fields.Monetary(string='Debit', default=0.0, currency_field='company_currency_id') + credit = fields.Monetary(string='Credit', default=0.0, currency_field='company_currency_id') + balance = fields.Monetary(string='Balance', store=True, + currency_field='company_currency_id', + compute='_compute_balance', + help="Technical field holding the debit - credit in order to open meaningful graph views from reports") + amount_currency = fields.Monetary(string='Balance in Currency', store=True, copy=True, + help="The amount expressed in an optional other currency if it is a multi-currency entry.") + price_subtotal = fields.Monetary(string='Subtotal', store=True, readonly=True, + currency_field='always_set_currency_id') + price_total = fields.Monetary(string='Total', store=True, readonly=True, + currency_field='always_set_currency_id') + reconciled = fields.Boolean(compute='_amount_residual', store=True) + blocked = fields.Boolean(string='No Follow-up', default=False, + help="You can check this box to mark this journal item as a litigation with the associated partner") + date_maturity = fields.Date(string='Due date', index=True, + help="This field is used for payable and receivable journal entries. You can put the limit date for the payment of this line.") + currency_id = fields.Many2one('res.currency', string='Currency') + partner_id = fields.Many2one('res.partner', string='Partner', ondelete='restrict') + product_uom_id = fields.Many2one('uom.uom', string='Unit of Measure') + product_id = fields.Many2one('product.product', string='Product') + + # ==== Origin fields ==== + reconcile_model_id = fields.Many2one('account.reconcile.model', string="Reconciliation Model", copy=False) + payment_id = fields.Many2one('account.payment', string="Originator Payment", copy=False, + help="Payment that created this entry") + statement_line_id = fields.Many2one('account.bank.statement.line', + string='Bank statement line reconciled with this entry', + index=True, copy=False, readonly=True) + statement_id = fields.Many2one(related='statement_line_id.statement_id', store=True, index=True, copy=False, + help="The bank statement used for bank reconciliation") + + # ==== Tax fields ==== + tax_ids = fields.Many2many('account.tax', string='Taxes') + tax_line_id = fields.Many2one('account.tax', string='Originator tax', ondelete='restrict', store=True, + compute='_compute_tax_line_id') + tax_base_amount = fields.Monetary(string="Base Amount", store=True, + currency_field='company_currency_id') + tax_exigible = fields.Boolean(string='Appears in VAT report', default=True, + help="Technical field used to mark a tax line as exigible in the vat report or not (only exigible journal items" + " are displayed). By default all new journal items are directly exigible, but with the feature cash_basis" + " on taxes, some will become exigible only when the payment is recorded.") + tax_repartition_line_id = fields.Many2one(comodel_name='account.tax.repartition.line', + string="Originator Tax Repartition Line", ondelete='restrict', + help="Tax repartition line that caused the creation of this move line, if any") + tag_ids = fields.Many2many(string="Tags", comodel_name='account.account.tag', ondelete='restrict', + help="Tags assigned to this line by the tax creating it, if any. It determines its impact on financial reports.") + tax_audit = fields.Char(string="Tax Audit String", compute="_compute_tax_audit", store=True, + help="Computed field, listing the tax grids impacted by this line, and the amount it applies to each of them.") + + # ==== Reconciliation fields ==== + amount_residual = fields.Monetary(string='Residual Amount', store=True, + currency_field='company_currency_id', + compute='_amount_residual', + help="The residual amount on a journal item expressed in the company currency.") + amount_residual_currency = fields.Monetary(string='Residual Amount in Currency', store=True, + compute='_amount_residual', + help="The residual amount on a journal item expressed in its currency (possibly not the company currency).") + full_reconcile_id = fields.Many2one('account.full.reconcile', string="Matching Number", copy=False, index=True) + matched_debit_ids = fields.One2many('account.partial.reconcile', 'credit_move_id', String='Matched Debits', + help='Debit journal items that are matched with this journal item.') + matched_credit_ids = fields.One2many('account.partial.reconcile', 'debit_move_id', String='Matched Credits', + help='Credit journal items that are matched with this journal item.') + + # ==== Analytic fields ==== + analytic_line_ids = fields.One2many('account.analytic.line', 'move_id', string='Analytic lines') + analytic_account_id = fields.Many2one('account.analytic.account', string='Analytic Account', index=True) + analytic_tag_ids = fields.Many2many('account.analytic.tag', string='Analytic Tags') + + # ==== Onchange / display purpose fields ==== + recompute_tax_line = fields.Boolean(store=False, readonly=True, + help="Technical field used to know on which lines the taxes must be recomputed.") + display_type = fields.Selection([ + ('line_section', 'Section'), + ('line_note', 'Note'), + ], default=False, help="Technical field for UX purpose.") + is_rounding_line = fields.Boolean(help="Technical field used to retrieve the cash rounding line.") + exclude_from_invoice_tab = fields.Boolean(help="Technical field used to exclude some lines from the invoice_line_ids tab in the form view.") + always_set_currency_id = fields.Many2one('res.currency', string='Foreign Currency', + compute='_compute_always_set_currency_id', + help="Technical field used to compute the monetary field. As currency_id is not a required field, we need to use either the foreign currency, either the company one.") + + _sql_constraints = [ + ( + 'check_credit_debit', + 'CHECK(credit + debit>=0 AND credit * debit=0)', + 'Wrong credit or debit value in accounting entry !' + ), + ( + 'check_accountable_required_fields', + "CHECK(display_type IN ('line_section', 'line_note') OR account_id IS NOT NULL)", + "Missing required account on accountable invoice line." + ), + ( + 'check_non_accountable_fields_null', + "CHECK(display_type NOT IN ('line_section', 'line_note') OR (amount_currency = 0 AND debit = 0 AND credit = 0 AND account_id IS NULL))", + "Forbidden unit price, account and quantity on non-accountable invoice line" + ), + ( + 'check_amount_currency_balance_sign', + '''CHECK( + currency_id IS NULL + OR + company_currency_id IS NULL + OR + ( + (currency_id != company_currency_id) + AND + ( + (balance > 0 AND amount_currency > 0) + OR (balance <= 0 AND amount_currency <= 0) + OR (balance >= 0 AND amount_currency >= 0) + ) + ) + )''', + "The amount expressed in the secondary currency must be positive when account is debited and negative when account is credited." + ), + ] + + # ------------------------------------------------------------------------- + # HELPERS + # ------------------------------------------------------------------------- + + @api.model + def _get_default_tax_account(self, repartition_line): + tax = repartition_line.invoice_tax_id or repartition_line.refund_tax_id + if tax.tax_exigibility == 'on_payment': + account = tax.cash_basis_transition_account_id + else: + account = repartition_line.account_id + return account + + @api.multi + def _get_computed_name(self): + self.ensure_one() + + if not self.product_id: + return '' + + if self.partner_id.lang: + product = self.product_id.with_context(lang=self.partner_id.lang) + else: + product = self.product_id + + values = [] + if product.partner_ref: + values.append(product.partner_ref) + if self.journal_id.type == 'sale': + if product.description_sale: + values.append(product.description_sale) + elif self.journal_id.type == 'purchase': + if product.description_purchase: + values.append(product.description_purchase) + return '\n'.join(values) + + @api.multi + def _get_computed_price_unit(self): + self.ensure_one() + + if not self.product_id: + return self.price_unit + elif self.move_id.is_sale_document(include_receipts=True): + # Out invoice. + price_unit = self.product_id.lst_price + elif self.move_id.is_purchase_document(include_receipts=True): + # In invoice. + price_unit = self.product_id.standard_price + else: + return self.price_unit + + if self.product_uom_id != self.product_id.uom_id: + price_unit = self.product_id.uom_id._compute_price(price_unit, self.product_uom_id) + + company = self.move_id.company_id + if self.move_id.currency_id != company.currency_id: + price_unit = company.currency_id._convert( + price_unit, self.move_id.currency_id, company, self.move_id.date) + return price_unit + + @api.multi + def _get_computed_account(self): + self.ensure_one() + + if not self.product_id: + return + + fiscal_position = self.move_id.fiscal_position_id + accounts = self.product_id.product_tmpl_id.get_product_accounts(fiscal_pos=fiscal_position) + if self.move_id.is_sale_document(include_receipts=True): + # Out invoice. + return accounts['income'] + elif self.move_id.is_purchase_document(include_receipts=True): + # In invoice. + return accounts['expense'] + + @api.multi + def _get_computed_taxes(self): + self.ensure_one() + + if self.move_id.is_sale_document(include_receipts=True): + # Out invoice. + if self.product_id.taxes_id: + tax_ids = self.product_id.taxes_id.filtered(lambda tax: tax.company_id == self.move_id.company_id) + elif self.account_id.tax_ids: + tax_ids = self.account_id.tax_ids + else: + tax_ids = self.env['account.tax'] + if not tax_ids and not self.exclude_from_invoice_tab: + tax_ids = self.move_id.company_id.account_sale_tax_id + elif self.move_id.is_purchase_document(include_receipts=True): + # In invoice. + if self.product_id.supplier_taxes_id: + tax_ids = self.product_id.supplier_taxes_id.filtered(lambda tax: tax.company_id == self.move_id.company_id) + elif self.account_id.tax_ids: + tax_ids = self.account_id.tax_ids + else: + tax_ids = self.env['account.tax'] + if not tax_ids and not self.exclude_from_invoice_tab: + tax_ids = self.move_id.company_id.account_purchase_tax_id + else: + # Miscellaneous operation. + tax_ids = self.account_id.tax_ids + + if self.company_id: + tax_ids = tax_ids.filtered(lambda tax: tax.company_id == self.company_id) + + fiscal_position = self.move_id.fiscal_position_id + if tax_ids and fiscal_position: + return fiscal_position.map_tax(tax_ids, partner=self.partner_id) + else: + return tax_ids + + @api.multi + def _get_computed_uom(self): + self.ensure_one() + if self.product_id: + return self.product_id.uom_id + return False + + @api.multi + def _get_price_total_and_subtotal(self, price_unit=None, quantity=None, discount=None, currency=None, product=None, partner=None, taxes=None, move_type=None): + self.ensure_one() + return self._get_price_total_and_subtotal_model( + price_unit=price_unit or self.price_unit, + quantity=quantity or self.quantity, + discount=discount or self.discount, + currency=currency or self.currency_id, + product=product or self.product_id, + partner=partner or self.partner_id, + taxes=taxes or self.tax_ids, + move_type=move_type or self.move_id.type, + ) + + @api.model + def _get_price_total_and_subtotal_model(self, price_unit, quantity, discount, currency, product, partner, taxes, move_type): + ''' This method is used to compute 'price_total' & 'price_subtotal'. + + :param price_unit: The current price unit. + :param quantity: The current quantity. + :param discount: The current discount. + :param currency: The line's currency. + :param product: The line's product. + :param partner: The line's partner. + :param taxes: The applied taxes. + :param move_type: The type of the move. + :return: A dictionary containing 'price_subtotal' & 'price_total'. + ''' + res = {} + + # Compute 'price_subtotal'. + price_unit_wo_discount = price_unit * (1 - (discount / 100.0)) + subtotal = quantity * price_unit_wo_discount + + # Compute 'price_total'. + if taxes: + taxes_res = taxes._origin.compute_all(price_unit_wo_discount, + quantity=quantity, currency=currency, product=product, partner=partner, is_refund=move_type in ('out_refund', 'in_refund')) + res['price_subtotal'] = taxes_res['total_excluded'] + res['price_total'] = taxes_res['total_included'] + else: + res['price_total'] = res['price_subtotal'] = subtotal + return res + + @api.multi + def _get_fields_onchange_subtotal(self, price_subtotal=None, move_type=None, currency=None, company=None, date=None): + self.ensure_one() + return self._get_fields_onchange_subtotal_model( + price_subtotal=price_subtotal or self.price_subtotal, + move_type=move_type or self.move_id.type, + currency=currency or self.currency_id, + company=company or self.move_id.company_id, + date=date or self.move_id.date, + ) + + @api.model + def _get_fields_onchange_subtotal_model(self, price_subtotal, move_type, currency, company, date): + ''' This method is used to recompute the values of 'amount_currency', 'debit', 'credit' due to a change made + in some business fields (affecting the 'price_subtotal' field). + + :param price_subtotal: The untaxed amount. + :param move_type: The type of the move. + :param currency: The line's currency. + :param company: The move's company. + :param date: The move's date. + :return: A dictionary containing 'debit', 'credit', 'amount_currency'. + ''' + if move_type in self.move_id.get_outbound_types(): + sign = 1 + elif move_type in self.move_id.get_inbound_types(): + sign = -1 + else: + sign = 1 + price_subtotal *= sign + + if currency and currency != company.currency_id: + # Multi-currencies. + balance = currency._convert(price_subtotal, company.currency_id, company, date) + return { + 'amount_currency': price_subtotal, + 'debit': balance > 0.0 and balance or 0.0, + 'credit': balance < 0.0 and -balance or 0.0, + } + else: + # Single-currency. + return { + 'amount_currency': 0.0, + 'debit': price_subtotal > 0.0 and price_subtotal or 0.0, + 'credit': price_subtotal < 0.0 and -price_subtotal or 0.0, + } + + @api.multi + def _get_fields_onchange_balance(self, quantity=None, discount=None, balance=None, move_type=None, currency=None, taxes=None): + self.ensure_one() + return self._get_fields_onchange_balance_model( + quantity=quantity or self.quantity, + discount=discount or self.discount, + balance=balance or self.balance, + move_type=move_type or self.move_id.type, + currency=currency or self.currency_id, + taxes=taxes or self.tax_ids, + ) + + @api.model + def _get_fields_onchange_balance_model(self, quantity, discount, balance, move_type, currency, taxes): + ''' This method is used to recompute the values of 'quantity', 'discount', 'price_unit' due to a change made + in some accounting fields such as 'balance'. + + This method is a bit complex as we need to handle some special cases. + For example, setting a positive balance with a 100% discount. + + :param quantity: The current quantity. + :param discount: The current discount. + :param balance: The new balance. + :param move_type: The type of the move. + :param currency: The currency. + :param taxes: The applied taxes. + :return: A dictionary containing 'quantity', 'discount', 'price_unit'. + ''' + if move_type in self.move_id.get_outbound_types(): + sign = 1 + elif move_type in self.move_id.get_inbound_types(): + sign = -1 + else: + sign = 1 + balance *= sign + + taxes = taxes.flatten_taxes_hierarchy() + if taxes and any(tax.price_include for tax in taxes): + # Inverse taxes. E.g: + # + # Price Unit | Taxes | Originator Tax |Price Subtotal | Price Total + # ----------------------------------------------------------------------------------- + # 110 | 10% incl, 5% | | 100 | 115 + # 10 | | 10% incl | 10 | 10 + # 5 | | 5% | 5 | 5 + # + # When setting the balance to -200, the expected result is: + # + # Price Unit | Taxes | Originator Tax |Price Subtotal | Price Total + # ----------------------------------------------------------------------------------- + # 220 | 10% incl, 5% | | 200 | 230 + # 20 | | 10% incl | 20 | 20 + # 10 | | 5% | 10 | 10 + taxes_res = taxes._origin.compute_all(balance, currency=currency, handle_price_include=False) + for tax_res in taxes_res['taxes']: + tax = self.env['account.tax'].browse(tax_res['id']) + if tax.price_include: + balance += tax_res['amount'] + + discount_factor = 1 - (discount / 100.0) + if balance and discount_factor: + # discount != 100% + vals = { + 'quantity': quantity or 1.0, + 'price_unit': balance / discount_factor / (quantity or 1.0), + } + elif balance and not discount_factor: + # discount == 100% + vals = { + 'quantity': quantity or 1.0, + 'discount': 0.0, + 'price_unit': balance / (quantity or 1.0), + } + else: + vals = {} + return vals + + # ------------------------------------------------------------------------- + # ONCHANGE METHODS + # ------------------------------------------------------------------------- + + @api.onchange('amount_currency', 'currency_id', 'debit', 'credit', 'tax_ids', 'account_id', 'analytic_account_id', 'analytic_tag_ids') + def _onchange_mark_recompute_taxes(self): + ''' Recompute the dynamic onchange based on taxes. + If the edited line is a tax line, don't recompute anything as the user must be able to + set a custom value. + ''' + for line in self: + if not line.tax_repartition_line_id: + line.recompute_tax_line = True - @api.onchange('debit', 'credit', 'tax_ids', 'analytic_account_id', 'analytic_tag_ids') - def onchange_tax_ids_create_aml(self): + @api.onchange('product_id') + def _onchange_product_id(self): for line in self: - line.recompute_tax_line = True + if not line.product_id or line.display_type in ('line_section', 'line_note'): + continue + + line.name = line._get_computed_name() + line.account_id = line._get_computed_account() + line.tax_ids = line._get_computed_taxes() + line.product_uom_id = line._get_computed_uom() + line.price_unit = line._get_computed_price_unit() + + if len(self) == 1: + return {'domain': {'product_uom_id': [('category_id', '=', self.product_uom_id.category_id.id)]}} + + @api.onchange('product_uom_id') + def _onchange_uom_id(self): + ''' Recompute the 'price_unit' depending of the unit of measure. ''' + self.price_unit = self._get_computed_price_unit() + + @api.onchange('account_id') + def _onchange_account_id(self): + ''' Recompute 'tax_ids' based on 'account_id'. ''' + if not self.display_type in ('line_section', 'line_note'): + self.tax_ids = self._get_computed_taxes() + + @api.multi + def _onchange_balance(self): + for line in self: + if line.currency_id: + continue + if not line.move_id.is_invoice(include_receipts=True): + continue + line.update(line._get_fields_onchange_balance()) + line.update(line._get_price_total_and_subtotal()) @api.onchange('debit') def _onchange_debit(self): - self.ensure_one() - if self.debit != 0: - self.credit = 0 + if self.debit: + self.credit = 0.0 + self._onchange_balance() @api.onchange('credit') def _onchange_credit(self): - self.ensure_one() - if self.credit != 0: - self.debit = 0 + if self.credit: + self.debit = 0.0 + self._onchange_balance() - @api.model_cr - def init(self): - """ change index on partner_id to a multi-column index on (partner_id, ref), the new index will behave in the - same way when we search on partner_id, with the addition of being optimal when having a query that will - search on partner_id and ref at the same time (which is the case when we open the bank reconciliation widget) - """ - cr = self._cr - cr.execute('DROP INDEX IF EXISTS account_move_line_partner_id_index') - cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('account_move_line_partner_id_ref_idx',)) - if not cr.fetchone(): - cr.execute('CREATE INDEX account_move_line_partner_id_ref_idx ON account_move_line (partner_id, ref)') + @api.onchange('amount_currency') + def _onchange_amount_currency(self): + for line in self: + if not line.currency_id: + continue + if not line.move_id.is_invoice(include_receipts=True): + continue + line.update(line._get_fields_onchange_balance( + balance=line.amount_currency, + )) + line.update(line._get_price_total_and_subtotal()) + + @api.onchange('quantity', 'discount', 'price_unit', 'tax_ids') + def _onchange_price_subtotal(self): + for line in self: + if not line.move_id.is_invoice(include_receipts=True): + continue + + line.update(line._get_price_total_and_subtotal()) + line.update(line._get_fields_onchange_subtotal()) + + @api.onchange('currency_id') + def _onchange_currency(self): + for line in self: + if line.move_id.is_invoice(include_receipts=True): + line._onchange_price_subtotal() + + # ------------------------------------------------------------------------- + # COMPUTE METHODS + # ------------------------------------------------------------------------- + + @api.depends('currency_id') + def _compute_always_set_currency_id(self): + for line in self: + line.always_set_currency_id = line.currency_id or line.company_currency_id + + @api.depends('debit', 'credit') + def _compute_balance(self): + for line in self: + line.balance = line.debit - line.credit @api.depends('debit', 'credit', 'amount_currency', 'currency_id', 'matched_debit_ids', 'matched_credit_ids', 'matched_debit_ids.amount', 'matched_credit_ids.amount', 'move_id.state') def _amount_residual(self): @@ -596,163 +2677,9 @@ def _amount_residual(self): reconciled = True line.reconciled = reconciled - line.amount_residual = line.company_id.currency_id.round(amount * sign) + line.amount_residual = line.move_id.company_id.currency_id.round(amount * sign) line.amount_residual_currency = line.currency_id and line.currency_id.round(amount_residual_currency * sign) or 0.0 - @api.depends('debit', 'credit') - def _store_balance(self): - for line in self: - line.balance = line.debit - line.credit - - @api.model - def _get_currency(self): - currency = False - context = self._context or {} - if context.get('default_journal_id', False): - currency = self.env['account.journal'].browse(context['default_journal_id']).currency_id - return currency - - @api.depends('move_id') - def _compute_parent_state(self): - for record in self.filtered('move_id'): - record.parent_state = record.move_id.state - - name = fields.Char(string="Label") - move_name = fields.Char(string='Number', related='move_id.name', store=True, index=True) - quantity = fields.Float(digits=dp.get_precision('Product Unit of Measure'), - help="The optional quantity expressed by this line, eg: number of product sold. The quantity is not a legal requirement but is very useful for some reports.") - product_uom_id = fields.Many2one('uom.uom', string='Unit of Measure') - product_id = fields.Many2one('product.product', string='Product') - debit = fields.Monetary(default=0.0, currency_field='company_currency_id') - credit = fields.Monetary(default=0.0, currency_field='company_currency_id') - balance = fields.Monetary(compute='_store_balance', store=True, currency_field='company_currency_id', - help="Technical field holding the debit - credit in order to open meaningful graph views from reports") - amount_currency = fields.Monetary(default=0.0, help="The amount expressed in an optional other currency if it is a multi-currency entry.") - company_currency_id = fields.Many2one('res.currency', related='company_id.currency_id', string="Company Currency", readonly=True, - help='Utility field to express amount currency', store=True) - currency_id = fields.Many2one('res.currency', string='Currency', default=_get_currency, - help="The optional other currency if it is a multi-currency entry.") - amount_residual = fields.Monetary(compute='_amount_residual', string='Residual Amount', store=True, currency_field='company_currency_id', - help="The residual amount on a journal item expressed in the company currency.") - amount_residual_currency = fields.Monetary(compute='_amount_residual', string='Residual Amount in Currency', store=True, - help="The residual amount on a journal item expressed in its currency (possibly not the company currency).") - tax_base_amount = fields.Monetary(string="Base Amount", currency_field='company_currency_id') - account_id = fields.Many2one('account.account', string='Account', required=True, index=True, - ondelete="cascade", domain=[('deprecated', '=', False)], default=lambda self: self._context.get('account_id', False)) - move_id = fields.Many2one('account.move', string='Journal Entry', ondelete="cascade", - help="The move of this entry line.", index=True, required=True, auto_join=True) - narration = fields.Text(related='move_id.narration', string='Narration', readonly=False) - ref = fields.Char(related='move_id.ref', string='Reference', store=True, copy=False, index=True, readonly=False) - payment_id = fields.Many2one('account.payment', string="Originator Payment", help="Payment that created this entry", copy=False) - statement_line_id = fields.Many2one('account.bank.statement.line', index=True, string='Bank statement line reconciled with this entry', copy=False, readonly=True) - statement_id = fields.Many2one('account.bank.statement', related='statement_line_id.statement_id', string='Statement', store=True, - help="The bank statement used for bank reconciliation", index=True, copy=False) - reconciled = fields.Boolean(compute='_amount_residual', store=True) - reconcile_model_id = fields.Many2one('account.reconcile.model', string="Reconciliation Model", copy=False) - full_reconcile_id = fields.Many2one('account.full.reconcile', string="Matching Number", copy=False, index=True) - matched_debit_ids = fields.One2many('account.partial.reconcile', 'credit_move_id', String='Matched Debits', - help='Debit journal items that are matched with this journal item.') - matched_credit_ids = fields.One2many('account.partial.reconcile', 'debit_move_id', String='Matched Credits', - help='Credit journal items that are matched with this journal item.') - journal_id = fields.Many2one('account.journal', related='move_id.journal_id', string='Journal', readonly=False, - index=True, store=True, copy=False) # related is required - blocked = fields.Boolean(string='No Follow-up', default=False, - help="You can check this box to mark this journal item as a litigation with the associated partner") - date_maturity = fields.Date(string='Due date', index=True, required=True, copy=False, - help="This field is used for payable and receivable journal entries. You can put the limit date for the payment of this line.") - date = fields.Date(related='move_id.date', string='Date', index=True, store=True, copy=False, readonly=False) # related is required - analytic_line_ids = fields.One2many('account.analytic.line', 'move_id', string='Analytic lines', oldname="analytic_lines") - tax_ids = fields.Many2many('account.tax', string='Taxes', domain=['|', ('active', '=', False), ('active', '=', True)]) - tax_line_id = fields.Many2one('account.tax', string='Originator tax', ondelete='restrict', compute='_compute_tax_line_id', store=True) - tax_repartition_line_id = fields.Many2one(comodel_name='account.tax.repartition.line', string="Originator Tax Repartition Line", ondelete='restrict', help="Tax repartition line that caused the creation of this move line, if any") - analytic_account_id = fields.Many2one('account.analytic.account', string='Analytic Account', index=True) - analytic_tag_ids = fields.Many2many('account.analytic.tag', string='Analytic Tags') - company_id = fields.Many2one('res.company', related='account_id.company_id', string='Company', store=True, readonly=True) - - # TODO: put the invoice link and partner_id on the account_move - invoice_id = fields.Many2one('account.invoice', oldname="invoice") - partner_id = fields.Many2one('res.partner', string='Partner', ondelete='restrict') - tax_exigible = fields.Boolean(string='Appears in VAT report', default=True, - help="Technical field used to mark a tax line as exigible in the vat report or not (only exigible journal items are displayed). By default all new journal items are directly exigible, but with the feature cash_basis on taxes, some will become exigible only when the payment is recorded.") - parent_state = fields.Char(compute="_compute_parent_state", help="State of the parent account.move") - - recompute_tax_line = fields.Boolean(store=False, help="Technical field used to know if the tax_ids field has been modified in the UI.") - tax_line_grouping_key = fields.Char(store=False, string='Old Taxes', help="Technical field used to store the old values of fields used to compute tax lines (in account.move form view) between the moment the user changed it and the moment the ORM reflects that change in its one2many") - tag_ids = fields.Many2many(string="Tags", comodel_name='account.account.tag', ondelete='restrict', help="Tags assigned to this line by the tax creating it, if any. It determines its impact on financial reports.") - tax_audit = fields.Char(string="Tax Audit String", compute="_compute_tax_audit", store=True, help="Computed field, listing the tax grids impacted by this line, and the amount it applies to each of them.") - - _sql_constraints = [ - ('credit_debit1', 'CHECK (credit*debit=0)', 'Wrong credit or debit value in accounting entry! Credit or debit should be zero.'), - ('credit_debit2', 'CHECK (credit+debit>=0)', 'Wrong credit or debit value in accounting entry! Credit and debit should be positive.'), - ] - - @api.model - def default_get(self, fields): - rec = super(AccountMoveLine, self).default_get(fields) - if 'line_ids' not in self._context or "account_move_line_default_get" in self._context: - return rec - if {'debit', 'credit', 'partner_id', 'account_id'}.isdisjoint(fields): - return rec - # compute the default credit/debit of the next line in case of a manual entry - move = self.env['account.move'].with_context(account_move_line_default_get=True).new({'line_ids': self._context['line_ids']}) - balance = 0 - for line in move.line_ids: - balance += line.debit - line.credit - if balance < 0: - rec.update({'debit': -balance}) - if balance > 0: - rec.update({'credit': balance}) - - # Set the partner/account to the same value as the two last lines if they are equal - if len(move.line_ids) > 1: - last_lines = move.line_ids[-2:] - if last_lines[0].account_id == last_lines[1].account_id: - rec['account_id'] = last_lines[0].account_id.id - if last_lines[0].partner_id == last_lines[1].partner_id: - rec['partner_id'] = last_lines[0].partner_id.id - - return rec - - @api.multi - @api.constrains('tax_ids', 'tax_line_id') - def _check_tax_lock_date1(self): - for line in self: - if line.date <= (line.company_id.tax_lock_date or date.min): - raise ValidationError(_("The operation is refused as it would impact an already issued tax statement. " + - "Please change the journal entry date or the tax lock date set in the settings ({}) to proceed").format(line.company_id.tax_lock_date or date.min)) - - @api.multi - @api.constrains('credit', 'debit', 'date') - def _check_tax_lock_date2(self): - for line in self: - if (line.tax_ids or line.tax_line_id) and line.date <= (line.company_id.tax_lock_date or date.min): - raise ValidationError(_("The operation is refused as it would impact an already issued tax statement. " + - "Please change the journal entry date or the tax lock date set in the settings ({}) to proceed").format(line.company_id.tax_lock_date or date.min)) - - @api.multi - @api.constrains('currency_id', 'account_id') - def _check_currency(self): - for line in self: - account_currency = line.account_id.currency_id - if account_currency and account_currency != line.company_id.currency_id: - if not line.currency_id or line.currency_id != account_currency: - raise ValidationError(_('The selected account of your Journal Entry forces to provide a secondary currency. You should remove the secondary currency on the account.')) - - @api.multi - @api.constrains('currency_id', 'amount_currency') - def _check_currency_and_amount(self): - for line in self: - if (line.amount_currency and not line.currency_id): - raise ValidationError(_("You cannot create journal items with a secondary currency without filling both 'currency' and 'amount currency' fields.")) - - @api.multi - @api.constrains('amount_currency', 'debit', 'credit') - def _check_currency_amount(self): - for line in self: - if line.amount_currency: - if (line.amount_currency > 0.0 and line.credit > 0.0) or (line.amount_currency < 0.0 and line.debit > 0.0): - raise ValidationError(_('The amount expressed in the secondary currency must be positive when account is debited and negative when account is credited.')) - @api.depends('tax_repartition_line_id.invoice_tax_id', 'tax_repartition_line_id.refund_tax_id') def _compute_tax_line_id(self): """ tax_line_id is computed as the tax linked to the repartition line creating @@ -769,7 +2696,7 @@ def _compute_is_unaffected_earnings_line(self): unaffected_earnings_type = self.env.ref("account.data_unaffected_earnings") record.is_unaffected_earnings_line = unaffected_earnings_type == record.account_id.user_type_id - @api.depends('tag_ids', 'debit', 'credit', 'journal_id', 'invoice_id') + @api.depends('tag_ids', 'debit', 'credit', 'journal_id') def _compute_tax_audit(self): separator = ' ' @@ -777,7 +2704,7 @@ def _compute_tax_audit(self): currency = record.company_id.currency_id audit_str = '' for tag in record.tag_ids: - tag_amount = (tag.tax_negate and -1 or 1) * (record.journal_id.type == 'sale' and -1 or 1) * (record.invoice_id.type in ('in_refund', 'out_refund') and -1 or 1) * record.balance + tag_amount = (tag.tax_negate and -1 or 1) * (record.move_id.is_outbound() and -1 or 1) * record.balance if tag.tax_report_line_ids: #Then, the tag comes from a report line, and hence has a + or - sign (also in its name) @@ -791,24 +2718,279 @@ def _compute_tax_audit(self): record.tax_audit = audit_str - @api.onchange('amount_currency', 'currency_id', 'account_id') - def _onchange_amount_currency(self): - '''Recompute the debit/credit based on amount_currency/currency_id and date. - However, date is a related field on account.move. Then, this onchange will not be triggered - by the form view by changing the date on the account.move. - To fix this problem, see _onchange_date method on account.move. - ''' + # ------------------------------------------------------------------------- + # CONSTRAINT METHODS + # ------------------------------------------------------------------------- + + @api.constrains('account_id') + def _check_constrains_account_id(self): + for line in self: + account = line.account_id + journal = line.journal_id + + if account.deprecated: + raise UserError(_('The account %s (%s) is deprecated.') % (account.name, account.code)) + + control_type_failed = journal.type_control_ids and account.user_type_id not in journal.type_control_ids + control_account_failed = journal.account_control_ids and account not in journal.account_control_ids + if control_type_failed or control_account_failed: + raise UserError(_('You cannot use this general account in this journal, check the tab \'Entry Controls\' on the related journal.')) + + @api.constrains('tax_ids', 'tax_line_id') + def _check_tax_lock_date1(self): + for line in self: + if line.date <= (line.company_id.tax_lock_date or date.min): + raise ValidationError( + _("The operation is refused as it would impact an already issued tax statement. " + + "Please change the journal entry date or the tax lock date set in the settings ({}) to proceed").format( + line.company_id.tax_lock_date or date.min)) + + @api.constrains('credit', 'debit', 'date') + def _check_tax_lock_date2(self): + for line in self: + if (line.tax_ids or line.tax_line_id) and line.date <= (line.company_id.tax_lock_date or date.min): + raise ValidationError( + _("The operation is refused as it would impact an already issued tax statement. " + + "Please change the journal entry date or the tax lock date set in the settings ({}) to proceed").format( + line.company_id.tax_lock_date or date.min)) + + @api.multi + def _update_check(self): + """ Raise Warning to cause rollback if the move is posted, some entries are reconciled or the move is older than the lock date""" + move_ids = set() + for line in self: + err_msg = _('Move name (id): %s (%s)') % (line.move_id.name, str(line.move_id.id)) + if line.move_id.state != 'draft': + raise UserError(_('You cannot do this modification on a posted journal entry, you can just change some non legal fields. You must revert the journal entry to cancel it.\n%s.') % err_msg) + if line.reconciled and not (line.debit == 0 and line.credit == 0): + raise UserError(_('You cannot do this modification on a reconciled entry. You can just change some non legal fields or you must unreconcile first.\n%s.') % err_msg) + if line.move_id.id not in move_ids: + move_ids.add(line.move_id.id) + self.env['account.move'].browse(list(move_ids))._check_fiscalyear_lock_date() + return True + + # ------------------------------------------------------------------------- + # LOW-LEVEL METHODS + # ------------------------------------------------------------------------- + + @api.model_cr + def init(self): + """ change index on partner_id to a multi-column index on (partner_id, ref), the new index will behave in the + same way when we search on partner_id, with the addition of being optimal when having a query that will + search on partner_id and ref at the same time (which is the case when we open the bank reconciliation widget) + """ + cr = self._cr + cr.execute('DROP INDEX IF EXISTS account_move_line_partner_id_index') + cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('account_move_line_partner_id_ref_idx',)) + if not cr.fetchone(): + cr.execute('CREATE INDEX account_move_line_partner_id_ref_idx ON account_move_line (partner_id, ref)') + + @api.model_create_multi + def create(self, vals_list): + # OVERRIDE + ACCOUNTING_FIELDS = ('debit', 'credit', 'amount_currency') + BUSINESS_FIELDS = ('price_unit', 'quantity', 'discount', 'tax_ids') + + for vals in vals_list: + move = self.env['account.move'].browse(vals['move_id']) + + if move.is_invoice(include_receipts=True): + currency = self.env['res.currency'].browse(vals.get('currency_id')) + partner = self.env['res.partner'].browse(vals.get('partner_id')) + taxes = self.resolve_2many_commands('tax_ids', vals.get('tax_ids', []), fields=['id']) + tax_ids = set(tax['id'] for tax in taxes) + taxes = self.env['account.tax'].browse(tax_ids) + + # Ensure consistency between accounting & business fields. + # As we can't express such synchronization as computed fields without cycling, we need to do it both + # in onchange and in create/write. So, if something changed in accounting [resp. business] fields, + # business [resp. accounting] fields are recomputed. + if any(vals.get(field) for field in ACCOUNTING_FIELDS): + if vals.get('currency_id'): + balance = vals.get('amount_currency', 0.0) + else: + balance = vals.get('debit', 0.0) - vals.get('credit', 0.0) + vals.update(self._get_fields_onchange_balance_model( + vals.get('quantity', 0.0), + vals.get('discount', 0.0), + balance, + move.type, + currency, + taxes + )) + vals.update(self._get_price_total_and_subtotal_model( + vals.get('price_unit', 0.0), + vals.get('quantity', 0.0), + vals.get('discount', 0.0), + currency, + self.env['product.product'].browse(vals.get('product_id')), + partner, + taxes, + move.type, + )) + elif any(vals.get(field) for field in BUSINESS_FIELDS): + vals.update(self._get_price_total_and_subtotal_model( + vals.get('price_unit', 0.0), + vals.get('quantity', 0.0), + vals.get('discount', 0.0), + currency, + self.env['product.product'].browse(vals.get('product_id')), + partner, + taxes, + move.type, + )) + vals.update(self._get_fields_onchange_subtotal_model( + vals['price_subtotal'], + move.type, + currency, + move.company_id, + move.date, + )) + + # Ensure consistency between taxes & tax exigibility fields. + if 'tax_exigible' in vals: + continue + if vals.get('tax_repartition_line_id'): + repartition_line = self.env['account.tax.repartition.line'].browse(vals['tax_repartition_line_id']) + tax = repartition_line.invoice_tax_id or repartition_line.refund_tax_id + vals['tax_exigible'] = tax.tax_exigibility == 'on_invoice' + elif vals.get('tax_ids'): + taxes = self.resolve_2many_commands('tax_ids', vals['tax_ids']) + vals['tax_exigible'] = not any([tax['tax_exigibility'] == 'on_payment' for tax in taxes]) + + lines = super(AccountMoveLine, self).create(vals_list) + + # Check the move is balanced debit = credit. + if self._context.get('check_move_validity', True): + lines.mapped('move_id')._check_move_consistency() + + return lines + + @api.multi + def write(self, vals): + # OVERRIDE + ACCOUNTING_FIELDS = ('debit', 'credit', 'amount_currency') + BUSINESS_FIELDS = ('price_unit', 'quantity', 'discount', 'tax_ids') + + if ('account_id' in vals) and self.env['account.account'].browse(vals['account_id']).deprecated: + raise UserError(_('You cannot use a deprecated account.')) + if any(key in vals for key in ('account_id', 'journal_id', 'date', 'move_id', 'debit', 'credit')): + self._update_check() + if not self._context.get('allow_amount_currency') and any( + key in vals for key in ('amount_currency', 'currency_id')): + # hackish workaround to write the amount_currency when assigning a payment to an invoice through the 'add' button + # this is needed to compute the correct amount_residual_currency and potentially create an exchange difference entry + self._update_check() + # when making a reconciliation on an existing liquidity journal item, mark the payment as reconciled + for record in self: + if 'statement_line_id' in vals and record.payment_id: + # In case of an internal transfer, there are 2 liquidity move lines to match with a bank statement + if all(line.statement_id for line in record.payment_id.move_line_ids.filtered( + lambda r: r.id != record.id and r.account_id.internal_type == 'liquidity')): + record.payment_id.state = 'reconciled' + + result = super(AccountMoveLine, self).write(vals) + + for line in self: + if not line.move_id.is_invoice(include_receipts=True): + continue + + # Ensure consistency between accounting & business fields. + # As we can't express such synchronization as computed fields without cycling, we need to do it both + # in onchange and in create/write. So, if something changed in accounting [resp. business] fields, + # business [resp. accounting] fields are recomputed. + if any(field in vals for field in ACCOUNTING_FIELDS): + price_subtotal = line.currency_id and line.amount_currency or line.debit - line.credit + to_write = line._get_fields_onchange_balance( + balance=price_subtotal, + ) + to_write.update(line._get_price_total_and_subtotal( + price_unit=to_write.get('price_unit', line.price_unit), + quantity=to_write.get('quantity', line.quantity), + discount=to_write.get('discount', line.discount), + )) + super(AccountMoveLine, line).write(to_write) + elif any(field in vals for field in BUSINESS_FIELDS): + to_write = self._get_price_total_and_subtotal() + to_write.update(line._get_fields_onchange_subtotal( + price_subtotal=to_write['price_subtotal'], + )) + super(AccountMoveLine, line).write(to_write) + + if self._context.get('check_move_validity', True) and any(key in vals for key in ('account_id', 'journal_id', 'date', 'move_id', 'debit', 'credit')): + self.env['account.move'].browse(self.mapped('move_id.id'))._check_move_consistency() + + return result + + @api.multi + def unlink(self): + self._update_check() + self._check_tax_lock_date2() + move_ids = set() + for line in self: + if line.move_id.id not in move_ids: + move_ids.add(line.move_id.id) + result = super(AccountMoveLine, self).unlink() + if self._context.get('check_move_validity', True) and move_ids: + self.env['account.move'].browse(list(move_ids))._check_move_consistency() + return result + + @api.model + def default_get(self, default_fields): + # OVERRIDE + values = super(AccountMoveLine, self).default_get(default_fields) + + if 'account_id' in default_fields \ + and (self._context.get('journal_id') or self._context.get('default_journal_id')) \ + and not values.get('account_id') \ + and self._context.get('default_type') in self.move_id.get_inbound_types(): + # Fill missing 'account_id'. + journal = self.env['account.journal'].browse(self._context.get('default_journal_id') or self._context['journal_id']) + values['account_id'] = journal.default_credit_account_id.id + elif 'account_id' in default_fields \ + and (self._context.get('journal_id') or self._context.get('default_journal_id')) \ + and not values.get('account_id') \ + and self._context.get('default_type') in self.move_id.get_outbound_types(): + # Fill missing 'account_id'. + journal = self.env['account.journal'].browse(self._context.get('default_journal_id') or self._context['journal_id']) + values['account_id'] = journal.default_debit_account_id.id + elif self._context.get('line_ids') and any(field_name in default_fields for field_name in ('debit', 'credit', 'account_id', 'partner_id')): + move = self.env['account.move'].new({'line_ids': self._context['line_ids']}) + + # Suggest default value for debit / credit to balance the journal entry. + balance = sum(line['debit'] - line['credit'] for line in move.line_ids) + if balance < 0.0: + values.update({'debit': -balance}) + if balance > 0.0: + values.update({'credit': balance}) + + # Suggest default value for 'partner_id'. + if 'partner_id' in default_fields and not values.get('partner_id'): + partners = move.line_ids[-2:].mapped('partner_id') + if len(partners) == 1: + values['partner_id'] = partners.id + + # Suggest default value for 'account_id'. + if 'account_id' in default_fields and not values.get('account_id'): + accounts = move.line_ids[-2:].mapped('account_id') + if len(accounts) == 1: + values['account_id'] = accounts.id + return values + + @api.multi + @api.depends('ref', 'move_id') + def name_get(self): + result = [] for line in self: - company_currency_id = line.account_id.company_id.currency_id - amount = line.amount_currency - if line.currency_id and company_currency_id and line.currency_id != company_currency_id: - amount = line.currency_id._convert(amount, company_currency_id, line.company_id, line.date or fields.Date.today()) - line.debit = amount > 0 and amount or 0.0 - line.credit = amount < 0 and -amount or 0.0 + if line.ref: + result.append((line.id, (line.move_id.name or '') + '(' + line.ref + ')')) + else: + result.append((line.id, line.move_id.name)) + return result - #################################################### - # Reconciliation methods - #################################################### + # ------------------------------------------------------------------------- + # RECONCILIATION + # ------------------------------------------------------------------------- @api.multi def check_full_reconcile(self): @@ -889,7 +3071,7 @@ def check_full_reconcile(self): exchange_move_id = False # Eventually create a journal entry to book the difference due to foreign currency's exchange rate that fluctuates if to_balance and any([not float_is_zero(residual, precision_rounding=digits_rounding_precision) for aml, residual in to_balance.values()]): - exchange_move = self.env['account.move'].create( + exchange_move = self.env['account.move'].with_context(default_type='entry').create( self.env['account.full.reconcile']._prepare_exchange_diff_move(move_date=maxdate, company=amls[0].company_id)) part_reconcile = self.env['account.partial.reconcile'] for aml_to_balance, total in to_balance.values(): @@ -980,13 +3162,12 @@ def _reconcile_lines(self, debit_moves, credit_moves, field): for after_rec_dict in cash_basis_subjected: new_rec = part_rec.create(after_rec_dict) # if the pair belongs to move being reverted, do not create CABA entry - if cash_basis and not (new_rec.debit_move_id + new_rec.credit_move_id).mapped('move_id').mapped('reverse_entry_id'): + if cash_basis and not (new_rec.debit_move_id + new_rec.credit_move_id).mapped('move_id.reversed_entry_id'): new_rec.create_tax_cash_basis_entry(cash_basis_percentage_before_rec) self.recompute() return debit_moves+credit_moves - @api.multi def auto_reconcile_lines(self): # Create list of debit and list of credit move ordered by date-currency @@ -1027,7 +3208,7 @@ def reconcile(self, writeoff_acc_id=False, writeoff_journal_id=False): # Empty self can happen if the user tries to reconcile entries which are already reconciled. # The calling method might have filtered out reconciled lines. if not self: - return True + return self._check_reconcile_validity() #reconcile everything that can be @@ -1053,7 +3234,6 @@ def reconcile(self, writeoff_acc_id=False, writeoff_journal_id=False): def _create_writeoff(self, writeoff_vals): """ Create a writeoff move per journal for the account.move.lines in self. If debit/credit is not specified in vals, the writeoff amount will be computed as the sum of amount_residual of the given recordset. - :param writeoff_vals: list of dicts containing values suitable for account_move_line.create(). The data in vals will be processed to create bot writeoff acount.move.line and their enclosing account.move. """ @@ -1133,208 +3313,38 @@ def compute_writeoff_counterpart_vals(values): 'line_ids': [(0, 0, line) for line in writeoff_lines], }) writeoff_moves += writeoff_move - # writeoff_move.post() - line_to_reconcile += writeoff_move.line_ids.filtered(lambda r: r.account_id == self[0].account_id).sorted(key='id')[-1:] + + #post all the writeoff moves at once if writeoff_moves: writeoff_moves.post() + # Return the writeoff move.line which is to be reconciled return line_to_reconcile @api.multi def remove_move_reconcile(self): """ Undo a reconciliation """ - if not self: - return True - rec_move_ids = self.env['account.partial.reconcile'] - for account_move_line in self: - account_move_line.reconcile_model_id = False - for invoice in account_move_line.payment_id.invoice_ids: - if invoice.id == self.env.context.get('invoice_id') and account_move_line in invoice.payment_move_line_ids: - account_move_line.payment_id.write({'invoice_ids': [(3, invoice.id, None)]}) - rec_move_ids += account_move_line.matched_debit_ids - rec_move_ids += account_move_line.matched_credit_ids - if self.env.context.get('invoice_id'): - current_invoice = self.env['account.invoice'].browse(self.env.context['invoice_id']) - aml_to_keep = current_invoice.move_id.line_ids | current_invoice.move_id.line_ids.mapped('full_reconcile_id.exchange_move_id.line_ids') - rec_move_ids = rec_move_ids.filtered( - lambda r: (r.debit_move_id + r.credit_move_id) & aml_to_keep - ) - return rec_move_ids.unlink() - - def _apply_taxes(self, vals, amount): - tax_lines_vals = [] - # Get ids from triplets : https://www.odoo.com/documentation/10.0/reference/orm.html#odoo.models.Model.write - tax_ids = [tax['id'] for tax in self.resolve_2many_commands('tax_ids', vals['tax_ids']) if tax.get('id')] - # Since create() receives ids instead of recordset, let's just use the old-api bridge - taxes = self.env['account.tax'].browse(tax_ids) - currency = self.env['res.currency'].browse(vals.get('currency_id')) - partner = self.env['res.partner'].browse(vals.get('partner_id')) - ctx = dict(self._context) - ctx['round'] = ctx.get('round', True) - res = taxes.with_context(ctx).compute_all(amount, - currency, 1, vals.get('product_id'), partner) - # Adjust line amount if any tax is price_include - if abs(res['total_excluded']) < abs(amount): - if vals['debit'] != 0.0: vals['debit'] = res['total_excluded'] - if vals['credit'] != 0.0: vals['credit'] = -res['total_excluded'] - if vals.get('amount_currency'): - vals['amount_currency'] = self.env['res.currency'].browse(vals['currency_id']).round(vals['amount_currency'] * (res['total_excluded']/amount)) - # Create tax lines - for tax_vals in res['taxes']: - if tax_vals['amount']: - tax = self.env['account.tax'].browse([tax_vals['id']]) - account_id = (amount > 0 and tax_vals['account_id'] or tax_vals['refund_account_id']) - if not account_id: account_id = vals['account_id'] - temp = { - 'account_id': account_id, - 'name': vals['name'] + ' ' + tax_vals['name'], - 'tax_line_id': tax_vals['id'], - 'move_id': vals['move_id'], - 'partner_id': vals.get('partner_id'), - 'statement_id': vals.get('statement_id'), - 'debit': tax_vals['amount'] > 0 and tax_vals['amount'] or 0.0, - 'credit': tax_vals['amount'] < 0 and -tax_vals['amount'] or 0.0, - 'analytic_account_id': vals.get('analytic_account_id') if tax.analytic else False, - } - bank = self.env["account.bank.statement.line"].browse(vals.get('statement_line_id')).statement_id - if bank.currency_id != bank.company_id.currency_id: - ctx = {} - if 'date' in vals: - ctx['date'] = vals['date'] - elif 'date_maturity' in vals: - ctx['date'] = vals['date_maturity'] - temp['currency_id'] = bank.currency_id.id - temp['amount_currency'] = bank.company_id.currency_id.with_context(ctx).compute(tax_vals['amount'], bank.currency_id, round=True) - if vals.get('tax_exigible'): - temp['tax_exigible'] = True - temp['account_id'] = tax.cash_basis_account.id or account_id - tax_lines_vals.append(temp) - return tax_lines_vals - - #################################################### - # CRUD methods - #################################################### - - @api.model_create_multi - def create(self, vals_list): - """ :context's key `check_move_validity`: check data consistency after move line creation. Eg. set to false to disable verification that the move - debit-credit == 0 while creating the move lines composing the move. - """ - for vals in vals_list: - amount = vals.get('debit', 0.0) - vals.get('credit', 0.0) - move = self.env['account.move'].browse(vals['move_id']) - account = self.env['account.account'].browse(vals['account_id']) - if account.deprecated: - raise UserError(_('The account %s (%s) is deprecated.') %(account.name, account.code)) - journal = vals.get('journal_id') and self.env['account.journal'].browse(vals['journal_id']) or move.journal_id - vals['date_maturity'] = vals.get('date_maturity') or vals.get('date') or move.date - - ok = ( - (not journal.type_control_ids and not journal.account_control_ids) - or account.user_type_id in journal.type_control_ids - or account in journal.account_control_ids - ) - if not ok: - raise UserError(_('You cannot use this general account in this journal, check the tab \'Entry Controls\' on the related journal.')) - - # Automatically convert in the account's secondary currency if there is one and - # the provided values were not already multi-currency - if account.currency_id and 'amount_currency' not in vals and account.currency_id.id != account.company_id.currency_id.id: - vals['currency_id'] = account.currency_id.id - date = vals.get('date') or vals.get('date_maturity') or fields.Date.today() - vals['amount_currency'] = account.company_id.currency_id._convert(amount, account.currency_id, account.company_id, date) - - #Toggle the 'tax_exigible' field to False in case it is not yet given and the tax in 'tax_line_id' or one of - #the 'tax_ids' is a cash based tax. - taxes = False - if vals.get('tax_repartition_line_id'): - taxes = [{'tax_exigibility': self.env['account.tax.repartition.line'].browse(vals['tax_repartition_line_id']).tax_id.tax_exigibility}] - if vals.get('tax_ids'): - taxes = self.env['account.move.line'].resolve_2many_commands('tax_ids', vals['tax_ids']) - if taxes and any([tax['tax_exigibility'] == 'on_payment' for tax in taxes]) and not vals.get('tax_exigible'): - vals['tax_exigible'] = False - - lines = super(AccountMoveLine, self).create(vals_list) - - if self._context.get('check_move_validity', True): - lines.mapped('move_id')._post_validate() - - return lines - - @api.multi - def unlink(self): - self._update_check() - self._check_tax_lock_date2() - move_ids = set() - for line in self: - if line.move_id.id not in move_ids: - move_ids.add(line.move_id.id) - result = super(AccountMoveLine, self).unlink() - if self._context.get('check_move_validity', True) and move_ids: - self.env['account.move'].browse(list(move_ids))._post_validate() - return result + (self.mapped('matched_debit_ids') + self.mapped('matched_credit_ids')).unlink() @api.multi - def write(self, vals): - if ('account_id' in vals) and self.env['account.account'].browse(vals['account_id']).deprecated: - raise UserError(_('You cannot use a deprecated account.')) - if any(key in vals for key in ('account_id', 'journal_id', 'date', 'move_id', 'debit', 'credit')): - self._update_check() - if not self._context.get('allow_amount_currency') and any(key in vals for key in ('amount_currency', 'currency_id')): - #hackish workaround to write the amount_currency when assigning a payment to an invoice through the 'add' button - #this is needed to compute the correct amount_residual_currency and potentially create an exchange difference entry - self._update_check() - #when we set the expected payment date, log a note on the invoice_id related (if any) - if vals.get('expected_pay_date') and self.invoice_id: - str_expected_pay_date = vals['expected_pay_date'] - if isinstance(str_expected_pay_date, date): - str_expected_pay_date = fields.Date.to_string(str_expected_pay_date) - msg = _('New expected payment date: ') + str_expected_pay_date + '.\n' + vals.get('internal_note', '') - self.invoice_id.message_post(body=msg) #TODO: check it is an internal note (not a regular email)! - #when making a reconciliation on an existing liquidity journal item, mark the payment as reconciled - for record in self: - if 'statement_line_id' in vals and record.payment_id: - # In case of an internal transfer, there are 2 liquidity move lines to match with a bank statement - if all(line.statement_id for line in record.payment_id.move_line_ids.filtered(lambda r: r.id != record.id and r.account_id.internal_type=='liquidity')): - record.payment_id.state = 'reconciled' - - result = super(AccountMoveLine, self).write(vals) - if self._context.get('check_move_validity', True) and any(key in vals for key in ('account_id', 'journal_id', 'date', 'move_id', 'debit', 'credit')): - move_ids = set() - for line in self: - if line.move_id.id not in move_ids: - move_ids.add(line.move_id.id) - self.env['account.move'].browse(list(move_ids))._post_validate() - return result + def _copy_data_extend_business_fields(self, values): + ''' Hook allowing copying business fields under certain conditions. + E.g. The link to the sale order lines must be preserved in case of a refund. + ''' + self.ensure_one() @api.multi - def _update_check(self): - """ Raise Warning to cause rollback if the move is posted, some entries are reconciled or the move is older than the lock date""" - move_ids = set() - for line in self: - err_msg = _('Move name (id): %s (%s)') % (line.move_id.name, str(line.move_id.id)) - if line.reconciled and not (line.debit == 0 and line.credit == 0): - raise UserError(_('You cannot do this modification on a reconciled entry. You can just change some non legal fields or you must unreconcile first.\n%s.') % err_msg) - if line.move_id.id not in move_ids: - move_ids.add(line.move_id.id) - self.env['account.move'].browse(list(move_ids))._check_lock_date() - return True - - #################################################### - # Misc / utility methods - #################################################### + def copy_data(self, default=None): + res = super(AccountMoveLine, self).copy_data(default=default) + if self._context.get('include_business_fields'): + for line, values in zip(self, res): + line._copy_data_extend_business_fields(values) + return res - @api.multi - @api.depends('ref', 'move_id') - def name_get(self): - result = [] - for line in self: - if line.ref: - result.append((line.id, (line.move_id.name or '') + '(' + line.ref + ')')) - else: - result.append((line.id, line.move_id.name)) - return result + # ------------------------------------------------------------------------- + # MISC + # ------------------------------------------------------------------------- def _get_matched_percentage(self): """ This function returns a dictionary giving for each move_id of self, the percentage to consider as cash basis factor. @@ -1365,7 +3375,7 @@ def _get_matched_percentage(self): break if not all_same_currency: #we cannot rely on amount_currency fields as it is not present on all partial reconciliation - matched_percentage_per_move[line.move_id.id] = line.move_id.matched_percentage + matched_percentage_per_move[line.move_id.id] = line.move_id._get_cash_basis_matched_percentage() else: #we can rely on amount_currency fields, which allow us to post a tax cash basis move at the initial rate #to avoid currency rate difference issues. @@ -1375,22 +3385,6 @@ def _get_matched_percentage(self): matched_percentage_per_move[line.move_id.id] = total_reconciled_currency / total_amount_currency return matched_percentage_per_move - @api.model - def _compute_amount_fields(self, amount, src_currency, company_currency): - """ Helper function to compute value for fields debit/credit/amount_currency based on an amount and the currencies given in parameter""" - amount_currency = False - currency_id = False - date = self.env.context.get('date') or fields.Date.today() - company = self.env.context.get('company_id') - company = self.env['res.company'].browse(company) if company else self.env.company - if src_currency and src_currency != company_currency: - amount_currency = amount - amount = src_currency._convert(amount, company_currency, company, date) - currency_id = src_currency.id - debit = amount > 0 and amount or 0.0 - credit = amount < 0 and -amount or 0.0 - return debit, credit, amount_currency, currency_id - def _get_analytic_tag_ids(self): self.ensure_one() return self.analytic_tag_ids.filtered(lambda r: not r.active_analytic_distribution).ids @@ -1436,7 +3430,7 @@ def _prepare_analytic_line(self): 'general_account_id': move_line.account_id.id, 'ref': move_line.ref, 'move_id': move_line.id, - 'user_id': move_line.invoice_id.user_id.id or self._uid, + 'user_id': move_line.move_id.invoice_user_id.id or self._uid, 'partner_id': move_line.partner_id.id, 'company_id': move_line.analytic_account_id.company_id.id or self.env.company.id, }) @@ -1462,7 +3456,7 @@ def _prepare_analytic_distribution_line(self, distribution): 'general_account_id': self.account_id.id, 'ref': self.ref, 'move_id': self.id, - 'user_id': self.invoice_id.user_id.id or self._uid, + 'user_id': self.move_id.invoice_user_id.id or self._uid, 'company_id': distribution.account_id.company_id.id or self.env.company.id, } @@ -1526,6 +3520,9 @@ def _query_get(self, domain=None): where_clause_params = [] tables = '' if domain: + domain.append(('display_type', 'not in', ('line_section', 'line_note'))) + domain.append(('move_id.state', '!=', 'cancel')) + query = self._where_calc(domain) # Wrap the query with 'company_id IN (...)' to avoid bypassing company access rights. @@ -1534,6 +3531,19 @@ def _query_get(self, domain=None): tables, where_clause, where_clause_params = query.get_sql() return tables, where_clause, where_clause_params + # FIXME: Clarify me and change me in master + @api.multi + def action_duplicate(self): + self.ensure_one() + action = self.env.ref('account.action_move_journal_line').read()[0] + action['target'] = 'inline' + action['context'] = dict(self.env.context) + action['context']['form_view_initial_mode'] = 'edit' + action['context']['view_no_maturity'] = False + action['views'] = [(self.env.ref('account.view_move_form').id, 'form')] + action['res_id'] = self.copy().id + return action + @api.multi def open_reconcile_view(self): [action] = self.env.ref('account.action_account_moves_all').read() @@ -1546,7 +3556,7 @@ def open_reconcile_view(self): return action @api.model - def _get_domain_for_edition_mode(self): + def _get_suspense_moves_domain(self): return [ ('move_id.to_check', '=', True), ('full_reconcile_id', '=', False), @@ -1598,7 +3608,6 @@ def create_exchange_rate_entry(self, aml_to_fix, move): new journal item will be made into the given `move` in the company `currency_exchange_journal_id`, and one of its journal items is matched with the other lines to balance the full reconciliation. - :param aml_to_fix: recordset of account.move.line (possible several but sharing the same currency) :param move: account.move @@ -1648,7 +3657,6 @@ def create_exchange_rate_entry(self, aml_to_fix, move): def _get_tax_cash_basis_base_account(self, line, tax): ''' Get the account of lines that will contain the base amount of taxes. - :param line: An account.move.line record :param tax: An account.tax record :return: An account record @@ -1706,7 +3714,6 @@ def create_tax_cash_basis_entry(self, percentage_before_rec): 'account_id': line.tax_repartition_line_id.account_id.id, 'analytic_account_id': line.analytic_account_id.id, 'analytic_tag_ids': line.analytic_tag_ids.ids, - 'tax_line_id': line.tax_line_id.id, 'tax_exigible': True, 'amount_currency': line.amount_currency and line.currency_id.round(line.amount_currency * amount / line.balance) or 0.0, 'currency_id': line.currency_id.id, @@ -1787,9 +3794,9 @@ def unlink(self): #reverse the tax basis move created at the reconciliation time for move in self.env['account.move'].search([('tax_cash_basis_rec_id', 'in', self._ids)]): if move.date > (move.company_id.period_lock_date or date.min): - move.reverse_moves(date=move.date) + move._reverse_moves([{'ref': _('Reversal of %s') % move.name}], cancel=True) else: - move.reverse_moves() + move._reverse_moves([{'date': fields.Date.today(), 'ref': _('Reversal of %s') % move.name}], cancel=True) res = super(AccountPartialReconcile, self).unlink() if full_to_unlink: full_to_unlink.unlink() @@ -1819,7 +3826,10 @@ def unlink(self): # (reversing will cause a nested attempt to drop the full reconciliation) to_reverse = rec.exchange_move_id rec.exchange_move_id = False - to_reverse.reverse_moves() + to_reverse._reverse_moves([{ + 'date': fields.Date.today(), + 'ref': _('Reversal of: %s') % to_reverse.name, + }], cancel=True) return super(AccountFullReconcile, self).unlink() @api.model diff --git a/addons/account/models/account_payment.py b/addons/account/models/account_payment.py index b8858ef04cb18..18528685864eb 100644 --- a/addons/account/models/account_payment.py +++ b/addons/account/models/account_payment.py @@ -2,22 +2,14 @@ from odoo import models, fields, api, _ from odoo.exceptions import UserError, ValidationError -from odoo.tools import float_compare - -from itertools import groupby MAP_INVOICE_TYPE_PARTNER_TYPE = { 'out_invoice': 'customer', 'out_refund': 'customer', + 'out_receipt': 'customer', 'in_invoice': 'supplier', 'in_refund': 'supplier', -} -# Since invoice amounts are unsigned, this is how we know if money comes in or goes out -MAP_INVOICE_TYPE_PAYMENT_SIGN = { - 'out_invoice': 1, - 'in_refund': -1, - 'in_invoice': -1, - 'out_refund': 1, + 'in_receipt': 'supplier', } @@ -47,11 +39,11 @@ class account_payment(models.Model): # For money transfer, money goes from journal_id to a transfer account, then from the transfer account to destination_journal_id destination_journal_id = fields.Many2one('account.journal', string='Transfer To', domain=[('type', 'in', ('bank', 'cash'))], readonly=True, states={'draft': [('readonly', False)]}) - invoice_ids = fields.Many2many('account.invoice', 'account_invoice_payment_rel', 'payment_id', 'invoice_id', string="Invoices", copy=False, readonly=True, + invoice_ids = fields.Many2many('account.move', 'account_invoice_payment_rel', 'payment_id', 'invoice_id', string="Invoices", copy=False, readonly=True, help="""Technical field containing the invoice for which the payment has been generated. This does not especially correspond to the invoices reconciled with the payment, as it can have been generated first, and reconciled later""") - reconciled_invoice_ids = fields.Many2many('account.invoice', string='Reconciled Invoices', compute='_compute_reconciled_invoice_ids', help="Invoices whose journal items have been reconciled with this payment's.") + reconciled_invoice_ids = fields.Many2many('account.move', string='Reconciled Invoices', compute='_compute_reconciled_invoice_ids', help="Invoices whose journal items have been reconciled with these payments.") has_invoices = fields.Boolean(compute="_compute_reconciled_invoice_ids", help="Technical field used for usability purposes") move_line_ids = fields.One2many('account.move.line', 'payment_id', readonly=True, copy=False, ondelete='restrict') @@ -94,19 +86,19 @@ class account_payment(models.Model): require_partner_bank_account = fields.Boolean(compute='_compute_show_partner_bank', help='Technical field used to know whether the field `partner_bank_account_id` needs to be required or not in the payments form views') @api.model - def default_get(self, fields): - rec = super(account_payment, self).default_get(fields) + def default_get(self, default_fields): + rec = super(account_payment, self).default_get(default_fields) active_ids = self._context.get('active_ids') or self._context.get('active_id') active_model = self._context.get('active_model') # Check for selected invoices ids - if not active_ids or active_model != 'account.invoice': + if not active_ids or active_model != 'account.move': return rec - invoices = self.env['account.invoice'].browse(active_ids) + invoices = self.env['account.move'].browse(active_ids).filtered(lambda move: move.is_invoice(include_receipts=True)) # Check all invoices are open - if any(invoice.state != 'open' for invoice in invoices): + if not invoices or any(invoice.state != 'posted' for invoice in invoices): raise UserError(_("You can only register payments for open invoices")) # Check if, in batch payments, there are not negative invoices and positive invoices dtype = invoices[0].type @@ -119,23 +111,23 @@ def default_get(self, fields): (dtype == 'out_invoice' and inv.type == 'out_refund')): raise UserError(_("You cannot register payments for customer invoices and credit notes at the same time.")) - amount = self._compute_payment_amount(invoices, invoices[0].currency_id) + amount = self._compute_payment_amount(invoices, invoices[0].currency_id, invoices[0].journal_id, rec.get('payment_date') or fields.Date.today()) rec.update({ 'currency_id': invoices[0].currency_id.id, 'amount': abs(amount), 'payment_type': 'inbound' if amount > 0 else 'outbound', 'partner_id': invoices[0].commercial_partner_id.id, 'partner_type': MAP_INVOICE_TYPE_PARTNER_TYPE[invoices[0].type], - 'communication': invoices[0].reference or invoices[0].number, + 'communication': invoices[0].ref or invoices[0].name, 'invoice_ids': [(6, 0, invoices.ids)], }) return rec - @api.one @api.constrains('amount') def _check_amount(self): - if self.amount < 0: - raise ValidationError(_('The payment amount cannot be negative.')) + for payment in self: + if payment.amount < 0: + raise ValidationError(_('The payment amount cannot be negative.')) @api.model def _get_method_codes_using_bank_account(self): @@ -169,11 +161,14 @@ def _compute_hide_payment_method(self): def _compute_payment_difference(self): for pay in self.filtered(lambda p: p.invoice_ids and p.state == 'draft'): payment_amount = -pay.amount if pay.payment_type == 'outbound' else pay.amount - pay.payment_difference = pay._compute_payment_amount() - payment_amount + pay.payment_difference = pay._compute_payment_amount(pay.invoice_ids, pay.currency_id, pay.journal_id, pay.payment_date) - payment_amount @api.onchange('journal_id') def _onchange_journal(self): if self.journal_id: + if self.journal_id.currency_id: + self.currency_id = self.journal_id.currency_id + # Set default payment method (we consider the first to be the default one) payment_methods = self.payment_type == 'inbound' and self.journal_id.inbound_payment_method_ids or self.journal_id.outbound_payment_method_ids payment_methods_list = payment_methods.ids @@ -190,18 +185,18 @@ def _onchange_journal(self): domain = {'payment_method_id': [('payment_type', '=', payment_type), ('id', 'in', payment_methods_list)]} - if self.env.context.get('active_model') == 'account.invoice': + if self.env.context.get('active_model') == 'account.move': active_ids = self._context.get('active_ids') - invoices = self.env['account.invoice'].browse(active_ids) - self.amount = abs(self._compute_payment_amount(invoices)) + invoices = self.env['account.move'].browse(active_ids) + self.amount = abs(self._compute_payment_amount(invoices, self.currency_id, self.journal_id, self.payment_date)) return {'domain': domain} return {} @api.onchange('partner_id') def _onchange_partner_id(self): - if self.invoice_ids and self.invoice_ids[0].partner_bank_id: - self.partner_bank_account_id = self.invoice_ids[0].partner_bank_id + if self.invoice_ids and self.invoice_ids[0].invoice_partner_bank_id: + self.partner_bank_account_id = self.invoice_ids[0].invoice_partner_bank_id elif self.partner_id != self.partner_bank_account_id.partner_id: # This condition ensures we use the default value provided into # context for partner_bank_account_id properly when provided with a @@ -272,7 +267,7 @@ def _onchange_amount(self): @api.onchange('currency_id') def _onchange_currency(self): - self.amount = abs(self._compute_payment_amount()) + self.amount = abs(self._compute_payment_amount(self.invoice_ids, self.currency_id, self.journal_id, self.payment_date)) if self.journal_id: # TODO: only return if currency differ? return @@ -285,36 +280,48 @@ def _onchange_currency(self): if journal: return {'value': {'journal_id': journal.id}} - @api.multi - def _compute_payment_amount(self, invoices=None, currency=None): + @api.model + def _compute_payment_amount(self, invoices, currency, journal, date): '''Compute the total amount for the payment wizard. - :param invoices: If not specified, pick all the invoices. - :param currency: If not specified, search a default currency on wizard/journal. - :return: The total amount to pay the invoices. + :param invoices: Invoices on which compute the total as an account.invoice recordset. + :param currency: The payment's currency as a res.currency record. + :param journal: The payment's journal as an account.journal record. + :param date: The payment's date as a datetime.date object. + :return: The total amount to pay the invoices. ''' + company = journal.company_id + currency = currency or journal.currency_id or company.currency_id + date = date or fields.Date.today() - # Get the payment invoices if not invoices: - invoices = self.invoice_ids - - # Get the payment currency - if not currency: - currency = self.currency_id or self.journal_id.currency_id or self.journal_id.company_id.currency_id + return 0.0 + + self._cr.execute(''' + SELECT + move.type AS type, + move.currency_id AS currency_id, + SUM(line.amount_residual) AS amount_residual, + SUM(line.amount_residual_currency) AS residual_currency + FROM account_move move + LEFT JOIN account_move_line line ON line.move_id = move.id + LEFT JOIN account_account account ON account.id = line.account_id + LEFT JOIN account_account_type account_type ON account_type.id = account.user_type_id + WHERE move.id IN %s + AND account_type.type IN ('receivable', 'payable') + GROUP BY move.id, move.type + ''', [tuple(invoices.ids)]) + query_res = self._cr.dictfetchall() - # Avoid currency rounding issues by summing the amounts according to the company_currency_id before - invoice_datas = invoices.read_group( - [('id', 'in', invoices.ids)], - ['currency_id', 'type', 'residual_signed'], - ['currency_id', 'type'], lazy=False) total = 0.0 - for invoice_data in invoice_datas: - amount_total = MAP_INVOICE_TYPE_PAYMENT_SIGN[invoice_data['type']] * invoice_data['residual_signed'] - payment_currency = self.env['res.currency'].browse(invoice_data['currency_id'][0]) - if payment_currency == currency: - total += amount_total + for res in query_res: + move_currency = self.env['res.currency'].browse(res['currency_id']) + if move_currency == currency and move_currency != company.currency_id: + total += res['residual_currency'] + elif move_currency == currency == company.currency_id: + total += res['amount_residual'] else: - total += payment_currency._convert(amount_total, currency, self.env.company, self.payment_date or fields.Date.today()) + total += move_currency._convert(res['amount_residual'], currency, company, date) return total @api.multi @@ -358,7 +365,8 @@ def open_payment_matching_screen(self): @api.depends('invoice_ids', 'payment_type', 'partner_type', 'partner_id') def _compute_destination_account_id(self): if self.invoice_ids: - self.destination_account_id = self.invoice_ids[0].account_id.id + self.destination_account_id = self.invoice_ids[0].mapped('line_ids.account_id')\ + .filtered(lambda account: account.user_type_id.type in ('receivable', 'payable'))[0] elif self.payment_type == 'transfer': if not self.company_id.transfer_account_id.id: raise UserError(_('There is no Transfer Account defined in the accounting settings. Please define one to be able to confirm this transfer.')) @@ -378,8 +386,9 @@ def _compute_destination_account_id(self): @api.depends('move_line_ids.matched_debit_ids', 'move_line_ids.matched_credit_ids') def _compute_reconciled_invoice_ids(self): for record in self: - record.reconciled_invoice_ids = (record.move_line_ids.mapped('matched_debit_ids.debit_move_id.invoice_id') | - record.move_line_ids.mapped('matched_credit_ids.credit_move_id.invoice_id')) + reconciled_moves = record.move_line_ids.mapped('matched_debit_ids.debit_move_id.move_id')\ + + record.move_line_ids.mapped('matched_credit_ids.credit_move_id.move_id') + record.reconciled_invoice_ids = reconciled_moves.filtered(lambda move: move.is_invoice()) record.has_invoices = bool(record.reconciled_invoice_ids) @api.multi @@ -413,17 +422,13 @@ def button_journal_entries(self): @api.multi def button_invoices(self): - if self.partner_type == 'supplier': - views = [(self.env.ref('account.invoice_supplier_tree').id, 'tree'), (self.env.ref('account.invoice_supplier_form').id, 'form')] - else: - views = [(self.env.ref('account.invoice_tree').id, 'tree'), (self.env.ref('account.invoice_form').id, 'form')] return { 'name': _('Paid Invoices'), 'view_type': 'form', 'view_mode': 'tree,form', - 'res_model': 'account.invoice', + 'res_model': 'account.move', 'view_id': False, - 'views': views, + 'views': [(self.env.ref('account.view_move_tree').id, 'tree'), (self.env.ref('account.view_move_form').id, 'form')], 'type': 'ir.actions.act_window', 'domain': [('id', 'in', [x.id for x in self.reconciled_invoice_ids])], } @@ -457,6 +462,186 @@ def unlink(self): raise UserError(_('It is not allowed to delete a payment that already created a journal entry since it would create a gap in the numbering. You should create the journal entry again and cancel it thanks to a regular revert.')) return super(account_payment, self).unlink() + @api.multi + def _prepare_payment_moves(self): + ''' Prepare the creation of journal entries (account.move) by creating a list of python dictionary to be passed + to the 'create' method. + + Example 1: outbound with write-off: + + Account | Debit | Credit + --------------------------------------------------------- + BANK | 900.0 | + RECEIVABLE | | 1000.0 + WRITE-OFF ACCOUNT | 100.0 | + + Example 2: internal transfer from BANK to CASH: + + Account | Debit | Credit + --------------------------------------------------------- + BANK | | 1000.0 + TRANSFER | 1000.0 | + CASH | 1000.0 | + TRANSFER | | 1000.0 + + :return: A list of Python dictionary to be passed to env['account.move'].create. + ''' + all_move_vals = [] + for payment in self: + company_currency = payment.company_id.currency_id + + # Compute amounts. + write_off_amount = payment.payment_difference_handling == 'reconcile' and -payment.payment_difference or 0.0 + if payment.payment_type in ('outbound', 'transfer'): + counterpart_amount = payment.amount + liquidity_line_account = payment.journal_id.default_debit_account_id + else: + counterpart_amount = -payment.amount + liquidity_line_account = payment.journal_id.default_credit_account_id + + # Manage currency. + if payment.currency_id == company_currency: + # Single-currency. + balance = counterpart_amount + write_off_balance = write_off_amount + counterpart_amount = write_off_amount = 0.0 + currency_id = False + else: + # Multi-currencies. + balance = payment.currency_id._convert(counterpart_amount, company_currency, payment.company_id, payment.payment_date) + write_off_balance = payment.currency_id._convert(write_off_amount, company_currency, payment.company_id, payment.payment_date) + currency_id = payment.currency_id.id + + # Manage custom currency on journal for liquidity line. + if payment.journal_id.currency_id and payment.currency_id != payment.journal_id.currency_id: + # Custom currency on journal. + liquidity_line_currency_id = payment.journal_id.currency_id.id + liquidity_amount = company_currency._convert( + balance, payment.journal_id.currency_id, payment.company_id, payment.payment_date) + else: + # Use the payment currency. + liquidity_line_currency_id = currency_id + liquidity_amount = counterpart_amount + + # Compute 'name' to be used in receivable/payable line. + rec_pay_line_name = '' + if payment.payment_type == 'transfer': + rec_pay_line_name = payment.name + else: + if payment.partner_type == 'customer': + if payment.payment_type == 'inbound': + rec_pay_line_name += _("Customer Payment") + elif payment.payment_type == 'outbound': + rec_pay_line_name += _("Customer Credit Note") + elif payment.partner_type == 'supplier': + if payment.payment_type == 'inbound': + rec_pay_line_name += _("Vendor Credit Note") + elif payment.payment_type == 'outbound': + rec_pay_line_name += _("Vendor Payment") + if payment.invoice_ids: + rec_pay_line_name += ': %s' % ', '.join(payment.invoice_ids.mapped('name')) + + # Compute 'name' to be used in liquidity line. + if payment.payment_type == 'transfer': + liquidity_line_name = _('Transfer to %s') % payment.destination_journal_id.name + else: + liquidity_line_name = payment.name + + # ==== 'inbound' / 'outbound' ==== + + move_vals = { + 'date': payment.payment_date, + 'ref': payment.communication, + 'journal_id': payment.journal_id.id, + 'currency_id': payment.journal_id.currency_id.id or payment.company_id.currency_id.id, + 'partner_id': payment.partner_id.id, + 'line_ids': [ + # Receivable / Payable / Transfer line. + (0, 0, { + 'name': rec_pay_line_name, + 'amount_currency': counterpart_amount + write_off_amount, + 'currency_id': currency_id, + 'debit': balance + write_off_balance > 0.0 and balance + write_off_balance or 0.0, + 'credit': balance + write_off_balance < 0.0 and -balance - write_off_balance or 0.0, + 'date_maturity': payment.payment_date, + 'partner_id': payment.partner_id.id, + 'account_id': payment.destination_account_id.id, + 'payment_id': payment.id, + }), + # Liquidity line. + (0, 0, { + 'name': liquidity_line_name, + 'amount_currency': -liquidity_amount, + 'currency_id': liquidity_line_currency_id, + 'debit': balance < 0.0 and -balance or 0.0, + 'credit': balance > 0.0 and balance or 0.0, + 'date_maturity': payment.payment_date, + 'partner_id': payment.partner_id.id, + 'account_id': liquidity_line_account.id, + 'payment_id': payment.id, + }), + ], + } + if write_off_balance: + # Write-off line. + move_vals['line_ids'].append((0, 0, { + 'name': payment.writeoff_label, + 'amount_currency': -write_off_amount, + 'currency_id': currency_id, + 'debit': write_off_balance < 0.0 and -write_off_balance or 0.0, + 'credit': write_off_balance > 0.0 and write_off_balance or 0.0, + 'date_maturity': payment.payment_date, + 'partner_id': payment.partner_id.id, + 'account_id': payment.writeoff_account_id.id, + 'payment_id': payment.id, + })) + + all_move_vals.append(move_vals) + + # ==== 'transfer' ==== + if payment.payment_type == 'transfer': + + if payment.destination_journal_id.currency_id: + transfer_amount = payment.currency_id._convert(counterpart_amount, payment.destination_journal_id.currency_id, payment.company_id, payment.payment_date) + else: + transfer_amount = 0.0 + + transfer_move_vals = { + 'date': payment.payment_date, + 'ref': payment.communication, + 'partner_id': payment.partner_id.id, + 'journal_id': payment.destination_journal_id.id, + 'line_ids': [ + # Transfer debit line. + (0, 0, { + 'name': payment.name, + 'amount_currency': -counterpart_amount, + 'currency_id': currency_id, + 'debit': balance < 0.0 and -balance or 0.0, + 'credit': balance > 0.0 and balance or 0.0, + 'date_maturity': payment.payment_date, + 'partner_id': payment.partner_id.id, + 'account_id': payment.company_id.transfer_account_id.id, + 'payment_id': payment.id, + }), + # Liquidity credit line. + (0, 0, { + 'name': _('Transfer from %s') % payment.journal_id.name, + 'amount_currency': transfer_amount, + 'currency_id': payment.destination_journal_id.currency_id.id, + 'debit': balance > 0.0 and balance or 0.0, + 'credit': balance < 0.0 and -balance or 0.0, + 'date_maturity': payment.payment_date, + 'partner_id': payment.partner_id.id, + 'account_id': payment.destination_journal_id.default_credit_account_id.id, + 'payment_id': payment.id, + }), + ], + } + + all_move_vals.append(transfer_move_vals) + return all_move_vals + @api.multi def post(self): """ Create the journal items for the payment and update the payment's state to 'posted'. @@ -465,12 +650,13 @@ def post(self): If invoice_ids is not empty, there will be one reconcilable move line per invoice to reconcile with. If the payment is a transfer, a second journal entry is created in the destination journal to receive money from the transfer account. """ + AccountMove = self.env['account.move'].with_context(default_type='entry') for rec in self: if rec.state != 'draft': raise UserError(_("Only a draft payment can be posted.")) - if any(inv.state != 'open' for inv in rec.invoice_ids): + if any(inv.state != 'posted' for inv in rec.invoice_ids): raise ValidationError(_("The payment cannot be processed because the invoice is not open!")) # keep the name in case of a payment reset to draft @@ -489,187 +675,35 @@ def post(self): sequence_code = 'account.payment.supplier.refund' if rec.payment_type == 'outbound': sequence_code = 'account.payment.supplier.invoice' - rec.name = self.env['ir.sequence'].with_context(ir_sequence_date=rec.payment_date).next_by_code(sequence_code) + rec.name = self.env['ir.sequence'].next_by_code(sequence_code, sequence_date=rec.payment_date) if not rec.name and rec.payment_type != 'transfer': raise UserError(_("You have to define a sequence for %s in your company.") % (sequence_code,)) - # Create the journal entry - amount = rec.amount * (rec.payment_type in ('outbound', 'transfer') and 1 or -1) - move = rec._create_payment_entry(amount) + moves = AccountMove.create(rec._prepare_payment_moves()) + moves.filtered(lambda move: not move.journal_id.post_at_bank_rec).post() + + # Update the state / move before performing any reconciliation. + rec.write({'state': 'posted', 'move_name': moves[0].name}) - # In case of a transfer, the first journal entry created debited the source liquidity account and credited - # the transfer account. Now we debit the transfer account and credit the destination liquidity account. - if rec.payment_type == 'transfer': - transfer_credit_aml = move.line_ids.filtered(lambda r: r.account_id == rec.company_id.transfer_account_id) - transfer_debit_aml = rec._create_transfer_entry(amount) - (transfer_credit_aml + transfer_debit_aml).reconcile() + if rec.payment_type in ('inbound', 'outbound'): + # ==== 'inbound' / 'outbound' ==== + if rec.invoice_ids: + (moves[0] + rec.invoice_ids).line_ids \ + .filtered(lambda line: not line.reconciled and line.account_id == rec.destination_account_id)\ + .reconcile() + elif rec.payment_type == 'transfer': + # ==== 'transfer' ==== + moves.mapped('line_ids')\ + .filtered(lambda line: line.account_id == rec.company_id.transfer_account_id)\ + .reconcile() - rec.write({'state': 'posted', 'move_name': move.name}) return True @api.multi def action_draft(self): return self.write({'state': 'draft'}) - def _create_payment_entry(self, amount): - """ Create a journal entry corresponding to a payment, if the payment references invoice(s) they are reconciled. - Return the journal entry. - """ - aml_obj = self.env['account.move.line'].with_context(check_move_validity=False) - debit, credit, amount_currency, currency_id = aml_obj.with_context(date=self.payment_date)._compute_amount_fields(amount, self.currency_id, self.company_id.currency_id) - - move = self.env['account.move'].create(self._get_move_vals()) - - #Write line corresponding to invoice payment - counterpart_aml_dict = self._get_shared_move_line_vals(debit, credit, amount_currency, move.id, False) - counterpart_aml_dict.update(self._get_counterpart_move_line_vals(self.invoice_ids)) - counterpart_aml_dict.update({'currency_id': currency_id}) - counterpart_aml = aml_obj.create(counterpart_aml_dict) - - #Reconcile with the invoices - if self.payment_difference_handling == 'reconcile' and self.payment_difference: - writeoff_line = self._get_shared_move_line_vals(0, 0, 0, move.id, False) - debit_wo, credit_wo, amount_currency_wo, currency_id = aml_obj.with_context(date=self.payment_date)._compute_amount_fields(self.payment_difference, self.currency_id, self.company_id.currency_id) - writeoff_line['name'] = self.writeoff_label - writeoff_line['account_id'] = self.writeoff_account_id.id - writeoff_line['debit'] = debit_wo - writeoff_line['credit'] = credit_wo - writeoff_line['amount_currency'] = amount_currency_wo - writeoff_line['currency_id'] = currency_id - writeoff_line = aml_obj.create(writeoff_line) - if counterpart_aml['debit'] or (writeoff_line['credit'] and not counterpart_aml['credit']): - counterpart_aml['debit'] += credit_wo - debit_wo - if counterpart_aml['credit'] or (writeoff_line['debit'] and not counterpart_aml['debit']): - counterpart_aml['credit'] += debit_wo - credit_wo - counterpart_aml['amount_currency'] -= amount_currency_wo - - #Write counterpart lines - if not self.currency_id.is_zero(self.amount): - if not self.currency_id != self.company_id.currency_id: - amount_currency = 0 - liquidity_aml_dict = self._get_shared_move_line_vals(credit, debit, -amount_currency, move.id, False) - liquidity_aml_dict.update(self._get_liquidity_move_line_vals(-amount)) - aml_obj.create(liquidity_aml_dict) - - #validate the payment - if not self.journal_id.post_at_bank_rec: - move.post() - - #reconcile the invoice receivable/payable line(s) with the payment - if self.invoice_ids: - self.invoice_ids.register_payment(counterpart_aml) - - return move - - def _create_transfer_entry(self, amount): - """ Create the journal entry corresponding to the 'incoming money' part of an internal transfer, return the reconcilable move line - """ - aml_obj = self.env['account.move.line'].with_context(check_move_validity=False) - debit, credit, amount_currency, dummy = aml_obj.with_context(date=self.payment_date)._compute_amount_fields(amount, self.currency_id, self.company_id.currency_id) - amount_currency = self.destination_journal_id.currency_id and self.currency_id._convert(amount, self.destination_journal_id.currency_id, self.company_id, self.payment_date or fields.Date.today()) or 0 - - dst_move = self.env['account.move'].create(self._get_move_vals(self.destination_journal_id)) - - dst_liquidity_aml_dict = self._get_shared_move_line_vals(debit, credit, amount_currency, dst_move.id) - dst_liquidity_aml_dict.update({ - 'name': _('Transfer from %s') % self.journal_id.name, - 'account_id': self.destination_journal_id.default_credit_account_id.id, - 'currency_id': self.destination_journal_id.currency_id.id, - 'journal_id': self.destination_journal_id.id}) - aml_obj.create(dst_liquidity_aml_dict) - - transfer_debit_aml_dict = self._get_shared_move_line_vals(credit, debit, 0, dst_move.id) - transfer_debit_aml_dict.update({ - 'name': self.name, - 'account_id': self.company_id.transfer_account_id.id, - 'journal_id': self.destination_journal_id.id}) - if self.currency_id != self.company_id.currency_id: - transfer_debit_aml_dict.update({ - 'currency_id': self.currency_id.id, - 'amount_currency': -self.amount, - }) - transfer_debit_aml = aml_obj.create(transfer_debit_aml_dict) - if not self.destination_journal_id.post_at_bank_rec: - dst_move.post() - return transfer_debit_aml - - def _get_move_vals(self, journal=None): - """ Return dict to create the payment move - """ - journal = journal or self.journal_id - move_vals = { - 'date': self.payment_date, - 'ref': self.communication or '', - 'company_id': self.company_id.id, - 'journal_id': journal.id, - } - if self.move_name: - move_vals['name'] = self.move_name - return move_vals - - def _get_shared_move_line_vals(self, debit, credit, amount_currency, move_id, invoice_id=False): - """ Returns values common to both move lines (except for debit, credit and amount_currency which are reversed) - """ - return { - 'partner_id': self.payment_type in ('inbound', 'outbound') and self.env['res.partner']._find_accounting_partner(self.partner_id).id or False, - 'invoice_id': invoice_id and invoice_id.id or False, - 'move_id': move_id, - 'debit': debit, - 'credit': credit, - 'amount_currency': amount_currency or False, - 'payment_id': self.id, - 'journal_id': self.journal_id.id, - } - - def _get_counterpart_move_line_vals(self, invoice=False): - if self.payment_type == 'transfer': - name = self.name - else: - name = '' - if self.partner_type == 'customer': - if self.payment_type == 'inbound': - name += _("Customer Payment") - elif self.payment_type == 'outbound': - name += _("Customer Credit Note") - elif self.partner_type == 'supplier': - if self.payment_type == 'inbound': - name += _("Vendor Credit Note") - elif self.payment_type == 'outbound': - name += _("Vendor Payment") - if invoice: - name += ': ' - for inv in invoice: - if inv.move_id: - name += inv.number + ', ' - name = name[:len(name)-2] - return { - 'name': name, - 'account_id': self.destination_account_id.id, - 'currency_id': self.currency_id != self.company_id.currency_id and self.currency_id.id or False, - } - - def _get_liquidity_move_line_vals(self, amount): - name = self.name - if self.payment_type == 'transfer': - name = _('Transfer to %s') % self.destination_journal_id.name - vals = { - 'name': name, - 'account_id': self.payment_type in ('outbound', 'transfer') and self.journal_id.default_debit_account_id.id or self.journal_id.default_credit_account_id.id, - 'journal_id': self.journal_id.id, - 'currency_id': self.currency_id != self.company_id.currency_id and self.currency_id.id or False, - } - - # If the journal has a currency specified, the journal item need to be expressed in this currency - if self.journal_id.currency_id and self.currency_id != self.journal_id.currency_id: - amount = self.currency_id._convert(amount, self.journal_id.currency_id, self.company_id, self.payment_date or fields.Date.today()) - debit, credit, amount_currency, dummy = self.env['account.move.line'].with_context(date=self.payment_date)._compute_amount_fields(amount, self.journal_id.currency_id, self.company_id.currency_id) - vals.update({ - 'amount_currency': amount_currency, - 'currency_id': self.journal_id.currency_id.id, - }) - - return vals - + @api.multi def _get_invoice_payment_amount(self, inv): """ Computes the amount covered by the current payment in the given invoice. @@ -680,11 +714,10 @@ def _get_invoice_payment_amount(self, inv): self.ensure_one() return sum([ data['amount'] - for data in inv._get_payments_vals() + for data in inv._get_reconciled_info_JSON_values() if data['account_payment_id'] == self.id ]) - class payment_register(models.TransientModel): _name = 'account.payment.register' _description = 'Register Payment' @@ -697,21 +730,20 @@ class payment_register(models.TransientModel): "Check: Pay bill by check and print it from Odoo.\n" "Batch Deposit: Encase several customer checks at once by generating a batch deposit to submit to your bank. When encoding the bank statement in Odoo, you are suggested to reconcile the transaction with the batch deposit.To enable batch deposit, module account_batch_payment must be installed.\n" "SEPA Credit Transfer: Pay bill from a SEPA Credit Transfer file you submit to your bank. To enable sepa credit transfer, module account_sepa must be installed ") - invoice_ids = fields.Many2many('account.invoice', 'account_invoice_payment_rel_transient', 'payment_id', 'invoice_id', string="Invoices", copy=False, readonly=True) + invoice_ids = fields.Many2many('account.move', 'account_invoice_payment_rel_transient', 'payment_id', 'invoice_id', string="Invoices", copy=False, readonly=True) @api.model def default_get(self, fields): rec = super(payment_register, self).default_get(fields) active_ids = self._context.get('active_ids') - active_model = self._context.get('active_model') - invoices = self.env['account.invoice'].browse(active_ids) + invoices = self.env['account.move'].browse(active_ids) # Check all invoices are open - if any(invoice.state != 'open' for invoice in invoices): + if any(invoice.state != 'posted' or invoice.invoice_payment_state != 'not_paid' or not invoice.is_invoice() for invoice in invoices): raise UserError(_("You can only register payments for open invoices")) # Check all invoices are inbound or all invoices are outbound - outbound_list = [invoice.type in ('in_invoice', 'out_refund') for invoice in invoices] - first_outbound = invoices[0].type in ('in_invoice', 'out_refund') + outbound_list = [invoice.is_outbound() for invoice in invoices] + first_outbound = invoices[0].is_outbound() if any(x != first_outbound for x in outbound_list): raise UserError(_("You can only register at the same time for payment that are all inbound or all outbound")) if 'invoice_ids' not in rec: @@ -719,7 +751,7 @@ def default_get(self, fields): if 'journal_id' not in rec: rec['journal_id'] = self.env['account.journal'].search([('company_id', '=', self.env.company.id), ('type', 'in', ('bank', 'cash'))], limit=1).id if 'payment_method_id' not in rec: - if invoices[0].type in ('out_invoice', 'in_refund'): + if invoices[0].is_inbound(): domain = [('payment_type', '=', 'inbound')] else: domain = [('payment_type', '=', 'outbound')] @@ -729,9 +761,9 @@ def default_get(self, fields): @api.onchange('journal_id', 'invoice_ids') def _onchange_journal(self): active_ids = self._context.get('active_ids') - invoices = self.env['account.invoice'].browse(active_ids) + invoices = self.env['account.move'].browse(active_ids) if self.journal_id and invoices: - if invoices[0].type in ('out_invoice', 'in_refund'): + if invoices[0].is_inbound(): domain = [('payment_type', '=', 'inbound'), ('id', 'in', self.journal_id.inbound_payment_method_ids.ids)] else: domain = [('payment_type', '=', 'outbound'), ('id', 'in', self.journal_id.outbound_payment_method_ids.ids)] @@ -746,19 +778,19 @@ def _prepare_payment_vals(self, invoice): :param invoice: A single invoice/bill to pay. :return: The payment values as a dictionary. ''' - amount = self.env['account.payment']._compute_payment_amount(invoices=invoice, currency=invoice.currency_id) + amount = self.env['account.payment']._compute_payment_amount(invoice, invoice.currency_id, self.journal_id, self.payment_date) values = { 'journal_id': self.journal_id.id, 'payment_method_id': self.payment_method_id.id, 'payment_date': self.payment_date, - 'communication': invoice.reference or invoice.number, + 'communication': invoice.ref or invoice.name, 'invoice_ids': [(6, 0, invoice.ids)], 'payment_type': ('inbound' if amount > 0 else 'outbound'), 'amount': abs(amount), 'currency_id': invoice.currency_id.id, 'partner_id': invoice.commercial_partner_id.id, 'partner_type': MAP_INVOICE_TYPE_PARTNER_TYPE[invoice.type], - 'partner_bank_account_id': invoice.partner_bank_id.id, + 'partner_bank_account_id': invoice.invoice_partner_bank_id.id, } return values diff --git a/addons/account/models/account_payment_term.py b/addons/account/models/account_payment_term.py index bec851e8d6bbb..d8705908bc493 100644 --- a/addons/account/models/account_payment_term.py +++ b/addons/account/models/account_payment_term.py @@ -33,15 +33,15 @@ def _check_lines(self): raise ValidationError(_('A Payment Term should have only one line of type Balance.')) @api.multi - def compute(self, value, date_ref=False): + def compute(self, value, date_ref=False, currency=None): self.ensure_one() date_ref = date_ref or fields.Date.today() amount = value sign = value < 0 and -1 or 1 result = [] - if self.env.context.get('currency_id'): + if not currency and self.env.context.get('currency_id'): currency = self.env['res.currency'].browse(self.env.context['currency_id']) - else: + elif not currency: currency = self.env.company.currency_id for line in self.line_ids: if line.value == 'fixed': @@ -50,19 +50,18 @@ def compute(self, value, date_ref=False): amt = currency.round(value * (line.value_amount / 100.0)) elif line.value == 'balance': amt = currency.round(amount) - if amt: - next_date = fields.Date.from_string(date_ref) - if line.option == 'day_after_invoice_date': - next_date += relativedelta(days=line.days) - if line.day_of_the_month > 0: - months_delta = (line.day_of_the_month < next_date.day) and 1 or 0 - next_date += relativedelta(day=line.day_of_the_month, months=months_delta) - elif line.option == 'day_following_month': - next_date += relativedelta(day=line.days, months=1) - elif line.option == 'day_current_month': - next_date += relativedelta(day=line.days, months=0) - result.append((fields.Date.to_string(next_date), amt)) - amount -= amt + next_date = fields.Date.from_string(date_ref) + if line.option == 'day_after_invoice_date': + next_date += relativedelta(days=line.days) + if line.day_of_the_month > 0: + months_delta = (line.day_of_the_month < next_date.day) and 1 or 0 + next_date += relativedelta(day=line.day_of_the_month, months=months_delta) + elif line.option == 'day_following_month': + next_date += relativedelta(day=line.days, months=1) + elif line.option == 'day_current_month': + next_date += relativedelta(day=line.days, months=0) + result.append((fields.Date.to_string(next_date), amt)) + amount -= amt amount = sum(amt for _, amt in result) dist = currency.round(value - amount) if dist: @@ -73,7 +72,7 @@ def compute(self, value, date_ref=False): @api.multi def unlink(self): for terms in self: - if self.env['account.invoice'].search([('payment_term_id', 'in', terms.ids)]): + if self.env['account.move'].search([('payment_term_id', 'in', terms.ids)]): raise UserError(_('You can not delete payment terms as other records still reference it. However, you can archive it.')) property_recs = self.env['ir.property'].search([('value_reference', 'in', ['account.payment.term,%s'%payment_term.id for payment_term in terms])]) property_recs.unlink() diff --git a/addons/account/models/account_reconcile_model.py b/addons/account/models/account_reconcile_model.py index 4966d498f1b99..67523d4e56242 100644 --- a/addons/account/models/account_reconcile_model.py +++ b/addons/account/models/account_reconcile_model.py @@ -102,18 +102,25 @@ class AccountReconcileModel(models.Model): second_analytic_account_id = fields.Many2one('account.analytic.account', string='Second Analytic Account', ondelete='set null') second_analytic_tag_ids = fields.Many2many('account.analytic.tag', string='Second Analytic Tags', relation='account_reconcile_model_second_analytic_tag_rel') - + number_entries = fields.Integer(string='Number of entries related to this model', compute='_compute_number_entries') @api.multi def action_reconcile_stat(self): + self.ensure_one() action = self.env.ref('account.action_move_journal_line').read()[0] + self._cr.execute(''' + SELECT ARRAY_AGG(DISTINCT move_id) + FROM account_move_line + WHERE reconcile_model_id = %s + ''', [self.id]) action.update({ - 'context': {'search_default_reconcile_model_id': self.name}, + 'context': {}, + 'domain': [('id', 'in', self._cr.fetchone()[0])], 'help': """

{}

""".format(_('No move from this reconciliation model')), }) return action - + @api.multi def _compute_number_entries(self): data = self.env['account.move.line'].read_group([('reconcile_model_id', 'in', self.ids)], ['reconcile_model_ids'], 'reconcile_model_id') @@ -438,10 +445,11 @@ def _get_invoice_matching_query(self, st_lines, excluded_ids=None, partner_map=N LEFT JOIN res_company company ON company.id = st_line.company_id LEFT JOIN partners_table line_partner ON line_partner.line_id = st_line.id , account_move_line aml - LEFT JOIN account_move move ON move.id = aml.move_id + LEFT JOIN account_move move ON move.id = aml.move_id AND move.state = 'posted' LEFT JOIN account_account account ON account.id = aml.account_id WHERE st_line.id IN %s AND aml.company_id = st_line.company_id + AND move.state = 'posted' AND ( -- the field match_partner of the rule might enforce the second part of -- the OR condition, later in _apply_conditions() @@ -456,7 +464,7 @@ def _get_invoice_matching_query(self, st_lines, excluded_ids=None, partner_map=N -- if there is a partner, propose all aml of the partner, otherwise propose only the ones -- matching the statement line communication - AND + AND ( ( line_partner.partner_id != 0 diff --git a/addons/account/models/chart_template.py b/addons/account/models/chart_template.py index 6409ae1344518..4ec8f31a0dd67 100644 --- a/addons/account/models/chart_template.py +++ b/addons/account/models/chart_template.py @@ -264,7 +264,7 @@ def existing_accounting(self, company_id): the provided company (meaning hence that its chart of accounts cannot be changed anymore). """ - model_to_check = ['account.move.line', 'account.invoice', 'account.payment', 'account.bank.statement'] + model_to_check = ['account.move', 'account.payment', 'account.bank.statement'] for model in model_to_check: if len(self.env[model].search([('company_id', '=', company_id.id)])) > 0: return True diff --git a/addons/account/models/company.py b/addons/account/models/company.py index b730ee001b31f..72634128406a3 100644 --- a/addons/account/models/company.py +++ b/addons/account/models/company.py @@ -2,7 +2,6 @@ from datetime import timedelta, datetime import calendar -import time from dateutil.relativedelta import relativedelta from odoo import fields, models, api, _ @@ -10,6 +9,7 @@ from odoo.tools.misc import DEFAULT_SERVER_DATE_FORMAT from odoo.tools.float_utils import float_round, float_is_zero from odoo.tools import date_utils +from odoo.tests.common import Form MONTH_SELECTION = [ @@ -102,7 +102,7 @@ class ResCompany(models.Model): account_dashboard_onboarding_state = fields.Selection([('not_done', "Not done"), ('just_done', "Just done"), ('done', "Done"), ('closed', "Closed")], string="State of the account dashboard onboarding panel", default='not_done') invoice_terms = fields.Text(string='Default Terms and Conditions', translate=True) - @api.constrains('account_opening_date', 'fiscalyear_last_day', 'fiscalyear_last_month') + @api.constrains('account_opening_move_id', 'fiscalyear_last_day', 'fiscalyear_last_month') def _check_fiscalyear_last_day(self): # if the user explicitly chooses the 29th of February we allow it: # there is no "fiscalyear_last_year" so we do not know his intentions. @@ -484,7 +484,7 @@ def _get_sample_invoice(self): company_id = self.env.company.id # try to find an existing sample invoice - sample_invoice = self.env['account.invoice'].search( + sample_invoice = self.env['account.move'].search( [('company_id', '=', company_id), ('partner_id', '=', partner.id)], limit=1) @@ -499,25 +499,23 @@ def _get_sample_invoice(self): "\nPlease go to Configuration > Journals.") raise RedirectWarning(msg, action.id, _("Go to the journal configuration")) - sample_invoice = self.env['account.invoice'].create({ - 'name': _("Sample invoice"), - 'journal_id': journal.id, + sample_invoice = self.env['account.move'].with_context(default_type='out_invoice', default_journal_id=journal.id).create({ + 'invoice_payment_ref': _('Sample invoice'), 'partner_id': partner.id, - }) - # sample invoice lines - self.env['account.invoice.line'].create({ - 'name': _("Sample invoice line name"), - 'invoice_id': sample_invoice.id, - 'account_id': account.id, - 'price_unit': 199.99, - 'quantity': 2, - }) - self.env['account.invoice.line'].create({ - 'name': _("Sample invoice line name 2"), - 'invoice_id': sample_invoice.id, - 'account_id': account.id, - 'price_unit': 25, - 'quantity': 1, + 'invoice_line_ids': [ + (0, 0, { + 'name': _('Sample invoice line name'), + 'account_id': account.id, + 'quantity': 2, + 'price_unit': 199.99, + }), + (0, 0, { + 'name': _('Sample invoice line name 2'), + 'account_id': account.id, + 'quantity': 1, + 'price_unit': 25.0, + }), + ], }) return sample_invoice @@ -532,7 +530,7 @@ def action_open_account_onboarding_sample_invoice(self): 'default_res_id': sample_invoice.id, 'default_use_template': bool(template), 'default_template_id': template and template.id or False, - 'default_model': 'account.invoice', + 'default_model': 'account.move', 'default_composition_mode': 'comment', 'mark_invoice_as_sent': True, 'custom_layout': 'mail.mail_notification_borders', diff --git a/addons/account/models/digest.py b/addons/account/models/digest.py index 0160efe52259e..da7d673053b37 100644 --- a/addons/account/models/digest.py +++ b/addons/account/models/digest.py @@ -16,14 +16,18 @@ def _compute_kpi_account_total_revenue_value(self): raise AccessError(_("Do not have access, skip this data for user's digest email")) for record in self: start, end, company = record._get_kpi_compute_parameters() - account_moves = self.env['account.move'].read_group([ - ('journal_id.type', '=', 'sale'), - ('company_id', '=', company.id), - ('date', '>=', start), - ('date', '<', end)], ['journal_id', 'amount'], ['journal_id']) - record.kpi_account_total_revenue_value = sum([account_move['amount'] for account_move in account_moves]) + self._cr.execute(''' + SELECT SUM(line.debit) + FROM account_move_line line + JOIN account_move move ON move.id = line.move_id + JOIN account_journal journal ON journal.id = move.journal_id + WHERE line.company_id = %s AND line.date >= %s AND line.date < %s + AND journal.type = 'sale' + ''', [company.id, start, end]) + query_res = self._cr.fetchone() + record.kpi_account_total_revenue_value = query_res and query_res[0] or 0.0 def compute_kpis_actions(self, company, user): res = super(Digest, self).compute_kpis_actions(company, user) - res['kpi_account_total_revenue'] = 'account.action_invoice_tree1&menu_id=%s' % self.env.ref('account.menu_finance').id + res['kpi_account_total_revenue'] = 'account.action_move_out_invoice_type&menu_id=%s' % self.env.ref('account.menu_finance').id return res diff --git a/addons/account/models/partner.py b/addons/account/models/partner.py index 4a706f93c7b54..2161634f93ed0 100644 --- a/addons/account/models/partner.py +++ b/addons/account/models/partner.py @@ -209,7 +209,7 @@ def _credit_debit_get(self): if where_clause: where_clause = 'AND ' + where_clause self._cr.execute("""SELECT account_move_line.partner_id, act.type, SUM(account_move_line.amount_residual) - FROM account_move_line + FROM """ + tables + """ LEFT JOIN account_account a ON (account_move_line.account_id=a.id) LEFT JOIN account_account_type act ON (a.user_type_id=act.id) WHERE act.type IN ('receivable','payable') @@ -286,7 +286,7 @@ def _invoice_total(self): # price_total is in the company currency query = """ - SELECT SUM(price_total) as total, partner_id + SELECT SUM(price_subtotal) as total, partner_id FROM account_invoice_report account_invoice_report WHERE %s GROUP BY partner_id @@ -410,7 +410,7 @@ def _get_company_currency(self): help='Last time the invoices & payments matching was performed for this partner. ' 'It is set either if there\'s not at least an unreconciled debit and an unreconciled credit ' 'or if you click the "Done" button.') - invoice_ids = fields.One2many('account.invoice', 'partner_id', string='Invoices', readonly=True, copy=False) + invoice_ids = fields.One2many('account.move', 'partner_id', string='Invoices', readonly=True, copy=False) contract_ids = fields.One2many('account.analytic.account', 'partner_id', string='Partner Contracts', readonly=True) bank_account_count = fields.Integer(compute='_compute_bank_count', string="Bank") trust = fields.Selection([('good', 'Good Debtor'), ('normal', 'Normal Debtor'), ('bad', 'Bad Debtor')], string='Degree of trust you have in this debtor', default='normal', company_dependent=True) @@ -437,14 +437,16 @@ def _commercial_fields(self): @api.multi def action_view_partner_invoices(self): self.ensure_one() - action = self.env.ref('account.action_invoice_refund_out_tree').read()[0] - action['domain'] = literal_eval(action['domain']) - action['domain'].append(('partner_id', 'child_of', self.id)) + action = self.env.ref('account.action_move_out_invoice_type').read()[0] + action['domain'] = [ + ('type', 'in', ('out_invoice', 'out_refund')), + ('type', '=', 'posted'), + ('partner_id', 'child_of', self.id), + ] return action @api.onchange('company_id') def _onchange_company_id(self): - company = self.env['res.company'] if self.company_id: company = self.company_id else: @@ -455,10 +457,9 @@ def can_edit_vat(self): can_edit_vat = super(ResPartner, self).can_edit_vat() if not can_edit_vat: return can_edit_vat - Invoice = self.env['account.invoice'] - has_invoice = Invoice.search([ + has_invoice = self.env['account.move'].search([ ('type', 'in', ['out_invoice', 'out_refund']), ('partner_id', 'child_of', self.commercial_partner_id.id), - ('state', 'not in', ['draft', 'cancel']) + ('state', '=', 'posted') ], limit=1) return can_edit_vat and not (bool(has_invoice)) diff --git a/addons/account/models/product.py b/addons/account/models/product.py index f9b046c652a86..2282da699c4fe 100644 --- a/addons/account/models/product.py +++ b/addons/account/models/product.py @@ -55,31 +55,3 @@ def get_product_accounts(self, fiscal_pos=None): if not fiscal_pos: fiscal_pos = self.env['account.fiscal.position'] return fiscal_pos.map_accounts(accounts) - -class ProductProduct(models.Model): - _inherit = "product.product" - - @api.model - def _convert_prepared_anglosaxon_line(self, line, partner): - return { - 'date_maturity': line.get('date_maturity', False), - 'partner_id': partner, - 'name': line['name'], - 'debit': line['price'] > 0 and line['price'], - 'credit': line['price'] < 0 and -line['price'], - 'account_id': line['account_id'], - 'analytic_line_ids': line.get('analytic_line_ids', []), - 'amount_currency': line['price'] > 0 and abs(line.get('amount_currency', False)) or -abs(line.get('amount_currency', False)), - 'currency_id': line.get('currency_id', False), - 'quantity': line.get('quantity', 1.00), - 'product_id': line.get('product_id', False), - 'product_uom_id': line.get('uom_id', False), - 'analytic_account_id': line.get('account_analytic_id', False), - 'invoice_id': line.get('invoice_id', False), - 'tax_ids': line.get('tax_ids', False), - 'tax_line_id': line.get('tax_line_id', False), - 'analytic_tag_ids': line.get('analytic_tag_ids', False), - 'tax_repartition_line_id': line.get('tax_repartition_line_id'), - 'tag_ids': line.get('tag_ids'), - 'tax_base_amount': line.get('tax_base_amount'), - } diff --git a/addons/account/models/reconciliation_widget.py b/addons/account/models/reconciliation_widget.py index f10bacfa3f339..5e49e94e2729c 100644 --- a/addons/account/models/reconciliation_widget.py +++ b/addons/account/models/reconciliation_widget.py @@ -200,7 +200,7 @@ def get_bank_statement_data(self, bank_statement_line_ids, search_str=False): """ if not bank_statement_line_ids: return {} - edition_mode = self._context.get('edition_mode') + suspense_moves_mode = self._context.get('suspense_moves_mode') bank_statements = self.env['account.bank.statement.line'].browse(bank_statement_line_ids).mapped('statement_id') search_sql = ''' @@ -220,7 +220,7 @@ def get_bank_statement_data(self, bank_statement_line_ids, search_str=False): {srch} GROUP BY line.id '''.format( - cond=not edition_mode and "AND NOT EXISTS (SELECT 1 from account_move_line aml WHERE aml.statement_line_id = line.id)" or "", + cond=not suspense_moves_mode and "AND NOT EXISTS (SELECT 1 from account_move_line aml WHERE aml.statement_line_id = line.id)" or "", srch=search_str and search_sql or "", ) self.env.cr.execute(query, {'ids':tuple(bank_statement_line_ids), 'search_str':search_str}) @@ -503,7 +503,7 @@ def _domain_move_lines_for_reconciliation(self, st_line, aml_accounts, partner_i AccountMoveLine = self.env['account.move.line'] #Always exclude the journal items that have been marked as 'to be checked' in a former bank statement reconciliation - to_check_excluded = AccountMoveLine.search(AccountMoveLine._get_domain_for_edition_mode()).ids + to_check_excluded = AccountMoveLine.search(AccountMoveLine._get_suspense_moves_domain()).ids excluded_ids.extend(to_check_excluded) domain_reconciliation = [ diff --git a/addons/account/report/account_invoice_report.py b/addons/account/report/account_invoice_report.py index 2cebf29d6d5b4..d63c997347f00 100644 --- a/addons/account/report/account_invoice_report.py +++ b/addons/account/report/account_invoice_report.py @@ -8,46 +8,19 @@ class AccountInvoiceReport(models.Model): _name = "account.invoice.report" _description = "Invoices Statistics" _auto = False - _rec_name = 'date' + _rec_name = 'invoice_date' + _order = 'invoice_date desc' - @api.multi - @api.depends('currency_id', 'date', 'price_total', 'price_average', 'residual') - def _compute_amounts_in_user_currency(self): - """Compute the amounts in the currency of the user - """ - user_currency_id = self.env.company.currency_id - currency_rate_id = self.env['res.currency.rate'].search([ - ('rate', '=', 1), - '|', ('company_id', '=', self.env.company.id), ('company_id', '=', False)], limit=1) - base_currency_id = currency_rate_id.currency_id - for record in self: - date = record.date or fields.Date.today() - company = record.company_id - record.user_currency_price_total = base_currency_id._convert(record.price_total, user_currency_id, company, date) - record.user_currency_price_average = base_currency_id._convert(record.price_average, user_currency_id, company, date) - record.user_currency_residual = base_currency_id._convert(record.residual, user_currency_id, company, date) - - number = fields.Char('Invoice #', readonly=True) - date = fields.Date(readonly=True, string="Invoice Date") - product_id = fields.Many2one('product.product', string='Product', readonly=True) - product_qty = fields.Float(string='Product Quantity', readonly=True) - uom_name = fields.Char(string='Reference Unit of Measure', readonly=True) - payment_term_id = fields.Many2one('account.payment.term', string='Payment Terms', oldname='payment_term', readonly=True) - fiscal_position_id = fields.Many2one('account.fiscal.position', oldname='fiscal_position', string='Fiscal Position', readonly=True) - currency_id = fields.Many2one('res.currency', string='Currency', readonly=True) - categ_id = fields.Many2one('product.category', string='Product Category', readonly=True) + # ==== Invoice fields ==== + move_id = fields.Many2one('account.move', readonly=True) + name = fields.Char('Invoice #', readonly=True) journal_id = fields.Many2one('account.journal', string='Journal', readonly=True) + company_id = fields.Many2one('res.company', string='Company', readonly=True) + currency_id = fields.Many2one('res.currency', string='Currency', readonly=True) partner_id = fields.Many2one('res.partner', string='Partner', readonly=True) commercial_partner_id = fields.Many2one('res.partner', string='Partner Company', help="Commercial Entity") - company_id = fields.Many2one('res.company', string='Company', readonly=True) - user_id = fields.Many2one('res.users', string='Salesperson', readonly=True) - price_total = fields.Float(string='Untaxed Total', readonly=True) - user_currency_price_total = fields.Float(string="Total Without Tax in Currency", compute='_compute_amounts_in_user_currency', digits=0) - price_average = fields.Float(string='Average Price', readonly=True, group_operator="avg") - user_currency_price_average = fields.Float(string="Average Price in Currency", compute='_compute_amounts_in_user_currency', digits=0) - currency_rate = fields.Float(string='Currency Rate', readonly=True, group_operator="avg", groups="base.group_multi_currency") - nbr = fields.Integer(string='Line Count', readonly=True) # TDE FIXME master: rename into nbr_lines - invoice_id = fields.Many2one('account.invoice', readonly=True) + country_id = fields.Many2one('res.country', string="Partner Company's Country") + invoice_user_id = fields.Many2one('res.users', string='Salesperson', readonly=True) type = fields.Selection([ ('out_invoice', 'Customer Invoice'), ('in_invoice', 'Vendor Bill'), @@ -56,32 +29,42 @@ def _compute_amounts_in_user_currency(self): ], readonly=True) state = fields.Selection([ ('draft', 'Draft'), - ('open', 'Open'), - ('paid', 'Paid'), + ('posted', 'Open'), ('cancel', 'Cancelled') ], string='Invoice Status', readonly=True) - date_due = fields.Date(string='Due Date', readonly=True) - account_id = fields.Many2one('account.account', string='Receivable/Payable Account', readonly=True, domain=[('deprecated', '=', False)]) - account_line_id = fields.Many2one('account.account', string='Revenue/Expense Account', readonly=True, domain=[('deprecated', '=', False)]) - partner_bank_id = fields.Many2one('res.partner.bank', string='Bank Account', readonly=True) + invoice_payment_state = fields.Selection(selection=[ + ('not_paid', 'Not Paid'), + ('in_payment', 'In Payment'), + ('paid', 'paid') + ], string='Payment Status', readonly=True) + fiscal_position_id = fields.Many2one('account.fiscal.position', oldname='fiscal_position', string='Fiscal Position', readonly=True) + invoice_date = fields.Date(readonly=True, string="Invoice Date") + invoice_payment_term_id = fields.Many2one('account.payment.term', string='Payment Terms', oldname='payment_term', readonly=True) + invoice_partner_bank_id = fields.Many2one('res.partner.bank', string='Bank Account', readonly=True) + nbr_lines = fields.Integer(string='Line Count', readonly=True) residual = fields.Float(string='Due Amount', readonly=True) - user_currency_residual = fields.Float(string="Total Residual", compute='_compute_amounts_in_user_currency', digits=0) - country_id = fields.Many2one('res.country', string="Partner Company's Country") - account_analytic_id = fields.Many2one('account.analytic.account', string='Analytic Account', groups="analytic.group_analytic_accounting") amount_total = fields.Float(string='Total', readonly=True) - _order = 'date desc' + # ==== Invoice line fields ==== + quantity = fields.Float(string='Product Quantity', readonly=True) + product_id = fields.Many2one('product.product', string='Product', readonly=True) + product_uom_id = fields.Many2one('uom.uom', string='Unit of Measure', readonly=True) + product_categ_id = fields.Many2one('product.category', string='Product Category', readonly=True) + invoice_date_due = fields.Date(string='Due Date', readonly=True) + account_id = fields.Many2one('account.account', string='Revenue/Expense Account', readonly=True, domain=[('deprecated', '=', False)]) + analytic_account_id = fields.Many2one('account.analytic.account', string='Analytic Account', groups="analytic.group_analytic_accounting") + price_subtotal = fields.Float(string='Untaxed Total', readonly=True) + price_average = fields.Float(string='Average Price', readonly=True, group_operator="avg") _depends = { - 'account.invoice': [ - 'account_id', 'amount_total_company_signed', 'commercial_partner_id', 'company_id', - 'currency_id', 'date_due', 'date_invoice', 'fiscal_position_id', - 'journal_id', 'number', 'partner_bank_id', 'partner_id', 'payment_term_id', - 'residual', 'state', 'type', 'user_id', + 'account.move': [ + 'name', 'state', 'type', 'partner_id', 'invoice_user_id', 'fiscal_position_id', + 'invoice_date', 'invoice_date_due', 'invoice_payment_term_id', 'invoice_partner_bank_id', ], - 'account.invoice.line': [ - 'account_id', 'invoice_id', 'price_subtotal', 'product_id', - 'quantity', 'uom_id', 'account_analytic_id', + 'account.move.line': [ + 'quantity', 'price_subtotal', 'amount_residual', 'balance', 'amount_currency', + 'move_id', 'product_id', 'product_uom_id', 'account_id', 'analytic_account_id', + 'journal_id', 'company_id', 'currency_id', 'partner_id', ], 'product.product': ['product_tmpl_id'], 'product.template': ['categ_id'], @@ -90,97 +73,107 @@ def _compute_amounts_in_user_currency(self): 'res.partner': ['country_id'], } + @api.model def _select(self): - select_str = """ - SELECT sub.id, sub.number, sub.date, sub.product_id, sub.partner_id, sub.country_id, sub.account_analytic_id, - sub.payment_term_id, sub.uom_name, sub.currency_id, sub.journal_id, - sub.fiscal_position_id, sub.user_id, sub.company_id, sub.nbr, sub.invoice_id, sub.type, sub.state, - sub.categ_id, sub.date_due, sub.account_id, sub.account_line_id, sub.partner_bank_id, - sub.product_qty, sub.price_total as price_total, sub.price_average as price_average, sub.amount_total / COALESCE(cr.rate, 1) as amount_total, - COALESCE(cr.rate, 1) as currency_rate, sub.residual as residual, sub.commercial_partner_id as commercial_partner_id - """ - return select_str - - def _sub_select(self): - select_str = """ - SELECT ail.id AS id, - ai.date_invoice AS date, - ai.number as number, - ail.product_id, ai.partner_id, ai.payment_term_id, ail.account_analytic_id, - u2.name AS uom_name, - ai.currency_id, ai.journal_id, ai.fiscal_position_id, ai.user_id, ai.company_id, - 1 AS nbr, - ai.id AS invoice_id, ai.type, ai.state, pt.categ_id, ai.date_due, ai.account_id, ail.account_id AS account_line_id, - ai.partner_bank_id, - SUM ((invoice_type.sign_qty * ail.quantity) / COALESCE(u.factor,1) * COALESCE(u2.factor,1)) AS product_qty, - SUM(ail.price_subtotal_signed * invoice_type.sign) AS price_total, - SUM(ail.price_total * invoice_type.sign_qty) AS amount_total, - SUM(ABS(ail.price_subtotal_signed)) / CASE - WHEN SUM(ail.quantity / COALESCE(u.factor,1) * COALESCE(u2.factor,1)) <> 0::numeric - THEN SUM(ail.quantity / COALESCE(u.factor,1) * COALESCE(u2.factor,1)) - ELSE 1::numeric - END AS price_average, - ai.residual_company_signed / (SELECT count(*) FROM account_invoice_line l where invoice_id = ai.id) * - count(*) * invoice_type.sign AS residual, - ai.commercial_partner_id as commercial_partner_id, - coalesce(partner.country_id, partner_ai.country_id) AS country_id - """ - return select_str + return ''' + SELECT + line.id, + line.move_id, + line.product_id, + line.account_id, + line.analytic_account_id, + line.journal_id, + line.company_id, + line.currency_id, + line.partner_id AS commercial_partner_id, + move.name, + move.state, + move.type, + move.partner_id, + move.invoice_user_id, + move.fiscal_position_id, + move.invoice_payment_state, + move.invoice_date, + move.invoice_date_due, + move.invoice_payment_term_id, + move.invoice_partner_bank_id, + move.amount_residual_signed AS residual, + move.amount_total_signed AS amount_total, + uom_template.id AS product_uom_id, + template.categ_id AS product_categ_id, + SUM(line.quantity / NULLIF(COALESCE(uom_line.factor, 1) * COALESCE(uom_template.factor, 1), 0.0)) + AS quantity, + -SUM(line.balance) AS price_subtotal, + -SUM(line.balance / NULLIF(COALESCE(uom_line.factor, 1) * COALESCE(uom_template.factor, 1), 0.0)) + AS price_average, + COALESCE(partner.country_id, commercial_partner.country_id) AS country_id, + 1 AS nbr_lines + ''' + @api.model def _from(self): - from_str = """ - FROM account_invoice_line ail - JOIN account_invoice ai ON ai.id = ail.invoice_id - JOIN res_partner partner ON ai.commercial_partner_id = partner.id - JOIN res_partner partner_ai ON ai.partner_id = partner_ai.id - LEFT JOIN product_product pr ON pr.id = ail.product_id - left JOIN product_template pt ON pt.id = pr.product_tmpl_id - LEFT JOIN uom_uom u ON u.id = ail.uom_id - LEFT JOIN uom_uom u2 ON u2.id = pt.uom_id - JOIN ( - -- Temporary table to decide if the qty should be added or retrieved (Invoice vs Credit Note) - SELECT id,(CASE - WHEN ai.type::text = ANY (ARRAY['in_refund'::character varying::text, 'in_invoice'::character varying::text]) - THEN -1 - ELSE 1 - END) AS sign,(CASE - WHEN ai.type::text = ANY (ARRAY['out_refund'::character varying::text, 'in_invoice'::character varying::text]) - THEN -1 - ELSE 1 - END) AS sign_qty - FROM account_invoice ai - ) AS invoice_type ON invoice_type.id = ai.id - """ - return from_str + return ''' + FROM account_move_line line + LEFT JOIN res_partner partner ON partner.id = line.partner_id + LEFT JOIN product_product product ON product.id = line.product_id + LEFT JOIN account_account account ON account.id = line.account_id + LEFT JOIN account_account_type user_type ON user_type.id = account.user_type_id + LEFT JOIN product_template template ON template.id = product.product_tmpl_id + LEFT JOIN uom_uom uom_line ON uom_line.id = line.product_uom_id + LEFT JOIN uom_uom uom_template ON uom_template.id = template.uom_id + INNER JOIN account_move move ON move.id = line.move_id + LEFT JOIN res_partner commercial_partner ON commercial_partner.id = move.commercial_partner_id + ''' + + @api.model + def _where(self): + return ''' + WHERE move.type IN ('out_invoice', 'out_refund', 'in_invoice', 'in_refund', 'out_receipt', 'in_receipt') + AND line.account_id IS NOT NULL + AND NOT line.exclude_from_invoice_tab + ''' + @api.model def _group_by(self): - group_by_str = """ - GROUP BY ail.id, ail.product_id, ail.account_analytic_id, ai.date_invoice, ai.id, - ai.partner_id, ai.payment_term_id, u2.name, u2.id, ai.currency_id, ai.journal_id, - ai.fiscal_position_id, ai.user_id, ai.company_id, ai.id, ai.type, invoice_type.sign, ai.state, pt.categ_id, - ai.date_due, ai.account_id, ail.account_id, ai.partner_bank_id, ai.residual_company_signed, - ai.amount_total_company_signed, ai.commercial_partner_id, coalesce(partner.country_id, partner_ai.country_id) - """ - return group_by_str + return ''' + GROUP BY + line.id, + line.move_id, + line.product_id, + line.account_id, + line.analytic_account_id, + line.journal_id, + line.company_id, + line.currency_id, + line.partner_id, + move.name, + move.state, + move.type, + move.amount_residual_signed, + move.amount_total_signed, + move.partner_id, + move.invoice_user_id, + move.fiscal_position_id, + move.invoice_payment_state, + move.invoice_date, + move.invoice_date_due, + move.invoice_payment_term_id, + move.invoice_partner_bank_id, + uom_template.id, + template.categ_id, + COALESCE(partner.country_id, commercial_partner.country_id) + ''' @api.model_cr def init(self): - # self._table = account_invoice_report tools.drop_view_if_exists(self.env.cr, self._table) - self.env.cr.execute("""CREATE or REPLACE VIEW %s as ( - WITH currency_rate AS (%s) - %s - FROM ( - %s %s WHERE ail.account_id IS NOT NULL %s - ) AS sub - LEFT JOIN currency_rate cr ON - (cr.currency_id = sub.currency_id AND - cr.company_id = sub.company_id AND - cr.date_start <= COALESCE(sub.date, NOW()) AND - (cr.date_end IS NULL OR cr.date_end > COALESCE(sub.date, NOW()))) - )""" % ( - self._table, self.env['res.currency']._select_companies_rates(), - self._select(), self._sub_select(), self._from(), self._group_by())) + self.env.cr.execute(''' + CREATE OR REPLACE VIEW %s AS ( + %s %s %s %s + ) + ''' % ( + self._table, self._select(), self._from(), self._where(), self._group_by() + )) class ReportInvoiceWithPayment(models.AbstractModel): diff --git a/addons/account/report/account_invoice_report_view.xml b/addons/account/report/account_invoice_report_view.xml index fceb9ed526ed7..99f69cf9db0e7 100644 --- a/addons/account/report/account_invoice_report_view.xml +++ b/addons/account/report/account_invoice_report_view.xml @@ -6,9 +6,9 @@ account.invoice.report - - - + + +
@@ -18,8 +18,8 @@ account.invoice.report - - + + @@ -30,35 +30,35 @@ account.invoice.report [] - {'group_by': ['date:month', 'user_id']} + {'group_by': ['invoice_date:month', 'invoice_user_id']} By Product account.invoice.report [] - {'group_by': ['date:month', 'product_id'], 'set_visible':True, 'residual_invisible':True} + {'group_by': ['invoice_date:month', 'product_id'], 'set_visible':True, 'residual_invisible':True} By Product Category account.invoice.report [] - {'group_by': ['date:month', 'categ_id'], 'residual_invisible':True} + {'group_by': ['invoice_date:month', 'product_categ_id'], 'residual_invisible':True} By Credit Note account.invoice.report [('type', '=', 'out_refund')] - {'group_by': ['date:month', 'user_id']} + {'group_by': ['invoice_date:month', 'invoice_user_id']} By Country account.invoice.report [] - {'group_by': ['date:month', 'country_id']} + {'group_by': ['invoice_date:month', 'country_id']} @@ -66,9 +66,9 @@ account.invoice.report - + - + @@ -79,19 +79,19 @@ - + - + - + - + - + - - + + diff --git a/addons/account/security/account_security.xml b/addons/account/security/account_security.xml index 34f80bf9c6872..dce3f3da4f1e9 100644 --- a/addons/account/security/account_security.xml +++ b/addons/account/security/account_security.xml @@ -131,13 +131,6 @@ ['|',('company_id','=',False),('company_id', 'in', company_ids)] - - Invoice multi-company - - - ['|',('company_id','=',False),('company_id', 'in', company_ids)] - - Invoice Analysis multi-company @@ -152,13 +145,6 @@ ['|',('company_id','=',False),('company_id', 'in', company_ids)] - - Invoice Line company rule - - - ['|',('company_id','=',False),('company_id', 'in', company_ids)] - - Account bank statement company rule @@ -193,18 +179,18 @@ ['|',('company_id','=',False),('company_id', 'in', company_ids)] - + Portal Personal Account Invoices - - [('message_partner_ids','child_of',[user.commercial_partner_id.id])] + + [('type', 'in', ('out_invoice', 'out_refund', 'in_invoice', 'in_refund')), ('message_partner_ids','child_of',[user.commercial_partner_id.id])] Portal Invoice Lines - - [('invoice_id.message_partner_ids','child_of',[user.commercial_partner_id.id])] + + [('move_id.type', 'in', ('out_invoice', 'out_refund', 'in_invoice', 'in_refund')), ('move_id.message_partner_ids','child_of',[user.commercial_partner_id.id])] diff --git a/addons/account/security/ir.model.access.csv b/addons/account/security/ir.model.access.csv index 467a9af3fb5a2..1925e05545fd4 100644 --- a/addons/account/security/ir.model.access.csv +++ b/addons/account/security/ir.model.access.csv @@ -23,11 +23,10 @@ access_account_bank_statement,account.bank.statement,model_account_bank_statemen access_account_bank_statement_line,account.bank.statement.line,model_account_bank_statement_line,account.group_account_user,1,1,1,1 access_account_analytic_line_manager,account.analytic.line manager,model_account_analytic_line,account.group_account_manager,1,0,0,0 access_account_analytic_account,account.analytic.account,analytic.model_account_analytic_account,base.group_user,1,0,0,0 -access_account_invoice_uinvoice,account.invoice,model_account_invoice,account.group_account_invoice,1,1,1,1 -access_account_invoice_line_uinvoice,account.invoice.line,model_account_invoice_line,account.group_account_invoice,1,1,1,1 -access_account_invoice_tax_uinvoice,account.invoice.tax,model_account_invoice_tax,account.group_account_invoice,1,1,1,1 access_account_move_uinvoice,account.move,model_account_move,account.group_account_invoice,1,1,1,1 access_account_move_line_uinvoice,account.move.line invoice,model_account_move_line,account.group_account_invoice,1,1,1,1 +access_account_invoice_portal,account.move.portal,account.model_account_move,base.group_portal,1,0,0,0 +access_account_invoice_line_portal,account.move.line.portal,account.model_account_move_line,base.group_portal,1,0,0,0 access_account_payment_term_manager,account.payment.term,model_account_payment_term,account.group_account_manager,1,1,1,1 access_account_payment_term_line_manager,account.payment.term.line,model_account_payment_term_line,account.group_account_manager,1,1,1,1 access_account_cash_rounding_uinvoice,account.cash.rounding,model_account_cash_rounding,account.group_account_invoice,1,1,1,1 @@ -35,11 +34,8 @@ access_account_tax_manager,account.tax,model_account_tax,account.group_account_m access_account_journal_manager,account.journal,model_account_journal,account.group_account_manager,1,1,1,1 access_account_journal_user,account.journal,model_account_journal,account.group_account_user,1,0,0,0 access_account_journal_invoice,account.journal invoice,model_account_journal,account.group_account_invoice,1,0,0,0 -access_account_invoice_group_invoice,account.invoice group invoice,model_account_invoice,account.group_account_invoice,1,1,1,1 access_res_currency_account_manager,res.currency account manager,base.model_res_currency,group_account_manager,1,1,1,1 access_res_currency_rate_account_manager,res.currency.rate account manager,base.model_res_currency_rate,group_account_manager,1,1,1,1 -access_account_invoice_portal,account.invoice.portal,account.model_account_invoice,base.group_portal,1,0,0,0 -access_account_invoice_line_portal,account.invoice.line.portal,account.model_account_invoice_line,base.group_portal,1,0,0,0 access_account_payment_term_partner_manager,account.payment.term partner manager,model_account_payment_term,base.group_user,1,0,0,0 access_account_payment_term_line_partner_manager,account.payment.term.line partner manager,model_account_payment_term_line,base.group_user,1,0,0,0 access_account_fiscal_position_product_manager,account.fiscal.position account.manager,model_account_fiscal_position,account.group_account_manager,1,1,1,1 @@ -55,12 +51,9 @@ access_account_invoice_report_user,account.invoice.report_user,model_account_inv access_account_invoice_report,account.invoice.report,model_account_invoice_report,account.group_account_manager,1,1,1,1 access_account_invoice_report_billing,account.invoice.report_billing,model_account_invoice_report,account.group_account_invoice,1,0,0,0 access_res_partner_group_account_manager,res_partner group_account_manager,model_res_partner,account.group_account_manager,1,0,0,0 -access_account_invoice_accountant,account.invoice accountant,model_account_invoice,account.group_account_user,1,0,0,0 access_account_move_line_manager,account.move.line manager,model_account_move_line,account.group_account_manager,1,0,0,0 access_account_move_manager,account.move manager,model_account_move,account.group_account_manager,1,0,0,0 -access_account_invoice_tax_accountant,account.invoice.tax accountant,model_account_invoice_tax,account.group_account_user,1,0,0,0 access_account_analytic_line_invoice,account.analytic.line invoice,model_account_analytic_line,account.group_account_invoice,1,1,1,1 -access_account_invoice_line_accountant,account.invoice.line accountant,model_account_invoice_line,account.group_account_user,1,0,0,0 access_account_account_invoice,account.account invoice,model_account_account,account.group_account_invoice,1,0,0,0 access_account_analytic_accountant,account.analytic.account accountant,analytic.model_account_analytic_account,account.group_account_user,1,1,1,1 access_account_account_type_invoice,account.account.type invoice,model_account_account_type,account.group_account_invoice,1,0,0,0 diff --git a/addons/account/static/src/js/account_payment_field.js b/addons/account/static/src/js/account_payment_field.js index 2b8507de1979d..8cba2a2d035c7 100644 --- a/addons/account/static/src/js/account_payment_field.js +++ b/addons/account/static/src/js/account_payment_field.js @@ -69,7 +69,6 @@ var ShowPaymentLineWidget = AbstractField.extend({ move_id: content.move_id, ref: content.ref, account_payment_id: content.account_payment_id, - invoice_id: content.invoice_id, })); $content.filter('.js_unreconcile_payment').on('click', self._onRemoveMoveReconcile.bind(self)); $content.filter('.js_open_payment').on('click', self._onOpenPayment.bind(self)); @@ -96,15 +95,11 @@ var ShowPaymentLineWidget = AbstractField.extend({ * @param {MouseEvent} event */ _onOpenPayment: function (event) { - var invoiceId = parseInt($(event.target).attr('invoice-id')); var paymentId = parseInt($(event.target).attr('payment-id')); var moveId = parseInt($(event.target).attr('move-id')); var res_model; var id; - if (invoiceId !== undefined && !isNaN(invoiceId)){ - res_model = "account.invoice"; - id = invoiceId; - } else if (paymentId !== undefined && !isNaN(paymentId)){ + if (paymentId !== undefined && !isNaN(paymentId)){ res_model = "account.payment"; id = paymentId; } else if (moveId !== undefined && !isNaN(moveId)){ @@ -133,9 +128,9 @@ var ShowPaymentLineWidget = AbstractField.extend({ var self = this; var id = $(event.target).data('id') || false; this._rpc({ - model: 'account.invoice', - method: 'assign_outstanding_credit', - args: [JSON.parse(this.value).invoice_id, id], + model: 'account.move', + method: 'js_assign_outstanding_line', + args: [JSON.parse(this.value).move_id, id], }).then(function () { self.trigger_up('reload'); }); @@ -153,7 +148,7 @@ var ShowPaymentLineWidget = AbstractField.extend({ model: 'account.move.line', method: 'remove_move_reconcile', args: [paymentId], - context: {'invoice_id': this.res_id}, + context: {'move_id': this.res_id}, }).then(function () { self.trigger_up('reload'); }); diff --git a/addons/account/static/src/xml/account_payment.xml b/addons/account/static/src/xml/account_payment.xml index 55b327183fc81..408681ecde8db 100644 --- a/addons/account/static/src/xml/account_payment.xml +++ b/addons/account/static/src/xml/account_payment.xml @@ -79,7 +79,7 @@ - + diff --git a/addons/account/static/tests/account_payment_field_tests.js b/addons/account/static/tests/account_payment_field_tests.js index 18ae4300ad115..379f099d29c41 100644 --- a/addons/account/static/tests/account_payment_field_tests.js +++ b/addons/account/static/tests/account_payment_field_tests.js @@ -9,7 +9,7 @@ var createView = testUtils.createView; QUnit.module('account', { beforeEach: function () { this.data = { - 'account.invoice': { + 'account.move': { fields: { payments_widget: {string: "payments_widget data", type: "char"}, outstanding_credits_debits_widget: {string: "outstanding_credits_debits_widget data", type: "char"}, @@ -17,7 +17,7 @@ QUnit.module('account', { records: [{ id: 1, payments_widget: '{"content": [{"digits": [69, 2], "currency": "$", "amount": 555.0, "name": "Customer Payment: INV/2017/0004", "date": "2017-04-25", "position": "before", "ref": "BNK1/2017/0003 (INV/2017/0004)", "payment_id": 22, "move_id": 10, "journal_name": "Bank"}], "outstanding": false, "title": "Less Payment"}', - outstanding_credits_debits_widget: '{"content": [{"digits": [69, 2], "currency": "$", "amount": 100.0, "journal_name": "INV/2017/0004", "position": "before", "id": 20}], "invoice_id": 4, "outstanding": true, "title": "Outstanding credits"}', + outstanding_credits_debits_widget: '{"content": [{"digits": [69, 2], "currency": "$", "amount": 100.0, "journal_name": "INV/2017/0004", "position": "before", "id": 20}], "move_id": 4, "outstanding": true, "title": "Outstanding credits"}', }] }, }; @@ -30,7 +30,7 @@ QUnit.module('account', { var form = await createView({ View: FormView, - model: 'account.invoice', + model: 'account.move', data: this.data, arch: '
'+ ''+ @@ -40,11 +40,11 @@ QUnit.module('account', { mockRPC: function (route, args) { if (args.method === 'remove_move_reconcile') { assert.deepEqual(args.args, [22], "should call remove_move_reconcile {warning: required focus}"); - assert.deepEqual(args.kwargs, {context: {"invoice_id": 1}}, "should call remove_move_reconcile {warning: required focus}"); + assert.deepEqual(args.kwargs, {context: {"move_id": 1}}, "should call remove_move_reconcile {warning: required focus}"); return Promise.resolve(); } - if (args.method === 'assign_outstanding_credit') { - assert.deepEqual(args.args, [4, 20], "should call assign_outstanding_credit {warning: required focus}"); + if (args.method === 'js_assign_outstanding_line') { + assert.deepEqual(args.args, [4, 20], "should call js_assign_outstanding_line {warning: required focus}"); return Promise.resolve(); } return this._super.apply(this, arguments); diff --git a/addons/account/tests/__init__.py b/addons/account/tests/__init__.py index 0eb1d84e61342..c2612d5055100 100644 --- a/addons/account/tests/__init__.py +++ b/addons/account/tests/__init__.py @@ -1,23 +1,20 @@ -#Accounting tests written in python should extend the class AccountingTestCase. -#See its doc for more info. +# -*- coding: utf-8 -*- -from . import test_account_customer_invoice -from . import test_account_move_closed_period -from . import test_account_move_tax_lock_date -from . import test_account_supplier_invoice -from . import test_account_validate_account_move -from . import test_account_invoice_rounding +from . import test_account_move_out_invoice +from . import test_account_move_out_refund +from . import test_account_move_in_invoice +from . import test_account_move_in_refund +from . import test_account_move_entry +from . import test_account_invoice_report +from . import test_account_journal_dashboard from . import test_bank_statement_reconciliation from . import test_fiscal_position -from . import test_invoice_onchange from . import test_reconciliation_widget from . import test_payment -from . import test_product_id_change from . import test_reconciliation from . import test_search from . import test_tax from . import test_invoice_taxes -from . import test_account_move_taxes_edition from . import test_templates_consistency from . import test_account_fiscal_year from . import test_account_all_l10n diff --git a/addons/account/tests/account_test_savepoint.py b/addons/account/tests/account_test_savepoint.py new file mode 100644 index 0000000000000..7cea2de366d73 --- /dev/null +++ b/addons/account/tests/account_test_savepoint.py @@ -0,0 +1,206 @@ +# -*- coding: utf-8 -*- +from odoo import fields +from odoo.tests.common import Form, SavepointCase +from odoo.tests import tagged + + +@tagged('post_install', '-at_install') +class AccountingSavepointCase(SavepointCase): + + # ------------------------------------------------------------------------- + # DATA GENERATION + # ------------------------------------------------------------------------- + + @classmethod + def setUpClass(cls): + super(AccountingSavepointCase, cls).setUpClass() + + chart_template = cls.env.user.company_id.chart_template_id + if not chart_template: + chart_template = cls.env.ref('l10n_generic_coa.configurable_chart_template', raise_if_not_found=False) + if not chart_template: + cls.skipTest("Accounting Tests skipped because the user's company has no chart of accounts.") + + # Create user. + user = cls.env['res.users'].create({ + 'name': 'Because I am accountman!', + 'login': 'accountman', + 'groups_id': [(6, 0, cls.env.user.groups_id.ids)], + }) + user.partner_id.email = 'accountman@test.com' + + # Shadow the current environment/cursor with one having the report user. + # This is mandatory to test access rights. + cls.env = cls.env(user=user) + cls.cr = cls.env.cr + + cls.company_data = cls.setup_company_data('company_1_data') + cls.currency_data = cls.setup_multi_currency_data() + + @classmethod + def setup_company_data(cls, company_name, **kwargs): + ''' Create a new company having the name passed as parameter. + A chart of accounts will be installed to this company: the same as the current company one. + The current user will get access to this company. + + :param company_name: The name of the company. + :return: A dictionary will be returned containing all relevant accounting data for testing. + ''' + chart_template = cls.env.user.company_id.chart_template_id + company = cls.env['res.company'].create({ + 'name': company_name, + 'currency_id': cls.env.user.company_id.currency_id.id, + **kwargs, + }) + cls.env.user.company_ids |= company + cls.env.user.company_id = company + + chart_template = cls.env['account.chart.template'].browse(chart_template.id) + chart_template.load_for_current_company(15.0, 15.0) + + # The currency could be different after the installation of the chart template. + company.write({'currency_id': kwargs.get('currency_id', cls.env.user.company_id.currency_id.id)}) + + return { + 'company': company, + 'currency': company.currency_id, + 'default_account_revenue': cls.env['account.account'].search([ + ('company_id', '=', company.id), + ('user_type_id', '=', cls.env.ref('account.data_account_type_revenue').id) + ], limit=1), + 'default_account_expense': cls.env['account.account'].search([ + ('company_id', '=', company.id), + ('user_type_id', '=', cls.env.ref('account.data_account_type_expenses').id) + ], limit=1), + 'default_account_receivable': cls.env['account.account'].search([ + ('company_id', '=', company.id), + ('user_type_id.type', '=', 'receivable') + ], limit=1), + 'default_account_payable': cls.env['account.account'].search([ + ('company_id', '=', company.id), + ('user_type_id.type', '=', 'payable') + ], limit=1), + 'default_account_tax_sale': company.account_sale_tax_id.mapped('invoice_repartition_line_ids.account_id'), + 'default_account_tax_purchase': company.account_purchase_tax_id.mapped('invoice_repartition_line_ids.account_id'), + 'default_journal_misc': cls.env['account.journal'].search([ + ('company_id', '=', company.id), + ('type', '=', 'general') + ], limit=1), + 'default_journal_sale': cls.env['account.journal'].search([ + ('company_id', '=', company.id), + ('type', '=', 'sale') + ], limit=1), + 'default_journal_purchase': cls.env['account.journal'].search([ + ('company_id', '=', company.id), + ('type', '=', 'purchase') + ], limit=1), + 'default_tax_sale': company.account_sale_tax_id, + 'default_tax_purchase': company.account_purchase_tax_id, + } + + @classmethod + def setup_multi_currency_data(cls): + gold_currency = cls.env['res.currency'].create({ + 'name': 'Gold Coin', + 'symbol': '☺', + 'rounding': 0.001, + 'position': 'after', + 'currency_unit_label': 'Gold', + 'currency_subunit_label': 'Silver', + }) + rate1 = cls.env['res.currency.rate'].create({ + 'name': '2016-01-01', + 'rate': 3.0, + 'currency_id': gold_currency.id, + 'company_id': cls.env.company.id, + }) + rate2 = cls.env['res.currency.rate'].create({ + 'name': '2017-01-01', + 'rate': 2.0, + 'currency_id': gold_currency.id, + 'company_id': cls.env.company.id, + }) + return { + 'currency': gold_currency, + 'rates': rate1 + rate2, + } + + @classmethod + def setup_armageddon_tax(cls, tax_name, company_data): + return cls.env['account.tax'].create({ + 'name': '%s (group)' % tax_name, + 'amount_type': 'group', + 'amount': 0.0, + 'children_tax_ids': [ + (0, 0, { + 'name': '%s (child 1)' % tax_name, + 'amount_type': 'percent', + 'amount': 20.0, + 'price_include': True, + 'include_base_amount': True, + 'tax_exigibility': 'on_invoice', + 'invoice_repartition_line_ids': [ + (0, 0, { + 'factor_percent': 100, + 'repartition_type': 'base', + }), + (0, 0, { + 'factor_percent': 40, + 'repartition_type': 'tax', + 'account_id': company_data['default_account_tax_sale'].id, + }), + (0, 0, { + 'factor_percent': 60, + 'repartition_type': 'tax', + # /!\ No account set. + }), + ], + 'refund_repartition_line_ids': [ + (0, 0, { + 'factor_percent': 100, + 'repartition_type': 'base', + }), + (0, 0, { + 'factor_percent': 40, + 'repartition_type': 'tax', + 'account_id': company_data['default_account_tax_sale'].id, + }), + (0, 0, { + 'factor_percent': 60, + 'repartition_type': 'tax', + # /!\ No account set. + }), + ], + }), + (0, 0, { + 'name': '%s (child 2)' % tax_name, + 'amount_type': 'percent', + 'amount': 10.0, + 'tax_exigibility': 'on_payment', + 'cash_basis_transition_account_id': company_data['default_account_tax_sale'].copy().id, + 'invoice_repartition_line_ids': [ + (0, 0, { + 'factor_percent': 100, + 'repartition_type': 'base', + }), + (0, 0, { + 'factor_percent': 100, + 'repartition_type': 'tax', + 'account_id': company_data['default_account_tax_sale'].id, + }), + ], + 'refund_repartition_line_ids': [ + (0, 0, { + 'factor_percent': 100, + 'repartition_type': 'base', + }), + + (0, 0, { + 'factor_percent': 100, + 'repartition_type': 'tax', + 'account_id': company_data['default_account_tax_sale'].id, + }), + ], + }), + ], + }) diff --git a/addons/account/tests/invoice_test_common.py b/addons/account/tests/invoice_test_common.py new file mode 100644 index 0000000000000..1094cbdd20845 --- /dev/null +++ b/addons/account/tests/invoice_test_common.py @@ -0,0 +1,147 @@ +# -*- coding: utf-8 -*- +from odoo.addons.account.tests.account_test_savepoint import AccountingSavepointCase +from odoo.tests.common import Form +from odoo.tests import tagged +from odoo.exceptions import ValidationError +from odoo import fields + +import logging + +_logger = logging.getLogger(__name__) + + +@tagged('post_install', '-at_install') +class InvoiceTestCommon(AccountingSavepointCase): + + @classmethod + def setUpClass(cls): + super(InvoiceTestCommon, cls).setUpClass() + + # ==== Taxes ==== + cls.tax_sale_a = cls.company_data['default_tax_sale'] + cls.tax_sale_b = cls.company_data['default_tax_sale'].copy() + cls.tax_purchase_a = cls.company_data['default_tax_purchase'] + cls.tax_purchase_b = cls.company_data['default_tax_purchase'].copy() + cls.tax_armageddon = cls.setup_armageddon_tax('complex_tax', cls.company_data) + + # ==== Products ==== + cls.product_a = cls.env['product.product'].create({ + 'name': 'product_a', + 'uom_id': cls.env.ref('uom.product_uom_unit').id, + 'lst_price': 1000.0, + 'standard_price': 800.0, + 'property_account_income_id': cls.company_data['default_account_revenue'].id, + 'property_account_expense_id': cls.company_data['default_account_expense'].id, + 'taxes_id': [(6, 0, cls.tax_sale_a.ids)], + 'supplier_taxes_id': [(6, 0, cls.tax_purchase_a.ids)], + }) + cls.product_b = cls.env['product.product'].create({ + 'name': 'product_b', + 'uom_id': cls.env.ref('uom.product_uom_dozen').id, + 'lst_price': 200.0, + 'standard_price': 160.0, + 'property_account_income_id': cls.company_data['default_account_revenue'].copy().id, + 'property_account_expense_id': cls.company_data['default_account_expense'].copy().id, + 'taxes_id': [(6, 0, (cls.tax_sale_a + cls.tax_sale_b).ids)], + 'supplier_taxes_id': [(6, 0, (cls.tax_purchase_a + cls.tax_purchase_b).ids)], + }) + + # ==== Fiscal positions ==== + cls.fiscal_pos_a = cls.env['account.fiscal.position'].create({ + 'name': 'fiscal_pos_a', + 'tax_ids': [ + (0, None, { + 'tax_src_id': cls.tax_sale_a.id, + 'tax_dest_id': cls.tax_sale_b.id, + }), + (0, None, { + 'tax_src_id': cls.tax_purchase_a.id, + 'tax_dest_id': cls.tax_purchase_b.id, + }), + ], + 'account_ids': [ + (0, None, { + 'account_src_id': cls.product_a.property_account_income_id.id, + 'account_dest_id': cls.product_b.property_account_income_id.id, + }), + (0, None, { + 'account_src_id': cls.product_a.property_account_expense_id.id, + 'account_dest_id': cls.product_b.property_account_expense_id.id, + }), + ], + }) + + # ==== Payment terms ==== + cls.pay_terms_a = cls.env.ref('account.account_payment_term_immediate') + cls.pay_terms_b = cls.env['account.payment.term'].create({ + 'name': '30% Advance End of Following Month', + 'note': 'Payment terms: 30% Advance End of Following Month', + 'line_ids': [ + (0, 0, { + 'value': 'percent', + 'value_amount': 30.0, + 'sequence': 400, + 'days': 0, + 'option': 'day_after_invoice_date', + }), + (0, 0, { + 'value': 'balance', + 'value_amount': 0.0, + 'sequence': 500, + 'days': 31, + 'option': 'day_following_month', + }), + ], + }) + + # ==== Partners ==== + cls.partner_a = cls.env['res.partner'].create({ + 'name': 'partner_a', + 'property_payment_term_id': cls.pay_terms_a.id, + 'property_supplier_payment_term_id': cls.pay_terms_a.id, + 'property_account_receivable_id': cls.company_data['default_account_receivable'].id, + 'property_account_payable_id': cls.company_data['default_account_payable'].id, + 'company_id': False, + }) + cls.partner_b = cls.env['res.partner'].create({ + 'name': 'partner_b', + 'property_payment_term_id': cls.pay_terms_b.id, + 'property_supplier_payment_term_id': cls.pay_terms_b.id, + 'property_account_position_id': cls.fiscal_pos_a.id, + 'property_account_receivable_id': cls.company_data['default_account_receivable'].copy().id, + 'property_account_payable_id': cls.company_data['default_account_payable'].copy().id, + 'company_id': False, + }) + + # ==== Cash rounding ==== + cls.cash_rounding_a = cls.env['account.cash.rounding'].create({ + 'name': 'add_invoice_line', + 'rounding': 0.05, + 'strategy': 'add_invoice_line', + 'account_id': cls.company_data['default_account_revenue'].copy().id, + 'rounding_method': 'UP', + }) + cls.cash_rounding_b = cls.env['account.cash.rounding'].create({ + 'name': 'biggest_tax', + 'rounding': 0.05, + 'strategy': 'biggest_tax', + 'rounding_method': 'DOWN', + }) + + @classmethod + def init_invoice(cls, move_type): + move_form = Form(cls.env['account.move'].with_context(default_type=move_type)) + move_form.invoice_date = fields.Date.from_string('2019-01-01') + move_form.partner_id = cls.partner_a + with move_form.invoice_line_ids.new() as line_form: + line_form.product_id = cls.product_a + with move_form.invoice_line_ids.new() as line_form: + line_form.product_id = cls.product_b + return move_form.save() + + def assertInvoiceValues(self, move, expected_lines_values, expected_move_values): + def sort_lines(lines): + return lines.sorted(lambda line: (line.exclude_from_invoice_tab, not bool(line.tax_line_id), line.name or '', line.balance)) + self.assertRecordValues(sort_lines(move.line_ids), expected_lines_values) + self.assertRecordValues(sort_lines(move.invoice_line_ids), expected_lines_values[:len(move.invoice_line_ids)]) + self.assertRecordValues(move, [expected_move_values]) diff --git a/addons/account/tests/test_account_customer_invoice.py b/addons/account/tests/test_account_customer_invoice.py deleted file mode 100644 index 1a84c82cda26c..0000000000000 --- a/addons/account/tests/test_account_customer_invoice.py +++ /dev/null @@ -1,364 +0,0 @@ -from unittest.mock import patch - -from odoo.addons.account.tests.account_test_users import AccountTestUsers -import datetime -from odoo.tests import tagged - - -@tagged('post_install', '-at_install') -class TestAccountCustomerInvoice(AccountTestUsers): - - def test_customer_invoice(self): - # I will create bank detail with using manager access rights - # because account manager can only create bank details. - self.res_partner_bank_0 = self.env['res.partner.bank'].sudo(self.account_manager.id).create(dict( - acc_type='bank', - company_id=self.main_company.id, - partner_id=self.main_partner.id, - acc_number='123456789', - bank_id=self.main_bank.id, - )) - - # Test with that user which have rights to make Invoicing and payment and who is accountant. - # Create a customer invoice - self.account_invoice_obj = self.env['account.invoice'] - self.payment_term = self.env.ref('account.account_payment_term_advance') - self.journalrec = self.env['account.journal'].search([('type', '=', 'sale')])[0] - self.partner3 = self.env.ref('base.res_partner_3') - account_user_type = self.env.ref('account.data_account_type_receivable') - self.ova = self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_current_assets').id)], limit=1) - - #only adviser can create an account - self.account_rec1_id = self.account_model.sudo(self.account_manager.id).create(dict( - code="cust_acc", - name="customer account", - user_type_id=account_user_type.id, - reconcile=True, - )) - - invoice_line_data = [ - (0, 0, - { - 'product_id': self.env.ref('product.product_product_5').id, - 'quantity': 10.0, - 'account_id': self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_revenue').id)], limit=1).id, - 'name': 'product test 5', - 'price_unit': 100.00, - } - ) - ] - - self.account_invoice_customer0 = self.account_invoice_obj.sudo(self.account_user.id).create(dict( - name="Test Customer Invoice", - payment_term_id=self.payment_term.id, - journal_id=self.journalrec.id, - partner_id=self.partner3.id, - account_id=self.account_rec1_id.id, - invoice_line_ids=invoice_line_data - )) - - # I manually assign tax on invoice - invoice_tax_line = { - 'name': 'Test Tax for Customer Invoice', - 'manual': 1, - 'amount': 9050, - 'account_id': self.ova.id, - 'invoice_id': self.account_invoice_customer0.id, - } - tax = self.env['account.invoice.tax'].create(invoice_tax_line) - assert tax, "Tax has not been assigned correctly" - - total_before_confirm = self.partner3.total_invoiced - - # I check that Initially customer invoice is in the "Draft" state - self.assertEquals(self.account_invoice_customer0.state, 'draft') - - # I check that there is no move attached to the invoice - self.assertEquals(len(self.account_invoice_customer0.move_id), 0) - - # I validate invoice by creating on - self.account_invoice_customer0.action_invoice_open() - - # I check that the invoice state is "Open" - self.assertEquals(self.account_invoice_customer0.state, 'open') - - # I check that now there is a move attached to the invoice - assert self.account_invoice_customer0.move_id, "Move not created for open invoice" - - # I totally pay the Invoice - self.account_invoice_customer0.pay_and_reconcile(self.env['account.journal'].search([('type', '=', 'bank')], limit=1), 10050.0) - - # I verify that invoice is now in Paid state - assert (self.account_invoice_customer0.state == 'paid'), "Invoice is not in Paid state" - - self.partner3.invalidate_cache(ids=self.partner3.ids) - total_after_confirm = self.partner3.total_invoiced - self.assertEquals(total_after_confirm - total_before_confirm, self.account_invoice_customer0.amount_untaxed_signed) - - # I created a credit note Using Add Credit Note Button - invoice_refund_obj = self.env['account.invoice.refund'] - self.account_invoice_refund_0 = invoice_refund_obj.create(dict( - description='Credit Note for China Export', - date=datetime.date.today(), - filter_refund='refund' - )) - - # I clicked on Add Credit Note button. - self.account_invoice_refund_0.invoice_refund() - - def test_customer_invoice_tax(self): - - self.env.company.tax_calculation_rounding_method = 'round_globally' - - payment_term = self.env.ref('account.account_payment_term_advance') - journalrec = self.env['account.journal'].search([('type', '=', 'sale')])[0] - partner3 = self.env.ref('base.res_partner_3') - account_id = self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_revenue').id)], limit=1).id - - tax = self.env['account.tax'].create({ - 'name': 'Tax 15.0', - 'amount': 15.0, - 'amount_type': 'percent', - 'type_tax_use': 'sale', - }) - - invoice_line_data = [ - (0, 0, - { - 'product_id': self.env.ref('product.product_product_1').id, - 'quantity': 40.0, - 'account_id': account_id, - 'name': 'product test 1', - 'discount' : 10.00, - 'price_unit': 2.27, - 'invoice_line_tax_ids': [(6, 0, [tax.id])], - } - ), - (0, 0, - { - 'product_id': self.env.ref('product.product_product_2').id, - 'quantity': 21.0, - 'account_id': self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_revenue').id)], limit=1).id, - 'name': 'product test 2', - 'discount' : 10.00, - 'price_unit': 2.77, - 'invoice_line_tax_ids': [(6, 0, [tax.id])], - } - ), - (0, 0, - { - 'product_id': self.env.ref('product.product_product_3').id, - 'quantity': 21.0, - 'account_id': self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_revenue').id)], limit=1).id, - 'name': 'product test 3', - 'discount' : 10.00, - 'price_unit': 2.77, - 'invoice_line_tax_ids': [(6, 0, [tax.id])], - } - ) - ] - - invoice = self.env['account.invoice'].create(dict( - name="Test Customer Invoice", - payment_term_id=payment_term.id, - journal_id=journalrec.id, - partner_id=partner3.id, - invoice_line_ids=invoice_line_data - )) - - self.assertAlmostEquals(invoice.amount_untaxed, sum([x.base for x in invoice.tax_line_ids])) - - def test_customer_invoice_tax_refund(self): - company = self.env.company - tax_account = self.env['account.account'].create({ - 'name': 'TAX', - 'code': 'TAX', - 'user_type_id': self.env.ref('account.data_account_type_current_assets').id, - 'company_id': company.id, - }) - - tax_refund_account = self.env['account.account'].create({ - 'name': 'TAX_REFUND', - 'code': 'TAX_R', - 'user_type_id': self.env.ref('account.data_account_type_current_assets').id, - 'company_id': company.id, - }) - - journalrec = self.env['account.journal'].search([('type', '=', 'sale')])[0] - partner3 = self.env.ref('base.res_partner_3') - account_id = self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_revenue').id)], limit=1).id - - tax = self.env['account.tax'].create({ - 'name': 'Tax 15.0', - 'amount': 15.0, - 'amount_type': 'percent', - 'type_tax_use': 'sale', - 'invoice_repartition_line_ids': [ - (0,0, { - 'factor_percent': 100, - 'repartition_type': 'base', - }), - - (0,0, { - 'factor_percent': 100, - 'repartition_type': 'tax', - 'account_id': tax_account.id, - }), - - ], - 'refund_repartition_line_ids': [ - (0,0, { - 'factor_percent': 100, - 'repartition_type': 'base', - }), - - (0,0, { - 'factor_percent': 100, - 'repartition_type': 'tax', - 'account_id': tax_refund_account.id, - }), - - ], - }) - - invoice_line_data = [ - (0, 0, - { - 'product_id': self.env.ref('product.product_product_1').id, - 'quantity': 40.0, - 'account_id': account_id, - 'name': 'product test 1', - 'discount': 10.00, - 'price_unit': 2.27, - 'invoice_line_tax_ids': [(6, 0, [tax.id])], - } - )] - - invoice = self.env['account.invoice'].create(dict( - name="Test Customer Invoice", - journal_id=journalrec.id, - partner_id=partner3.id, - invoice_line_ids=invoice_line_data - )) - - invoice.action_invoice_open() - - refund = invoice.refund() - self.assertEqual(invoice.tax_line_ids.mapped('account_id'), tax_account) - self.assertEqual(refund.tax_line_ids.mapped('account_id'), tax_refund_account) - - def test_customer_invoice_dashboard(self): - def patched_today(*args, **kwargs): - return '2019-01-22' - - date_invoice = '2019-01-21' - partner3 = self.env.ref('base.res_partner_3') - account_id = self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_revenue').id)], limit=1).id - - journal = self.env['account.journal'].create({ - 'name': 'sale_0', - 'code': 'SALE0', - 'type': 'sale', - }) - - invoice_line_data = [ - (0, 0, - { - 'product_id': self.env.ref('product.product_product_1').id, - 'quantity': 40.0, - 'account_id': account_id, - 'name': 'product test 1', - 'discount': 10.00, - 'price_unit': 2.27, - } - ) - ] - - invoice = self.env['account.invoice'].create(dict( - name="Test Customer Invoice", - journal_id=journal.id, - partner_id=partner3.id, - invoice_line_ids=invoice_line_data, - date_invoice=date_invoice, - )) - - refund_line_data = [ - (0, 0, - { - 'product_id': self.env.ref('product.product_product_1').id, - 'quantity': 1.0, - 'account_id': account_id, - 'name': 'product test 1', - 'price_unit': 13.3, - } - )] - - refund = self.env['account.invoice'].create(dict( - name="Test Customer Refund", - type='out_refund', - journal_id=journal.id, - partner_id=partner3.id, - invoice_line_ids=refund_line_data, - date_invoice=date_invoice, - )) - - # Check Draft - dashboard_data = journal.get_journal_dashboard_datas() - - self.assertEquals(dashboard_data['number_draft'], 2) - self.assertIn('68.42', dashboard_data['sum_draft']) - - self.assertEquals(dashboard_data['number_waiting'], 0) - self.assertIn('0.00', dashboard_data['sum_waiting']) - - # Check Both - invoice.action_invoice_open() - - dashboard_data = journal.get_journal_dashboard_datas() - self.assertEquals(dashboard_data['number_draft'], 1) - self.assertIn('-13.30', dashboard_data['sum_draft']) - - self.assertEquals(dashboard_data['number_waiting'], 1) - self.assertIn('81.72', dashboard_data['sum_waiting']) - - # Check waiting payment - refund.action_invoice_open() - - dashboard_data = journal.get_journal_dashboard_datas() - self.assertEquals(dashboard_data['number_draft'], 0) - self.assertIn('0.00', dashboard_data['sum_draft']) - - self.assertEquals(dashboard_data['number_waiting'], 2) - self.assertIn('68.42', dashboard_data['sum_waiting']) - - # Check partial - receivable_account = refund.move_id.line_ids.mapped('account_id').filtered(lambda a: a.internal_type == 'receivable') - payment_move = self.env['account.move'].create({ - 'journal_id': journal.id, - }) - payment_move_line = self.env['account.move.line'].with_context(check_move_validity=False).create({ - 'move_id': payment_move.id, - 'account_id': receivable_account.id, - 'debit': 10.00, - }) - self.env['account.move.line'].with_context(check_move_validity=False).create({ - 'move_id': payment_move.id, - 'account_id': self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_liquidity').id)], limit=1).id, - 'credit': 10.00, - }) - - payment_move.post() - - refund.register_payment(payment_move_line) - - dashboard_data = journal.get_journal_dashboard_datas() - self.assertEquals(dashboard_data['number_draft'], 0) - self.assertIn('0.00', dashboard_data['sum_draft']) - - self.assertEquals(dashboard_data['number_waiting'], 2) - self.assertIn('78.42', dashboard_data['sum_waiting']) - - with patch('odoo.fields.Date.today', patched_today): - dashboard_data = journal.get_journal_dashboard_datas() - self.assertEquals(dashboard_data['number_late'], 2) - self.assertIn('78.42', dashboard_data['sum_late']) diff --git a/addons/account/tests/test_account_invoice_report.py b/addons/account/tests/test_account_invoice_report.py new file mode 100644 index 0000000000000..d54f4e944d853 --- /dev/null +++ b/addons/account/tests/test_account_invoice_report.py @@ -0,0 +1,123 @@ +# -*- coding: utf-8 -*- +from odoo.addons.account.tests.invoice_test_common import InvoiceTestCommon +from odoo.tests.common import Form +from odoo.tests import tagged +from odoo import fields + +from unittest.mock import patch + + +@tagged('post_install', '-at_install') +class TestAccountInvoiceReport(InvoiceTestCommon): + + @classmethod + def setUpClass(cls): + super(TestAccountInvoiceReport, cls).setUpClass() + + cls.invoices = cls.env['account.move'].create([ + { + 'type': 'out_invoice', + 'partner_id': cls.partner_a.id, + 'invoice_date': fields.Date.from_string('2016-01-01'), + 'currency_id': cls.currency_data['currency'].id, + 'invoice_line_ids': [ + (0, None, { + 'product_id': cls.product_a.id, + 'quantity': 3, + 'price_unit': 1000, + }), + (0, None, { + 'product_id': cls.product_a.id, + 'quantity': 1, + 'price_unit': 3000, + }), + ] + }, + { + 'type': 'out_receipt', + 'invoice_date': fields.Date.from_string('2016-01-01'), + 'currency_id': cls.currency_data['currency'].id, + 'invoice_line_ids': [ + (0, None, { + 'product_id': cls.product_a.id, + 'quantity': 1, + 'price_unit': 6000, + }), + ] + }, + { + 'type': 'out_refund', + 'partner_id': cls.partner_a.id, + 'invoice_date': fields.Date.from_string('2017-01-01'), + 'currency_id': cls.currency_data['currency'].id, + 'invoice_line_ids': [ + (0, None, { + 'product_id': cls.product_a.id, + 'quantity': 1, + 'price_unit': 1200, + }), + ] + }, + { + 'type': 'in_invoice', + 'partner_id': cls.partner_a.id, + 'invoice_date': fields.Date.from_string('2016-01-01'), + 'currency_id': cls.currency_data['currency'].id, + 'invoice_line_ids': [ + (0, None, { + 'product_id': cls.product_a.id, + 'quantity': 1, + 'price_unit': 60, + }), + ] + }, + { + 'type': 'in_receipt', + 'partner_id': cls.partner_a.id, + 'invoice_date': fields.Date.from_string('2016-01-01'), + 'currency_id': cls.currency_data['currency'].id, + 'invoice_line_ids': [ + (0, None, { + 'product_id': cls.product_a.id, + 'quantity': 1, + 'price_unit': 60, + }), + ] + }, + { + 'type': 'in_refund', + 'partner_id': cls.partner_a.id, + 'invoice_date': fields.Date.from_string('2017-01-01'), + 'currency_id': cls.currency_data['currency'].id, + 'invoice_line_ids': [ + (0, None, { + 'product_id': cls.product_a.id, + 'quantity': 1, + 'price_unit': 12, + }), + ] + }, + ]) + + def assertInvoiceReportValues(self, expected_values_list): + reports = self.env['account.invoice.report'].search([('company_id', '=', self.company_data['company'].id)], order='price_subtotal DESC') + expected_values_dict = [{ + 'amount_total': vals[0], + 'price_average': vals[1], + 'price_subtotal': vals[2], + 'residual': vals[3], + 'quantity': vals[4], + } for vals in expected_values_list] + self.assertRecordValues(reports, expected_values_dict) + + def test_invoice_report_multiple_types(self): + self.assertInvoiceReportValues([ + # amount_total price_average price_subtotal residual quantity + [2000, 2000, 2000, 2000, 1], + [2000, 1000, 1000, 2000, 1], + [2000, 1000, 1000, 2000, 3], + [6, 6, 6, 6, 1], + [-20, -20, -20, -20, 1], + [-20, -20, -20, -20, 1], + [-600, -600, -600, -600, 1], + ]) diff --git a/addons/account/tests/test_account_invoice_rounding.py b/addons/account/tests/test_account_invoice_rounding.py deleted file mode 100644 index dcb3752f6bc9e..0000000000000 --- a/addons/account/tests/test_account_invoice_rounding.py +++ /dev/null @@ -1,108 +0,0 @@ -# -*- coding: utf-8 -*- - -from odoo.addons.account.tests.account_test_classes import AccountingTestCase -from odoo.exceptions import ValidationError -from odoo.tests import tagged - -import time - - -@tagged('post_install', '-at_install') -class TestAccountInvoiceRounding(AccountingTestCase): - - def setUp(self): - super(TestAccountInvoiceRounding, self).setUp() - self.account_receivable = self.env['account.account'].search( - [('user_type_id', '=', self.env.ref('account.data_account_type_receivable').id)], limit=1) - self.account_revenue = self.env['account.account'].search( - [('user_type_id', '=', self.env.ref('account.data_account_type_revenue').id)], limit=1) - self.fixed_tax = self.env['account.tax'].create({ - 'name': 'Test Tax', - 'amount': 0.0, - 'amount_type': 'fixed', - }) - - def create_cash_rounding(self, rounding, method, strategy): - return self.env['account.cash.rounding'].create({ - 'name': 'rounding ' + method, - 'rounding': rounding, - 'account_id': self.account_receivable.id, - 'strategy': strategy, - 'rounding_method': method, - }) - - def create_invoice(self, amount, cash_rounding_id, tax_amount=None): - """ Returns an open invoice """ - invoice_id = self.env['account.invoice'].create({ - 'partner_id': self.env.ref("base.res_partner_2").id, - 'currency_id': self.env.ref('base.USD').id, - 'name': 'invoice test rounding', - 'account_id': self.account_receivable.id, - 'type': 'out_invoice', - 'date_invoice': time.strftime('%Y') + '-06-26', - }) - if tax_amount: - self.fixed_tax.amount = tax_amount - self.env['account.invoice.line'].create({ - 'product_id': self.env.ref("product.product_product_4").id, - 'quantity': 1, - 'price_unit': amount, - 'invoice_id': invoice_id.id, - 'name': 'something', - 'account_id': self.account_revenue.id, - 'invoice_line_tax_ids': [(6, 0, [self.fixed_tax.id])] if tax_amount else None - }) - # Create the tax_line_ids - invoice_id._onchange_invoice_line_ids() - - # We need to set the cash_rounding_id after the _onchange_invoice_line_ids - # to avoid a ValidationError from _check_cash_rounding because the onchange - # are not well triggered in the tests. - try: - invoice_id.cash_rounding_id = cash_rounding_id - except ValidationError: - pass - - invoice_id._onchange_cash_rounding() - invoice_id.action_invoice_open() - return invoice_id - - def _check_invoice_rounding(self, inv, exp_lines_values, exp_tax_values=None): - inv_lines = inv.invoice_line_ids - self.assertEquals(len(inv_lines), len(exp_lines_values)) - for i in range(0, len(exp_lines_values)): - self.assertEquals(inv_lines[i].price_unit, exp_lines_values[i]) - - if exp_tax_values: - tax_lines = inv.tax_line_ids - self.assertEquals(len(tax_lines), len(exp_tax_values)) - for i in range(0, len(exp_tax_values)): - self.assertEquals(tax_lines[i].amount_total, exp_tax_values[i]) - - def test_rounding_add_invoice_line(self): - self._check_invoice_rounding( - self.create_invoice(100.2, self.create_cash_rounding(0.5, 'UP', 'add_invoice_line')), - [100.2, 0.3] - ) - self._check_invoice_rounding( - self.create_invoice(100.9, self.create_cash_rounding(1.0, 'DOWN', 'add_invoice_line')), - [100.9, -0.9] - ) - self._check_invoice_rounding( - self.create_invoice(100.5, self.create_cash_rounding(1.0, 'HALF-UP', 'add_invoice_line')), - [100.5, 0.5] - ) - - def test_rounding_biggest_tax(self): - self._check_invoice_rounding( - self.create_invoice(100.2, self.create_cash_rounding(0.5, 'UP', 'biggest_tax'), 1.0), - [100.2], [1.3] - ) - self._check_invoice_rounding( - self.create_invoice(100.9, self.create_cash_rounding(1.0, 'DOWN', 'biggest_tax'), 2.0), - [100.9], [1.1] - ) - self._check_invoice_rounding( - self.create_invoice(100.5, self.create_cash_rounding(1.0, 'HALF-UP', 'biggest_tax'), 1.0), - [100.5], [1.5] - ) diff --git a/addons/account/tests/test_account_journal_dashboard.py b/addons/account/tests/test_account_journal_dashboard.py new file mode 100644 index 0000000000000..ef7f8fad3a1c0 --- /dev/null +++ b/addons/account/tests/test_account_journal_dashboard.py @@ -0,0 +1,108 @@ +from unittest.mock import patch + +from odoo.addons.account.tests.account_test_users import AccountTestUsers +from odoo.tests import tagged + + +@tagged('post_install', '-at_install') +class TestAccountJournalDashboard(AccountTestUsers): + def test_customer_invoice_dashboard(self): + def patched_today(*args, **kwargs): + return '2019-01-22' + + date_invoice = '2019-01-21' + + journal = self.env['account.journal'].create({ + 'name': 'sale_0', + 'code': 'SALE0', + 'type': 'sale', + }) + + invoice = self.env['account.move'].create({ + 'type': 'out_invoice', + 'journal_id': journal.id, + 'partner_id': self.env.ref('base.res_partner_3').id, + 'invoice_date': date_invoice, + 'date': date_invoice, + 'invoice_line_ids': [(0, 0, { + 'product_id': self.env.ref('product.product_product_1').id, + 'quantity': 40.0, + 'name': 'product test 1', + 'discount': 10.00, + 'price_unit': 2.27, + })] + }) + refund = self.env['account.move'].create({ + 'type': 'out_refund', + 'journal_id': journal.id, + 'partner_id': self.env.ref('base.res_partner_3').id, + 'invoice_date': '2019-01-21', + 'date': date_invoice, + 'invoice_line_ids': [(0, 0, { + 'product_id': self.env.ref('product.product_product_1').id, + 'quantity': 1.0, + 'name': 'product test 1', + 'price_unit': 13.3, + })] + }) + + # Check Draft + dashboard_data = journal.get_journal_dashboard_datas() + + self.assertEquals(dashboard_data['number_draft'], 2) + self.assertIn('68.42', dashboard_data['sum_draft']) + + self.assertEquals(dashboard_data['number_waiting'], 0) + self.assertIn('0.00', dashboard_data['sum_waiting']) + + # Check Both + invoice.post() + + dashboard_data = journal.get_journal_dashboard_datas() + self.assertEquals(dashboard_data['number_draft'], 1) + self.assertIn('-13.30', dashboard_data['sum_draft']) + + self.assertEquals(dashboard_data['number_waiting'], 1) + self.assertIn('81.72', dashboard_data['sum_waiting']) + + # Check waiting payment + refund.post() + + dashboard_data = journal.get_journal_dashboard_datas() + self.assertEquals(dashboard_data['number_draft'], 0) + self.assertIn('0.00', dashboard_data['sum_draft']) + + self.assertEquals(dashboard_data['number_waiting'], 2) + self.assertIn('68.42', dashboard_data['sum_waiting']) + + # Check partial + receivable_account = refund.line_ids.mapped('account_id').filtered(lambda a: a.internal_type == 'receivable') + payment_move = self.env['account.move'].create({ + 'journal_id': journal.id, + }) + payment_move_line = self.env['account.move.line'].with_context(check_move_validity=False).create({ + 'move_id': payment_move.id, + 'account_id': receivable_account.id, + 'debit': 10.00, + }) + self.env['account.move.line'].with_context(check_move_validity=False).create({ + 'move_id': payment_move.id, + 'account_id': self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_liquidity').id)], limit=1).id, + 'credit': 10.00, + }) + + payment_move.post() + + refund.js_assign_outstanding_line(payment_move_line.id) + + dashboard_data = journal.get_journal_dashboard_datas() + self.assertEquals(dashboard_data['number_draft'], 0) + self.assertIn('0.00', dashboard_data['sum_draft']) + + self.assertEquals(dashboard_data['number_waiting'], 2) + self.assertIn('78.42', dashboard_data['sum_waiting']) + + with patch('odoo.fields.Date.today', patched_today): + dashboard_data = journal.get_journal_dashboard_datas() + self.assertEquals(dashboard_data['number_late'], 2) + self.assertIn('78.42', dashboard_data['sum_late']) diff --git a/addons/account/tests/test_account_move_closed_period.py b/addons/account/tests/test_account_move_closed_period.py deleted file mode 100644 index 5de3e97ac4fe6..0000000000000 --- a/addons/account/tests/test_account_move_closed_period.py +++ /dev/null @@ -1,48 +0,0 @@ -from odoo.addons.account.tests.account_test_classes import AccountingTestCase -from odoo.osv.orm import except_orm -from datetime import datetime -from dateutil.relativedelta import relativedelta -from calendar import monthrange -from odoo.tools import DEFAULT_SERVER_DATE_FORMAT -from odoo.tests import tagged - - -@tagged('post_install', '-at_install') -class TestPeriodState(AccountingTestCase): - """ - Forbid creation of Journal Entries for a closed period. - """ - - def setUp(self): - super(TestPeriodState, self).setUp() - self.user_id = self.env.user - - last_day_month = datetime.now() - relativedelta(months=1) - last_day_month = last_day_month.replace(day=monthrange(last_day_month.year, last_day_month.month)[1]) - self.last_day_month_str = last_day_month.strftime(DEFAULT_SERVER_DATE_FORMAT) - - #make sure there is no unposted entry - draft_entries = self.env['account.move'].search([('date', '<=', self.last_day_month_str), ('state', '=', 'draft')]) - if draft_entries: - draft_entries.post() - self.user_id.company_id.fiscalyear_lock_date = self.last_day_month_str - self.sale_journal_id = self.env['account.journal'].search([('type', '=', 'sale')])[0] - self.account_id = self.env['account.account'].search([('internal_type', '=', 'receivable')])[0] - - def test_period_state(self): - with self.assertRaises(except_orm): - move = self.env['account.move'].create({ - 'name': '/', - 'journal_id': self.sale_journal_id.id, - 'date': self.last_day_month_str, - 'line_ids': [(0, 0, { - 'name': 'foo', - 'debit': 10, - 'account_id': self.account_id.id, - }), (0, 0, { - 'name': 'bar', - 'credit': 10, - 'account_id': self.account_id.id, - })] - }) - move.post() diff --git a/addons/account/tests/test_account_move_entry.py b/addons/account/tests/test_account_move_entry.py new file mode 100644 index 0000000000000..e831ab5487899 --- /dev/null +++ b/addons/account/tests/test_account_move_entry.py @@ -0,0 +1,112 @@ +# -*- coding: utf-8 -*- +from odoo.addons.account.tests.invoice_test_common import InvoiceTestCommon +from odoo.tests import tagged +from odoo import fields +from odoo.exceptions import ValidationError, UserError + + +@tagged('post_install', '-at_install') +class TestAccountMove(InvoiceTestCommon): + + @classmethod + def setUpClass(cls): + super(TestAccountMove, cls).setUpClass() + + tax_repartition_line = cls.company_data['default_tax_sale'].invoice_repartition_line_ids\ + .filtered(lambda line: line.repartition_type == 'tax') + cls.test_move = cls.env['account.move'].create({ + 'type': 'entry', + 'date': fields.Date.from_string('2016-01-01'), + 'line_ids': [ + (0, None, { + 'name': 'revenue line 1', + 'account_id': cls.company_data['default_account_revenue'].id, + 'debit': 500.0, + 'credit': 0.0, + }), + (0, None, { + 'name': 'revenue line 2', + 'account_id': cls.company_data['default_account_revenue'].id, + 'debit': 1000.0, + 'credit': 0.0, + 'tax_ids': [(6, 0, cls.company_data['default_tax_sale'].ids)], + }), + (0, None, { + 'name': 'tax line', + 'account_id': cls.company_data['default_account_tax_sale'].id, + 'debit': 150.0, + 'credit': 0.0, + 'tax_repartition_line_id': tax_repartition_line.id, + }), + (0, None, { + 'name': 'counterpart line', + 'account_id': cls.company_data['default_account_expense'].id, + 'debit': 0.0, + 'credit': 1650.0, + }), + ] + }) + + def test_misc_fiscalyear_lock_date_1(self): + with self.assertRaises((ValidationError, UserError)): + self.test_move.company_id.fiscalyear_lock_date = fields.Date.from_string('2017-01-01') + + self.cr.execute('''UPDATE res_company SET fiscalyear_lock_date = '2017-01-01' WHERE id = %s''', self.test_move.company_id.ids) + + with self.assertRaises((ValidationError, UserError)): + self.test_move.post() + + with self.assertRaises(UserError): + self.env['account.move'].create(self.test_move.copy_data()) + + def test_misc_tax_lock_date_1(self): + # Set the tax lock date after the journal entry date. + self.test_move.company_id.tax_lock_date = fields.Date.from_string('2017-01-01') + + # lines[0] = 'counterpart line' + # lines[1] = 'tax line' + # lines[2] = 'revenue line 1' + # lines[3] = 'revenue line 2' + lines = self.test_move.line_ids.sorted('debit') + + # Writing not affecting a tax is allowed. + self.test_move.write({ + 'line_ids': [ + (1, lines[0].id, {'credit': 1750.0}), # counterpart line + (1, lines[2].id, {'debit': 600.0}), # revenue line 1 + ], + }) + + self.cr.execute('SAVEPOINT test_misc_tax_lock_date_1') + + # Writing something affecting a tax is not allowed. + with self.assertRaises(ValidationError): + self.test_move.write({ + 'line_ids': [ + (1, lines[0].id, {'credit': 2750.0}), + (1, lines[3].id, {'debit': 2000.0}), + ], + }) + + with self.assertRaises(ValidationError): + self.test_move.write({ + 'line_ids': [ + (1, lines[3].id, {'tax_ids': [(6, 0, self.company_data['default_tax_purchase'].ids)]}), + ], + }) + + with self.assertRaises(ValidationError): + self.test_move.write({ + 'line_ids': [ + (1, lines[0].id, {'credit': 1900.0}), + (1, lines[1].id, {'debit': 300.0}), + ], + }) + + with self.assertRaises(ValidationError): + self.test_move.unlink() + + self.cr.execute('ROLLBACK TO SAVEPOINT test_misc_tax_lock_date_1') + + with self.assertRaises(UserError): + self.test_move.post() diff --git a/addons/account/tests/test_account_move_in_invoice.py b/addons/account/tests/test_account_move_in_invoice.py new file mode 100644 index 0000000000000..b14c1206f4b85 --- /dev/null +++ b/addons/account/tests/test_account_move_in_invoice.py @@ -0,0 +1,1043 @@ +# -*- coding: utf-8 -*- +from odoo.addons.account.tests.invoice_test_common import InvoiceTestCommon +from odoo.tests.common import Form +from odoo.tests import tagged +from odoo import fields + + +@tagged('post_install', '-at_install') +class TestAccountMoveInInvoiceOnchanges(InvoiceTestCommon): + + @classmethod + def setUpClass(cls): + super(TestAccountMoveInInvoiceOnchanges, cls).setUpClass() + + cls.invoice = cls.init_invoice('in_invoice') + + cls.product_line_vals_1 = { + 'name': cls.product_a.name, + 'product_id': cls.product_a.id, + 'account_id': cls.product_a.property_account_expense_id.id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': cls.product_a.uom_id.id, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 800.0, + 'price_subtotal': 800.0, + 'price_total': 920.0, + 'tax_ids': cls.product_a.supplier_taxes_id.ids, + 'tax_line_id': False, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 800.0, + 'credit': 0.0, + 'date_maturity': False, + 'tax_exigible': True, + } + cls.product_line_vals_2 = { + 'name': cls.product_b.name, + 'product_id': cls.product_b.id, + 'account_id': cls.product_b.property_account_expense_id.id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': cls.product_b.uom_id.id, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 160.0, + 'price_subtotal': 160.0, + 'price_total': 208.0, + 'tax_ids': cls.product_b.supplier_taxes_id.ids, + 'tax_line_id': False, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 160.0, + 'credit': 0.0, + 'date_maturity': False, + 'tax_exigible': True, + } + cls.tax_line_vals_1 = { + 'name': cls.tax_purchase_a.name, + 'product_id': False, + 'account_id': cls.company_data['default_account_tax_purchase'].id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 144.0, + 'price_subtotal': 144.0, + 'price_total': 144.0, + 'tax_ids': [], + 'tax_line_id': cls.tax_purchase_a.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 144.0, + 'credit': 0.0, + 'date_maturity': False, + 'tax_exigible': True, + } + cls.tax_line_vals_2 = { + 'name': cls.tax_purchase_b.name, + 'product_id': False, + 'account_id': cls.company_data['default_account_tax_purchase'].id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 24.0, + 'price_subtotal': 24.0, + 'price_total': 24.0, + 'tax_ids': [], + 'tax_line_id': cls.tax_purchase_b.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 24.0, + 'credit': 0.0, + 'date_maturity': False, + 'tax_exigible': True, + } + cls.term_line_vals_1 = { + 'name': '', + 'product_id': False, + 'account_id': cls.company_data['default_account_payable'].id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': -1128.0, + 'price_subtotal': -1128.0, + 'price_total': -1128.0, + 'tax_ids': [], + 'tax_line_id': False, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 1128.0, + 'date_maturity': fields.Date.from_string('2019-01-01'), + 'tax_exigible': True, + } + cls.move_vals = { + 'partner_id': cls.partner_a.id, + 'currency_id': cls.company_data['currency'].id, + 'journal_id': cls.company_data['default_journal_purchase'].id, + 'date': fields.Date.from_string('2019-01-01'), + 'fiscal_position_id': False, + 'invoice_payment_ref': '', + 'invoice_payment_term_id': cls.pay_terms_a.id, + 'amount_untaxed': 960.0, + 'amount_tax': 168.0, + 'amount_total': 1128.0, + } + + def setUp(self): + super(TestAccountMoveInInvoiceOnchanges, self).setUp() + self.assertInvoiceValues(self.invoice, [ + self.product_line_vals_1, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + self.term_line_vals_1, + ], self.move_vals) + + def test_in_invoice_line_onchange_product_1(self): + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + line_form.product_id = self.product_b + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'name': self.product_b.name, + 'product_id': self.product_b.id, + 'product_uom_id': self.product_b.uom_id.id, + 'account_id': self.product_b.property_account_expense_id.id, + 'price_unit': 160.0, + 'price_subtotal': 160.0, + 'price_total': 208.0, + 'tax_ids': self.product_b.supplier_taxes_id.ids, + 'debit': 160.0, + }, + self.product_line_vals_2, + { + **self.tax_line_vals_1, + 'price_unit': 48.0, + 'price_subtotal': 48.0, + 'price_total': 48.0, + 'debit': 48.0, + }, + { + **self.tax_line_vals_2, + 'price_unit': 48.0, + 'price_subtotal': 48.0, + 'price_total': 48.0, + 'debit': 48.0, + }, + { + **self.term_line_vals_1, + 'price_unit': -416.0, + 'price_subtotal': -416.0, + 'price_total': -416.0, + 'credit': 416.0, + }, + ], { + **self.move_vals, + 'amount_untaxed': 320.0, + 'amount_tax': 96.0, + 'amount_total': 416.0, + }) + + def test_in_invoice_line_onchange_business_fields_1(self): + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + # Current price_unit is 1000. + # We set quantity = 4, discount = 50%, price_unit = 400 because (4 * 400) * 0.5 = 800. + line_form.quantity = 4 + line_form.discount = 50 + line_form.price_unit = 400 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'quantity': 4, + 'discount': 50.0, + 'price_unit': 400.0, + }, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + self.term_line_vals_1, + ], self.move_vals) + + move_form = Form(self.invoice) + with move_form.line_ids.edit(2) as line_form: + # Reset field except the discount that becomes 100%. + # /!\ The modification is made on the accounting tab. + line_form.quantity = 1 + line_form.discount = 100 + line_form.price_unit = 800 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'discount': 100.0, + 'price_subtotal': 0.0, + 'price_total': 0.0, + 'debit': 0.0, + }, + self.product_line_vals_2, + { + **self.tax_line_vals_1, + 'price_unit': 24.0, + 'price_subtotal': 24.0, + 'price_total': 24.0, + 'debit': 24.0, + }, + self.tax_line_vals_2, + { + **self.term_line_vals_1, + 'price_unit': -208.0, + 'price_subtotal': -208.0, + 'price_total': -208.0, + 'credit': 208.0, + }, + ], { + **self.move_vals, + 'amount_untaxed': 160.0, + 'amount_tax': 48.0, + 'amount_total': 208.0, + }) + + def test_in_invoice_line_onchange_accounting_fields_1(self): + move_form = Form(self.invoice) + with move_form.line_ids.edit(2) as line_form: + # Custom debit on the first product line. + line_form.debit = 3000 + with move_form.line_ids.edit(3) as line_form: + # Custom credit on the second product line. Credit should be reset by onchange. + # /!\ It's a negative line. + line_form.credit = 500 + with move_form.line_ids.edit(0) as line_form: + # Custom debit on the first tax line. + line_form.debit = 800 + with move_form.line_ids.edit(4) as line_form: + # Custom debit on the second tax line. + line_form.debit = 250 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'price_unit': 3000.0, + 'price_subtotal': 3000.0, + 'price_total': 3450.0, + 'debit': 3000.0, + }, + { + **self.product_line_vals_2, + 'price_unit': -500.0, + 'price_subtotal': -500.0, + 'price_total': -650.0, + 'debit': 0.0, + 'credit': 500.0, + }, + { + **self.tax_line_vals_1, + 'price_unit': 800.0, + 'price_subtotal': 800.0, + 'price_total': 800.0, + 'debit': 800.0, + }, + { + **self.tax_line_vals_2, + 'price_unit': 250.0, + 'price_subtotal': 250.0, + 'price_total': 250.0, + 'debit': 250.0, + }, + { + **self.term_line_vals_1, + 'price_unit': -3550.0, + 'price_subtotal': -3550.0, + 'price_total': -3550.0, + 'credit': 3550.0, + }, + ], { + **self.move_vals, + 'amount_untaxed': 2500.0, + 'amount_tax': 1050.0, + 'amount_total': 3550.0, + }) + + def test_in_invoice_line_onchange_partner_1(self): + move_form = Form(self.invoice) + move_form.partner_id = self.partner_b + move_form.invoice_payment_ref = 'turlututu' + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'partner_id': self.partner_b.id, + }, + { + **self.product_line_vals_2, + 'partner_id': self.partner_b.id, + }, + { + **self.tax_line_vals_1, + 'partner_id': self.partner_b.id, + }, + { + **self.tax_line_vals_2, + 'partner_id': self.partner_b.id, + }, + { + **self.term_line_vals_1, + 'name': 'turlututu', + 'partner_id': self.partner_b.id, + 'price_unit': -789.6, + 'price_subtotal': -789.6, + 'price_total': -789.6, + 'credit': 789.6, + 'date_maturity': fields.Date.from_string('2019-02-28'), + }, + { + **self.term_line_vals_1, + 'name': 'turlututu', + 'partner_id': self.partner_b.id, + 'price_unit': -338.4, + 'price_subtotal': -338.4, + 'price_total': -338.4, + 'credit': 338.4, + }, + ], { + **self.move_vals, + 'partner_id': self.partner_b.id, + 'invoice_payment_ref': 'turlututu', + 'fiscal_position_id': self.fiscal_pos_a.id, + 'invoice_payment_term_id': self.pay_terms_b.id, + 'amount_untaxed': 960.0, + 'amount_tax': 168.0, + 'amount_total': 1128.0, + }) + + # Remove lines and recreate them to apply the fiscal position. + move_form = Form(self.invoice) + move_form.invoice_line_ids.remove(0) + move_form.invoice_line_ids.remove(0) + with move_form.invoice_line_ids.new() as line_form: + line_form.product_id = self.product_a + with move_form.invoice_line_ids.new() as line_form: + line_form.product_id = self.product_b + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'account_id': self.product_b.property_account_expense_id.id, + 'partner_id': self.partner_b.id, + 'tax_ids': self.tax_purchase_b.ids, + }, + { + **self.product_line_vals_2, + 'partner_id': self.partner_b.id, + 'price_total': 184.0, + 'tax_ids': self.tax_purchase_b.ids, + }, + { + **self.tax_line_vals_1, + 'name': self.tax_purchase_b.name, + 'partner_id': self.partner_b.id, + 'tax_line_id': self.tax_purchase_b.id, + }, + { + **self.term_line_vals_1, + 'name': 'turlututu', + 'account_id': self.partner_b.property_account_payable_id.id, + 'partner_id': self.partner_b.id, + 'price_unit': -772.8, + 'price_subtotal': -772.8, + 'price_total': -772.8, + 'credit': 772.8, + 'date_maturity': fields.Date.from_string('2019-02-28'), + }, + { + **self.term_line_vals_1, + 'name': 'turlututu', + 'account_id': self.partner_b.property_account_payable_id.id, + 'partner_id': self.partner_b.id, + 'price_unit': -331.2, + 'price_subtotal': -331.2, + 'price_total': -331.2, + 'credit': 331.2, + }, + ], { + **self.move_vals, + 'partner_id': self.partner_b.id, + 'invoice_payment_ref': 'turlututu', + 'fiscal_position_id': self.fiscal_pos_a.id, + 'invoice_payment_term_id': self.pay_terms_b.id, + 'amount_untaxed': 960.0, + 'amount_tax': 144.0, + 'amount_total': 1104.0, + }) + + def test_in_invoice_line_onchange_taxes_1(self): + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + line_form.price_unit = 960 + line_form.tax_ids.add(self.tax_armageddon) + move_form.save() + + child_tax_1 = self.tax_armageddon.children_tax_ids[0] + child_tax_2 = self.tax_armageddon.children_tax_ids[1] + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'price_unit': 960.0, + 'price_subtotal': 800.0, + 'price_total': 1176.0, + 'tax_ids': (self.tax_purchase_a + self.tax_armageddon).ids, + 'tax_exigible': False, + }, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + { + 'name': child_tax_1.name, + 'product_id': False, + 'account_id': self.company_data['default_account_tax_sale'].id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 64.0, + 'price_subtotal': 64.0, + 'price_total': 70.4, + 'tax_ids': child_tax_2.ids, + 'tax_line_id': child_tax_1.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 64.0, + 'credit': 0.0, + 'date_maturity': False, + 'tax_exigible': True, + }, + { + 'name': child_tax_1.name, + 'product_id': False, + 'account_id': self.company_data['default_account_expense'].id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 96.0, + 'price_subtotal': 96.0, + 'price_total': 105.6, + 'tax_ids': child_tax_2.ids, + 'tax_line_id': child_tax_1.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 96.0, + 'credit': 0.0, + 'date_maturity': False, + 'tax_exigible': True, + }, + { + 'name': child_tax_2.name, + 'product_id': False, + 'account_id': child_tax_2.cash_basis_transition_account_id.id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 96.0, + 'price_subtotal': 96.0, + 'price_total': 96.0, + 'tax_ids': [], + 'tax_line_id': child_tax_2.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 96.0, + 'credit': 0.0, + 'date_maturity': False, + 'tax_exigible': False, + }, + { + **self.term_line_vals_1, + 'price_unit': -1384.0, + 'price_subtotal': -1384.0, + 'price_total': -1384.0, + 'credit': 1384.0, + }, + ], { + **self.move_vals, + 'amount_untaxed': 960.0, + 'amount_tax': 424.0, + 'amount_total': 1384.0, + }) + + def test_in_invoice_line_onchange_cash_rounding_1(self): + move_form = Form(self.invoice) + # Add a cash rounding having 'add_invoice_line'. + move_form.invoice_cash_rounding_id = self.cash_rounding_a + move_form.save() + + # The cash rounding does nothing as the total is already rounded. + self.assertInvoiceValues(self.invoice, [ + self.product_line_vals_1, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + self.term_line_vals_1, + ], self.move_vals) + + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + line_form.price_unit = 799.99 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + 'name': 'add_invoice_line', + 'product_id': False, + 'account_id': self.cash_rounding_a.account_id.id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 0.01, + 'price_subtotal': 0.01, + 'price_total': 0.01, + 'tax_ids': [], + 'tax_line_id': False, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.01, + 'credit': 0.0, + 'date_maturity': False, + 'tax_exigible': True, + }, + { + **self.product_line_vals_1, + 'price_unit': 799.99, + 'price_subtotal': 799.99, + 'price_total': 919.99, + 'debit': 799.99, + }, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + self.term_line_vals_1, + ], self.move_vals) + + move_form = Form(self.invoice) + # Change the cash rounding to one having 'biggest_tax'. + move_form.invoice_cash_rounding_id = self.cash_rounding_b + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'price_unit': 799.99, + 'price_subtotal': 799.99, + 'price_total': 919.99, + 'debit': 799.99, + }, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + { + 'name': '%s (rounding)' % self.tax_purchase_a.name, + 'product_id': False, + 'account_id': self.company_data['default_account_tax_purchase'].id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': -0.04, + 'price_subtotal': -0.04, + 'price_total': -0.04, + 'tax_ids': [], + 'tax_line_id': self.tax_purchase_a.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 0.04, + 'date_maturity': False, + 'tax_exigible': True, + }, + { + **self.term_line_vals_1, + 'price_unit': -1127.95, + 'price_subtotal': -1127.95, + 'price_total': -1127.95, + 'credit': 1127.95, + }, + ], { + **self.move_vals, + 'amount_untaxed': 959.99, + 'amount_tax': 167.96, + 'amount_total': 1127.95, + }) + + def test_in_invoice_line_onchange_currency_1(self): + # New journal having a foreign currency set. + journal = self.company_data['default_journal_purchase'].copy() + journal.currency_id = self.currency_data['currency'] + + move_form = Form(self.invoice) + move_form.journal_id = journal + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': 800.0, + 'debit': 400.0, + }, + { + **self.product_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': 160.0, + 'debit': 80.0, + }, + { + **self.tax_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': 144.0, + 'debit': 72.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': 24.0, + 'debit': 12.0, + }, + { + **self.term_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': -1128.0, + 'credit': 564.0, + }, + ], { + **self.move_vals, + 'currency_id': journal.currency_id.id, + 'journal_id': journal.id, + }) + + move_form = Form(self.invoice) + # Change the date to get another rate: 1/3 instead of 1/2. + move_form.date = fields.Date.from_string('2016-01-01') + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': 800.0, + 'debit': 266.67, + }, + { + **self.product_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': 160.0, + 'debit': 53.33, + }, + { + **self.tax_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': 144.0, + 'debit': 48.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': 24.0, + 'debit': 8.0, + }, + { + **self.term_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': -1128.0, + 'credit': 376.0, + }, + ], { + **self.move_vals, + 'currency_id': journal.currency_id.id, + 'journal_id': journal.id, + 'date': fields.Date.from_string('2016-01-01'), + }) + + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + # 0.045 * 0.1 = 0.0045. As the foreign currency has a 0.001 rounding, + # the result should be 0.005 after rounding. + line_form.quantity = 0.1 + line_form.price_unit = 0.045 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'quantity': 0.1, + 'price_unit': 0.05, + 'price_subtotal': 0.005, + 'price_total': 0.006, + 'currency_id': journal.currency_id.id, + 'amount_currency': 0.005, + 'debit': 0.0, + }, + { + **self.product_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': 160.0, + 'debit': 53.33, + }, + { + **self.tax_line_vals_1, + 'price_unit': 24.0, + 'price_subtotal': 24.001, + 'price_total': 24.001, + 'currency_id': journal.currency_id.id, + 'amount_currency': 24.001, + 'debit': 8.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': 24.0, + 'debit': 8.0, + }, + { + **self.term_line_vals_1, + 'currency_id': journal.currency_id.id, + 'price_unit': -208.01, + 'price_subtotal': -208.006, + 'price_total': -208.006, + 'amount_currency': -208.006, + 'credit': 69.33, + }, + ], { + **self.move_vals, + 'currency_id': journal.currency_id.id, + 'journal_id': journal.id, + 'date': fields.Date.from_string('2016-01-01'), + 'amount_untaxed': 160.005, + 'amount_tax': 48.001, + 'amount_total': 208.006, + }) + + move_form = Form(self.invoice) + move_form.currency_id = self.company_data['currency'] + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'quantity': 0.1, + 'price_unit': 0.1, + 'price_subtotal': 0.01, + 'price_total': 0.01, + 'debit': 0.01, + }, + self.product_line_vals_2, + { + **self.tax_line_vals_1, + 'price_unit': 24.0, + 'price_subtotal': 24.0, + 'price_total': 24.0, + 'debit': 24.0, + }, + self.tax_line_vals_2, + { + **self.term_line_vals_1, + 'price_unit': -208.01, + 'price_subtotal': -208.01, + 'price_total': -208.01, + 'credit': 208.01, + }, + ], { + **self.move_vals, + 'currency_id': self.company_data['currency'].id, + 'journal_id': journal.id, + 'date': fields.Date.from_string('2016-01-01'), + 'amount_untaxed': 160.01, + 'amount_tax': 48.0, + 'amount_total': 208.01, + }) + + def test_in_invoice_line_onchange_sequence_number_1(self): + self.assertRecordValues(self.invoice, [{ + 'invoice_sequence_number_next': '0001', + 'invoice_sequence_number_next_prefix': 'BILL/2019/', + }]) + + move_form = Form(self.invoice) + move_form.invoice_sequence_number_next = '0042' + move_form.save() + + self.assertRecordValues(self.invoice, [{ + 'invoice_sequence_number_next': '0042', + 'invoice_sequence_number_next_prefix': 'BILL/2019/', + }]) + + self.invoice.post() + + self.assertRecordValues(self.invoice, [{'name': 'BILL/2019/0042'}]) + + invoice_copy = self.invoice.copy() + invoice_copy.post() + + self.assertRecordValues(invoice_copy, [{'name': 'BILL/2019/0043'}]) + + def test_in_invoice_onchange_past_invoice_1(self): + copy_invoice = self.invoice.copy() + + move_form = Form(self.invoice) + move_form.invoice_line_ids.remove(0) + move_form.invoice_line_ids.remove(0) + move_form.invoice_vendor_bill_id = copy_invoice + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + self.product_line_vals_1, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + self.term_line_vals_1, + ], self.move_vals) + + def test_in_invoice_create_draft_refund(self): + self.invoice.post() + + move_reversal = self.env['account.move.reversal'].with_context(active_ids=self.invoice.ids).create({ + 'date': fields.Date.from_string('2019-02-01'), + 'reason': 'no reason', + 'refund_method': 'refund', + }) + reversal = move_reversal.reverse_moves() + reverse_move = self.env['account.move'].browse(reversal['res_id']) + + self.assertInvoiceValues(reverse_move, [ + { + **self.product_line_vals_1, + 'debit': 0.0, + 'credit': 800.0, + }, + { + **self.product_line_vals_2, + 'debit': 0.0, + 'credit': 160.0, + }, + { + **self.tax_line_vals_1, + 'debit': 0.0, + 'credit': 144.0, + }, + { + **self.tax_line_vals_2, + 'debit': 0.0, + 'credit': 24.0, + }, + { + **self.term_line_vals_1, + 'debit': 1128.0, + 'credit': 0.0, + }, + ], { + **self.move_vals, + 'date': move_reversal.date, + 'state': 'draft', + 'invoice_payment_ref': move_reversal.reason, + 'invoice_payment_state': 'not_paid', + }) + + move_reversal = self.env['account.move.reversal'].with_context(active_ids=self.invoice.ids).create({ + 'date': fields.Date.from_string('2019-02-01'), + 'reason': 'no reason', + 'refund_method': 'cancel', + }) + reversal = move_reversal.reverse_moves() + reverse_move = self.env['account.move'].browse(reversal['res_id']) + + self.assertInvoiceValues(reverse_move, [ + { + **self.product_line_vals_1, + 'debit': 0.0, + 'credit': 800.0, + }, + { + **self.product_line_vals_2, + 'debit': 0.0, + 'credit': 160.0, + }, + { + **self.tax_line_vals_1, + 'debit': 0.0, + 'credit': 144.0, + }, + { + **self.tax_line_vals_2, + 'debit': 0.0, + 'credit': 24.0, + }, + { + **self.term_line_vals_1, + 'debit': 1128.0, + 'credit': 0.0, + }, + ], { + **self.move_vals, + 'date': move_reversal.date, + 'state': 'posted', + 'invoice_payment_ref': move_reversal.reason, + 'invoice_payment_state': 'paid', + }) + + def test_in_invoice_create_1(self): + # Test creating an account_move with the least information. + move = self.env['account.move'].create({ + 'type': 'in_invoice', + 'partner_id': self.partner_a.id, + 'invoice_date': fields.Date.from_string('2019-01-01'), + 'currency_id': self.currency_data['currency'].id, + 'invoice_payment_term_id': self.pay_terms_a.id, + 'invoice_line_ids': [ + (0, None, self.product_line_vals_1), + (0, None, self.product_line_vals_2), + ] + }) + + self.assertInvoiceValues(move, [ + { + **self.product_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 800.0, + 'debit': 400.0, + }, + { + **self.product_line_vals_2, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 160.0, + 'debit': 80.0, + }, + { + **self.tax_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 144.0, + 'debit': 72.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 24.0, + 'debit': 12.0, + }, + { + **self.term_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -1128.0, + 'credit': 564.0, + }, + ], { + **self.move_vals, + 'currency_id': self.currency_data['currency'].id, + }) + + def test_in_invoice_write_1(self): + # Test creating an account_move with the least information. + move = self.env['account.move'].create({ + 'type': 'in_invoice', + 'partner_id': self.partner_a.id, + 'invoice_date': fields.Date.from_string('2019-01-01'), + 'currency_id': self.currency_data['currency'].id, + 'invoice_payment_term_id': self.pay_terms_a.id, + 'invoice_line_ids': [ + (0, None, self.product_line_vals_1), + ] + }) + move.write({ + 'invoice_line_ids': [ + (0, None, self.product_line_vals_2), + ] + }) + + self.assertInvoiceValues(move, [ + { + **self.product_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 800.0, + 'debit': 400.0, + }, + { + **self.product_line_vals_2, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 160.0, + 'debit': 80.0, + }, + { + **self.tax_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 144.0, + 'debit': 72.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 24.0, + 'debit': 12.0, + }, + { + **self.term_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -1128.0, + 'credit': 564.0, + }, + ], { + **self.move_vals, + 'currency_id': self.currency_data['currency'].id, + }) diff --git a/addons/account/tests/test_account_move_in_refund.py b/addons/account/tests/test_account_move_in_refund.py new file mode 100644 index 0000000000000..7fc49e62eaa79 --- /dev/null +++ b/addons/account/tests/test_account_move_in_refund.py @@ -0,0 +1,956 @@ +# -*- coding: utf-8 -*- +from odoo.addons.account.tests.invoice_test_common import InvoiceTestCommon +from odoo.tests.common import Form +from odoo.tests import tagged +from odoo import fields + + +@tagged('post_install', '-at_install') +class TestAccountMoveInRefundOnchanges(InvoiceTestCommon): + + @classmethod + def setUpClass(cls): + super(TestAccountMoveInRefundOnchanges, cls).setUpClass() + + cls.invoice = cls.init_invoice('in_refund') + + cls.product_line_vals_1 = { + 'name': cls.product_a.name, + 'product_id': cls.product_a.id, + 'account_id': cls.product_a.property_account_expense_id.id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': cls.product_a.uom_id.id, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 800.0, + 'price_subtotal': 800.0, + 'price_total': 920.0, + 'tax_ids': cls.product_a.supplier_taxes_id.ids, + 'tax_line_id': False, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 800.0, + 'date_maturity': False, + 'tax_exigible': True, + } + cls.product_line_vals_2 = { + 'name': cls.product_b.name, + 'product_id': cls.product_b.id, + 'account_id': cls.product_b.property_account_expense_id.id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': cls.product_b.uom_id.id, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 160.0, + 'price_subtotal': 160.0, + 'price_total': 208.0, + 'tax_ids': cls.product_b.supplier_taxes_id.ids, + 'tax_line_id': False, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 160.0, + 'date_maturity': False, + 'tax_exigible': True, + } + cls.tax_line_vals_1 = { + 'name': cls.tax_purchase_a.name, + 'product_id': False, + 'account_id': cls.company_data['default_account_tax_purchase'].id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 144.0, + 'price_subtotal': 144.0, + 'price_total': 144.0, + 'tax_ids': [], + 'tax_line_id': cls.tax_purchase_a.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 144.0, + 'date_maturity': False, + 'tax_exigible': True, + } + cls.tax_line_vals_2 = { + 'name': cls.tax_purchase_b.name, + 'product_id': False, + 'account_id': cls.company_data['default_account_tax_purchase'].id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 24.0, + 'price_subtotal': 24.0, + 'price_total': 24.0, + 'tax_ids': [], + 'tax_line_id': cls.tax_purchase_b.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 24.0, + 'date_maturity': False, + 'tax_exigible': True, + } + cls.term_line_vals_1 = { + 'name': '', + 'product_id': False, + 'account_id': cls.company_data['default_account_payable'].id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': -1128.0, + 'price_subtotal': -1128.0, + 'price_total': -1128.0, + 'tax_ids': [], + 'tax_line_id': False, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 1128.0, + 'credit': 0.0, + 'date_maturity': fields.Date.from_string('2019-01-01'), + 'tax_exigible': True, + } + cls.move_vals = { + 'partner_id': cls.partner_a.id, + 'currency_id': cls.company_data['currency'].id, + 'journal_id': cls.company_data['default_journal_purchase'].id, + 'date': fields.Date.from_string('2019-01-01'), + 'fiscal_position_id': False, + 'invoice_payment_ref': '', + 'invoice_payment_term_id': cls.pay_terms_a.id, + 'amount_untaxed': 960.0, + 'amount_tax': 168.0, + 'amount_total': 1128.0, + } + + def setUp(self): + super(TestAccountMoveInRefundOnchanges, self).setUp() + self.assertInvoiceValues(self.invoice, [ + self.product_line_vals_1, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + self.term_line_vals_1, + ], self.move_vals) + + def test_in_refund_line_onchange_product_1(self): + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + line_form.product_id = self.product_b + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'name': self.product_b.name, + 'product_id': self.product_b.id, + 'product_uom_id': self.product_b.uom_id.id, + 'account_id': self.product_b.property_account_expense_id.id, + 'price_unit': 160.0, + 'price_subtotal': 160.0, + 'price_total': 208.0, + 'tax_ids': self.product_b.supplier_taxes_id.ids, + 'credit': 160.0, + }, + self.product_line_vals_2, + { + **self.tax_line_vals_1, + 'price_unit': 48.0, + 'price_subtotal': 48.0, + 'price_total': 48.0, + 'credit': 48.0, + }, + { + **self.tax_line_vals_2, + 'price_unit': 48.0, + 'price_subtotal': 48.0, + 'price_total': 48.0, + 'credit': 48.0, + }, + { + **self.term_line_vals_1, + 'price_unit': -416.0, + 'price_subtotal': -416.0, + 'price_total': -416.0, + 'debit': 416.0, + }, + ], { + **self.move_vals, + 'amount_untaxed': 320.0, + 'amount_tax': 96.0, + 'amount_total': 416.0, + }) + + def test_in_refund_line_onchange_business_fields_1(self): + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + # Current price_unit is 1000. + # We set quantity = 4, discount = 50%, price_unit = 400 because (4 * 400) * 0.5 = 800. + line_form.quantity = 4 + line_form.discount = 50 + line_form.price_unit = 400 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'quantity': 4, + 'discount': 50.0, + 'price_unit': 400.0, + }, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + self.term_line_vals_1, + ], self.move_vals) + + move_form = Form(self.invoice) + with move_form.line_ids.edit(2) as line_form: + # Reset field except the discount that becomes 100%. + # /!\ The modification is made on the accounting tab. + line_form.quantity = 1 + line_form.discount = 100 + line_form.price_unit = 800 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'discount': 100.0, + 'price_subtotal': 0.0, + 'price_total': 0.0, + 'credit': 0.0, + }, + self.product_line_vals_2, + { + **self.tax_line_vals_1, + 'price_unit': 24.0, + 'price_subtotal': 24.0, + 'price_total': 24.0, + 'credit': 24.0, + }, + self.tax_line_vals_2, + { + **self.term_line_vals_1, + 'price_unit': -208.0, + 'price_subtotal': -208.0, + 'price_total': -208.0, + 'debit': 208.0, + }, + ], { + **self.move_vals, + 'amount_untaxed': 160.0, + 'amount_tax': 48.0, + 'amount_total': 208.0, + }) + + def test_in_refund_line_onchange_accounting_fields_1(self): + move_form = Form(self.invoice) + with move_form.line_ids.edit(2) as line_form: + # Custom credit on the first product line. + line_form.credit = 3000 + with move_form.line_ids.edit(3) as line_form: + # Custom debit on the second product line. Credit should be reset by onchange. + # /!\ It's a negative line. + line_form.debit = 500 + with move_form.line_ids.edit(0) as line_form: + # Custom credit on the first tax line. + line_form.credit = 800 + with move_form.line_ids.edit(4) as line_form: + # Custom credit on the second tax line. + line_form.credit = 250 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'price_unit': 3000.0, + 'price_subtotal': 3000.0, + 'price_total': 3450.0, + 'credit': 3000.0, + }, + { + **self.product_line_vals_2, + 'price_unit': -500.0, + 'price_subtotal': -500.0, + 'price_total': -650.0, + 'credit': 0.0, + 'debit': 500.0, + }, + { + **self.tax_line_vals_1, + 'price_unit': 800.0, + 'price_subtotal': 800.0, + 'price_total': 800.0, + 'credit': 800.0, + }, + { + **self.tax_line_vals_2, + 'price_unit': 250.0, + 'price_subtotal': 250.0, + 'price_total': 250.0, + 'credit': 250.0, + }, + { + **self.term_line_vals_1, + 'price_unit': -3550.0, + 'price_subtotal': -3550.0, + 'price_total': -3550.0, + 'debit': 3550.0, + }, + ], { + **self.move_vals, + 'amount_untaxed': 2500.0, + 'amount_tax': 1050.0, + 'amount_total': 3550.0, + }) + + def test_in_refund_line_onchange_partner_1(self): + move_form = Form(self.invoice) + move_form.partner_id = self.partner_b + move_form.invoice_payment_ref = 'turlututu' + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'partner_id': self.partner_b.id, + }, + { + **self.product_line_vals_2, + 'partner_id': self.partner_b.id, + }, + { + **self.tax_line_vals_1, + 'partner_id': self.partner_b.id, + }, + { + **self.tax_line_vals_2, + 'partner_id': self.partner_b.id, + }, + { + **self.term_line_vals_1, + 'name': 'turlututu', + 'partner_id': self.partner_b.id, + 'price_unit': -338.4, + 'price_subtotal': -338.4, + 'price_total': -338.4, + 'debit': 338.4, + }, + { + **self.term_line_vals_1, + 'name': 'turlututu', + 'partner_id': self.partner_b.id, + 'price_unit': -789.6, + 'price_subtotal': -789.6, + 'price_total': -789.6, + 'debit': 789.6, + 'date_maturity': fields.Date.from_string('2019-02-28'), + }, + ], { + **self.move_vals, + 'partner_id': self.partner_b.id, + 'invoice_payment_ref': 'turlututu', + 'fiscal_position_id': self.fiscal_pos_a.id, + 'invoice_payment_term_id': self.pay_terms_b.id, + 'amount_untaxed': 960.0, + 'amount_tax': 168.0, + 'amount_total': 1128.0, + }) + + # Remove lines and recreate them to apply the fiscal position. + move_form = Form(self.invoice) + move_form.invoice_line_ids.remove(0) + move_form.invoice_line_ids.remove(0) + with move_form.invoice_line_ids.new() as line_form: + line_form.product_id = self.product_a + with move_form.invoice_line_ids.new() as line_form: + line_form.product_id = self.product_b + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'account_id': self.product_b.property_account_expense_id.id, + 'partner_id': self.partner_b.id, + 'tax_ids': self.tax_purchase_b.ids, + }, + { + **self.product_line_vals_2, + 'partner_id': self.partner_b.id, + 'price_total': 184.0, + 'tax_ids': self.tax_purchase_b.ids, + }, + { + **self.tax_line_vals_1, + 'name': self.tax_purchase_b.name, + 'partner_id': self.partner_b.id, + 'tax_line_id': self.tax_purchase_b.id, + }, + { + **self.term_line_vals_1, + 'name': 'turlututu', + 'account_id': self.partner_b.property_account_payable_id.id, + 'partner_id': self.partner_b.id, + 'price_unit': -331.2, + 'price_subtotal': -331.2, + 'price_total': -331.2, + 'debit': 331.2, + }, + { + **self.term_line_vals_1, + 'name': 'turlututu', + 'account_id': self.partner_b.property_account_payable_id.id, + 'partner_id': self.partner_b.id, + 'price_unit': -772.8, + 'price_subtotal': -772.8, + 'price_total': -772.8, + 'debit': 772.8, + 'date_maturity': fields.Date.from_string('2019-02-28'), + }, + ], { + **self.move_vals, + 'partner_id': self.partner_b.id, + 'invoice_payment_ref': 'turlututu', + 'fiscal_position_id': self.fiscal_pos_a.id, + 'invoice_payment_term_id': self.pay_terms_b.id, + 'amount_untaxed': 960.0, + 'amount_tax': 144.0, + 'amount_total': 1104.0, + }) + + def test_in_refund_line_onchange_taxes_1(self): + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + line_form.price_unit = 960 + line_form.tax_ids.add(self.tax_armageddon) + move_form.save() + + child_tax_1 = self.tax_armageddon.children_tax_ids[0] + child_tax_2 = self.tax_armageddon.children_tax_ids[1] + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'price_unit': 960.0, + 'price_subtotal': 800.0, + 'price_total': 1176.0, + 'tax_ids': (self.tax_purchase_a + self.tax_armageddon).ids, + 'tax_exigible': False, + }, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + { + 'name': child_tax_1.name, + 'product_id': False, + 'account_id': self.company_data['default_account_expense'].id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 96.0, + 'price_subtotal': 96.0, + 'price_total': 105.6, + 'tax_ids': child_tax_2.ids, + 'tax_line_id': child_tax_1.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 96.0, + 'date_maturity': False, + 'tax_exigible': True, + }, + { + 'name': child_tax_1.name, + 'product_id': False, + 'account_id': self.company_data['default_account_tax_sale'].id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 64.0, + 'price_subtotal': 64.0, + 'price_total': 70.4, + 'tax_ids': child_tax_2.ids, + 'tax_line_id': child_tax_1.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 64.0, + 'date_maturity': False, + 'tax_exigible': True, + }, + { + 'name': child_tax_2.name, + 'product_id': False, + 'account_id': child_tax_2.cash_basis_transition_account_id.id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 96.0, + 'price_subtotal': 96.0, + 'price_total': 96.0, + 'tax_ids': [], + 'tax_line_id': child_tax_2.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 96.0, + 'date_maturity': False, + 'tax_exigible': False, + }, + { + **self.term_line_vals_1, + 'price_unit': -1384.0, + 'price_subtotal': -1384.0, + 'price_total': -1384.0, + 'debit': 1384.0, + }, + ], { + **self.move_vals, + 'amount_untaxed': 960.0, + 'amount_tax': 424.0, + 'amount_total': 1384.0, + }) + + def test_in_refund_line_onchange_cash_rounding_1(self): + move_form = Form(self.invoice) + # Add a cash rounding having 'add_invoice_line'. + move_form.invoice_cash_rounding_id = self.cash_rounding_a + move_form.save() + + # The cash rounding does nothing as the total is already rounded. + self.assertInvoiceValues(self.invoice, [ + self.product_line_vals_1, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + self.term_line_vals_1, + ], self.move_vals) + + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + line_form.price_unit = 799.99 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + 'name': 'add_invoice_line', + 'product_id': False, + 'account_id': self.cash_rounding_a.account_id.id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 0.01, + 'price_subtotal': 0.01, + 'price_total': 0.01, + 'tax_ids': [], + 'tax_line_id': False, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 0.01, + 'date_maturity': False, + 'tax_exigible': True, + }, + { + **self.product_line_vals_1, + 'price_unit': 799.99, + 'price_subtotal': 799.99, + 'price_total': 919.99, + 'credit': 799.99, + }, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + self.term_line_vals_1, + ], self.move_vals) + + move_form = Form(self.invoice) + # Change the cash rounding to one having 'biggest_tax'. + move_form.invoice_cash_rounding_id = self.cash_rounding_b + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'price_unit': 799.99, + 'price_subtotal': 799.99, + 'price_total': 919.99, + 'credit': 799.99, + }, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + { + 'name': '%s (rounding)' % self.tax_purchase_a.name, + 'product_id': False, + 'account_id': self.company_data['default_account_tax_purchase'].id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': -0.04, + 'price_subtotal': -0.04, + 'price_total': -0.04, + 'tax_ids': [], + 'tax_line_id': self.tax_purchase_a.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.04, + 'credit': 0.0, + 'date_maturity': False, + 'tax_exigible': True, + }, + { + **self.term_line_vals_1, + 'price_unit': -1127.95, + 'price_subtotal': -1127.95, + 'price_total': -1127.95, + 'debit': 1127.95, + }, + ], { + **self.move_vals, + 'amount_untaxed': 959.99, + 'amount_tax': 167.96, + 'amount_total': 1127.95, + }) + + def test_in_refund_line_onchange_currency_1(self): + # New journal having a foreign currency set. + journal = self.company_data['default_journal_purchase'].copy() + journal.currency_id = self.currency_data['currency'] + + move_form = Form(self.invoice) + move_form.journal_id = journal + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': -800.0, + 'credit': 400.0, + }, + { + **self.product_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': -160.0, + 'credit': 80.0, + }, + { + **self.tax_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': -144.0, + 'credit': 72.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': -24.0, + 'credit': 12.0, + }, + { + **self.term_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': 1128.0, + 'debit': 564.0, + }, + ], { + **self.move_vals, + 'currency_id': journal.currency_id.id, + 'journal_id': journal.id, + }) + + move_form = Form(self.invoice) + # Change the date to get another rate: 1/3 instead of 1/2. + move_form.date = fields.Date.from_string('2016-01-01') + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': -800.0, + 'credit': 266.67, + }, + { + **self.product_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': -160.0, + 'credit': 53.33, + }, + { + **self.tax_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': -144.0, + 'credit': 48.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': -24.0, + 'credit': 8.0, + }, + { + **self.term_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': 1128.0, + 'debit': 376.0, + }, + ], { + **self.move_vals, + 'currency_id': journal.currency_id.id, + 'journal_id': journal.id, + 'date': fields.Date.from_string('2016-01-01'), + }) + + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + # 0.045 * 0.1 = 0.0045. As the foreign currency has a 0.001 rounding, + # the result should be 0.005 after rounding. + line_form.quantity = 0.1 + line_form.price_unit = 0.045 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'quantity': 0.1, + 'price_unit': 0.05, + 'price_subtotal': 0.005, + 'price_total': 0.006, + 'currency_id': journal.currency_id.id, + 'amount_currency': -0.005, + 'credit': 0.0, + }, + { + **self.product_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': -160.0, + 'credit': 53.33, + }, + { + **self.tax_line_vals_1, + 'price_unit': 24.0, + 'price_subtotal': 24.001, + 'price_total': 24.001, + 'currency_id': journal.currency_id.id, + 'amount_currency': -24.001, + 'credit': 8.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': -24.0, + 'credit': 8.0, + }, + { + **self.term_line_vals_1, + 'currency_id': journal.currency_id.id, + 'price_unit': -208.01, + 'price_subtotal': -208.006, + 'price_total': -208.006, + 'amount_currency': 208.006, + 'debit': 69.33, + }, + ], { + **self.move_vals, + 'currency_id': journal.currency_id.id, + 'journal_id': journal.id, + 'date': fields.Date.from_string('2016-01-01'), + 'amount_untaxed': 160.005, + 'amount_tax': 48.001, + 'amount_total': 208.006, + }) + + move_form = Form(self.invoice) + move_form.currency_id = self.company_data['currency'] + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'quantity': 0.1, + 'price_unit': 0.1, + 'price_subtotal': 0.01, + 'price_total': 0.01, + 'credit': 0.01, + }, + self.product_line_vals_2, + { + **self.tax_line_vals_1, + 'price_unit': 24.0, + 'price_subtotal': 24.0, + 'price_total': 24.0, + 'credit': 24.0, + }, + self.tax_line_vals_2, + { + **self.term_line_vals_1, + 'price_unit': -208.01, + 'price_subtotal': -208.01, + 'price_total': -208.01, + 'debit': 208.01, + }, + ], { + **self.move_vals, + 'currency_id': self.company_data['currency'].id, + 'journal_id': journal.id, + 'date': fields.Date.from_string('2016-01-01'), + 'amount_untaxed': 160.01, + 'amount_tax': 48.0, + 'amount_total': 208.01, + }) + + def test_in_refund_line_onchange_sequence_number_1(self): + self.assertRecordValues(self.invoice, [{ + 'invoice_sequence_number_next': '0001', + 'invoice_sequence_number_next_prefix': 'BILL/2019/', + }]) + + move_form = Form(self.invoice) + move_form.invoice_sequence_number_next = '0042' + move_form.save() + + self.assertRecordValues(self.invoice, [{ + 'invoice_sequence_number_next': '0042', + 'invoice_sequence_number_next_prefix': 'BILL/2019/', + }]) + + self.invoice.post() + + self.assertRecordValues(self.invoice, [{'name': 'BILL/2019/0042'}]) + + invoice_copy = self.invoice.copy() + invoice_copy.post() + + self.assertRecordValues(invoice_copy, [{'name': 'BILL/2019/0043'}]) + + def test_in_refund_onchange_past_invoice_1(self): + copy_invoice = self.invoice.copy() + + move_form = Form(self.invoice) + move_form.invoice_line_ids.remove(0) + move_form.invoice_line_ids.remove(0) + move_form.invoice_vendor_bill_id = copy_invoice + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + self.product_line_vals_1, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + self.term_line_vals_1, + ], self.move_vals) + + def test_in_refund_create_1(self): + # Test creating an account_move with the least information. + move = self.env['account.move'].create({ + 'type': 'in_refund', + 'partner_id': self.partner_a.id, + 'invoice_date': fields.Date.from_string('2019-01-01'), + 'currency_id': self.currency_data['currency'].id, + 'invoice_payment_term_id': self.pay_terms_a.id, + 'invoice_line_ids': [ + (0, None, self.product_line_vals_1), + (0, None, self.product_line_vals_2), + ] + }) + + self.assertInvoiceValues(move, [ + { + **self.product_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -800.0, + 'credit': 400.0, + }, + { + **self.product_line_vals_2, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -160.0, + 'credit': 80.0, + }, + { + **self.tax_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -144.0, + 'credit': 72.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -24.0, + 'credit': 12.0, + }, + { + **self.term_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 1128.0, + 'debit': 564.0, + }, + ], { + **self.move_vals, + 'currency_id': self.currency_data['currency'].id, + }) + + def test_in_refund_write_1(self): + # Test creating an account_move with the least information. + move = self.env['account.move'].create({ + 'type': 'in_refund', + 'partner_id': self.partner_a.id, + 'invoice_date': fields.Date.from_string('2019-01-01'), + 'currency_id': self.currency_data['currency'].id, + 'invoice_payment_term_id': self.pay_terms_a.id, + 'invoice_line_ids': [ + (0, None, self.product_line_vals_1), + ] + }) + move.write({ + 'invoice_line_ids': [ + (0, None, self.product_line_vals_2), + ] + }) + + self.assertInvoiceValues(move, [ + { + **self.product_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -800.0, + 'credit': 400.0, + }, + { + **self.product_line_vals_2, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -160.0, + 'credit': 80.0, + }, + { + **self.tax_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -144.0, + 'credit': 72.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -24.0, + 'credit': 12.0, + }, + { + **self.term_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 1128.0, + 'debit': 564.0, + }, + ], { + **self.move_vals, + 'currency_id': self.currency_data['currency'].id, + }) diff --git a/addons/account/tests/test_account_move_out_invoice.py b/addons/account/tests/test_account_move_out_invoice.py new file mode 100644 index 0000000000000..50038eb9efc6f --- /dev/null +++ b/addons/account/tests/test_account_move_out_invoice.py @@ -0,0 +1,1117 @@ +# -*- coding: utf-8 -*- +from odoo.addons.account.tests.invoice_test_common import InvoiceTestCommon +from odoo.tests.common import Form +from odoo.tests import tagged +from odoo import fields + +from unittest.mock import patch + + +@tagged('post_install', '-at_install') +class TestAccountMoveOutInvoiceOnchanges(InvoiceTestCommon): + + @classmethod + def setUpClass(cls): + super(TestAccountMoveOutInvoiceOnchanges, cls).setUpClass() + + cls.invoice = cls.init_invoice('out_invoice') + + cls.product_line_vals_1 = { + 'name': cls.product_a.name, + 'product_id': cls.product_a.id, + 'account_id': cls.product_a.property_account_income_id.id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': cls.product_a.uom_id.id, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 1000.0, + 'price_subtotal': 1000.0, + 'price_total': 1150.0, + 'tax_ids': cls.product_a.taxes_id.ids, + 'tax_line_id': False, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 1000.0, + 'date_maturity': False, + 'tax_exigible': True, + } + cls.product_line_vals_2 = { + 'name': cls.product_b.name, + 'product_id': cls.product_b.id, + 'account_id': cls.product_b.property_account_income_id.id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': cls.product_b.uom_id.id, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 200.0, + 'price_subtotal': 200.0, + 'price_total': 260.0, + 'tax_ids': cls.product_b.taxes_id.ids, + 'tax_line_id': False, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 200.0, + 'date_maturity': False, + 'tax_exigible': True, + } + cls.tax_line_vals_1 = { + 'name': cls.tax_sale_a.name, + 'product_id': False, + 'account_id': cls.company_data['default_account_tax_sale'].id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 180.0, + 'price_subtotal': 180.0, + 'price_total': 180.0, + 'tax_ids': [], + 'tax_line_id': cls.tax_sale_a.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 180.0, + 'date_maturity': False, + 'tax_exigible': True, + } + cls.tax_line_vals_2 = { + 'name': cls.tax_sale_b.name, + 'product_id': False, + 'account_id': cls.company_data['default_account_tax_sale'].id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 30.0, + 'price_subtotal': 30.0, + 'price_total': 30.0, + 'tax_ids': [], + 'tax_line_id': cls.tax_sale_b.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 30.0, + 'date_maturity': False, + 'tax_exigible': True, + } + cls.term_line_vals_1 = { + 'name': '', + 'product_id': False, + 'account_id': cls.company_data['default_account_receivable'].id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': -1410.0, + 'price_subtotal': -1410.0, + 'price_total': -1410.0, + 'tax_ids': [], + 'tax_line_id': False, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 1410.0, + 'credit': 0.0, + 'date_maturity': fields.Date.from_string('2019-01-01'), + 'tax_exigible': True, + } + cls.move_vals = { + 'partner_id': cls.partner_a.id, + 'currency_id': cls.company_data['currency'].id, + 'journal_id': cls.company_data['default_journal_sale'].id, + 'date': fields.Date.from_string('2019-01-01'), + 'fiscal_position_id': False, + 'invoice_payment_ref': '', + 'invoice_payment_term_id': cls.pay_terms_a.id, + 'amount_untaxed': 1200.0, + 'amount_tax': 210.0, + 'amount_total': 1410.0, + } + + def setUp(self): + super(TestAccountMoveOutInvoiceOnchanges, self).setUp() + self.assertInvoiceValues(self.invoice, [ + self.product_line_vals_1, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + self.term_line_vals_1, + ], self.move_vals) + + def test_out_invoice_line_onchange_product_1(self): + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + line_form.product_id = self.product_b + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'name': self.product_b.name, + 'product_id': self.product_b.id, + 'product_uom_id': self.product_b.uom_id.id, + 'account_id': self.product_b.property_account_income_id.id, + 'price_unit': 200.0, + 'price_subtotal': 200.0, + 'price_total': 260.0, + 'tax_ids': self.product_b.taxes_id.ids, + 'credit': 200.0, + }, + self.product_line_vals_2, + { + **self.tax_line_vals_1, + 'price_unit': 60.0, + 'price_subtotal': 60.0, + 'price_total': 60.0, + 'credit': 60.0, + }, + { + **self.tax_line_vals_2, + 'price_unit': 60.0, + 'price_subtotal': 60.0, + 'price_total': 60.0, + 'credit': 60.0, + }, + { + **self.term_line_vals_1, + 'price_unit': -520.0, + 'price_subtotal': -520.0, + 'price_total': -520.0, + 'debit': 520.0, + }, + ], { + **self.move_vals, + 'amount_untaxed': 400.0, + 'amount_tax': 120.0, + 'amount_total': 520.0, + }) + + def test_out_invoice_line_onchange_business_fields_1(self): + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + # Current price_unit is 1000. + # We set quantity = 4, discount = 50%, price_unit = 500 because (4 * 500) * 0.5 = 1000. + line_form.quantity = 4 + line_form.discount = 50 + line_form.price_unit = 500 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'quantity': 4, + 'discount': 50.0, + 'price_unit': 500.0, + }, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + self.term_line_vals_1, + ], self.move_vals) + + move_form = Form(self.invoice) + with move_form.line_ids.edit(2) as line_form: + # Reset field except the discount that becomes 100%. + # /!\ The modification is made on the accounting tab. + line_form.quantity = 1 + line_form.discount = 100 + line_form.price_unit = 1000 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'discount': 100.0, + 'price_subtotal': 0.0, + 'price_total': 0.0, + 'credit': 0.0, + }, + self.product_line_vals_2, + { + **self.tax_line_vals_1, + 'price_unit': 30.0, + 'price_subtotal': 30.0, + 'price_total': 30.0, + 'credit': 30.0, + }, + self.tax_line_vals_2, + { + **self.term_line_vals_1, + 'price_unit': -260.0, + 'price_subtotal': -260.0, + 'price_total': -260.0, + 'debit': 260.0, + }, + ], { + **self.move_vals, + 'amount_untaxed': 200.0, + 'amount_tax': 60.0, + 'amount_total': 260.0, + }) + + def test_out_invoice_line_onchange_accounting_fields_1(self): + move_form = Form(self.invoice) + with move_form.line_ids.edit(2) as line_form: + # Custom credit on the first product line. + line_form.credit = 3000 + with move_form.line_ids.edit(3) as line_form: + # Custom debit on the second product line. Credit should be reset by onchange. + # /!\ It's a negative line. + line_form.debit = 500 + with move_form.line_ids.edit(0) as line_form: + # Custom credit on the first tax line. + line_form.credit = 800 + with move_form.line_ids.edit(4) as line_form: + # Custom credit on the second tax line. + line_form.credit = 250 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'price_unit': 3000.0, + 'price_subtotal': 3000.0, + 'price_total': 3450.0, + 'credit': 3000.0, + }, + { + **self.product_line_vals_2, + 'price_unit': -500.0, + 'price_subtotal': -500.0, + 'price_total': -650.0, + 'debit': 500.0, + 'credit': 0.0, + }, + { + **self.tax_line_vals_1, + 'price_unit': 800.0, + 'price_subtotal': 800.0, + 'price_total': 800.0, + 'credit': 800.0, + }, + { + **self.tax_line_vals_2, + 'price_unit': 250.0, + 'price_subtotal': 250.0, + 'price_total': 250.0, + 'credit': 250.0, + }, + { + **self.term_line_vals_1, + 'price_unit': -3550.0, + 'price_subtotal': -3550.0, + 'price_total': -3550.0, + 'debit': 3550.0, + }, + ], { + **self.move_vals, + 'amount_untaxed': 2500.0, + 'amount_tax': 1050.0, + 'amount_total': 3550.0, + }) + + def test_out_invoice_line_onchange_partner_1(self): + move_form = Form(self.invoice) + move_form.partner_id = self.partner_b + move_form.invoice_payment_ref = 'turlututu' + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'partner_id': self.partner_b.id, + }, + { + **self.product_line_vals_2, + 'partner_id': self.partner_b.id, + }, + { + **self.tax_line_vals_1, + 'partner_id': self.partner_b.id, + }, + { + **self.tax_line_vals_2, + 'partner_id': self.partner_b.id, + }, + { + **self.term_line_vals_1, + 'name': 'turlututu', + 'partner_id': self.partner_b.id, + 'price_unit': -423.0, + 'price_subtotal': -423.0, + 'price_total': -423.0, + 'debit': 423.0, + }, + { + **self.term_line_vals_1, + 'name': 'turlututu', + 'partner_id': self.partner_b.id, + 'price_unit': -987.0, + 'price_subtotal': -987.0, + 'price_total': -987.0, + 'debit': 987.0, + 'date_maturity': fields.Date.from_string('2019-02-28'), + }, + ], { + **self.move_vals, + 'partner_id': self.partner_b.id, + 'invoice_payment_ref': 'turlututu', + 'fiscal_position_id': self.fiscal_pos_a.id, + 'invoice_payment_term_id': self.pay_terms_b.id, + 'amount_untaxed': 1200.0, + 'amount_tax': 210.0, + 'amount_total': 1410.0, + }) + + # Remove lines and recreate them to apply the fiscal position. + move_form = Form(self.invoice) + move_form.invoice_line_ids.remove(0) + move_form.invoice_line_ids.remove(0) + with move_form.invoice_line_ids.new() as line_form: + line_form.product_id = self.product_a + with move_form.invoice_line_ids.new() as line_form: + line_form.product_id = self.product_b + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'account_id': self.product_b.property_account_income_id.id, + 'partner_id': self.partner_b.id, + 'tax_ids': self.tax_sale_b.ids, + }, + { + **self.product_line_vals_2, + 'partner_id': self.partner_b.id, + 'price_total': 230.0, + 'tax_ids': self.tax_sale_b.ids, + }, + { + **self.tax_line_vals_1, + 'name': self.tax_sale_b.name, + 'partner_id': self.partner_b.id, + 'tax_line_id': self.tax_sale_b.id, + }, + { + **self.term_line_vals_1, + 'name': 'turlututu', + 'account_id': self.partner_b.property_account_receivable_id.id, + 'partner_id': self.partner_b.id, + 'price_unit': -414.0, + 'price_subtotal': -414.0, + 'price_total': -414.0, + 'debit': 414.0, + }, + { + **self.term_line_vals_1, + 'name': 'turlututu', + 'account_id': self.partner_b.property_account_receivable_id.id, + 'partner_id': self.partner_b.id, + 'price_unit': -966.0, + 'price_subtotal': -966.0, + 'price_total': -966.0, + 'debit': 966.0, + 'date_maturity': fields.Date.from_string('2019-02-28'), + }, + ], { + **self.move_vals, + 'partner_id': self.partner_b.id, + 'invoice_payment_ref': 'turlututu', + 'fiscal_position_id': self.fiscal_pos_a.id, + 'invoice_payment_term_id': self.pay_terms_b.id, + 'amount_untaxed': 1200.0, + 'amount_tax': 180.0, + 'amount_total': 1380.0, + }) + + def test_out_invoice_line_onchange_taxes_1(self): + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + line_form.price_unit = 1200 + line_form.tax_ids.add(self.tax_armageddon) + move_form.save() + + child_tax_1 = self.tax_armageddon.children_tax_ids[0] + child_tax_2 = self.tax_armageddon.children_tax_ids[1] + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'price_unit': 1200.0, + 'price_subtotal': 1000.0, + 'price_total': 1470.0, + 'tax_ids': (self.tax_sale_a + self.tax_armageddon).ids, + 'tax_exigible': False, + }, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + { + 'name': child_tax_1.name, + 'product_id': False, + 'account_id': self.company_data['default_account_revenue'].id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 120.0, + 'price_subtotal': 120.0, + 'price_total': 132.0, + 'tax_ids': child_tax_2.ids, + 'tax_line_id': child_tax_1.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 120.0, + 'date_maturity': False, + 'tax_exigible': True, + }, + { + 'name': child_tax_1.name, + 'product_id': False, + 'account_id': self.company_data['default_account_tax_sale'].id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 80.0, + 'price_subtotal': 80.0, + 'price_total': 88.0, + 'tax_ids': child_tax_2.ids, + 'tax_line_id': child_tax_1.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 80.0, + 'date_maturity': False, + 'tax_exigible': True, + }, + { + 'name': child_tax_2.name, + 'product_id': False, + 'account_id': child_tax_2.cash_basis_transition_account_id.id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 120.0, + 'price_subtotal': 120.0, + 'price_total': 120.0, + 'tax_ids': [], + 'tax_line_id': child_tax_2.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 120.0, + 'date_maturity': False, + 'tax_exigible': False, + }, + { + **self.term_line_vals_1, + 'price_unit': -1730.0, + 'price_subtotal': -1730.0, + 'price_total': -1730.0, + 'debit': 1730.0, + }, + ], { + **self.move_vals, + 'amount_untaxed': 1200.0, + 'amount_tax': 530.0, + 'amount_total': 1730.0, + }) + + def test_out_invoice_line_onchange_cash_rounding_1(self): + move_form = Form(self.invoice) + # Add a cash rounding having 'add_invoice_line'. + move_form.invoice_cash_rounding_id = self.cash_rounding_a + move_form.save() + + # The cash rounding does nothing as the total is already rounded. + self.assertInvoiceValues(self.invoice, [ + self.product_line_vals_1, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + self.term_line_vals_1, + ], self.move_vals) + + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + line_form.price_unit = 999.99 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + 'name': 'add_invoice_line', + 'product_id': False, + 'account_id': self.cash_rounding_a.account_id.id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 0.01, + 'price_subtotal': 0.01, + 'price_total': 0.01, + 'tax_ids': [], + 'tax_line_id': False, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 0.01, + 'date_maturity': False, + 'tax_exigible': True, + }, + { + **self.product_line_vals_1, + 'price_unit': 999.99, + 'price_subtotal': 999.99, + 'price_total': 1149.99, + 'credit': 999.99, + }, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + self.term_line_vals_1, + ], self.move_vals) + + move_form = Form(self.invoice) + # Change the cash rounding to one having 'biggest_tax'. + move_form.invoice_cash_rounding_id = self.cash_rounding_b + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'price_unit': 999.99, + 'price_subtotal': 999.99, + 'price_total': 1149.99, + 'credit': 999.99, + }, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + { + 'name': '%s (rounding)' % self.tax_sale_a.name, + 'product_id': False, + 'account_id': self.company_data['default_account_tax_sale'].id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': -0.04, + 'price_subtotal': -0.04, + 'price_total': -0.04, + 'tax_ids': [], + 'tax_line_id': self.tax_sale_a.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.04, + 'credit': 0.0, + 'date_maturity': False, + 'tax_exigible': True, + }, + { + **self.term_line_vals_1, + 'price_unit': -1409.95, + 'price_subtotal': -1409.95, + 'price_total': -1409.95, + 'debit': 1409.95, + }, + ], { + **self.move_vals, + 'amount_untaxed': 1199.99, + 'amount_tax': 209.96, + 'amount_total': 1409.95, + }) + + def test_out_invoice_line_onchange_currency_1(self): + # New journal having a foreign currency set. + journal = self.company_data['default_journal_sale'].copy() + journal.currency_id = self.currency_data['currency'] + + move_form = Form(self.invoice) + move_form.journal_id = journal + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': -1000.0, + 'credit': 500.0, + }, + { + **self.product_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': -200.0, + 'credit': 100.0, + }, + { + **self.tax_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': -180.0, + 'credit': 90.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': -30.0, + 'credit': 15.0, + }, + { + **self.term_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': 1410.0, + 'debit': 705.0, + }, + ], { + **self.move_vals, + 'currency_id': journal.currency_id.id, + 'journal_id': journal.id, + }) + + move_form = Form(self.invoice) + # Change the date to get another rate: 1/3 instead of 1/2. + move_form.date = fields.Date.from_string('2016-01-01') + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': -1000.0, + 'credit': 333.33, + }, + { + **self.product_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': -200.0, + 'credit': 66.67, + }, + { + **self.tax_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': -180.0, + 'credit': 60.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': -30.0, + 'credit': 10.0, + }, + { + **self.term_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': 1410.0, + 'debit': 470.0, + }, + ], { + **self.move_vals, + 'currency_id': journal.currency_id.id, + 'journal_id': journal.id, + 'date': fields.Date.from_string('2016-01-01'), + }) + + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + # 0.045 * 0.1 = 0.0045. As the foreign currency has a 0.001 rounding, + # the result should be 0.005 after rounding. + line_form.quantity = 0.1 + line_form.price_unit = 0.045 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'quantity': 0.1, + 'price_unit': 0.05, + 'price_subtotal': 0.005, + 'price_total': 0.006, + 'currency_id': journal.currency_id.id, + 'amount_currency': -0.005, + 'credit': 0.0, + }, + { + **self.product_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': -200.0, + 'credit': 66.67, + }, + { + **self.tax_line_vals_1, + 'price_unit': 30.0, + 'price_subtotal': 30.001, + 'price_total': 30.001, + 'currency_id': journal.currency_id.id, + 'amount_currency': -30.001, + 'credit': 10.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': -30.0, + 'credit': 10.0, + }, + { + **self.term_line_vals_1, + 'currency_id': journal.currency_id.id, + 'price_unit': -260.01, + 'price_subtotal': -260.006, + 'price_total': -260.006, + 'amount_currency': 260.006, + 'debit': 86.67, + }, + ], { + **self.move_vals, + 'currency_id': journal.currency_id.id, + 'journal_id': journal.id, + 'date': fields.Date.from_string('2016-01-01'), + 'amount_untaxed': 200.005, + 'amount_tax': 60.001, + 'amount_total': 260.006, + }) + + move_form = Form(self.invoice) + move_form.currency_id = self.company_data['currency'] + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'quantity': 0.1, + 'price_unit': 0.1, + 'price_subtotal': 0.01, + 'price_total': 0.01, + 'credit': 0.01, + }, + self.product_line_vals_2, + { + **self.tax_line_vals_1, + 'price_unit': 30.0, + 'price_subtotal': 30.0, + 'price_total': 30.0, + 'credit': 30.0, + }, + self.tax_line_vals_2, + { + **self.term_line_vals_1, + 'price_unit': -260.01, + 'price_subtotal': -260.01, + 'price_total': -260.01, + 'debit': 260.01, + }, + ], { + **self.move_vals, + 'currency_id': self.company_data['currency'].id, + 'journal_id': journal.id, + 'date': fields.Date.from_string('2016-01-01'), + 'amount_untaxed': 200.01, + 'amount_tax': 60.0, + 'amount_total': 260.01, + }) + + def test_out_invoice_onchange_sequence_number_1(self): + self.assertRecordValues(self.invoice, [{ + 'invoice_sequence_number_next': '0001', + 'invoice_sequence_number_next_prefix': 'INV/2019/', + }]) + + move_form = Form(self.invoice) + move_form.invoice_sequence_number_next = '0042' + move_form.save() + + self.assertRecordValues(self.invoice, [{ + 'invoice_sequence_number_next': '0042', + 'invoice_sequence_number_next_prefix': 'INV/2019/', + }]) + + self.invoice.post() + + self.assertRecordValues(self.invoice, [{'name': 'INV/2019/0042'}]) + + invoice_copy = self.invoice.copy() + invoice_copy.post() + + self.assertRecordValues(invoice_copy, [{'name': 'INV/2019/0043'}]) + + def test_out_invoice_create_draft_refund(self): + self.invoice.post() + + move_reversal = self.env['account.move.reversal'].with_context(active_ids=self.invoice.ids).create({ + 'date': fields.Date.from_string('2019-02-01'), + 'reason': 'no reason', + 'refund_method': 'refund', + }) + reversal = move_reversal.reverse_moves() + reverse_move = self.env['account.move'].browse(reversal['res_id']) + + self.assertInvoiceValues(reverse_move, [ + { + **self.product_line_vals_1, + 'debit': 1000.0, + 'credit': 0.0, + }, + { + **self.product_line_vals_2, + 'debit': 200.0, + 'credit': 0.0, + }, + { + **self.tax_line_vals_1, + 'debit': 180.0, + 'credit': 0.0, + }, + { + **self.tax_line_vals_2, + 'debit': 30.0, + 'credit': 0.0, + }, + { + **self.term_line_vals_1, + 'name': self.invoice.name, + 'debit': 0.0, + 'credit': 1410.0, + }, + ], { + **self.move_vals, + 'date': move_reversal.date, + 'state': 'draft', + 'invoice_payment_ref': move_reversal.reason, + 'invoice_payment_state': 'not_paid', + }) + + move_reversal = self.env['account.move.reversal'].with_context(active_ids=self.invoice.ids).create({ + 'date': fields.Date.from_string('2019-02-01'), + 'reason': 'no reason', + 'refund_method': 'cancel', + }) + reversal = move_reversal.reverse_moves() + reverse_move = self.env['account.move'].browse(reversal['res_id']) + + self.assertInvoiceValues(reverse_move, [ + { + **self.product_line_vals_1, + 'debit': 1000.0, + 'credit': 0.0, + }, + { + **self.product_line_vals_2, + 'debit': 200.0, + 'credit': 0.0, + }, + { + **self.tax_line_vals_1, + 'debit': 180.0, + 'credit': 0.0, + }, + { + **self.tax_line_vals_2, + 'debit': 30.0, + 'credit': 0.0, + }, + { + **self.term_line_vals_1, + 'name': self.invoice.name, + 'debit': 0.0, + 'credit': 1410.0, + }, + ], { + **self.move_vals, + 'date': move_reversal.date, + 'state': 'posted', + 'invoice_payment_ref': move_reversal.reason, + 'invoice_payment_state': 'paid', + }) + + def test_out_invoice_create_1(self): + # Test creating an account_move with the least information. + move = self.env['account.move'].create({ + 'type': 'out_invoice', + 'partner_id': self.partner_a.id, + 'invoice_date': fields.Date.from_string('2019-01-01'), + 'currency_id': self.currency_data['currency'].id, + 'invoice_payment_term_id': self.pay_terms_a.id, + 'invoice_line_ids': [ + (0, None, self.product_line_vals_1), + (0, None, self.product_line_vals_2), + ] + }) + + self.assertInvoiceValues(move, [ + { + **self.product_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -1000.0, + 'credit': 500.0, + }, + { + **self.product_line_vals_2, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -200.0, + 'credit': 100.0, + }, + { + **self.tax_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -180.0, + 'credit': 90.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -30.0, + 'credit': 15.0, + }, + { + **self.term_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 1410.0, + 'debit': 705.0, + }, + ], { + **self.move_vals, + 'currency_id': self.currency_data['currency'].id, + }) + + def test_out_invoice_write_1(self): + # Test creating an account_move with the least information. + move = self.env['account.move'].create({ + 'type': 'out_invoice', + 'partner_id': self.partner_a.id, + 'invoice_date': fields.Date.from_string('2019-01-01'), + 'currency_id': self.currency_data['currency'].id, + 'invoice_payment_term_id': self.pay_terms_a.id, + 'invoice_line_ids': [ + (0, None, self.product_line_vals_1), + ] + }) + move.write({ + 'invoice_line_ids': [ + (0, None, self.product_line_vals_2), + ] + }) + + self.assertInvoiceValues(move, [ + { + **self.product_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -1000.0, + 'credit': 500.0, + }, + { + **self.product_line_vals_2, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -200.0, + 'credit': 100.0, + }, + { + **self.tax_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -180.0, + 'credit': 90.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -30.0, + 'credit': 15.0, + }, + { + **self.term_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 1410.0, + 'debit': 705.0, + }, + ], { + **self.move_vals, + 'currency_id': self.currency_data['currency'].id, + }) + + def test_out_invoice_post_1(self): + frozen_today = fields.Date.today() + with patch.object(fields.Date, 'today', lambda *args, **kwargs: frozen_today), patch.object(fields.Date, 'context_today', lambda *args, **kwargs: frozen_today): + # Create an invoice with rate 1/3. + move = self.env['account.move'].create({ + 'type': 'out_invoice', + 'partner_id': self.partner_a.id, + 'invoice_date': fields.Date.from_string('2016-01-01'), + 'currency_id': self.currency_data['currency'].id, + 'invoice_payment_term_id': self.pay_terms_a.id, + 'invoice_line_ids': [ + (0, None, self.product_line_vals_1), + (0, None, self.product_line_vals_2), + ] + }) + + # Remove the invoice_date to check: + # - The invoice_date must be set automatically at today during the post. + # - As the invoice_date changed, date did too so the currency rate has changed (1/3 => 1/2). + # - A different invoice_date implies also a new date_maturity. + # Add a manual edition of a tax line: + # - The modification must be preserved in the business fields. + # - The journal entry must be balanced before / after the post. + move.write({ + 'invoice_date': False, + 'line_ids': [ + (1, move.line_ids.filtered(lambda line: line.tax_line_id.id == self.tax_line_vals_1['tax_line_id']).id, { + 'amount_currency': -200.0, + }), + (1, move.line_ids.filtered(lambda line: line.date_maturity).id, { + 'amount_currency': 1430.0, + }), + ], + }) + + move.post() + + self.assertInvoiceValues(move, [ + { + **self.product_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -1000.0, + 'credit': 500.0, + }, + { + **self.product_line_vals_2, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -200.0, + 'credit': 100.0, + }, + { + **self.tax_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'price_unit': 200.0, + 'price_subtotal': 200.0, + 'price_total': 200.0, + 'amount_currency': -200.0, + 'credit': 100.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -30.0, + 'credit': 15.0, + }, + { + **self.term_line_vals_1, + 'name': move.name, + 'currency_id': self.currency_data['currency'].id, + 'price_unit': -1430.0, + 'price_subtotal': -1430.0, + 'price_total': -1430.0, + 'amount_currency': 1430.0, + 'debit': 715.0, + 'date_maturity': frozen_today, + }, + ], { + **self.move_vals, + 'invoice_payment_ref': move.name, + 'currency_id': self.currency_data['currency'].id, + 'date': frozen_today, + 'invoice_date': frozen_today, + 'invoice_date_due': frozen_today, + 'amount_tax': 230.0, + 'amount_total': 1430.0, + }) diff --git a/addons/account/tests/test_account_move_out_refund.py b/addons/account/tests/test_account_move_out_refund.py new file mode 100644 index 0000000000000..6d7c38d5ca3cf --- /dev/null +++ b/addons/account/tests/test_account_move_out_refund.py @@ -0,0 +1,939 @@ +# -*- coding: utf-8 -*- +from odoo.addons.account.tests.invoice_test_common import InvoiceTestCommon +from odoo.tests.common import Form +from odoo.tests import tagged +from odoo import fields + + +@tagged('post_install', '-at_install') +class TestAccountMoveOutRefundOnchanges(InvoiceTestCommon): + + @classmethod + def setUpClass(cls): + super(TestAccountMoveOutRefundOnchanges, cls).setUpClass() + + cls.invoice = cls.init_invoice('out_refund') + + cls.product_line_vals_1 = { + 'name': cls.product_a.name, + 'product_id': cls.product_a.id, + 'account_id': cls.product_a.property_account_income_id.id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': cls.product_a.uom_id.id, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 1000.0, + 'price_subtotal': 1000.0, + 'price_total': 1150.0, + 'tax_ids': cls.product_a.taxes_id.ids, + 'tax_line_id': False, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 1000.0, + 'credit': 0.0, + 'date_maturity': False, + 'tax_exigible': True, + } + cls.product_line_vals_2 = { + 'name': cls.product_b.name, + 'product_id': cls.product_b.id, + 'account_id': cls.product_b.property_account_income_id.id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': cls.product_b.uom_id.id, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 200.0, + 'price_subtotal': 200.0, + 'price_total': 260.0, + 'tax_ids': cls.product_b.taxes_id.ids, + 'tax_line_id': False, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 200.0, + 'credit': 0.0, + 'date_maturity': False, + 'tax_exigible': True, + } + cls.tax_line_vals_1 = { + 'name': cls.tax_sale_a.name, + 'product_id': False, + 'account_id': cls.company_data['default_account_tax_sale'].id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 180.0, + 'price_subtotal': 180.0, + 'price_total': 180.0, + 'tax_ids': [], + 'tax_line_id': cls.tax_sale_a.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 180.0, + 'credit': 0.0, + 'date_maturity': False, + 'tax_exigible': True, + } + cls.tax_line_vals_2 = { + 'name': cls.tax_sale_b.name, + 'product_id': False, + 'account_id': cls.company_data['default_account_tax_sale'].id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 30.0, + 'price_subtotal': 30.0, + 'price_total': 30.0, + 'tax_ids': [], + 'tax_line_id': cls.tax_sale_b.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 30.0, + 'credit': 0.0, + 'date_maturity': False, + 'tax_exigible': True, + } + cls.term_line_vals_1 = { + 'name': '', + 'product_id': False, + 'account_id': cls.company_data['default_account_receivable'].id, + 'partner_id': cls.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': -1410.0, + 'price_subtotal': -1410.0, + 'price_total': -1410.0, + 'tax_ids': [], + 'tax_line_id': False, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 1410.0, + 'date_maturity': fields.Date.from_string('2019-01-01'), + 'tax_exigible': True, + } + cls.move_vals = { + 'partner_id': cls.partner_a.id, + 'currency_id': cls.company_data['currency'].id, + 'journal_id': cls.company_data['default_journal_sale'].id, + 'date': fields.Date.from_string('2019-01-01'), + 'fiscal_position_id': False, + 'invoice_payment_ref': '', + 'invoice_payment_term_id': cls.pay_terms_a.id, + 'amount_untaxed': 1200.0, + 'amount_tax': 210.0, + 'amount_total': 1410.0, + } + + def setUp(self): + super(TestAccountMoveOutRefundOnchanges, self).setUp() + self.assertInvoiceValues(self.invoice, [ + self.product_line_vals_1, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + self.term_line_vals_1, + ], self.move_vals) + + def test_out_refund_line_onchange_product_1(self): + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + line_form.product_id = self.product_b + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'name': self.product_b.name, + 'product_id': self.product_b.id, + 'product_uom_id': self.product_b.uom_id.id, + 'account_id': self.product_b.property_account_income_id.id, + 'price_unit': 200.0, + 'price_subtotal': 200.0, + 'price_total': 260.0, + 'tax_ids': self.product_b.taxes_id.ids, + 'debit': 200.0, + }, + self.product_line_vals_2, + { + **self.tax_line_vals_1, + 'price_unit': 60.0, + 'price_subtotal': 60.0, + 'price_total': 60.0, + 'debit': 60.0, + }, + { + **self.tax_line_vals_2, + 'price_unit': 60.0, + 'price_subtotal': 60.0, + 'price_total': 60.0, + 'debit': 60.0, + }, + { + **self.term_line_vals_1, + 'price_unit': -520.0, + 'price_subtotal': -520.0, + 'price_total': -520.0, + 'credit': 520.0, + }, + ], { + **self.move_vals, + 'amount_untaxed': 400.0, + 'amount_tax': 120.0, + 'amount_total': 520.0, + }) + + def test_out_refund_line_onchange_business_fields_1(self): + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + # Current price_unit is 1000. + # We set quantity = 4, discount = 50%, price_unit = 500 because (4 * 500) * 0.5 = 1000. + line_form.quantity = 4 + line_form.discount = 50 + line_form.price_unit = 500 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'quantity': 4, + 'discount': 50.0, + 'price_unit': 500.0, + }, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + self.term_line_vals_1, + ], self.move_vals) + + move_form = Form(self.invoice) + with move_form.line_ids.edit(2) as line_form: + # Reset field except the discount that becomes 100%. + # /!\ The modification is made on the accounting tab. + line_form.quantity = 1 + line_form.discount = 100 + line_form.price_unit = 1000 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'discount': 100.0, + 'price_subtotal': 0.0, + 'price_total': 0.0, + 'debit': 0.0, + }, + self.product_line_vals_2, + { + **self.tax_line_vals_1, + 'price_unit': 30.0, + 'price_subtotal': 30.0, + 'price_total': 30.0, + 'debit': 30.0, + }, + self.tax_line_vals_2, + { + **self.term_line_vals_1, + 'price_unit': -260.0, + 'price_subtotal': -260.0, + 'price_total': -260.0, + 'credit': 260.0, + }, + ], { + **self.move_vals, + 'amount_untaxed': 200.0, + 'amount_tax': 60.0, + 'amount_total': 260.0, + }) + + def test_out_refund_line_onchange_accounting_fields_1(self): + move_form = Form(self.invoice) + with move_form.line_ids.edit(2) as line_form: + # Custom debit on the first product line. + line_form.debit = 3000 + with move_form.line_ids.edit(3) as line_form: + # Custom credit on the second product line. Credit should be reset by onchange. + # /!\ It's a negative line. + line_form.credit = 500 + with move_form.line_ids.edit(0) as line_form: + # Custom debit on the first tax line. + line_form.debit = 800 + with move_form.line_ids.edit(4) as line_form: + # Custom debit on the second tax line. + line_form.debit = 250 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'price_unit': 3000.0, + 'price_subtotal': 3000.0, + 'price_total': 3450.0, + 'debit': 3000.0, + }, + { + **self.product_line_vals_2, + 'price_unit': -500.0, + 'price_subtotal': -500.0, + 'price_total': -650.0, + 'debit': 0.0, + 'credit': 500.0, + }, + { + **self.tax_line_vals_1, + 'price_unit': 800.0, + 'price_subtotal': 800.0, + 'price_total': 800.0, + 'debit': 800.0, + }, + { + **self.tax_line_vals_2, + 'price_unit': 250.0, + 'price_subtotal': 250.0, + 'price_total': 250.0, + 'debit': 250.0, + }, + { + **self.term_line_vals_1, + 'price_unit': -3550.0, + 'price_subtotal': -3550.0, + 'price_total': -3550.0, + 'credit': 3550.0, + }, + ], { + **self.move_vals, + 'amount_untaxed': 2500.0, + 'amount_tax': 1050.0, + 'amount_total': 3550.0, + }) + + def test_out_refund_line_onchange_partner_1(self): + move_form = Form(self.invoice) + move_form.partner_id = self.partner_b + move_form.invoice_payment_ref = 'turlututu' + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'partner_id': self.partner_b.id, + }, + { + **self.product_line_vals_2, + 'partner_id': self.partner_b.id, + }, + { + **self.tax_line_vals_1, + 'partner_id': self.partner_b.id, + }, + { + **self.tax_line_vals_2, + 'partner_id': self.partner_b.id, + }, + { + **self.term_line_vals_1, + 'name': 'turlututu', + 'partner_id': self.partner_b.id, + 'price_unit': -987.0, + 'price_subtotal': -987.0, + 'price_total': -987.0, + 'credit': 987.0, + 'date_maturity': fields.Date.from_string('2019-02-28'), + }, + { + **self.term_line_vals_1, + 'name': 'turlututu', + 'partner_id': self.partner_b.id, + 'price_unit': -423.0, + 'price_subtotal': -423.0, + 'price_total': -423.0, + 'credit': 423.0, + }, + ], { + **self.move_vals, + 'partner_id': self.partner_b.id, + 'invoice_payment_ref': 'turlututu', + 'fiscal_position_id': self.fiscal_pos_a.id, + 'invoice_payment_term_id': self.pay_terms_b.id, + 'amount_untaxed': 1200.0, + 'amount_tax': 210.0, + 'amount_total': 1410.0, + }) + + # Remove lines and recreate them to apply the fiscal position. + move_form = Form(self.invoice) + move_form.invoice_line_ids.remove(0) + move_form.invoice_line_ids.remove(0) + with move_form.invoice_line_ids.new() as line_form: + line_form.product_id = self.product_a + with move_form.invoice_line_ids.new() as line_form: + line_form.product_id = self.product_b + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'account_id': self.product_b.property_account_income_id.id, + 'partner_id': self.partner_b.id, + 'tax_ids': self.tax_sale_b.ids, + }, + { + **self.product_line_vals_2, + 'partner_id': self.partner_b.id, + 'price_total': 230.0, + 'tax_ids': self.tax_sale_b.ids, + }, + { + **self.tax_line_vals_1, + 'name': self.tax_sale_b.name, + 'partner_id': self.partner_b.id, + 'tax_line_id': self.tax_sale_b.id, + }, + { + **self.term_line_vals_1, + 'name': 'turlututu', + 'account_id': self.partner_b.property_account_receivable_id.id, + 'partner_id': self.partner_b.id, + 'price_unit': -966.0, + 'price_subtotal': -966.0, + 'price_total': -966.0, + 'credit': 966.0, + 'date_maturity': fields.Date.from_string('2019-02-28'), + }, + { + **self.term_line_vals_1, + 'name': 'turlututu', + 'account_id': self.partner_b.property_account_receivable_id.id, + 'partner_id': self.partner_b.id, + 'price_unit': -414.0, + 'price_subtotal': -414.0, + 'price_total': -414.0, + 'credit': 414.0, + }, + ], { + **self.move_vals, + 'partner_id': self.partner_b.id, + 'invoice_payment_ref': 'turlututu', + 'fiscal_position_id': self.fiscal_pos_a.id, + 'invoice_payment_term_id': self.pay_terms_b.id, + 'amount_untaxed': 1200.0, + 'amount_tax': 180.0, + 'amount_total': 1380.0, + }) + + def test_out_refund_line_onchange_taxes_1(self): + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + line_form.price_unit = 1200 + line_form.tax_ids.add(self.tax_armageddon) + move_form.save() + + child_tax_1 = self.tax_armageddon.children_tax_ids[0] + child_tax_2 = self.tax_armageddon.children_tax_ids[1] + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'price_unit': 1200.0, + 'price_subtotal': 1000.0, + 'price_total': 1470.0, + 'tax_ids': (self.tax_sale_a + self.tax_armageddon).ids, + 'tax_exigible': False, + }, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + { + 'name': child_tax_1.name, + 'product_id': False, + 'account_id': self.company_data['default_account_tax_sale'].id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 80.0, + 'price_subtotal': 80.0, + 'price_total': 88.0, + 'tax_ids': child_tax_2.ids, + 'tax_line_id': child_tax_1.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 80.0, + 'credit': 0.0, + 'date_maturity': False, + 'tax_exigible': True, + }, + { + 'name': child_tax_1.name, + 'product_id': False, + 'account_id': self.company_data['default_account_revenue'].id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 120.0, + 'price_subtotal': 120.0, + 'price_total': 132.0, + 'tax_ids': child_tax_2.ids, + 'tax_line_id': child_tax_1.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 120.0, + 'credit': 0.0, + 'date_maturity': False, + 'tax_exigible': True, + }, + { + 'name': child_tax_2.name, + 'product_id': False, + 'account_id': child_tax_2.cash_basis_transition_account_id.id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 120.0, + 'price_subtotal': 120.0, + 'price_total': 120.0, + 'tax_ids': [], + 'tax_line_id': child_tax_2.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 120.0, + 'credit': 0.0, + 'date_maturity': False, + 'tax_exigible': False, + }, + { + **self.term_line_vals_1, + 'price_unit': -1730.0, + 'price_subtotal': -1730.0, + 'price_total': -1730.0, + 'credit': 1730.0, + }, + ], { + **self.move_vals, + 'amount_untaxed': 1200.0, + 'amount_tax': 530.0, + 'amount_total': 1730.0, + }) + + def test_out_refund_line_onchange_cash_rounding_1(self): + move_form = Form(self.invoice) + # Add a cash rounding having 'add_invoice_line'. + move_form.invoice_cash_rounding_id = self.cash_rounding_a + move_form.save() + + # The cash rounding does nothing as the total is already rounded. + self.assertInvoiceValues(self.invoice, [ + self.product_line_vals_1, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + self.term_line_vals_1, + ], self.move_vals) + + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + line_form.price_unit = 999.99 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + 'name': 'add_invoice_line', + 'product_id': False, + 'account_id': self.cash_rounding_a.account_id.id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': 0.01, + 'price_subtotal': 0.01, + 'price_total': 0.01, + 'tax_ids': [], + 'tax_line_id': False, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.01, + 'credit': 0.0, + 'date_maturity': False, + 'tax_exigible': True, + }, + { + **self.product_line_vals_1, + 'price_unit': 999.99, + 'price_subtotal': 999.99, + 'price_total': 1149.99, + 'debit': 999.99, + }, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + self.term_line_vals_1, + ], self.move_vals) + + move_form = Form(self.invoice) + # Change the cash rounding to one having 'biggest_tax'. + move_form.invoice_cash_rounding_id = self.cash_rounding_b + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'price_unit': 999.99, + 'price_subtotal': 999.99, + 'price_total': 1149.99, + 'debit': 999.99, + }, + self.product_line_vals_2, + self.tax_line_vals_1, + self.tax_line_vals_2, + { + 'name': '%s (rounding)' % self.tax_sale_a.name, + 'product_id': False, + 'account_id': self.company_data['default_account_tax_sale'].id, + 'partner_id': self.partner_a.id, + 'product_uom_id': False, + 'quantity': 1.0, + 'discount': 0.0, + 'price_unit': -0.04, + 'price_subtotal': -0.04, + 'price_total': -0.04, + 'tax_ids': [], + 'tax_line_id': self.tax_sale_a.id, + 'currency_id': False, + 'amount_currency': 0.0, + 'debit': 0.0, + 'credit': 0.04, + 'date_maturity': False, + 'tax_exigible': True, + }, + { + **self.term_line_vals_1, + 'price_unit': -1409.95, + 'price_subtotal': -1409.95, + 'price_total': -1409.95, + 'credit': 1409.95, + }, + ], { + **self.move_vals, + 'amount_untaxed': 1199.99, + 'amount_tax': 209.96, + 'amount_total': 1409.95, + }) + + def test_out_refund_line_onchange_currency_1(self): + # New journal having a foreign currency set. + journal = self.company_data['default_journal_sale'].copy() + journal.currency_id = self.currency_data['currency'] + + move_form = Form(self.invoice) + move_form.journal_id = journal + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': 1000.0, + 'debit': 500.0, + }, + { + **self.product_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': 200.0, + 'debit': 100.0, + }, + { + **self.tax_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': 180.0, + 'debit': 90.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': 30.0, + 'debit': 15.0, + }, + { + **self.term_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': -1410.0, + 'credit': 705.0, + }, + ], { + **self.move_vals, + 'currency_id': journal.currency_id.id, + 'journal_id': journal.id, + }) + + move_form = Form(self.invoice) + # Change the date to get another rate: 1/3 instead of 1/2. + move_form.date = fields.Date.from_string('2016-01-01') + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': 1000.0, + 'debit': 333.33, + }, + { + **self.product_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': 200.0, + 'debit': 66.67, + }, + { + **self.tax_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': 180.0, + 'debit': 60.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': 30.0, + 'debit': 10.0, + }, + { + **self.term_line_vals_1, + 'currency_id': journal.currency_id.id, + 'amount_currency': -1410.0, + 'credit': 470.0, + }, + ], { + **self.move_vals, + 'currency_id': journal.currency_id.id, + 'journal_id': journal.id, + 'date': fields.Date.from_string('2016-01-01'), + }) + + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + # 0.045 * 0.1 = 0.0045. As the foreign currency has a 0.001 rounding, + # the result should be 0.005 after rounding. + line_form.quantity = 0.1 + line_form.price_unit = 0.045 + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'quantity': 0.1, + 'price_unit': 0.05, + 'price_subtotal': 0.005, + 'price_total': 0.006, + 'currency_id': journal.currency_id.id, + 'amount_currency': 0.005, + 'debit': 0.0, + }, + { + **self.product_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': 200.0, + 'debit': 66.67, + }, + { + **self.tax_line_vals_1, + 'price_unit': 30.0, + 'price_subtotal': 30.001, + 'price_total': 30.001, + 'currency_id': journal.currency_id.id, + 'amount_currency': 30.001, + 'debit': 10.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': journal.currency_id.id, + 'amount_currency': 30.0, + 'debit': 10.0, + }, + { + **self.term_line_vals_1, + 'currency_id': journal.currency_id.id, + 'price_unit': -260.01, + 'price_subtotal': -260.006, + 'price_total': -260.006, + 'amount_currency': -260.006, + 'credit': 86.67, + }, + ], { + **self.move_vals, + 'currency_id': journal.currency_id.id, + 'journal_id': journal.id, + 'date': fields.Date.from_string('2016-01-01'), + 'amount_untaxed': 200.005, + 'amount_tax': 60.001, + 'amount_total': 260.006, + }) + + move_form = Form(self.invoice) + move_form.currency_id = self.company_data['currency'] + move_form.save() + + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'quantity': 0.1, + 'price_unit': 0.1, + 'price_subtotal': 0.01, + 'price_total': 0.01, + 'debit': 0.01, + }, + self.product_line_vals_2, + { + **self.tax_line_vals_1, + 'price_unit': 30.0, + 'price_subtotal': 30.0, + 'price_total': 30.0, + 'debit': 30.0, + }, + self.tax_line_vals_2, + { + **self.term_line_vals_1, + 'price_unit': -260.01, + 'price_subtotal': -260.01, + 'price_total': -260.01, + 'credit': 260.01, + }, + ], { + **self.move_vals, + 'currency_id': self.company_data['currency'].id, + 'journal_id': journal.id, + 'date': fields.Date.from_string('2016-01-01'), + 'amount_untaxed': 200.01, + 'amount_tax': 60.0, + 'amount_total': 260.01, + }) + + def test_out_refund_line_onchange_sequence_number_1(self): + self.assertRecordValues(self.invoice, [{ + 'invoice_sequence_number_next': '0001', + 'invoice_sequence_number_next_prefix': 'INV/2019/', + }]) + + move_form = Form(self.invoice) + move_form.invoice_sequence_number_next = '0042' + move_form.save() + + self.assertRecordValues(self.invoice, [{ + 'invoice_sequence_number_next': '0042', + 'invoice_sequence_number_next_prefix': 'INV/2019/', + }]) + + self.invoice.post() + + self.assertRecordValues(self.invoice, [{'name': 'INV/2019/0042'}]) + + invoice_copy = self.invoice.copy() + invoice_copy.post() + + self.assertRecordValues(invoice_copy, [{'name': 'INV/2019/0043'}]) + + def test_out_refund_create_1(self): + # Test creating an account_move with the least information. + move = self.env['account.move'].create({ + 'type': 'out_refund', + 'partner_id': self.partner_a.id, + 'invoice_date': fields.Date.from_string('2019-01-01'), + 'currency_id': self.currency_data['currency'].id, + 'invoice_payment_term_id': self.pay_terms_a.id, + 'invoice_line_ids': [ + (0, None, self.product_line_vals_1), + (0, None, self.product_line_vals_2), + ] + }) + + self.assertInvoiceValues(move, [ + { + **self.product_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 1000.0, + 'debit': 500.0, + }, + { + **self.product_line_vals_2, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 200.0, + 'debit': 100.0, + }, + { + **self.tax_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 180.0, + 'debit': 90.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 30.0, + 'debit': 15.0, + }, + { + **self.term_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -1410.0, + 'credit': 705.0, + }, + ], { + **self.move_vals, + 'currency_id': self.currency_data['currency'].id, + }) + + def test_out_refund_write_1(self): + # Test creating an account_move with the least information. + move = self.env['account.move'].create({ + 'type': 'out_refund', + 'partner_id': self.partner_a.id, + 'invoice_date': fields.Date.from_string('2019-01-01'), + 'currency_id': self.currency_data['currency'].id, + 'invoice_payment_term_id': self.pay_terms_a.id, + 'invoice_line_ids': [ + (0, None, self.product_line_vals_1), + ] + }) + move.write({ + 'invoice_line_ids': [ + (0, None, self.product_line_vals_2), + ] + }) + + self.assertInvoiceValues(move, [ + { + **self.product_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 1000.0, + 'debit': 500.0, + }, + { + **self.product_line_vals_2, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 200.0, + 'debit': 100.0, + }, + { + **self.tax_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 180.0, + 'debit': 90.0, + }, + { + **self.tax_line_vals_2, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': 30.0, + 'debit': 15.0, + }, + { + **self.term_line_vals_1, + 'currency_id': self.currency_data['currency'].id, + 'amount_currency': -1410.0, + 'credit': 705.0, + }, + ], { + **self.move_vals, + 'currency_id': self.currency_data['currency'].id, + }) diff --git a/addons/account/tests/test_account_move_tax_lock_date.py b/addons/account/tests/test_account_move_tax_lock_date.py deleted file mode 100644 index 7afd4b7d0dbba..0000000000000 --- a/addons/account/tests/test_account_move_tax_lock_date.py +++ /dev/null @@ -1,116 +0,0 @@ -from odoo.addons.account.tests.account_test_classes import AccountingTestCase -from odoo.exceptions import ValidationError, UserError -from datetime import datetime, timedelta -from dateutil.relativedelta import relativedelta -from calendar import monthrange -from odoo.tools import DEFAULT_SERVER_DATE_FORMAT -from odoo.tests import tagged - - -@tagged('post_install', '-at_install') -class TestTaxBlockDate(AccountingTestCase): - """ - Forbid creation, edition and deletion of Journal Items related to taxes with - a date prior to the Tax Block Date. - """ - - def setUp(self): - super(TestTaxBlockDate, self).setUp() - self.user_id = self.env.user - company_id = self.user_id.company_id.id - - last_day_month = datetime.now() - last_day_month = last_day_month.replace(day=monthrange(last_day_month.year, last_day_month.month)[1]) - self.last_day_month_str = last_day_month.strftime(DEFAULT_SERVER_DATE_FORMAT) - first_day_month = datetime.now() - first_day_month = first_day_month.replace(day=1) - self.first_day_month_str = first_day_month.strftime(DEFAULT_SERVER_DATE_FORMAT) - middle_day_month = datetime.now() - middle_day_month = middle_day_month.replace(day=15) - self.middle_day_month_str = middle_day_month.strftime(DEFAULT_SERVER_DATE_FORMAT) - - self.sale_journal_id = self.env['account.journal'].search([('type', '=', 'sale'), ('company_id', '=', company_id)], limit=1)[0] - self.account_id = self.env['account.account'].search([('internal_type', '=', 'receivable'), ('company_id', '=', company_id)], limit=1)[0] - self.other_account_id = self.env['account.account'].search([('internal_type', '!=', 'receivable'), ('company_id', '=', company_id)], limit=1)[0] - self.tax_id = self.env['account.tax'].search([('company_id', '=', company_id)], limit=1)[0] - - self.move = { - 'name': '/', - 'journal_id': self.sale_journal_id.id, - 'date': self.middle_day_month_str, - 'line_ids': [(0, 0, { - 'name': 'foo', - 'debit': 10, - 'account_id': self.account_id.id, - 'tax_ids': [(6, False, [self.tax_id.id])] - }), (0, 0, { - 'name': 'bar', - 'credit': 10, - 'account_id': self.account_id.id, - })] - } - - def test_create_before_block_date(self): - """ - Checks that you cannot create an account.move with a date before the tax - lock date - """ - self.user_id.company_id.tax_lock_date = self.last_day_month_str - with self.assertRaises(ValidationError): - move = self.env['account.move'].create(self.move) - move.post() - - def test_change_after_block_date(self): - """ - Checks that you can change an account.move with a date after the tax - lock date - """ - self.user_id.company_id.tax_lock_date = self.first_day_month_str - move = self.env['account.move'].create(self.move) - move.line_ids[0].write({'account_id': self.other_account_id.id}) - move.line_ids[1].write({'account_id': self.other_account_id.id}) - move.line_ids[1].write({'debit': 11}) - move.line_ids[0].write({'credit': 11}) - move.post() - move.line_ids[1].write({'tax_ids': [(5, False, False)]}) - - def test_change_before_block_date(self): - """ - Checks that you cannot change an account.move with a date before the tax - lock date - """ - self.user_id.company_id.tax_lock_date = self.first_day_month_str - move = self.env['account.move'].create(self.move) - self.user_id.company_id.tax_lock_date = self.last_day_month_str - move.line_ids[0].write({'account_id': self.other_account_id.id}) - move.line_ids[1].write({'account_id': self.other_account_id.id}) - with self.assertRaises(ValidationError): - with self.cr.savepoint(): - move.line_ids[1].write({'debit': 11}) - with self.assertRaises(ValidationError): - with self.cr.savepoint(): - move.line_ids[1].write({'date': self.last_day_month_str, 'tax_ids': [(5, False, False)]}) - move.line_ids[0].write({'credit': 10}) - move.post() - - def test_unlink_before_block_date(self): - """ - Checks that you cannot unlink an account.move with a date before the tax - lock date - """ - self.user_id.company_id.tax_lock_date = self.first_day_month_str - move = self.env['account.move'].create(self.move) - move.post() - self.user_id.company_id.tax_lock_date = self.last_day_month_str - with self.assertRaises(ValidationError): - move.unlink() - - def test_unlink_after_block_date(self): - """ - Checks that you can unlink an account.move with a date after the lock - date - """ - self.user_id.company_id.tax_lock_date = self.first_day_month_str - move = self.env['account.move'].create(self.move) - move.post() - move.unlink() diff --git a/addons/account/tests/test_account_move_taxes_edition.py b/addons/account/tests/test_account_move_taxes_edition.py deleted file mode 100644 index df17788266c30..0000000000000 --- a/addons/account/tests/test_account_move_taxes_edition.py +++ /dev/null @@ -1,192 +0,0 @@ -# -*- coding: utf-8 -*- -from odoo.addons.account.tests.account_test_classes import AccountingTestCase -from odoo.tests import tagged -from odoo.tests.common import Form - - -@tagged('post_install', '-at_install') -class TestAccountMoveTaxesEdition(AccountingTestCase): - - def setUp(self): - super(AccountingTestCase, self).setUp() - self.percent_tax = self.env['account.tax'].create({ - 'name': 'tax_line', - 'amount_type': 'percent', - 'amount': 10, - }) - self.account = self.env['account.account'].search([('deprecated', '=', False)], limit=1) - self.journal = self.env['account.journal'].search([], limit=1) - - def test_onchange_taxes_1(self): - ''' - Test an account.move.line is created automatically when adding a tax. - This test uses the following scenario: - - Create manually a debit line of 1000 having a tax. - - Assume a line containing the tax amount is created automatically. - - Create manually a credit line to balance the two previous lines. - - Save the move. - - tax = 10% - - Name | Debit | Credit | Tax_ids | Tax_line_id's name - ----------------|-----------|-----------|---------------|------------------- - debit_line_1 | 1000 | | tax | - tax_line | 100 | | | tax_line - debit_line_1 | | 1100 | | - ''' - move_form = Form(self.env['account.move'], view='account.view_move_form') - move_form.ref = 'azerty' - move_form.journal_id = self.journal - - # Create a new account.move.line with debit amount. - with move_form.line_ids.new() as debit_line: - debit_line.name = 'debit_line_1' - debit_line.account_id = self.account - debit_line.debit = 1000 - debit_line.tax_ids.clear() - debit_line.tax_ids.add(self.percent_tax) - - self.assertTrue(debit_line.recompute_tax_line) - - # Create a third account.move.line with credit amount. - with move_form.line_ids.new() as credit_line: - credit_line.name = 'credit_line_1' - credit_line.account_id = self.account - credit_line.credit = 1100 - - move = move_form.save() - - self.assertRecordValues(move.line_ids, [ - {'name': 'credit_line_1', 'debit': 0.0, 'credit': 1100.0, 'tax_ids': [], 'tax_line_id': False}, - {'name': 'tax_line', 'debit': 100.0, 'credit': 0.0, 'tax_ids': [], 'tax_line_id': self.percent_tax.id}, - {'name': 'debit_line_1', 'debit': 1000.0, 'credit': 0.0, 'tax_ids': [self.percent_tax.id], 'tax_line_id': False}, - ]) - - def test_onchange_taxes_2(self): - ''' - Test the amount of tax account.move.line is adapted when editing the account.move.line amount. - This test uses the following scenario: - - Create manually a debit line of 1000 having a tax. - - Assume a line containing the tax amount is created automatically. - - Set the debit amount to 2000 in the first created line. - - Assume the line containing the tax amount has been updated automatically. - - Create manually a credit line to balance the two previous lines. - - Save the move. - - tax = 10% - - Name | Debit | Credit | Tax_ids | Tax_line_id's name - ----------------|-----------|-----------|---------------|------------------- - debit_line_1 | 2000 | | tax | - tax_line | 200 | | | tax_line - debit_line_1 | | 2200 | | - ''' - move_form = Form(self.env['account.move'], view='account.view_move_form') - move_form.ref = 'azerty' - move_form.journal_id = self.journal - - # Create a new account.move.line with debit amount. - with move_form.line_ids.new() as debit_line: - debit_line.name = 'debit_line_1' - debit_line.account_id = self.account - debit_line.debit = 1000 - debit_line.tax_ids.clear() - debit_line.tax_ids.add(self.percent_tax) - - self.assertTrue(debit_line.recompute_tax_line) - - debit_line.debit = 2000 - - self.assertTrue(debit_line.recompute_tax_line) - - # Create a third account.move.line with credit amount. - with move_form.line_ids.new() as credit_line: - credit_line.name = 'credit_line_1' - credit_line.account_id = self.account - credit_line.credit = 2200 - - move = move_form.save() - - self.assertRecordValues(move.line_ids, [ - {'name': 'credit_line_1', 'debit': 0.0, 'credit': 2200.0, 'tax_ids': [], 'tax_line_id': False}, - {'name': 'tax_line', 'debit': 200.0, 'credit': 0.0, 'tax_ids': [], 'tax_line_id': self.percent_tax.id}, - {'name': 'debit_line_1', 'debit': 2000.0, 'credit': 0.0, 'tax_ids': [self.percent_tax.id], 'tax_line_id': False}, - ]) - - def test_onchange_taxes_3(self): - ''' - Test the amount of tax account.move.line is still editable manually. - Test the amount of tax account.move.line is cumulative for the same tax. - This test uses the following scenario: - - Create manually a debit line of 1000 having a tax. - - Assume a line containing the tax amount is created automatically. - - Edit the tax line amount of the auto-generated line by adding 5. - - Create manually a credit line to balance the two previous lines. - - Save the move. - - Edit the move. - - Create manually a debit line of 2000 having the same tax. - - Assume the line containing the tax amount has been updated (no new line created). - - Create manually a credit line to balance the four previous lines. - - Save the move. - - tax = 10% - - Name | Debit | Credit | Tax_ids | Tax_line_id's name - ----------------|-----------|-----------|---------------|------------------- - debit_line_1 | 1000 | | tax | - tax_line | 300 | | | tax_line - credit_line_1 | | 1105 | | - debit_line_2 | 2000 | | tax | - credit_line_2 | | 2195 | | - ''' - move_form = Form(self.env['account.move'], view='account.view_move_form') - move_form.ref = 'azerty' - move_form.journal_id = self.journal - - # Create a new account.move.line with debit amount. - with move_form.line_ids.new() as debit_line: - debit_line.name = 'debit_line_1' - debit_line.account_id = self.account - debit_line.debit = 1000 - debit_line.tax_ids.clear() - debit_line.tax_ids.add(self.percent_tax) - - self.assertTrue(debit_line.recompute_tax_line) - - # Edit the tax account.move.line - with move_form.line_ids.edit(index=1) as tax_line: - tax_line.debit = 105 # Was 100 - - # Create a third account.move.line with credit amount. - with move_form.line_ids.new() as credit_line: - credit_line.name = 'credit_line_1' - credit_line.account_id = self.account - credit_line.credit = 1105 - - move = move_form.save() - - move_form = Form(move, view='account.view_move_form') - # Create a new account.move.line with debit amount. - with move_form.line_ids.new() as debit_line2: - debit_line2.name = 'debit_line_2' - debit_line2.account_id = self.account - debit_line2.debit = 2000 - debit_line2.tax_ids.clear() - debit_line2.tax_ids.add(self.percent_tax) - - self.assertTrue(debit_line2.recompute_tax_line) - - with move_form.line_ids.new() as credit_line2: - credit_line2.name = 'credit_line_2' - credit_line2.account_id = self.account - credit_line2.credit = 2195 - - move = move_form.save() - - self.assertRecordValues(move.line_ids, [ - {'name': 'credit_line_2', 'debit': 0.0, 'credit': 2195.0, 'tax_ids': [], 'tax_line_id': False}, - {'name': 'debit_line_2', 'debit': 2000.0, 'credit': 0.0, 'tax_ids': [self.percent_tax.id], 'tax_line_id': False}, - {'name': 'credit_line_1', 'debit': 0.0, 'credit': 1105.0, 'tax_ids': [], 'tax_line_id': False}, - {'name': 'tax_line', 'debit': 300.0, 'credit': 0.0, 'tax_ids': [], 'tax_line_id': self.percent_tax.id}, - {'name': 'debit_line_1', 'debit': 1000.0, 'credit': 0.0, 'tax_ids': [self.percent_tax.id], 'tax_line_id': False}, - ]) diff --git a/addons/account/tests/test_account_supplier_invoice.py b/addons/account/tests/test_account_supplier_invoice.py deleted file mode 100644 index 60979f81b1af9..0000000000000 --- a/addons/account/tests/test_account_supplier_invoice.py +++ /dev/null @@ -1,145 +0,0 @@ -from odoo.addons.account.tests.account_test_classes import AccountingTestCase -from odoo.tests import tagged -from odoo.exceptions import Warning - - -@tagged('post_install', '-at_install') -class TestAccountSupplierInvoice(AccountingTestCase): - - def test_supplier_invoice(self): - tax = self.env['account.tax'].create({ - 'name': 'Tax 10.0', - 'amount': 10.0, - 'amount_type': 'fixed', - }) - analytic_account = self.env['account.analytic.account'].create({ - 'name': 'test account', - }) - - # Should be changed by automatic on_change later - invoice_account = self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_receivable').id)], limit=1).id - invoice_line_account = self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_expenses').id)], limit=1).id - - invoice = self.env['account.invoice'].create({'partner_id': self.env.ref('base.res_partner_2').id, - 'account_id': invoice_account, - 'type': 'in_invoice', - }) - self.assertEquals(invoice.journal_id.type, 'purchase') - - self.env['account.invoice.line'].create({'product_id': self.env.ref('product.product_product_4').id, - 'quantity': 1.0, - 'price_unit': 100.0, - 'invoice_id': invoice.id, - 'name': 'product that cost 100', - 'account_id': invoice_line_account, - 'invoice_line_tax_ids': [(6, 0, [tax.id])], - 'account_analytic_id': analytic_account.id, - }) - - # check that Initially supplier bill state is "Draft" - self.assertTrue((invoice.state == 'draft'), "Initially vendor bill state is Draft") - - #change the state of invoice to open by clicking Validate button - invoice.action_invoice_open() - - #I cancel the account move which is in posted state and verifies that it gives warning message - with self.assertRaises(Warning): - invoice.move_id.button_cancel() - - def test_supplier_invoice2(self): - tax_fixed = self.env['account.tax'].create({ - 'sequence': 10, - 'name': 'Tax 10.0 (Fixed)', - 'amount': 10.0, - 'amount_type': 'fixed', - 'include_base_amount': True, - }) - tax_percent_included_base_incl = self.env['account.tax'].create({ - 'sequence': 20, - 'name': 'Tax 50.0% (Percentage of Price Tax Included)', - 'amount': 50.0, - 'amount_type': 'division', - 'include_base_amount': True, - }) - tax_percentage = self.env['account.tax'].create({ - 'sequence': 30, - 'name': 'Tax 20.0% (Percentage of Price)', - 'amount': 20.0, - 'amount_type': 'percent', - 'include_base_amount': False, - }) - analytic_account = self.env['account.analytic.account'].create({ - 'name': 'test account', - }) - - # Should be changed by automatic on_change later - invoice_account = self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_receivable').id)], limit=1).id - invoice_line_account = self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_expenses').id)], limit=1).id - - invoice = self.env['account.invoice'].create({'partner_id': self.env.ref('base.res_partner_2').id, - 'account_id': invoice_account, - 'type': 'in_invoice', - }) - self.assertEquals(invoice.journal_id.type, 'purchase') - - invoice_line = self.env['account.invoice.line'].create({'product_id': self.env.ref('product.product_product_4').id, - 'quantity': 5.0, - 'price_unit': 100.0, - 'invoice_id': invoice.id, - 'name': 'product that cost 100', - 'account_id': invoice_line_account, - 'invoice_line_tax_ids': [(6, 0, [tax_fixed.id, tax_percent_included_base_incl.id, tax_percentage.id])], - 'account_analytic_id': analytic_account.id, - }) - invoice.compute_taxes() - - # check that Initially supplier bill state is "Draft" - self.assertTrue((invoice.state == 'draft'), "Initially vendor bill state is Draft") - - #change the state of invoice to open by clicking Validate button - invoice.action_invoice_open() - - # Check if amount and corresponded base is correct for all tax scenarios given on a computational base - # Keep in mind that tax amount can be changed by the user at any time before validating (based on the invoice and tax laws applicable) - invoice_tax = invoice.tax_line_ids.sorted(key=lambda r: r.sequence) - self.assertEquals(invoice_tax.mapped('amount'), [50.0, 550.0, 220.0]) - self.assertEquals(invoice_tax.mapped('base'), [500.0, 550.0, 1100.0]) - - #I cancel the account move which is in posted state and verifies that it gives warning message - with self.assertRaises(Warning): - invoice.move_id.button_cancel() - - def test_vendor_bill_refund(self): - invoice_account = self.env['account.account'].search( - [('user_type_id', '=', self.env.ref('account.data_account_type_receivable').id)], limit=1) - invoice_line_account = self.env['account.account'].search( - [('user_type_id', '=', self.env.ref('account.data_account_type_expenses').id)], limit=1) - - if self.env.ref('base.main_partner').bank_account_count > 0: - bank = self.env['res.partner.bank'].search([('partner_id', '=', self.env.ref('base.main_partner').id)], limit=1) - - else: - bank = self.env['res.partner.bank'].create({ - 'acc_number': '12345678910', - 'partner_id': self.env.ref('base.main_partner').id, - }) - - invoice_id = self.env['account.invoice'].create({ - 'name': 'invoice test refund', - 'partner_id': self.env.ref("base.res_partner_2").id, - 'account_id': invoice_account.id, - 'currency_id': self.env.ref('base.USD').id, - 'type': 'in_invoice', - }) - self.env['account.invoice.line'].create({ - 'product_id': self.env.ref("product.product_product_4").id, - 'quantity': 1, - 'price_unit': 15.0, - 'invoice_id': invoice_id.id, - 'name': 'something', - 'account_id': invoice_line_account.id, - }) - - refund_invoices = invoice_id.refund() - - self.assertEqual(refund_invoices.partner_bank_id, bank) diff --git a/addons/account/tests/test_account_validate_account_move.py b/addons/account/tests/test_account_validate_account_move.py deleted file mode 100644 index dfc872d57fd65..0000000000000 --- a/addons/account/tests/test_account_validate_account_move.py +++ /dev/null @@ -1,44 +0,0 @@ -from odoo.addons.account.tests.account_test_classes import AccountingTestCase -from odoo.tests import tagged - - -@tagged('post_install', '-at_install') -class TestAccountValidateAccount(AccountingTestCase): - - def test_account_validate_account(self): - account_move_line = self.env['account.move.line'] - account_cash = self.env['account.account'].search([('user_type_id.type', '=', 'liquidity')], limit=1) - journal = self.env['account.journal'].search([('type', '=', 'bank')], limit=1) - - company_id = self.env['res.users'].browse(self.env.uid).company_id.id - - # create move - move = self.env['account.move'].create({'name': '/', - 'ref':'2011010', - 'journal_id': journal.id, - 'state':'draft', - 'company_id': company_id, - }) - # create move line - account_move_line.create({'account_id': account_cash.id, - 'name': 'Four Person Desk', - 'move_id': move.id, - }) - - # create another move line - account_move_line.create({'account_id': account_cash.id, - 'name': 'Four Person Desk', - 'move_id': move.id, - }) - - # check that Initially account move state is "Draft" - self.assertTrue((move.state == 'draft'), "Initially account move state is Draft") - - # validate this account move by using the 'Post Journal Entries' wizard - validate_account_move = self.env['validate.account.move'].with_context(active_ids=move.id).create({}) - - #click on validate Button - validate_account_move.with_context({'active_ids': [move.id]}).validate_move() - - #check that the move state is now "Posted" - self.assertTrue((move.state == 'posted'), "Initially account move state is Posted") diff --git a/addons/account/tests/test_bank_statement_reconciliation.py b/addons/account/tests/test_bank_statement_reconciliation.py index a0a5fe2e65b53..185b46542603f 100644 --- a/addons/account/tests/test_bank_statement_reconciliation.py +++ b/addons/account/tests/test_bank_statement_reconciliation.py @@ -7,8 +7,6 @@ class TestBankStatementReconciliation(AccountingTestCase): def setUp(self): super(TestBankStatementReconciliation, self).setUp() - self.i_model = self.env['account.invoice'] - self.il_model = self.env['account.invoice.line'] self.bs_model = self.env['account.bank.statement'] self.bsl_model = self.env['account.bank.statement.line'] self.reconciliation_widget = self.env['account.reconciliation.widget'] @@ -62,40 +60,24 @@ def _reconcile_invoice_with_statement(self, post_at_bank_rec): self.assertTrue(rcv_mv_line.reconciled) self.assertTrue(counterpart_mv_line.reconciled) self.assertEqual(counterpart_mv_line.matched_credit_ids, rcv_mv_line.matched_debit_ids) - self.assertEqual(rcv_mv_line.invoice_id.state, 'paid', "The related invoice's state should now be 'paid'") + self.assertEqual(rcv_mv_line.move_id.invoice_payment_state, 'paid', "The related invoice's state should now be 'paid'") def test_reconcile_with_write_off(self): pass def create_invoice(self, amount): """ Return the move line that gets to be reconciled (the one in the receivable account) """ - vals = {'partner_id': self.partner.id, - 'type': 'out_invoice', - 'name': '-', - 'currency_id': self.env.company.currency_id.id, - } - # new creates a temporary record to apply the on_change afterwards - invoice = self.i_model.new(vals) - invoice._onchange_partner_id() - vals.update({'account_id': invoice.account_id.id}) - invoice = self.i_model.create(vals) - - self.il_model.create({ - 'quantity': 1, - 'price_unit': amount, - 'invoice_id': invoice.id, - 'name': '.', - 'account_id': self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_revenue').id)], limit=1).id, + move = self.env['account.move'].create({ + 'type': 'out_invoice', + 'partner_id': self.partner.id, + 'invoice_line_ids': [(0, 0, { + 'quantity': 1, + 'price_unit': amount, + 'name': 'test invoice', + })], }) - invoice.action_invoice_open() - - mv_line = None - for l in invoice.move_id.line_ids: - if l.account_id.id == vals['account_id']: - mv_line = l - self.assertIsNotNone(mv_line) - - return mv_line + move.post() + return move.line_ids.filtered(lambda line: line.account_id.user_type_id.type in ('receivable', 'payable')) def create_statement_line(self, st_line_amount): journal = self.bs_model.with_context(journal_type='bank')._default_journal() diff --git a/addons/account/tests/test_invoice_onchange.py b/addons/account/tests/test_invoice_onchange.py deleted file mode 100644 index 3a1402de50314..0000000000000 --- a/addons/account/tests/test_invoice_onchange.py +++ /dev/null @@ -1,33 +0,0 @@ -from odoo.addons.account.tests.account_test_classes import AccountingTestCase -from odoo.tests import tagged -from odoo.tests.common import Form - - -@tagged('post_install', '-at_install') -class TestInvoiceOnchange(AccountingTestCase): - - def setUp(self): - super(TestInvoiceOnchange, self).setUp() - self.invoice_line_onchange = self.env['account.invoice.line']._onchange_spec() - self.half_currency = self.env['res.currency'].create({ - 'name': 'HALF', 'symbol': '$HALF', - 'rate_ids': [(0, 0, {'name': '1980-01-01', 'rate': 2})], - }) - self.apples_product = self.env['product.product'].create(dict( - self.env['product.product'].default_get(self.env['product.product']._fields), - lst_price=10, name='apples', - )) - - def test_invoice_currency_onchange(self): - self_ctx = self.env['account.invoice'].with_context(type='out_invoice') - with Form(self_ctx, view='account.invoice_form') as invoice_form: - invoice_form.partner_id = self.env.user.partner_id - with invoice_form.invoice_line_ids.new() as invoice_line_form: - invoice_line_form.product_id = self.apples_product - # Check onchange keep price_unit if currency not changed - self.assertEqual(invoice_line_form.price_unit, 10) - invoice_form.currency_id = self.half_currency - with invoice_form.invoice_line_ids.new() as invoice_line_form: - invoice_line_form.product_id = self.apples_product - # Check onchange gives converted price with custom currency - self.assertEqual(invoice_line_form.price_unit, 20) diff --git a/addons/account/tests/test_invoice_taxes.py b/addons/account/tests/test_invoice_taxes.py index dd2e8b52f15cc..6ff407a28921e 100644 --- a/addons/account/tests/test_invoice_taxes.py +++ b/addons/account/tests/test_invoice_taxes.py @@ -46,21 +46,18 @@ def _create_invoice(self, taxes_per_line, inv_type='out_invoice'): :param taxes_per_line: A list of tuple (price_unit, account.tax recordset) ''' - self_ctx = self.env['account.invoice'].with_context(type=inv_type) - journal_id = self_ctx._default_journal().id - self_ctx = self_ctx.with_context(journal_id=journal_id) - - with Form(self_ctx, view=(inv_type in ('out_invoice', 'out_refund') and 'account.invoice_form' or 'invoice_supplier_form')) as invoice_form: - invoice_form.partner_id = self.env.ref('base.partner_demo') - - for amount, taxes in taxes_per_line: - with invoice_form.invoice_line_ids.new() as invoice_line_form: - invoice_line_form.name = 'xxxx' - invoice_line_form.quantity = 1 - invoice_line_form.price_unit = amount - invoice_line_form.invoice_line_tax_ids.clear() - for tax in taxes: - invoice_line_form.invoice_line_tax_ids.add(tax) + self_ctx = self.env['account.move'].with_context(default_type=inv_type) + invoice_form = Form(self_ctx) + invoice_form.partner_id = self.env.ref('base.partner_demo') + + for amount, taxes in taxes_per_line: + with invoice_form.invoice_line_ids.new() as invoice_line_form: + invoice_line_form.name = 'xxxx' + invoice_line_form.quantity = 1 + invoice_line_form.price_unit = amount + invoice_line_form.tax_ids.clear() + for tax in taxes: + invoice_line_form.tax_ids.add(tax) return invoice_form.save() def test_one_tax_per_line(self): @@ -83,11 +80,11 @@ def test_one_tax_per_line(self): (121, self.percent_tax_1_incl), (100, self.percent_tax_2), ]) - invoice.action_invoice_open() - self.assertRecordValues(invoice.tax_line_ids, [ - {'name': self.percent_tax_1.name, 'base': 100, 'amount': 21, 'tax_ids': []}, - {'name': self.percent_tax_1_incl.name, 'base': 100, 'amount': 21, 'tax_ids': []}, - {'name': self.percent_tax_2.name, 'base': 100, 'amount': 12, 'tax_ids': []}, + invoice.post() + self.assertRecordValues(invoice.line_ids.filtered('tax_line_id'), [ + {'name': self.percent_tax_1.name, 'tax_base_amount': 100, 'price_unit': 21, 'tax_ids': []}, + {'name': self.percent_tax_1_incl.name, 'tax_base_amount': 100, 'price_unit': 21, 'tax_ids': []}, + {'name': self.percent_tax_2.name, 'tax_base_amount': 100, 'price_unit': 12, 'tax_ids': []}, ]) def test_affecting_base_amount(self): @@ -108,10 +105,10 @@ def test_affecting_base_amount(self): (121, self.percent_tax_1_incl + self.percent_tax_2), (100, self.percent_tax_2), ]) - invoice.action_invoice_open() - self.assertRecordValues(invoice.tax_line_ids.sorted(lambda x: x.amount), [ - {'name': self.percent_tax_1_incl.name, 'base': 100, 'amount': 21, 'tax_ids': [self.percent_tax_2.id]}, - {'name': self.percent_tax_2.name, 'base': 221, 'amount': 26.52, 'tax_ids': []}, + invoice.post() + self.assertRecordValues(invoice.line_ids.filtered('tax_line_id').sorted(lambda x: x.price_unit), [ + {'name': self.percent_tax_1_incl.name, 'tax_base_amount': 100, 'price_unit': 21, 'tax_ids': [self.percent_tax_2.id]}, + {'name': self.percent_tax_2.name, 'tax_base_amount': 221, 'price_unit': 26.52, 'tax_ids': []}, ]) def test_group_of_taxes(self): @@ -132,10 +129,10 @@ def test_group_of_taxes(self): (121, self.group_tax), (100, self.percent_tax_2), ]) - invoice.action_invoice_open() - self.assertRecordValues(invoice.tax_line_ids.sorted(lambda x: x.amount), [ - {'name': self.percent_tax_1_incl.name, 'base': 100, 'amount': 21, 'tax_ids': [self.percent_tax_2.id]}, - {'name': self.percent_tax_2.name, 'base': 221, 'amount': 26.52, 'tax_ids': []}, + invoice.post() + self.assertRecordValues(invoice.line_ids.filtered('tax_line_id').sorted(lambda x: x.price_unit), [ + {'name': self.percent_tax_1_incl.name, 'tax_base_amount': 100, 'price_unit': 21, 'tax_ids': [self.percent_tax_2.id]}, + {'name': self.percent_tax_2.name, 'tax_base_amount': 221, 'price_unit': 26.52, 'tax_ids': []}, ]) def _create_tax_tag(self, tag_name): @@ -205,15 +202,14 @@ def test_tax_repartition(self): # Test invoice repartition invoice = self._create_invoice([(100, tax)], inv_type='out_invoice') - invoice.action_invoice_open() - invoice_move = invoice.move_id + invoice.post() - self.assertEqual(len(invoice_move.line_ids), 4, "There should be 4 account move lines created for the invoice: payable, base and 2 tax lines") - inv_base_line = invoice_move.line_ids.filtered(lambda x: not x.tax_repartition_line_id and x.account_id.user_type_id.type != 'receivable') + self.assertEqual(len(invoice.line_ids), 4, "There should be 4 account move lines created for the invoice: payable, base and 2 tax lines") + inv_base_line = invoice.line_ids.filtered(lambda x: not x.tax_repartition_line_id and x.account_id.user_type_id.type != 'receivable') self.assertEqual(len(inv_base_line), 1, "There should be only one base line generated") self.assertEqual(abs(inv_base_line.balance), 100, "Base amount should be 100") self.assertEqual(inv_base_line.tag_ids, inv_base_tag, "Base line should have received base tag") - inv_tax_lines = invoice_move.line_ids.filtered(lambda x: x.tax_repartition_line_id.repartition_type == 'tax') + inv_tax_lines = invoice.line_ids.filtered(lambda x: x.tax_repartition_line_id.repartition_type == 'tax') self.assertEqual(len(inv_tax_lines), 2, "There should be two tax lines, one for each repartition line.") self.assertEqual(abs(inv_tax_lines.filtered(lambda x: x.account_id == account_1).balance), 4.2, "Tax line on account 1 should amount to 4.2 (10% of 42)") self.assertEqual(inv_tax_lines.filtered(lambda x: x.account_id == account_1).tag_ids, inv_tax_tag_10, "Tax line on account 1 should have 10% tag") @@ -222,15 +218,14 @@ def test_tax_repartition(self): # Test refund repartition refund = self._create_invoice([(100, tax)], inv_type='out_refund') - refund.action_invoice_open() - refund_move = refund.move_id + refund.post() - self.assertEqual(len(refund_move.line_ids), 4, "There should be 4 account move lines created for the refund: payable, base and 2 tax lines") - ref_base_line = refund_move.line_ids.filtered(lambda x: not x.tax_repartition_line_id and x.account_id.user_type_id.type != 'receivable') + self.assertEqual(len(refund.line_ids), 4, "There should be 4 account move lines created for the refund: payable, base and 2 tax lines") + ref_base_line = refund.line_ids.filtered(lambda x: not x.tax_repartition_line_id and x.account_id.user_type_id.type != 'receivable') self.assertEqual(len(ref_base_line), 1, "There should be only one base line generated") self.assertEqual(abs(ref_base_line.balance), 100, "Base amount should be 100") self.assertEqual(ref_base_line.tag_ids, ref_base_tag, "Base line should have received base tag") - ref_tax_lines = refund_move.line_ids.filtered(lambda x: x.tax_repartition_line_id.repartition_type == 'tax') + ref_tax_lines = refund.line_ids.filtered(lambda x: x.tax_repartition_line_id.repartition_type == 'tax') self.assertEqual(len(ref_tax_lines), 2, "There should be two refund tax lines") self.assertEqual(abs(ref_tax_lines.filtered(lambda x: x.account_id == ref_base_line.account_id).balance), 4.2, "Refund tax line on base account should amount to 4.2 (10% of 42)") self.assertEqual(abs(ref_tax_lines.filtered(lambda x: x.account_id == account_1).balance), 37.8, "Refund tax line on account 1 should amount to 37.8 (90% of 42)") diff --git a/addons/account/tests/test_payment.py b/addons/account/tests/test_payment.py index 1556bb1462843..fb83cc45a54b4 100644 --- a/addons/account/tests/test_payment.py +++ b/addons/account/tests/test_payment.py @@ -9,10 +9,8 @@ class TestPayment(AccountingTestCase): def setUp(self): super(TestPayment, self).setUp() - self.register_payments_model = self.env['account.payment.register'].with_context(active_model='account.invoice') + self.register_payments_model = self.env['account.payment.register'].with_context(active_model='account.move') self.payment_model = self.env['account.payment'] - self.invoice_model = self.env['account.invoice'] - self.invoice_line_model = self.env['account.invoice.line'] self.acc_bank_stmt_model = self.env['account.bank.statement'] self.acc_bank_stmt_line_model = self.env['account.bank.statement.line'] @@ -46,23 +44,17 @@ def setUp(self): def create_invoice(self, amount=100, type='out_invoice', currency_id=None, partner=None, account_id=None): """ Returns an open invoice """ - invoice = self.invoice_model.create({ + invoice = self.env['account.move'].create({ + 'type': type, 'partner_id': partner or self.partner_agrolait.id, 'currency_id': currency_id or self.currency_eur_id, - 'name': type, - 'account_id': account_id or self.account_receivable.id, - 'type': type, - 'date_invoice': time.strftime('%Y') + '-06-26', + 'invoice_date': time.strftime('%Y') + '-06-26', + 'date': time.strftime('%Y') + '-06-26', + 'invoice_line_ids': [ + (0, 0, {'product_id': self.product.id, 'quantity': 1, 'price_unit': amount}) + ], }) - self.invoice_line_model.create({ - 'product_id': self.product.id, - 'quantity': 1, - 'price_unit': amount, - 'invoice_id': invoice.id, - 'name': 'something', - 'account_id': self.account_revenue.id, - }) - invoice.action_invoice_open() + invoice.post() return invoice def reconcile(self, liquidity_aml, amount=0.0, amount_currency=0.0, currency_id=None): @@ -94,21 +86,21 @@ def test_full_payment_process(self): 'journal_id': self.bank_journal_euro.id, 'payment_method_id': self.payment_method_manual_in.id, }) - register_payments.create_payments() - payment = self.payment_model.search([], order="id desc", limit=1) + payment = self.payment_model.browse(register_payments.create_payments()['res_id']) self.assertAlmostEquals(payment.amount, 300) self.assertEqual(payment.state, 'posted') self.assertEqual(payment.state, 'posted') - self.assertEqual(inv_1.state, 'paid') + self.assertEqual(inv_1.invoice_payment_state, 'paid') - self.assertRecordValues(payment.move_line_ids, [ + rec_line = payment.move_line_ids.filtered(lambda line: line.account_id.user_type_id.type in ('receivable', 'payable')) + self.assertRecordValues(payment.move_line_ids.sorted('credit'), [ {'account_id': self.account_eur.id, 'debit': 300.0, 'credit': 0.0, 'amount_currency': 0, 'currency_id': False}, - {'account_id': inv_1.account_id.id, 'debit': 0.0, 'credit': 300.0, 'amount_currency': 0, 'currency_id': False}, + {'account_id': rec_line.account_id.id, 'debit': 0.0, 'credit': 300.0, 'amount_currency': 0, 'currency_id': False}, ]) - self.assertTrue(payment.move_line_ids.filtered(lambda l: l.account_id == inv_1.account_id)[0].full_reconcile_id) + self.assertTrue(rec_line.full_reconcile_id.exists()) - liquidity_aml = payment.move_line_ids.filtered(lambda r: r.account_id == self.account_eur) + liquidity_aml = payment.move_line_ids - rec_line bank_statement = self.reconcile(liquidity_aml, 300, 0, False) self.assertEqual(liquidity_aml.statement_id, bank_statement) @@ -129,8 +121,8 @@ def test_internal_transfer_journal_usd_journal_eur(self): }) payment.post() self.assertRecordValues(payment.move_line_ids, [ - {'account_id': self.account_usd.id, 'debit': 0.0, 'credit': 32.70, 'amount_currency': -50, 'currency_id': self.currency_usd_id}, {'account_id': self.transfer_account.id, 'debit': 32.70, 'credit': 0.0, 'amount_currency': 50, 'currency_id': self.currency_usd_id}, + {'account_id': self.account_usd.id, 'debit': 0.0, 'credit': 32.70, 'amount_currency': -50, 'currency_id': self.currency_usd_id}, {'account_id': self.transfer_account.id, 'debit': 0.0, 'credit': 32.70, 'amount_currency': -50, 'currency_id': self.currency_usd_id}, {'account_id': self.account_eur.id, 'debit': 32.70, 'credit': 0.0, 'amount_currency': 0, 'currency_id': False}, ]) @@ -149,118 +141,49 @@ def test_payment_chf_journal_usd(self): payment.post() self.assertRecordValues(payment.move_line_ids, [ - {'account_id': self.account_usd.id, 'debit': 0.0, 'credit': 38.21, 'amount_currency': -58.42, 'currency_id': self.currency_usd_id}, {'account_id': self.partner_china_exp.property_account_payable_id.id, 'debit': 38.21, 'credit': 0.0, 'amount_currency': 50, 'currency_id': self.currency_chf_id}, + {'account_id': self.account_usd.id, 'debit': 0.0, 'credit': 38.21, 'amount_currency': -58.42, 'currency_id': self.currency_usd_id}, ]) - def test_multiple_payments_00(self): - """ Create test to pay several vendor bills/invoices at once """ - # One payment for inv_1 and inv_2 (same partner) - inv_1 = self.create_invoice(amount=100, partner=self.partner_agrolait.id) - inv_2 = self.create_invoice(amount=500, partner=self.partner_agrolait.id) - # One payment for inv_3 (different partner) - inv_3 = self.create_invoice(amount=200, partner=self.partner_china_exp.id) - # One payment for inv_4 (Vendor Bill) - inv_4 = self.create_invoice(amount=50, partner=self.partner_agrolait.id, type='in_invoice') - - ids_out = [inv_1.id, inv_2.id, inv_3.id] - ids_in = [inv_4.id] - for ids in [ids_out, ids_in]: - register_payments = self.register_payments_model.with_context(active_ids=ids).create({ - 'payment_date': time.strftime('%Y') + '-07-15', - 'journal_id': self.bank_journal_euro.id, - 'payment_method_id': self.payment_method_manual_in.id, - }) - register_payments.create_payments() - payment_ids = self.payment_model.search([('invoice_ids', 'in', ids_out + ids_in)], order="id desc") - - self.assertEqual(len(payment_ids), 4) - self.assertAlmostEquals(sum(payment_ids.filtered(lambda r: r.payment_type == 'inbound').mapped('amount')) - sum(payment_ids.filtered(lambda r: r.payment_type == 'outbound').mapped('amount')), 750) - - inv_1_2_pay = self.env['account.payment'] - inv_3_pay = None - inv_4_pay = None - for payment_id in payment_ids: - self.assertEqual('posted', payment_id.state) - if payment_id.partner_id == self.partner_agrolait: - if payment_id.partner_type == 'supplier': - self.assertEqual(payment_id.amount, 50) - inv_4_pay = payment_id - else: - self.assertTrue(payment_id.amount in (100, 500)) - inv_1_2_pay |= payment_id - else: - self.assertEqual(payment_id.amount, 200) - inv_3_pay = payment_id - - self.assertTrue(len(inv_1_2_pay) == 2) - self.assertEqual(inv_1.state, 'paid') - self.assertEqual(inv_2.state, 'paid') - - self.assertIsNotNone(inv_3_pay) - self.assertRecordValues(inv_3_pay.move_line_ids, [ - {'account_id': self.account_eur.id, 'debit': 200.0, 'credit': 0.0, 'amount_currency': 0.0, 'currency_id': False}, - {'account_id': inv_1.account_id.id, 'debit': 0.0, 'credit': 200.0, 'amount_currency': 0.0, 'currency_id': False}, - ]) - self.assertEqual(inv_3.state, 'paid') - - self.assertIsNotNone(inv_4_pay) - self.assertRecordValues(inv_4_pay.move_line_ids, [ - {'account_id': self.account_eur.id, 'debit': 0.0, 'credit': 50.0, 'amount_currency': 0.0, 'currency_id': False}, - {'account_id': inv_1.account_id.id, 'debit': 50.0, 'credit': 0.0, 'amount_currency': 0.0, 'currency_id': False}, - ]) - self.assertEqual(inv_4.state, 'paid') - def test_partial_payment(self): """ Create test to pay invoices (cust. inv + vendor bill) with partial payment """ # Test Customer Invoice inv_1 = self.create_invoice(amount=600) - ids = [inv_1.id] - payment_register = Form(self.env['account.payment'].with_context(active_model='account.invoice', active_ids=ids)) - with payment_register as pr: - pr.payment_date = time.strftime('%Y') + '-07-15' - pr.journal_id = self.bank_journal_euro - pr.payment_method_id = self.payment_method_manual_in + payment_register = Form(self.env['account.payment'].with_context(active_model='account.move', active_ids=inv_1.ids)) + payment_register.payment_date = time.strftime('%Y') + '-07-15' + payment_register.journal_id = self.bank_journal_euro + payment_register.payment_method_id = self.payment_method_manual_in - # Perform the partial payment by setting the amount at 550 instead of 600 - pr.amount = 550 + # Perform the partial payment by setting the amount at 550 instead of 600 + payment_register.amount = 550 - payment_ids = self.payment_model.search([('invoice_ids', 'in', ids)], order="id desc") + payment = payment_register.save() - self.assertEqual(len(payment_ids), 1) - - payment_id = payment_ids[0] - - self.assertEqual(payment_id.invoice_ids[0].id, inv_1.id) - self.assertAlmostEquals(payment_id.amount, 550) - self.assertEqual(payment_id.payment_type, 'inbound') - self.assertEqual(payment_id.partner_id, self.partner_agrolait) - self.assertEqual(payment_id.partner_type, 'customer') + self.assertEqual(len(payment), 1) + self.assertEqual(payment.invoice_ids[0].id, inv_1.id) + self.assertAlmostEquals(payment.amount, 550) + self.assertEqual(payment.payment_type, 'inbound') + self.assertEqual(payment.partner_id, self.partner_agrolait) + self.assertEqual(payment.partner_type, 'customer') # Test Vendor Bill inv_2 = self.create_invoice(amount=500, type='in_invoice', partner=self.partner_china_exp.id) - ids = [inv_2.id] - payment_register = Form(self.env['account.payment'].with_context(active_model='account.invoice', active_ids=ids)) - with payment_register as pr: - pr.payment_date = time.strftime('%Y') + '-07-15' - pr.journal_id = self.bank_journal_euro - pr.payment_method_id = self.payment_method_manual_in - - # Perform the partial payment by setting the amount at 300 instead of 500 - pr.amount = 300 - - payment_register.save() - payment_ids = self.payment_model.search([('invoice_ids', 'in', ids)], order="id desc") + payment_register = Form(self.env['account.payment'].with_context(active_model='account.move', active_ids=inv_2.ids)) + payment_register.payment_date = time.strftime('%Y') + '-07-15' + payment_register.journal_id = self.bank_journal_euro + payment_register.payment_method_id = self.payment_method_manual_in - self.assertEqual(len(payment_ids), 1) + # Perform the partial payment by setting the amount at 300 instead of 500 + payment_register.amount = 300 - payment_id = payment_ids[0] + payment = payment_register.save() - self.assertEqual(payment_id.invoice_ids[0].id, inv_2.id) - self.assertAlmostEquals(payment_id.amount, 300) - self.assertEqual(payment_id.payment_type, 'outbound') - self.assertEqual(payment_id.partner_id, self.partner_china_exp) - self.assertEqual(payment_id.partner_type, 'supplier') + self.assertEqual(len(payment), 1) + self.assertEqual(payment.invoice_ids[0].id, inv_2.id) + self.assertAlmostEquals(payment.amount, 300) + self.assertEqual(payment.payment_type, 'outbound') + self.assertEqual(payment.partner_id, self.partner_china_exp) + self.assertEqual(payment.partner_type, 'supplier') def test_payment_and_writeoff_in_other_currency_1(self): # Use case: @@ -268,6 +191,7 @@ def test_payment_and_writeoff_in_other_currency_1(self): # Mark invoice as fully paid with a write_off # Check that all the aml are correctly created. invoice = self.create_invoice(amount=25, type='out_invoice', currency_id=self.currency_eur_id, partner=self.partner_agrolait.id) + receivable_line = invoice.line_ids.filtered(lambda l: l.account_id.user_type_id.type == 'receivable') # register payment on invoice payment = self.payment_model.create({'payment_type': 'inbound', 'payment_method_id': self.env.ref('account.account_payment_method_manual_in').id, @@ -283,18 +207,19 @@ def test_payment_and_writeoff_in_other_currency_1(self): }) payment.post() self.assertRecordValues(payment.move_line_ids, [ + {'account_id': receivable_line.account_id.id, 'debit': 0.0, 'credit': 25.0, 'amount_currency': -38.22, 'currency_id': self.currency_usd_id}, {'account_id': self.account_eur.id, 'debit': 16.35, 'credit': 0.0, 'amount_currency': 25.0, 'currency_id': self.currency_usd_id}, {'account_id': self.account_payable.id, 'debit': 8.65, 'credit': 0.0, 'amount_currency': 13.22, 'currency_id': self.currency_usd_id}, - {'account_id': self.account_receivable.id, 'debit': 0.0, 'credit': 25.0, 'amount_currency': -38.22, 'currency_id': self.currency_usd_id}, ]) - self.assertTrue(payment.move_line_ids.filtered(lambda l: l.account_id == invoice.account_id)[0].full_reconcile_id) - self.assertEqual(invoice.state, 'paid') + self.assertTrue(receivable_line.full_reconcile_id) + self.assertEqual(invoice.invoice_payment_state, 'paid') # Use case: # Company is in EUR, create a vendor bill for 25 EUR and register payment of 25 USD. # Mark invoice as fully paid with a write_off # Check that all the aml are correctly created. invoice = self.create_invoice(amount=25, type='in_invoice', currency_id=self.currency_eur_id, partner=self.partner_agrolait.id) + payable_line = invoice.line_ids.filtered(lambda l: l.account_id.user_type_id.type == 'payable') # register payment on invoice payment = self.payment_model.create({'payment_type': 'outbound', 'payment_method_id': self.env.ref('account.account_payment_method_manual_in').id, @@ -304,18 +229,18 @@ def test_payment_and_writeoff_in_other_currency_1(self): 'currency_id': self.currency_usd_id, 'payment_date': time.strftime('%Y') + '-07-15', 'payment_difference_handling': 'reconcile', - 'writeoff_account_id': self.account_payable.id, + 'writeoff_account_id': self.account_receivable.id, 'journal_id': self.bank_journal_euro.id, 'invoice_ids': [(4, invoice.id, None)] }) payment.post() self.assertRecordValues(payment.move_line_ids, [ + {'account_id': payable_line.account_id.id, 'debit': 25.0, 'credit': 0.0, 'amount_currency': 38.22, 'currency_id': self.currency_usd_id}, {'account_id': self.account_eur.id, 'debit': 0.0, 'credit': 16.35, 'amount_currency': -25.0, 'currency_id': self.currency_usd_id}, - {'account_id': self.account_payable.id, 'debit': 0.0, 'credit': 8.65, 'amount_currency': -13.22, 'currency_id': self.currency_usd_id}, - {'account_id': self.account_receivable.id, 'debit': 25.0, 'credit': 0.0, 'amount_currency': 38.22, 'currency_id': self.currency_usd_id}, + {'account_id': self.account_receivable.id, 'debit': 0.0, 'credit': 8.65, 'amount_currency': -13.22, 'currency_id': self.currency_usd_id}, ]) - self.assertTrue(payment.move_line_ids.filtered(lambda l: l.account_id == invoice.account_id)[0].full_reconcile_id) - self.assertEqual(invoice.state, 'paid') + self.assertTrue(payable_line.full_reconcile_id) + self.assertEqual(invoice.invoice_payment_state, 'paid') def test_payment_and_writeoff_out_refund(self): # Use case: @@ -323,6 +248,7 @@ def test_payment_and_writeoff_out_refund(self): # Mark invoice as fully paid with a write_off # Check that all the aml are correctly created. invoice = self.create_invoice(amount=100, type='out_refund', currency_id=self.currency_eur_id, partner=self.partner_agrolait.id) + receivable_line = invoice.line_ids.filtered(lambda l: l.account_id.user_type_id.type == 'receivable') # register payment on invoice payment = self.payment_model.create({'payment_type': 'outbound', 'payment_method_id': self.env.ref('account.account_payment_method_manual_in').id, @@ -337,11 +263,11 @@ def test_payment_and_writeoff_out_refund(self): }) payment.post() self.assertRecordValues(payment.move_line_ids, [ + {'account_id': receivable_line.account_id.id, 'debit': 100.0, 'credit': 0.0, 'amount_currency': 0.0, 'currency_id': False}, {'account_id': self.account_eur.id, 'debit': 0.0, 'credit': 90.0, 'amount_currency': 0.0, 'currency_id': False}, {'account_id': self.account_payable.id, 'debit': 0.0, 'credit': 10.0, 'amount_currency': 0.0, 'currency_id': False}, - {'account_id': self.account_receivable.id, 'debit': 100.0, 'credit': 0.0, 'amount_currency': 0.0, 'currency_id': False}, ]) - self.assertEqual(invoice.state, 'paid') + self.assertEqual(invoice.invoice_payment_state, 'paid') def test_payment_and_writeoff_in_other_currency_2(self): # Use case: @@ -360,10 +286,8 @@ def test_payment_and_writeoff_in_other_currency_2(self): 'name': time.strftime('%Y') + '-07-15'}) invoice = self.create_invoice(amount=5325.6, type='in_invoice', currency_id=self.currency_usd_id, partner=self.partner_agrolait.id) - self.assertRecordValues(invoice.move_id.line_ids, [ - {'account_id': self.account_receivable.id, 'debit': 0.0, 'credit': 5950.39, 'amount_currency': -5325.6, 'currency_id': self.currency_usd_id}, - {'account_id': self.account_revenue.id, 'debit': 5950.39, 'credit': 0.0, 'amount_currency': 5325.6, 'currency_id': self.currency_usd_id}, - ]) + payable_line = invoice.line_ids.filtered(lambda l: l.account_id.user_type_id.type == 'payable') + # register payment on invoice payment = self.payment_model.create({'payment_type': 'outbound', 'payment_method_id': self.env.ref('account.account_payment_method_manual_in').id, @@ -379,19 +303,18 @@ def test_payment_and_writeoff_in_other_currency_2(self): }) payment.post() self.assertRecordValues(payment.move_line_ids, [ + {'debit': 6051.82, 'credit': 0.0, 'amount_currency': 5325.6, 'currency_id': self.currency_usd_id}, {'debit': 0.0, 'credit': 6051.14, 'amount_currency': -5325.0, 'currency_id': self.currency_usd_id}, {'debit': 0.0, 'credit': 0.68, 'amount_currency': -0.6, 'currency_id': self.currency_usd_id}, - {'debit': 6051.82, 'credit': 0.0, 'amount_currency': 5325.6, 'currency_id': self.currency_usd_id}, ]) - exchange_lines = payment.move_line_ids[-1].full_reconcile_id.exchange_move_id.line_ids + exchange_lines = payable_line.full_reconcile_id.exchange_move_id.line_ids self.assertRecordValues(exchange_lines, [ + {'debit': 0.0, 'credit': 101.43, 'account_id': payable_line.account_id.id}, {'debit': 101.43, 'credit': 0.0, 'account_id': self.diff_expense_account.id}, - {'debit': 0.0, 'credit': 101.43, 'account_id': self.account_receivable.id}, ]) #check the invoice status - self.assertEqual(invoice.state, 'paid') - + self.assertEqual(invoice.invoice_payment_state, 'paid') def test_payment_and_writeoff_in_other_currency_3(self): # Use case related in revision 20935462a0cabeb45480ce70114ff2f4e91eaf79 @@ -412,10 +335,8 @@ def test_payment_and_writeoff_in_other_currency_3(self): 'name': time.strftime('%Y') + '-06-26'}) invoice = self.create_invoice(amount=247590.4, type='out_invoice', currency_id=self.currency_eur_id, partner=self.partner_agrolait.id) - self.assertRecordValues(invoice.move_id.line_ids, [ - {'account_id': self.account_receivable.id, 'debit': 247590.4, 'credit': 0.0, 'amount_currency': 0.0, 'currency_id': False}, - {'account_id': self.account_revenue.id, 'debit': 0.0, 'credit': 247590.4, 'amount_currency': 0.0, 'currency_id': False}, - ]) + receivable_line = invoice.line_ids.filtered(lambda l: l.account_id.user_type_id.type == 'receivable') + # register payment on invoice payment = self.payment_model.create({'payment_type': 'inbound', 'payment_method_id': self.env.ref('account.account_payment_method_manual_in').id, @@ -432,19 +353,19 @@ def test_payment_and_writeoff_in_other_currency_3(self): }) payment.post() self.assertRecordValues(payment.move_line_ids, [ + {'account_id': receivable_line.account_id.id, 'debit': 0.0, 'credit': 247589.16, 'amount_currency': -261.17, 'currency_id': self.currency_usd_id}, {'account_id': self.account_eur.id, 'debit': 253116.0, 'credit': 0.0, 'amount_currency': 267.0, 'currency_id': self.currency_usd_id}, {'account_id': self.account_revenue.id, 'debit': 0.0, 'credit': 5526.84, 'amount_currency': -5.83, 'currency_id': self.currency_usd_id}, - {'account_id': self.account_receivable.id, 'debit': 0.0, 'credit': 247589.16, 'amount_currency': -261.17, 'currency_id': self.currency_usd_id}, ]) # Check the invoice status and the full reconciliation: the difference on the receivable account # should have been completed by an exchange rate difference entry - self.assertEqual(invoice.state, 'paid') - self.assertTrue(invoice.move_id.line_ids.filtered(lambda l: l.account_id == self.account_receivable)[0].full_reconcile_id) + self.assertEqual(invoice.invoice_payment_state, 'paid') + self.assertTrue(receivable_line.full_reconcile_id) def test_post_at_bank_reconciliation_payment(self): # Create two new payments in a journal requiring the journal entries to be posted at bank reconciliation - post_at_bank_rec_journal = bank_journal_euro = self.env['account.journal'].create({ + post_at_bank_rec_journal = self.env['account.journal'].create({ 'name': 'Bank', 'type': 'bank', 'code': 'COUCOU', @@ -481,10 +402,9 @@ def test_post_at_bank_reconciliation_payment(self): # Reconcile the two payments with an invoice, whose full amount is equal to their sum invoice = self.create_invoice(amount=53, partner=self.partner_agrolait.id) - (payment_one.move_line_ids + payment_two.move_line_ids + invoice.move_id.line_ids).filtered(lambda x: x.account_id.user_type_id.type == 'receivable').reconcile() + (payment_one.move_line_ids + payment_two.move_line_ids + invoice.line_ids).filtered(lambda x: x.account_id.user_type_id.type == 'receivable').reconcile() - self.assertTrue(invoice.reconciled, "Invoice should have been reconciled with the payments") - self.assertEqual(invoice.state, 'in_payment', "Invoice should be in 'in payment' state") + self.assertEqual(invoice.invoice_payment_state, 'in_payment', "Invoice should be in 'in payment' state") # Match the first payment with a bank statement line bank_statement_one = self.reconcile(payment_one.move_line_ids.filtered(lambda x: x.account_id.user_type_id.type == 'liquidity'), 42) @@ -493,7 +413,7 @@ def test_post_at_bank_reconciliation_payment(self): self.assertEqual(payment_one.mapped('move_line_ids.move_id.state'), ['posted'], "After bank reconciliation, payment one's account.move should be posted.") self.assertEqual(payment_one.mapped('move_line_ids.move_id.date'), stmt_line_date_one, "After bank reconciliation, payment one's account.move should share the same date as the bank statement.") self.assertEqual([payment_one.payment_date], stmt_line_date_one, "After bank reconciliation, payment one should share the same date as the bank statement.") - self.assertEqual(invoice.state, 'in_payment', "The invoice should still be 'in payment', not all its payments are reconciled with a statement") + self.assertEqual(invoice.invoice_payment_state, 'in_payment', "The invoice should still be 'in payment', not all its payments are reconciled with a statement") # Match the second payment with a bank statement line bank_statement_two = self.reconcile(payment_two.move_line_ids.filtered(lambda x: x.account_id.user_type_id.type == 'liquidity'), 42) @@ -504,4 +424,4 @@ def test_post_at_bank_reconciliation_payment(self): self.assertEqual([payment_two.payment_date], stmt_line_date_two, "After bank reconciliation, payment two should share the same date as the bank statement.") # The invoice should now be paid - self.assertEqual(invoice.state, 'paid', "Invoice should be in 'paid' state after having reconciled the two payments with a bank statement") + self.assertEqual(invoice.invoice_payment_state, 'paid', "Invoice should be in 'paid' state after having reconciled the two payments with a bank statement") diff --git a/addons/account/tests/test_product_id_change.py b/addons/account/tests/test_product_id_change.py deleted file mode 100644 index c9ebaaebca384..0000000000000 --- a/addons/account/tests/test_product_id_change.py +++ /dev/null @@ -1,92 +0,0 @@ -from odoo.addons.account.tests.account_test_classes import AccountingTestCase -from odoo.tests import tagged -import time - - -@tagged('post_install', '-at_install') -class TestProductIdChange(AccountingTestCase): - """Test that when an included tax is mapped by a fiscal position, the included tax must be - subtracted to the price of the product. - """ - - def setUp(self): - super(TestProductIdChange, self).setUp() - self.invoice_model = self.env['account.invoice'] - self.fiscal_position_model = self.env['account.fiscal.position'] - self.fiscal_position_tax_model = self.env['account.fiscal.position.tax'] - self.tax_model = self.env['account.tax'] - self.pricelist_model = self.env['product.pricelist'] - self.res_partner_model = self.env['res.partner'] - self.product_tmpl_model = self.env['product.template'] - self.product_model = self.env['product.product'] - self.invoice_line_model = self.env['account.invoice.line'] - self.account_receivable = self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_receivable').id)], limit=1) - self.account_revenue = self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_revenue').id)], limit=1) - - def test_product_id_change(self): - partner = self.res_partner_model.create(dict(name="George")) - tax_include_sale = self.tax_model.create(dict(name="Include tax", - type_tax_use='sale', - amount='21.00', - price_include=True)) - tax_include_purchase = self.tax_model.create(dict(name="Include tax", - type_tax_use='purchase', - amount='21.00', - price_include=True)) - tax_exclude_sale = self.tax_model.create(dict(name="Exclude tax", - type_tax_use='sale', - amount='0.00')) - tax_exclude_purchase = self.tax_model.create(dict(name="Exclude tax", - type_tax_use='purchase', - amount='0.00')) - product_tmpl = self.product_tmpl_model.create(dict(name="Voiture", - list_price='121', - taxes_id=[(6, 0, [tax_include_sale.id])], - supplier_taxes_id=[(6, 0, [tax_include_purchase.id])])) - product = self.product_model.create(dict(product_tmpl_id=product_tmpl.id, - standard_price='242')) - fp = self.fiscal_position_model.create(dict(name="fiscal position", sequence=1)) - fp_tax_sale = self.fiscal_position_tax_model.create(dict(position_id=fp.id, - tax_src_id=tax_include_sale.id, - tax_dest_id=tax_exclude_sale.id)) - fp_tax_purchase = self.fiscal_position_tax_model.create(dict(position_id=fp.id, - tax_src_id=tax_include_purchase.id, - tax_dest_id=tax_exclude_purchase.id)) - - out_invoice = self.invoice_model.create({ - 'partner_id': partner.id, - 'name': 'invoice to client', - 'account_id': self.account_receivable.id, - 'type': 'out_invoice', - 'date_invoice': time.strftime('%Y') + '-06-26', - 'fiscal_position_id': fp.id, - }) - out_line = self.invoice_line_model.create({ - 'product_id': product.id, - 'quantity': 1, - 'price_unit': 121.0, - 'invoice_id': out_invoice.id, - 'name': 'something out', - 'account_id': self.account_revenue.id, - }) - - in_invoice = self.invoice_model.create({ - 'partner_id': partner.id, - 'name': 'invoice to supplier', - 'account_id': self.account_receivable.id, - 'type': 'in_invoice', - 'date_invoice': time.strftime('%Y') + '-06-26', - 'fiscal_position_id': fp.id, - }) - in_line = self.invoice_line_model.create({ - 'product_id': product.id, - 'quantity': 1, - 'price_unit': 242.0, - 'invoice_id': in_invoice.id, - 'name': 'something in', - 'account_id': self.account_revenue.id, - }) - out_line._onchange_product_id() - self.assertEquals(100, out_line.price_unit, "The included tax must be subtracted to the price") - in_line._onchange_product_id() - self.assertEquals(200, in_line.price_unit, "The included tax must be subtracted to the price") diff --git a/addons/account/tests/test_reconciliation.py b/addons/account/tests/test_reconciliation.py index 0a35310f8a7dc..641ff761fa8c8 100644 --- a/addons/account/tests/test_reconciliation.py +++ b/addons/account/tests/test_reconciliation.py @@ -1,7 +1,7 @@ from odoo import api, fields from odoo.addons.account.tests.account_test_classes import AccountingTestCase from odoo.exceptions import UserError -from odoo.tests import tagged +from odoo.tests import Form, tagged import time import unittest @@ -18,22 +18,20 @@ class TestReconciliation(AccountingTestCase): def setUp(self): super(TestReconciliation, self).setUp() - self.account_invoice_model = self.env['account.invoice'] - self.account_invoice_line_model = self.env['account.invoice.line'] self.acc_bank_stmt_model = self.env['account.bank.statement'] self.acc_bank_stmt_line_model = self.env['account.bank.statement.line'] self.res_currency_model = self.registry('res.currency') self.res_currency_rate_model = self.registry('res.currency.rate') - partner_agrolait = self.env.ref("base.res_partner_2") - self.partner_agrolait_id = partner_agrolait.id + self.partner_agrolait = self.env.ref("base.res_partner_2") + self.partner_agrolait_id = self.partner_agrolait.id self.currency_swiss_id = self.env.ref("base.CHF").id self.currency_usd_id = self.env.ref("base.USD").id self.currency_euro_id = self.env.ref("base.EUR").id company = self.env.ref('base.main_company') self.cr.execute("UPDATE res_company SET currency_id = %s WHERE id = %s", [self.currency_euro_id, company.id]) - self.account_rcv = partner_agrolait.property_account_receivable_id or self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_receivable').id)], limit=1) - self.account_rsa = partner_agrolait.property_account_payable_id or self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_payable').id)], limit=1) + self.account_rcv = self.partner_agrolait.property_account_receivable_id or self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_receivable').id)], limit=1) + self.account_rsa = self.partner_agrolait.property_account_payable_id or self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_payable').id)], limit=1) self.product = self.env.ref("product.product_product_4") self.bank_journal_euro = self.env['account.journal'].create({'name': 'Bank', 'type': 'bank', 'code': 'BNK67'}) @@ -133,31 +131,29 @@ def setUp(self): }) def create_invoice(self, type='out_invoice', invoice_amount=50, currency_id=None): - #we create an invoice in given currency - invoice = self.account_invoice_model.create({'partner_id': self.partner_agrolait_id, - 'currency_id': currency_id, - 'name': type == 'out_invoice' and 'invoice to client' or 'invoice to vendor', - 'account_id': self.account_rcv.id, + invoice_vals = { 'type': type, - 'date_invoice': time.strftime('%Y') + '-07-01', - }) - self.account_invoice_line_model.create({'product_id': self.product.id, - 'quantity': 1, - 'price_unit': invoice_amount, - 'invoice_id': invoice.id, - 'name': 'product that cost ' + str(invoice_amount), - 'account_id': self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_revenue').id)], limit=1).id, - }) - - #validate invoice - invoice.action_invoice_open() + 'partner_id': self.partner_agrolait_id, + 'invoice_date': time.strftime('%Y') + '-07-01', + 'date': time.strftime('%Y') + '-07-01', + 'invoice_line_ids': [(0, 0, { + 'name': 'product that cost %s' % invoice_amount, + 'quantity': 1, + 'price_unit': invoice_amount, + 'tax_ids': [(6, 0, [])], + })] + } + if currency_id: + invoice_vals['currency_id'] = currency_id + invoice = self.env['account.move'].with_context(default_type=type).create(invoice_vals) + invoice.post() return invoice def make_payment(self, invoice_record, bank_journal, amount=0.0, amount_currency=0.0, currency_id=None): bank_stmt = self.acc_bank_stmt_model.create({ 'journal_id': bank_journal.id, 'date': time.strftime('%Y') + '-07-15', - 'name': 'payment' + invoice_record.number + 'name': 'payment' + invoice_record.name }) bank_stmt_line = self.acc_bank_stmt_line_model.create({'name': 'payment', @@ -166,13 +162,9 @@ def make_payment(self, invoice_record, bank_journal, amount=0.0, amount_currency 'amount': amount, 'amount_currency': amount_currency, 'currency_id': currency_id, - 'date': time.strftime('%Y') + '-07-15',}) - - #reconcile the payment with the invoice - for l in invoice_record.move_id.line_ids: - if l.account_id.id == self.account_rcv.id: - line_id = l - break + 'date': time.strftime('%Y') + '-07-15', + }) + line_id = invoice_record.line_ids.filtered(lambda line: line.account_id.user_type_id.type in ('receivable', 'payable')) amount_in_widget = currency_id and amount_currency or amount bank_stmt_line.process_reconciliation(counterpart_aml_dicts=[{ 'move_line': line_id, @@ -203,135 +195,146 @@ class TestReconciliationExec(TestReconciliation): def test_statement_usd_invoice_eur_transaction_eur(self): customer_move_lines, supplier_move_lines = self.make_customer_and_supplier_flows(self.currency_euro_id, 30, self.bank_journal_usd, 42, 30, self.currency_euro_id) self.assertRecordValues(customer_move_lines, [ - {'debit': 30.0, 'credit': 0.0, 'amount_currency': 42, 'currency_id': self.currency_usd_id}, {'debit': 0.0, 'credit': 30.0, 'amount_currency': -42, 'currency_id': self.currency_usd_id}, + {'debit': 30.0, 'credit': 0.0, 'amount_currency': 42, 'currency_id': self.currency_usd_id}, ]) self.assertRecordValues(supplier_move_lines, [ - {'debit': 0.0, 'credit': 30.0, 'amount_currency': -42, 'currency_id': self.currency_usd_id}, {'debit': 30.0, 'credit': 0.0, 'amount_currency': 42, 'currency_id': self.currency_usd_id}, + {'debit': 0.0, 'credit': 30.0, 'amount_currency': -42, 'currency_id': self.currency_usd_id}, ]) def test_statement_usd_invoice_usd_transaction_usd(self): customer_move_lines, supplier_move_lines = self.make_customer_and_supplier_flows(self.currency_usd_id, 50, self.bank_journal_usd, 50, 0, False) self.assertRecordValues(customer_move_lines, [ - {'debit': 32.70, 'credit': 0.0, 'amount_currency': 50, 'currency_id': self.currency_usd_id}, {'debit': 0.0, 'credit': 32.70, 'amount_currency': -50, 'currency_id': self.currency_usd_id}, + {'debit': 32.70, 'credit': 0.0, 'amount_currency': 50, 'currency_id': self.currency_usd_id}, ]) self.assertRecordValues(supplier_move_lines, [ - {'debit': 0.0, 'credit': 32.70, 'amount_currency': -50, 'currency_id': self.currency_usd_id}, {'debit': 32.70, 'credit': 0.0, 'amount_currency': 50, 'currency_id': self.currency_usd_id}, + {'debit': 0.0, 'credit': 32.70, 'amount_currency': -50, 'currency_id': self.currency_usd_id}, ]) def test_statement_usd_invoice_usd_transaction_eur(self): customer_move_lines, supplier_move_lines = self.make_customer_and_supplier_flows(self.currency_usd_id, 50, self.bank_journal_usd, 50, 40, self.currency_euro_id) self.assertRecordValues(customer_move_lines, [ - {'debit': 40.0, 'credit': 0.0, 'amount_currency': 50, 'currency_id': self.currency_usd_id}, {'debit': 0.0, 'credit': 40.0, 'amount_currency': -50, 'currency_id': self.currency_usd_id}, + {'debit': 40.0, 'credit': 0.0, 'amount_currency': 50, 'currency_id': self.currency_usd_id}, ]) exchange_lines = customer_move_lines.mapped('full_reconcile_id.exchange_move_id.line_ids') self.assertRecordValues(exchange_lines, [ - {'debit': 0.0, 'credit': 7.30, 'account_id': self.diff_income_account.id}, {'debit': 7.30, 'credit': 0.0, 'account_id': self.account_rcv.id}, + {'debit': 0.0, 'credit': 7.30, 'account_id': self.diff_income_account.id}, ]) self.assertRecordValues(supplier_move_lines, [ - {'debit': 0.0, 'credit': 40.0, 'amount_currency': -50, 'currency_id': self.currency_usd_id}, {'debit': 40.0, 'credit': 0.0, 'amount_currency': 50, 'currency_id': self.currency_usd_id}, + {'debit': 0.0, 'credit': 40.0, 'amount_currency': -50, 'currency_id': self.currency_usd_id}, ]) exchange_lines = supplier_move_lines.mapped('full_reconcile_id.exchange_move_id.line_ids') self.assertRecordValues(exchange_lines, [ + {'debit': 0.0, 'credit': 7.30, 'account_id': self.account_rsa.id}, {'debit': 7.30, 'credit': 0.0, 'account_id': self.diff_expense_account.id}, - {'debit': 0.0, 'credit': 7.30, 'account_id': self.account_rcv.id}, ]) def test_statement_usd_invoice_chf_transaction_chf(self): customer_move_lines, supplier_move_lines = self.make_customer_and_supplier_flows(self.currency_swiss_id, 50, self.bank_journal_usd, 42, 50, self.currency_swiss_id) self.assertRecordValues(customer_move_lines, [ - {'debit': 27.47, 'credit': 0.0, 'amount_currency': 42, 'currency_id': self.currency_usd_id}, {'debit': 0.0, 'credit': 27.47, 'amount_currency': -50, 'currency_id': self.currency_swiss_id}, + {'debit': 27.47, 'credit': 0.0, 'amount_currency': 42, 'currency_id': self.currency_usd_id}, ]) exchange_lines = customer_move_lines.mapped('full_reconcile_id.exchange_move_id.line_ids') self.assertRecordValues(exchange_lines, [ - {'debit': 10.74, 'credit': 0.0, 'account_id': self.diff_expense_account.id}, {'debit': 0.0, 'credit': 10.74, 'account_id': self.account_rcv.id}, + {'debit': 10.74, 'credit': 0.0, 'account_id': self.diff_expense_account.id}, ]) self.assertRecordValues(supplier_move_lines, [ - {'debit': 0.0, 'credit': 27.47, 'amount_currency': -42, 'currency_id': self.currency_usd_id}, {'debit': 27.47, 'credit': 0.0, 'amount_currency': 50, 'currency_id': self.currency_swiss_id}, + {'debit': 0.0, 'credit': 27.47, 'amount_currency': -42, 'currency_id': self.currency_usd_id}, ]) exchange_lines = supplier_move_lines.mapped('full_reconcile_id.exchange_move_id.line_ids') self.assertRecordValues(exchange_lines, [ + {'debit': 10.74, 'credit': 0.0, 'account_id': self.account_rsa.id}, {'debit': 0.0, 'credit': 10.74, 'account_id': self.diff_income_account.id}, - {'debit': 10.74, 'credit': 0.0, 'account_id': self.account_rcv.id}, ]) def test_statement_eur_invoice_usd_transaction_usd(self): customer_move_lines, supplier_move_lines = self.make_customer_and_supplier_flows(self.currency_usd_id, 50, self.bank_journal_euro, 40, 50, self.currency_usd_id) self.assertRecordValues(customer_move_lines, [ - {'debit': 40.0, 'credit': 0.0, 'amount_currency': 50, 'currency_id': self.currency_usd_id}, {'debit': 0.0, 'credit': 40.0, 'amount_currency': -50, 'currency_id': self.currency_usd_id}, + {'debit': 40.0, 'credit': 0.0, 'amount_currency': 50, 'currency_id': self.currency_usd_id}, ]) exchange_lines = customer_move_lines.mapped('full_reconcile_id.exchange_move_id.line_ids') self.assertRecordValues(exchange_lines, [ - {'debit': 0.0, 'credit': 7.30, 'account_id': self.diff_income_account.id}, {'debit': 7.30, 'credit': 0.0, 'account_id': self.account_rcv.id}, + {'debit': 0.0, 'credit': 7.30, 'account_id': self.diff_income_account.id}, ]) self.assertRecordValues(supplier_move_lines, [ - {'debit': 0.0, 'credit': 40.0, 'amount_currency': -50, 'currency_id': self.currency_usd_id}, {'debit': 40.0, 'credit': 0.0, 'amount_currency': 50, 'currency_id': self.currency_usd_id}, + {'debit': 0.0, 'credit': 40.0, 'amount_currency': -50, 'currency_id': self.currency_usd_id}, ]) exchange_lines = supplier_move_lines.mapped('full_reconcile_id.exchange_move_id.line_ids') self.assertRecordValues(exchange_lines, [ + {'debit': 0.0, 'credit': 7.30, 'account_id': self.account_rsa.id}, {'debit': 7.30, 'credit': 0.0, 'account_id': self.diff_expense_account.id}, - {'debit': 0.0, 'credit': 7.30, 'account_id': self.account_rcv.id}, ]) def test_statement_eur_invoice_usd_transaction_eur(self): customer_move_lines, supplier_move_lines = self.make_customer_and_supplier_flows(self.currency_usd_id, 50, self.bank_journal_euro, 40, 0.0, False) self.assertRecordValues(customer_move_lines, [ - {'debit': 40.0, 'credit': 0.0, 'amount_currency': 0.0, 'currency_id': False}, {'debit': 0.0, 'credit': 40.0, 'amount_currency': 0.0, 'currency_id': False}, + {'debit': 40.0, 'credit': 0.0, 'amount_currency': 0.0, 'currency_id': False}, ]) self.assertRecordValues(supplier_move_lines, [ - {'debit': 0.0, 'credit': 40.0, 'amount_currency': 0.0, 'currency_id': False}, {'debit': 40.0, 'credit': 0.0, 'amount_currency': 0.0, 'currency_id': False}, + {'debit': 0.0, 'credit': 40.0, 'amount_currency': 0.0, 'currency_id': False}, ]) def test_statement_euro_invoice_usd_transaction_chf(self): customer_move_lines, supplier_move_lines = self.make_customer_and_supplier_flows(self.currency_usd_id, 50, self.bank_journal_euro, 42, 50, self.currency_swiss_id) self.assertRecordValues(customer_move_lines, [ - {'debit': 42.0, 'credit': 0.0, 'amount_currency': 50, 'currency_id': self.currency_swiss_id}, {'debit': 0.0, 'credit': 42.0, 'amount_currency': -50, 'currency_id': self.currency_swiss_id}, + {'debit': 42.0, 'credit': 0.0, 'amount_currency': 50, 'currency_id': self.currency_swiss_id}, ]) self.assertRecordValues(supplier_move_lines, [ - {'debit': 0.0, 'credit': 42.0, 'amount_currency': -50, 'currency_id': self.currency_swiss_id}, {'debit': 42.0, 'credit': 0.0, 'amount_currency': 50, 'currency_id': self.currency_swiss_id}, + {'debit': 0.0, 'credit': 42.0, 'amount_currency': -50, 'currency_id': self.currency_swiss_id}, ]) def test_statement_euro_invoice_usd_transaction_euro_full(self): - #we create an invoice in given invoice_currency - invoice_record = self.create_invoice(type='out_invoice', invoice_amount=50, currency_id=self.currency_usd_id) - #we encode a payment on it, on the given bank_journal with amount, amount_currency and transaction_currency given - bank_stmt = self.acc_bank_stmt_model.create({ - 'journal_id': self.bank_journal_euro.id, - 'date': time.strftime('%Y') + '-01-01', + # Create a customer invoice of 50 USD. + partner = self.env['res.partner'].create({'name': 'test'}) + move = self.env['account.move'].with_context(default_type='out_invoice').create({ + 'type': 'out_invoice', + 'partner_id': partner.id, + 'invoice_date': '%s-07-01' % time.strftime('%Y'), + 'date': '%s-07-01' % time.strftime('%Y'), + 'currency_id': self.currency_usd_id, + 'invoice_line_ids': [ + (0, 0, {'quantity': 1, 'price_unit': 50.0, 'name': 'test'}) + ], }) + move.post() - bank_stmt_line = self.acc_bank_stmt_line_model.create({'name': 'payment', - 'statement_id': bank_stmt.id, - 'partner_id': self.partner_agrolait_id, - 'amount': 40, - 'date': time.strftime('%Y') + '-01-01',}) + # Create a bank statement of 40 EURO. + bank_stmt = self.env['account.bank.statement'].create({ + 'journal_id': self.bank_journal_euro.id, + 'date': '%s-01-01' % time.strftime('%Y'), + 'line_ids': [ + (0, 0, { + 'name': 'test', + 'partner_id': partner.id, + 'amount': 40.0, + 'date': '%s-01-01' % time.strftime('%Y') + }) + ], + }) - #reconcile the payment with the invoice - for l in invoice_record.move_id.line_ids: - if l.account_id.id == self.account_rcv.id: - line_id = l - break - bank_stmt_line.process_reconciliation(counterpart_aml_dicts=[{ - 'move_line': line_id, + # Reconcile the bank statement with the invoice. + receivable_line = move.line_ids.filtered(lambda line: line.account_id.user_type_id.type in ('receivable', 'payable')) + bank_stmt.line_ids[0].process_reconciliation(counterpart_aml_dicts=[{ + 'move_line': receivable_line, 'debit': 0.0, 'credit': 32.7, 'name': 'test_statement_euro_invoice_usd_transaction_euro_full', @@ -343,18 +346,17 @@ def test_statement_euro_invoice_usd_transaction_euro_full(self): }]) self.assertRecordValues(bank_stmt.move_line_ids, [ - {'debit': 40.0, 'credit': 0.0, 'amount_currency': 0.0, 'currency_id': False}, - {'debit': 0.0, 'credit': 32.7, 'amount_currency': 0.0, 'currency_id': False}, {'debit': 0.0, 'credit': 7.3, 'amount_currency': 0.0, 'currency_id': False}, + {'debit': 0.0, 'credit': 32.7, 'amount_currency': 0.0, 'currency_id': False}, + {'debit': 40.0, 'credit': 0.0, 'amount_currency': 0.0, 'currency_id': False}, ]) # The invoice should be paid, as the payments totally cover its total - self.assertEquals(invoice_record.state, 'paid', 'The invoice should be paid by now') - invoice_rec_line = invoice_record.move_id.line_ids.filtered(lambda x: x.account_id.reconcile) - self.assertTrue(invoice_rec_line.reconciled, 'The invoice should be totally reconciled') - self.assertTrue(invoice_rec_line.full_reconcile_id, 'The invoice should have a full reconcile number') - self.assertEquals(invoice_rec_line.amount_residual, 0, 'The invoice should be totally reconciled') - self.assertEquals(invoice_rec_line.amount_residual_currency, 0, 'The invoice should be totally reconciled') + self.assertEquals(move.invoice_payment_state, 'paid', 'The invoice should be paid by now') + self.assertTrue(receivable_line.reconciled, 'The invoice should be totally reconciled') + self.assertTrue(receivable_line.full_reconcile_id, 'The invoice should have a full reconcile number') + self.assertEquals(receivable_line.amount_residual, 0, 'The invoice should be totally reconciled') + self.assertEquals(receivable_line.amount_residual_currency, 0, 'The invoice should be totally reconciled') @unittest.skip('adapt to new accounting') def test_balanced_exchanges_gain_loss(self): @@ -376,7 +378,8 @@ def test_balanced_exchanges_gain_loss(self): 'name': 'Foreign invoice with exchange gain', 'account_id': self.account_rcv_id, 'type': 'out_invoice', - 'date_invoice': time.strftime('%Y-%m-%d'), + 'invoice_date': time.strftime('%Y-%m-%d'), + 'date': time.strftime('%Y-%m-%d'), 'journal_id': self.bank_journal_usd_id, 'invoice_line': [ (0, 0, { @@ -386,7 +389,7 @@ def test_balanced_exchanges_gain_loss(self): }) ] }) - invoice.action_invoice_open() + invoice.post() # We create a bank statement with two lines of 1.00 USD each. statement = self.acc_bank_stmt_model.create({ 'journal_id': self.bank_journal_usd_id, @@ -409,7 +412,7 @@ def test_balanced_exchanges_gain_loss(self): # We process the reconciliation of the invoice line with the two bank statement lines line_id = None - for l in invoice.move_id.line_id: + for l in invoice.line_id: if l.account_id.id == self.account_rcv_id: line_id = l break @@ -581,16 +584,10 @@ def test_reconcile_bank_statement_with_payment_and_writeoff(self): 'currency_id': self.currency_usd_id, 'payment_date': time.strftime('%Y') + '-07-15', 'journal_id': self.bank_journal_usd.id, + 'invoice_ids': [(6, 0, invoice.ids)], }) payment.post() - payment_move_line = False - bank_move_line = False - for l in payment.move_line_ids: - if l.account_id.id == self.account_rcv.id: - payment_move_line = l - else: - bank_move_line = l - invoice.register_payment(payment_move_line) + bank_move_line = payment.move_line_ids.filtered(lambda line: line.account_id.internal_type == 'liquidity') # create bank statement bank_stmt = self.acc_bank_stmt_model.create({ @@ -659,7 +656,6 @@ def test_partial_reconcile_currencies_01(self): # Counterpart Credit goes in Exchange diff dest_journal_id = self.env['account.journal'].search([('type', '=', 'purchase'), ('company_id', '=', self.env.ref('base.main_company').id)], limit=1) - account_expenses = self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_expenses').id)], limit=1) self.bank_journal_euro.write({'default_debit_account_id': self.account_rsa.id, 'default_credit_account_id': self.account_rsa.id}) @@ -682,38 +678,27 @@ def test_partial_reconcile_currencies_01(self): 'company_id': self.env.ref('base.main_company').id}) # Preparing Invoices (from vendor) - invoice_a = self.account_invoice_model.create({'partner_id': self.partner_agrolait_id, - 'currency_id': self.currency_usd_id, - 'name': 'invoice to vendor', - 'account_id': self.account_rsa.id, + invoice_a = self.env['account.move'].with_context(default_type='in_invoice').create({ 'type': 'in_invoice', - 'date_invoice': time.strftime('%Y') + '-' + '07' + '-01', - }) - self.account_invoice_line_model.create({'product_id': self.product.id, - 'quantity': 1, - 'price_unit': 50, - 'invoice_id': invoice_a.id, - 'name': 'product that cost ' + str(50), - 'account_id': account_expenses.id, - }) - - invoice_b = self.account_invoice_model.create({'partner_id': self.partner_agrolait_id, + 'partner_id': self.partner_agrolait_id, 'currency_id': self.currency_usd_id, - 'name': 'invoice to vendor', - 'account_id': self.account_rsa.id, + 'invoice_date': '%s-07-01' % time.strftime('%Y'), + 'date': '%s-07-01' % time.strftime('%Y'), + 'invoice_line_ids': [ + (0, 0, {'product_id': self.product.id, 'quantity': 1, 'price_unit': 50.0}) + ], + }) + invoice_b = self.env['account.move'].with_context(default_type='in_invoice').create({ 'type': 'in_invoice', - 'date_invoice': time.strftime('%Y') + '-' + '08' + '-01', - }) - self.account_invoice_line_model.create({'product_id': self.product.id, - 'quantity': 1, - 'price_unit': 50, - 'invoice_id': invoice_b.id, - 'name': 'product that cost ' + str(50), - 'account_id': account_expenses.id, + 'partner_id': self.partner_agrolait_id, + 'currency_id': self.currency_usd_id, + 'invoice_date': '%s-08-01' % time.strftime('%Y'), + 'date': '%s-08-01' % time.strftime('%Y'), + 'invoice_line_ids': [ + (0, 0, {'product_id': self.product.id, 'quantity': 1, 'price_unit': 50.0}) + ], }) - - invoice_a.action_invoice_open() - invoice_b.action_invoice_open() + (invoice_a + invoice_b).post() # Preparing Payments # One partial for invoice_a (fully assigned to it) @@ -761,27 +746,27 @@ def test_partial_reconcile_currencies_01(self): debit_line_b = payment_b.move_line_ids.filtered(lambda l: l.debit and l.account_id == dest_journal_id.default_debit_account_id) debit_line_c = payment_c.move_line_ids.filtered(lambda l: l.debit and l.account_id == dest_journal_id.default_debit_account_id) - invoice_a.assign_outstanding_credit(debit_line_a.id) - invoice_a.assign_outstanding_credit(debit_line_b.id) - invoice_b.assign_outstanding_credit(debit_line_b.id) - invoice_b.assign_outstanding_credit(debit_line_c.id) + invoice_a.js_assign_outstanding_line(debit_line_a.id) + invoice_a.js_assign_outstanding_line(debit_line_b.id) + invoice_b.js_assign_outstanding_line(debit_line_b.id) + invoice_b.js_assign_outstanding_line(debit_line_c.id) # Asserting correctness (only in the payable account) full_reconcile = False - for inv in (invoice_a + invoice_b): - self.assertTrue(inv.reconciled) - for aml in (inv.payment_move_line_ids + inv.move_id.line_ids).filtered(lambda l: l.account_id == self.account_rsa): - self.assertEqual(aml.amount_residual, 0.0) - self.assertEqual(aml.amount_residual_currency, 0.0) - self.assertTrue(aml.reconciled) - if not full_reconcile: - full_reconcile = aml.full_reconcile_id - else: - self.assertTrue(aml.full_reconcile_id == full_reconcile) + reconciled_amls = (debit_line_a + debit_line_b + debit_line_c + (invoice_a + invoice_b).mapped('line_ids'))\ + .filtered(lambda l: l.account_id == self.account_rsa) + for aml in reconciled_amls: + self.assertEqual(aml.amount_residual, 0.0) + self.assertEqual(aml.amount_residual_currency, 0.0) + self.assertTrue(aml.reconciled) + if not full_reconcile: + full_reconcile = aml.full_reconcile_id + else: + self.assertTrue(aml.full_reconcile_id == full_reconcile) full_rec_move = full_reconcile.exchange_move_id # Globally check whether the amount is correct - self.assertEqual(full_rec_move.amount, 18.75) + self.assertEqual(sum(full_rec_move.mapped('line_ids.debit')), 18.75) # Checking if the direction of the move is correct full_rec_payable = full_rec_move.line_ids.filtered(lambda l: l.account_id == self.account_rsa) @@ -806,22 +791,19 @@ def test_unreconcile(self): credit_aml = payment.move_line_ids.filtered('credit') # Check residual before assignation - self.assertAlmostEquals(inv1.residual, 10) - self.assertAlmostEquals(inv2.residual, 20) + self.assertAlmostEquals(inv1.amount_residual, 10) + self.assertAlmostEquals(inv2.amount_residual, 20) # Assign credit and residual - inv1.assign_outstanding_credit(credit_aml.id) - inv2.assign_outstanding_credit(credit_aml.id) - self.assertAlmostEquals(inv1.residual, 0) - self.assertAlmostEquals(inv2.residual, 0) + inv1.js_assign_outstanding_line(credit_aml.id) + inv2.js_assign_outstanding_line(credit_aml.id) + self.assertAlmostEquals(inv1.amount_residual, 0) + self.assertAlmostEquals(inv2.amount_residual, 0) # Unreconcile one invoice at a time and check residual - credit_aml.with_context(invoice_id=inv1.id).remove_move_reconcile() - self.assertAlmostEquals(inv1.residual, 10) - self.assertAlmostEquals(inv2.residual, 0) - credit_aml.with_context(invoice_id=inv2.id).remove_move_reconcile() - self.assertAlmostEquals(inv1.residual, 10) - self.assertAlmostEquals(inv2.residual, 20) + credit_aml.remove_move_reconcile() + self.assertAlmostEquals(inv1.amount_residual, 10) + self.assertAlmostEquals(inv2.amount_residual, 20) def test_unreconcile_exchange(self): # Use case: @@ -860,16 +842,16 @@ def test_unreconcile_exchange(self): credit_aml = payment.move_line_ids.filtered('credit') # Check residual before assignation - self.assertAlmostEquals(inv.residual, 111) + self.assertAlmostEquals(inv.amount_residual, 111) # Assign credit, check exchange move and residual - inv.assign_outstanding_credit(credit_aml.id) + inv.js_assign_outstanding_line(credit_aml.id) self.assertEqual(len(payment.move_line_ids.mapped('full_reconcile_id').exchange_move_id), 1) - self.assertAlmostEquals(inv.residual, 0) + self.assertAlmostEquals(inv.amount_residual, 0) # Unreconcile invoice and check residual credit_aml.with_context(invoice_id=inv.id).remove_move_reconcile() - self.assertAlmostEquals(inv.residual, 111) + self.assertAlmostEquals(inv.amount_residual, 111) def test_revert_payment_and_reconcile(self): payment = self.env['account.payment'].create({ @@ -896,9 +878,8 @@ def test_revert_payment_and_reconcile(self): move = bank_line.move_id # Reversing the payment's move - reversed_move_list = move.reverse_moves('2018-06-04') - self.assertEqual(len(reversed_move_list), 1) - reversed_move = self.env['account.move'].browse(reversed_move_list[0]) + reversed_move = move._reverse_moves([{'date': '2018-06-04'}]) + self.assertEqual(len(reversed_move), 1) self.assertEqual(len(reversed_move.line_ids), 2) @@ -914,26 +895,22 @@ def test_revert_payment_and_reconcile(self): self.assertEqual(reversed_bank_line.full_reconcile_id.id, bank_line.full_reconcile_id.id) self.assertEqual(reversed_customer_line.full_reconcile_id.id, customer_line.full_reconcile_id.id) - def create_invoice_partner(self, type='out_invoice', invoice_amount=50, currency_id=None, partner_id=False): - #we create an invoice in given currency - invoice = self.account_invoice_model.create({'partner_id': partner_id, - 'currency_id': currency_id, - 'name': type == 'out_invoice' and 'invoice to client' or 'invoice to vendor', - 'account_id': self.account_rcv.id, + def create_invoice_partner(self, type='out_invoice', invoice_amount=50, currency_id=None, partner_id=False, payment_term_id=False): + move = self.env['account.move'].with_context(default_type=type).create({ 'type': type, - 'date_invoice': time.strftime('%Y') + '-07-01', - }) - self.account_invoice_line_model.create({'product_id': self.product.id, - 'quantity': 1, - 'price_unit': invoice_amount, - 'invoice_id': invoice.id, - 'name': 'product that cost ' + str(invoice_amount), - 'account_id': self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_revenue').id)], limit=1).id, + 'partner_id': partner_id, + 'invoice_date': time.strftime('%Y') + '-07-01', + 'date': time.strftime('%Y') + '-07-01', + 'currency_id': currency_id, + 'invoice_payment_term_id': payment_term_id, + 'invoice_line_ids': [(0, 0, { + 'quantity': 1, + 'price_unit': invoice_amount, + 'name': 'product that cost ' + str(invoice_amount), + })], }) - - #validate invoice - invoice.action_invoice_open() - return invoice + move.post() + return move def test_aged_report(self): AgedReport = self.env['report.account.report_agedpartnerbalance'].with_context(include_nullified_amount=True) @@ -963,7 +940,7 @@ def test_aged_report(self): # Case 2: The invoice and payment are not reconciled: we should have one line on the report # and 2 amls - invoice.move_id.line_ids.with_context(invoice_id=invoice.id).remove_move_reconcile() + invoice.line_ids.with_context(invoice_id=invoice.id).remove_move_reconcile() report_lines, total, amls = AgedReport._get_partner_move_lines(account_type, report_date_to, 'posted', 30) partner_lines = [line for line in report_lines if line['partner_id'] == partner.id] @@ -1028,17 +1005,17 @@ def _move_revert_test_pair(move, revert): payment.post() credit_aml = payment.move_line_ids.filtered('credit') - inv.assign_outstanding_credit(credit_aml.id) - self.assertTrue(inv.state == 'paid', 'The invoice should be paid') + inv.js_assign_outstanding_line(credit_aml.id) + self.assertTrue(inv.invoice_payment_state == 'paid', 'The invoice should be paid') exchange_reconcile = payment.move_line_ids.mapped('full_reconcile_id') exchange_move = exchange_reconcile.exchange_move_id payment_move = payment.move_line_ids[0].move_id - reverted_payment_move = self.env['account.move'].browse(payment_move.reverse_moves(time.strftime('%Y') + '-08-01')) + reverted_payment_move = payment_move._reverse_moves([{'date': time.strftime('%Y') + '-08-01'}], cancel=True) # After reversal of payment, the invoice should be open - self.assertTrue(inv.state == 'open', 'The invoice should be open again') + self.assertTrue(inv.state == 'posted', 'The invoice should be open again') self.assertFalse(exchange_reconcile.exists()) reverted_exchange_move = self.env['account.move'].search([('journal_id', '=', exchange_move.journal_id.id), ('ref', 'ilike', exchange_move.name)], limit=1) @@ -1061,12 +1038,12 @@ def test_aged_report_future_payment(self): # Also, in this case, there can be only 1 partial_reconcile statement_partial_id = statement.move_line_ids.mapped(lambda l: l.matched_credit_ids + l.matched_debit_ids) self.env.cr.execute('UPDATE account_partial_reconcile SET create_date = %(date)s WHERE id = %(partial_id)s', - {'date': invoice.date_invoice, + {'date': invoice.invoice_date, 'partial_id': statement_partial_id.id}) # Case 1: report date is invoice date # There should be an entry for the partner - report_date_to = invoice.date_invoice + report_date_to = invoice.invoice_date report_lines, total, amls = AgedReport._get_partner_move_lines(account_type, report_date_to, 'posted', 30) partner_lines = [line for line in report_lines if line['partner_id'] == partner.id] @@ -1129,9 +1106,6 @@ def test_partial_reconcile_currencies_02(self): # * Dr. 100 USD / 50 EUR - Accounts receivable # * Cr. 100 USD / 50 EUR - Revenue #### - account_revenue = self.env['account.account'].search( - [('user_type_id', '=', self.env.ref( - 'account.data_account_type_revenue').id)], limit=1) dest_journal_id = self.env['account.journal'].search( [('type', '=', 'purchase'), ('company_id', '=', self.env.ref('base.main_company').id)], @@ -1148,24 +1122,18 @@ def test_partial_reconcile_currencies_02(self): 'rate': 2, }) - invoice_cust_1 = self.account_invoice_model.create({ - 'partner_id': self.partner_agrolait_id, - 'account_id': self.account_rcv.id, + invoice_cust_1 = self.env['account.move'].with_context(default_type='out_invoice').create({ 'type': 'out_invoice', + 'partner_id': self.partner_agrolait_id, + 'invoice_date': '%s-01-01' % time.strftime('%Y'), + 'date': '%s-01-01' % time.strftime('%Y'), 'currency_id': self.currency_usd_id, - 'date_invoice': time.strftime('%Y') + '-01-01', - }) - self.account_invoice_line_model.create({ - 'quantity': 1.0, - 'price_unit': 100.0, - 'invoice_id': invoice_cust_1.id, - 'name': 'product that cost 100', - 'account_id': account_revenue.id, - }) - invoice_cust_1.action_invoice_open() - self.assertEqual(invoice_cust_1.residual_company_signed, 50.0) - aml = invoice_cust_1.move_id.mapped('line_ids').filtered( - lambda x: x.account_id == account_revenue) + 'invoice_line_ids': [ + (0, 0, {'quantity': 1, 'price_unit': 100.0, 'name': 'product that cost 100'}) + ], + }) + invoice_cust_1.post() + aml = invoice_cust_1.invoice_line_ids[0] self.assertEqual(aml.credit, 50.0) ##### # Day 2: Receive payment for half invoice Cust/1 (in USD) @@ -1193,17 +1161,12 @@ def test_partial_reconcile_currencies_02(self): 'currency_id': self.currency_usd_id, 'payment_date': time.strftime('%Y') + '-01-02', 'journal_id': dest_journal_id.id, + 'invoice_ids': [(6, 0, invoice_cust_1.ids)], }) payment.post() - payment_move_line = False - for l in payment.move_line_ids: - if l.account_id == invoice_cust_1.account_id: - payment_move_line = l - invoice_cust_1.register_payment(payment_move_line) # We expect at this point that the invoice should still be open, # because they owe us still 50 CC. - self.assertEqual(invoice_cust_1.state, 'open', - 'Invoice is in status %s' % invoice_cust_1.state) + self.assertEqual(invoice_cust_1.invoice_payment_state, 'not_paid', 'Invoice is in status %s' % invoice_cust_1.state) def test_multiple_term_reconciliation_opw_1906665(self): '''Test that when registering a payment to an invoice with multiple @@ -1228,25 +1191,11 @@ def test_multiple_term_reconciliation_opw_1906665(self): }) # can't use self.create_invoice because it validates and we need to set payment_term_id - invoice = self.account_invoice_model.create({ - 'partner_id': self.partner_agrolait_id, - 'payment_term_id': payment_term.id, - 'currency_id': self.currency_usd_id, - 'name': 'Multiple payment terms', - 'account_id': self.account_rcv.id, - 'type': 'out_invoice', - 'date_invoice': time.strftime('%Y') + '-07-01', - }) - self.account_invoice_line_model.create({ - 'product_id': self.product.id, - 'quantity': 1, - 'price_unit': 50, - 'invoice_id': invoice.id, - 'name': self.product.display_name, - 'account_id': self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_revenue').id)], limit=1).id, - }) - - invoice.action_invoice_open() + invoice = self.create_invoice_partner( + partner_id=self.partner_agrolait_id, + payment_term_id=payment_term.id, + currency_id=self.currency_usd_id, + ) payment = self.env['account.payment'].create({ 'payment_type': 'inbound', @@ -1259,10 +1208,10 @@ def test_multiple_term_reconciliation_opw_1906665(self): }) payment.post() - invoice.assign_outstanding_credit(payment.move_line_ids.filtered('credit').id) + receivable_line = payment.move_line_ids.filtered('credit') + invoice.js_assign_outstanding_line(receivable_line.id) - receivable_lines = invoice.move_id.line_ids.filtered(lambda line: line.account_id == self.account_rcv).sorted('date_maturity')[0] - self.assertTrue(receivable_lines.matched_credit_ids) + self.assertTrue(receivable_line.matched_debit_ids) def test_reconciliation_cash_basis01(self): # Simulates an expense made up by 2 lines @@ -1272,61 +1221,31 @@ def test_reconciliation_cash_basis01(self): company = self.env.ref('base.main_company') company.tax_cash_basis_journal_id = self.cash_basis_journal - AccountMoveLine = self.env['account.move.line'].with_context(check_move_validity=False) - - # Purchase purchase_move = self.env['account.move'].create({ - 'name': 'purchase', 'journal_id': self.purchase_journal.id, - }) - - purchase_payable_line0 = AccountMoveLine.create({ - 'account_id': self.account_rsa.id, - 'credit': 100, - 'move_id': purchase_move.id, - }) - purchase_payable_line1 = AccountMoveLine.create({ - 'account_id': self.account_rsa.id, - 'credit': 50, - 'move_id': purchase_move.id, - }) - AccountMoveLine.create({ - 'name': 'expensNoTax', - 'account_id': self.expense_account.id, - 'debit': 50, - 'move_id': purchase_move.id, - }) - AccountMoveLine.create({ - 'name': 'expenseTaxed', - 'account_id': self.expense_account.id, - 'debit': 83.33, - 'move_id': purchase_move.id, - 'tax_ids': [(4, self.tax_cash_basis.id, False)], - }) - tax_line = AccountMoveLine.create({ - 'name': 'TaxLine', - 'account_id': self.tax_waiting_account.id, - 'debit': 16.67, - 'move_id': purchase_move.id, - 'tax_repartition_line_id': self.tax_cash_basis.invoice_repartition_line_ids.filtered(lambda x: x.repartition_type == 'tax').id, - 'tax_base_amount': 83.33, + 'line_ids': [ + (0, 0, {'account_id': self.account_rsa.id, 'debit': 0.0, 'credit': 100.0}), + (0, 0, {'account_id': self.account_rsa.id, 'debit': 0.0, 'credit': 50.0}), + (0, 0, {'account_id': self.expense_account.id, 'debit': 50.0, 'credit': 0.0}), + (0, 0, {'account_id': self.expense_account.id, 'debit': 83.33, 'credit': 0.0, 'tax_ids': [(4, self.tax_cash_basis.id)], 'tax_exigible': False}), + (0, 0, { + 'account_id': self.tax_waiting_account.id, + 'debit': 16.67, + 'credit': 0.0, + 'tax_repartition_line_id': self.tax_cash_basis.invoice_repartition_line_ids.filtered(lambda x: x.repartition_type == 'tax').id, + 'tax_base_amount': 83.33, + 'tax_exigible': False + }), + ], }) purchase_move.post() - # Payment Move payment_move = self.env['account.move'].create({ - 'name': 'payment', 'journal_id': self.bank_journal_euro.id, - }) - payment_payable_line = AccountMoveLine.create({ - 'account_id': self.account_rsa.id, - 'debit': 150, - 'move_id': payment_move.id, - }) - AccountMoveLine.create({ - 'account_id': self.account_euro.id, - 'credit': 150, - 'move_id': payment_move.id, + 'line_ids': [ + (0, 0, {'account_id': self.account_rsa.id, 'debit': 150.0, 'credit': 0.0}), + (0, 0, {'account_id': self.account_euro.id, 'debit': 0.0, 'credit': 150.0}), + ], }) payment_move.post() @@ -1339,21 +1258,21 @@ def test_reconciliation_cash_basis01(self): self.assertTrue(cash_basis_moves.exists()) # check reconciliation in Payable account - self.assertTrue(purchase_payable_line0.full_reconcile_id.exists()) - self.assertEqual(purchase_payable_line0.full_reconcile_id.reconciled_line_ids, - purchase_payable_line0 + purchase_payable_line1 + payment_payable_line) + self.assertTrue(purchase_move.line_ids[0].full_reconcile_id.exists()) + self.assertEqual(purchase_move.line_ids[0].full_reconcile_id.reconciled_line_ids, + purchase_move.line_ids[0] + purchase_move.line_ids[1] + payment_move.line_ids[0]) cash_basis_aml_ids = cash_basis_moves.mapped('line_ids') # check reconciliation in the tax waiting account - self.assertTrue(tax_line.full_reconcile_id.exists()) - self.assertEqual(tax_line.full_reconcile_id.reconciled_line_ids, - cash_basis_aml_ids.filtered(lambda l: l.account_id == self.tax_waiting_account) + tax_line) + self.assertTrue(purchase_move.line_ids[4].full_reconcile_id.exists()) + self.assertEqual(purchase_move.line_ids[4].full_reconcile_id.reconciled_line_ids, + cash_basis_aml_ids.filtered(lambda l: l.account_id == self.tax_waiting_account) + purchase_move.line_ids[4]) self.assertEqual(len(cash_basis_aml_ids), 8) # check amounts - cash_basis_move1 = cash_basis_moves.filtered(lambda m: m.amount == 33.34) - cash_basis_move2 = cash_basis_moves.filtered(lambda m: m.amount == 66.66) + cash_basis_move1 = cash_basis_moves.filtered(lambda m: m.amount_total == 33.34) + cash_basis_move2 = cash_basis_moves.filtered(lambda m: m.amount_total == 66.66) self.assertTrue(cash_basis_move1.exists()) self.assertTrue(cash_basis_move2.exists()) @@ -1396,88 +1315,50 @@ def test_reconciliation_cash_basis02(self): 'code': 'TWAIT1', }) - - AccountMoveLine = self.env['account.move.line'].with_context(check_move_validity=False) - - # Purchase purchase_move = self.env['account.move'].create({ - 'name': 'invoice', 'journal_id': self.purchase_journal.id, - }) - - purchase_payable_line0 = AccountMoveLine.create({ - 'account_id': self.account_rsa.id, - 'credit': 105, - 'move_id': purchase_move.id, - }) - purchase_payable_line1 = AccountMoveLine.create({ - 'account_id': self.account_rsa.id, - 'credit': 50, - 'move_id': purchase_move.id, - }) - AccountMoveLine.create({ - 'name': 'expenseTaxed 10%', - 'account_id': self.expense_account.id, - 'debit': 50, - 'move_id': purchase_move.id, - 'tax_ids': [(4, tax_cash_basis10percent.id, False)], - }) - tax_line0 = AccountMoveLine.create({ - 'name': 'TaxLine0', - 'account_id': tax_waiting_account10.id, - 'debit': 5, - 'move_id': purchase_move.id, - 'tax_repartition_line_id': tax_cash_basis10percent.invoice_repartition_line_ids.filtered(lambda x: x.repartition_type == 'tax').id, - 'tax_base_amount': 50, - }) - AccountMoveLine.create({ - 'name': 'expenseTaxed 20%', - 'account_id': self.expense_account.id, - 'debit': 83.33, - 'move_id': purchase_move.id, - 'tax_ids': [(4, self.tax_cash_basis.id, False)], - }) - tax_line1 = AccountMoveLine.create({ - 'name': 'TaxLine1', - 'account_id': self.tax_waiting_account.id, - 'debit': 16.67, - 'move_id': purchase_move.id, - 'tax_repartition_line_id': self.tax_cash_basis.invoice_repartition_line_ids.filtered(lambda x: x.repartition_type == 'tax').id, - 'tax_base_amount': 83.33, + 'line_ids': [ + (0, 0, {'account_id': self.account_rsa.id, 'debit': 0.0, 'credit': 50.0}), + (0, 0, {'account_id': self.account_rsa.id, 'debit': 0.0, 'credit': 105.0}), + (0, 0, {'name': 'expenseTaxed 10%', 'account_id': self.expense_account.id, 'debit': 50.0, 'credit': 0.0, 'tax_ids': [(4, tax_cash_basis10percent.id)], 'tax_exigible': False}), + (0, 0, { + 'name': 'TaxLine0', + 'account_id': tax_waiting_account10.id, + 'debit': 5.0, + 'credit': 0.0, + 'tax_repartition_line_id': tax_cash_basis10percent.invoice_repartition_line_ids.filtered(lambda x: x.repartition_type == 'tax').id, + 'tax_exigible': False, + 'tax_base_amount': 50.00, + }), + (0, 0, {'name': 'expenseTaxed 20%', 'account_id': self.expense_account.id, 'debit': 83.33, 'credit': 0.0, 'tax_ids': [(4, self.tax_cash_basis.id)], 'tax_exigible': False}), + (0, 0, { + 'name': 'TaxLine1', + 'account_id': self.tax_waiting_account.id, + 'debit': 16.67, + 'credit': 0.0, + 'tax_repartition_line_id': self.tax_cash_basis.invoice_repartition_line_ids.filtered(lambda x: x.repartition_type == 'tax').id, + 'tax_exigible': False, + 'tax_base_amount': 83.33, + }), + ], }) purchase_move.post() - # Payment Move payment_move0 = self.env['account.move'].create({ - 'name': 'payment', 'journal_id': self.bank_journal_euro.id, - }) - payment_payable_line0 = AccountMoveLine.create({ - 'account_id': self.account_rsa.id, - 'debit': 40, - 'move_id': payment_move0.id, - }) - AccountMoveLine.create({ - 'account_id': self.account_euro.id, - 'credit': 40, - 'move_id': payment_move0.id, + 'line_ids': [ + (0, 0, {'account_id': self.account_rsa.id, 'debit': 40.0, 'credit': 0.0}), + (0, 0, {'account_id': self.account_euro.id, 'debit': 0.0, 'credit': 40.0}), + ], }) payment_move0.post() - # Payment Move payment_move1 = self.env['account.move'].create({ - 'name': 'payment', 'journal_id': self.bank_journal_euro.id, - }) - payment_payable_line1 = AccountMoveLine.create({ - 'account_id': self.account_rsa.id, - 'debit': 115, - 'move_id': payment_move1.id, - }) - AccountMoveLine.create({ - 'account_id': self.account_euro.id, - 'credit': 115, - 'move_id': payment_move1.id, + 'line_ids': [ + (0, 0, {'account_id': self.account_rsa.id, 'debit': 115.0, 'credit': 0.0}), + (0, 0, {'account_id': self.account_euro.id, 'debit': 0.0, 'credit': 115.0}), + ], }) payment_move1.post() @@ -1490,32 +1371,32 @@ def test_reconciliation_cash_basis02(self): self.assertTrue(cash_basis_moves.exists()) # check reconciliation in Payable account - self.assertTrue(purchase_payable_line0.full_reconcile_id.exists()) - self.assertEqual(purchase_payable_line0.full_reconcile_id.reconciled_line_ids, - purchase_payable_line0 + purchase_payable_line1 + payment_payable_line0 + payment_payable_line1) + self.assertTrue(purchase_move.line_ids[0].full_reconcile_id.exists()) + self.assertEqual(purchase_move.line_ids[0].full_reconcile_id.reconciled_line_ids, + (purchase_move + payment_move0 + payment_move1).mapped('line_ids').filtered(lambda l: l.account_id.internal_type == 'payable')) cash_basis_aml_ids = cash_basis_moves.mapped('line_ids') # check reconciliation in the tax waiting account - self.assertTrue(tax_line0.full_reconcile_id.exists()) - self.assertEqual(tax_line0.full_reconcile_id.reconciled_line_ids, - cash_basis_aml_ids.filtered(lambda l: l.account_id == tax_waiting_account10) + tax_line0) + self.assertTrue(purchase_move.line_ids[3].full_reconcile_id.exists()) + self.assertEqual(purchase_move.line_ids[3].full_reconcile_id.reconciled_line_ids, + cash_basis_aml_ids.filtered(lambda l: l.account_id == tax_waiting_account10) + purchase_move.line_ids[3]) - self.assertTrue(tax_line1.full_reconcile_id.exists()) - self.assertEqual(tax_line1.full_reconcile_id.reconciled_line_ids, - cash_basis_aml_ids.filtered(lambda l: l.account_id == self.tax_waiting_account) + tax_line1) + self.assertTrue(purchase_move.line_ids[5].full_reconcile_id.exists()) + self.assertEqual(purchase_move.line_ids[5].full_reconcile_id.reconciled_line_ids, + cash_basis_aml_ids.filtered(lambda l: l.account_id == self.tax_waiting_account) + purchase_move.line_ids[5]) self.assertEqual(len(cash_basis_aml_ids), 24) # check amounts expected_move_amounts = [ {'base_20': 56.45, 'tax_20': 11.29, 'base_10': 33.87, 'tax_10': 3.39}, - {'base_20': 21.50, 'tax_20': 4.30, 'base_10': 12.90, 'tax_10': 1.29}, {'base_20': 5.38, 'tax_20': 1.08, 'base_10': 3.23, 'tax_10': 0.32}, + {'base_20': 21.50, 'tax_20': 4.30, 'base_10': 12.90, 'tax_10': 1.29}, ] index = 0 - for cb_move in cash_basis_moves.sorted('amount', reverse=True): + for cb_move in cash_basis_moves: expected = expected_move_amounts[index] move_lines = cb_move.line_ids base_amount_tax_lines20per = move_lines.filtered(lambda l: l.account_id == self.tax_base_amount_account and '20%' in l.name) @@ -1564,9 +1445,9 @@ def test_reconciliation_to_check(self): previous_name = st_line.move_name with self.assertRaises(UserError): #you need edition mode to be able to change it - st_line.with_context(edition_mode=False).process_reconciliation( + st_line.with_context(suspense_moves_mode=False).process_reconciliation( counterpart_aml_dicts=[], - new_aml_dicts = [{ + new_aml_dicts=[{ 'debit': 0, 'credit': 50, 'name': 'exchange difference', @@ -1574,9 +1455,9 @@ def test_reconciliation_to_check(self): }], ) - st_line.with_context(edition_mode=True).process_reconciliation( + st_line.with_context(suspense_moves_mode=True).process_reconciliation( counterpart_aml_dicts=[], - new_aml_dicts = [{ + new_aml_dicts=[{ 'debit': 0, 'credit': 50, 'name': 'exchange difference', @@ -1628,83 +1509,70 @@ def test_reconciliation_cash_basis_fx_01(self): company.country_id = self.ref('base.us') company.tax_cash_basis_journal_id = self.cash_basis_journal - aml_obj = self.env['account.move.line'].with_context( - check_move_validity=False) - # Purchase purchase_move = self.env['account.move'].create({ - 'name': 'purchase', 'journal_id': self.purchase_journal.id, - }) - - aml_obj.create({ - 'name': 'expenseTaxed', - 'account_id': self.expense_account.id, - 'debit': 106841.65, - 'move_id': purchase_move.id, - 'tax_ids': [(4, self.tax_cash_basis.id, False)], - 'currency_id': self.currency_usd_id, - 'amount_currency': 5301.00, - }) - aml_obj.create({ - 'name': 'TaxLine', - 'account_id': self.tax_waiting_account.id, - 'debit': 17094.66, - 'move_id': purchase_move.id, - 'currency_id': self.currency_usd_id, - 'amount_currency': 848.16, - 'tax_repartition_line_id': self.tax_cash_basis.invoice_repartition_line_ids.filtered(lambda x: x.repartition_type == 'tax').id, - 'tax_base_amount': 106841.65, - }) - purchase_payable_line0 = aml_obj.create({ - 'name': 'Payable', - 'account_id': self.account_rsa.id, - 'credit': 123936.31, - 'move_id': purchase_move.id, - 'currency_id': self.currency_usd_id, - 'amount_currency': -6149.16, + 'line_ids': [ + (0, 0, { + 'name': 'expenseTaxed', + 'account_id': self.expense_account.id, + 'currency_id': self.currency_usd_id, + 'tax_ids': [(4, self.tax_cash_basis.id)], + 'tax_exigible': False, + 'debit': 106841.65, 'credit': 0.0, 'amount_currency': 5301.00, + }), + (0, 0, { + 'name': 'TaxLine', + 'account_id': self.tax_waiting_account.id, + 'currency_id': self.currency_usd_id, + 'tax_repartition_line_id': self.tax_cash_basis.invoice_repartition_line_ids.filtered(lambda x: x.repartition_type == 'tax').id, + 'tax_exigible': False, + 'tax_base_amount': 106841.65, + 'debit': 17094.66, 'credit': 0.0, 'amount_currency': 848.16, + }), + (0, 0, { + 'name': 'Payable', + 'account_id': self.account_rsa.id, + 'currency_id': self.currency_usd_id, + 'debit': 0.0, 'credit': 123936.31, 'amount_currency': -6149.16, + }), + ], }) purchase_move.post() # FX 01 Move fx_move_01 = self.env['account.move'].create({ - 'name': 'FX 01', 'journal_id': self.fx_journal.id, - }) - fx_01_payable_line = aml_obj.create({ - 'account_id': self.account_rsa.id, - 'debit': 167.86, - 'move_id': fx_move_01.id, - 'currency_id': self.currency_usd_id, - 'amount_currency': 0.00, - }) - aml_obj.create({ - 'account_id': self.diff_income_account.id, - 'credit': 167.86, - 'move_id': fx_move_01.id, - 'currency_id': self.currency_usd_id, - 'amount_currency': 0.00, + 'line_ids': [ + (0, 0, { + 'account_id': self.account_rsa.id, + 'currency_id': self.currency_usd_id, + 'debit': 167.86, 'credit': 0.0, 'amount_currency': 0.00, + }), + (0, 0, { + 'account_id': self.diff_income_account.id, + 'currency_id': self.currency_usd_id, + 'debit': 0.0, 'credit': 167.86, 'amount_currency': 0.0, + }), + ], }) fx_move_01.post() # Payment Move payment_move = self.env['account.move'].create({ - 'name': 'payment', 'journal_id': self.bank_journal_usd.id, - }) - payment_payable_line = aml_obj.create({ - 'account_id': self.account_rsa.id, - 'debit': 123768.45, - 'move_id': payment_move.id, - 'currency_id': self.currency_usd_id, - 'amount_currency': 6149.16, - }) - aml_obj.create({ - 'account_id': self.account_usd.id, - 'credit': 123768.45, - 'move_id': payment_move.id, - 'currency_id': self.currency_usd_id, - 'amount_currency': -6149.16, + 'line_ids': [ + (0, 0, { + 'account_id': self.account_rsa.id, + 'currency_id': self.currency_usd_id, + 'debit': 123768.45, 'credit': 0.0, 'amount_currency': 6149.16, + }), + (0, 0, { + 'account_id': self.account_usd.id, + 'currency_id': self.currency_usd_id, + 'debit': 0.0, 'credit': 123768.45, 'amount_currency': -6149.16, + }), + ], }) payment_move.post() @@ -1714,10 +1582,10 @@ def test_reconciliation_cash_basis_fx_01(self): to_reconcile.reconcile() # check reconciliation in Payable account - self.assertTrue(purchase_payable_line0.full_reconcile_id.exists()) + self.assertTrue(purchase_move.line_ids[2].full_reconcile_id.exists()) self.assertEqual( - purchase_payable_line0.full_reconcile_id.reconciled_line_ids, - purchase_payable_line0 + fx_01_payable_line + payment_payable_line) + purchase_move.line_ids[2].full_reconcile_id.reconciled_line_ids, + purchase_move.line_ids[2] + fx_move_01.line_ids[0] + payment_move.line_ids[0]) # check cash basis cash_basis_moves = self.env['account.move'].search( @@ -1734,7 +1602,7 @@ def test_reconciliation_cash_basis_fx_01(self): self.assertTrue(cash_basis_move1.exists()) # For first move - move_lines = cash_basis_move1.line_ids + move_lines = cash_basis_moves.line_ids base_amount_tax_lines = move_lines.filtered( lambda l: l.account_id == self.tax_base_amount_account) self.assertEqual(len(base_amount_tax_lines), 2) @@ -1801,104 +1669,88 @@ def test_reconciliation_cash_basis_fx_02(self): company.country_id = self.ref('base.us') company.tax_cash_basis_journal_id = self.cash_basis_journal - aml_obj = self.env['account.move.line'].with_context( - check_move_validity=False) - # Purchase purchase_move = self.env['account.move'].create({ - 'name': 'purchase', 'journal_id': self.purchase_journal.id, - }) - - aml_obj.create({ - 'name': 'expenseTaxed', - 'account_id': self.expense_account.id, - 'debit': 106841.65, - 'move_id': purchase_move.id, - 'tax_ids': [(4, self.tax_cash_basis.id, False)], - 'currency_id': self.currency_usd_id, - 'amount_currency': 5301.00, - }) - aml_obj.create({ - 'name': 'TaxLine', - 'account_id': self.tax_waiting_account.id, - 'debit': 17094.66, - 'move_id': purchase_move.id, - 'tax_repartition_line_id': self.tax_cash_basis.invoice_repartition_line_ids.filtered(lambda x: x.repartition_type == 'tax').id, - 'tax_base_amount': 106841.65, - 'currency_id': self.currency_usd_id, - 'amount_currency': 848.16, - }) - purchase_payable_line0 = aml_obj.create({ - 'name': 'Payable', - 'account_id': self.account_rsa.id, - 'credit': 123936.31, - 'move_id': purchase_move.id, - 'currency_id': self.currency_usd_id, - 'amount_currency': -6149.16, + 'line_ids': [ + (0, 0, { + 'name': 'expenseTaxed', + 'account_id': self.expense_account.id, + 'currency_id': self.currency_usd_id, + 'tax_ids': [(4, self.tax_cash_basis.id)], + 'tax_exigible': False, + 'debit': 106841.65, 'credit': 0.0, 'amount_currency': 5301.00, + }), + (0, 0, { + 'name': 'TaxLine', + 'account_id': self.tax_waiting_account.id, + 'currency_id': self.currency_usd_id, + 'tax_repartition_line_id': self.tax_cash_basis.invoice_repartition_line_ids.filtered(lambda x: x.repartition_type == 'tax').id, + 'tax_exigible': False, + 'tax_base_amount': 106841.65, + 'debit': 17094.66, 'credit': 0.0, 'amount_currency': 848.16, + }), + (0, 0, { + 'name': 'Payable', + 'account_id': self.account_rsa.id, + 'currency_id': self.currency_usd_id, + 'debit': 0.0, 'credit': 123936.31, 'amount_currency': -6149.16, + }), + ], }) purchase_move.post() # FX 01 Move fx_move_01 = self.env['account.move'].create({ - 'name': 'FX 01', 'journal_id': self.fx_journal.id, - }) - fx_01_payable_line = aml_obj.create({ - 'account_id': self.account_rsa.id, - 'credit': 1572.96, - 'move_id': fx_move_01.id, - 'currency_id': self.currency_usd_id, - 'amount_currency': 0.00, - }) - aml_obj.create({ - 'account_id': self.diff_expense_account.id, - 'debit': 1572.96, - 'move_id': fx_move_01.id, - 'currency_id': self.currency_usd_id, - 'amount_currency': 0.00, + 'line_ids': [ + (0, 0, { + 'account_id': self.account_rsa.id, + 'currency_id': self.currency_usd_id, + 'debit': 0.0, 'credit': 1572.96, 'amount_currency': 0.00, + }), + (0, 0, { + 'account_id': self.diff_expense_account.id, + 'currency_id': self.currency_usd_id, + 'debit': 1572.96, 'credit': 0.0, 'amount_currency': 0.0, + }), + ], }) fx_move_01.post() # FX 02 Move fx_move_02 = self.env['account.move'].create({ - 'name': 'FX 02', 'journal_id': self.fx_journal.id, - }) - fx_02_payable_line = aml_obj.create({ - 'account_id': self.account_rsa.id, - 'debit': 1740.82, - 'move_id': fx_move_02.id, - 'currency_id': self.currency_usd_id, - 'amount_currency': 0.00, - }) - aml_obj.create({ - 'account_id': self.diff_income_account.id, - 'credit': 1740.82, - 'move_id': fx_move_02.id, - 'currency_id': self.currency_usd_id, - 'amount_currency': 0.00, + 'line_ids': [ + (0, 0, { + 'account_id': self.account_rsa.id, + 'currency_id': self.currency_usd_id, + 'debit': 1740.82, 'credit': 0.0, 'amount_currency': 0.00, + }), + (0, 0, { + 'account_id': self.diff_income_account.id, + 'currency_id': self.currency_usd_id, + 'debit': 0.0, 'credit': 1740.82, 'amount_currency': 0.0, + }), + ], }) fx_move_02.post() # Payment Move payment_move = self.env['account.move'].create({ - 'name': 'payment', 'journal_id': self.bank_journal_usd.id, - }) - payment_payable_line = aml_obj.create({ - 'account_id': self.account_rsa.id, - 'debit': 123768.45, - 'move_id': payment_move.id, - 'currency_id': self.currency_usd_id, - 'amount_currency': 6149.16, - }) - aml_obj.create({ - 'account_id': self.account_usd.id, - 'credit': 123768.45, - 'move_id': payment_move.id, - 'currency_id': self.currency_usd_id, - 'amount_currency': -6149.16, + 'line_ids': [ + (0, 0, { + 'account_id': self.account_rsa.id, + 'currency_id': self.currency_usd_id, + 'debit': 123768.45, 'credit': 0.0, 'amount_currency': 6149.16, + }), + (0, 0, { + 'account_id': self.account_usd.id, + 'currency_id': self.currency_usd_id, + 'debit': 0.0, 'credit': 123768.45, 'amount_currency': -6149.16, + }), + ], }) payment_move.post() @@ -1909,11 +1761,11 @@ def test_reconciliation_cash_basis_fx_02(self): to_reconcile.reconcile() # check reconciliation in Payable account - self.assertTrue(purchase_payable_line0.full_reconcile_id.exists()) + self.assertTrue(purchase_move.line_ids[2].full_reconcile_id.exists()) self.assertEqual( - purchase_payable_line0.full_reconcile_id.reconciled_line_ids, - purchase_payable_line0 + fx_01_payable_line + fx_02_payable_line + - payment_payable_line) + purchase_move.line_ids[2].full_reconcile_id.reconciled_line_ids, + purchase_move.line_ids[2] + fx_move_01.line_ids[0] + fx_move_02.line_ids[0] + + payment_move.line_ids[0]) # check cash basis cash_basis_moves = self.env['account.move'].search( @@ -2004,7 +1856,7 @@ def test_reconciliation_cash_basis_revert(self): }) purchase_move.post() - reverted = self.env['account.move'].browse(purchase_move.reverse_moves()) + reverted = purchase_move._reverse_moves(cancel=True) self.assertTrue(reverted.exists()) for inv_line in [purchase_payable_line0, tax_line0, tax_line1]: @@ -2026,24 +1878,30 @@ def test_reconciliation_cash_basis_foreign_currency_low_values(self): 'currency_id': self.currency_usd_id, 'company_id': self.env.ref('base.main_company').id, }) - invoice = self.create_invoice( - type='out_invoice', invoice_amount=50, - currency_id=self.currency_usd_id) - invoice.journal_id.update_posted = True - invoice.action_cancel() - invoice.state = 'draft' - invoice.invoice_line_ids.write({ - 'invoice_line_tax_ids': [(6, 0, [self.tax_cash_basis.id])]}) - invoice.compute_taxes() - invoice.action_invoice_open() + + move_form = Form(self.env['account.move'].with_context(default_type='out_invoice')) + move_form.partner_id = self.partner_agrolait + move_form.currency_id = self.env.ref('base.USD') + move_form.invoice_date = time.strftime('%Y') + '-07-01' + with move_form.invoice_line_ids.new() as line_form: + line_form.name = 'test line' + line_form.price_unit = 50 + line_form.tax_ids.clear() + invoice = move_form.save() + move_form = Form(invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + line_form.tax_ids.add(self.tax_cash_basis) + invoice = move_form.save() + invoice.post() self.assertTrue(invoice.currency_id != self.env.user.company_id.currency_id) # First Payment - payment0 = self.make_payment(invoice, journal, invoice.amount_total - 0.01) - self.assertEqual(invoice.residual, 0.01) + payment0 = self.make_payment(invoice, journal, amount=59.99) + self.assertEqual(invoice.amount_residual, 0.01) - tax_waiting_line = invoice.move_id.line_ids.filtered(lambda l: l.account_id == self.tax_waiting_account) + tax_waiting_line = invoice.line_ids.filtered(lambda l: l.account_id == self.tax_waiting_account) + self.assertTrue(tax_waiting_line.exists()) self.assertFalse(tax_waiting_line.reconciled) move_caba0 = tax_waiting_line.matched_debit_ids.debit_move_id.move_id @@ -2056,8 +1914,8 @@ def test_reconciliation_cash_basis_foreign_currency_low_values(self): # Second Payment payment1 = self.make_payment(invoice, journal, 0.01) - self.assertEqual(invoice.residual, 0) - self.assertEqual(invoice.state, 'paid') + self.assertEqual(invoice.amount_residual, 0) + self.assertEqual(invoice.invoice_payment_state, 'paid') self.assertTrue(tax_waiting_line.reconciled) move_caba1 = tax_waiting_line.matched_debit_ids.mapped('debit_move_id').mapped('move_id').filtered(lambda m: m != move_caba0) @@ -2211,4 +2069,4 @@ def test_reconciliation_process_move_lines_with_mixed_currencies(self): }]) writeoff_line = self.env['account.move.line'].search([('name', '=', 'writeoff')]) - self.assertEquals(writeoff_line.credit, 15.0) \ No newline at end of file + self.assertEquals(writeoff_line.credit, 15.0) diff --git a/addons/account/tests/test_reconciliation_matching_rules.py b/addons/account/tests/test_reconciliation_matching_rules.py index cb10bee162edc..b9e9e7d465ddd 100644 --- a/addons/account/tests/test_reconciliation_matching_rules.py +++ b/addons/account/tests/test_reconciliation_matching_rules.py @@ -8,21 +8,17 @@ class TestReconciliationMatchingRules(AccountingTestCase): def _create_invoice_line(self, amount, partner, type): ''' Create an invoice on the fly.''' - self_ctx = self.env['account.invoice'].with_context(type=type) - journal_id = self_ctx._default_journal().id - self_ctx = self_ctx.with_context(journal_id=journal_id) - view = type in ('in_invoice', 'in_refund') and 'account.invoice_supplier_form' or 'account.invoice_form' - with Form(self_ctx, view=view) as invoice_form: - invoice_form.partner_id = partner - with invoice_form.invoice_line_ids.new() as invoice_line_form: - invoice_line_form.name = 'xxxx' - invoice_line_form.quantity = 1 - invoice_line_form.price_unit = amount - invoice_line_form.invoice_line_tax_ids.clear() + invoice_form = Form(self.env['account.move'].with_context(default_type=type)) + invoice_form.partner_id = partner + with invoice_form.invoice_line_ids.new() as invoice_line_form: + invoice_line_form.name = 'xxxx' + invoice_line_form.quantity = 1 + invoice_line_form.price_unit = amount + invoice_line_form.tax_ids.clear() invoice = invoice_form.save() - invoice.action_invoice_open() - lines = invoice.move_id.line_ids - return lines.filtered(lambda l: l.account_id == invoice.account_id) + invoice.post() + lines = invoice.line_ids + return lines.filtered(lambda l: l.account_id.user_type_id.type in ('receivable', 'payable')) def _check_statement_matching(self, rules, expected_values, statements=None): if statements is None: @@ -293,15 +289,15 @@ def test_auto_reconcile(self): # Check first line has been well reconciled. self.assertRecordValues(self.bank_line_1.journal_entry_ids, [ - {'partner_id': self.partner_1.id, 'debit': 105.0, 'credit': 0.0}, - {'partner_id': self.partner_1.id, 'debit': 0.0, 'credit': 100.0}, {'partner_id': self.partner_1.id, 'debit': 0.0, 'credit': 5.0}, + {'partner_id': self.partner_1.id, 'debit': 0.0, 'credit': 100.0}, + {'partner_id': self.partner_1.id, 'debit': 105.0, 'credit': 0.0}, ]) # Check second line has been well reconciled. self.assertRecordValues(self.cash_line_1.journal_entry_ids, [ - {'partner_id': self.partner_2.id, 'debit': 0.0, 'credit': 1000.0}, {'partner_id': self.partner_2.id, 'debit': 1000.0, 'credit': 0.0}, + {'partner_id': self.partner_2.id, 'debit': 0.0, 'credit': 1000.0}, ]) def test_reverted_move_matching(self): @@ -329,7 +325,7 @@ def test_reverted_move_matching(self): }) move.post() - move_reversed = AccountMove.browse(move.reverse_moves()) + move_reversed = move._reverse_moves() self.assertTrue(move_reversed.exists()) bank_st = self.env['account.bank.statement'].create({ diff --git a/addons/account/tests/test_reconciliation_widget.py b/addons/account/tests/test_reconciliation_widget.py index d0ebd37d37b30..46dbaceca0583 100644 --- a/addons/account/tests/test_reconciliation_widget.py +++ b/addons/account/tests/test_reconciliation_widget.py @@ -41,7 +41,7 @@ def test_statement_suggestion_other_currency(self): bank_stmt = self.acc_bank_stmt_model.create({ 'journal_id': self.bank_journal_usd.id, 'date': time.strftime('%Y-07-15'), - 'name': 'payment %s' % invoice.number, + 'name': 'payment %s' % invoice.name, }) bank_stmt_line = self.acc_bank_stmt_line_model.create({'name': 'payment', @@ -52,4 +52,4 @@ def test_statement_suggestion_other_currency(self): }) result = self.env['account.reconciliation.widget'].get_bank_statement_line_data(bank_stmt_line.ids) - self.assertEqual(result['lines'][0]['reconciliation_proposition'][0]['amount_str'], '$ 50.00') \ No newline at end of file + self.assertEqual(result['lines'][0]['reconciliation_proposition'][0]['amount_str'], '$ 50.00') diff --git a/addons/account/views/account_invoice_view.xml b/addons/account/views/account_invoice_view.xml deleted file mode 100644 index 83f4f188163e4..0000000000000 --- a/addons/account/views/account_invoice_view.xml +++ /dev/null @@ -1,953 +0,0 @@ - - - - - - - account.invoice.calendar - account.invoice - - - - - - - - - - account.invoice.pivot - account.invoice - - - - - - - - - - - - - - account.invoice.graph - account.invoice - - - - - - - - - - account.invoice.activity - account.invoice - - - -
-
- - -
-
-
-
-
-
- - - account.invoice.line.tree - account.invoice.line - - - - - - - - - - - - - - - - - - account.invoice.line.form - account.invoice.line - - - - - - - - - - - - - - - - - - - - - - - account.invoice.tax.tree - account.invoice.tax - - - - - - - - - - - - - - - - account.invoice.tax.form - account.invoice.tax - -
- - - - - - - - - - - -
-
-
- - - account.invoice.tree - account.invoice - - - - - - - - - - - - - - - - - - - - - - - - - - - account.invoice.tree.with.onboarding - account.invoice - - primary - - - /account/account_invoice_onboarding - - - - - - account.invoice.kanban - account.invoice - - - - - - - - - - - - -
-
-
- -
- -
-
-
- -
-
- - - -
-
-
-
-
-
-
-
- - - account.invoice.supplier.tree - account.invoice - - - - - - - - - - - - - - - - - - - - - - - - - - - - - account.invoice.supplier.form - account.invoice - 2 - -
-
-
- - - - - -
- Draft Bill - - First Number: - Draft Credit Note - Bill - Credit Note -

- -
- - -
-

-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - -
-
- - - - - - - - - -
-
- -
-
- - - - - - - - - - - - - - - - - - -
-
-
-
- - - -
- - - - - - account.invoice.form - account.invoice - -
-
-
- - - - - -
-
- Credit Note -

-
-
-
-
-

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
-
- - - - - -
-
- - - - - - -
-
-
-
- - Quantity: - - - -
-
-
-
- - Unit Price: - - -
-
-
- -
-
- - - -
-
-
-
-
-
-
-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
-
-
- - - -
- - - - - - - By Salespersons - account.invoice - - {'group_by': ['date_invoice:month', 'user_id']} - - - - account.invoice.select - account.invoice - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Invoices - account.invoice - form - tree,form,calendar,graph - - {'type':'out_invoice'} - - - - - Invoices - account.invoice - form - tree,form,kanban,calendar,graph,pivot - - [('type','in', ['out_invoice', 'out_refund']), ('state', 'not in', ['draft', 'cancel'])] - {'default_type':'out_invoice', 'type':'out_invoice', 'journal_type': 'sale'} - - - - - - tree - - - - - - - form - - - - - - Pending Invoice - account.invoice - form - tree,form,calendar,graph - - {'type':'out_invoice'} - [('state','=','draft')] - - - - - - tree - - - - - - form - - - - - - Invoices - account.invoice - form - tree,kanban,form,calendar,pivot,graph,activity - - [('type','=','out_invoice')] - {'type':'out_invoice', 'journal_type': 'sale'} - - -

- Create a customer invoice -

- Create invoices, register payments and keep track of the discussions with your customers. -

-
-
- - - - tree - - - - - - - form - - - - - - - - Credit Notes - account.invoice - form - tree,kanban,form,calendar,pivot,graph,activity - - [('type','=','out_refund')] - {'default_type': 'out_refund', 'type': 'out_refund', 'journal_type': 'sale'} - - -

- Create a credit note -

- Note that the easiest way to create a credit note is to do it directly - from the customer invoice. -

-
-
- - - - tree - - - - - - - form - - - - - - - - Vendor Bills - account.invoice - form - tree,kanban,form,calendar,pivot,graph,activity - - [('type','=','in_invoice')] - {'default_type': 'in_invoice', 'type': 'in_invoice', 'journal_type': 'purchase'} - - -

- Record a new vendor bill -

-
-
- - - Bills - - code - -action_values = env.ref('account.action_vendor_bill_template').read()[0] -new_help = model.complete_empty_list_help() -action_values.update({'help': action_values.get('help', '') + new_help}) -action = action_values - - - - - - tree - - - - - - - form - - - - - - - - - Refund - account.invoice - form - tree,kanban,form,calendar,pivot,graph,activity - - [('type','=','in_refund')] - {'default_type': 'in_refund', 'type': 'in_refund', 'journal_type': 'purchase'} - - -

- Create a vendor credit note -

- Note that the easiest way to create a vendor credit note it to do it directly from the vendor bill. -

-
-
- - - - tree - - - - - - - form - - - - - - - - - - diff --git a/addons/account/views/account_journal_dashboard_view.xml b/addons/account/views/account_journal_dashboard_view.xml index 32083dc6ac8d6..d0a4a10837cec 100644 --- a/addons/account/views/account_journal_dashboard_view.xml +++ b/addons/account/views/account_journal_dashboard_view.xml @@ -124,12 +124,12 @@ View
Payments Matching @@ -175,10 +175,10 @@ View
Journal Items diff --git a/addons/account/views/account_move_views.xml b/addons/account/views/account_move_views.xml index 6f3dd3c1f27dd..ec5b6e413b30c 100644 --- a/addons/account/views/account_move_views.xml +++ b/addons/account/views/account_move_views.xml @@ -5,7 +5,6 @@ - account.move.line.form account.move.line @@ -33,10 +32,9 @@ - - + @@ -71,7 +69,6 @@ - @@ -153,8 +150,7 @@ - - + @@ -337,19 +333,47 @@ account.move.tree account.move - + - + + + account.invoice.tree + account.move + + + + + + + + + + + + + + + + + + + + + + + + + account.move.kanban account.move @@ -363,7 +387,7 @@
-
+
@@ -377,7 +401,7 @@
- +
@@ -397,89 +421,396 @@ account.move.form account.move -
+
-
+ + + + + - - -
- +
-

- -

- - - - - + + +
+ Paid + In Payment +
+ + + + + + + + + + + + + + + + +
+ + Draft Invoice + Draft Credit Note + Draft Bill + Draft Refund + Draft Sales Receipt + Draft Purchase Receipt + + + - First Number: + + +

+ + + +
+ + +
+

+
- - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + +
+
+ + + +
-
-
+ + + + + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
- +
@@ -506,8 +837,6 @@ - - @@ -518,6 +847,49 @@ + + account.invoice.select + account.move + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -557,9 +929,9 @@ view_id="account.view_move_line_tax_audit_tree"/> - {'journal_type':'general', 'search_default_group_by_move': 1, 'group_by':'move_id', 'search_default_posted':1, 'search_default_sales':1, 'name_groupby':1} - Journal Items + {'journal_type':'general', 'search_default_group_by_move': 1, 'group_by':'move_id', 'search_default_posted':1, 'search_default_sales':1, 'name_groupby':1} Journal Items account.move.line + [('display_type', 'not in', ('line_section', 'line_note'))] tree,pivot,graph,form,kanban @@ -568,6 +940,7 @@ {'journal_type':'sales', 'search_default_group_by_move': 1, 'group_by':'move_id', 'search_default_posted':1, 'search_default_sales':1, 'name_groupby':1} Sales account.move.line + [('display_type', 'not in', ('line_section', 'line_note'))] tree,pivot,graph,form,kanban @@ -576,6 +949,7 @@ {'journal_type':'purchase', 'search_default_group_by_move': 1, 'group_by':'move_id', 'search_default_posted':1, 'search_default_purchases':1, 'name_groupby':1} Purchase account.move.line + [('display_type', 'not in', ('line_section', 'line_note'))] tree,pivot,graph,form,kanban @@ -584,6 +958,7 @@ {'journal_type':'bank', 'search_default_group_by_move': 1, 'group_by':'move_id', 'search_default_posted':1, 'search_default_bank':1, 'search_default_cash':1, 'name_groupby':1} Bank and Cash account.move.line + [('display_type', 'not in', ('line_section', 'line_note'))] tree,pivot,graph,form,kanban @@ -592,6 +967,7 @@ {'journal_type':'general', 'search_default_group_by_move': 1, 'group_by':'move_id', 'search_default_posted':1, 'search_default_misc_filter':1, 'name_groupby':1} Miscellaneous account.move.line + [('display_type', 'not in', ('line_section', 'line_note'))] tree,pivot,graph,form,kanban @@ -600,6 +976,7 @@ {'journal_type':'general', 'search_default_group_by_account': 1, 'group_by':'account_id', 'search_default_posted':1} General Ledger account.move.line + [('display_type', 'not in', ('line_section', 'line_note'))] tree,pivot,graph,form,kanban @@ -608,13 +985,50 @@ {'journal_type':'general', 'search_default_group_by_partner': 1, 'group_by':'partner_id', 'search_default_posted':1, 'search_default_payable':1, 'search_default_receivable':1, 'search_default_unreconciled':1} Partner Ledger account.move.line + [('display_type', 'not in', ('line_section', 'line_note'))] tree,pivot,graph,form,kanban + + {'journal_type':'bank', 'search_default_group_by_move': 1, 'group_by':'move_id', 'search_default_posted':1, 'search_default_bank':1, 'search_default_cash':1, 'name_groupby':1} + Bank and Cash + account.move.line + [('display_type', 'not in', ('line_section', 'line_note'))] + + tree,pivot,graph,form,kanban + + + + {'journal_type':'general', 'search_default_group_by_move': 1, 'group_by':'move_id', 'search_default_posted':1, 'search_default_misc_filter':1, 'name_groupby':1} + Miscellaneous + account.move.line + [('display_type', 'not in', ('line_section', 'line_note'))] + + tree,pivot,graph,form,kanban + + + + {'journal_type':'general', 'search_default_group_by_account': 1, 'group_by':'account_id', 'search_default_posted':1} + General Ledger + account.move.line + [('display_type', 'not in', ('line_section', 'line_note'))] + + tree,pivot,graph,form,kanban + + + + {'journal_type':'general', 'search_default_group_by_partner': 1, 'group_by':'partner_id', 'search_default_posted':1, 'search_default_payable':1, 'search_default_receivable':1, 'search_default_unreconciled':1} + Partner Ledger + account.move.line + [('display_type', 'not in', ('line_section', 'line_note'))] + + + Journal Items account.move.line + [('display_type', 'not in', ('line_section', 'line_note'))] {'search_default_partner_id': [active_id], 'default_partner_id': active_id, 'search_default_posted':1} @@ -623,6 +1037,7 @@ {'journal_type':'general', 'search_default_posted':1} Journal Items account.move.line + [('display_type', 'not in', ('line_section', 'line_note'))] tree,pivot,graph,form,kanban @@ -631,6 +1046,7 @@ Journal Items graph,pivot + [('display_type', 'not in', ('line_section', 'line_note'))] {'search_default_account_id': [active_id]} account.move.line @@ -639,6 +1055,7 @@ Journal Items graph,pivot + [('display_type', 'not in', ('line_section', 'line_note'))] {'search_default_account_id': [active_id], 'search_default_posted': 1} account.move.line @@ -652,7 +1069,7 @@ tree,kanban,form - {'type': 'misc', 'search_default_misc_filter':1, 'view_no_maturity': True} + {'default_type': 'entry', 'search_default_misc_filter':1, 'view_no_maturity': True}

Create a journal entry @@ -663,6 +1080,117 @@ + + Invoices + account.move + form + tree,kanban,form + + + [('type', '=', 'out_invoice')] + {'default_type': 'out_invoice'} + +

+ Create a customer invoice +

+ Create invoices, register payments and keep track of the discussions with your customers. +

+
+ + + + Credit Notes + account.move + form + tree,kanban,form + + + [('type', '=', 'out_refund')] + {'default_type': 'out_refund'} + +

+ Create a credit note +

+ Note that the easiest way to create a credit note is to do it directly + from the customer invoice. +

+
+
+ + + Bills + account.move + form + tree,kanban,form + + + [('type', '=', 'in_invoice')] + {'default_type': 'in_invoice'} + +

+ Create a vendor bill +

+ Create invoices, register payments and keep track of the discussions with your vendors. +

+
+
+ + + Refund + account.move + form + tree,kanban,form + + + [('type', '=', 'in_refund')] + {'default_type': 'in_refund'} + +

+ Create a vendor credit note +

+ Note that the easiest way to create a vendor credit note it to do it directly from the vendor bill. +

+
+
+ + + Receipts + account.move + form + tree,kanban,form + + + [('type', '=', 'out_receipt')] + {'default_type': 'out_receipt'} + +

+ Create a new sales receipt +

+ When the sale receipt is confirmed, you can record the customer + payment related to this sales receipt. +

+
+
+ + + Receipts + account.move + form + tree,kanban,form + + + [('type', '=', 'in_receipt')] + {'default_type': 'in_receipt'} + +

+ Register a new purchase receipt +

+ When the purchase receipt is confirmed, you can record the + vendor payment related to this purchase receipt. +

+
+
+ Entries ir.actions.act_window @@ -682,6 +1210,42 @@ + + + + + + + + + + + +
- + @@ -301,7 +302,7 @@ Register Payment - + code action = model.action_register_payment() diff --git a/addons/account/views/account_portal_templates.xml b/addons/account/views/account_portal_templates.xml index f3c7787980383..ace31556b58bf 100644 --- a/addons/account/views/account_portal_templates.xml +++ b/addons/account/views/account_portal_templates.xml @@ -6,7 +6,7 @@ Invoices & Bills @@ -46,13 +46,13 @@ - - + + Draft Invoice - - + + Waiting for Payment @@ -64,7 +64,7 @@ Cancelled - + @@ -76,7 +76,7 @@ - + @@ -86,10 +86,10 @@

- +

-
+
@@ -104,15 +104,15 @@
-
  • +
  • Salesperson
    - Contact + Contact Contact
    diff --git a/addons/account/views/account_report.xml b/addons/account/views/account_report.xml index 17341cccc0e77..9a8dc5c5c2a28 100644 --- a/addons/account/views/account_report.xml +++ b/addons/account/views/account_report.xml @@ -4,24 +4,24 @@ diff --git a/addons/account/views/account_view.xml b/addons/account/views/account_view.xml index cc89e8b4a40e4..c70b70e7dc989 100644 --- a/addons/account/views/account_view.xml +++ b/addons/account/views/account_view.xml @@ -1,10 +1,6 @@ - - - - account.account.form account.account @@ -34,6 +30,7 @@ + account.account.list account.account @@ -97,6 +94,7 @@ + Chart of Accounts account.account @@ -225,7 +223,6 @@ - @@ -287,7 +284,6 @@ - diff --git a/addons/account/views/report_invoice.xml b/addons/account/views/report_invoice.xml index 0f83dee7c3900..4a31cff037c4e 100644 --- a/addons/account/views/report_invoice.xml +++ b/addons/account/views/report_invoice.xml @@ -10,13 +10,13 @@

    - Invoice + Invoice Draft Invoice Cancelled Invoice Credit Note Vendor Credit Note Vendor Bill - +

    @@ -24,17 +24,17 @@ Description:

    -
    +
    Invoice Date: -

    +

    -
    +
    Due Date: -

    +

    -
    +
    Source: -

    +

    Customer Code: @@ -76,10 +76,9 @@ - - + @@ -88,7 +87,7 @@ - + @@ -136,7 +135,7 @@ - + @@ -165,24 +164,24 @@
    -

    - Please use the following communication for your payment : +

    + Please use the following communication for your payment :

    -

    - +

    +

    -

    - +

    +

    -
    -

    +

    +

    Scan me with your banking app.

    - +

    -

    +

    The SEPA QR Code informations are not set correctly.

    @@ -192,7 +191,7 @@