[IMP] commission_simple: periodicity moved from company to profile
Update commission_simple_agent_purchase to adapt config page form view accordingly
This commit is contained in:
@@ -41,7 +41,6 @@ This module has been written by Alexis de Lattre from Akretion
|
|||||||
'views/commission_rule.xml',
|
'views/commission_rule.xml',
|
||||||
'views/commission_result.xml',
|
'views/commission_result.xml',
|
||||||
'views/account_move_line.xml',
|
'views/account_move_line.xml',
|
||||||
'views/res_config_settings.xml',
|
|
||||||
'wizards/commission_compute_view.xml',
|
'wizards/commission_compute_view.xml',
|
||||||
],
|
],
|
||||||
'installable': True,
|
'installable': True,
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
from . import commission_profile
|
from . import commission_profile
|
||||||
from . import commission_rule
|
from . import commission_rule
|
||||||
from . import commission_result
|
from . import commission_result
|
||||||
from . import res_company
|
|
||||||
from . import account_move_line
|
from . import account_move_line
|
||||||
|
|||||||
@@ -27,6 +27,9 @@ class CommissionProfile(models.Model):
|
|||||||
('paid', 'Paid'),
|
('paid', 'Paid'),
|
||||||
('in_payment', 'In Payment and Paid'),
|
('in_payment', 'In Payment and Paid'),
|
||||||
], default='paid', string='Trigger', required=True)
|
], default='paid', string='Trigger', required=True)
|
||||||
|
date_range_type_id = fields.Many2one(
|
||||||
|
'date.range.type', string='Commission Periodicity', ondelete='restrict',
|
||||||
|
domain="[('company_id', 'in', (False, company_id))]")
|
||||||
|
|
||||||
|
|
||||||
class CommissionProfileAssignment(models.Model):
|
class CommissionProfileAssignment(models.Model):
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
# 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 ResCompany(models.Model):
|
|
||||||
_inherit = 'res.company'
|
|
||||||
|
|
||||||
commission_date_range_type_id = fields.Many2one(
|
|
||||||
'date.range.type', string='Commission Periodicity', ondelete='restrict')
|
|
||||||
@@ -22,6 +22,7 @@
|
|||||||
<field name="name"/>
|
<field name="name"/>
|
||||||
<field name="active" invisible="1"/>
|
<field name="active" invisible="1"/>
|
||||||
<field name="trigger_type" widget="radio"/>
|
<field name="trigger_type" widget="radio"/>
|
||||||
|
<field name="date_range_type_id"/>
|
||||||
</group>
|
</group>
|
||||||
<group name="main-right">
|
<group name="main-right">
|
||||||
<field name="company_id" invisible="1"/>
|
<field name="company_id" invisible="1"/>
|
||||||
@@ -56,11 +57,30 @@
|
|||||||
<field name="sequence" widget="handle"/>
|
<field name="sequence" widget="handle"/>
|
||||||
<field name="name" decoration-bf="1"/>
|
<field name="name" decoration-bf="1"/>
|
||||||
<field name="trigger_type" optional="show" widget="badge" decoration-info="trigger_type == 'invoice'" decoration-success="trigger_type == 'paid'" decoration-warning="trigger_type == 'in_payment'"/>
|
<field name="trigger_type" optional="show" widget="badge" decoration-info="trigger_type == 'invoice'" decoration-success="trigger_type == 'paid'" decoration-warning="trigger_type == 'in_payment'"/>
|
||||||
|
<field name="date_range_type_id" optional="show"/>
|
||||||
<field name="company_id" groups="base.group_multi_company"/>
|
<field name="company_id" groups="base.group_multi_company"/>
|
||||||
</tree>
|
</tree>
|
||||||
</field>
|
</field>
|
||||||
</record>
|
</record>
|
||||||
|
|
||||||
|
<record id="commission_profile_search" model="ir.ui.view">
|
||||||
|
<field name="model">commission.profile</field>
|
||||||
|
<field name="arch" type="xml">
|
||||||
|
<search>
|
||||||
|
<field name="name"/>
|
||||||
|
<filter string="Invoiced" name="invoice" domain="[('trigger_type', '=', 'invoice')]"/>
|
||||||
|
<filter string="Paid" name="paid" domain="[('trigger_type', '=', 'paid')]"/>
|
||||||
|
<filter string="In Payment and Paid" name="in_payment" domain="[('trigger_type', '=', 'in_payment')]"/>
|
||||||
|
<separator/>
|
||||||
|
<filter string="Archived" name="inactive" domain="[('active', '=', False)]"/>
|
||||||
|
<separator/>
|
||||||
|
<group name="groupby">
|
||||||
|
<filter name="date_range_type_groupby" string="Commission Periodicity" context="{'group_by': 'date_range_type_id'}"/>
|
||||||
|
</group>
|
||||||
|
</search>
|
||||||
|
</field>
|
||||||
|
</record>
|
||||||
|
|
||||||
<record id="commission_profile_action" model="ir.actions.act_window">
|
<record id="commission_profile_action" model="ir.actions.act_window">
|
||||||
<field name="name">Commission Profiles</field>
|
<field name="name">Commission Profiles</field>
|
||||||
<field name="res_model">commission.profile</field>
|
<field name="res_model">commission.profile</field>
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
<?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="res_config_settings_view_form" model="ir.ui.view">
|
|
||||||
<field name="name">commission.res.config.settings.form</field>
|
|
||||||
<field name="model">res.config.settings</field>
|
|
||||||
<field name="inherit_id" ref="account.res_config_settings_view_form" />
|
|
||||||
<field name="arch" type="xml">
|
|
||||||
<xpath expr="//div[@id='storno']" position="after">
|
|
||||||
<h2>Commissions</h2>
|
|
||||||
<div class="row mt16 o_settings_container" id="commission_simple">
|
|
||||||
<div class="col-12 col-lg-12 o_setting_box" id="commission_simple-settings">
|
|
||||||
<div class="o_setting_left_pane" />
|
|
||||||
<div class="o_setting_right_pane">
|
|
||||||
<div class="row" id="commission_date_range_type_id">
|
|
||||||
<label
|
|
||||||
for="commission_date_range_type_id"
|
|
||||||
class="col-md-5"
|
|
||||||
/>
|
|
||||||
<field name="commission_date_range_type_id" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</xpath>
|
|
||||||
</field>
|
|
||||||
</record>
|
|
||||||
|
|
||||||
|
|
||||||
</odoo>
|
|
||||||
@@ -1,2 +1 @@
|
|||||||
from . import commission_compute
|
from . import commission_compute
|
||||||
from . import res_config_settings
|
|
||||||
|
|||||||
@@ -3,8 +3,9 @@
|
|||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
from odoo import api, fields, models, _
|
from odoo import api, fields, models, _
|
||||||
from dateutil.relativedelta import relativedelta
|
from datetime import datetime, timedelta
|
||||||
from odoo.exceptions import UserError
|
from odoo.exceptions import UserError
|
||||||
|
from odoo.tools.misc import format_date
|
||||||
import logging
|
import logging
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -13,46 +14,48 @@ class CommissionCompute(models.TransientModel):
|
|||||||
_name = 'commission.compute'
|
_name = 'commission.compute'
|
||||||
_description = 'Compute Commissions'
|
_description = 'Compute Commissions'
|
||||||
|
|
||||||
company_id = fields.Many2one('res.company', required=True, default=lambda self: self.env.company)
|
company_id = fields.Many2one('res.company', required=True)
|
||||||
date_range_type_id = fields.Many2one(related='company_id.commission_date_range_type_id')
|
date_start = fields.Date(string="Period Start Date", required=True)
|
||||||
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')
|
@api.model
|
||||||
def _compute_date_range_id(self):
|
def default_get(self, fields_list):
|
||||||
for wiz in self:
|
res = super().default_get(fields_list)
|
||||||
date_range_id = False
|
company = self.env.company
|
||||||
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([
|
last_commission_result = self.env['commission.result'].search([
|
||||||
('company_id', '=', company.id),
|
('company_id', '=', company.id),
|
||||||
], order='date_end desc', limit=1)
|
], order='date_start 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))
|
if last_commission_result:
|
||||||
date_range = self.env['date.range'].search([
|
last_start_date = last_commission_result.date_start
|
||||||
('company_id', 'in', (company.id, False)),
|
commissions_last_start_date = self.env['commission.result'].search([
|
||||||
('type_id', '=', type_id),
|
('date_start', '=', last_start_date),
|
||||||
('date_start', '>', limit_date)
|
('company_id', '=', company.id),
|
||||||
], order='date_start', limit=1)
|
], order="date_end asc", limit=1)
|
||||||
date_range_id = date_range and date_range.id or False
|
min_end_date = commissions_last_start_date.date_end
|
||||||
wiz.date_range_id = date_range_id
|
date_start = min_end_date + timedelta(1)
|
||||||
|
else:
|
||||||
|
today = fields.Date.context_today(self)
|
||||||
|
date_start = datetime(today.year, today.month, 1)
|
||||||
|
res.update({
|
||||||
|
'company_id': company.id,
|
||||||
|
'date_start': date_start,
|
||||||
|
})
|
||||||
|
return res
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.ensure_one()
|
self.ensure_one()
|
||||||
|
if not self.date_start:
|
||||||
|
raise UserError(_("Missing Period Start Date."))
|
||||||
creso = self.env['commission.result']
|
creso = self.env['commission.result']
|
||||||
date_range = self.date_range_id
|
|
||||||
existing_commissions = creso.search([
|
existing_commissions = creso.search([
|
||||||
('date_range_id', '=', date_range.id),
|
('date_start', '=', self.date_start),
|
||||||
('company_id', '=', self.company_id.id),
|
('company_id', '=', self.company_id.id),
|
||||||
])
|
])
|
||||||
if existing_commissions:
|
if existing_commissions:
|
||||||
raise UserError(_(
|
raise UserError(_(
|
||||||
'Commissions already exist for %(period)s in company %(company)s.',
|
'%(count)s commissions already exist(s) with start date %(date_start)s in company %(company)s.',
|
||||||
period=date_range.display_name, company=self.company_id.display_name))
|
count=len(existing_commissions),
|
||||||
|
date_start=format_date(self.env, self.date_start),
|
||||||
|
company=self.company_id.display_name))
|
||||||
com_result_ids = self._core_compute()
|
com_result_ids = self._core_compute()
|
||||||
if not com_result_ids:
|
if not com_result_ids:
|
||||||
raise UserError(_('No commissions generated.'))
|
raise UserError(_('No commissions generated.'))
|
||||||
@@ -68,8 +71,27 @@ class CommissionCompute(models.TransientModel):
|
|||||||
rules = self.env['commission.rule'].load_all_rules()
|
rules = self.env['commission.rule'].load_all_rules()
|
||||||
com_result_ids = []
|
com_result_ids = []
|
||||||
assignments = self.env['commission.profile.assignment'].search([('company_id', '=', self.company_id.id)])
|
assignments = self.env['commission.profile.assignment'].search([('company_id', '=', self.company_id.id)])
|
||||||
|
date_range_type2date_range = {}
|
||||||
for assignment in assignments:
|
for assignment in assignments:
|
||||||
com_result = assignment._generate_commission_result(self.date_range_id, rules)
|
profile = assignment.profile_id
|
||||||
|
date_range_type = profile.date_range_type_id
|
||||||
|
if not date_range_type:
|
||||||
|
raise UserError(_("Missing commission periodicity on commission profile '%s'.") % profile.display_name)
|
||||||
|
if date_range_type not in date_range_type2date_range:
|
||||||
|
domain = [
|
||||||
|
('date_start', '=', self.date_start),
|
||||||
|
('type_id', '=', date_range_type.id),
|
||||||
|
]
|
||||||
|
date_range = self.env['date.range'].search(
|
||||||
|
domain + [('company_id', '=', self.company_id.id)], limit=1)
|
||||||
|
if not date_range:
|
||||||
|
date_range = self.env['date.range'].search(
|
||||||
|
domain + [('company_id', '=', False)], limit=1)
|
||||||
|
if not date_range:
|
||||||
|
logger.info('There is no date range with type %s starting on %s. Skipping commission generation for assignment ID %s', date_range_type.name, self.date_start, assignment.id)
|
||||||
|
continue
|
||||||
|
date_range_type2date_range[date_range_type] = date_range
|
||||||
|
com_result = assignment._generate_commission_result(date_range_type2date_range[date_range_type], rules)
|
||||||
if com_result:
|
if com_result:
|
||||||
com_result_ids.append(com_result.id)
|
com_result_ids.append(com_result.id)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -15,10 +15,7 @@
|
|||||||
<group name="main">
|
<group name="main">
|
||||||
<field name="company_id" groups="base.group_multi_company"/>
|
<field name="company_id" groups="base.group_multi_company"/>
|
||||||
<field name="company_id" invisible="1"/>
|
<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_start"/>
|
||||||
<field name="date_end"/>
|
|
||||||
</group>
|
</group>
|
||||||
<footer>
|
<footer>
|
||||||
<button name="run" type="object" string="Compute"
|
<button name="run" type="object" string="Compute"
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
# 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)
|
|
||||||
@@ -11,17 +11,25 @@
|
|||||||
<record id="res_config_settings_view_form" model="ir.ui.view">
|
<record id="res_config_settings_view_form" model="ir.ui.view">
|
||||||
<field name="name">commission.res.config.settings.form</field>
|
<field name="name">commission.res.config.settings.form</field>
|
||||||
<field name="model">res.config.settings</field>
|
<field name="model">res.config.settings</field>
|
||||||
<field name="inherit_id" ref="commission_simple.res_config_settings_view_form" />
|
<field name="inherit_id" ref="account.res_config_settings_view_form" />
|
||||||
<field name="arch" type="xml">
|
<field name="arch" type="xml">
|
||||||
<xpath expr="//div[@id='commission_simple-settings']/div[hasclass('o_setting_right_pane')]" position="inside">
|
<xpath expr="//div[@id='analytic']" position="after">
|
||||||
|
<h2>Commissions</h2>
|
||||||
|
<div class="row mt16 o_settings_container" id="commission_simple">
|
||||||
|
<div class="col-12 col-lg-12 o_setting_box" id="commission_simple-settings">
|
||||||
|
<div class="o_setting_left_pane" />
|
||||||
|
<div class="o_setting_right_pane">
|
||||||
<div class="row" id="commission_product_id">
|
<div class="row" id="commission_product_id">
|
||||||
<label for="commission_product_id" class="col-md-5" />
|
<label for="commission_product_id" class="col-md-5" />
|
||||||
<field name="commission_product_id" context="{'default_detailed_type': 'service', 'default_purchase_ok': True, 'default_sale_ok': False, 'default_available_in_pos': False, 'default_purchase_method': 'purchase'}"/>
|
<field name="commission_product_id" context="{'default_type': 'service', 'default_purchase_ok': True, 'default_sale_ok': False, 'default_available_in_pos': False, 'default_purchase_method': 'purchase'}"/>
|
||||||
</div>
|
</div>
|
||||||
<div class="row" id="commission_po_config">
|
<div class="row" id="commission_po_config">
|
||||||
<label for="commission_po_config" class="col-md-5" string="Purchase Order"/>
|
<label for="commission_po_config" class="col-md-5" string="Purchase Order"/>
|
||||||
<field name="commission_po_config" />
|
<field name="commission_po_config" />
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</xpath>
|
</xpath>
|
||||||
</field>
|
</field>
|
||||||
</record>
|
</record>
|
||||||
|
|||||||
Reference in New Issue
Block a user