Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] website_sale_product_trial #352

Open
wants to merge 5 commits into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions setup/website_sale_product_trial/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
70 changes: 70 additions & 0 deletions website_sale_product_trial/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
==========================
Website Sale Product Trial
==========================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:a25ed6054ea1e7db1fe6f90cdb8102edd80859c7504cc1cebdaae1a0123c4819
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-coopiteasy%2Faddons-lightgray.png?logo=github
:target: https://github.com/coopiteasy/addons/tree/16.0/website_sale_product_trial
:alt: coopiteasy/addons

|badge1| |badge2| |badge3|

Configure product to be a trial subscription.

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/coopiteasy/addons/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/coopiteasy/addons/issues/new?body=module:%20website_sale_product_trial%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Coop IT Easy SC

Contributors
~~~~~~~~~~~~

* `Coop IT Easy SC <https://coopiteasy.be>`_:

* Rémy Taymans

Maintainers
~~~~~~~~~~~

.. |maintainer-remytms| image:: https://github.com/remytms.png?size=40px
:target: https://github.com/remytms
:alt: remytms

Current maintainer:

|maintainer-remytms|

This module is part of the `coopiteasy/addons <https://github.com/coopiteasy/addons/tree/16.0/website_sale_product_trial>`_ project on GitHub.

You are welcome to contribute.
5 changes: 5 additions & 0 deletions website_sale_product_trial/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-FileCopyrightText: 2025 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later
from . import models
from . import controllers
28 changes: 28 additions & 0 deletions website_sale_product_trial/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# SPDX-FileCopyrightText: 2025 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

{
"name": "Website Sale Product Trial",
"summary": """
Configure product contract to be a trial subscription.""",
"version": "16.0.1.0.0",
"category": "Website",
"website": "https://github.com/coopiteasy/addons",
"author": "Coop IT Easy SC",
"maintainers": ["remytms"],
"license": "AGPL-3",
"application": False,
"depends": [
"website_sale_product_compatibility",
"product",
"delivery",
],
"excludes": [],
"data": [
"views/product_views.xml",
"views/templates.xml",
],
"demo": [],
"qweb": [],
}
4 changes: 4 additions & 0 deletions website_sale_product_trial/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SPDX-FileCopyrightText: 2025 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later
from . import main
35 changes: 35 additions & 0 deletions website_sale_product_trial/controllers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# SPDX-FileCopyrightText: 2025 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from odoo import http
from odoo.http import request

from odoo.addons.website_sale.controllers.main import WebsiteSale


class WebsiteSaleProductTrial(WebsiteSale):
@http.route(
"/shop/payment/validate",
type="http",
auth="public",
website=True,
sitemap=False,
)
def shop_payment_validate(self, sale_order_id=None, **post):
result = super().shop_payment_validate(sale_order_id=sale_order_id, **post)
if "my/order" in result.location:
result.location = "/shop/main/confirmation"
return result

@http.route(
"/shop/main/confirmation",
type="http",
auth="public",
website=True,
sitemap=False,
)
def shop_main_confirmation(self):
"""General confirmation for product. Used to skip view of the
invoice."""
return request.render("website_sale_product_trial.main_confirmation", {})
5 changes: 5 additions & 0 deletions website_sale_product_trial/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-FileCopyrightText: 2025 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later
from . import product_template
from . import sale_order
11 changes: 11 additions & 0 deletions website_sale_product_trial/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-FileCopyrightText: 2025 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from odoo import fields, models


class ProductTemplate(models.Model):
_inherit = "product.template"

is_trial = fields.Boolean()
81 changes: 81 additions & 0 deletions website_sale_product_trial/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# SPDX-FileCopyrightText: 2025 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from datetime import datetime, timedelta

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

TRIAL_DURATION_DAYS = 14


class SaleOrder(models.Model):
_inherit = "sale.order"

@api.constrains("order_line")
def _check_trial_alone(self):
"""Ensure trial are not mixed in a sale order."""
for order in self:
lines_to_check = order._exclude_delivery_order_line()
if not self._is_product_compatible(lines_to_check.product_id):
raise ValidationError(
_(
"Cannot add product trial in an order that "
"contains other products."
)
)

@api.model
def _is_product_compatible(self, product_ids):
at_least_one_trial = any(product_ids.mapped("is_trial"))
return not at_least_one_trial or len(product_ids) <= 1

def _exclude_delivery_order_line(self):
"""Return the order_lines that are not delivery"""
self.ensure_one()
return self.order_line.filtered(lambda r: not r.is_delivery)

def check_product_compatibility(self, product_id):
warning = super().check_product_compatibility(product_id)
lines_to_check = self._exclude_delivery_order_line()
product = self.env["product.product"].browse(product_id).exists()
if not warning and lines_to_check and product:
is_product_compatible = self._is_product_compatible(
lines_to_check.product_id | product
)
if not is_product_compatible:
if product.is_trial:
warning = _(
f"Product {product.name} cannot be added because "
"it's a trial and trial must be ordered seperately."
)
else:
warning = _(
f"Product {product.name} cannot be added because "
"order contains products that are trials and "
"trials must be ordered separately."
)
return warning

def _prepare_order_line_values(
self,
product_id,
quantity,
linked_line_id=False,
no_variant_attribute_values=None,
product_custom_attribute_values=None,
**kwargs,
):
values = super()._prepare_order_line_values(
product_id=product_id,
quantity=quantity,
linked_line_id=linked_line_id,
no_variant_attirbute_values=no_variant_attribute_values,
)
# Hard coded 15 days for trial duration
product = self.env["product.product"].browse(product_id)
if product.is_trial:
values["date_start"] = datetime.today()
values["date_end"] = datetime.today() + timedelta(days=TRIAL_DURATION_DAYS)
return values
3 changes: 3 additions & 0 deletions website_sale_product_trial/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `Coop IT Easy SC <https://coopiteasy.be>`_:

* Rémy Taymans
1 change: 1 addition & 0 deletions website_sale_product_trial/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Configure product to be a trial subscription.
Loading
Loading