-
Notifications
You must be signed in to change notification settings - Fork 6
/
analytic_account.py
307 lines (258 loc) · 10 KB
/
analytic_account.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# This file is part of the cooperative_ar module for Tryton.
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
from dateutil.relativedelta import relativedelta
from datetime import datetime, date
from trytond.model import ModelView, ModelSQL, fields
from trytond.wizard import Wizard, StateView, StateAction, Button
from trytond.report import Report
from trytond.pool import Pool
from trytond.transaction import Transaction
class AnalyticAccountNota(ModelSQL, ModelView):
'Notes Analytic Account'
__name__ = 'analytic_account.account.nota'
name = fields.Char('Name')
date = fields.Date('Date')
note = fields.Text('Note')
account = fields.Many2One('analytic_account.account', 'Account',
required=True, select=True, domain=[
('root.name', '=', 'Balance Social Cooperativo'),
('type', '=', 'normal'),
])
type = fields.Selection([
('line', 'Line'),
('subtotal', 'Subtotal'),
('title', 'Title'),
('comment', 'Comment'),
], 'Type', select=True, required=True)
@staticmethod
def default_type():
return 'line'
class PrintBalanceSocialStart(ModelView):
'Print Balance Social'
__name__ = 'analytic_account.print_balance_social.start'
from_date = fields.Date('From Date', required=True)
to_date = fields.Date('To Date', required=True)
company = fields.Many2One('company.company', 'Company', required=True)
@staticmethod
def default_from_date():
Date = Pool().get('ir.date')
return date(Date.today().year, 1, 1)
@staticmethod
def default_to_date():
Date = Pool().get('ir.date')
return Date.today()
@staticmethod
def default_company():
return Transaction().context.get('company')
class PrintBalanceSocial(Wizard):
'Print Balance Social'
__name__ = 'analytic_account.print_balance_social'
start = StateView('analytic_account.print_balance_social.start',
'cooperative_ar.print_balance_social_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Print', 'print_', 'tryton-print', default=True),
])
print_ = StateAction('cooperative_ar.report_balance_social')
def do_print_(self, action):
data = {
'company': self.start.company.id,
'from_date': self.start.from_date,
'to_date': self.start.to_date,
}
return action, data
def transition_print_(self):
return 'end'
class BalanceSocial(Report):
__name__ = 'cooperative_ar.report_balance_social'
@classmethod
def _get_records(cls, ids, model, data):
pool = Pool()
AnalyticAccount = pool.get('analytic_account.account')
accounts = AnalyticAccount.search([
('type', '=', 'normal'),
('root.name', '=', 'Balance Social Cooperativo'),
])
return accounts
@classmethod
def get_context(cls, records, header, data):
report_context = super().get_context(records, header, data)
company = report_context['user'].company
report_context['company'] = company
report_context['digits'] = company.currency.digits
report_context['from_date'] = data['from_date']
report_context['to_date'] = data['to_date']
report_context['get_analytic_lines'] = cls.get_analytic_lines
report_context['has_analytic_lines'] = cls.has_analytic_lines
report_context['get_partners'] = cls.get_partners
report_context['get_partners_nuevos'] = cls.get_partners_nuevos
report_context['get_partners_leave'] = cls.get_partners_leave
report_context['get_partners_gender'] = cls.get_partners_gender
report_context['get_meetings'] = cls.get_meetings
report_context['get_notas'] = cls.get_notas
report_context['has_notas'] = cls.has_notas
report_context['get_analytic_accounts'] = cls.get_analytic_accounts
report_context['get_analytic_subaccounts'] = \
cls.get_analytic_subaccounts
report_context['get_partners_birthdate'] = cls.get_partners_birthdate
report_context['get_birthdate'] = cls.get_birthdate
report_context['get_meeting_type'] = cls.get_meeting_type
report_context['get_summary_meeting'] = cls.get_summary_meeting
return report_context
@classmethod
def get_analytic_accounts(self, company):
pool = Pool()
AnalyticAccount = pool.get('analytic_account.account')
accounts = AnalyticAccount.search([
('root.name', '=', 'Balance Social Cooperativo'),
('type', '=', 'view'),
])
return accounts
@classmethod
def get_analytic_subaccounts(self, account_id):
pool = Pool()
AnalyticAccount = pool.get('analytic_account.account')
accounts = AnalyticAccount.search([
('root.name', '=', 'Balance Social Cooperativo'),
('type', '=', 'normal'),
('parent', '=', account_id),
])
return accounts
@classmethod
def get_notas(self, analytic_account_id, from_date, to_date):
pool = Pool()
AnalyticAccountNota = pool.get('analytic_account.account.nota')
clause = [
('date', '>=', from_date),
('date', '<=', to_date),
('account', '=', analytic_account_id),
]
notas = AnalyticAccountNota.search(clause,
order=[('date', 'ASC'), ('id', 'ASC')])
return notas
@classmethod
def has_notas(self, analytic_account_id, from_date, to_date):
pool = Pool()
AnalyticAccountNota = pool.get('analytic_account.account.nota')
clause = [
('date', '>=', from_date),
('date', '<=', to_date),
('account', '=', analytic_account_id),
]
notas = AnalyticAccountNota.search(clause,
order=[('date', 'ASC'), ('id', 'ASC')])
if not notas:
return False
return True
@classmethod
def get_summary_meeting(self, from_date, to_date, type):
pool = Pool()
Meeting = pool.get('cooperative.meeting')
clause = [
('start_date', '>=', from_date),
('start_date', '<=', to_date),
('status', '=', 'complete'),
('type', '=', type),
]
return len(Meeting.search(clause))
@classmethod
def get_meetings(self, from_date, to_date):
pool = Pool()
Meeting = pool.get('cooperative.meeting')
clause = [
('start_date', '>=', from_date),
('start_date', '<=', to_date),
('status', '=', 'complete'),
]
meetings = Meeting.search(clause,
order=[('start_date', 'ASC'), ('type', 'ASC'), ('id', 'ASC')])
return meetings
@classmethod
def get_meeting_type(self, type):
pool = Pool()
Meeting = pool.get('cooperative.meeting')
return dict(Meeting._fields['type'].selection)[type]
@classmethod
def get_birthdate(self, partner):
today = datetime.now()
age = relativedelta(today, partner.birthdate) # calculate age
return age.years
@classmethod
def get_partners_birthdate(self):
pool = Pool()
CooperativePartner = pool.get('cooperative.partner')
clause = [
('status', '=', 'active'),
]
partners = CooperativePartner.search(clause,
order=[('last_name', 'ASC'), ('id', 'ASC')])
return partners
@classmethod
def get_partners(self):
pool = Pool()
CooperativePartner = pool.get('cooperative.partner')
clause = [
('status', '=', 'active'),
]
partners = CooperativePartner.search(clause,
order=[('last_name', 'ASC'), ('id', 'ASC')])
return partners
@classmethod
def get_partners_leave(self, from_date, to_date):
pool = Pool()
CooperativePartner = pool.get('cooperative.partner')
clause = [
('leaving_date', '>=', from_date),
('leaving_date', '<=', to_date),
('status', '=', 'give_up'),
]
partners = CooperativePartner.search(clause,
order=[('last_name', 'ASC'), ('id', 'ASC')])
return partners
@classmethod
def get_partners_nuevos(self, from_date, to_date):
pool = Pool()
CooperativePartner = pool.get('cooperative.partner')
clause = [
('incorporation_date', '>=', from_date),
('incorporation_date', '<=', to_date),
('status', '=', 'active'),
]
partners = CooperativePartner.search(clause,
order=[('last_name', 'ASC'), ('id', 'ASC')])
return partners
@classmethod
def get_partners_gender(self, from_date, to_date, gender):
pool = Pool()
CooperativePartner = pool.get('cooperative.partner')
clause = [
('status', '=', 'active'),
('gender', '=', gender),
]
return len(CooperativePartner.search(clause))
@classmethod
def get_analytic_lines(self, analytic_account_id, from_date, to_date):
pool = Pool()
AnalyticAccountLine = pool.get('analytic_account.line')
clause = [
('date', '>=', from_date),
('date', '<=', to_date),
('account', '=', analytic_account_id),
]
lines = AnalyticAccountLine.search(clause,
order=[('date', 'ASC'), ('id', 'ASC')])
return lines
@classmethod
def has_analytic_lines(self, analytic_account_id, from_date, to_date):
pool = Pool()
AnalyticAccountLine = pool.get('analytic_account.line')
clause = [
('date', '>=', from_date),
('date', '<=', to_date),
('account', '=', analytic_account_id),
]
lines = AnalyticAccountLine.search(clause,
order=[('date', 'ASC'), ('id', 'ASC')])
if not lines:
return False
return True