-
Notifications
You must be signed in to change notification settings - Fork 4
/
giscedata_polissa.py
270 lines (224 loc) · 9.94 KB
/
giscedata_polissa.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
# -*- coding: utf-8 -*-
from osv import osv, fields
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from tools.translate import _
from tools import email_send
class GiscedataPolissaInfoenergia(osv.osv):
"""
Pòlissa per afegir els camps relacionats amb infoenergia
"""
_name = "giscedata.polissa"
_inherit = "giscedata.polissa"
MINIM_DIES_CONSUM = 122
def write(self, cursor, uid, ids, vals, context=None):
if not isinstance(ids, (tuple, list)):
ids = [ids]
if "emp_allow_recieve_mail_infoenergia" in vals:
profile_obj = self.pool.get("empowering.customize.profile")
profile_id = None
if vals["emp_allow_recieve_mail_infoenergia"]:
profile_id = profile_obj.search(cursor, uid, [("name", "=", "Default profile")])[0]
vals["empowering_profile_id"] = profile_id
if "emp_allow_send_data" in vals:
if not vals["emp_allow_send_data"]:
vals["emp_allow_recieve_mail_infoenergia"] = False
res = super(GiscedataPolissaInfoenergia, self).write(cursor, uid, ids, vals, context)
return res
def get_consum_anual_consum_lectures(self, cursor, uid, polissa_id, context=None):
"""Calculem el consum anual a partir del consum de les lectures"""
if isinstance(polissa_id, (tuple, list)):
polissa_id = polissa_id[0]
lectures_obj = self.pool.get("giscedata.lectures.lectura")
limit_date = (datetime.today() - timedelta(self.MINIM_DIES_CONSUM)).strftime("%Y-%m-%d")
from_date = (datetime.today() - relativedelta(months=14)).strftime("%Y-%m-%d")
data_ultima_lectura = self.read(cursor, uid, polissa_id, ["data_ultima_lectura"])[
"data_ultima_lectura"
]
search_params = [
("comptador.polissa", "=", polissa_id),
("name", ">", from_date),
("name", "<=", data_ultima_lectura),
("tipus", "=", "A"),
("consum", "<", 989999),
]
lect_ids = lectures_obj.search(cursor, uid, search_params, context={"active_test": False})
lect_info = lectures_obj.read(cursor, uid, lect_ids, ["consum", "name"])
lect_info.sort(key=lambda x: x["name"])
if not lect_ids or lect_info[0]["name"] > limit_date or lect_info[-1]["name"] < limit_date:
return False
consum = sum([x["consum"] for x in lect_info])
n_dies = datetime.strptime(lect_info[-1]["name"], "%Y-%m-%d") - datetime.strptime(
lect_info[0]["name"], "%Y-%m-%d"
)
n_dies = n_dies.days
if n_dies < self.MINIM_DIES_CONSUM:
return False
consum_anual = consum * 365 / n_dies
if consum_anual < 0 or consum_anual > 10000000:
return False
return consum_anual
def get_consum_anual_factures(self, cursor, uid, polissa_id, context=None):
"""Calculem el consum anual a partir del consum de les lectures"""
if isinstance(polissa_id, (tuple, list)):
polissa_id = polissa_id[0]
fact_obj = self.pool.get("giscedata.facturacio.factura")
search_params = [
("polissa_id", "=", polissa_id),
("state", "!=", "draft"),
("type", "in", ["out_invoice", "out_refund"]),
]
f_id = fact_obj.search(cursor, uid, search_params, limit=1, order="id desc")
if not f_id:
return False
data_final = fact_obj.read(cursor, uid, f_id[0], ["data_final"])["data_final"]
today = datetime.strptime(data_final, "%Y-%m-%d")
from_date = (today - relativedelta(months=12, days=15)).strftime("%Y-%m-%d")
search_params = [
("polissa_id", "=", polissa_id),
("state", "!=", "draft"),
("data_inici", ">=", from_date),
("type", "in", ["out_invoice", "out_refund"]),
]
fact_ids = fact_obj.search(cursor, uid, search_params)
if not fact_ids:
return False
fact_infos = fact_obj.read(cursor, uid, fact_ids, ["energia_kwh", "type"])
consum_anual = 0
for fact_info in fact_infos:
if fact_info["type"] == "out_invoice":
consum_anual += fact_info["energia_kwh"]
else:
consum_anual -= fact_info["energia_kwh"]
if consum_anual < 0 or consum_anual > 10000000:
return False
return consum_anual
def get_consum_anual_pdf(self, cursor, uid, polissa_id, context=None):
"""Calculem el consum anual a partir del consum de la factura en pdf"""
if isinstance(polissa_id, (tuple, list)):
polissa_id = polissa_id[0]
fact_obj = self.pool.get("giscedata.facturacio.factura")
rprt_obj = self.pool.get("giscedata.facturacio.factura.report")
ids = fact_obj.search(
cursor,
uid,
[
("polissa_id", "=", polissa_id),
("state", "!=", "draft"),
("type", "in", ["out_refund", "out_invoice"]),
],
limit=1,
order="id DESC",
)
if not ids:
return False
try:
data = rprt_obj.get_conany_kwh_energy_consumption_graphic_td_data(cursor, uid, ids[0])
except Exception:
return False
limit_days = eval(self.pool.get("res.config").get(
cursor, uid, "som_conany_pdf_days_limit", "[355,375]"
))
min_days = min(limit_days)
max_days = max(limit_days)
if min_days > data['days'] or data['days'] > max_days:
return False
data.pop('days')
data.pop('consum')
return data
def _conany_updater(self, cursor, uid, context=None):
cups_obj = self.pool.get("giscedata.cups.ps")
msg = []
msg.append(str(datetime.today()))
msg.append("Cercant polisses a updatar")
domain = [("state", "=", "activa"), ("tarifa_codi", "like", "2%")]
contracts2_ids = self.search(cursor, uid, domain)
msg.append("Trobades {} polisses 2.X".format(len(contracts2_ids)))
domain = [("state", "=", "activa"), ("tarifa_codi", "like", "3%")]
contracts3_ids = self.search(cursor, uid, domain)
msg.append("Trobades {} polisses 3.X".format(len(contracts3_ids)))
domain = [("state", "=", "activa"), ("tarifa_codi", "like", "6%")]
contracts6_ids = self.search(cursor, uid, domain)
msg.append("Trobades {} polisses 6.X".format(len(contracts6_ids)))
contracts_ids = contracts3_ids + contracts6_ids
msg.append("Trobades {} polisses candidates".format(len(contracts_ids)))
failed = []
company_contracts_cups_id = []
for contract in self.read(cursor, uid, contracts_ids, ["id", "cups"]):
try:
company_contracts_cups_id.append(contract["cups"][0])
except Exception as e:
failed.append(("contract read", contract["id"], str(e)))
cups_search = [
("id", "in", company_contracts_cups_id),
"|",
("conany_origen", "!=", "manual"),
("conany_origen", "=", False),
]
toupdate_cups_id = cups_obj.search(cursor, uid, cups_search)
domestic_contracts_cups_id = []
for contract in self.read(cursor, uid, contracts2_ids, ["id", "cups"]):
try:
domestic_contracts_cups_id.append(contract["cups"][0])
except Exception as e:
failed.append(("contract read", contract["id"], str(e)))
thirtyfive_days_ago = (datetime.now() - timedelta(days=35)).strftime("%Y-%m-%d")
cups_search = [
("id", "in", domestic_contracts_cups_id),
"|",
("conany_data", "=", False),
("conany_data", "<", thirtyfive_days_ago),
"|",
("conany_origen", "=", False),
("conany_origen", "!=", "manual"),
]
toupdate_cups_id = toupdate_cups_id + cups_obj.search(cursor, uid, cups_search)
msg.append("Trobats {} cups a updatar".format(len(toupdate_cups_id)))
cups_updated = 0
for cups_id in toupdate_cups_id:
try:
cups_obj.omple_consum_anual_periods(cursor, uid, cups_id, periods=True)
cups_updated += 1
except Exception as e:
failed.append(("cups update", cups_id, str(e)))
msg.append("Updatats {} cups".format(cups_updated))
msg.append(str(datetime.today()))
msg = "\n".join(msg)
if failed:
msg += "\n\nErrors\n"
for fail in failed:
msg += "{} {} {}\n".format(fail[0], fail[1], fail[2])
return msg
def _cronjob_conany_updater_mail_text(self, cursor, uid, data=None, context=None):
if not data:
data = {}
if not context:
context = {}
subject = _(u"Resultat accions update de consum anual")
msg = self._conany_updater(cursor, uid, context)
emails_to = data.get("emails_to", "").split(",")
emails = []
for email_to in emails_to:
if email_to:
emails.append(email_to.strip())
emails_to = emails
if emails_to:
user_obj = self.pool.get("res.users")
email_from = user_obj.browse(cursor, uid, uid).address_id.email
email_send(email_from, emails_to, subject, msg)
return True
_columns = {
"emp_allow_send_data": fields.boolean(
"Permetre compartir dades amb BeeData",
help="Compartir dades a través de l'API amb BeeData",
),
"emp_allow_recieve_mail_infoenergia": fields.boolean(
"Permetre rebre informes",
help="Indica si es vol rebre informes per email del servei" "d'infoenergia",
),
}
_defaults = {
"emp_allow_send_data": lambda *a: True,
"emp_allow_recieve_mail_infoenergia": lambda *a: True,
}
GiscedataPolissaInfoenergia()