Add modules commission_simple, commission_simple_agent, commission_simple_agent_purchase
Add demo data in sale_agent
This commit is contained in:
2
commission_simple/wizards/__init__.py
Normal file
2
commission_simple/wizards/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from . import commission_compute
|
||||
from . import res_config_settings
|
||||
77
commission_simple/wizards/commission_compute.py
Normal file
77
commission_simple/wizards/commission_compute.py
Normal file
@@ -0,0 +1,77 @@
|
||||
# Copyright 2019-2024 Akretion France (http://www.akretion.com)
|
||||
# @author Alexis de Lattre <alexis.delattre@akretion.com>
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import api, fields, models, _
|
||||
from dateutil.relativedelta import relativedelta
|
||||
from odoo.exceptions import UserError
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CommissionCompute(models.TransientModel):
|
||||
_name = 'commission.compute'
|
||||
_description = 'Compute Commissions'
|
||||
|
||||
company_id = fields.Many2one('res.company', required=True, default=lambda self: self.env.company)
|
||||
date_range_type_id = fields.Many2one(related='company_id.commission_date_range_type_id')
|
||||
date_range_id = fields.Many2one(
|
||||
'date.range', required=True, string='Period',
|
||||
compute='_compute_date_range_id', store=True, precompute=True, readonly=False,
|
||||
domain="[('type_id', '=', date_range_type_id)]")
|
||||
date_start = fields.Date(related='date_range_id.date_start')
|
||||
date_end = fields.Date(related='date_range_id.date_end')
|
||||
|
||||
@api.depends('company_id')
|
||||
def _compute_date_range_id(self):
|
||||
for wiz in self:
|
||||
date_range_id = False
|
||||
company = wiz.company_id
|
||||
if company and company.commission_date_range_type_id:
|
||||
type_id = company.commission_date_range_type_id.id
|
||||
last_commission_result = self.env['commission.result'].search([
|
||||
('company_id', '=', company.id),
|
||||
], order='date_end desc', limit=1)
|
||||
limit_date = last_commission_result and last_commission_result.date_end or (fields.Date.context_today(self) + relativedelta(months=-2, day=31))
|
||||
date_range = self.env['date.range'].search([
|
||||
('company_id', 'in', (company.id, False)),
|
||||
('type_id', '=', type_id),
|
||||
('date_start', '>', limit_date)
|
||||
], order='date_start', limit=1)
|
||||
date_range_id = date_range and date_range.id or False
|
||||
wiz.date_range_id = date_range_id
|
||||
|
||||
def run(self):
|
||||
self.ensure_one()
|
||||
creso = self.env['commission.result']
|
||||
date_range = self.date_range_id
|
||||
existing_commissions = creso.search([
|
||||
('date_range_id', '=', date_range.id),
|
||||
('company_id', '=', self.company_id.id),
|
||||
])
|
||||
if existing_commissions:
|
||||
raise UserError(_(
|
||||
'Commissions already exist for %(period)s in company %(company)s.',
|
||||
period=date_range.display_name, company=self.company_id.display_name))
|
||||
com_result_ids = self._core_compute()
|
||||
if not com_result_ids:
|
||||
raise UserError(_('No commissions generated.'))
|
||||
action = self.env['ir.actions.actions']._for_xml_id(
|
||||
'commission_simple.commission_result_action')
|
||||
action.update({
|
||||
'views': False,
|
||||
'domain': f"[('id', 'in', {com_result_ids})]",
|
||||
})
|
||||
return action
|
||||
|
||||
def _core_compute(self):
|
||||
rules = self.env['commission.rule'].load_all_rules()
|
||||
com_result_ids = []
|
||||
assignments = self.env['commission.profile.assignment'].search([('company_id', '=', self.company_id.id)])
|
||||
for assignment in assignments:
|
||||
com_result = assignment._generate_commission_result(self.date_range_id, rules)
|
||||
if com_result:
|
||||
com_result_ids.append(com_result.id)
|
||||
else:
|
||||
logger.info("No commission for %s", assignment._get_partner().display_name)
|
||||
return com_result_ids
|
||||
41
commission_simple/wizards/commission_compute_view.xml
Normal file
41
commission_simple/wizards/commission_compute_view.xml
Normal file
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright 2019-2024 Akretion France (https://www.akretion.com)
|
||||
@author Alexis de Lattre <alexis.delattre@akretion.com>
|
||||
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
-->
|
||||
|
||||
<odoo>
|
||||
|
||||
<record id="commission_compute_form" model="ir.ui.view">
|
||||
<field name="name">commission.compute.form</field>
|
||||
<field name="model">commission.compute</field>
|
||||
<field name="arch" type="xml">
|
||||
<form>
|
||||
<group name="main">
|
||||
<field name="company_id" groups="base.group_multi_company"/>
|
||||
<field name="company_id" invisible="1"/>
|
||||
<field name="date_range_type_id" invisible="1"/>
|
||||
<field name="date_range_id"/>
|
||||
<field name="date_start"/>
|
||||
<field name="date_end"/>
|
||||
</group>
|
||||
<footer>
|
||||
<button name="run" type="object" string="Compute"
|
||||
class="btn-primary"/>
|
||||
<button special="cancel" string="Cancel"/>
|
||||
</footer>
|
||||
</form>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="commission_compute_action" model="ir.actions.act_window">
|
||||
<field name="name">Compute Commissions</field>
|
||||
<field name="res_model">commission.compute</field>
|
||||
<field name="view_mode">form</field>
|
||||
<field name="target">new</field>
|
||||
</record>
|
||||
|
||||
<menuitem id="commission_compute_menu" action="commission_compute_action" parent="commission_root" sequence="15" groups="account.group_account_user"/>
|
||||
|
||||
</odoo>
|
||||
12
commission_simple/wizards/res_config_settings.py
Normal file
12
commission_simple/wizards/res_config_settings.py
Normal file
@@ -0,0 +1,12 @@
|
||||
# Copyright 2019-2024 Akretion France (https://www.akretion.com/)
|
||||
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class ResConfigSettings(models.TransientModel):
|
||||
_inherit = 'res.config.settings'
|
||||
|
||||
commission_date_range_type_id = fields.Many2one(
|
||||
related='company_id.commission_date_range_type_id', readonly=False)
|
||||
Reference in New Issue
Block a user