diff --git a/account_banking_ach_discount/models/account_move_line.py b/account_banking_ach_discount/models/account_move_line.py index 683cdf18..3ac68887 100644 --- a/account_banking_ach_discount/models/account_move_line.py +++ b/account_banking_ach_discount/models/account_move_line.py @@ -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 @@ -25,7 +27,7 @@ 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] @@ -33,7 +35,7 @@ def _prepare_payment_line_vals(self, payment_order): { "discount_amount": discount_amt, "amount_currency": amount_currency - discount_amt, + "writeoff_account_id": discount_information[1] } ) - return vals diff --git a/account_banking_ach_discount/models/account_payment_order.py b/account_banking_ach_discount/models/account_payment_order.py index 4a218808..8f55977b 100644 --- a/account_banking_ach_discount/models/account_payment_order.py +++ b/account_banking_ach_discount/models/account_payment_order.py @@ -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: @@ -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 diff --git a/account_banking_ach_discount/models/bank_payment_line.py b/account_banking_ach_discount/models/bank_payment_line.py index d1f4a1da..9ee03c19 100644 --- a/account_banking_ach_discount/models/bank_payment_line.py +++ b/account_banking_ach_discount/models/bank_payment_line.py @@ -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() diff --git a/account_banking_ach_discount/views/account_payment_view.xml b/account_banking_ach_discount/views/account_payment_view.xml index 9c8350a7..39f12d8c 100644 --- a/account_banking_ach_discount/views/account_payment_view.xml +++ b/account_banking_ach_discount/views/account_payment_view.xml @@ -33,13 +33,12 @@ /> - - - - - - + + + + +