Skip to content

Commit

Permalink
Merge commit 'refs/pull/212/head' of https://github.com/oca/sale-prom…
Browse files Browse the repository at this point in the history
…otion into 16.0-add-loyalty_setup_validity-20240430
  • Loading branch information
PlantBasedStudio committed May 2, 2024
2 parents 5862a5f + c003902 commit 30937ec
Show file tree
Hide file tree
Showing 26 changed files with 1,041 additions and 0 deletions.
1 change: 1 addition & 0 deletions loyalty_partner_birthdate/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
16 changes: 16 additions & 0 deletions loyalty_partner_birthdate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2024 Moka Tourisme (https://www.mokatourisme.fr/).
# @author Damien Horvat <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Loyalty Partner Birthdate",
"version": "16.0.1.0.0",
"category": "Sale",
"author": "Moka",
"website": "https://github.com/OCA/sale-promotion",
"summary": "Implement a cron job to launch the generation of loyalty where conditions are set on the partner's birthdate.",
"depends": ["loyalty", "loyalty_setup_validity", "partner_contact_birthdate"],
"installable": True,
"auto_install": False,
"license": "AGPL-3",
"data": ["views/loyalty_program_views_inherit.xml", "data/ir_cron_data.xml"],
}
16 changes: 16 additions & 0 deletions loyalty_partner_birthdate/data/ir_cron_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="ir_cron_birthday_rewards" model="ir.cron">
<field name="name">Birthday Loyalty Cron</field>
<field name="model_id" ref="model_loyalty_program"/>
<field name="type">ir.actions.server</field>
<field name="state">code</field>
<field name="code">model.generate_birthday_loyalty_cards()</field>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field name="nextcall" eval="(DateTime.now().replace(hour=0,minute=0)+timedelta(days=1)).strftime('%Y-%m-%d %H:%M:%S')" />
<field name="active" eval="True"/>
</record>
</odoo>

77 changes: 77 additions & 0 deletions loyalty_partner_birthdate/i18n/fr.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * loyalty_partner_birthdate
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 16.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-24 11:32+0000\n"
"PO-Revision-Date: 2024-04-24 11:32+0000\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: loyalty_partner_birthdate
#: model:ir.model.fields.selection,name:loyalty_partner_birthdate.selection__loyalty_program__birthday_trigger_mode__after
msgid "After Birthday"
msgstr "Après l'anniversaire"

#. module: loyalty_partner_birthdate
#: model:ir.model.fields.selection,name:loyalty_partner_birthdate.selection__loyalty_program__birthday_trigger_mode__before
msgid "Before Birthday"
msgstr "Avant l'anniversaire"

#. module: loyalty_partner_birthdate
#: model:ir.model.fields,field_description:loyalty_partner_birthdate.field_loyalty_program__birthday_check_days
msgid "Birthday Check Days"
msgstr "Jours d'écarts"

#. module: loyalty_partner_birthdate
#: model:ir.actions.server,name:loyalty_partner_birthdate.ir_cron_birthday_rewards_ir_actions_server
#: model:ir.cron,cron_name:loyalty_partner_birthdate.ir_cron_birthday_rewards
msgid "Birthday Loyalty Cron"
msgstr "Cron de remise sur les anniversaires"

#. module: loyalty_partner_birthdate
#: model_terms:ir.ui.view,arch_db:loyalty_partner_birthdate.loyalty_program_view_form_inherit
msgid "Birthday Trigger"
msgstr "Mode anniversaire"

#. module: loyalty_partner_birthdate
#: model:ir.model.fields,field_description:loyalty_partner_birthdate.field_loyalty_program__birthday_trigger_mode
msgid "Birthday Trigger Mode"
msgstr "Mode de déclenchement"

#. module: loyalty_partner_birthdate
#: model:ir.model.fields,field_description:loyalty_partner_birthdate.field_loyalty_program__birthday_trigger_enabled
msgid "Enable Birthday Trigger"
msgstr "Activer le mode anniversaire"

#. module: loyalty_partner_birthdate
#: model:ir.model.fields,help:loyalty_partner_birthdate.field_loyalty_program__birthday_trigger_enabled
msgid "Enable to trigger the discount based on birthday."
msgstr "Activer pour basé la remise sur une condition d'anniversaire"

#. module: loyalty_partner_birthdate
#: model:ir.model.fields.selection,name:loyalty_partner_birthdate.selection__loyalty_program__birthday_trigger_mode__exact
msgid "Exact Birthday"
msgstr "Jour exact"

#. module: loyalty_partner_birthdate
#: model:ir.model,name:loyalty_partner_birthdate.model_loyalty_program
msgid "Loyalty program"
msgstr "Règle de fidélité"

#. module: loyalty_partner_birthdate
#: model:ir.model.fields,help:loyalty_partner_birthdate.field_loyalty_program__birthday_check_days
msgid "Number of days before or after the birthday to check."
msgstr "Nombre de jours d'écarts à vérifier avant ou après l'anniversaire"

#. module: loyalty_partner_birthdate
#: model:ir.model.fields,help:loyalty_partner_birthdate.field_loyalty_program__birthday_trigger_mode
msgid "Trigger the discount before, on, or after the birthday."
msgstr "Lancer la remise avant, pendant ou après l'anniversaire"
1 change: 1 addition & 0 deletions loyalty_partner_birthdate/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import loyalty_program_inherit
107 changes: 107 additions & 0 deletions loyalty_partner_birthdate/models/loyalty_program_inherit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Copyright 2024 Moka Tourisme (https://www.mokatourisme.fr/).
# @author Damien Horvat <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).


from odoo import _, api, fields, models
import logging
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta

class LoyaltyProgram(models.Model):
_inherit = 'loyalty.program'

birthday_trigger_enabled = fields.Boolean('Enable Birthday Trigger',
help="Enable to trigger the discount based on birthday.")

birthday_trigger_mode = fields.Selection([
('before', 'Before Birthday'),
('exact', 'Exact Birthday'),
('after', 'After Birthday'),
], string='Birthday Trigger Mode', default='before',
help="Trigger the discount before, on, or after the birthday.")

birthday_check_days = fields.Integer('Birthday Check Days', default=7,
help="Number of days before or after the birthday to check.")

@api.model
def _is_partner_birthday_within_range(self, partner):
"""Check if partner's birthday is within the specified range."""
if not self.birthday_trigger_enabled:
return False

today = datetime.now().date()
birthday = partner.birthdate
if not birthday:
return False

if self.birthday_trigger_mode == 'before':
deadline_date = today + timedelta(days=self.birthday_check_days)
elif self.birthday_trigger_mode == 'exact':
deadline_date = today
else:
deadline_date = today - timedelta(days=self.birthday_check_days)

return deadline_date

def _compute_amount(self, currency_to):
self.ensure_one()

deadline_date = self._is_partner_birthday_within_range(self)

if not deadline_date:
return super()._compute_amount(currency_to)

if self.birthday_trigger_mode == 'exact':
valid_partners = self.env['res.partner'].search([('birthdate', '=', deadline_date)])
elif self.birthday_trigger_mode == 'before':
valid_partners = self.env['res.partner'].search([('birthdate', '<=', deadline_date)])
else:
valid_partners = self.env['res.partner'].search([('birthdate', '>=', deadline_date)])

valid_amount = super()._compute_amount(currency_to)

if self.program_type == 'birthday' and valid_partners:
valid_amount *= len(valid_partners)

return valid_amount

@api.model
def generate_birthday_loyalty_cards(self):
"""Cron method to generate birthday loyalty cards."""
birthday_rules = self.search([('birthday_trigger_enabled', '=', True)])
for rule in birthday_rules:
valid_partners = self.env['res.partner'].search([('birthdate_date', '!=', False)])

today = datetime.now().date()
if rule.birthday_trigger_mode == 'before':
target_date = today + relativedelta(days=rule.birthday_check_days)
valid_partners = valid_partners.filtered(lambda partner:
partner.birthdate_date.month == target_date.month and
partner.birthdate_date.day == target_date.day
)
elif rule.birthday_trigger_mode == 'exact':
valid_partners = valid_partners.filtered(lambda partner:
partner.birthdate_date.month == today.month and
partner.birthdate_date.day == today.day
)
elif rule.birthday_trigger_mode == 'after':
target_date = today - relativedelta(days=rule.birthday_check_days)
valid_partners = valid_partners.filtered(lambda partner:
partner.birthdate_date.month == target_date.month and
partner.birthdate_date.day == target_date.day
)


for partner in valid_partners:
try:
loyalty_card = self.env['loyalty.card'].create({
'program_id': rule.id,
'partner_id': partner.id,
'points' : 1
})
except Exception as e:
logging.error(f"Error creating loyalty card for partner {partner.id}: {e}")



32 changes: 32 additions & 0 deletions loyalty_partner_birthdate/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
loyalty_partner_birthdate
=========

Allow to setup loyalty based on partner's birthdate (before, exact or after) by using a cron

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

Bugs are tracked on GitHub Issues https://github.com/Moka-Tourisme/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.


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

Credits
=======

## Authors

* Moka Tourisme

## Contributors

* Horvat Damien : https://github.com/PlantBasedStudio

## Maintainers
This module is maintained by Moka.


This module is a addon for the Odoo/addons/loyalty https://github.com/odoo/odoo/tree/16.0/addons/loyalty> project on GitHub.
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
26 changes: 26 additions & 0 deletions loyalty_partner_birthdate/views/loyalty_program_views_inherit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2024 Moka Tourisme (https://www.mokatourisme.fr/).
@author Damien Horvat <[email protected]>
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
-->
<odoo>
<record id="loyalty_program_view_form_inherit" model="ir.ui.view">
<field name="name">loyalty.program.view.form.inherit</field>
<field name="model">loyalty.program</field>
<field name="inherit_id" ref="loyalty.loyalty_program_view_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='date_to']" position="after">
<field name="birthday_trigger_enabled"/>
<field name="birthday_trigger_mode"
attrs="{'invisible': [('birthday_trigger_enabled', '=', False)]}"
on_change="on_change_birthday_trigger_mode(birthday_trigger_mode)"/>
<field name="birthday_check_days"
attrs="{'invisible': ['|', ('birthday_trigger_enabled', '=', False),
('birthday_trigger_mode', '=', 'exact')]}"/>
</xpath>
</field>
</record>


</odoo>
74 changes: 74 additions & 0 deletions loyalty_setup_validity/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
======================
Loyalty Setup Validity
======================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:f8dd509d9d06a7632d937198a7bdd4f1ea6481f953ce334738621e81f86bae43
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |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/16.0/loyalty_setup_validity
: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-loyalty_setup_validity
: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|

Normally, the loyalty program is based on a fixed validity date. With this module, you
can choose between a fixed date or a date generated within a period of time after the
loyalty has been generated (for example, after a visit to the point of sale, a customer
can obtain a discount for 30 days).

**Table of contents**

.. contents::
:local:

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:%20loyalty_setup_validity%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
~~~~~~~

* Moka

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/16.0/loyalty_setup_validity>`_ 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 loyalty_setup_validity/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
19 changes: 19 additions & 0 deletions loyalty_setup_validity/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2024 Moka Tourisme (https://www.mokatourisme.fr/).
# @author Damien Horvat <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Loyalty Setup Validity",
"version": "16.0.1.0.0",
"category": "Sale",
"author": "Moka, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/sale-promotion",
"summary": (
"Allow to define a fixed validity or a validity after"
" the generation date of a loyalty card."
),
"depends": ["loyalty"],
"installable": True,
"auto_install": False,
"license": "AGPL-3",
"data": ["views/loyalty_program_inherit.xml"],
}
Loading

0 comments on commit 30937ec

Please sign in to comment.