2022-06-29 18:32:15 +00:00
|
|
|
# Copyright 2019-2022 Akretion France (http://www.akretion.com/)
|
|
|
|
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
|
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
|
|
|
|
|
|
|
from dateutil.relativedelta import relativedelta
|
|
|
|
|
|
|
|
from flectra import _, fields, models
|
|
|
|
from flectra.exceptions import UserError
|
2023-05-19 08:19:13 +00:00
|
|
|
from flectra.tools import date_utils
|
|
|
|
from flectra.tools.misc import format_amount, format_date
|
2022-06-29 18:32:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AccountCutoff(models.Model):
|
|
|
|
_inherit = "account.cutoff"
|
|
|
|
|
|
|
|
def get_lines(self):
|
|
|
|
res = super().get_lines()
|
|
|
|
if self.cutoff_type not in ["accrued_expense", "accrued_revenue"]:
|
|
|
|
return res
|
2023-05-19 08:19:13 +00:00
|
|
|
|
2022-06-29 18:32:15 +00:00
|
|
|
line_obj = self.env["account.cutoff.line"]
|
|
|
|
sub_obj = self.env["account.cutoff.accrual.subscription"]
|
2023-05-19 08:19:13 +00:00
|
|
|
|
|
|
|
fy_start_date, fy_end_date = date_utils.get_fiscal_year(
|
|
|
|
self.cutoff_date,
|
|
|
|
day=self.company_id.fiscalyear_last_day,
|
|
|
|
month=int(self.company_id.fiscalyear_last_month),
|
|
|
|
)
|
|
|
|
if not fy_start_date:
|
|
|
|
raise UserError(_("Flectra cannot compute the fiscal year start date."))
|
|
|
|
|
2022-06-29 18:32:15 +00:00
|
|
|
type2subtype = {
|
|
|
|
"accrued_expense": "expense",
|
|
|
|
"accrued_revenue": "revenue",
|
|
|
|
}
|
|
|
|
sub_type = type2subtype[self.cutoff_type]
|
|
|
|
sign = sub_type == "revenue" and -1 or 1
|
|
|
|
subs = sub_obj.search(
|
|
|
|
[
|
|
|
|
("company_id", "=", self.company_id.id),
|
|
|
|
("subscription_type", "=", sub_type),
|
|
|
|
("start_date", "<=", self.cutoff_date),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
if subs:
|
|
|
|
# check that the cutoff is the last day of a month
|
|
|
|
# otherwise, we have pb with when we compute intervals
|
|
|
|
# with cutoff date is a day which is not present for all months
|
|
|
|
last_day_same_month = self.cutoff_date + relativedelta(day=31)
|
|
|
|
if last_day_same_month.day != self.cutoff_date.day:
|
|
|
|
raise UserError(
|
|
|
|
_(
|
|
|
|
"The cutoffs with subscription only work when the cutoff "
|
|
|
|
"date (%s) is the last day of a month."
|
|
|
|
)
|
2023-05-19 08:19:13 +00:00
|
|
|
% format_date(self.env, self.cutoff_date)
|
2022-06-29 18:32:15 +00:00
|
|
|
)
|
|
|
|
if not self.source_journal_ids:
|
|
|
|
raise UserError(_("Missing source journals."))
|
|
|
|
self.message_post(
|
|
|
|
body=_("Computing provisions from %d subscriptions.") % len(subs)
|
|
|
|
)
|
2023-05-19 08:19:13 +00:00
|
|
|
common_domain = [("journal_id", "in", self.source_journal_ids.ids)]
|
|
|
|
if self.source_move_state == "posted":
|
|
|
|
common_domain.append(("parent_state", "=", "posted"))
|
|
|
|
else:
|
|
|
|
common_domain.append(("parent_state", "in", ("draft", "posted")))
|
2022-06-29 18:32:15 +00:00
|
|
|
work = {}
|
|
|
|
# Generate time intervals and compute existing expenses/revenue
|
|
|
|
for sub in subs:
|
2023-05-19 08:19:13 +00:00
|
|
|
sub._process_subscription(
|
|
|
|
work, fy_start_date, self.cutoff_date, common_domain, sign
|
|
|
|
)
|
2022-06-29 18:32:15 +00:00
|
|
|
# Create mapping dict
|
|
|
|
mapping = self._get_mapping_dict()
|
|
|
|
sub_type_label = sub_type == "expense" and _("Expense") or _("Revenue")
|
|
|
|
lsign = sub_type == "expense" and -1 or 1
|
|
|
|
for sub in work.keys():
|
|
|
|
vals = self._prepare_subscription_cutoff_line(
|
|
|
|
work[sub], mapping, sub_type_label, lsign
|
|
|
|
)
|
|
|
|
if vals:
|
|
|
|
line_obj.create(vals)
|
|
|
|
return res
|
|
|
|
|
|
|
|
def _prepare_subscription_cutoff_line(self, data, mapping, sub_type_label, lsign):
|
|
|
|
# Compute provision for a subscription
|
|
|
|
# -> analyse each time interval
|
|
|
|
# Write the details of the computation in the notes
|
|
|
|
# (or in the chatter if the amount to provision is 0)
|
|
|
|
sub = data["sub"]
|
2023-05-19 08:19:13 +00:00
|
|
|
ccur = self.company_currency_id
|
|
|
|
notes = [
|
|
|
|
_(
|
|
|
|
"CONFIG: %s periodicity, start date %s, "
|
|
|
|
"min. expense amount %s, default provision amount %s"
|
|
|
|
)
|
|
|
|
% (
|
|
|
|
sub._fields["periodicity"].convert_to_export(sub.periodicity, sub),
|
|
|
|
format_date(self.env, sub.start_date),
|
|
|
|
format_amount(self.env, sub.min_amount, ccur),
|
|
|
|
format_amount(self.env, sub.provision_amount, ccur),
|
|
|
|
),
|
|
|
|
_("PERIODS:"),
|
|
|
|
]
|
2022-06-29 18:32:15 +00:00
|
|
|
cutoff_amount = 0
|
|
|
|
for interval in data["intervals"]:
|
2023-05-19 08:19:13 +00:00
|
|
|
prorata_label = (
|
|
|
|
interval["prorata"]
|
|
|
|
and " "
|
|
|
|
+ _("PRORATED min. amount %s, default provisionning amount %s")
|
|
|
|
% (
|
|
|
|
format_amount(self.env, interval["min_amount"], ccur),
|
|
|
|
format_amount(self.env, interval["provision_amount"], ccur),
|
2022-06-29 18:32:15 +00:00
|
|
|
)
|
2023-05-19 08:19:13 +00:00
|
|
|
or ""
|
|
|
|
)
|
|
|
|
if ccur.compare_amounts(interval["amount"], interval["min_amount"]) < 0:
|
|
|
|
period_cutoff_amount = ccur.round(
|
|
|
|
interval["provision_amount"] - interval["amount"]
|
2022-06-29 18:32:15 +00:00
|
|
|
)
|
|
|
|
notes.append(
|
2023-05-19 08:19:13 +00:00
|
|
|
_("%s → %s%s: %s %s under min. amount ⇒provisionning %s")
|
2022-06-29 18:32:15 +00:00
|
|
|
% (
|
2023-05-19 08:19:13 +00:00
|
|
|
format_date(self.env, interval["start"]),
|
|
|
|
format_date(self.env, interval["end"]),
|
|
|
|
prorata_label,
|
2022-06-29 18:32:15 +00:00
|
|
|
sub_type_label,
|
2023-05-19 08:19:13 +00:00
|
|
|
format_amount(self.env, interval["amount"], ccur),
|
|
|
|
format_amount(self.env, period_cutoff_amount, ccur),
|
2022-06-29 18:32:15 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
cutoff_amount += period_cutoff_amount * lsign
|
|
|
|
else:
|
|
|
|
notes.append(
|
2023-05-19 08:19:13 +00:00
|
|
|
_("%s → %s%s: %s %s over min. amount ⇒ no provisionning")
|
2022-06-29 18:32:15 +00:00
|
|
|
% (
|
2023-05-19 08:19:13 +00:00
|
|
|
format_date(self.env, interval["start"]),
|
|
|
|
format_date(self.env, interval["end"]),
|
|
|
|
prorata_label,
|
2022-06-29 18:32:15 +00:00
|
|
|
sub_type_label,
|
2023-05-19 08:19:13 +00:00
|
|
|
format_amount(self.env, interval["amount"], ccur),
|
2022-06-29 18:32:15 +00:00
|
|
|
)
|
|
|
|
)
|
2023-05-19 08:19:13 +00:00
|
|
|
if ccur.is_zero(cutoff_amount):
|
2022-06-29 18:32:15 +00:00
|
|
|
msg = _(
|
|
|
|
"<p>No provision for subscription <a href=# "
|
|
|
|
"data-oe-model=account.cutoff.accrual.subscription "
|
|
|
|
"data-oe-id=%d>%s</a>:</p>"
|
|
|
|
) % (sub.id, sub.name)
|
|
|
|
if notes:
|
|
|
|
msg += "<ul>"
|
|
|
|
for note in notes:
|
|
|
|
msg += "<li>%s</li>" % note
|
|
|
|
msg += "</ul>"
|
|
|
|
self.message_post(body=msg)
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if sub.partner_type == "one":
|
|
|
|
partner_id = sub.partner_id.id
|
|
|
|
else:
|
|
|
|
partner_id = False
|
|
|
|
if sub.account_id.id in mapping:
|
|
|
|
cutoff_account_id = mapping[sub.account_id.id]
|
|
|
|
else:
|
|
|
|
cutoff_account_id = sub.account_id.id
|
|
|
|
vals = {
|
2023-05-19 08:19:13 +00:00
|
|
|
"subscription_id": sub.id,
|
2022-06-29 18:32:15 +00:00
|
|
|
"parent_id": self.id,
|
|
|
|
"partner_id": partner_id,
|
|
|
|
"account_id": sub.account_id.id,
|
|
|
|
"analytic_account_id": sub.analytic_account_id.id or False,
|
|
|
|
"name": sub.name,
|
|
|
|
"currency_id": sub.company_currency_id.id,
|
|
|
|
"amount": 0,
|
|
|
|
"cutoff_amount": cutoff_amount,
|
|
|
|
"cutoff_account_id": cutoff_account_id,
|
|
|
|
"notes": "\n".join(notes),
|
|
|
|
}
|
|
|
|
if sub.tax_ids and self.company_id.accrual_taxes:
|
|
|
|
tax_compute_all_res = sub.tax_ids.compute_all(
|
|
|
|
cutoff_amount, partner=sub.partner_id, handle_price_include=False
|
|
|
|
)
|
|
|
|
vals["tax_line_ids"] = self._prepare_tax_lines(
|
2023-05-19 08:19:13 +00:00
|
|
|
tax_compute_all_res, ccur
|
2022-06-29 18:32:15 +00:00
|
|
|
)
|
|
|
|
return vals
|
2023-05-19 08:19:13 +00:00
|
|
|
|
|
|
|
class AccountCutoffLine(models.Model):
|
|
|
|
_inherit = "account.cutoff.line"
|
|
|
|
|
|
|
|
subscription_id = fields.Many2one(
|
|
|
|
"account.cutoff.accrual.subscription", ondelete="restrict"
|
|
|
|
)
|