Skip to content

Commit

Permalink
[FIX] sale_unreconciled: account domain
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronHForgeFlow committed Dec 28, 2022
1 parent 8a3f835 commit f11fef3
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions sale_unreconciled/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -46,12 +49,15 @@ 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
rec.amount_unreconciled = sum(unreconciled_items.mapped("amount_residual"))
rec.amount_unreconciled = sum(unreconciled_items.mapped("amount_residual"))

def _search_unreconciled(self, operator, value):
if operator != "=" or not isinstance(value, bool):
Expand All @@ -72,8 +78,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")
Expand All @@ -95,8 +103,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"]
Expand Down Expand Up @@ -196,10 +206,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 = {
Expand Down

0 comments on commit f11fef3

Please sign in to comment.