-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmove.py
91 lines (77 loc) · 3.44 KB
/
move.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
# This file is part of the account_voucher_ar module for Tryton.
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
from decimal import Decimal
from trytond.model import fields
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction
class Move(metaclass=PoolMeta):
__name__ = 'account.move'
@classmethod
def _get_origin(cls):
return super()._get_origin() + ['account.voucher']
class Line(metaclass=PoolMeta):
__name__ = 'account.move.line'
amount_residual = fields.Function(fields.Numeric('Amount Residual',
digits=(16, 2)), 'get_amount_residual')
amount_residual_second_currency = fields.Function(fields.Numeric(
'Amount Residual Second Currency', digits=(16, 2)),
'get_amount_residual_second_currency')
voucher_payments = fields.One2Many('account.voucher.line', 'move_line',
'Voucher Payments', readonly=True)
@classmethod
def get_amount_residual(cls, lines, name):
pool = Pool()
Currency = pool.get('currency.currency')
amounts = {}
for line in lines:
if line.reconciliation or not (
line.account.type.payable or line.account.type.receivable):
amounts[line.id] = Decimal(0)
continue
amount = abs(line.credit - line.debit)
for payment in line.voucher_payments:
voucher = payment.voucher
if voucher and voucher.state == 'posted':
if voucher.currency != voucher.company.currency:
with Transaction().set_context(
currency_rate=voucher.currency_rate,
date=voucher.date):
amount -= Currency.compute(voucher.currency,
payment.amount, voucher.company.currency)
else:
amount -= payment.amount
amounts[line.id] = amount
return amounts
@classmethod
def get_amount_residual_second_currency(cls, lines, name):
pool = Pool()
Currency = pool.get('currency.currency')
amounts = {}
for line in lines:
if not line.amount_second_currency or line.reconciliation or not (
line.account.type.payable or line.account.type.receivable):
amounts[line.id] = Decimal(0)
continue
amount = abs(line.amount_second_currency)
for payment in line.voucher_payments:
voucher = payment.voucher
if voucher and voucher.state == 'posted':
if voucher.currency != voucher.company.currency:
amount -= payment.amount
else:
with Transaction().set_context(
currency_rate=voucher.currency_rate,
date=voucher.date):
amount -= Currency.compute(voucher.currency,
payment.amount, voucher.company.currency)
amounts[line.id] = amount
return amounts
@classmethod
def copy(cls, lines, default=None):
if default is None:
default = {}
else:
default = default.copy()
default.setdefault('voucher_payments', [])
return super().copy(lines, default=default)