Skip to content

Commit

Permalink
[IMP] membership_[type,custom_date]: Add restraint on date_from and d…
Browse files Browse the repository at this point in the history
…ate_to on membership line

This was a bit tricky to get right, architecturally, but I think I
arrived at the correct design decision. On the whole, the new
restriction makes sense. Following from the fact that dates are
mandatory on 'fixed dates' (read: default) membership products, and
following from the fact that dates are always set on the membership
lines of 'variable period' membership products, membership lines should
_always_ have values for date_from and date_to. The fact that these
fields are not already required is a strange design decision.

The constraint in membership_type _effectively_ sets required=True on
these fields.

However, there is a use-case for memberships that do not have an end
date (using the 'custom' membership product). For those scenarios, we
add a toggle on the product (exclusive to 'custom' products) on whether
to apply the constraint.

Signed-off-by: Carmen Bianca BAKKER <[email protected]>
  • Loading branch information
carmenbianca committed Nov 24, 2023
1 parent 9f3eb6c commit 6ea6037
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions membership_custom_date/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
],
"excludes": [],
"data": [
"views/product_template_views.xml",
"wizard/membership_invoice_views.xml",
],
"demo": [],
Expand Down
1 change: 1 addition & 0 deletions membership_custom_date/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
# SPDX-License-Identifier: AGPL-3.0-or-later

from . import product_template
from . import membership_membership_line
19 changes: 19 additions & 0 deletions membership_custom_date/models/membership_membership_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-FileCopyrightText: 2023 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from odoo import api, models


class MembershipLine(models.Model):
_inherit = "membership.membership_line"

@api.constrains(
"membership_id", "membership_id.dates_mandatory", "date_from", "date_to"
)
def _check_mandatory_dates(self):
lines = self.filtered(
lambda line: line.membership_id.membership_type != "custom"
or line.membership_id.dates_mandatory
)
return super(MembershipLine, lines)._check_mandatory_dates()
8 changes: 8 additions & 0 deletions membership_custom_date/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ class ProductTemplate(models.Model):
],
ondelete={"custom": "set default"},
)
dates_mandatory = fields.Boolean(
string="Mandatory dates",
default=True,
help="Membership lines that use this product must have a start and end date.",
)

@api.model
def _correct_vals_membership_type(self, vals):
vals = super()._correct_vals_membership_type(vals)
if vals.get("membership_type") == "custom":
vals["membership_date_from"] = vals["membership_date_to"] = False
# TODO: should we set dates_mandatory back to True if switching away
# from custom? At the moment this variable is not used _at all_ for
# non-custom memberships.
return vals
20 changes: 20 additions & 0 deletions membership_custom_date/views/product_template_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
SPDX-FileCopyrightText: 2023 Coop IT Easy SC
SPDX-License-Identifier: AGPL-3.0-or-later
-->
<odoo>
<record id="membership_products_form_period" model="ir.ui.view">
<field name="name">Membership Products (custom type)</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="membership_type.membership_products_form" />
<field name="arch" type="xml">
<field name="membership_type" position="after">
<field
name="dates_mandatory"
attrs="{'invisible': [('membership_type, '!=', 'custom')]}"
/>
</field>
</field>
</record>
</odoo>
1 change: 1 addition & 0 deletions membership_type/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
# SPDX-License-Identifier: AGPL-3.0-or-later

from . import product_template
from . import membership_membership_line
18 changes: 18 additions & 0 deletions membership_type/models/membership_membership_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# SPDX-FileCopyrightText: 2023 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from odoo import _, api, models
from odoo.exceptions import ValidationError


class MembershipLine(models.Model):
_inherit = "membership.membership_line"

@api.constrains("date_from", "date_to")
def _check_mandatory_dates(self):
for line in self:
if not line.date_from or not line.date_to:
raise ValidationError(
_("Error: the start and end dates are mandatory.")
)

0 comments on commit 6ea6037

Please sign in to comment.