Skip to content

Commit

Permalink
[ADD] fixed date functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoParadeda committed Jun 17, 2024
1 parent 1b97b73 commit 74f6b83
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 3 deletions.
60 changes: 59 additions & 1 deletion account_payment_term_manual/models/account_payment_term.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Copyright 2024 KMEE
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import models
from dateutil.relativedelta import relativedelta

from odoo import fields, models


class AccountPaymentTerm(models.Model):
Expand All @@ -15,6 +17,8 @@ def compute(self, value, date_ref=False, currency=None):
manual_payment_term_id must have manual lines (else just use normal term_id)
if self == manual_payment_term_id there's no need to override this method
complete override for terms with fixed dates > look for alternatives
"""
manual_payment_term_id = self.env.context.get("manual_payment_term_id")
if (
Expand All @@ -25,4 +29,58 @@ def compute(self, value, date_ref=False, currency=None):
return manual_payment_term_id.compute(
value, date_ref=date_ref, currency=currency
)

if "custom" in self.line_ids.mapped("option"):
# No alternative other than complete method override
# can't call super
return self.compute_override(value, date_ref=date_ref, currency=currency)

return super().compute(value, date_ref=date_ref, currency=currency)

def compute_override(self, value, date_ref=False, currency=None):
"""
Full override of compute method to fixed date manual lines if set.
"""
self.ensure_one()
date_ref = date_ref or fields.Date.context_today(self)
amount = value
sign = value < 0 and -1 or 1
result = []
if not currency and self.env.context.get("currency_id"):
currency = self.env["res.currency"].browse(self.env.context["currency_id"])
elif not currency:
currency = self.env.company.currency_id
for line in self.line_ids:
if line.value == "fixed":
amt = sign * currency.round(line.value_amount)
elif line.value == "percent":
amt = currency.round(value * (line.value_amount / 100.0))
elif line.value == "balance":
amt = currency.round(amount)
next_date = fields.Date.from_string(date_ref)
if line.option == "day_after_invoice_date":
next_date += relativedelta(days=line.days)
if line.day_of_the_month > 0:
months_delta = (line.day_of_the_month < next_date.day) and 1 or 0
next_date += relativedelta(
day=line.day_of_the_month, months=months_delta
)
elif line.option == "after_invoice_month":
next_first_date = next_date + relativedelta(
day=1, months=1
) # Getting 1st of next month
next_date = next_first_date + relativedelta(days=line.days - 1)
elif line.option == "day_following_month":
next_date += relativedelta(day=line.days, months=1)
elif line.option == "day_current_month":
next_date += relativedelta(day=line.days, months=0)
elif line.option == "custom":
next_date = line.fixed_date
result.append((fields.Date.to_string(next_date), amt))
amount -= amt
amount = sum(amt for _, amt in result)
dist = currency.round(value - amount)
if dist:
last_date = result and result[-1][0] or fields.Date.context_today(self)
result.append((last_date, dist))
return sorted(result, key=lambda k: k[0])
17 changes: 17 additions & 0 deletions account_payment_term_manual/models/account_payment_term_manual.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ class AccountPaymentTermLineManual(models.Model):
copy=False,
)

option = fields.Selection(
selection_add=[("custom", "Custom")],
ondelete={
"custom": "set default",
},
default="day_after_invoice_date",
)

@api.constrains(
"value",
"value_amount",
Expand All @@ -87,4 +95,13 @@ class AccountPaymentTermLineManual(models.Model):
"fixed_date",
)
def _check_manual_payment_term_id(self):
for record in self:
if record.fixed_date and record.option != "custom":
record.option = "custom"
self.manual_payment_id.set_as_edited()

@api.onchange("fixed_date")
def _onchange_fixed_date(self):
for record in self:
if record.fixed_date:
record.option = "custom"
2 changes: 2 additions & 0 deletions account_payment_term_manual/views/account_invoice_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
/>
<field name="days" />
<field name="option" string="" />
<field name="fixed_date" />
<field name="day_of_the_month" string="Day of the month" />
</tree>
</field>
Expand Down Expand Up @@ -79,6 +80,7 @@
/>
<field name="days" />
<field name="option" string="" />
<field name="fixed_date" />
<field name="day_of_the_month" string="Day of the month" />
</tree>
</field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<field name="inherit_id" ref="account.view_payment_term_line_tree" />
<field name="mode">primary</field>
<field name="arch" type="xml">
<field name="value" position="attributes">
<attribute name="invisible">0</attribute>
<field name="option" position="after">
<field name="fixed_date" />
</field>
</field>
</record>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
/>
<field name="days" />
<field name="option" string="" />
<field name="fixed_date" />
<field name="day_of_the_month" string="Day of the month" />
</tree>
</field>
Expand Down
1 change: 1 addition & 0 deletions account_payment_term_manual_sale/views/sale_order.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
/>
<field name="days" />
<field name="option" string="" />
<field name="fixed_date" />
<field name="day_of_the_month" string="Day of the month" />
</tree>
</field>
Expand Down

0 comments on commit 74f6b83

Please sign in to comment.