Files
tryton/modules/commission_waiting/commission.py
T

98 lines
3.4 KiB
Python
Raw Normal View History

2014-12-19 12:24:22 +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
from trytond.model import fields
2021-12-11 17:58:42 +01:00
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Bool, Eval
2018-08-31 14:58:05 +02:00
from trytond.transaction import Transaction
2014-12-19 12:24:22 +01:00
2018-06-26 19:11:46 +02:00
class Agent(metaclass=PoolMeta):
2014-12-19 12:24:22 +01:00
__name__ = 'commission.agent'
waiting_account = fields.Many2One('account.account', 'Waiting Account',
domain=[
2019-12-28 17:48:50 +01:00
('type', '!=', None),
('closed', '!=', True),
('company', '=', Eval('company', -1)),
2014-12-19 12:24:22 +01:00
],
2020-04-13 17:37:23 +02:00
help="The account the agent's waiting commission amounts are posted "
"to.")
2014-12-19 12:24:22 +01:00
2018-06-26 19:11:46 +02:00
class Commission(metaclass=PoolMeta):
2014-12-19 12:24:22 +01:00
__name__ = 'commission'
2020-04-13 17:37:23 +02:00
waiting_move = fields.Many2One(
'account.move', "Waiting Move", readonly=True)
2014-12-19 12:24:22 +01:00
@classmethod
def __setup__(cls):
2025-04-04 00:45:18 +02:00
super().__setup__()
2014-12-19 12:24:22 +01:00
cls.amount.states['readonly'] |= Bool(Eval('waiting_move'))
@classmethod
def copy(cls, commissions, default=None):
if default is None:
default = {}
2018-09-01 01:02:03 +02:00
else:
default = default.copy()
2014-12-19 12:24:22 +01:00
default.setdefault('waiting_move', None)
2025-04-04 00:45:18 +02:00
return super().copy(commissions, default=default)
2014-12-19 12:24:22 +01:00
@classmethod
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, date=date)
2014-12-19 12:24:22 +01:00
move = Move(journal=self.get_journal(), origin=self,
2021-12-04 21:20:55 +01:00
date=date, period=period, company=self.agent.company)
2014-12-19 12:24:22 +01:00
amount = Currency.compute(self.currency, self.amount,
self.agent.company.currency)
line = Line()
2018-08-31 14:58:05 +02:00
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
2014-12-19 12:24:22 +01:00
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