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 Jan 11, 2023
1 parent 8a3f835 commit dd9e3e3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
6 changes: 5 additions & 1 deletion sale_unreconciled/models/account_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"),
Expand Down
55 changes: 40 additions & 15 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,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
Expand All @@ -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")
Expand 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"]
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit dd9e3e3

Please sign in to comment.