forked from openlabs/magento_integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
partner.py
308 lines (264 loc) · 10.9 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
# -*- coding: utf-8 -*-
"""
partner
Partner
:copyright: (c) 2013 by Openlabs Technologies & Consulting (P) Limited
:license: AGPLv3, see LICENSE for more details.
"""
import magento
from openerp.osv import fields, osv
from openerp.tools.translate import _
class MagentoWebsitePartner(osv.Model):
"Magento Website partner store"
_name = 'magento.website.partner'
_columns = dict(
magento_id=fields.integer('Magento ID', readonly=True),
website=fields.many2one(
'magento.instance.website', 'Website', required=True,
readonly=True,
),
partner=fields.many2one(
'res.partner', 'Partner', required=True, readonly=True
)
)
def check_unique_partner(self, cursor, user, ids, context=None):
"""Checks thats each partner should be unique in a website if it
does not have a magento ID of 0. magento_id of 0 means its a guest
cutsomers.
:param cursor: Database cursor
:param user: ID of current user
:param ids: IDs of records
:param context: Application context
:return: True or False
"""
for magento_partner in self.browse(cursor, user, ids, context=context):
if magento_partner.magento_id != 0 and self.search(cursor, user, [
('magento_id', '=', magento_partner.magento_id),
('website', '=', magento_partner.website.id),
('id', '!=', magento_partner.id),
], context=context, count=True) > 0:
return False
return True
_constraints = [
(
check_unique_partner,
'Error: Customers should be unique for a website',
[]
)
]
class Partner(osv.Model):
"Partner"
_inherit = 'res.partner'
_columns = dict(
magento_ids=fields.one2many(
'magento.website.partner', 'partner', "Magento IDs", readonly=True
),
)
def find_or_create_using_magento_id(
self, cursor, user, magento_id, context
):
"""
Finds or creates partner using magento ID
:param cursor: Database cursor
:param user: ID of current user
:param magento_id: Partner ID sent by magento
:param context: Application context.
:return: Browse record of record created/found
"""
instance_obj = self.pool.get('magento.instance')
partner = self.find_using_magento_id(cursor, user, magento_id, context)
if not partner:
instance = instance_obj.browse(
cursor, user, context['magento_instance'], context=context
)
with magento.Customer(
instance.url, instance.api_user, instance.api_key
) as customer_api:
customer_data = customer_api.info(magento_id)
partner = self.create_using_magento_data(
cursor, user, customer_data, context
)
return partner
def find_using_magento_id(self, cursor, user, magento_id, context):
"""
Finds partner with magento id
:param cursor: Database cursor
:param user: ID of current user
:param magento_id: Partner ID sent by magento
:param context: Application context.
:return: Browse record of record found
"""
magento_partner_obj = self.pool.get('magento.website.partner')
record_ids = magento_partner_obj.search(
cursor, user, [
('magento_id', '=', magento_id),
('website', '=', context['magento_website'])
], context=context
)
return record_ids and magento_partner_obj.browse(
cursor, user, record_ids[0], context=context
).partner or None
def find_or_create(self, cursor, user, customer_data, context):
"""
Looks for the customer whose `customer_data` is sent by magento against
the `magento_website_id` in context.
If a record exists for this, return that else create a new one and
return
:param cursor: Database cursor
:param user: ID of current user
:param customer_data: Dictionary of values for customer sent by magento
:param context: Application context. Contains the magento_website to
which the customer has to be linked
:return: Browse record of record created/found
"""
if not context['magento_website']:
raise osv.except_osv(
_('Not Found!'),
_('Website does not exists in context. ')
)
partner = self.find_using_magento_data(
cursor, user, customer_data, context
)
if not partner:
partner = self.create_using_magento_data(
cursor, user, customer_data, context
)
return partner
def create_using_magento_data(self, cursor, user, customer_data, context):
"""
Creates record of customer values sent by magento
:param cursor: Database cursor
:param user: ID of current user
:param customer_data: Dictionary of values for customer sent by magento
:param context: Application context. Contains the magento_website
to which the customer has to be linked
:return: Browse record of record created
"""
partner_id = self.create(
cursor, user, {
'name': u' '.join(
[customer_data['firstname'], customer_data['lastname']]
),
'email': customer_data['email'],
'magento_ids': [
(0, 0, {
'magento_id': customer_data.get('customer_id', 0),
'website': context['magento_website'],
})
],
}, context=context
)
return self.browse(cursor, user, partner_id, context)
def find_using_magento_data(self, cursor, user, customer_data, context):
"""
Looks for the customer whose `customer_data` is sent by magento against
the `magento_website_id` in context.
If record exists returns that else None
:param cursor: Database cursor
:param user: ID of current user
:param customer_data: Dictionary of values for customer sent by magento
:param context: Application context. Contains the magento_website
to which the customer has to be linked
:return: Browse record of record found
"""
magento_partner_obj = self.pool.get('magento.website.partner')
# This is a guest customer. Create a new partner for this
if not customer_data.get('customer_id'):
return None
record_ids = magento_partner_obj.search(
cursor, user, [
('magento_id', '=', customer_data['customer_id']),
('website', '=', context['magento_website'])
], context=context
)
return record_ids and magento_partner_obj.browse(
cursor, user, record_ids[0], context
).partner or None
def find_or_create_address_as_partner_using_magento_data(
self, cursor, user, address_data, parent, context
):
"""Find or Create an address from magento with `address_data` as a
partner in openerp with `parent` as the parent partner of this address
partner (how fucked up is that).
:param cursor: Database cursor
:param user: ID of current user
:param address_data: Dictionary of address data from magento
:param parent: Parent partner for this address partner.
:param context: Application context.
:return: Browse record of address created/found
"""
for address in parent.child_ids + [parent]:
if self.match_address_with_magento_data(
cursor, user, address, address_data
):
break
else:
address = self.create_address_as_partner_using_magento_data(
cursor, user, address_data, parent, context
)
return address
def match_address_with_magento_data(
self, cursor, user, address, address_data
):
"""Match the `address` in openerp with the `address_data` from magento
If everything matches exactly, return True, else return False
:param cursor: Database cursor
:param user: ID of current user
:param address: Browse record of address partner
:param address_data: Dictionary of address data from magento
:return: True if address matches else False
"""
# Check if the name matches
if address.name != u' '.join(
[address_data['firstname'], address_data['lastname']]
):
return False
if not all([
(address.street or None) == address_data['street'],
(address.zip or None) == address_data['postcode'],
(address.city or None) == address_data['city'],
(address.phone or None) == address_data['telephone'],
(address.fax or None) == address_data['fax'],
(address.country_id and address.country_id.code or None) ==
address_data['country_id'],
(address.state_id and address.state_id.name or None) ==
address_data['region']
]):
return False
return True
def create_address_as_partner_using_magento_data(
self, cursor, user, address_data, parent, context
):
"""Create a new partner with the `address_data` under the `parent`
:param cursor: Database cursor
:param user: ID of current user
:param address_data: Dictionary of address data from magento
:param parent: Parent partner for this address partner.
:param context: Application Context
:return: Browse record of address created
"""
country_obj = self.pool.get('res.country')
state_obj = self.pool.get('res.country.state')
country = country_obj.search_using_magento_code(
cursor, user, address_data['country_id'], context
)
if address_data['region']:
state_id = state_obj.find_or_create_using_magento_region(
cursor, user, country, address_data['region'], context
).id
else:
state_id = None
address_id = self.create(cursor, user, {
'name': u' '.join(
[address_data['firstname'], address_data['lastname']]
),
'street': address_data['street'],
'state_id': state_id,
'country_id': country.id,
'city': address_data['city'],
'zip': address_data['postcode'],
'phone': address_data['telephone'],
'fax': address_data['fax'],
'parent_id': parent.id,
}, context=context)
return self.browse(cursor, user, address_id, context=context)