Newer
Older
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
# -*- coding: utf-8 -*-
# © 2018 Le Filament (<http://www.le-filament.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import json
import urllib2
import requests
from odoo import api, fields, models
URL = "https://data.opendatasoft.com/api/records/1.0/search/?dataset=sirene%40public&q="
CHAMPS = "&rows=100"
class SirenWizard(models.TransientModel):
_name = 'siren.wizard'
_description = 'Get values from companies'
## Default functions
@api.model
def _default_name(self):
return self.env['res.partner'].browse(self.env.context.get('active_id')).name
@api.model
def _default_partner(self):
return self.env.context.get('active_id')
## Fields
name = fields.Char(string='Entreprise', default=_default_name)
company_lines = fields.One2many('siren.wizard.company', 'wizard_id', string="Résultats",)
partner_id = fields.Integer('Partner', default=_default_partner)
## Action
def get_company_lines(self):
# Get request
r = requests.get(URL + self.name + CHAMPS)
# Serialization request to JSON
companies = r.json()
# Unlink all company lines
self.company_lines.unlink()
# Fill new company lines
for company in companies['records']:
new_company = self.company_lines.create({
'wizard_id': self.id,
'name': company['fields']['l1_normalisee'],
})
if company['fields'].get('l4_normalisee'):
new_company.street = company['fields']['l4_normalisee']
if company['fields'].get('codpos'):
new_company.zip = company['fields']['codpos']
if company['fields'].get('libcom'):
new_company.city = company['fields']['libcom']
if company['fields'].get('siren'):
new_company.siren = company['fields']['siren']
if company['fields'].get('siret'):
new_company.siret = company['fields']['siret']
if company['fields'].get('categorie'):
new_company.categorie = company['fields']['categorie']
if company['fields'].get('dcret'):
new_company.date_creation = company['fields']['dcret']
if company['fields'].get('apen700'):
new_company.ape = company['fields']['apen700']
if company['fields'].get('libapet'):
new_company.lib_ape = company['fields']['libapet']
if company['fields'].get('dateess'):
new_company.date_ess = company['fields']['dateess']
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
new_company.ess = True
if company['fields'].get('libessen'):
new_company.lib_ess = company['fields']['libessen']
if company['fields'].get('libnj'):
new_company.forme_juridique = company['fields']['libnj']
if company['fields']['efetcent'] in ["NN","0"]:
new_company.effectif = 0
else:
new_company.effectif = int(company['fields']['efetcent'])
return { "type": "ir.actions.do_nothing", }
class SirenWizardCompanies(models.TransientModel):
_name = 'siren.wizard.company'
_description = 'Companies Selection'
## Fields
wizard_id = fields.Many2one('siren.wizard', string='Wizard',)
name = fields.Char(string='Nom')
street = fields.Char(string='Rue')
zip = fields.Char(string='CP')
city = fields.Char(string='Ville')
forme_juridique = fields.Char("Forme Juridique")
siren = fields.Char("SIREN")
siret = fields.Char("SIRET")
ape = fields.Char("Code APE")
lib_ape = fields.Char("Libellé APE")
date_creation = fields.Date("Date de création")
effectif = fields.Char("Effectif")
lib_ess = fields.Char("Libellé ESS")
date_ess = fields.Date("Date ESS")
ess = fields.Boolean("ESS", default=False)
categorie = fields.Char("Catégorie")
## Action
@api.multi
def update_partner(self):
partner = self.env['res.partner'].browse(self.wizard_id.partner_id)
partner.write({
'name': self.name,
'street': self.street,
'zip': self.zip,
'city': self.city,
'forme_juridique': self.forme_juridique,
'siren': self.siren,
'siret': self.siret,
'ape': self.ape,
'lib_ape': self.lib_ape,
'date_creation': self.date_creation,
'effectif': self.effectif,
'lib_ess': self.lib_ess,
'date_ess': self.date_ess,
'ess': self.ess,
'categorie': self.categorie,
})
return True