-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecibo.py
155 lines (132 loc) · 4.86 KB
/
recibo.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# This file is part of the cooperative_cashflow_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 simpleeval import simple_eval
from trytond.model import Workflow, ModelView, fields
from trytond.model.exceptions import ValidationError
from trytond.wizard import Wizard, StateView, StateTransition, Button
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
from trytond.i18n import gettext
from trytond.tools import decistmt
class Recibo(metaclass=PoolMeta):
__name__ = 'cooperative.partner.recibo'
@classmethod
def __setup__(cls):
super().__setup__()
state = ('projected', 'Projected')
if state not in cls.state.selection:
cls.state.selection.append(state)
cls._transitions |= set((
('draft', 'projected'),
('projected', 'draft'),
))
cls._buttons['draft']['invisible'] = ~Eval('state').in_(
['projected', 'cancelled'])
cls._buttons.update({
'project': {
'invisible': Eval('state') != 'draft',
'depends': ['state'],
},
})
@classmethod
@ModelView.button
@Workflow.transition('projected')
def project(cls, recibos):
pass
class ReciboLote(metaclass=PoolMeta):
__name__ = 'cooperative.partner.recibo.lote'
@classmethod
def __setup__(cls):
super().__setup__()
state = ('projected', 'Projected')
if state not in cls.state.selection:
cls.state.selection.append(state)
cls._transitions |= set((
('draft', 'projected'),
('projected', 'draft'),
))
cls._buttons['draft']['invisible'] = ~Eval('state').in_(
['projected', 'cancelled'])
cls._buttons.update({
'project': {
'invisible': Eval('state') != 'draft',
'depends': ['state'],
},
})
@classmethod
@ModelView.button
@Workflow.transition('projected')
def project(cls, lotes):
pool = Pool()
Recibo = pool.get('cooperative.partner.recibo')
for lote in lotes:
Recibo.project(lote.recibos)
@classmethod
@ModelView.button
@Workflow.transition('draft')
def draft(cls, lotes):
pool = Pool()
Recibo = pool.get('cooperative.partner.recibo')
super().draft(lotes)
for lote in lotes:
Recibo.draft(lote.recibos)
class UpdateReciboLoteProjectionStart(ModelView):
'Update Recibo Projection'
__name__ = 'cooperative.lote.update_projection.start'
formula = fields.Char('Unit Price Formula', required=True,
help=('Python expression that will be evaluated with:\n'
'- amount: the current amount of each receipt'))
@classmethod
def default_formula(cls):
return 'amount'
class UpdateReciboLoteProjection(Wizard):
'Update Recibo Projection'
__name__ = 'cooperative.lote.update_projection'
start_state = 'check'
check = StateTransition()
start = StateView('cooperative.lote.update_projection.start',
'cooperative_cashflow_ar.lote_update_projection_start_view', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Update', 'update', 'tryton-ok', True),
])
update = StateTransition()
def transition_check(self):
sale = self.record
if sale.state == 'projected':
return 'start'
return 'end'
def transition_update(self):
self.check_formula()
self.update_amount()
return 'end'
def check_formula(self):
try:
if not isinstance(self.get_amount(Decimal(0)), Decimal):
raise ValueError
except Exception as exception:
raise ValidationError(gettext(
'cooperative_cashflow_ar.msg_invalid_formula',
formula=self.start.formula,
exception=exception)) from exception
def get_amount(self, amount, **context):
context.setdefault('names', {})['amount'] = amount
context.setdefault('functions', {})['Decimal'] = Decimal
return simple_eval(decistmt(self.start.formula), **context)
def update_amount(self):
pool = Pool()
Recibo = pool.get('cooperative.partner.recibo')
for lote in self.records:
if lote.state != 'projected':
continue
recibos = []
for recibo in lote.recibos:
if recibo.state != 'projected':
continue
amount = self.get_amount(recibo.amount).quantize(
Decimal(1) / 10 ** 2)
recibo.amount = amount
recibos.append(recibo)
if recibos:
Recibo.save(recibos)