Files
tryton/modules/commission_waiting/invoice.py
T

68 lines
2.1 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
2014-12-19 12:24:22 +01:00
from collections import defaultdict
2021-12-11 17:58:42 +01:00
from trytond.pool import Pool, PoolMeta
2014-12-19 12:24:22 +01:00
2018-06-26 19:11:46 +02:00
class Invoice(metaclass=PoolMeta):
2014-12-19 12:24:22 +01:00
__name__ = 'account.invoice'
2015-01-03 11:32:45 +01:00
@classmethod
2021-02-27 15:23:46 +01:00
def _post(cls, invoices):
super()._post(invoices)
2015-01-03 11:32:45 +01:00
cls.post_commission_waiting_moves(invoices)
2014-12-19 12:24:22 +01:00
@classmethod
def create_commissions(cls, invoices):
pool = Pool()
Commission = pool.get('commission')
2025-04-04 00:45:18 +02:00
commissions = super().create_commissions(invoices)
2014-12-19 12:24:22 +01:00
Commission.create_waiting_move(commissions)
return commissions
2015-01-03 11:32:45 +01:00
@classmethod
def post_commission_waiting_moves(cls, invoices):
pool = Pool()
Move = pool.get('account.move')
moves = []
for invoice in invoices:
for line in invoice.lines:
for commission in line.from_commissions:
if (commission.waiting_move
and commission.waiting_move.state != 'posted'):
moves.append(commission.waiting_move)
if moves:
Move.post(moves)
2014-12-19 12:24:22 +01:00
2018-06-26 19:11:46 +02:00
class InvoiceLine(metaclass=PoolMeta):
2014-12-19 12:24:22 +01:00
__name__ = 'account.invoice.line'
def get_move_lines(self):
pool = Pool()
MoveLine = pool.get('account.move.line')
2025-04-04 00:45:18 +02:00
lines = super().get_move_lines()
2014-12-19 12:24:22 +01:00
if self.from_commissions:
2018-10-31 18:17:35 +01:00
amounts = defaultdict(int)
2014-12-19 12:24:22 +01:00
for commission in self.from_commissions:
if not commission.waiting_move:
continue
for line in commission.waiting_move.lines:
amounts[(line.account, line.party)] += (
line.debit - line.credit)
2018-06-26 19:11:46 +02:00
for (account, party), amount in amounts.items():
line = MoveLine()
line.debit = -amount if amount < 0 else 0
line.credit = amount if amount > 0 else 0
line.account = account
line.party = party
line.amount_second_currency = None
lines.append(line)
2014-12-19 12:24:22 +01:00
return lines