Initial commit: Sale packages

This commit is contained in:
Ernad Husremovic 2025-08-29 15:20:49 +02:00
commit 14e3d26998
6469 changed files with 2479670 additions and 0 deletions

View file

@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import sale_loyalty_coupon_wizard
from . import sale_loyalty_reward_wizard

View file

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import _, fields, models
from odoo.exceptions import ValidationError
class SaleLoyaltyCouponWizard(models.TransientModel):
_name = 'sale.loyalty.coupon.wizard'
_description = 'Sale Loyalty - Apply Coupon Wizard'
order_id = fields.Many2one('sale.order', default=lambda self: self.env.context.get('active_id'), required=True)
coupon_code = fields.Char(required=True)
def action_apply(self):
self.ensure_one()
if not self.order_id:
raise ValidationError(_('Invalid sales order.'))
status = self.order_id._try_apply_code(self.coupon_code)
if 'error' in status:
raise ValidationError(status['error'])
all_rewards = self.env['loyalty.reward']
for rewards in status.values():
all_rewards |= rewards
action = self.env['ir.actions.actions']._for_xml_id('sale_loyalty.sale_loyalty_reward_wizard_action')
action['context'] = {
'active_id': self.order_id.id,
'default_reward_ids': all_rewards.ids,
}
return action

View file

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="sale_loyalty_coupon_wizard_view_form" model="ir.ui.view">
<field name="name">sale.loyalty.coupon.wizard.view.form</field>
<field name="model">sale.loyalty.coupon.wizard</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<group>
<field name="coupon_code"/>
</group>
</group>
</sheet>
<footer>
<button type="object" name="action_apply" string="Apply" class="btn btn-primary" data-hotkey="q"/>
<button special="cancel" string="Discard" class="btn btn-secondary" data-hotkey="z"/>
</footer>
</form>
</field>
</record>
<record id="sale_loyalty_coupon_wizard_action" model="ir.actions.act_window">
<field name="name">Enter Promotion or Coupon Code</field>
<field name="res_model">sale.loyalty.coupon.wizard</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
</odoo>

View file

@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
class SaleLoyaltyRewardWizard(models.TransientModel):
_name = 'sale.loyalty.reward.wizard'
_description = 'Sale Loyalty - Reward Selection Wizard'
order_id = fields.Many2one('sale.order', default=lambda self: self.env.context.get('active_id'), required=True)
reward_ids = fields.Many2many('loyalty.reward', compute='_compute_claimable_reward_ids')
selected_reward_id = fields.Many2one('loyalty.reward', domain="[('id', 'in', reward_ids)]")
# In case of multi_product reward
multi_product_reward = fields.Boolean(related='selected_reward_id.multi_product')
reward_product_ids = fields.Many2many(related='selected_reward_id.reward_product_ids')
selected_product_id = fields.Many2one('product.product', domain="[('id', 'in', reward_product_ids)]",
compute='_compute_selected_product_id', readonly=False, store=True,)
@api.depends('order_id')
def _compute_claimable_reward_ids(self):
for wizard in self:
if not wizard.order_id:
wizard.reward_ids = False
else:
claimable_reward = wizard.order_id._get_claimable_rewards()
reward_ids = self.env['loyalty.reward']
for rewards in claimable_reward.values():
reward_ids |= rewards
wizard.reward_ids = reward_ids
@api.depends('reward_product_ids')
def _compute_selected_product_id(self):
for wizard in self:
if not wizard.selected_reward_id.reward_type == 'product':
wizard.selected_product_id = False
else:
wizard.selected_product_id = wizard.reward_product_ids[:1]
def action_apply(self):
self.ensure_one()
if not self.selected_reward_id:
raise ValidationError(_('No reward selected.'))
claimable_rewards = self.order_id._get_claimable_rewards()
selected_coupon = False
for coupon, rewards in claimable_rewards.items():
if self.selected_reward_id in rewards:
selected_coupon = coupon
break
if not selected_coupon:
raise ValidationError(_('Coupon not found while trying to add the following reward: %s', self.selected_reward_id.description))
self.order_id._apply_program_reward(self.selected_reward_id, coupon, product=self.selected_product_id)
self.order_id._update_programs_and_rewards()
return True

View file

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="sale_loyalty_reward_wizard_view_form" model="ir.ui.view">
<field name="name">sale.loyalty.reward.wizard.view.form</field>
<field name="model">sale.loyalty.reward.wizard</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<field name="order_id" invisible="1"/>
<field name="reward_ids" invisible="1"/>
<field name="multi_product_reward" invisible="1"/>
<field name="reward_product_ids" invisible="1"/>
<a attrs="{'invisible': [('reward_ids', '!=', [])]}">
No rewards available for this customer!
</a>
<label for="selected_reward_id" string="Choose your reward:" colspan="2"
attrs="{'invisible': [('reward_ids', '=', [])]}"/>
<field name="selected_reward_id" widget="radio" colspan="2" nolabel="1"
attrs="{'invisible': [('reward_ids', '=', [])]}"/>
<label for="selected_product_id" string="Choose a product:" colspan="2"
attrs="{'invisible': [('multi_product_reward', '=', False)]}"/>
<field name="selected_product_id" widget="radio" colspan="2" nolabel="1"
attrs="{'invisible': [('multi_product_reward', '=', False)]}"/>
</group>
</sheet>
<footer>
<!-- Has rewards -->
<button type="object" name="action_apply" string="Apply" class="btn btn-primary"
attrs="{'invisible': [('reward_ids', '=', [])]}" data-hotkey="q"/>
<button special="cancel" string="Discard" class="btn btn-secondary"
attrs="{'invisible': [('reward_ids', '=', [])]}" data-hotkey="z"/>
<!-- No rewards -->
<button special="cancel" string="Discard" class="btn btn-primary"
attrs="{'invisible': [('reward_ids', '!=', [])]}" data-hotkey="z"/>
<button type="action" name="%(loyalty.loyalty_program_discount_loyalty_action)d" string="Coupons &amp; Loyalty" class="btn btn-secondary float-end"
attrs="{'invisible': [('reward_ids', '!=', [])]}" groups="sales_team.group_sale_manager" data-hotkey="q"/>
</footer>
</form>
</field>
</record>
<record id="sale_loyalty_reward_wizard_action" model="ir.actions.act_window">
<field name="name">Available Rewards</field>
<field name="res_model">sale.loyalty.reward.wizard</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
</odoo>