This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
commission.py
98 lines (85 loc) · 3.42 KB
/
commission.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond.model import ModelView, fields
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Bool, Eval
from trytond.transaction import Transaction
class Agent(metaclass=PoolMeta):
__name__ = 'commission.agent'
waiting_account = fields.Many2One('account.account', 'Waiting Account',
domain=[
('type', '!=', None),
('closed', '!=', True),
('company', '=', Eval('company')),
],
help="The account the agent's waiting commission amounts are posted "
"to.")
class Commission(metaclass=PoolMeta):
__name__ = 'commission'
waiting_move = fields.Many2One(
'account.move', "Waiting Move", readonly=True)
@classmethod
def __setup__(cls):
super(Commission, cls).__setup__()
cls.amount.states['readonly'] |= Bool(Eval('waiting_move'))
@classmethod
def copy(cls, commissions, default=None):
if default is None:
default = {}
else:
default = default.copy()
default.setdefault('waiting_move', None)
return super(Commission, cls).copy(commissions, default=default)
@classmethod
@ModelView.button
def create_waiting_move(cls, commissions):
pool = Pool()
Move = pool.get('account.move')
Commission = pool.get('commission')
moves = []
for commission in commissions:
move = commission.get_move()
if move:
moves.append(move)
commission.waiting_move = move
Move.save(moves)
Commission.save(commissions)
def get_move(self, date=None):
pool = Pool()
Move = pool.get('account.move')
Line = pool.get('account.move.line')
Date = pool.get('ir.date')
Period = pool.get('account.period')
Currency = pool.get('currency.currency')
if not self.agent.waiting_account:
return
if self.waiting_move:
return self.waiting_move
if date is None:
with Transaction().set_context(company=self.agent.company.id):
date = Date.today()
period = Period.find(self.agent.company.id, date=date)
move = Move(journal=self.get_journal(), origin=self,
date=date, period=period, company=self.agent.company)
amount = Currency.compute(self.currency, self.amount,
self.agent.company.currency)
line = Line()
with Transaction().set_context(date=date):
if self.type_ == 'in':
line.credit = amount if amount > 0 else 0
line.debit = amount if amount < 0 else 0
line.account = self.product.account_revenue_used
else:
line.debit = amount if amount > 0 else 0
line.credit = amount if amount < 0 else 0
line.account = self.product.account_expense_used
if line.account.party_required:
line.party = self.agent.party
# XXX second currency?
counterpart = Line()
counterpart.debit, counterpart.credit = line.credit, line.debit
counterpart.account = self.agent.waiting_account
if counterpart.account.party_required:
counterpart.party = self.agent.party
move.lines = (line, counterpart)
return move