Skip to content

Commit

Permalink
Refactored _compute_upflow_type
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandregaldeano authored and petrus-v committed Sep 4, 2024
1 parent 01bce11 commit 40ddabf
Showing 1 changed file with 36 additions and 54 deletions.
90 changes: 36 additions & 54 deletions base_upflow/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import base64
import logging

from odoo import _, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo.tools.float_utils import float_compare

Expand Down Expand Up @@ -42,59 +42,41 @@ class AccountMove(models.Model):

def _compute_upflow_type(self):
for move in self:
if move.move_type.startswith("in_") or move.state != "posted":
move.upflow_type = "none"
continue
if move.move_type == "out_invoice":
move.upflow_type = "invoices"
elif move.move_type == "out_refund":
move.upflow_type = "creditNotes"
else:
receivables_lines = move.line_ids.filtered(
lambda line: line.account_id.user_type_id.type == "receivable"
)
if not receivables_lines:
move.upflow_type = "none"
continue
debit = sum(receivables_lines.mapped("debit"))
credit = sum(receivables_lines.mapped("credit"))
if (
float_compare(
debit,
0,
precision_rounding=move.currency_id.rounding,
)
== 0
and float_compare(
credit,
0,
precision_rounding=move.currency_id.rounding,
)
!= 0
):
move.upflow_type = "payments"
elif (
float_compare(
debit,
0,
precision_rounding=move.currency_id.rounding,
)
!= 0
and float_compare(
credit,
0,
precision_rounding=move.currency_id.rounding,
)
== 0
):
move.upflow_type = "refunds"
else:
_logger.error(
"Sum of receivable move lines on %s have credit (%d) and debit(%d). "
"Which sounds suspicious and can't set upflow type",
move.name,
)
move.upflow_type = "none"
move.upflow_type = self._get_upflow_type_for_move(move)

@api.model
def _get_upflow_type_for_move(self, move):
if move.move_type.startswith("in_") or move.state != "posted":
return "none"
if move.move_type == "out_invoice":
return "invoices"
if move.move_type == "out_refund":
return "creditNotes"
receivables_lines = move.line_ids.filtered(
lambda line: line.account_id.user_type_id.type == "receivable"
)
if not receivables_lines:
return "none"
rounding = move.currency_id.rounding

debit = sum(receivables_lines.mapped("debit"))
has_debit = float_compare(debit, 0, precision_rounding=rounding) != 0

credit = sum(receivables_lines.mapped("credit"))
has_credit = float_compare(credit, 0, precision_rounding=rounding) != 0

if not has_debit and has_credit:
return "payments"
if has_debit and not has_credit:
return "refunds"
_logger.error(
"Sum of receivable move lines on %s have credit (%d) and debit(%d). "
"Which sounds suspicious and can't set upflow type",
move.name,
credit,
debit,
)
return "none"

def _compute_upflow_commercial_partner_id(self):
# while using OD as counter part or bank statement
Expand Down

0 comments on commit 40ddabf

Please sign in to comment.