[FIX] account,point_of_sale: ImbalanceJournalEntryError issue

The crash manager doesn't recognize ImbalanceJournalEntryError as
a user error and when creating a journal entry that is not balanced,
a traceback is shown instead of the normal UserError dialog.

In this commit, we fix this issue by getting rid of the new error
and put back the use of UserError when checking the balance of an
account move. We also make sure that the feature in pos where we
show a wizard to unblock the user from closing session with
imbalance amount is intact.

closes odoo/odoo#67743

Signed-off-by: pimodoo <pimodoo@users.noreply.github.com>
This commit is contained in:
Joseph Caburnay
2021-03-12 09:12:45 +00:00
parent 78ae6df22d
commit 3cbed4d4f6
2 changed files with 22 additions and 39 deletions
+1 -9
View File
@@ -16,14 +16,6 @@ import json
import re
import warnings
class ImbalanceJournalEntryError(UserError):
"""Specialized UserError raised by `_check_balanced` method."""
def __init__(self, message, move_ids, imbalance_amounts):
super().__init__(message)
self.move_ids = move_ids
self.imbalance_amounts = imbalance_amounts
#forbidden fields
INTEGRITY_HASH_MOVE_FIELDS = ('date', 'journal_id', 'company_id')
@@ -1687,7 +1679,7 @@ class AccountMove(models.Model):
if query_res:
ids = [res[0] for res in query_res]
sums = [res[1] for res in query_res]
raise ImbalanceJournalEntryError(_("Cannot create unbalanced journal entry. Ids: %s\nDifferences debit - credit: %s") % (ids, sums), ids, sums)
raise UserError(_("Cannot create unbalanced journal entry. Ids: %s\nDifferences debit - credit: %s") % (ids, sums))
def _check_fiscalyear_lock_date(self):
for move in self:
+21 -30
View File
@@ -8,8 +8,6 @@ from odoo import api, fields, models, _
from odoo.exceptions import UserError, ValidationError
from odoo.tools import float_is_zero, float_compare
from odoo.addons.account.models.account_move import ImbalanceJournalEntryError
class PosSession(models.Model):
_name = 'pos.session'
_order = 'id desc'
@@ -306,25 +304,18 @@ class PosSession(models.Model):
self._check_if_no_draft_orders()
if self.update_stock_at_closing:
self._create_picking_at_end_of_session()
# Users without any accounting rights won't be able to create the journal entry. If this
# case, switch to sudo for creation and posting.
sudo = False
try:
if (
not self.env['account.move'].check_access_rights('create', raise_exception=False)
and self.user_has_groups('point_of_sale.group_pos_user')
):
sudo = True
self.sudo().with_company(self.company_id)._create_account_move(balancing_account, amount_to_balance)
else:
self.with_company(self.company_id)._create_account_move(balancing_account, amount_to_balance)
if self.move_id.line_ids:
# Set the uninvoiced orders' state to 'done'
self.env['pos.order'].search([('session_id', '=', self.id), ('state', '=', 'paid')]).write({'state': 'done'})
else:
self.move_id.unlink()
if (
not self.env['account.move'].check_access_rights('create', raise_exception=False)
and self.user_has_groups('point_of_sale.group_pos_user')
):
data = self.sudo().with_company(self.company_id)._create_account_move(balancing_account, amount_to_balance)
else:
data = self.with_company(self.company_id)._create_account_move(balancing_account, amount_to_balance)
except ImbalanceJournalEntryError as error:
try:
balance = sum(self.move_id.line_ids.mapped('balance'))
self.move_id._check_balanced()
except UserError:
# Creating the account move is just part of a big database transaction
# when closing a session. There are other database changes that will happen
# before attempting to create the account move, such as, creating the picking
@@ -333,12 +324,15 @@ class PosSession(models.Model):
# failed; therefore, we need to roll back this transaction before showing the
# close session wizard.
self.env.cr.rollback()
# When closing a session, single account move is checked which resulted to
# `ImbalanceJournalEntryError`. Thus, there should only be one value in imbalance_amounts
# which is what we need.
if len(error.imbalance_amounts) != 1:
raise UserError(_("Single imbalance amount that matches the pos session's account move is expected."))
return self._close_session_action(error.imbalance_amounts[0])
return self._close_session_action(balance)
if self.move_id.line_ids:
self.move_id._post()
self._reconcile_account_move_lines(data)
# Set the uninvoiced orders' state to 'done'
self.env['pos.order'].search([('session_id', '=', self.id), ('state', '=', 'paid')]).write({'state': 'done'})
else:
self.move_id.unlink()
self.write({'state': 'closed'})
return {
@@ -442,10 +436,7 @@ class PosSession(models.Model):
if balancing_account and amount_to_balance:
data = self._create_balancing_line(data, balancing_account, amount_to_balance)
if account_move.line_ids:
account_move._post()
data = self._reconcile_account_move_lines(data)
return data
def _accumulate_amounts(self, data):
# Accumulate the amounts for each accounting lines group