Skip to content

Commit

Permalink
[MIG] sale_coupon_order_line_link: Migration to version 16.0
Browse files Browse the repository at this point in the history
TT44349
  • Loading branch information
pilarvargas-tecnativa committed Sep 19, 2023
1 parent 2ce7044 commit 4527b89
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 158 deletions.
20 changes: 9 additions & 11 deletions sale_coupon_order_line_link/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
Link coupons to order lines
===========================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:48825c029f35b6de6644b4af46bb75003501eeea6212c0ff81dcb59db4429d02
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
:target: https://odoo-community.org/page/development-status
Expand All @@ -17,16 +14,16 @@ Link coupons to order lines
: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/15.0/sale_coupon_order_line_link
:target: https://github.com/OCA/sale-promotion/tree/16.0/sale_coupon_order_line_link
: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-15-0/sale-promotion-15-0-sale_coupon_order_line_link
:target: https://translation.odoo-community.org/projects/sale-promotion-16-0/sale-promotion-16-0-sale_coupon_order_line_link
: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=15.0
:target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/sale-promotion&target_branch=16.0
:alt: Try me on Runboat

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

This module adds a link in the sale order line to the program used in a discount, so
it can be easily tracked afterwards. Also eases the implementation of coupon modules
Expand Down Expand Up @@ -56,8 +53,8 @@ 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_coupon_order_line_link%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/sale-promotion/issues/new?body=module:%20sale_coupon_order_line_link%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.

Expand All @@ -75,6 +72,7 @@ Contributors
* `Tecnativa <https://www.tecnativa.com>`_:

* David Vidal
* Pilar Vargas

* `Ooops404 <https://www.ooops404.com>`_:

Expand All @@ -101,6 +99,6 @@ 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/15.0/sale_coupon_order_line_link>`_ project on GitHub.
This module is part of the `OCA/sale-promotion <https://github.com/OCA/sale-promotion/tree/16.0/sale_coupon_order_line_link>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
4 changes: 2 additions & 2 deletions sale_coupon_order_line_link/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"name": "Link coupons to order lines",
"summary": "Adds a link between coupons and their generated order lines for easing "
"tracking",
"version": "15.0.1.0.0",
"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_coupon"],
"depends": ["sale_loyalty"],
"data": ["reports/sale_report_views.xml", "views/sale_order_views.xml"],
}
77 changes: 21 additions & 56 deletions sale_coupon_order_line_link/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,16 @@
class SaleOrder(models.Model):
_inherit = "sale.order"

def _get_reward_values_product(self, program):
"""Add the link to the program in the discount line"""
res = super()._get_reward_values_product(program)
res["coupon_program_id"] = program.id
return res

def _get_reward_values_discount(self, program):
"""Add the link to the program in the discount line. The original method returns
a dict.values() to support multiple taxes. We can safely return a list instead
as the main method just iterates over the result of these auxiliar products to
write vals. https://git.io/J88As
"""
res = list(super()._get_reward_values_discount(program))
[r.update(coupon_program_id=program.id) for r in res]
return res

def write(self, vals):
"""We're looking for reward lines set in the write, from those, we'll get
the proper ids to write them into the related lines"""
res = super().write(vals)
reward_lines = [
x[2]
for x in vals.get("order_line", [])
if len(x) > 2 and x[0] == 0 and x[2].get("is_reward_line")
]
reward_lines = self.order_line.filtered(
lambda x: x.reward_id and x.is_reward_line
)
if not reward_lines:
return res
programs = self.env["coupon.program"].browse(
list({x.get("coupon_program_id") for x in reward_lines})
)
programs = reward_lines.reward_id
for order in self:
order._link_reward_lines(programs)
order._link_reward_generated_lines(programs)
Expand All @@ -46,13 +26,16 @@ def _get_discounted_lines(self, program):
"""Hook method that allows to link lines from extra discount reward options"""
# TODO: Should we add the lines that meet the criteria even if they aren't
# on the filtered lines?
if program.discount_apply_on == "on_order":
if program.discount_applicability == "order":
return self.order_line.filtered(lambda x: not x.is_reward_line)
elif program.discount_apply_on == "cheapest_product":
return self._get_cheapest_line()
elif program.discount_apply_on == "specific_products":
elif program.discount_applicability == "cheapest":
return self._cheapest_line()
elif program.discount_applicability == "specific":
return self.order_line.filtered(
lambda x: x.product_id in program.discount_specific_product_ids
lambda x: x.product_id
in self.env["product.product"].search(
program._get_discount_product_domain()
)
)
# An extra discount scope could not depend no this module. For integration
# purposes, allways return at least an empty object
Expand All @@ -66,9 +49,7 @@ def _link_reward_discount_lines(self, program):
promotion and available in the order lines.
Thes filters are the same the core methods use.
"""
reward_lines = self.order_line.filtered(
lambda x: x.coupon_program_id == program
)
reward_lines = self.order_line.filtered(lambda x: x.reward_id == program)
lines = self._get_discounted_lines(program)
# Distribute different tax reward lines with their correspondant order lines.
# We use a dictionary so we can compare taxes even if they are composed.
Expand All @@ -84,15 +65,13 @@ def _link_reward_discount_lines(self, program):
def _link_reward_product_lines(self, program):
"""We want to link to the reward those lines that generated it as well as
the rewarded product, that could be not in the original domain"""
reward_lines = self.order_line.filtered(
lambda x: x.coupon_program_id == program
)
reward_lines = self.order_line.filtered(lambda x: x.reward_id == program)
lines_with_reward_product_id = self.order_line.filtered(
lambda x: x.product_id == program.reward_product_id
)
# We'll just consider as many lines as the reward quantity can cover.
lines = self.env["sale.order.line"]
reward_qty = program.reward_product_quantity
reward_qty = program.reward_product_qty
for line in lines_with_reward_product_id:
lines |= line
if line.product_uom_qty >= reward_qty:
Expand All @@ -114,29 +93,15 @@ def _link_reward_lines(self, programs):
def _link_reward_generated_lines(self, programs):
"""Link the lines that generated reward lines to those lines"""
for program in programs:
reward_lines = self.order_line.filtered(
lambda x: x.coupon_program_id == program
)
reward_lines = self.order_line.filtered(lambda x: x.reward_id == program)
self.order_line._filter_related_program_lines(program).write(
{"reward_generated_line_ids": [(4, rl.id) for rl in reward_lines]}
)

def _create_new_no_code_promo_reward_lines(self):
"""Ensure that the links remain"""
res = super()._create_new_no_code_promo_reward_lines()
for order in self:
order._link_reward_generated_lines(order.order_line.coupon_program_id)
return res


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

coupon_program_id = fields.Many2one(
comodel_name="coupon.program",
ondelete="restrict",
string="Coupon Program",
)
reward_line_ids = fields.Many2many(
comodel_name="sale.order.line",
relation="sale_line_reward_line_rel",
Expand Down Expand Up @@ -165,8 +130,8 @@ class SaleOrderLine(models.Model):
def write(self, vals):
"""When the reward line is update we should refresh the line links as well"""
res = super().write(vals)
if vals.get("is_reward_line") and vals.get("coupon_program_id"):
program = self.env["coupon.program"].browse(vals.get("coupon_program_id"))
if vals.get("is_reward_line") and vals.get("reward_id"):
program = self.env["loyalty.program"].browse(vals.get("reward_id"))

Check warning on line 134 in sale_coupon_order_line_link/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

sale_coupon_order_line_link/models/sale_order.py#L134

Added line #L134 was not covered by tests
for order in self.mapped("order_id"):
order._link_reward_lines(program)

Check warning on line 136 in sale_coupon_order_line_link/models/sale_order.py

View check run for this annotation

Codecov / codecov/patch

sale_coupon_order_line_link/models/sale_order.py#L136

Added line #L136 was not covered by tests
return res
Expand All @@ -176,9 +141,9 @@ def _filter_related_program_lines(self, program):
the product criteria rules, we can extend this method to return the proper
records."""
# No domain means that any product meets the promotion rules
if not program.rule_products_domain:
return self
domain = safe_eval(program.rule_products_domain)
if not program.program_id.rule_ids.product_domain:
return self.filtered(lambda x: not x.is_reward_line)
domain = safe_eval(program.program_id.rule_ids.product_domain)
products = self.mapped("product_id").filtered_domain(domain)
return self.filtered(
lambda x: x.product_id in products and not x.is_reward_line
Expand Down
1 change: 1 addition & 0 deletions sale_coupon_order_line_link/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
* `Tecnativa <https://www.tecnativa.com>`_:

* David Vidal
* Pilar Vargas

* `Ooops404 <https://www.ooops404.com>`_:

Expand Down
29 changes: 12 additions & 17 deletions sale_coupon_order_line_link/reports/sale_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,17 @@
class SaleReport(models.Model):
_inherit = "sale.report"

coupon_program_id = fields.Many2one(
comodel_name="coupon.program",
string="Coupon Program",
reward_id = fields.Many2one(
comodel_name="loyalty.reward",
string="Reward",
)

def _query(self, with_clause="", fields=None, groupby="", from_clause=""):
if fields is None:
fields = {}
select_str = """ ,
l.coupon_program_id as coupon_program_id
"""
fields.update({"coupon_program_id": select_str})
groupby += ", l.coupon_program_id"
return super()._query(
with_clause=with_clause,
fields=fields,
groupby=groupby,
from_clause=from_clause,
)
def _select_additional_fields(self):
res = super()._select_additional_fields()
res["reward_id"] = "l.reward_id"
return res

Check warning on line 18 in sale_coupon_order_line_link/reports/sale_report.py

View check run for this annotation

Codecov / codecov/patch

sale_coupon_order_line_link/reports/sale_report.py#L16-L18

Added lines #L16 - L18 were not covered by tests

def _group_by_sale(self):
res = super()._group_by_sale()
res += """, l.reward_id"""
return res

Check warning on line 23 in sale_coupon_order_line_link/reports/sale_report.py

View check run for this annotation

Codecov / codecov/patch

sale_coupon_order_line_link/reports/sale_report.py#L21-L23

Added lines #L21 - L23 were not covered by tests
8 changes: 4 additions & 4 deletions sale_coupon_order_line_link/reports/sale_report_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
<field name="model">sale.report</field>
<field name="arch" type="xml">
<field name="user_id" position="before">
<field name="coupon_program_id" />
<field name="reward_id" />
<separator />
<filter
name="promotion"
string="Promotion Lines"
domain="[('coupon_program_id','!=', False)]"
domain="[('reward_id','!=', False)]"
/>
</field>
<xpath expr="//filter[@name='status']" position="after">
<filter
string="Coupon Program"
string="Reward"
name="promotion_group"
context="{'group_by': 'coupon_program_id'}"
context="{'group_by': 'reward_id'}"
/>
</xpath>
</field>
Expand Down
Loading

0 comments on commit 4527b89

Please sign in to comment.