forked from openlabs/magento_integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.py
659 lines (568 loc) · 22.7 KB
/
product.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# -*- coding: UTF-8 -*-
'''
magento
:copyright: (c) 2013 by Openlabs Technologies & Consulting (P) LTD
:license: AGPLv3, see LICENSE for more details
'''
import magento
from openerp.osv import fields, osv
from openerp.tools.translate import _
import openerp.addons.decimal_precision as dp
class Category(osv.Model):
"""Product Category
"""
_inherit = 'product.category'
_columns = dict(
magento_ids=fields.one2many(
'magento.instance.product_category', 'category',
string='Magento IDs', readonly=True,
),
)
def create_tree_using_magento_data(
self, cursor, user, category_tree, context
):
"""Create the categories from the category tree
:param cursor: Database cursor
:param user: ID of current user
:param category_tree: Category Tree from magento
:param context: Application context
"""
# Create the root
root_categ = self.find_or_create_using_magento_data(
cursor, user, category_tree, context=context
)
for child in category_tree['children']:
self.find_or_create_using_magento_data(
cursor, user, child, parent=root_categ.id, context=context
)
if child['children']:
self.create_tree_using_magento_data(
cursor, user, child, context
)
def find_or_create_using_magento_data(
self, cursor, user, category_data, parent=None, context=None
):
"""Find or Create category using magento data
:param cursor: Database cursor
:param user: ID of current user
:param category_data: Category Data from magento
:param parent: openerp ID of parent if present else None
:param context: Application context
:returns: Browse record of category found/created
"""
category = self.find_using_magento_data(
cursor, user, category_data, context
)
if not category:
category = self.create_using_magento_data(
cursor, user, category_data, parent, context
)
return category
def find_or_create_using_magento_id(
self, cursor, user, magento_id, parent=None, context=None
):
"""Find or Create category using magento ID of category
:param cursor: Database cursor
:param user: ID of current user
:param magento_id: Category ID from magento
:param parent: openerp ID of parent if present else None
:param context: Application context
:returns: Browse record of category found/created
"""
instance_obj = self.pool.get('magento.instance')
category = self.find_using_magento_id(
cursor, user, magento_id, context
)
if not category:
instance = instance_obj.browse(
cursor, user, context['magento_instance'], context=context
)
with magento.Category(
instance.url, instance.api_user, instance.api_key
) as category_api:
category_data = category_api.info(magento_id)
category = self.create_using_magento_data(
cursor, user, category_data, parent, context
)
return category
def find_using_magento_data(
self, cursor, user, category_data, context=None
):
"""Find category using magento data
:param cursor: Database cursor
:param user: ID of current user
:param category_data: Category Data from magento
:param context: Application context
:returns: Browse record of category found or None
"""
magento_category_obj = self.pool.get(
'magento.instance.product_category'
)
record_ids = magento_category_obj.search(cursor, user, [
('magento_id', '=', int(category_data['category_id'])),
('instance', '=', context['magento_instance'])
], context=context)
return record_ids and magento_category_obj.browse(
cursor, user, record_ids[0], context=context
).category or None
def find_using_magento_id(
self, cursor, user, magento_id, context=None
):
"""Find category using magento id or category
:param cursor: Database cursor
:param user: ID of current user
:param magento_id: Category ID from magento
:param context: Application context
:returns: Browse record of category found or None
"""
magento_category_obj = self.pool.get(
'magento.instance.product_category'
)
record_ids = magento_category_obj.search(cursor, user, [
('magento_id', '=', magento_id),
('instance', '=', context['magento_instance'])
], context=context)
return record_ids and magento_category_obj.browse(
cursor, user, record_ids[0], context=context
).category or None
def create_using_magento_data(
self, cursor, user, category_data, parent=None, context=None
):
"""Create category using magento data
:param cursor: Database cursor
:param user: ID of current user
:param category_data: Category Data from magento
:param parent: openerp ID of parent if present else None
:param context: Application context
:returns: Browse record of category created
"""
category_id = self.create(cursor, user, {
'name': category_data['name'],
'parent_id': parent,
'magento_ids': [(0, 0, {
'magento_id': int(category_data['category_id']),
'instance': context['magento_instance'],
})]
}, context=context)
return self.browse(cursor, user, category_id, context=context)
class MagentoInstanceCategory(osv.Model):
"""Magento Instance - Product category store
This model keeps a record of a category's association with an instance and
the ID of category on that instance
"""
_name = 'magento.instance.product_category'
_description = 'Magento Instance - Product category store'
_columns = dict(
magento_id=fields.integer(
'Magento ID', readonly=True, required=True, select=True,
),
instance=fields.many2one(
'magento.instance', 'Magento Instance', readonly=True,
select=True, required=True
),
category=fields.many2one(
'product.category', 'Product Category', readonly=True,
required=True, select=True
)
)
_sql_constraints = [
(
'magento_id_instance_unique',
'unique(magento_id, instance)',
'Each category in an instance must be unique!'
),
]
class Product(osv.Model):
"""Product
"""
_inherit = 'product.product'
_columns = dict(
magento_product_type=fields.selection([
('simple', 'Simple'),
('configurable', 'Configurable'),
('grouped', 'Grouped'),
('bundle', 'Bundle'),
('virtual', 'Virtual'),
('downloadable', 'Downloadable'),
], 'Magento Product type', readonly=True),
magento_ids=fields.one2many(
'magento.website.product', 'product',
string='Magento IDs', readonly=True,
),
price_tiers=fields.one2many(
'product.price_tier', 'product', string='Price Tiers'
),
)
def find_or_create_using_magento_id(
self, cursor, user, magento_id, context
):
"""
Find or create product using magento_id
:param cursor: Database cursor
:param user: ID of current user
:param magento_id: Product ID from magento
:param context: Application context
:returns: Browse record of product found/created
"""
website_obj = self.pool.get('magento.instance.website')
product = self.find_using_magento_id(
cursor, user, magento_id, context
)
if not product:
# If product is not found, get the info from magento and delegate
# to create_using_magento_data
website = website_obj.browse(
cursor, user, context['magento_website'], context=context
)
instance = website.instance
with magento.Product(
instance.url, instance.api_user, instance.api_key
) as product_api:
product_data = product_api.info(magento_id)
product = self.create_using_magento_data(
cursor, user, product_data, context
)
return product
def find_using_magento_id(self, cursor, user, magento_id, context):
"""
Finds product using magento id
:param cursor: Database cursor
:param user: ID of current user
:param magento_id: Product ID from magento
:param context: Application context
:returns: Browse record of product found
"""
magento_product_obj = self.pool.get('magento.website.product')
record_ids = magento_product_obj.search(
cursor, user, [
('magento_id', '=', magento_id),
('website', '=', context['magento_website'])
], context=context
)
return record_ids and magento_product_obj.browse(
cursor, user, record_ids[0], context=context
).product or None
def find_or_create_using_magento_data(
self, cursor, user, product_data, context=None
):
"""Find or Create product using magento data
:param cursor: Database cursor
:param user: ID of current user
:param product_data: Product Data from magento
:param context: Application context
:returns: Browse record of product found/created
"""
product = self.find_using_magento_data(
cursor, user, product_data, context
)
if not product:
product = self.create_using_magento_data(
cursor, user, product_data, context
)
return product
def find_using_magento_data(
self, cursor, user, product_data, context=None
):
"""Find product using magento data
:param cursor: Database cursor
:param user: ID of current user
:param product_data: Category Data from magento
:param context: Application context
:returns: Browse record of product found or None
"""
magento_product_obj = self.pool.get('magento.website.product')
record_ids = magento_product_obj.search(cursor, user, [
('magento_id', '=', int(product_data['product_id'])),
('website', '=', context['magento_website'])
], context=context)
return record_ids and magento_product_obj.browse(
cursor, user, record_ids[0], context=context
).product or None
def update_from_magento(
self, cursor, user, product, context=None
):
"""Update product using magento ID for that product
:param cursor: Database cursor
:param user: ID of current user
:param product: Browse record of product to be updated
:param context: Application context
:returns: Browse record of product updated
"""
website_obj = self.pool.get('magento.instance.website')
magento_product_obj = self.pool.get('magento.website.product')
website = website_obj.browse(
cursor, user, context['magento_website'], context=context
)
instance = website.instance
with magento.Product(
instance.url, instance.api_user, instance.api_key
) as product_api:
magento_product_id, = magento_product_obj.search(
cursor, user, [
('product', '=', product.id),
('website', '=', website.id),
], context=context
)
magento_product = magento_product_obj.browse(
cursor, user, magento_product_id, context=context
)
product_data = product_api.info(magento_product.magento_id)
return self.update_from_magento_using_data(
cursor, user, product, product_data, context
)
def extract_product_values_from_data(self, product_data):
"""Extract product values from the magento data
These values are used for creation/updation of product
:param product_data: Product Data from magento
:return: Dictionary of values
"""
return {
'name': product_data['name'],
'default_code': product_data['sku'],
'description': product_data['description'],
'list_price': float(
product_data.get('special_price') or
product_data.get('price') or 0.00
),
'standard_price': float(product_data.get('price') or 0.00),
}
def update_from_magento_using_data(
self, cursor, user, product, product_data, context=None
):
"""Update product using magento data
:param cursor: Database cursor
:param user: ID of current user
:param product: Browse record of product to be updated
:param product_data: Product Data from magento
:param context: Application context
:returns: Browse record of product updated
"""
product_values = self.extract_product_values_from_data(product_data)
self.write(cursor, user, product.id, product_values, context=context)
# Rebrowse the record
product = self.browse(cursor, user, product.id, context=context)
return product
def create_using_magento_data(
self, cursor, user, product_data, context=None
):
"""Create product using magento data
:param cursor: Database cursor
:param user: ID of current user
:param product_data: Product Data from magento
:param context: Application context
:returns: Browse record of product created
"""
category_obj = self.pool.get('product.category')
website_obj = self.pool.get('magento.instance.website')
# Get only the first category from list of categories
# If not category is found, put product under unclassified category
# which is created by default data
if product_data.get('categories'):
category_id = category_obj.find_or_create_using_magento_id(
cursor, user, int(product_data['categories'][0]),
context=context
).id
else:
category_id, = category_obj.search(cursor, user, [
('name', '=', 'Unclassified Magento Products')
], context=context)
product_values = self.extract_product_values_from_data(product_data)
product_values.update({
'categ_id': category_id,
'uom_id':
website_obj.get_default_uom(
cursor, user, context
).id,
'magento_product_type': product_data['type'],
'procure_method': product_values.get(
'procure_method', 'make_to_order'
),
'magento_ids': [(0, 0, {
'magento_id': int(product_data['product_id']),
'website': context['magento_website'],
})]
})
if product_data['type'] == 'bundle':
# Bundles are produced
product_values['supply_method'] = 'produce'
product_id = self.create(cursor, user, product_values, context=context)
return self.browse(cursor, user, product_id, context=context)
def get_product_values_for_export_to_magento(
self, product, categories, websites, context
):
"""Creates a dictionary of values which have to exported to magento for
creating a product
:param product: Browse record of product
:param categories: List of Browse record of categories
:param websites: List of Browse record of websites
:param context: Application context
"""
return {
'categories': map(
lambda mag_categ: mag_categ.magento_id,
categories[0].magento_ids
),
'websites': map(lambda website: website.magento_id, websites),
'name': product.name,
'description': product.description or product.name,
'short_description': product.description or product.name,
'status': '1',
'weight': product.weight_net,
'visibility': '4',
'price': product.lst_price,
'tax_class_id': '1',
}
def export_to_magento(self, cursor, user, product, category, context):
"""Export the given `product` to the magento category corresponding to
the given `category` under the current website in context
:param cursor: Database cursor
:param user: ID of current user
:param product: Browserecord of product to be exported
:param category: Browserecord of category to which the product has
to be exported
:param context: Application context
:return: Browserecord of product
"""
website_obj = self.pool.get('magento.instance.website')
website_product_obj = self.pool.get('magento.website.product')
if not category.magento_ids:
raise osv.except_osv(
_('Invalid Category!'),
_('Category %s must have a magento category associated') %
category.complete_name,
)
if product.magento_ids:
raise osv.except_osv(
_('Invalid Product!'),
_('Product %s already has a magento product associated') %
product.name,
)
if not product.default_code:
raise osv.except_osv(
_('Invalid Product!'),
_('Product %s has a missing code.') %
product.name,
)
website = website_obj.browse(
cursor, user, context['magento_website'], context=context
)
instance = website.instance
with magento.Product(
instance.url, instance.api_user, instance.api_key
) as product_api:
# We create only simple products on magento with the default
# attribute set
# TODO: We have to call the method from core API extension
# because the method for catalog create from core API does not seem
# to work. This should ideally be from core API rather than
# extension
magento_id = product_api.call(
'ol_catalog_product.create', [
'simple',
int(context['magento_attribute_set']),
product.default_code,
self.get_product_values_for_export_to_magento(
product, [category], [website], context
)
]
)
website_product_obj.create(cursor, user, {
'magento_id': magento_id,
'website': context['magento_website'],
'product': product.id,
}, context=context)
self.write(cursor, user, product.id, {
'magento_product_type': 'simple'
}, context=context)
return product
class MagentoWebsiteProduct(osv.Model):
"""Magento Website - Product store
This model keeps a record of a product's association with a website and
the ID of product on that website
"""
_name = 'magento.website.product'
_description = 'Magento Website - Product store'
_columns = dict(
magento_id=fields.integer(
'Magento ID', readonly=True, required=True, select=True,
),
website=fields.many2one(
'magento.instance.website', 'Magento Website', readonly=True,
select=True, required=True
),
product=fields.many2one(
'product.product', 'Product', readonly=True,
required=True, select=True
)
)
_sql_constraints = [
(
'magento_id_website_unique',
'unique(magento_id, website)',
'Each product in a website must be unique!'
),
]
def update_product_from_magento(self, cursor, user, ids, context):
"""Update the product from magento with the details from magento
for the current website
:param cursor: Database cursor
:param user: ID of current user
:param ids: Record IDs
:param context: Application context
"""
product_obj = self.pool.get('product.product')
for record in self.browse(cursor, user, ids, context=context):
context.update({
'magento_website': record.website.id,
})
product_obj.update_from_magento(
cursor, user, record.product, context
)
return {}
class ProductPriceTier(osv.Model):
"""Price Tiers for product
This model stores the price tiers to be used while sending
tier prices for a product from OpenERP to Magento.
"""
_name = 'product.price_tier'
_description = 'Price Tiers for product'
_rec_name = 'quantity'
def get_price(self, cursor, user, ids, name, _, context):
"""Calculate the price of the product for quantity set in record
:param cursor: Database cursor
:param user: ID of current user
:param ids: Records IDs
:param name: Nameo of field
:param context: Application context
"""
pricelist_obj = self.pool.get('product.pricelist')
store_obj = self.pool.get('magento.website.store')
res = {}
if not context.get('magento_store'):
return res
for tier in self.browse(cursor, user, ids, context=context):
store = store_obj.browse(
cursor, user, context['magento_store'], context=context
)
res[tier.id] = pricelist_obj.price_get(
cursor, user, [store.shop.pricelist_id.id], tier.product.id,
tier.quantity, context={
'uom': store.website.default_product_uom.id
}
)[store.shop.pricelist_id.id]
return res
_columns = dict(
product=fields.many2one(
'product.product', 'Product', required=True,
readonly=True,
),
quantity=fields.float(
'Quantity', digits_compute=dp.get_precision('Product UoS'),
required=True
),
price=fields.function(get_price, type='float', string='Price'),
)
_sql_constraints = [
('product_quantity_unique', 'unique(product, quantity)',
'Quantity in price tiers must be unique for a product'),
]