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] loyalty_setup_validity #212

Closed
Closed
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
5 changes: 4 additions & 1 deletion .copier-answers.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Do NOT update manually; changes here will be overwritten by Copier
_commit: v1.17.2
_commit: v1.21.1
_src_path: gh:oca/oca-addons-repo-template
ci: GitHub
convert_readme_fragments_to_markdown: false
generate_requirements_txt: true
github_check_license: true
github_ci_extra_env: {}
Expand All @@ -19,4 +20,6 @@ repo_description: Odoo addons for handling promotions on the sales funnel.
repo_name: Sales promotion management
repo_slug: sale-promotion
repo_website: https://github.com/OCA/sale-promotion
use_pyproject_toml: false
use_ruff: false

4 changes: 2 additions & 2 deletions .github/workflows/stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Stale PRs and issues policy
uses: actions/stale@v4
uses: actions/stale@v9
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
# General settings.
Expand Down Expand Up @@ -48,7 +48,7 @@ jobs:
# * Issues that are pending more information
# * Except Issues marked as "no stale"
- name: Needs more information stale issues policy
uses: actions/stale@v4
uses: actions/stale@v9
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
ascending: true
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ jobs:
run: oca_init_test_database
- name: Run tests
run: oca_run_tests
- uses: codecov/codecov-action@v1
- uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
- name: Update .pot files
run: oca_export_and_push_pot https://x-access-token:${{ secrets.GIT_PUSH_TOKEN }}@github.com/${{ github.repository }}
if: ${{ matrix.makepot == 'true' && github.event_name == 'push' && github.repository_owner == 'OCA' }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ __pycache__/
*.py[cod]
/.venv
/.pytest_cache
/.ruff_cache

# C extensions
*.so
Expand Down
7 changes: 6 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ exclude: |
^docs/_templates/.*\.html$|
# Don't bother non-technical authors with formatting issues in docs
readme/.*\.(rst|md)$|
# Ignore build and dist directories in addons
/build/|/dist/|
# Ignore test files in addons
/tests/samples/.*|
# You don't usually want a bot to modify your legal texts
(LICENSE.*|COPYING.*)
default_language_version:
Expand All @@ -35,7 +39,7 @@ repos:
language: fail
files: '[a-zA-Z0-9_]*/i18n/en\.po$'
- repo: https://github.com/oca/maintainer-tools
rev: 969238e47c07d0c40573acff81d170f63245d738
rev: 9a170331575a265c092ee6b24b845ec508e8ef75
hooks:
# update the NOT INSTALLABLE ADDONS section above
- id: oca-update-pre-commit-excluded-addons
Expand All @@ -48,6 +52,7 @@ repos:
- --org-name=OCA
- --repo-name=sale-promotion
- --if-source-changed
- --keep-source-digest
- repo: https://github.com/OCA/odoo-pre-commit-hooks
rev: v0.0.25
hooks:
Expand Down
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>
Loading
Loading