From dd9e3e33bfecb1f3074368a5fcd1198bb08a3709 Mon Sep 17 00:00:00 2001 From: AaronHForgeFlow Date: Thu, 22 Dec 2022 16:22:13 +0100 Subject: [PATCH] [FIX] sale_unreconciled: account domain --- sale_unreconciled/models/account_move_line.py | 6 +- sale_unreconciled/models/sale_order.py | 55 ++++++++++++++----- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/sale_unreconciled/models/account_move_line.py b/sale_unreconciled/models/account_move_line.py index f2dba1441118..c447f64bf8cb 100644 --- a/sale_unreconciled/models/account_move_line.py +++ b/sale_unreconciled/models/account_move_line.py @@ -18,7 +18,9 @@ def _get_so_writeoff_amounts(self): writeoff_amount_curr = round( sum([line["amount_residual_currency"] for line in self]), precision ) - + if writeoff_amount_curr and not writeoff_amount: + # Data inconsistency, do not create the write-off + return (0.0, 0.0, True) first_currency = self[0]["currency_id"] if all([line["currency_id"] == first_currency for line in self]): same_curr = True @@ -37,6 +39,8 @@ def _create_so_writeoff(self, writeoff_vals): amount_writeoff_curr, same_curr, ) = self._get_so_writeoff_amounts() + if not amount_writeoff: + return self.env["account.move.line"] partners = self.mapped("partner_id") write_off_vals = { "name": _("Automatic writeoff"), diff --git a/sale_unreconciled/models/sale_order.py b/sale_unreconciled/models/sale_order.py index e53ea7b7c14d..93e1678ccf06 100644 --- a/sale_unreconciled/models/sale_order.py +++ b/sale_unreconciled/models/sale_order.py @@ -18,25 +18,28 @@ class SaleOrder(models.Model): ) amount_unreconciled = fields.Float(compute="_compute_unreconciled") - @api.model - def _get_sale_unreconciled_base_domain(self): + def _get_account_domain(self): + self.ensure_one() included_accounts = ( ( - self.env["product.category"].search( - [("property_valuation", "=", "real_time")] - ) + self.env["product.category"] + .with_company(self.company_id.id) + .search([("property_valuation", "=", "real_time")]) ) .mapped("property_stock_account_output_categ_id") .ids ) + return [("account_id", "in", included_accounts)] + + @api.model + def _get_sale_unreconciled_base_domain(self): unreconciled_domain = [ ("account_id.reconcile", "=", True), - ("account_id", "in", included_accounts), + ("account_id.internal_type", "not in", ["receivable", "payable"]), ("move_id.state", "=", "posted"), # for some reason when amount_residual is zero # is marked as reconciled, this is better check - ("amount_residual", "!=", 0.0), - ("company_id", "in", self.env.companies.ids), + ("full_reconcile_id", "=", False), ] return unreconciled_domain @@ -46,8 +49,10 @@ def _compute_unreconciled(self): domain = rec.with_company( rec.company_id )._get_sale_unreconciled_base_domain() + domain_account = rec._get_account_domain() + unreconciled_domain = expression.AND([domain, domain_account]) unreconciled_domain = expression.AND( - [domain, [("sale_order_id", "=", rec.id)]] + [unreconciled_domain, [("sale_order_id", "=", rec.id)]] ) unreconciled_items = acc_item.search(unreconciled_domain) rec.unreconciled = len(unreconciled_items) > 0 @@ -72,8 +77,10 @@ def action_view_unreconciled(self): domain = self.with_company( self.company_id.id )._get_sale_unreconciled_base_domain() + domain_account = self._get_account_domain() + unreconciled_domain = expression.AND([domain, domain_account]) unreconciled_domain = expression.AND( - [domain, [("sale_order_id", "=", self.id)]] + [unreconciled_domain, [("sale_order_id", "=", self.id)]] ) unreconciled_items = acc_item.search(unreconciled_domain) action = self.env.ref("account.action_account_moves_all") @@ -95,8 +102,10 @@ def action_reconcile(self): self.ensure_one() acc_item = self.env["account.move.line"] domain = self._get_sale_unreconciled_base_domain() + domain_account = self._get_account_domain() + unreconciled_domain = expression.AND([domain, domain_account]) unreconciled_domain = expression.AND( - [domain, [("sale_order_id", "=", self.id)]] + [unreconciled_domain, [("sale_order_id", "=", self.id)]] ) unreconciled_items = acc_item.search(unreconciled_domain) writeoff_to_reconcile = self.env["account.move.line"] @@ -144,8 +153,16 @@ def action_reconcile(self): # Check if reconciliation is total or needs an exchange rate entry to be created if remaining_moves: remaining_moves.filtered( - lambda l: l.amount_residual != 0.0 - ).reconcile() + lambda l: l.balance != 0.0 + ).remove_move_reconcile() + remaining_moves.filtered(lambda l: l.balance != 0.0).reconcile() + # There are some journal items that are zero balance that shows + # as unreconciled, we just attached the full reconcile just created + full_reconcile_id = remaining_moves.mapped("full_reconcile_id") + full_reconcile_id = full_reconcile_id and full_reconcile_id[0] + remaining_moves.filtered( + lambda l: l.balance == 0.0 and not l.full_reconcile_id + ).write({"full_reconcile_id": full_reconcile_id}) reconciled_ids = unreconciled_items | all_writeoffs return { "name": _("Reconciled journal items"), @@ -196,10 +213,18 @@ def sale_unreconciled_exception(self, exception_msg=None): def unreconciled_exception_msg(self): self.ensure_one() exception_msg = "" + amount_total = self.amount_total + if self.currency_id and self.company_id.currency_id != self.currency_id: + amount_total = self.currency_id._convert( + amount_total, + self.company_id.currency_id, + self.company_id, + fields.Date.today(), + ) if ( self.company_id.sale_reconcile_tolerance - and self.amount_total - and abs(self.amount_unreconciled / self.amount_total) + and amount_total + and abs(self.amount_unreconciled / amount_total) >= self.company_id.sale_reconcile_tolerance / 100.0 ): params = {