-
Notifications
You must be signed in to change notification settings - Fork 0
/
mchimp_generationsocis.py
executable file
·114 lines (93 loc) · 3.46 KB
/
mchimp_generationsocis.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
#!/usr/bin/env python
#-*- coding: utf8 -*-
import ooop
import codecs
import sys
import argparse
from consolemsg import step, error, fail, warn
import config
O = ooop.OOOP(**config.ooop)
soci_ids = O.ResPartner.search(
[('category_id','=','Soci')
])
with codecs.open(sys.argv[1],'w', 'utf8') as output:
print >> output, u'\t'.join(unicode(x) for x in [
'ID',
'Name',
'Call name',
'Soci',
'DNI',
'E-mail',
'Language',
'Legal entity',
'Contracts',
'Anual use',
'Recommended shares',
'Covered use',
'Recommended investment',
])
for i,soci_id in enumerate(soci_ids):
step("Soci {}...".format(soci_id))
try:
soci = O.ResPartner.get(soci_id)
contractes = O.GiscedataPolissa.search(
[
'|',
('titular.id', '=', soci_id),
('pagador.id', '=', soci_id),
]
)
consums = [
dict(
contract_id = contracte['name'],
supply_address = contracte['cups_direccio'],
annual_use_kwh = cups['conany_kwh'],
)
if cups else dict()
for contracte, cups in (
(contracte, O.GiscedataCupsPs.read(contracte['cups'][0], ['conany_kwh']))
if contracte['cups'] else (contracte, None)
for contracte in O.GiscedataPolissa.read(
contractes, ['name','cups','cups_direccio'])
)
]
if not len(consums) :
error("El soci {} no te contractes".format(soci_id))
continue
if any('annual_use_kwh' not in contract for contract in consums) :
warn("El soci {} te un contracte sense consum anual calculat".format(soci_id))
continue
shareUse = 170
recommendedPercent = 70
shareCost = 100
def ambPuntDeMilers(numero) :
return '{:,}'.format(numero).replace(',','.')
totalUse = sum((contract.get('annual_use_kwh',0) for contract in consums))
recommendedShares = (totalUse*recommendedPercent/100) // shareUse
recommendedInvestment = recommendedShares * shareCost
if totalUse < shareUse :
error("El soci {} no te prou consum ({})".format(soci_id, totalUse))
continue
if soci.vat[:2] != 'ES':
warn("Soci amb un VAT code no espanyol: {}".format(soci.vat[:2]))
print >> output, u'\t'.join(unicode(x).replace('\t',' ') for x in [
soci_id,
soci.name,
soci.name.split(',')[-1].strip(),
soci.ref[1:].lstrip('0'),
soci.vat[2:],
soci.address[0].email,
soci.lang,
1 if soci.vat[2] in "ABCDEFGHJNPQRSUVW" else 0,
len(consums),
ambPuntDeMilers(totalUse),
ambPuntDeMilers(recommendedShares),
ambPuntDeMilers(recommendedShares * shareUse),
ambPuntDeMilers(recommendedInvestment),
])
except Exception as e:
import traceback
error("{}\n{}".format(
e,
"\n".join(traceback.format_stack()),
))