Skip to content

Commit

Permalink
[IMP] account_payment_batch_process: prefer grouping payments per inv…
Browse files Browse the repository at this point in the history
…oice
  • Loading branch information
GSLabIt authored and Chionne27 committed Jan 22, 2025
1 parent 390d8d1 commit f394ad3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
57 changes: 31 additions & 26 deletions account_payment_batch_process/wizard/account_payment_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
)
num2words = None


MAP_INVOICE_TYPE_PARTNER_TYPE = {
"out_invoice": "customer",
"out_refund": "customer",
Expand Down Expand Up @@ -58,6 +57,11 @@ def _compute_cheque_amount(self):
readonly=False,
)
total_amount = fields.Float("Total Invoices:", compute="_compute_total")
group_by_partner = fields.Boolean(
string="Group By Partner",
default=False,
help="Enable grouping payments by partner",
)

def get_invoice_payment_line(self, invoice):
return (
Expand Down Expand Up @@ -258,7 +262,7 @@ def get_payment_invoice_value(self, name, data_get):
}

def update_group_pay_data(
self, partner_id, group_data, data_get, check_amount_in_words
self, group_id, group_data, data_get, check_amount_in_words
):
# build memo value
if self.communication:
Expand All @@ -283,8 +287,8 @@ def update_group_pay_data(
}
group_data.update(
{
partner_id: {
"partner_id": partner_id,
group_id: {
"partner_id": data_get.invoice_id.partner_id.id,
"partner_type": MAP_INVOICE_TYPE_PARTNER_TYPE[
data_get.invoice_id.move_type
],
Expand All @@ -299,27 +303,28 @@ def update_group_pay_data(

def get_amount(self, memo, group_data, line):
line.payment_difference = line.balance - line.amount
partner_id = line.invoice_id.partner_id.id
if partner_id in group_data:
old_total = group_data[partner_id]["total"]
group_id = line.invoice_id.id

Check warning on line 306 in account_payment_batch_process/wizard/account_payment_register.py

View check run for this annotation

Codecov / codecov/patch

account_payment_batch_process/wizard/account_payment_register.py#L306

Added line #L306 was not covered by tests
if self.group_by_partner:
group_id = line.invoice_id.partner_id.id

Check warning on line 308 in account_payment_batch_process/wizard/account_payment_register.py

View check run for this annotation

Codecov / codecov/patch

account_payment_batch_process/wizard/account_payment_register.py#L308

Added line #L308 was not covered by tests

if group_id in group_data:
old_total = group_data[group_id]["total"]

Check warning on line 311 in account_payment_batch_process/wizard/account_payment_register.py

View check run for this annotation

Codecov / codecov/patch

account_payment_batch_process/wizard/account_payment_register.py#L311

Added line #L311 was not covered by tests
# build memo value
if self.communication:
memo = (
group_data[partner_id]["memo"]
group_data[group_id]["memo"]
+ " : "
+ self.communication
+ "-"
+ str(line.invoice_id.name)
)
else:
memo = (
group_data[partner_id]["memo"] + " : " + str(line.invoice_id.name)
)
memo = group_data[group_id]["memo"] + " : " + str(line.invoice_id.name)

Check warning on line 322 in account_payment_batch_process/wizard/account_payment_register.py

View check run for this annotation

Codecov / codecov/patch

account_payment_batch_process/wizard/account_payment_register.py#L322

Added line #L322 was not covered by tests
# Calculate amount in words
check_amount_in_words = self.total_amount_in_words(line, old_total)
group_data[partner_id].update(
group_data[group_id].update(

Check warning on line 325 in account_payment_batch_process/wizard/account_payment_register.py

View check run for this annotation

Codecov / codecov/patch

account_payment_batch_process/wizard/account_payment_register.py#L325

Added line #L325 was not covered by tests
{
"partner_id": partner_id,
"partner_id": line.invoice_id.partner_id.id,
"partner_type": MAP_INVOICE_TYPE_PARTNER_TYPE[
line.invoice_id.move_type
],
Expand All @@ -339,13 +344,13 @@ def get_amount(self, memo, group_data, line):
name = "Counterpart"
# Update with payment diff data
inv_val = self.get_payment_invoice_value(name, line)
group_data[partner_id]["inv_val"].update({line.invoice_id.id: inv_val})
group_data[group_id]["inv_val"].update({line.invoice_id.id: inv_val})

Check warning on line 347 in account_payment_batch_process/wizard/account_payment_register.py

View check run for this annotation

Codecov / codecov/patch

account_payment_batch_process/wizard/account_payment_register.py#L347

Added line #L347 was not covered by tests
else:
# calculate amount in words
check_amount_in_words = self.total_amount_in_words(line, 0)
# prepare name
self.update_group_pay_data(
partner_id, group_data, line, check_amount_in_words
group_id, group_data, line, check_amount_in_words
)

def _reconcile_open_invoices(
Expand Down Expand Up @@ -395,16 +400,16 @@ def make_payments(self):
context.update({"group_data": group_data})
# making partner wise payment
payment_ids = []
for partner in list(group_data):
for group in list(group_data):
# update active_ids with active invoice ids
if context.get("active_ids", False) and group_data[partner].get(
if context.get("active_ids", False) and group_data[group].get(
"inv_val", False
):
context.update({"active_ids": list(group_data[partner]["inv_val"])})
context.update({"active_ids": list(group_data[group]["inv_val"])})

Check warning on line 408 in account_payment_batch_process/wizard/account_payment_register.py

View check run for this annotation

Codecov / codecov/patch

account_payment_batch_process/wizard/account_payment_register.py#L408

Added line #L408 was not covered by tests
payment = (
self.env["account.payment"] # pylint: disable=context-overridden
.with_context(context)
.create(self.get_payment_values(group_data=group_data[partner]))
.create(self.get_payment_values(group_data=group_data[group]))
)
payment_ids.append(payment.id)
payment.action_post()
Expand All @@ -421,13 +426,13 @@ def make_payments(self):
[("account_id", "=", account.id), ("reconciled", "=", False)]
).reconcile()
if any(
group_data[partner]["inv_val"][inv.id]["payment_difference_handling"]
group_data[group]["inv_val"][inv.id]["payment_difference_handling"]
== "open"
for inv in invoices
):
for inv in invoices:
if (
group_data[partner]["inv_val"][inv.id][
group_data[group]["inv_val"][inv.id][
"payment_difference_handling"
]
== "open"
Expand All @@ -438,7 +443,7 @@ def make_payments(self):
for line in inv.line_ids:
if line.amount_residual > 0 and payment_state == "paid":
line_amount = 0.0
partial_amount = group_data[partner]["inv_val"][inv.id][
partial_amount = group_data[group]["inv_val"][inv.id][

Check warning on line 446 in account_payment_batch_process/wizard/account_payment_register.py

View check run for this annotation

Codecov / codecov/patch

account_payment_batch_process/wizard/account_payment_register.py#L446

Added line #L446 was not covered by tests
"amount"
]
self._reconcile_open_invoices(
Expand All @@ -453,10 +458,10 @@ def make_payments(self):
)
continue
if line.reconciled and payment_state == "partial":
line_amount = group_data[partner]["inv_val"][inv.id][
line_amount = group_data[group]["inv_val"][inv.id][

Check warning on line 461 in account_payment_batch_process/wizard/account_payment_register.py

View check run for this annotation

Codecov / codecov/patch

account_payment_batch_process/wizard/account_payment_register.py#L461

Added line #L461 was not covered by tests
"payment_difference"
]
partial_amount = group_data[partner]["inv_val"][inv.id][
partial_amount = group_data[group]["inv_val"][inv.id][

Check warning on line 464 in account_payment_batch_process/wizard/account_payment_register.py

View check run for this annotation

Codecov / codecov/patch

account_payment_batch_process/wizard/account_payment_register.py#L464

Added line #L464 was not covered by tests
"amount"
]
self._reconcile_open_invoices(
Expand All @@ -473,10 +478,10 @@ def make_payments(self):
inv.update(
{
"payment_state": payment_state,
"amount_residual": group_data[partner]["inv_val"][inv.id][
"amount_residual": group_data[group]["inv_val"][inv.id][
"payment_difference"
],
"amount_residual_signed": group_data[partner]["inv_val"][
"amount_residual_signed": group_data[group]["inv_val"][
inv.id
]["payment_difference"],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
>Customer Invoices Being Paid</h3>
</div>
<group invisible="not context.get('batch', False)">
<group>
<field name="group_by_partner" />
<button
name="auto_fill_payments"
string="Auto-Fill Pay Amount"
type="object"
class="oe_highlight"
/>
name="auto_fill_payments"
string="Auto-Fill Pay Amount"
type="object"
class="oe_highlight"
/>
</group>
</group>
<notebook invisible="not context.get('batch', False)">
<page name="invoice_payments" string="Invoice Payments">
Expand Down

0 comments on commit f394ad3

Please sign in to comment.