|
| 1 | +# Copyright 2026 ACSONE SA/NV |
| 2 | +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). |
| 3 | + |
| 4 | +from odoo import _, api, fields, models |
| 5 | +from odoo.exceptions import ValidationError |
| 6 | +from odoo.tools import float_round |
| 7 | + |
| 8 | + |
| 9 | +class PerfObligation(models.Model): |
| 10 | + _inherit = "perf.obligation" |
| 11 | + |
| 12 | + recognition_at_date_method = fields.Selection( |
| 13 | + selection_add=[ |
| 14 | + ("daily", "Daily Pro-Rata"), |
| 15 | + ], |
| 16 | + ) |
| 17 | + start_date = fields.Date() |
| 18 | + end_date = fields.Date() |
| 19 | + is_start_date_required = fields.Boolean( |
| 20 | + compute="_compute_is_date_required", |
| 21 | + ) |
| 22 | + is_end_date_required = fields.Boolean( |
| 23 | + compute="_compute_is_date_required", |
| 24 | + ) |
| 25 | + |
| 26 | + @api.depends("recognition_at_date_method") |
| 27 | + def _compute_is_date_required(self): |
| 28 | + for rec in self: |
| 29 | + method = rec.recognition_at_date_method |
| 30 | + rec.is_start_date_required = method == "daily" |
| 31 | + rec.is_end_date_required = method == "daily" |
| 32 | + |
| 33 | + @api.constrains("start_date", "end_date") |
| 34 | + def _check_dates(self): |
| 35 | + for rec in self: |
| 36 | + if rec.start_date and rec.end_date and rec.start_date > rec.end_date: |
| 37 | + raise ValidationError( |
| 38 | + _( |
| 39 | + "The start date must be before or equal to " |
| 40 | + "the end date on performance obligation %(name)s.", |
| 41 | + name=rec.display_name, |
| 42 | + ) |
| 43 | + ) |
| 44 | + |
| 45 | + @api.constrains("recognition_at_date_method", "start_date", "end_date") |
| 46 | + def _check_recognition_method_dates(self): |
| 47 | + for rec in self: |
| 48 | + if not rec.is_start_date_required and not rec.is_end_date_required: |
| 49 | + continue |
| 50 | + if (rec.is_start_date_required and not rec.start_date) or ( |
| 51 | + rec.is_end_date_required and not rec.end_date |
| 52 | + ): |
| 53 | + raise ValidationError( |
| 54 | + _( |
| 55 | + "Start and end dates are required when the daily " |
| 56 | + "pro-rata recognition method is selected " |
| 57 | + "on performance obligation %(name)s.", |
| 58 | + name=rec.display_name, |
| 59 | + ) |
| 60 | + ) |
| 61 | + |
| 62 | + def _compute_amount_to_recognize_daily(self, date): |
| 63 | + """Daily pro-rata recognition method. |
| 64 | +
|
| 65 | + Computes the amount to recognize based on the number of calendar |
| 66 | + days elapsed from ``start_date`` (inclusive) to ``date`` (inclusive), |
| 67 | + over the total number of days of the period. |
| 68 | +
|
| 69 | + Rounding strategy: each intermediate amount is rounded using the |
| 70 | + currency's decimal precision. The last day always returns exactly |
| 71 | + ``total_amount`` to avoid any residual rounding difference. |
| 72 | + """ |
| 73 | + self.ensure_one() |
| 74 | + start_date = self.start_date |
| 75 | + end_date = self.end_date |
| 76 | + |
| 77 | + if date < start_date: |
| 78 | + return 0.0 |
| 79 | + if date >= end_date: |
| 80 | + return self.total_amount |
| 81 | + |
| 82 | + precision = self.currency_id.decimal_places |
| 83 | + total_days = (end_date - start_date).days + 1 |
| 84 | + elapsed_days = (date - start_date).days + 1 |
| 85 | + daily_amount = self.total_amount / total_days |
| 86 | + amount = float_round(daily_amount * elapsed_days, precision_digits=precision) |
| 87 | + # Safety cap: never exceed total |
| 88 | + return min(amount, self.total_amount) |
0 commit comments