-
Notifications
You must be signed in to change notification settings - Fork 3
/
import_cuentas_peek.py
166 lines (133 loc) · 5.21 KB
/
import_cuentas_peek.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
#-*- coding: utf-8 -*-
import ydbf
import sys
import erppeek
import csv
import datetime
import requests
import os.path
import vatnumber
SERVER_origen = 'http://localhost:8069'
DATABASE_origen = 'base_de_datos'
USERNAME = 'usuario'
PASSWORD = 'contraseña'
debug = True
# Conectar al ERP
origen = erppeek.Client(SERVER_origen, DATABASE_origen, USERNAME, PASSWORD)
def getLetraDNI(dni):
NIF='TRWAGMYFPDXBNJZSQVHLCKE'
if type(dni) == str or type(dni) == unicode:
try:
dni = int(dni)
except:
dni = dni.replace('/','')
dni = int(dni)
return NIF[dni%23]
return NIF[dni%23]
def decodeCIF(originalCIF):
if len(originalCIF):
# Quitar guiones, puntos y espacios
decodedCIF = originalCIF.replace("-", "").replace(".", "").replace(" ", "")
if decodedCIF[0].isdigit():
# Es una persona física
if decodedCIF[-1].isdigit():
decodedCIF += getLetraDNI(decodedCIF) # Letra de control
if len(decodedCIF) < 9:
decodedCIF = "0" + decodedCIF # Añadir un 0 delante
# Ver si lleva ya el prefijo de dos letras del país delante del código
if decodedCIF[0:2].isalpha():
return decodedCIF.upper()
if not decodedCIF.startswith('ES'):
decodedCIF = "ES" + decodedCIF
return decodedCIF.upper()
else:
return originalCIF
def import_account(dbf):
for row in dbf:
# CREACIÓN DE CUENTAS
account_obj = origen.model(name='account.account')
accounts = account_obj.browse([("code", "=", row['COD'][0:2]+'0000')])
if not accounts:
accounts = account_obj.browse([("code", "like", row['COD'][0:2])])
account = {
'name': row['TITULO'].strip(),
'code': row['COD'],
'user_type_id': accounts[0].user_type_id.id or 1,
'reconcile': accounts[0].reconcile
}
if not account_obj.browse([('code','=',row['COD'])]):
account = account_obj.create(account)
def import_partner(dbf):
for row in dbf:
account_obj = origen.model(name='account.account')
# PARA PROVEEDORES
if ((row["COD"].startswith('410') and row["COD"] != "41000000")
or (row["COD"].startswith('400') and row["COD"] != "40000000")):
country_obj = origen.model('res.country.state')
country_id = country_obj.browse([('name','ilike',row['PROVINCIA'])])
if country_id:
country_id = country_id[0].id
else:
country_id = False
account_id = account_obj.search([('code','=',row['COD'])])
if account_id:
account_id = account_id[0]
partner = {
'name': row['TITULO'].strip(),
'vat': decodeCIF(row['NIF']),
'supplier': True,
'customer': False,
'opt_out': True,
'street': row['DOMICILIO'].strip(),
'zip': row['CODPOSTAL'],
'city':row['POBLACION'],
'state_id': country_id,
'property_account_payable_id':account_id
}
if not vatnumber.check_vat(partner['vat']):
partner['vat'] = False
partner_obj = origen.model(name='res.partner')
if not partner_obj.browse([('name','=',row['TITULO'])]):
partner = partner_obj.create(partner)
else:
print row['TITULO']
# PARA CLIENTES
if ((row["COD"].startswith('430') and row["COD"] != "43000000")
or (row["COD"].startswith('440') and row["COD"] != "44000000")):
country_obj = origen.model('res.country.state')
country_id = country_obj.browse([('name','ilike',row['PROVINCIA'])])
if country_id:
country_id = country_id[0].id
else:
country_id = False
account_id = account_obj.search([('code','=',row['COD'])])
if account_id:
account_id = account_id[0]
partner = {
'name': row['TITULO'].strip(),
'vat': decodeCIF(row['NIF']),
'supplier': False,
'customer': True,
'opt_out': True,
'street': row['DOMICILIO'].strip(),
'zip': row['CODPOSTAL'],
'city':row['POBLACION'],
'state_id': country_id,
'property_account_receivable_id':account_id,
}
if not vatnumber.check_vat(partner['vat']):
partner['vat'] = False
partner_obj = origen.model(name='res.partner')
if not partner_obj.browse([('name','=',row['TITULO'])]):
partner = partner_obj.create(partner)
else:
print row['TITULO']
def importar():
dbf = ydbf.open(os.path.join('dbf', 'Subcta.dbf'), encoding='latin-1')
account_obj = origen.model(name='account.account')
print 'Importando Cuentas'
import_account(dbf)
print 'Importando Partners'
dbf2 = ydbf.open(os.path.join('dbf', 'Subcta.dbf'), encoding='latin-1')
import_partner(dbf2)
importar()