Skip to content

Commit

Permalink
[MIG] account_cash_discount_write_off: migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
AnizR committed Mar 27, 2023
1 parent 107b6af commit f3532b9
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 412 deletions.
4 changes: 2 additions & 2 deletions account_cash_discount_write_off/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
"summary": """
Create an automatic writeoff for payment with discount on the payment
order confirmation""",
"version": "14.0.1.0.0",
"version": "16.0.1.0.0",
"license": "AGPL-3",
"author": "ACSONE SA/NV, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/account-payment",
"depends": ["account_cash_discount_payment"],
"data": ["views/res_company.xml"],
"data": [],
"demo": [],
}
2 changes: 0 additions & 2 deletions account_cash_discount_write_off/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
from . import account_payment_line
from . import account_payment_order
from . import res_company
177 changes: 41 additions & 136 deletions account_cash_discount_write_off/models/account_payment_line.py
Original file line number Diff line number Diff line change
@@ -1,154 +1,59 @@
# Copyright 2018 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, models
from odoo.exceptions import UserError
from odoo.tools import float_compare, float_is_zero, float_round
from odoo import models


class PaymentLine(models.Model):

_inherit = "account.payment.line"

def _check_cash_discount_write_off_creation(self):
self.ensure_one()
create_write_off = (
self.pay_with_discount
and self.move_line_id
and self.move_line_id.move_id
and self.move_line_id.move_id.has_discount
def _prepare_account_payment_vals(self):
"""Prepare the dictionary to create an account payment record from a set of
payment lines.
"""

payment_vals = super()._prepare_account_payment_vals()
currency = self.currency_id
conversion_rate = self.env["res.currency"]._get_conversion_rate(
currency,
self.company_id.currency_id,
self.company_id,
self.date,
)
if not create_write_off:
return False

invoice = self.move_line_id.move_id
refunds_amount_total = invoice._get_refunds_amount_total()["total"]

# Invoice residual has already changed so we need to compute
# the residual with discount before the first reconciliation
amount_to_pay = (
invoice.amount_total
- refunds_amount_total
- invoice.discount_amount
+ invoice.refunds_discount_amount
)
return (
float_compare(
self.amount_currency,
amount_to_pay,
precision_rounding=self.currency_id.rounding,
)
== 0
)

def get_cash_discount_writeoff_move_values(self):
self.ensure_one()
move_line = self.move_line_id
partner = move_line.partner_id
invoice = move_line.move_id
company = invoice.company_id
tax_adjustment = company.cash_discount_use_tax_adjustment
rounding = self.currency_id.rounding

woff_account = False
woff_journal = False

if invoice:
company = invoice.company_id
woff_account = company.default_cash_discount_writeoff_account_id
woff_journal = company.default_cash_discount_writeoff_journal_id

if not woff_account or not woff_journal:
raise UserError(
_(
"You have to fill in journal and account for cash discount "
"write-off on the company."
)
)

move_line_name = _("Cash Discount Write-Off")
supplier_account_amount = (
invoice.discount_amount - invoice.refunds_discount_amount
)
discount_amount_credit = supplier_account_amount

lines_values = [
{
"partner_id": partner.id,
"name": move_line_name,
"debit": supplier_account_amount,
"account_id": move_line.account_id.id,
}
]

if tax_adjustment:
refund_moves = (
invoice._get_payment_move_lines()
.filtered(lambda line: line.move_id.move_type == "in_refund")
.mapped("move_id")
)
target_move_ids = refund_moves.ids + [move_line.move_id.id]

tax_move_lines = self.env["account.move.line"].search(
[
("move_id", "in", target_move_ids),
"|",
("tax_line_id", "!=", False),
("tax_ids", "!=", False),
]
epd_aml_values_list = []
aml = self.move_line_id
if aml._is_eligible_for_early_payment_discount(currency, self.date):
epd_aml_values_list.append(
{
"aml": aml,
"amount_currency": -aml.amount_residual_currency,
"balance": aml.company_currency_id.round(
-aml.amount_residual_currency * conversion_rate
),
}
)

for tax_move_line in tax_move_lines:
tax_invoice = tax_move_line.move_id
amount = float_round(
abs(tax_move_line.balance) * tax_invoice.discount_percent / 100.0,
precision_rounding=rounding,
)
if tax_move_line.credit > 0:
discount_amount_credit += amount
elif tax_move_line.debit > 0:
discount_amount_credit -= amount
# compute open amount
invoice = self.move_line_id.move_id
refunds_amount_total = 0
sign = -1 if invoice.payment_mode_id.payment_type == "outbound" else 1
for invoice_line in invoice.line_ids:
refunds_amount_total += (invoice_line.discount_amount_currency) * sign

if tax_move_line.tax_line_id:
account = tax_move_line.account_id
else:
account = woff_account
lines_values.append(
{
"partner_id": partner.id,
"name": move_line_name,
"debit": tax_move_line.credit > 0 and amount or 0.0,
"credit": tax_move_line.debit > 0 and amount or 0.0,
"account_id": account.id,
"tax_repartition_line_id": (
tax_move_line.tax_repartition_line_id.id
),
"tax_ids": [(6, 0, tax_move_line.tax_ids.ids)],
"tax_tag_ids": [(6, 0, tax_move_line.tax_tag_ids.ids)],
}
)
open_amount_currency = (invoice.amount_total - refunds_amount_total) * sign

amount_left = not float_is_zero(
discount_amount_credit, precision_rounding=rounding
)
if amount_left:
writeoff_amount = float_round(
abs(discount_amount_credit),
precision_rounding=rounding,
open_balance = self.company_id.currency_id.round(
open_amount_currency * conversion_rate
)
lines_values.append(
{
"partner_id": partner.id,
"name": move_line_name,
"credit": (writeoff_amount if discount_amount_credit > 0 else 0.0),
"debit": (writeoff_amount if discount_amount_credit < 0 else 0.0),
"account_id": woff_account.id,
}
early_payment_values = self.env[
"account.move"
]._get_invoice_counterpart_amls_for_early_payment_discount(
epd_aml_values_list, open_balance
)
payment_vals["write_off_line_vals"] = []
for aml_values_list in early_payment_values.values():
payment_vals["write_off_line_vals"] += aml_values_list

move_values = {
"journal_id": woff_journal.id,
"partner_id": partner.id,
"line_ids": [(0, 0, values) for values in lines_values],
}
return move_values
return payment_vals
41 changes: 0 additions & 41 deletions account_cash_discount_write_off/models/account_payment_order.py

This file was deleted.

16 changes: 0 additions & 16 deletions account_cash_discount_write_off/models/res_company.py

This file was deleted.

Loading

0 comments on commit f3532b9

Please sign in to comment.