[IMP] hr_commission: margin threshold and no commission products

H11044
This commit is contained in:
galeshibou
2022-09-29 02:06:14 +00:00
parent d957c043bb
commit a0924cc036
9 changed files with 242 additions and 8 deletions

View File

@@ -4,5 +4,6 @@ from . import account
from . import commission
from . import hr
from . import partner
from . import product_template
from . import res_company
from . import res_config_settings

View File

@@ -37,15 +37,45 @@ class AccountMove(models.Model):
return res
def amount_for_commission(self, commission=None):
if hasattr(self, 'margin') and self.company_id.commission_amount_type == 'on_invoice_margin':
sign = -1 if self.move_type in ['in_refund', 'out_refund'] else 1
return self.margin * sign
elif self.company_id.commission_amount_type == 'on_invoice_untaxed':
return self.amount_untaxed_signed
return self.amount_total_signed
# Override to exclude ineligible products
amount = 0.0
if self.is_invoice():
invoice_lines = self.invoice_line_ids.filtered(lambda l: not l.product_id.no_commission)
if self.company_id.commission_amount_type == 'on_invoice_margin':
margin_threshold = float(self.env['ir.config_parameter'].sudo().get_param('commission.margin.threshold', default=0.0))
if margin_threshold:
invoice_lines = invoice_lines.filtered(lambda l: l.margin_percent > margin_threshold)
sign = -1 if self.move_type in ['in_refund', 'out_refund'] else 1
margin = sum(invoice_lines.mapped('margin'))
amount = margin * sign
else:
amount = sum(invoice_lines.mapped('balance'))
amount = abs(amount) if self.move_type == 'entry' else -amount
return amount
def action_cancel(self):
res = super(AccountMove, self).action_cancel()
for move in self:
move.sudo().commission_ids.unlink()
return res
class AccountMoveLine(models.Model):
_inherit = 'account.move.line'
margin_percent = fields.Float(string='Margin percent (%)', compute='_compute_margin_percent', digits=(3, 2))
@api.depends('margin', 'product_id', 'purchase_price', 'quantity', 'price_unit', 'price_subtotal')
def _compute_margin_percent(self):
for line in self:
currency = line.move_id.currency_id
price = line.purchase_price
if line.product_id and not price:
date = line.move_id.date if line.move_id.date else fields.Date.context_today(line.move_id)
from_cur = line.move_id.company_currency_id.with_context(date=date)
price = from_cur._convert(line.product_id.standard_price, currency, line.company_id, date, round=False)
total_price = price * line.quantity
if total_price == 0.0:
line.margin_percent = -1.0
else:
line.margin_percent = (line.margin / total_price) * 100.0

View File

@@ -0,0 +1,13 @@
from odoo import fields, models
class ProductTemplate(models.Model):
_inherit = 'product.template'
no_commission = fields.Boolean('Exclude from Commissions')
can_edit_no_commission = fields.Boolean(compute='_compute_can_edit_no_commission')
def _compute_can_edit_no_commission(self):
can_edit = self.env.user.has_group('account.group_account_user')
for template in self:
template.can_edit_no_commission = can_edit

View File

@@ -1,6 +1,6 @@
# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details.
from odoo import fields, models
from odoo import api, fields, models
class ResConfigSettings(models.TransientModel):
@@ -10,3 +10,15 @@ class ResConfigSettings(models.TransientModel):
commission_liability_id = fields.Many2one(related='company_id.commission_liability_id', readonly=False)
commission_type = fields.Selection(related='company_id.commission_type', readonly=False)
commission_amount_type = fields.Selection(related='company_id.commission_amount_type', readonly=False)
commission_margin_threshold = fields.Float()
@api.model
def get_values(self):
res = super(ResConfigSettings, self).get_values()
margin_threshold = float(self.env['ir.config_parameter'].sudo().get_param('commission.margin.threshold', default=0.0))
res.update(commission_margin_threshold=margin_threshold)
return res
def set_values(self):
super(ResConfigSettings, self).set_values()
self.env['ir.config_parameter'].sudo().set_param('commission.margin.threshold', self.commission_margin_threshold)