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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
|
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2010 Ting! <http://www.ting.es> <hugo.santos@ting.es>. All Rights Reserved
# d$
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import osv, fields
import netsvc
import ir
import time
import threading
import pooler
import simplejson as json
import httplib
import urllib
from types import ListType
from tools.translate import _
def get_spree_list(spree_domain, url, api_key, method='GET', params=None):
#
# spree_domain is the internet direction or domain of the spree commerce ex: spree.example.com or 127.0.0.1:3000
# url is the rest of the direcction to get or post the desired object. ex: /api/products
# api_key is the spree api key to get access to the store through the api
# method is the REST method to get, post or update data
# params is a list corresponding to object structure to post or update data
params = json.dumps(params)
headers = {"Content-type":"application/json",
"Accept":"application/json",
"X-SpreeAPIKey":str(api_key)}
conn = httplib.HTTPConnection(spree_domain)
conn.request(method, url, params, headers)
response = conn.getresponse()
status = response.status
reason = response.reason
data = response.read()
spree_list = None
try:
spree_list = json.loads(data)
except:
resp_list = []
conn.close()
if spree_list or spree_list == []:
return spree_list
else:
resp = [status, reason]
return resp
def get_country_id(cr, uid, country_iso, country_name):
pool = pooler.get_pool(cr.dbname)
country_ids = pool.get('res.country').search(cr, uid, ['|', ('code', '=', country_iso), ('name', '=', country_name)])
if country_ids:
return country_ids[0]
else:
country_id = pool.get('res.country').create(cr, uid, {'name': country_name,
'code': country_iso})
return country_id
def get_state_id(cr, uid, add_data, country_id):
pool = pooler.get_pool(cr.dbname)
if 'state' in add_data:
state_name = add_data['state']['name']
state_abbr = add_data['state']['abbr']
else:
state_name = add_data['state_name']
state_abbr = add_data['state_name']
state_ids = pool.get('res.country.state').search(cr, uid, [('name', 'like', state_name),
('country_id', '=', country_id)])
if state_ids:
return state_ids[0]
else:
state_id = pool.get('res.country.state').create(cr, uid, {'name': state_name,
'code': state_abbr,
'country_id': country_id})
return state_id
class esale_spree_web(osv.osv):
_name = 'esale.spree.web'
_description = 'Spree Website'
_columns = {
'name':fields.char('Name', size=64, required=True, readonly=False),
'url': fields.char('URL', size=128, required=True),
'products_ids':fields.one2many('esale.spree.product', 'website_id', 'Products', required=False),
'categories_ids':fields.one2many('esale.spree.category', 'website_id', 'Categories', required=False),
# 'customer_ids':fields.one2many('esale.spree.customer', 'website_id', 'Customers', required=False),
'sale_order_ids': fields.one2many('esale.spree.sale.order', 'website_id', 'Sale Orders', required=False),
'taxes_ids': fields.one2many('esale.spree.taxes', 'website_id', 'Taxes', required=False),
'api_key': fields.char('Api', size=128),
'product_sync_date': fields.datetime('Sync. Date'),
'taxon_sync_date': fields.datetime('Sync. Date'),
# 'customer_sync_date': fields.datetime('Sync. Date'),
'sale_order_sync_date': fields.datetime('Sync. Date'),
'tax_sync_date': fields.datetime('Sync. Date'),
'tax_included': fields.boolean('Tax Included In Price', help="If you have define spree prices with tax included you need to check this. Only works with sale_tax_include addon installed."),
}
def category_import_create(self, cr, uid, ids, *args):
category_obj = self.pool.get('product.category')
esale_category_obj = self.pool.get('esale.spree.category')
for website in self.browse(cr, uid, ids):
if not website.taxon_sync_date:
spree_url = "/api/taxons"
else:
sync_date = time.strftime('%Y-%m-%d', time.strptime(str(website.taxon_sync_date), '%Y-%m-%d %H:%M:%S'))
spree_url = "/api/taxons"
categories_spree = get_spree_list(website.url, str(spree_url), website.api_key)
for category_data in categories_spree:
category = category_data['taxon']
esale_category_ids = esale_category_obj.search(cr, uid, [('website_id', '=', website.id),
('spree_category_id', '=', category['id'])])
if esale_category_ids:
esale_category = esale_category_obj.browse(cr, uid, esale_category_ids[0])
oerp_category = esale_category.category_id.id
else:
oerp_category = category_obj.create(cr, uid, {'name': category['name']})
esale_category = esale_category_obj.create(cr, uid, {'name': category['name'],
'website_id': website.id,
'category_id': oerp_category,
'spree_category_id': category['id'],
})
#Write category parents to categories
for category_data in categories_spree:
category = category_data['taxon']
esale_category_ids = esale_category_obj.search(cr, uid, [(('spree_category_id'), '=', category['id'])])
if esale_category_ids:
oerp_category = esale_category_obj.browse(cr, uid, esale_category_ids[0]).category_id.id
esale_parent_id = esale_category_obj.search(cr, uid, [('spree_category_id', '=', category['parent_id'])])
if esale_parent_id and oerp_category:
esale_parent = esale_category_obj.browse(cr, uid, esale_parent_id[0])
category_obj.write(cr, uid, oerp_category, {'parent_id': esale_parent.category_id.id})
self.pool.get('esale.spree.web').write(cr, uid, website.id, {'taxon_sync_date': time.strftime('%Y-%m-%d %H:%M:%S')})
def taxes_import(self, cr, uid, ids, *args):
tax_obj = self.pool.get('account.tax')
for website in self.browse(cr, uid, ids):
if not website.tax_sync_date:
spree_url = "/api/tax_categories"
else:
sync_date = time.strftime('%Y-%m-%d', time.strptime(str(website.tax_sync_date), '%Y-%m-%d %H:%M:%S'))
spree_url = "/api/tax_categories?search[updated_at_gt]=%s" % sync_date
tax_categories_spree = get_spree_list(website.url, str(spree_url), website.api_key)
for tax_data in tax_categories_spree:
spree_tax = tax_data['tax_category']
if not spree_tax['tax_rates']:
continue
values = {
'name': spree_tax['name'],
'amount': spree_tax['tax_rates'][0]['amount'],
'tax_group': 'vat',
'type_tax_use': 'sale'
}
if website.tax_included:
values['price_include'] = True
tax_id = tax_obj.create(cr, uid, values)
esale_tax = self.pool.get('esale.spree.taxes').create(cr, uid, {'name': spree_tax['name'],
'website_id': website.id,
'spree_tax_id': spree_tax['id'],
'tax_id': tax_id})
self.pool.get('esale.spree.web').write(cr, uid, website.id, {'tax_sync_date': time.strftime('%Y-%m-%d %H:%M:%S')})
def _product_import(self, db_name, uid, ids, *args):
cr = pooler.get_db(db_name).cursor()
pool = pooler.get_pool(cr.dbname)
product_obj = pool.get('product.product')
esale_product_obj = pool.get('esale.spree.product')
esale_category_obj = pool.get('esale.spree.category')
for website in pool.get('esale.spree.web').browse(cr, uid, ids):
created_obj = 0
created_variant = 0
updated_obj = 0
updated_variant = 0
esale_category_ids = esale_category_obj.search(cr, uid, [])
if not esale_category_ids:
raise osv.except_osv(_('No categories'), _('There are no product categories. Please first import categories'))
if not website.product_sync_date:
spree_url = "/api/products"
else:
sync_date = time.strftime('%Y-%m-%d', time.strptime(str(website.product_sync_date), '%Y-%m-%d %H:%M:%S'))
spree_url = "/api/products?search[updated_at_gt]=%s" % sync_date
products_spree = get_spree_list(website.url, spree_url, website.api_key)
for product in products_spree:
info_prod = product['product']
#Search the category of the product
spree_url = "/api/products/%s" % str(info_prod['id'])
info_prod = get_spree_list(website.url, spree_url, website.api_key)
info_prod = info_prod['product']
taxon_id = 0
for taxon in info_prod['taxons']:
taxon_id = taxon['taxon_id']
esale_category_ids = esale_category_obj.search(cr, uid, [('website_id', '=', website.id), ('spree_category_id', '=', taxon_id)])
if esale_category_ids:
esale_category_id = esale_category_obj.browse(cr, uid, esale_category_ids[0]).category_id.id
else:
esale_category_id = pool.get('product.category').search(cr, uid, [])[0]
esale_product_ids = esale_product_obj.search(cr, uid, [('website_id', '=', website.id), ('spree_id', '=', info_prod['id'])])
if esale_product_ids:
esale_product_obj.write(cr, uid, esale_product_ids[0], {'name': info_prod['name'],
'spree_id': info_prod['master']['id'],
'spree_product_id': info_prod['id']})
esale_product = esale_product_obj.browse(cr, uid, esale_product_ids[0])
product_obj.write(cr, uid, esale_product.product_id.id, {'name': info_prod['name'],
'description': info_prod['description'],
'list_price': info_prod['master']['price'],
'standard_price': info_prod['master']['cost_price'],
'default_code': info_prod['master']['sku'],
'categ_id': esale_category_id
})
oerp_product = esale_product.product_id.id
cr.commit()
updated_obj += 1
else:
oerp_product = product_obj.create(cr, uid, {'name': info_prod['name'],
'description': info_prod['description'],
'list_price': info_prod['master']['price'],
'standard_price': info_prod['master']['cost_price'],
'default_code': info_prod['master']['sku'],
'categ_id': esale_category_id
})
esale_product = esale_product_obj.create(cr, uid, {'name': info_prod['name'],
'product_id': oerp_product,
'website_id': website.id,
'spree_id': info_prod['master']['id'],
'spree_product_id': info_prod['id']
})
cr.commit()
created_obj += 1
if 'variants' in info_prod:
for variant in info_prod['variants']:
esale_product_variant = esale_product_obj.search(cr, uid, [('website_id', '=', website.id),
('spree_master_id', '=', info_prod['master']['id']),
('spree_id', '=', variant['id'])])
if not esale_product_variant:
variant_name = " "
for options in variant['option_values']:
variant_name += options['option_type']['presentation'] + ": " + options['presentation'] + " "
product_variant_id = product_obj.create(cr, uid, {'name': info_prod['name'],
'description': info_prod['description'],
'list_price': variant['price'],
'standard_price': variant['cost_price'],
'default_code': variant['sku'],
'categ_id': esale_category_id,
'variants': variant_name,
'is_master': False,
'master_product_id': oerp_product,
})
esale_variant_name = info_prod['name'] + variant_name
esale_product_variant = esale_product_obj.create(cr, uid, {'name': esale_variant_name,
'product_id': product_variant_id,
'website_id': website.id,
'spree_id': variant['id'],
'master_product_id': oerp_product,
'spree_master_id': info_prod['master']['id'],
'spree_product_id': info_prod['id']
})
cr.commit()
created_variant += 1
request_body = """
Spree Product Download End
End of Download: %s
Created Master Products: %s
Updated Master Products: %s
Created Product Variants: %s
""" % (time.strftime('%Y-%m-%d %H:%M:%S'), created_obj, updated_obj, created_variant)
pool.get('esale.spree.web').write(cr, uid, website.id, {'product_sync_date': time.strftime('%Y-%m-%d %H:%M:%S')})
pool.get('res.request').create(cr, uid, {'name': 'Spree product download report',
'act_from': uid,
'act_to': uid,
'body': request_body})
cr.commit()
cr.close()
def _inventory_import(self, db_name, uid, ids, esale_products_ids=None, *args):
if esale_products_ids is None:
esale_products_ids = []
cr = pooler.get_db(db_name).cursor()
pool = pooler.get_pool(cr.dbname)
for website in pool.get('esale.spree.web').browse(cr, uid, ids):
created_obj = 0
exceptions = ""
if not esale_products_ids:
esale_products_ids = pool.get('esale.spree.product').search(cr, uid, [('website_id', '=', website.id)])
location_ids = pool.get('stock.location').search(cr, uid, [('name', '=', 'Stock')])
if esale_products_ids and location_ids:
location_id = location_ids[0]
stock_inventory_id = pool.get('stock.inventory').create(cr, uid, {'name':'Product Download'})
for esale_product_id in esale_products_ids:
esale_product = pool.get('esale.spree.product').browse(cr, uid, esale_product_id)
if esale_product.spree_master_id:
variant_url = "/api/products/%s/variants/%s" % (esale_product.spree_product_id, esale_product.spree_id)
variant_info = get_spree_list(website.url, variant_url, website.api_key)
variant = variant_info['variant']
pool.get('stock.inventory.line').create(cr, uid, {'inventory_id': stock_inventory_id,
'location_id': location_id,
'product_id': esale_product.product_id.id,
'product_uom': esale_product.product_id.uom_id.id,
'product_qty': variant['count_on_hand']
})
cr.commit()
created_obj += 1
else:
product_url = "/api/products/%s" % (esale_product.spree_product_id)
product_info = get_spree_list(website.url, product_url, website.api_key)
product = product_info['product']
pool.get('stock.inventory.line').create(cr, uid, {'inventory_id': stock_inventory_id,
'location_id': location_id,
'product_id': esale_product.product_id.id,
'product_uom': esale_product.product_id.uom_id.id,
'product_qty': product['master']['count_on_hand']})
cr.commit()
created_obj += 1
exceptions += "No Exceptions"
else:
exceptions += "Spree Downloaded Product doesn't exist. Please Download Products First"
request_body = """
Inventory Import End
End Date: %s
Succesfully created: %s
Exceptions: %s
""" % (time.strftime('%Y-%m-%d %H:%M:%S'), created_obj, exceptions)
pool.get('res.request').create(cr, uid, {'name': 'Spree Inventory Import report',
'act_from': uid,
'act_to': uid,
'body': request_body})
cr.commit()
cr.close()
def _inventory_export(self, db_name, uid, ids, esale_products_ids=None, *args):
if esale_products_ids is None:
esale_products_ids = []
cr = pooler.get_db(db_name).cursor()
pool = pooler.get_pool(cr.dbname)
for website in pool.get('esale.spree.web').browse(cr, uid, ids):
exceptions = ""
if not esale_products_ids:
esale_products_ids = pool.get('esale.spree.product').search(cr, uid, [('website_id', '=', website.id)])
if esale_products_ids:
for esale_product_id in esale_products_ids:
esale_product = pool.get('esale.spree.product').browse(cr, uid, esale_product_id)
product = pool.get('product.product').browse(cr, uid, esale_product.product_id.id)
qty = product.virtual_available
params = {'variant': {'count_on_hand': qty}}
url = "/api/variants/%s" % (esale_product.spree_id)
response = get_spree_list(website.url, url, website.api_key, "PUT", params)
if response[0] != 200:
exceptions += "Error Ocurred HTTP response: %s:%s " % (response[0], response[1])
if not exceptions:
exceptions += "No exceptions"
request_body = """
Inventory Export End
End Date: %s
Exceptions:
%s
""" % (time.strftime('%Y-%m-%d %H:%M:%S'), exceptions)
pool.get('res.request').create(cr, uid, {'name': 'Spree Inventory Export report',
'act_from': uid,
'act_to': uid,
'body': request_body})
cr.commit()
cr.close()
def _sale_order_import(self, db_name, uid, ids, *args):
cr = pooler.get_db(db_name).cursor()
pool = pooler.get_pool(cr.dbname)
sale_order_obj = pool.get('sale.order')
sale_order_line_obj = pool.get('sale.order.line')
partner_obj = pool.get('res.partner')
address_obj = pool.get('res.partner.address')
customer_address_obj = pool.get('esale.spree.customer.address')
picking_obj = pool.get('stock.picking')
#states for sale order. Key is spree state, value is openERP sale order state
order_state = {'new': 'draft',
'paid': 'progress',
'shipped': 'done',
'canceled': 'cancel'
}
picking_state = {'pending': 'draft',
'shipped': 'done',
'ready_to_ship': 'confirmed'
}
for website in pool.get('esale.spree.web').browse(cr, uid, ids):
created_obj = 0
updated_obj = 0
exceptions = ""
if not website.sale_order_sync_date:
spree_url = "/api/orders"
else:
sync_date = time.strftime('%Y-%m-%d', time.strptime(str(website.sale_order_sync_date), '%Y-%m-%d %H:%M:%S'))
spree_url = "/api/orders?search[updated_at_gt]=%s" % sync_date
order_list = get_spree_list(website.url, spree_url, website.api_key)
for order_data in order_list:
order_uri = "/api/orders/%s" % str(order_data['order']['id'])
order_info = get_spree_list(website.url, order_uri, website.api_key)
order = order_info['order']
if order['state'] == 'in_progress':
continue
esale_order_ids = pool.get('esale.spree.sale.order').search(cr, uid, [('website_id', '=', website.id), ('spree_sale_order_id', '=', order['id'])])
if not esale_order_ids:
bill_add_data = order['bill_address']
bill_name = bill_add_data['firstname'] + " " + bill_add_data['lastname']
bill_country_id = get_country_id(cr, uid, bill_add_data['country']['iso'],
bill_add_data['country']['name'])
bill_state_id = get_state_id(cr, uid, bill_add_data, bill_country_id)
#Search partner
bill_partner_ids = address_obj.search(cr, uid, [('name','=',bill_name),
('street','=',bill_add_data['address1']),
('street2','=',bill_add_data['address2']),
('country_id', '=', bill_country_id),
('state_id','=', bill_state_id)])
if bill_partner_ids:
bill_address_id = bill_partner_ids[0]
partner_id = address_obj.browse(cr, uid, bill_address_id).partner_id.id
else:
partner_id = partner_obj.create(cr, uid, {'name' : bill_name})
bill_address_id = address_obj.create(cr, uid, {'name' : bill_name,
'street' : bill_add_data['address1'],
'street2' : bill_add_data['address2'],
'city': bill_add_data['city'],
'zip': bill_add_data['zipcode'],
'state_id': bill_state_id,
'country_id': bill_country_id,
'phone': bill_add_data['phone'],
'mobile': bill_add_data['alternative_phone'],
'type': 'invoice',
'partner_id': partner_id})
#Search or create ship_address_id
ship_add_data = order['ship_address']
ship_name = ship_add_data['firstname'] + " " + ship_add_data['lastname']
ship_country_id = get_country_id(cr, uid, ship_add_data['country']['iso'],
ship_add_data['country']['name'])
ship_state_id = get_state_id(cr, uid, ship_add_data, bill_country_id)
ship_address_ids = address_obj.search(cr, uid, [('name','=',ship_name),
('street','=',ship_add_data['address1']),
('street2','=',ship_add_data['address2']),
('country_id', '=', ship_country_id),
('state_id','=', ship_state_id)])
if ship_address_ids:
ship_address_id = ship_address_ids[0]
else:
ship_address_id = address_obj.create(cr, uid, {'name' : ship_name,
'street' : ship_add_data['address1'],
'street2' : ship_add_data['address2'],
'city': ship_add_data['city'],
'zip': ship_add_data['zipcode'],
'state_id': ship_state_id,
'country_id': ship_country_id,
'phone': ship_add_data['phone'],
'mobile': ship_add_data['alternative_phone'],
'type': 'delivery',
'partner_id': partner_id})
#-------------------------------------------
#--------------ORDER CREATION---------------
#-------------------------------------------
order_values = {'partner_id': partner_id,
'date_order': order['created_at'],
'partner_invoice_id': bill_address_id,
'name': order['number'],
'partner_order_id': bill_address_id,
'partner_shipping_id': ship_address_id,
'pricelist_id': 1,
'state': order_state[order['state']]
}
if website.tax_included:
order_values['price_type'] = 'tax_included'
oerp_order_id = sale_order_obj.create(cr, uid, order_values)
for shipping in order['shipments']:
shipping_category_ids = pool.get('product.category').search(cr, uid, [('name', '=', 'Spree shipping')])
if not shipping_category_ids:
shipping_category_id = pool.get('product.category').create(cr, uid, {'name': 'Spree shipping'})
else:
shipping_category_id = shipping_category_ids[0]
if not 'shipping_method' in shipping:
shipping_name = "Shipping: " + str(shipping['shipping_method_id'])
else:
shipping_name = shipping['shipping_method']['name'] \
+ " Zone: " + str(shipping['shipping_method']['zone_id'])
shipping_product_ids = pool.get('product.product').search(cr, uid,
[('name', '=', shipping_name),
('categ_id', '=', shipping_category_id)])
shipping_cost = shipping['cost']
if not shipping_product_ids:
shipping_product_id = pool.get('product.product').create(cr, uid, {'name': shipping_name,
'categ_id': shipping_category_id,
'list_price': shipping_cost,
'type': 'service'})
else:
shipping_product_id = shipping_product_ids[0]
pick_state = picking_state[shipping['state']]
picking_id = picking_obj.create(cr, uid, {'name': "PACK: " + shipping['number'],
'origin': order['number'],
'address_id': ship_address_id,
'sale_id': oerp_order_id,
'type': 'out',
'date': shipping['created_at'],
'date_done': shipping['shipped_at'],
'state': pick_state,
})
cr.commit()
if shipping_product_id:
shipping_product_obj = pool.get('product.product').browse(cr, uid, shipping_product_id)
sale_order_line_obj.create(cr, uid, {'order_id': oerp_order_id,
'name': shipping_product_obj.name,
'product_id': shipping_product_id,
'price_unit': shipping_cost,
'product_uom_qty': 1,
'product_uom': shipping_product_obj.uom_id.id
})
order_lines = order['line_items']
for line in order_lines:
product_ids = pool.get('esale.spree.product').search(cr, uid, [('website_id', '=', website.id), ('spree_id', '=', line['variant_id'])])
if product_ids:
product_obj = pool.get('esale.spree.product').browse(cr, uid, product_ids[0]).product_id
else:
exceptions += """Error in line of spree order %s product %s not exist in OpenERP /n
Please re-import products to openERP.""" % (order['number'], line['description'])
continue
values = {
'order_id': oerp_order_id,
'product_id': product_obj.id,
'price_unit': line['price'],
'product_uom_qty': line['quantity'],
'product_uom': product_obj.uom_id.id,
'name': product_obj.name
}
spree_tax_id = self.pool.get('esale.spree.taxes').search(cr, uid, [('spree_tax_id', '=', line['variant']['tax_category_id'])])
if spree_tax_id:
spree_tax = self.pool.get('esale.spree.taxes').browse(cr, uid, spree_tax_id[0])
values['tax_id'] = [(6, 0, [spree_tax.tax_id.id])]
line_id = sale_order_line_obj.create(cr, uid, values)
if picking_id:
location_ids = pool.get('stock.location').search(cr, uid, [('name', '=', 'Stock')])
location_dest_ids = pool.get('stock.location').search(cr, uid, [('usage', '=', 'customer')])
if location_ids and location_dest_ids:
location_id = location_ids[0]
location_dest_id = location_dest_ids[0]
move_id = pool.get('stock.move').create(cr, uid, {'picking_id': picking_id,
'product_uom': product_obj.uom_id.id,
'product_qty': line['quantity'],
'location_id': location_id,
'location_dest_id': location_dest_id,
'name': product_obj.name,
'date_planned': order['created_at'],
'product_id': product_obj.id,
'state':pick_state,
})
cr.commit()
pool.get('esale.spree.sale.order').create(cr, uid, {'name': order['number'],
'website_id': website.id,
'spree_sale_order_id': order['id'],
'sale_order_id': oerp_order_id
})
created_obj += 1
cr.commit()
else:
order_id = pool.get('esale.spree.sale.order').browse(cr, uid, esale_order_ids[0]).sale_order_id.id
picking_ids = sale_order_obj.browse(cr, uid, order_id).picking_ids
sale_order_obj.write(cr, uid, order_id, {'state': order_state[order['state']]})
for picking_id in picking_ids:
pool.get('stock.picking').write(cr, uid, picking_id.id, {'state': picking_state[order['shipments'][0]['state']]})
for move_line in picking_id.move_lines:
pool.get('stock.move').write(cr, uid, move_line.id, {'state': picking_state[order['shipments'][0]['state']]})
updated_obj += 1
cr.commit()
if not exceptions:
exceptions = "No exceptions"
request_body = """
Sale Order Synchronization End.
End Date: %s
Created Orders: %s
Updated Orders: %s
Exceptions:
""" % (time.strftime('%Y-%m-%d %H:%M:%S'), created_obj, updated_obj)
request_body += exceptions
pool.get('esale.spree.web').write(cr, uid, website.id, {'sale_order_sync_date': time.strftime('%Y-%m-%d %H:%M:%S')})
pool.get('res.request').create(cr, uid, {'name':'Sale Order Sync. Report',
'act_from':uid,
'act_to':uid,
'body': request_body
})
cr.commit()
cr.close()
def sale_order_import(self, cr, uid, ids, *args):
threaded_proccess = threading.Thread(target=self._sale_order_import, args=(cr.dbname, uid, ids))
threaded_proccess.start()
return 'finish'
def product_import(self, cr, uid, ids, *args):
threaded_proccess = threading.Thread(target=self._product_import, args=(cr.dbname, uid, ids))
threaded_proccess.start()
return 'finish'
def inventory_import(self, cr, uid, ids, context=None, esale_product_ids=None):
threaded_proccess = threading.Thread(target=self._inventory_import, args=(cr.dbname, uid, ids, esale_product_ids))
threaded_proccess.start()
return 'finish'
def inventory_export(self, cr, uid, ids, esale_product_ids=None, *args):
threaded_proccess = threading.Thread(target=self._inventory_export(cr.dbname, uid, ids, esale_product_ids))
threaded_proccess.start()
return 'finish'
esale_spree_web()
class esale_spree_product(osv.osv):
_name = 'esale.spree.product'
_description = 'Spree Products'
_columns = {
'name':fields.char('Name', size=64, required=False, readonly=False),
'product_id':fields.many2one('product.product', 'Product', required=False),
'master_product_id': fields.many2one('product.product', 'Master Product', required=False),
'website_id':fields.many2one('esale.spree.web', 'Spree Website', required=False),
'spree_id': fields.integer('Spree Variant Id', required=True),
'spree_master_id': fields.integer('Spree Master Variant ID'),
'spree_product_id': fields.integer('Spree Product ID')
}
esale_spree_product()
class esale_spree_category(osv.osv):
_name = 'esale.spree.category'
_description = 'Spree Categories'
_columns = {
'name':fields.char('Name', size=64, required=False, readonly=False),
'website_id':fields.many2one('esale.spree.web', 'Spree Website', required=False),
'spree_category_id': fields.integer('Spree Category Id', required=True),
'category_id':fields.many2one('product.category', 'Openerp Category', required=False),
}
esale_spree_category()
class esale_spree_sale_order(osv.osv):
_name = 'esale.spree.sale.order'
_description = 'Spree Sale Orders'
_columns = {
'name': fields.char('Name', size=128),
'website_id': fields.many2one('esale.spree.web', 'Spree Website'),
'spree_sale_order_id': fields.integer('Spree Sale Order'),
'sale_order_id': fields.many2one('sale.order', 'Sale Order'),
}
esale_spree_sale_order()
class esale_spree_taxes(osv.osv):
_name = 'esale.spree.taxes'
_description = 'Spree Taxes'
_columns = {
'name': fields.char('Name', size=30),
'website_id': fields.many2one('esale.spree.web', 'Spree Website'),
'spree_tax_id': fields.integer('Spree Tax'),
'tax_id': fields.many2one('account.tax', 'Tax'),
}
esale_spree_taxes()
|