forked from openlabs/magento_integration
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsale.py
436 lines (371 loc) · 14.5 KB
/
sale.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# -*- coding: utf-8 -*-
"""
sale
Sale
:copyright: (c) 2013 by Openlabs Technologies & Consulting (P) Limited
:license: AGPLv3, see LICENSE for more details.
"""
import xmlrpclib
import magento
from openerp.osv import fields, osv
from openerp.tools.translate import _
# Its a map between order states of openerp and magento
# TODO: Replace by a better structure
ORDER_STATES_MAP = {
'new': 'sent',
'canceled': 'cancel',
'closed': 'done',
'complete': 'done',
'processing': 'progress',
'holded': 'sent',
'pending_payment': 'sent',
'payment_review': 'sent',
}
class Sale(osv.Model):
"Sale"
_inherit = 'sale.order'
_columns = dict(
magento_id=fields.integer('Magento ID', readonly=True),
magento_instance=fields.many2one(
'magento.instance', 'Magento Instance', readonly=True,
),
magento_store_view=fields.many2one(
'magento.store.store_view', 'Store View', readonly=True,
),
)
_sql_constraints = [(
'magento_id_instance_unique', 'unique(magento_id, magento_instance)',
'A sale must be unique in an instance'
)]
def check_store_view_instance(self, cursor, user, ids, context=None):
"""
Checks if instance of store view is same as instance of sale order
:param cursor: Database cursor
:param user: ID of current user
:param ids: IDs of records
:param context: Application context
:return: True or False
"""
for sale in self.browse(cursor, user, ids, context=context):
if sale.magento_id:
if sale.magento_store_view.instance != sale.magento_instance:
return False
return True
_constraints = [
(
check_store_view_instance,
'Error: Store view must have same instance as sale order',
[]
)
]
def find_or_create_using_magento_data(
self, cursor, user, order_data, context
):
"""
Find or Create sale using magento data
:param cursor: Database cursor
:param user: ID of current user
:param order_data: Order Data from magento
:param context: Application context
:returns: Browse record of sale order found/created
"""
sale = self.find_using_magento_data(
cursor, user, order_data, context
)
if not sale:
sale = self.create_using_magento_data(
cursor, user, order_data, context
)
return sale
def find_using_magento_data(
self, cursor, user, order_data, context
):
"""
Create sale using magento data
:param cursor: Database cursor
:param user: ID of current user
:param order_data: Order Data from magento
:param context: Application context
:returns: Browse record of sale order found
"""
# each sale has to be unique in an instance of magento
sale_ids = self.search(cursor, user, [
('magento_id', '=', int(order_data['order_id'])),
('magento_instance', '=', context.get('magento_instance'))
], context=context)
return sale_ids and self.browse(
cursor, user, sale_ids[0], context
) or None
def find_or_create_using_magento_increment_id(
self, cursor, user, order_increment_id, context
):
"""
Finds or create sale order using magento ID
:param cursor: Database cursor
:param user: ID of current user
:param order_increment_id: Order increment ID from magento
:type order_increment_id: string
:param context: Application context
:returns: Browse record of sale order created/found
"""
instance_obj = self.pool.get('magento.instance')
sale = self.find_using_magento_increment_id(
cursor, user, order_increment_id, context
)
if not sale:
instance = instance_obj.browse(
cursor, user, context['magento_instance'], context=context
)
with magento.Order(
instance.url, instance.api_user, instance.api_key
) as order_api:
order_data = order_api.info(order_increment_id)
sale = self.create_using_magento_data(
cursor, user, order_data, context
)
return sale
def find_using_magento_id(self, cursor, user, order_id, context):
"""
Create sale using magento id
:param cursor: Database cursor
:param user: ID of current user
:param order_id: Order ID from magento
:type order_id: integer
:param context: Application context
:returns: Browse record of sale order created
"""
# each sale has to be unique in an instance of magento
sale_ids = self.search(cursor, user, [
('magento_id', '=', order_id),
('magento_instance', '=', context.get('magento_instance'))
], context=context)
return sale_ids and self.browse(
cursor, user, sale_ids[0], context
) or None
def find_using_magento_increment_id(
self, cursor, user, order_increment_id, context
):
"""
Create sale using magento id
:param cursor: Database cursor
:param user: ID of current user
:param order_increment_id: Order Increment ID from magento
:type order_increment_id: string
:param context: Application context
:returns: Browse record of sale order created
"""
instance_obj = self.pool.get('magento.instance')
instance = instance_obj.browse(
cursor, user, context['magento_instance'], context=context
)
# Each sale has to be unique in an instance of magento
sale_ids = self.search(cursor, user, [
('name', '=', instance.order_prefix + order_increment_id),
('magento_instance', '=', context['magento_instance'])
], context=context)
return sale_ids and self.browse(
cursor, user, sale_ids[0], context
) or None
def create_using_magento_data(self, cursor, user, order_data, context):
"""
Create a sale order from magento data
:param cursor: Database cursor
:param user: ID of current user
:param order_data: Order Data from magento
:param context: Application context
:returns: Browse record of sale order created
"""
currency_obj = self.pool.get('res.currency')
store_view_obj = self.pool.get('magento.store.store_view')
partner_obj = self.pool.get('res.partner')
uom_obj = self.pool.get('product.uom')
product_obj = self.pool.get('product.product')
store_view = store_view_obj.browse(
cursor, user, context['magento_store_view'], context
)
if not store_view.shop:
raise osv.except_osv(
_('Not Found!'),
_(
'Magento Store %s should have a shop configured.'
% store_view.store.name
)
)
if not store_view.shop.pricelist_id:
raise osv.except_osv(
_('Not Found!'),
_(
'Shop on store %s does not have a pricelist!'
% store_view.store.name
)
)
instance = store_view.instance
currency = currency_obj.search_using_magento_code(
cursor, user, order_data['order_currency_code'], context
)
if order_data['customer_id']:
partner = partner_obj.find_or_create_using_magento_id(
cursor, user, order_data['customer_id'], context
)
else:
partner = partner_obj.create_using_magento_data(
cursor, user, {
'firstname': order_data['customer_firstname'],
'lastname': order_data['customer_lastname'],
'email': order_data['customer_email'],
'magento_id': 0
},
context
)
partner_invoice_address = \
partner_obj.find_or_create_address_as_partner_using_magento_data(
cursor, user, order_data['billing_address'], partner, context
)
partner_shipping_address = \
partner_obj.find_or_create_address_as_partner_using_magento_data(
cursor, user, order_data['shipping_address'], partner, context
)
unit, = uom_obj.search(
cursor, user, [('name', '=', 'Unit(s)')], context=context
)
sale_data = {
'name': instance.order_prefix + order_data['increment_id'],
'shop_id': store_view.shop.id,
'date_order': order_data['created_at'].split()[0],
'partner_id': partner.id,
'pricelist_id': store_view.shop.pricelist_id.id,
'currency_id': currency.id,
'partner_invoice_id': partner_invoice_address.id,
'partner_shipping_id': partner_shipping_address.id,
'magento_id': int(order_data['order_id']),
'magento_instance': instance.id,
'magento_store_view': store_view.id,
'order_line': [
(0, 0, {
'name': item['name'],
'price_unit': float(item['price']),
'product_uom': unit,
'product_uom_qty': float(item['qty_ordered']),
'notes': item['product_options'],
'product_id':
product_obj.find_or_create_using_magento_id(
cursor, user, item['product_id'],
context=context
).id
}) for item in order_data['items']
]
}
if float(order_data.get('shipping_amount')):
sale_data['order_line'].append(
self.get_shipping_line_data_using_magento_data(
cursor, user, order_data, context
)
)
if float(order_data.get('discount_amount')):
sale_data['order_line'].append(
self.get_discount_line_data_using_magento_data(
cursor, user, order_data, context
)
)
sale_id = self.create(
cursor, user, sale_data, context=context
)
sale = self.browse(cursor, user, sale_id, context)
# Process sale now
self.process_sale_using_magento_state(
cursor, user, sale, order_data['state'], context
)
return sale
def get_shipping_line_data_using_magento_data(
self, cursor, user, order_data, context
):
"""
Create a shipping line for the given sale using magento data
:param cursor: Database cursor
:param user: ID of current user
:param order_data: Order Data from magento
:param context: Application context
"""
uom_obj = self.pool.get('product.uom')
unit, = uom_obj.search(
cursor, user, [('name', '=', 'Unit(s)')], context=context
)
return (0, 0, {
'name': 'Magento Shipping',
'price_unit': float(order_data.get('shipping_amount', 0.00)),
'product_uom': unit,
'notes': ' - '.join([
order_data['shipping_method'],
order_data['shipping_description']
])
})
def get_discount_line_data_using_magento_data(
self, cursor, user, order_data, context
):
"""
Create a discount line for the given sale using magento data
:param cursor: Database cursor
:param user: ID of current user
:param order_data: Order Data from magento
:param context: Application context
"""
uom_obj = self.pool.get('product.uom')
unit, = uom_obj.search(
cursor, user, [('name', '=', 'Unit(s)')], context=context
)
return (0, 0, {
'name': order_data['discount_description'] or 'Magento Discount',
'price_unit': float(order_data.get('discount_amount', 0.00)),
'product_uom': unit,
'notes': order_data['discount_description'],
})
def process_sale_using_magento_state(
self, cursor, user, sale, magento_state, context
):
"""Process the sale in openerp based on the state of order
when its imported from magento
:param cursor: Database cursor
:param user: ID of current user
:param sale: Browse record of sale
:param magento_state: State on magento the order was imported in
:param context: Application context
"""
# TODO: Improve this for invoicing and shipping etc
openerp_state = ORDER_STATES_MAP[magento_state]
# If order is canceled, just cancel it
if openerp_state == 'cancel':
self.action_cancel(cursor, user, [sale.id], context)
return
# Order is not canceled, move it to quotation
self.action_button_confirm(cursor, user, [sale.id], context)
if openerp_state in ['closed', 'complete', 'processing']:
self.action_wait(cursor, user, [sale.id], context)
if openerp_state in ['closed', 'complete']:
self.action_done(cursor, user, [sale.id], context)
def export_order_status_to_magento(self, cursor, user, sale, context):
"""
Export order status to magento.
:param cursor: Database cursor
:param user: ID of current user
:param sale: Browse record of sale
:param context: Application context
:return: Browse record of sale
"""
if not sale.magento_id:
return sale
instance = sale.magento_instance
if sale.state == 'cancel':
increment_id = sale.name.split(instance.order_prefix)[1]
# This try except is placed because magento might not accept this
# order status change due to its workflow constraints.
# TODO: Find a better way to do it
try:
with magento.Order(
instance.url, instance.api_user, instance.api_key
) as order_api:
order_api.cancel(increment_id)
except xmlrpclib.Fault, f:
if f.faultCode == 103:
return sale
# TODO: Add logic for other sale states also
return sale