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

[16.0][MIG] sale_coupon_order_suggestion>sale_loyalty_order_suggestion: Migration to version 16.0 #198

Merged
merged 8 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
104 changes: 104 additions & 0 deletions sale_loyalty_order_suggestion/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
========================
Sale Loyalty Suggestions
========================

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

.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
:target: https://odoo-community.org/page/development-status
:alt: Production/Stable
.. |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-OCA%2Fsale--promotion-lightgray.png?logo=github
:target: https://github.com/OCA/sale-promotion/tree/16.0/sale_loyalty_order_suggestion
:alt: OCA/sale-promotion
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/sale-promotion-16-0/sale-promotion-16-0-sale_loyalty_order_suggestion
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/sale-promotion&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module extends the functionality of the `sale_loyalty` wizard by giving hints of
available promotions and products needed to apply them to the seller placing the sales
order. A product added to the lines of a sales order will be marked with a gift icon if
there is a promotion containing that product as part of its rules.

**Table of contents**

.. contents::
:local:

Usage
=====

To use this module:

* Configure or create a promotion and set in its rules products and a required quantity.
* Create a sales order and add to the order lines one of the products that were part of
the promotion rules. This line will then be marked with an icon (🎁) which will appear
at the end on the right.
* Click on the icon and the wizard will open with the available promotions of which the
product is part of its rules.
* Select the promotion and the products needed to apply it.

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/sale-promotion/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/OCA/sale-promotion/issues/new?body=module:%20sale_loyalty_order_suggestion%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
~~~~~~~

* Tecnativa

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

* `Tecnativa <https://www.tecnativa.com>`_:

* Pedro M. Baeza
* David Vidal
* Pilar Vargas

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

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

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

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-chienandalu|

This module is part of the `OCA/sale-promotion <https://github.com/OCA/sale-promotion/tree/16.0/sale_loyalty_order_suggestion>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions sale_loyalty_order_suggestion/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import wizard
26 changes: 26 additions & 0 deletions sale_loyalty_order_suggestion/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2021 Tecnativa - David Vidal
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Sale Loyalty Suggestions",
"summary": "Suggest promotions in the sale order line",
"version": "16.0.1.0.0",
"development_status": "Production/Stable",
"category": "Sale",
"website": "https://github.com/OCA/sale-promotion",
"author": "Tecnativa, Odoo Community Association (OCA)",
"maintainers": ["chienandalu"],
"license": "AGPL-3",
"depends": ["sale_loyalty", "sale_loyalty_initial_date_validity"],
"data": [
"security/ir.model.access.csv",
"views/sale_order_views.xml",
"wizard/sale_loyalty_reward_wizard_views.xml",
],
"assets": {
"web.assets_backend": [
"/sale_loyalty_order_suggestion/static/src/js/suggest_promotion_widget.esm.js",
"/sale_loyalty_order_suggestion/static/src/xml/suggest_promotion.xml",
],
},
"installable": True,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * sale_loyalty_order_suggestion
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 15.0\n"
"Report-Msgid-Bugs-To: \n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: sale_loyalty_order_suggestion
#. openerp-web
#: code:addons/sale_loyalty_order_suggestion/static/src/xml/suggest_promotion.xml:0
#, python-format
msgid "Available promotions"
msgstr ""

#. module: sale_loyalty_order_suggestion
#: model:ir.model,name:sale_loyalty_order_suggestion.model_sale_order_line
msgid "Sales Order Line"
msgstr ""

#. module: sale_loyalty_order_suggestion
#: model:ir.model.fields,field_description:sale_loyalty_order_suggestion.field_sale_order_line__suggested_promotion_ids
msgid "Suggested Promotion"
msgstr ""
1 change: 1 addition & 0 deletions sale_loyalty_order_suggestion/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import sale_order
118 changes: 118 additions & 0 deletions sale_loyalty_order_suggestion/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Copyright 2021 Tecnativa - David Vidal
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import api, fields, models
from odoo.osv import expression


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

def _get_available_programs_domain(self):
domain = [
("active", "=", True),
("trigger", "=", "auto"),
("program_type", "not in", ("promo_code", "next_order_coupons")),
"|",
("date_from", "=", False),
("date_from", "<=", fields.Date.today()),
"|",
("date_to", "=", False),
("date_to", ">=", fields.Date.today()),
"|",
("applies_on", "=", "current"),
("applies_on", "=", "both"),
]
return domain

def _get_available_programs(self):
programs = self.env["loyalty.program"]
self._update_programs_and_rewards()
programs_applied = self._get_reward_programs()
domain = expression.AND(
[
self._get_available_programs_domain(),
[("id", "not in", programs_applied.ids)],
]
)
programs = (
self.env["loyalty.program"]
.search(domain)
.filtered(lambda p: not p.limit_usage or p.total_order_count < p.max_usage)
)
# If the same reward has already been applied from another program, the reward
# and the program to which it belongs must be deleted from the list of
# suggestions. For example, if a 10% discount has already been applied, the same
# discount cannot be applied even if it belongs to another program.
order_rewards = self.order_line.reward_id
programs_to_delete = []
if order_rewards:
programs_to_delete = [
program.id
for program in programs
if any(
all(
getattr(program_reward, field) == getattr(order_reward, field)
for field in [
"reward_type",
"discount",
"discount_mode",
"discount_applicability",
"discount_product_domain",
"discount_product_ids",
"discount_product_category_id",
"discount_max_amount",
"reward_product_id",
"reward_product_tag_id",
"reward_product_qty",
"required_points",
]
)
for program_reward in program.reward_ids
for order_reward in order_rewards
)
]
return programs.filtered(lambda p: p.id not in programs_to_delete)

def _available_programs(self):
self.ensure_one()
valid_programs = self._get_available_programs()
# Filters programs that have rules with minimum_qty > 0
programs_with_minimum_qty = valid_programs.filtered(
lambda x: any(rule.minimum_qty > 0 for rule in x.rule_ids)
)
programs = self.env["loyalty.program"]
if programs_with_minimum_qty:
product_id = self.env.context.get("product_id")
programs += programs_with_minimum_qty.filtered(
lambda x: any(
product_id in rule._get_valid_products().ids for rule in x.rule_ids
)
)
return programs


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

suggested_promotion_ids = fields.Many2many(
comodel_name="loyalty.program",
compute="_compute_suggested_promotion_ids",
)
suggested_promotions = fields.Boolean(
compute="_compute_suggested_promotion_ids", default=False
)
suggested_reward_ids = fields.Many2many(
comodel_name="loyalty.reward",
compute="_compute_suggested_promotion_ids",
)

@api.depends("product_id")
def _compute_suggested_promotion_ids(self):
self.suggested_promotion_ids = False
self.suggested_reward_ids = False
for line in self.filtered("product_id"):
line.suggested_promotion_ids = line.order_id.with_context(
product_id=line.product_id.id
)._available_programs()
line.suggested_promotions = bool(line.suggested_promotion_ids)
line.suggested_reward_ids = line.suggested_promotion_ids.reward_ids
5 changes: 5 additions & 0 deletions sale_loyalty_order_suggestion/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
* `Tecnativa <https://www.tecnativa.com>`_:

* Pedro M. Baeza
* David Vidal
* Pilar Vargas
4 changes: 4 additions & 0 deletions sale_loyalty_order_suggestion/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This module extends the functionality of the `sale_loyalty` wizard by giving hints of
available promotions and products needed to apply them to the seller placing the sales
order. A product added to the lines of a sales order will be marked with a gift icon if
there is a promotion containing that product as part of its rules.
9 changes: 9 additions & 0 deletions sale_loyalty_order_suggestion/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
To use this module:

* Configure or create a promotion and set in its rules products and a required quantity.
* Create a sales order and add to the order lines one of the products that were part of
the promotion rules. This line will then be marked with an icon (🎁) which will appear
at the end on the right.
* Click on the icon and the wizard will open with the available promotions of which the
product is part of its rules.
* Select the promotion and the products needed to apply it.
2 changes: 2 additions & 0 deletions sale_loyalty_order_suggestion/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
sale_loyalty_order_suggestion.access_sale_loyalty_rule_product_line_wizard,access_sale_loyalty_rule_product_line_wizard,sale_loyalty_order_suggestion.model_sale_loyalty_rule_product_line_wizard,base.group_user,1,1,1,1
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading