[FIX] account: forbid making refunds on incompatible journals

It shouldn't be possible to create refunds on misc journals, as the tax report would compute the wrong multiplicator sign, and hence end up being entirely wrong.

Introducing the constraint also allowed finding an issue within _move_autocomplete_invoice_lines_create: some tests directly create an invoice by giving it line_ids instead of using the invoice_line_ids helper. The former implementation of this function made it so that the default type wasn't set in the context properly, and these tests were putting their invoices on misc journals. We fix this here as well.

closes odoo/odoo#67965

X-original-commit: fdc96de0e411760f84a78c36543f9157332346de
Signed-off-by: Laurent Smet <smetl@users.noreply.github.com>
Signed-off-by: oco-odoo <oco-odoo@users.noreply.github.com>
This commit is contained in:
oco-odoo
2021-03-16 14:23:32 +00:00
parent 9857b96b97
commit 758f09890a
2 changed files with 33 additions and 22 deletions
+6
View File
@@ -14066,6 +14066,12 @@ msgstr ""
msgid "technical field to compute starting balance correctly"
msgstr ""
#. module: account
#: code:addons/account/models/account_move.py:0
#, python-format
msgid "The chosen journal has a type that is not compatible with your invoice type. Sales operations should go to 'sale' journals, and purchase operations to 'purchase' ones."
msgstr ""
#. module: account
#: model_terms:ir.ui.view,arch_db:account.view_partner_property_form
msgid "the parent company"
+27 -22
View File
@@ -1698,6 +1698,14 @@ class AccountMove(models.Model):
raise UserError(message)
return True
@api.constrains('move_type', 'journal_id')
def _check_journal_type(self):
for record in self:
journal_type = record.journal_id.type
if record.is_sale_document() and journal_type != 'sale' or record.is_purchase_document() and journal_type != 'purchase':
raise ValidationError(_("The chosen journal has a type that is not compatible with your invoice type. Sales operations should go to 'sale' journals, and purchase operations to 'purchase' ones."))
# -------------------------------------------------------------------------
# LOW-LEVEL METHODS
# -------------------------------------------------------------------------
@@ -1754,30 +1762,15 @@ class AccountMove(models.Model):
'''
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('move_type') and not self._context.get('default_move_type'):
vals.pop('invoice_line_ids', None)
new_vals_list.append(vals)
continue
vals['move_type'] = vals.get('move_type', self._context.get('default_move_type', 'entry'))
if not vals['move_type'] in self.get_invoice_types(include_receipts=True):
new_vals_list.append(vals)
continue
vals['line_ids'] = vals.pop('invoice_line_ids')
vals = dict(vals)
if vals.get('invoice_date') and not vals.get('date'):
vals['date'] = vals['invoice_date']
ctx_vals = {'default_move_type': vals.get('move_type') or self._context.get('default_move_type')}
if vals.get('currency_id'):
ctx_vals['default_currency_id'] = vals['currency_id']
default_move_type = vals.get('move_type') or self._context.get('default_move_type')
ctx_vals = {}
if default_move_type:
ctx_vals['default_move_type'] = default_move_type
if vals.get('journal_id'):
ctx_vals['default_journal_id'] = vals['journal_id']
# reorder the companies in the context so that the company of the journal
@@ -1788,9 +1781,21 @@ class AccountMove(models.Model):
reordered_companies = sorted(allowed_companies, key=lambda cid: cid != journal_company.id)
ctx_vals['allowed_company_ids'] = reordered_companies
self_ctx = self.with_context(**ctx_vals)
new_vals = self_ctx._add_missing_default_values(vals)
vals = self_ctx._add_missing_default_values(vals)
move = self_ctx.new(new_vals)
is_invoice = vals.get('move_type') in self.get_invoice_types(include_receipts=True)
if 'line_ids' in vals:
vals.pop('invoice_line_ids', None)
new_vals_list.append(vals)
continue
if is_invoice and 'invoice_line_ids' in vals:
vals['line_ids'] = vals['invoice_line_ids']
vals.pop('invoice_line_ids', None)
move = self_ctx.new(vals)
new_vals_list.append(move._move_autocomplete_invoice_lines_values())
return new_vals_list