-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpartner.py
419 lines (381 loc) · 15.1 KB
/
partner.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# -*- coding: utf-8 -*-
from osv import fields, osv
from tools.translate import _
import datetime
ACC_CODE_WIDTH = 12
class ResPartner(osv.osv):
"""Modifiquem res_partner per poder crear un compte comptable a partir del
seu codi (ref)."""
_name = "res.partner"
_inherit = "res.partner"
def create_account(
self, cursor, uid, ids, acc_code, acc_type, user_type, code_gen, context=None
):
"""Mètode genèric per crear comptes comptables específics per clients"""
if not context:
context = {}
parent = None
res = {}
account_obj = self.pool.get("account.account")
search_params = [("code", "=", acc_code)]
account = account_obj.search(cursor, uid, search_params)
if account:
parent = account[0]
else:
raise osv.except_osv(_("Error"), _(u"No s'ha trobat el compte pare"))
ut_obj = self.pool.get("account.account.type")
search_params = [("code", "=", user_type)]
usertype = ut_obj.search(cursor, uid, search_params)
for partner in self.browse(cursor, uid, ids):
account_code = code_gen(cursor, uid, partner.id, parent, context)
account_code = account_code[str(partner.id)]
# comprovem que no existeixi ja el compte primer
search_params = [("code", "=", account_code)]
found = account_obj.search(cursor, uid, search_params)
if found:
res[str(partner.id)] = found[0]
else:
vals = {
"name": partner.name,
"parent_id": parent,
"code": account_code,
"user_type": usertype[0],
"type": acc_type,
"reconcile": 1,
}
res[str(partner.id)] = account_obj.create(cursor, uid, vals, context)
return res
def generate_acc_code_43(self, cursor, uid, ids, parent_id=None, context=None):
"""Mètode per generar el codi del nou compte pel soci a Clientes 4300."""
if not context:
context = {}
res = {}
account_obj = self.pool.get("account.account")
if not parent_id: # si no hi ha parent, malament.
return res
if not isinstance(ids, list):
ids = [ids]
parent = account_obj.read(cursor, uid, parent_id, ["code", "parent_id"])
grandparent = account_obj.read(cursor, uid, parent["parent_id"][0], ["code"])
parent_prefix = grandparent["code"]
for partner in self.browse(cursor, uid, ids):
base = partner.ref
if partner.ref.startswith("S"):
base = partner.ref.replace("S", "1", 1)
if partner.ref.startswith("T"):
base = partner.ref.replace("T", "2", 1)
# netejem caràcters no numèrics
if not base.isdigit():
base = "".join([c for c in base if c.isdigit()])
padding = "0" * (ACC_CODE_WIDTH - len(parent_prefix) - len(base))
res[str(partner.id)] = "%s%s%s" % (parent_prefix, padding, base)
return res
def generate_acc_code_410(self, cursor, uid, ids, parent_id=None, context=None):
"""Mètode que genera el codi pel compte comptable 410*."""
if not context:
context = {}
if not isinstance(ids, list):
ids = [ids]
res = {}
prefix = "4100"
for partner in self.browse(cursor, uid, ids, context):
ref = "".join([a for a in partner.ref if a.isdigit()])
padding = "0" * (ACC_CODE_WIDTH - len(prefix) - len(ref))
res[str(partner.id)] = "%s%s%s" % (prefix, padding, ref)
return res
def generate_acc_code_163(self, cursor, uid, ids, parent_id=None, context=None):
"""Mètode que genera el codi pel compte comptable 163*."""
if not context:
context = {}
if not isinstance(ids, list):
ids = [ids]
res = {}
prefix = "163"
for partner in self.browse(cursor, uid, ids, context):
ref = "".join([a for a in partner.ref if a.isdigit()])
padding = "0" * (ACC_CODE_WIDTH - len(prefix) - len(ref))
res[str(partner.id)] = "%s%s%s" % (prefix, padding, ref)
return res
def generate_acc_code_1714(self, cursor, uid, ids, parent_id=None, context=None):
"""Mètode que genera el codi pel compte comptable 1714*."""
if not context:
context = {}
if not isinstance(ids, list):
ids = [ids]
res = {}
prefix = "1714"
for partner in self.browse(cursor, uid, ids, context):
ref = "".join([a for a in partner.ref if a.isdigit()])
padding = "0" * (ACC_CODE_WIDTH - len(prefix) - len(ref))
res[str(partner.id)] = "%s%s%s" % (prefix, padding, ref)
return res
def generate_acc_code_1635(self, cursor, uid, ids, parent_id=None, context=None):
"""Mètode que genera el codi pel compte comptable 1635*."""
if not context:
context = {}
if not isinstance(ids, list):
ids = [ids]
res = {}
prefix = "1635"
for partner in self.browse(cursor, uid, ids, context):
ref = "".join([a for a in partner.ref if a.isdigit()])
padding = "0" * (ACC_CODE_WIDTH - len(prefix) - len(ref))
res[str(partner.id)] = "%s%s%s" % (prefix, padding, ref)
return res
def create_account_43(self, cursor, uid, ids, context=None):
"""Mètode que crea l'account_account 43* corresponent al partner"""
if not context:
context = {}
return self.create_account(
cursor,
uid,
ids,
"4300",
"receivable",
"terceros - rec",
self.generate_acc_code_43,
context,
)
def create_account_410(self, cursor, uid, ids, context=None):
"""Mètode que crea l'account_account 410* corresponent al partner"""
if not context:
context = {}
return self.create_account(
cursor,
uid,
ids,
"4100",
"payable",
"terceros - pay",
self.generate_acc_code_410,
context,
)
def create_account_163(self, cursor, uid, ids, context=None):
"""Mètode que crea l'account_account 163* corresponent al partner"""
if not context:
context = {}
return self.create_account(
cursor, uid, ids, "163", "other", "capital", self.generate_acc_code_163, context
)
def create_account_1714(self, cursor, uid, ids, context=None):
"""Mètode que crea l'account_account 163* corresponent al partner"""
if not context:
context = {}
return self.create_account(
cursor, uid, ids, "1714", "other", "capital", self.generate_acc_code_1714, context
)
def create_account_1635(self, cursor, uid, ids, context=None):
"""Mètode que crea l'account_account 163* corresponent al partner"""
if not context:
context = {}
return self.create_account(
cursor, uid, ids, "1635", "other", "capital", self.generate_acc_code_1635, context
)
def button_assign_acc_43(self, cursor, uid, ids, context=None):
"""Mètode per ser cridat des del botó."""
if not context:
context = {}
res = self.create_account_43(cursor, uid, ids, context)
for partner_id in res:
self.write(
cursor, uid, int(partner_id), {"property_account_receivable": res[partner_id]}
)
return True
def button_assign_acc_410(self, cursor, uid, ids, context=None):
"""Mètode per ser cridat des del botó."""
if not context:
context = {}
res = self.create_account_410(cursor, uid, ids, context)
for partner_id in res:
self.write(
cursor, uid, int(partner_id), {"property_account_liquidacio": res[partner_id]}
)
return True
def button_assign_acc_163(self, cursor, uid, ids, context=None):
"""Mètode per ser cridat des del botó."""
if not context:
context = {}
gen_ids = []
for partner in self.read(cursor, uid, ids, ["property_account_aportacions"]):
if not partner["property_account_aportacions"]:
gen_ids.append(partner["id"])
if gen_ids:
aa_obj = self.pool.get("account.account")
ag_id = aa_obj.search(cursor, uid, [("code", "=", "163000000000")])
if ag_id:
for partner in self.browse(cursor, uid, ids):
self.write(
cursor, uid, int(partner.id), {"property_account_aportacions": ag_id[0]}
)
else:
return False
return True
def button_assign_acc_1714(self, cursor, uid, ids, context=None):
"""Mètode per ser cridat des del botó."""
if not context:
context = {}
res = self.create_account_1714(cursor, uid, ids, context)
for partner_id in res:
self.write(cursor, uid, int(partner_id), {"property_account_titols": res[partner_id]})
return True
def button_assign_acc_1635(self, cursor, uid, ids, context=None):
"""Mètode per ser cridat des del botó."""
if not context:
context = {}
aa_obj = self.pool.get("account.account")
ag_id = aa_obj.search(cursor, uid, [("code", "=", "163500000000")])
if ag_id:
for partner in self.browse(cursor, uid, ids):
self.write(cursor, uid, int(partner.id), {"property_account_gkwh": ag_id[0]})
return True
else:
return False
def become_member(self, cursor, uid, id, context=None):
imd_obj = self.pool.get("ir.model.data")
soci_obj = self.pool.get("somenergia.soci")
ir_seq = self.pool.get("ir.sequence")
soci_cat = imd_obj._get_obj(cursor, uid, "som_partner_account", "res_partner_category_soci")
# Assign Member category
partner = self.read(cursor, uid, id, ["ref", "category_id"])
if soci_cat.id not in partner["category_id"]:
self.write(cursor, uid, partner["id"], {"category_id": [(4, soci_cat.id)]})
# Assign Member ref code
if not partner["ref"] or partner["ref"][0] != "S":
codi = ir_seq.get(cursor, uid, "res.partner.soci")
self.write(cursor, uid, partner["id"], {"ref": codi})
# Create Member instance
soci_ids = soci_obj.search(
cursor,
uid,
[
("partner_id", "=", partner["id"]),
],
context={"active_test": False},
)
if soci_ids:
soci_id = soci_ids[0]
soci = soci_obj.read(
cursor,
uid,
soci_id,
[
"data_baixa_soci",
"comment",
],
)
newcomment = (
(
_(
"{today:%Y-%m-%d} " "Donat d'alta quan estava de baixa des de {dropoutdate}"
).format(
today=datetime.date.today(),
dropoutdate=soci["data_baixa_soci"],
)
)
if soci["data_baixa_soci"]
else ""
)
comment = "\n".join([x for x in (soci["comment"], newcomment) if x])
soci_obj.write(
cursor,
uid,
soci_id,
dict(
active=True,
data_baixa_soci=False,
baixa=False,
comment=comment,
),
)
return soci_id
return soci_obj.create_one_soci(cursor, uid, partner["id"])
def adopt_contracts_as_member(self, cursor, uid, partner_id, context=None):
contract_obj = self.pool.get("giscedata.polissa")
contract_ids = contract_obj.search(
cursor,
uid,
[
"|",
("titular", "=", partner_id),
("pagador", "=", partner_id),
],
)
adopted_ids = []
for contract_id in contract_ids:
contract = contract_obj.read(cursor, uid, contract_id, ["pagador", "titular", "soci"])
if contract["soci"]:
if contract["soci"][0] == contract["pagador"][0]:
continue
if contract["soci"][0] == contract["titular"][0]:
continue
adopted_ids.append(contract_id)
contract_obj.write(
cursor,
uid,
contract_id,
{
"soci": partner_id,
},
)
return adopted_ids
def button_assign_soci_seq(self, cursor, uid, ids, context=None):
"""Mètode per ser cridat des de botó.
Assigna un nou codi de seqüència de soci al ref del partner
en cas de no tenir-ne.
"""
if not isinstance(ids, list):
ids = [ids]
for id in ids:
self.become_member(cursor, uid, id)
self.adopt_contracts_as_member(cursor, uid, id)
_columns = {
"property_account_aportacions": fields.property(
"account.account",
type="many2one",
relation="account.account",
string="Compte aportacions",
method=True,
view_load=True,
domain=[("type", "=", "other")],
help="Aquest és el compte on s'apuntaran les aportacions",
required=False,
readonly=True,
),
"property_account_liquidacio": fields.property(
"account.account",
type="many2one",
relation="account.account",
string="Compte liquidacions",
method=True,
view_load=True,
domain=[("type", "=", "payable")],
help="Aquest és el compte on s'apuntaran les liquidacions " "d'aportacions",
required=False,
readonly=True,
),
"property_account_titols": fields.property(
"account.account",
type="many2one",
relation="account.account",
string="Compte títols",
method=True,
view_load=True,
domain=[("type", "=", "other")],
help="Aquest és el compte on s'apuntaran la compra de títols",
required=False,
readonly=True,
),
"property_account_gkwh": fields.property(
"account.account",
type="many2one",
relation="account.account",
string="Compte Generation kWh",
method=True,
view_load=True,
domain=[("type", "=", "other")],
help="Aquest és el compte on s'apuntarà el préstec generation kWh",
required=False,
readonly=True,
),
}
ResPartner()