-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathparty.py
326 lines (273 loc) · 10.7 KB
/
party.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
# -*- coding: utf-8 -*-
import magento
from trytond.model import ModelSQL, ModelView, fields
from trytond.pool import PoolMeta, Pool
from trytond.transaction import Transaction
__all__ = ['Party', 'MagentoWebsiteParty', 'Address']
__metaclass__ = PoolMeta
class Party:
"Party"
__name__ = 'party.party'
magento_ids = fields.One2Many(
"sale.channel.magento.party", "party", "Magento IDs", readonly=True
)
@classmethod
def __setup__(cls):
"""
Setup the class before adding to pool
"""
super(Party, cls).__setup__()
cls._error_messages.update({
'channel_not_found': 'Website does not exist in context'
})
@classmethod
def find_or_create_using_magento_id(cls, magento_id):
"""
This method tries to find the party with the magento ID first and
if not found it will fetch the info from magento and create a new
party with the data from magento using create_using_magento_data
:param magento_id: Party ID sent by magento
:return: Active record of record created/found
"""
Channel = Pool().get('sale.channel')
channel = Channel.get_current_magento_channel()
party = cls.find_using_magento_id(magento_id)
if not party:
with magento.Customer(
channel.magento_url, channel.magento_api_user,
channel.magento_api_key
) as customer_api:
customer_data = customer_api.info(magento_id)
party = cls.create_using_magento_data(customer_data)
return party
@classmethod
def find_using_magento_id(cls, magento_id):
"""
This method tries to find the party with the magento ID
:param magento_id: Party ID sent by magento
:return: Active record of record found
"""
MagentoParty = Pool().get('sale.channel.magento.party')
try:
magento_party, = MagentoParty.search([
('magento_id', '=', magento_id),
('channel', '=', Transaction().context['current_channel'])
])
except ValueError:
return None
else:
return magento_party.party
@classmethod
def find_or_create_using_magento_data(cls, magento_data):
"""
Looks for the customer whose magento_data is sent by magento against
the magento_channel in context.
If a record exists for this, return that else create a new one and
return
:param magento_data: Dictionary of values for customer sent by magento
:return: Active record of record created/found
"""
if Transaction().context.get('current_channel') is None:
cls.raise_user_error('channel_not_found')
party = cls.find_using_magento_data(magento_data)
if not party:
party = cls.create_using_magento_data(magento_data)
return party
@classmethod
def create_using_magento_data(cls, magento_data):
"""
Creates record of customer values sent by magento
:param magento_data: Dictionary of values for customer sent by magento
:return: Active record of record created
"""
values = {
'name': u' '.join(filter(
None, [magento_data['firstname'], magento_data['lastname']]
)),
'magento_ids': [
('create', [{
'magento_id': magento_data['customer_id'],
'channel': Transaction().context['current_channel'],
}])
],
}
if magento_data.get('email'):
values.update({'contact_mechanisms': [
('create', [{
'type': 'email',
'value': magento_data['email'],
}])
]})
party, = cls.create([values])
return party
@classmethod
def find_using_magento_data(cls, magento_data):
"""
Looks for the customer whose magento_data is sent by magento against
the magento_channel_id in context.
If record exists returns that else None
:param magento_data: Dictionary of values for customer sent by magento
:return: Active record of record found or None
"""
MagentoParty = Pool().get('sale.channel.magento.party')
try:
magento_party, = MagentoParty.search([
('magento_id', '=', magento_data['customer_id']),
('channel', '=', Transaction().context['current_channel'])
])
except ValueError:
return None
else:
return magento_party.party
class MagentoWebsiteParty(ModelSQL, ModelView):
"Magento Website Party"
__name__ = 'sale.channel.magento.party'
magento_id = fields.Integer('Magento ID', readonly=True)
channel = fields.Many2One(
'sale.channel', 'Channel', required=True, readonly=True
)
party = fields.Many2One(
'party.party', 'Party', required=True, readonly=True
)
@classmethod
def validate(cls, records):
super(MagentoWebsiteParty, cls).validate(records)
cls.check_unique_party(records)
@classmethod
def __setup__(cls):
"""
Setup the class before adding to pool
"""
super(MagentoWebsiteParty, cls).__setup__()
cls._error_messages.update({
'party_exists': 'A party must be unique in a channel'
})
@classmethod
def check_unique_party(cls, records):
"""Checks thats each party should be unique in a channel if it
does not have a magento ID of 0. magento_id of 0 means its a guest
customer.
:param records: List of active records
"""
for magento_partner in records:
if magento_partner.magento_id != 0 and cls.search([
('magento_id', '=', magento_partner.magento_id),
('channel', '=', magento_partner.channel.id),
('id', '!=', magento_partner.id),
], count=True) > 0:
cls.raise_user_error('party_exists')
class Address:
"Address"
__name__ = 'party.address'
def match_with_magento_data(self, address_data):
"""
Match the current address with the address_record.
Match all the fields of the address, i.e., streets, city, subdivision
and country. For any deviation in any field, returns False.
:param address_data: Dictionary of address data from magento
:return: True if address matches else False
"""
Country = Pool().get('country.country')
Subdivision = Pool().get('country.subdivision')
# Check if the name matches
if self.name != ' '.join(
filter(None, [address_data['firstname'], address_data['lastname']])
):
return False
# Find country and subdivision based on magento data
country = None
subdivision = None
if address_data['country_id']:
country = Country.search_using_magento_code(
address_data['country_id']
)
if address_data['region']:
subdivision = Subdivision.search_using_magento_region(
address_data['region'], country
)
street, streetbis = self.get_street_parts(address_data['street'])
if not all([
self.street == (street or None),
self.streetbis == (streetbis or None),
self.zip == (address_data['postcode'] or None),
self.city == (address_data['city'] or None),
self.country == country,
self.subdivision == subdivision,
]):
return False
return True
@classmethod
def get_street_parts(cls, magento_street_address):
"""
Magento has only 1 street address column and a line separator
puts that into two address lines.
"""
street_parts = magento_street_address.split('\n', 1)
if len(street_parts) == 2:
return street_parts[0], street_parts[1]
else:
return magento_street_address, None
@classmethod
def find_or_create_for_party_using_magento_data(cls, party, address_data):
"""
Look for the address in tryton corresponding to the address_record.
If found, return the same else create a new one and return that.
:param party: Party active record
:param address_data: Dictionary of address data from magento
:return: Active record of address created/found
"""
for address in party.addresses:
if address.match_with_magento_data(address_data):
break
else:
address = cls.create_for_party_using_magento_data(
party, address_data
)
return address
@classmethod
def create_for_party_using_magento_data(cls, party, address_data):
"""
Create address from the address record given and link it to the
party.
:param party: Party active record
:param address_data: Dictionary of address data from magento
:return: Active record of created address
"""
Country = Pool().get('country.country')
Subdivision = Pool().get('country.subdivision')
ContactMechanism = Pool().get('party.contact_mechanism')
country = None
subdivision = None
if address_data['country_id']:
country = Country.search_using_magento_code(
address_data['country_id']
)
if address_data['region']:
subdivision = Subdivision.search_using_magento_region(
address_data['region'], country
)
street, streetbis = cls.get_street_parts(address_data['street'])
address, = cls.create([{
'party': party.id,
'name': ' '.join(filter(
None, [address_data['firstname'], address_data['lastname']]
)),
'street': street,
'streetbis': streetbis,
'zip': address_data['postcode'],
'city': address_data['city'],
'country': country and country.id or None,
'subdivision': subdivision and subdivision.id or None,
}])
# Create phone as contact mechanism
if address_data.get('telephone') and not ContactMechanism.search([
('party', '=', party.id),
('type', 'in', ['phone', 'mobile']),
('value', '=', address_data['telephone']),
]):
ContactMechanism.create([{
'party': party.id,
'type': 'phone',
'value': address_data['telephone'],
}])
return address