summaryrefslogtreecommitdiff
path: root/addons/account/models/account_move.py
diff options
context:
space:
mode:
authoroco-odoo <oco@odoo.com>2021-03-10 13:59:02 +0000
committeroco-odoo <oco@odoo.com>2021-03-15 16:33:47 +0000
commitfdc96de0e411760f84a78c36543f9157332346de (patch)
treea6280a98a09b251707a41fda7165f0dfe2f460b3 /addons/account/models/account_move.py
parent26a43f23ec2b2490aec0731e25568703729581f0 (diff)
[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#67667 X-original-commit: 03e87628e364f6df9bf43ac0de7469f3253b329f Signed-off-by: Laurent Smet <smetl@users.noreply.github.com> Signed-off-by: oco-odoo <oco-odoo@users.noreply.github.com>
Diffstat (limited to 'addons/account/models/account_move.py')
-rw-r--r--addons/account/models/account_move.py49
1 files changed, 27 insertions, 22 deletions
diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py
index 78c040c6b3d..b54f5c67e3c 100644
--- a/addons/account/models/account_move.py
+++ b/addons/account/models/account_move.py
@@ -1694,6 +1694,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
# -------------------------------------------------------------------------
@@ -1750,30 +1758,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
@@ -1784,9 +1777,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)
+
+ 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(new_vals)
+ move = self_ctx.new(vals)
new_vals_list.append(move._move_autocomplete_invoice_lines_values())
return new_vals_list