diff --git a/addons/account/i18n/account.pot b/addons/account/i18n/account.pot index e820a124278..8e79d5ad58a 100644 --- a/addons/account/i18n/account.pot +++ b/addons/account/i18n/account.pot @@ -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" diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index fcf81cc932d..3dcea5b140a 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -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