Skip to content

Commit

Permalink
Merge PR #113 into 13.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Sep 6, 2023
2 parents ef1cbaf + ae25e1b commit 482d45f
Show file tree
Hide file tree
Showing 16 changed files with 803 additions and 0 deletions.
91 changes: 91 additions & 0 deletions sale_coupon_promotion_generate_coupon/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
==========================================
Generate coupons in another coupon program
==========================================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |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-OCA%2Fsale--promotion-lightgray.png?logo=github
:target: https://github.com/OCA/sale-promotion/tree/13.0/sale_coupon_promotion_generate_coupon
: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-13-0/sale-promotion-13-0-sale_coupon_promotion_generate_coupon
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/296/13.0
:alt: Try me on Runbot

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

This module allows to generate pending coupons from a promotion to another one so the
rules to apply the coupon could be different from the ones to generate it.

**Table of contents**

.. contents::
:local:

Usage
=====

To use this module, you need to have configured:

#. A coupon program which will be the one where the coupons will be generated.
#. A promotion program with applicability on the next order and the first program
selected as next order program.

Then, when a user meets the promotion program requisites, a pending coupon will be
generated in the coupon program.

The user will be able to enjoy it in a later order as usual but the rules will be those
configured in that promotion.

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 smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/sale-promotion/issues/new?body=module:%20sale_coupon_promotion_generate_coupon%0Aversion:%2013.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>`_:

* David Vidal

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.

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

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions sale_coupon_promotion_generate_coupon/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
13 changes: 13 additions & 0 deletions sale_coupon_promotion_generate_coupon/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2023 Tecnativa - David Vidal
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Generate coupons in another coupon program",
"summary": "Allows to generate pending coupons in a coupon program",
"version": "13.0.1.0.0",
"author": "Tecnativa, Odoo Community Association (OCA)",
"license": "AGPL-3",
"category": "Sales Management",
"website": "https://github.com/OCA/sale-promotion",
"depends": ["sale_coupon"],
"data": ["views/coupon_program_views.xml"],
}
2 changes: 2 additions & 0 deletions sale_coupon_promotion_generate_coupon/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import coupon_program
from . import sale_order
46 changes: 46 additions & 0 deletions sale_coupon_promotion_generate_coupon/models/coupon_program.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2023 Tecnativa - David Vidal
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import api, fields, models


class SaleCouponProgram(models.Model):
_inherit = "sale.coupon.program"

next_order_program_id = fields.Many2one(
comodel_name="sale.coupon.program",
domain=[("program_type", "=", "coupon_program")],
)

@api.onchange("promo_applicability")
def _onchange_promo_applicability(self):
"""Remove the destination program on as all would behave in unexpected ways"""
if self.promo_applicability == "on_current_order":
self.next_order_program_id = False

def _compute_coupon_count(self):
"""Hook the destination program coupon counter"""
next_programs = self.filtered("next_order_program_id")
for program in next_programs:
program.coupon_count = program.next_order_program_id.coupon_count
return super(SaleCouponProgram, (self - next_programs))._compute_coupon_count()

def _compute_order_count(self):
"""Hook the destination program sales counter"""
next_programs = self.filtered("next_order_program_id")
for program in next_programs:
program.order_count = program.next_order_program_id.order_count
return super(SaleCouponProgram, (self - next_programs))._compute_order_count()

def action_next_order_program_coupons(self):
"""Hook the destination program coupon action"""
action = self.env["ir.actions.act_window"].for_xml_id(
"sale_coupon", "sale_coupon_action"
)
action["domain"] = [("program_id", "=", self.next_order_program_id.id)]
return action

def action_view_sales_orders(self):
"""Hook the destination program sales action"""
if self.next_order_program_id:
return self.next_order_program_id.action_view_sales_orders()
return super().action_view_sales_orders()
32 changes: 32 additions & 0 deletions sale_coupon_promotion_generate_coupon/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2023 Tecnativa - David Vidal
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import models


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

def _create_reward_coupon(self, program):
"""Check if the program is set to generate the coupon in other promotion"""
if program.next_order_program_id:
program = program.next_order_program_id
return super()._create_reward_coupon(program)

def _remove_invalid_reward_lines(self):
"""Ensure that the generated coupons for the next program are expired. The
original method relies on the original promo discount product ids so we
have to take an alternative approach expiring those remaining generated
coupons with no applied promo"""
res = super()._remove_invalid_reward_lines()
applied_programs = (
self.no_code_promo_program_ids
+ self.code_promo_program_id
+ self.applied_coupon_ids.mapped("program_id")
)
applied_programs += applied_programs.next_order_program_id
not_applicable_generated_coupons = self.generated_coupon_ids.filtered(
lambda x: x.program_id not in applied_programs
)
not_applicable_generated_coupons.write({"state": "expired"})
self.generated_coupon_ids -= not_applicable_generated_coupons
return res
3 changes: 3 additions & 0 deletions sale_coupon_promotion_generate_coupon/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `Tecnativa <https://www.tecnativa.com>`_:

* David Vidal
2 changes: 2 additions & 0 deletions sale_coupon_promotion_generate_coupon/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This module allows to generate pending coupons from a promotion to another one so the
rules to apply the coupon could be different from the ones to generate it.
11 changes: 11 additions & 0 deletions sale_coupon_promotion_generate_coupon/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
To use this module, you need to have configured:

#. A coupon program which will be the one where the coupons will be generated.
#. A promotion program with applicability on the next order and the first program
selected as next order program.

Then, when a user meets the promotion program requisites, a pending coupon will be
generated in the coupon program.

The user will be able to enjoy it in a later order as usual but the rules will be those
configured in that promotion.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 482d45f

Please sign in to comment.