Skip to content

Commit

Permalink
Merge branch '14.0-MIG-account_banking_ach_discount' into 14.0-MIG-ac…
Browse files Browse the repository at this point in the history
…count_banking_ach_discount-unbalanced-entry
  • Loading branch information
atchuthan authored Apr 13, 2022
2 parents f70a04f + 92a2c27 commit 7c519f5
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 30 deletions.
10 changes: 6 additions & 4 deletions account_banking_ach_discount/models/account_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ def _prepare_payment_line_vals(self, payment_order):
amount_currency = vals.get("amount_currency")
# No discount for open invoices
if (
"payment_line_state" in self._context
and self._context.get("payment_line_state") != "open"
("payment_line_state" in self._context
and self._context.get("payment_line_state") != "open")
or self._context.get("is_new_order")
or self._context.get("is_update_order")
):
if (
invoice
Expand All @@ -25,15 +27,15 @@ def _prepare_payment_line_vals(self, payment_order):
discount_information = (
invoice.invoice_payment_term_id._check_payment_term_discount(
invoice,
self._context.get("payment_date") or invoice.date_invoice,
self._context.get("payment_date") or invoice.invoice_date,
)
)
discount_amt = discount_information[0]
vals.update(
{
"discount_amount": discount_amt,
"amount_currency": amount_currency - discount_amt,
"writeoff_account_id": discount_information[1]
}
)

return vals
97 changes: 89 additions & 8 deletions account_banking_ach_discount/models/account_payment_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,64 @@ def _prepare_move(self, bank_lines=None):

temp_vals = vals[2].copy()
amount = line.amount_currency
total_amount = line.total_amount
amount_difference = round((total_amount - amount), 2)
payment_difference = amount_difference
writeoff = payment_difference or 0.0
invoice_close = line.payment_difference_handling != "open"
discount = line.discount_amount
payment_difference = line.payment_difference
writeoff = 0.0
invoice_close = False
if payment_difference:
writeoff = (
payment_difference and payment_difference - discount or 0.0
)
invoice_close = line.payment_difference_handling != "open"
use_debit = line.move_id.move_type in (
"in_invoice",
"out_refund",
)
temp_vals["move_id"] = line.move_id.id
if use_debit:
temp_vals["debit"] = total_amount - payment_difference
temp_vals["debit"] = amount + discount
else:
temp_vals["credit"] = total_amount - payment_difference
temp_vals["credit"] = amount + discount

line_ids.append((0, 0, temp_vals))

if invoice_close:
if discount > 0:
if payment_difference:
discount_information = line.move_id.invoice_payment_term_id._check_payment_term_discount(
line.move_id, line.date
)
discount_vals = temp_vals.copy()
discount_vals["account_id"] = discount_information[1]
discount_vals["name"] = "Early Pay Discount"
if use_debit:
discount_vals["debit"] = 0.0
discount_vals["credit"] = discount_information[0]
else:
discount_vals["credit"] = 0.0
discount_vals["debit"] = discount_information[0]
discount_vals["bank_payment_line_id"] = False
if discount_vals:
line_ids.append((0, 0, discount_vals))
# Discount Taken Update
line.move_id.discount_taken = discount
else:
#Case: If user Manually enters discount amount
discount_vals = temp_vals.copy()
discount_vals["account_id"] = line.writeoff_account_id and line.writeoff_account_id.id or False
discount_vals["name"] = "Early Pay Discount"
if use_debit:
discount_vals["debit"] = 0.0
discount_vals["credit"] = discount
else:
discount_vals["credit"] = 0.0
discount_vals["debit"] = discount
discount_vals["bank_payment_line_id"] = False
if discount_vals:
line_ids.append((0, 0, discount_vals))
# Discount Taken Update
line.move_id.discount_taken = discount

if invoice_close and round(writeoff, 2):
if use_debit:
temp_vals["debit"] = amount + payment_difference
else:
Expand All @@ -51,5 +91,46 @@ def _prepare_move(self, bank_lines=None):
# payment order line
else:
line_ids.append(vals)
if line_ids:
line_ids = self._prepare_ach_payment_move(values)
values["line_ids"] = line_ids
return values

def _prepare_ach_payment_move(self,values):
bank_payment_line_obj = self.env["bank.payment.line"]
line_vals = []
for vals in values.get("line_ids"):
if "bank_payment_line_id" in vals[2] and vals[2]["bank_payment_line_id"]:
bank_payment_id = vals[2].get("bank_payment_line_id")
account_bank_payment = bank_payment_line_obj.browse(bank_payment_id)
for payment_line in account_bank_payment.payment_line_ids:
temp_vals = vals[2].copy()
amount = payment_line.amount_currency
total_amount = payment_line.total_amount
amount_difference = round((total_amount - amount), 2)
payment_difference = amount_difference
writeoff = payment_difference or 0.0
invoice_close = payment_line.payment_difference_handling != "open"
use_debit = payment_line.move_id.move_type in (
"in_invoice",
"out_refund",
)
temp_vals["move_id"] = payment_line.move_id.id
if use_debit:
temp_vals["debit"] = total_amount - payment_difference
else:
temp_vals["credit"] = total_amount - payment_difference
line_vals.append((0, 0, temp_vals))
if invoice_close:
if use_debit:
temp_vals["debit"] = amount + payment_difference
else:
temp_vals["credit"] = amount + payment_difference
if round(writeoff, 2):
writeoff_vals = payment_line.move_id._prepare_writeoff_move_line(payment_line, temp_vals.copy())
writeoff_vals["bank_payment_line_id"] = False
if writeoff_vals:
line_vals.append((0, 0, writeoff_vals))
else:
line_vals.append(vals)
return line_vals
12 changes: 0 additions & 12 deletions account_banking_ach_discount/models/bank_payment_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,3 @@ def _compute_discount_amount(self):
for bline in self:
discount_amount = sum(bline.mapped("payment_line_ids.discount_amount"))
bline.discount_amount = discount_amount

# def reconcile(self):
# self.ensure_one()
# amlo = self.env["account.move.line"]
# transit_mlines = amlo.search([("bank_payment_line_id", "=", self.id)])
# for line in transit_mlines:
# ap_mlines = line.move_id.line_ids.filtered(
# lambda x: x.account_id == line.account_id
# )
# lines_to_rec = line
# lines_to_rec += ap_mlines
# lines_to_rec.reconcile()
11 changes: 5 additions & 6 deletions account_banking_ach_discount/views/account_payment_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@
/>
<field name="arch" type="xml">
<field name="amount_currency" position="after">
<field name="discount_amount" />
<field name="total_amount" />
<field name="payment_difference_handling" />
<field name="writeoff_account_id" />
<field name="reason_code" />
<field name="note" />
<field name="move_id" />
<field name="discount_amount" />
<field name="writeoff_account_id" string="Discount Account"/>
<field name="reason_code" optional="hide"/>
<field name="note" optional="hide"/>
<field name="move_id" optional="hide"/>
</field>
</field>
</record>
Expand Down

0 comments on commit 7c519f5

Please sign in to comment.