Files
tryton/modules/commission/sale.py
T

109 lines
3.6 KiB
Python
Raw Normal View History

2014-12-19 12:14:20 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
2018-06-26 19:11:46 +02:00
2014-12-19 12:14:20 +01:00
2020-02-24 18:38:48 +01:00
from trytond.model import ModelView, Workflow, fields
2021-12-11 17:58:42 +01:00
from trytond.pool import Pool, PoolMeta
2014-12-19 12:14:20 +01:00
from trytond.pyson import Eval
2020-06-07 12:24:55 +02:00
from trytond.transaction import Transaction
2014-12-19 12:14:20 +01:00
2018-06-26 19:11:46 +02:00
class Sale(metaclass=PoolMeta):
2014-12-19 12:14:20 +01:00
__name__ = 'sale.sale'
agent = fields.Many2One('commission.agent', 'Commission Agent',
domain=[
('type_', '=', 'agent'),
2014-12-19 12:14:20 +01:00
('company', '=', Eval('company', -1)),
],
states={
2020-02-24 18:38:48 +01:00
'readonly': ~Eval('state').in_(['draft', 'quotation']),
2014-12-19 12:14:20 +01:00
},
2020-04-13 17:35:36 +02:00
help="The agent who receives a commission for the sale.")
2014-12-19 12:14:20 +01:00
2016-03-20 22:58:54 +01:00
def create_invoice(self):
invoice = super(Sale, self).create_invoice()
2014-12-19 12:14:20 +01:00
if invoice:
invoice.agent = self.agent
invoice.save()
return invoice
2020-02-24 18:38:48 +01:00
@classmethod
@ModelView.button
@Workflow.transition('quotation')
def quote(cls, sales):
cls.set_agent(sales)
super().quote(sales)
def _get_agent_pattern(self):
pattern = {}
pattern['date'] = self.sale_date
pattern['company'] = self.company.id
pattern['party'] = self.party.id
2020-06-07 12:24:55 +02:00
if self.quoted_by:
pattern['employee'] = self.quoted_by.id
else:
pattern['employee'] = Transaction().context.get('employee')
2020-02-24 18:38:48 +01:00
return pattern
@classmethod
def _get_agent_selection_domain(cls, pattern):
domain = []
if 'company' in pattern:
domain.append(('agent.company', '=', pattern['company']))
if 'party' in pattern:
domain.append(('party', 'in', [None, pattern['party']]))
2020-06-07 12:24:55 +02:00
if 'employee' in pattern:
domain.append(('employee', 'in', [None, pattern['employee']]))
2020-02-24 18:38:48 +01:00
return domain
@classmethod
def set_agent(cls, sales):
pool = Pool()
AgentSelection = pool.get('commission.agent.selection')
for sale in sales:
if sale.agent:
continue
pattern = sale._get_agent_pattern()
for selection in AgentSelection.search(
cls._get_agent_selection_domain(pattern)):
if selection.match(pattern):
sale.agent = selection.agent
break
cls.save(sales)
2014-12-19 12:14:20 +01:00
2020-03-01 12:33:52 +01:00
class Line(metaclass=PoolMeta):
2014-12-19 12:14:20 +01:00
__name__ = 'sale.line'
principal = fields.Many2One('commission.agent', 'Commission Principal',
domain=[
('type_', '=', 'principal'),
('company', '=', Eval('_parent_sale', {}).get('company', -1)),
2020-04-13 17:35:36 +02:00
],
depends={'sale'},
2020-04-13 17:35:36 +02:00
help="The principal who pays a commission for the line.")
2014-12-19 12:14:20 +01:00
2016-03-20 22:58:54 +01:00
def get_invoice_line(self):
2020-03-01 12:33:52 +01:00
lines = super().get_invoice_line()
2014-12-19 12:14:20 +01:00
if self.principal:
for line in lines:
if line.product == self.product:
line.principal = self.principal
return lines
@fields.depends('product', 'principal')
def on_change_product(self):
2020-03-01 12:33:52 +01:00
super().on_change_product()
2014-12-19 12:14:20 +01:00
if self.product:
if self.product.principals:
if self.principal not in self.product.principals:
self.principal = self.product.principal
2014-12-19 12:14:20 +01:00
elif self.principal:
self.principal = None
2015-03-09 19:41:36 +01:00
@classmethod
def view_attributes(cls):
2020-03-01 12:33:52 +01:00
return super().view_attributes() + [
2015-03-09 19:41:36 +01:00
('//page[@id="commissions"]', 'states', {
'invisible': Eval('type') != 'line',
})]