-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodels.py
More file actions
1005 lines (848 loc) · 44.8 KB
/
models.py
File metadata and controls
1005 lines (848 loc) · 44.8 KB
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
from openerp import models, fields, api, exceptions, _
from openerp.osv.orm import except_orm
from openerp.exceptions import UserError
from pprint import pprint
import json
import sys
from datetime import datetime, date, timedelta
from magento import MagentoAPI
import config
import logging
_logger = logging.getLogger(__name__)
# http://stackoverflow.com/questions/1305532/convert-python-dict-to-object (last one)
class dict2obj(dict):
def __init__(self, dict_):
super(dict2obj, self).__init__(dict_)
for key in self:
item = self[key]
if isinstance(item, list):
for idx, it in enumerate(item):
if isinstance(it, dict):
item[idx] = dict2obj(it)
elif isinstance(item, dict):
self[key] = dict2obj(item)
def __getattr__(self, key):
return self[key]
def __getstate__(self):
return self.__dict__.copy()
def __setstate__(self, state):
self.__dict__.update(state)
class ProductTemplate(models.Model):
_inherit = 'product.template'
magento_sync = fields.Boolean(string='Magento sync error')
magento_sync_date = fields.Date(string='Sync error date')
@api.multi
def write(self, vals):
result = super(ProductTemplate, self).write(vals)
data = {}
if 'list_price' in vals or 'extra_price' in vals:
syncid = self.env['syncid.reference'].search([('model', '=', 190), ('source', '=', 1), ('odoo_id', '=', self.id)])
if syncid and len(syncid) == 1:
# m = MagentoAPI(config.domain, config.port, config.user, config.key, proto=config.protocol)
if 'list_price' in vals:
data['price'] = vals['list_price']
if True:
# if syncid[0].source_id == '29436':
# print 'Entro exclusion DAVIDTESTSTOCK'
database = 'motoscoot_production'
motoscoot_db = self.env['base.external.dbsource'].search([('name', '=', database)])
# sql = 'update catalog_product_entity set value = %s where entity_id=%s and attribute_set_id=%s limit=%s;'
motoscoot_db.execute("""update catalog_product_entity_decimal set value = %s where entity_id=%s and attribute_id=%s;""", (vals['list_price'], int(syncid[0].source_id), 75), nodata=True)
if 'extra_price' in vals:
data['special_price'] = vals['extra_price'] or self.extra_price
if True:
# if syncid[0].source_id == '29436':
# print 'Entro exclusion DAVIDTESTSTOCK'
database = 'motoscoot_production'
motoscoot_db = self.env['base.external.dbsource'].search([('name', '=', database)])
# sql = 'update catalog_product_entity set value = %s where entity_id=%s and attribute_set_id=%s limit=%s;'
motoscoot_db.execute("""update catalog_product_entity_decimal set value = %s where entity_id=%s and attribute_id=%s;""", (vals['extra_price'], int(syncid[0].source_id), 76), nodata=True)
# m.catalog_product.update(syncid[0].source_id, data)
return result
class SaleOrder(models.Model):
_inherit = 'sale.order'
@api.multi
def action_cancel(self):
if 'MAG' in self.name and self.state == 'draft':
_logger.info('*** Canceling Magento Order...')
m = MagentoAPI(config.domain, config.port, config.user, config.key, proto=config.protocol)
magento_id = self.name[4:]
order = m.sales_order.info(magento_id)
if order:
if order['status'] != 'canceled':
order = m.sales_order.cancel(magento_id)
self.write({'state': 'cancel'})
@api.multi
def action_confirm(self):
res = super(SaleOrder, self).action_confirm()
if 'MAG' in self.name and self.state == 'sale':
m = MagentoAPI(config.domain, config.port, config.user, config.key, proto=config.protocol)
magento_id = self.name[4:]
order = m.sales_order.addComment(magento_id, 'processing', 'En proceso')
@api.multi
def update_magento_orders(self):
_logger.info('*** Updating Magento Orders from tree view')
m = MagentoAPI(config.domain, config.port, config.user, config.key, proto=config.protocol)
con = 1
for item in self:
_logger.info('*** Updating %s/%s' % (con, len(self)))
con +=1
if 'MAG' in item.name and item.state == 'draft':
magento_id = item.name[4:]
order = m.sales_order.info({'increment_id': magento_id})
_logger.info('*** %s' % order['increment_id'])
if order['status_history']:
note = '===============================\n'
for j in order['status_history']:
note += 'created_at: '+ j['created_at']
note += '\nentity_name: '+ j['entity_name']
note += '\nstatus: '+ j['status']
note += '\ncomment:'+ str(j['comment'])
note += '\n===============================\n'
if item.note != note:
item.note = note
# task to schedule
class magento_task(models.Model):
_name = 'magento.task'
#SALEORDERS
#SALEORDERS
@api.model
def get_bom_product(self, product_ids):
res = None
product_ids.sort()
boms =[obj.bom_id for obj in self.env['mrp.bom.line'].search([('product_id', '=', product_ids[0])])]
for bom in boms:
# _logger.info('*** ', bom, boms)
bom_products = [obj.product_id.id for obj in bom.bom_line_ids]
if product_ids == sorted(bom_products):
res = bom.product_id
# _logger.info('*** bingo!', res)
break
return res
@api.model
def create_syncid_data(self, odoo_id, magento_id, scope=None):
syncid_data = {}
syncid_data['model'] = 80 #res.partner model
syncid_data['source'] = 1 #syncid magento source
syncid_data['odoo_id'] = odoo_id
syncid_data['source_id'] = magento_id
syncid_data['scope'] = scope
res = self.env['syncid.reference'].create(syncid_data)
@api.model
def create_partner_address(self, data, partner_id, mode, address_id=None, scope=None):
#method to create a delivery or invoice address given magento address data
#dictionary to match spanish region_id with odoo state code
region_states_spain = {
130: '15', #A coruña
131: '01', #Alava
132: '02', #Albacete
133: '03', #Alicante
134: '04', #Almeria
135: '33', #Asturias
136: '05', #Avila
137: '06', #Badajoz
138: '07', #Baleares
139: '08', #Barcelona
140: '09', #Burgos
141: '10', #Caceres
142: '11', #Cadiz
143: '39', #Cantabria
144: '12', #Castellon
145: '51', #Ceuta
146: '13', #Ciudad Real
147: '14', #Cordoba
148: '16', #Cuenca
149: '17', #Girona
150: '18', #Granada
151: '19', #Guadalajara
152: '20', #Guipuzcoa
153: '21', #Huelva
154: '22', #Huesca
155: '23', #Jaen
156: '26', #La rioja
157: '35', #Las Palmas
158: '24', #Leon
159: '25', #Lleida
160: '27', #Lugo
161: '28', #Madrid
162: '29', #Malaga
163: '52', #Melilla
164: '30', #Murcia
165: '31', #Navarra
166: '32', #Ourense
167: '34', #Palencia
168: '36', #Pontevedra
169: '37', #Salamanca
170: '38', #Santa Cruz de Tenerife
171: '40', #Segovia
172: '41', #Sevilla
173: '42', #Soria
174: '43', #Tarragona
175: '44', #Teruel
176: '45', #Toledo
177: '46', #Valencia
178: '47', #Valladolid
179: '48', #Vizcaya
180: '49', #Zamora
181: '50', #Zaragoza
}
address_data = {}
address_data['name'] = data['firstname'] + ' ' + data['lastname']
address_data['street'] = data['street'].replace("\n", " ")
address_data['city'] = data['city']
address_data['zip'] = data['postcode']
address_data['phone'] = data['telephone']
address_data['email'] = data['email']
address_data['active'] = True
address_data['customer'] = False
address_data['parent_id'] = partner_id
country = self.env['res.country'].search([('code', '=', data['country_id'])])
if country:
address_data['country_id'] = country[0].id
if data['country_id'] == 'ES':
state_code = region_states_spain[int(data['region_id'])]
state = self.env['res.country.state'].search([('code', '=', state_code)])
if state:
address_data['state_id'] = state[0].id
if data['address_type'] == 'billing':
address_data['type'] = 'invoice'
elif data['address_type'] == 'shipping':
address_data['type'] = 'delivery'
if mode == 'create':
res = self.env['res.partner'].create(address_data)
#create syncid reference
if scope != None:
res_syncid = self.create_syncid_data(res, data['customer_address_id'], scope)
else:
res_syncid = self.create_syncid_data(res, data['address_id'], scope)
else:
address_data['id'] = address_id
res = self.env['res.partner'].write(address_data)
self.env.cr.commit()
return address_id or res
@api.model
def create_partner(self, data, scope=None):
#method to create basic partner data
#TODO: maybe add more accurate data in address
customer_tags = {
'0': None, #NOT LOGGED IN
'1':1, #General
'4':2, #Piloto
'6':3, #Taller
'7':4, #Taler NO VAT
'8':5, #TTQ
'9':11,#TTQ NO VAT
}
address_data = {}
address_data['name'] = (data['customer_firstname'] or '') + ' ' + (data['customer_lastname'] or '')
# address_data['street'] = data['street']
# address_data['city'] = data['city']
# address_data['zip'] = data['postcode']
address_data['phone'] = data['billing_address']['telephone']
address_data['email'] = data['customer_email']
address_data['active'] = True
address_data['customer'] = True
address_data['category_id'] = [(6, 0, [customer_tags[data['customer_group_id']]])]
# if 'vat_id' in data['billing_address']:
# if data['billing_address']['vat_id']:
# address_data['vat'] = data['billing_address']['vat_id']
res = self.env['res.partner'].create(address_data)
res_syncid = self.create_syncid_data(res, data['customer_id'], scope)
self.env.cr.commit()
return res
#UPDATE SALEORDERS
#UPDATE SALEORDERS
@api.model
def update_orders_from_magento(self):
reload(sys)
sys.setdefaultencoding("utf-8")
# check config and do nothing if it's missing some parameter
if not config.domain or \
not config.port or \
not config.user or \
not config.key or \
not config.protocol:
return
#testing
_logger.info('*** Fetching magento orders to update...')
m = MagentoAPI(config.domain, config.port, config.user, config.key, proto=config.protocol)
order_filter = {'created_at':{'from': date.today().strftime('%Y-%m-%d')}}
#fetch a list of magent orders from date
orders = m.sales_order.list(order_filter)
#filter orders to process state in ['new', 'processing']
m_orders_list = []
for i in orders:
if i['state'] in ['new', 'processing', 'payment_review']:
m_orders_list.append('MAG-'+i['increment_id'])
elif i['state'] in ['pending_payment']:
order = m.sales_order.info({'increment_id': i['increment_id']})
if order:
if order['payment']:
if order['payment']['method'] == 'paypal_standard':
m_orders_list.append('MAG-'+i['increment_id'])
elif i['state'] in ['complete'] and i['status'] in ['processing']:
order = m.sales_order.info({'increment_id': i['increment_id']})
if order:
if order['payment']:
if order['payment']['method'] == 'multibanco':
m_orders_list.append('MAG-'+i['increment_id'])
#check which sale orders are allready imported in odoo
orders_to_update = []
for i in m_orders_list:
o_saleorder = self.env['sale.order'].search([('name', '=', i)])
if o_saleorder:
orders_to_update.append(i)
_logger.info('*** Total orders to update %i' % len(orders_to_update))
for i in orders_to_update:
_logger.info('*** Processing... %s' % i)
#checking sale order
odoo_order = self.env['sale.order'].search([('name', '=', i),('state', '=', 'draft')])
if odoo_order:
#updating history...
order = m.sales_order.info({'increment_id': i[4:]})
if order['status_history']:
note = '===============================\n'
for j in order['status_history']:
note += 'created_at: '+ j['created_at']
note += '\nentity_name: '+ j['entity_name']
note += '\nstatus: '+ j['status']
note += '\ncomment:'+ str(j['comment'])
note += '\n===============================\n'
if odoo_order.note != note:
odoo_order.note = note
#SYNC SALEORDERS
#SYNC SALEORDERS
@api.model
def sync_orders_from_magento(self):
reload(sys)
sys.setdefaultencoding("utf-8")
# check config and do nothing if it's missing some parameter
if not config.domain or \
not config.port or \
not config.user or \
not config.key or \
not config.protocol:
return
#testing
_logger.info('*** Fetching magento orders...')
m = MagentoAPI(config.domain, config.port, config.user, config.key, proto=config.protocol)
S_IVA_21S = self.env['account.tax'].search([('description', '=', 'S_IVA21B')])
PRODUCT_UOM = self.env['product.uom'].search([('id','=',1)]).id
#first get de date range to check orders
#TODO: today minus 10 days for safe check
# order_filter = {'created_at':{'from': date.today().strftime('%Y-%m-%d')}}
# order_filter = {'created_at':{'from': '2017-10-15'}}
order_filter = {'created_at':{'from': (date.today()-timedelta(7)).strftime('%Y-%m-%d')}}
#fetch a list of magent orders from date
orders = m.sales_order.list(order_filter)
#filter orders to process state in ['new', 'processing']
m_orders_list = []
for i in orders:
if i['state'] in ['new', 'processing', 'payment_review']:
m_orders_list.append('MAG-'+i['increment_id'])
elif i['state'] in ['pending_payment']:
order = m.sales_order.info({'increment_id': i['increment_id']})
if order:
if order['payment']:
if order['payment']['method'] == 'paypal_standard':
m_orders_list.append('MAG-'+i['increment_id'])
elif i['state'] in ['complete'] and i['status'] in ['processing']:
order = m.sales_order.info({'increment_id': i['increment_id']})
if order:
if order['payment']:
if order['payment']['method'] == 'multibanco':
m_orders_list.append('MAG-'+i['increment_id'])
#check which sale orders are allready imported in odoo
orders_to_process = []
orders_to_check = []
for i in m_orders_list:
o_saleorder = self.env['sale.order'].search([('name', '=', i)])
if not o_saleorder:
if order['customer_id']:
orders_to_process.append(i)
else:
orders_to_check.append(i)
_logger.info('*** ORDERS TO CHECK %s' % orders_to_check)
#processing sale orders:
_logger.info('*** Total orders to process %s' % len(orders_to_process))
for i in orders_to_process:
_logger.info('*** Processing... %s' % i)
#fetching order info
order = m.sales_order.info({'increment_id': i[4:]})
#checking partner, invoice address and shipping address
#if not exist on odoo, create it!
#TODO:partner
m_customer_id = order['customer_id']
#new clients hack to prevent bad assign while cleaning
im_customer_id = int(order['customer_id'])
if im_customer_id > 63972:
#its a new client, use the new logic
syncid_customer = self.env['syncid.reference'].search([('scope', '=', 'client'),('source','=',1),('model','=',80),('source_id','=',m_customer_id)])
if syncid_customer:
o_customer_id = syncid_customer[0].odoo_id
else:
o_customer_id = self.create_partner(order, 'client').id
#TODO:billing
m_billing_address_id = order['billing_address']['customer_address_id']
if m_billing_address_id != None:
syncid_billing = self.env['syncid.reference'].search([('scope', '=', 'billing'),('source','=',1),('model','=',80),('source_id','=',m_billing_address_id)])
if syncid_billing:
o_billing_id = self.create_partner_address(order['billing_address'], o_customer_id, 'update', syncid_billing[0].odoo_id)
else:
o_billing_id = self.create_partner_address(order['billing_address'], o_customer_id, 'create', None, 'billing').id
else:
o_billing_id = o_customer_id
#TODO:shipping
m_shipping_addess_id = order['shipping_address']['customer_address_id']
if m_shipping_addess_id != None:
syncid_shipping = self.env['syncid.reference'].search([('scope', '=', 'shipment'),('source','=',1),('model','=',80),('source_id','=',m_shipping_addess_id)])
if syncid_shipping:
o_shipping_id = self.create_partner_address(order['shipping_address'], o_customer_id, 'update', syncid_shipping[0].odoo_id)
else:
o_shipping_id = self.create_partner_address(order['shipping_address'], o_customer_id, 'create', None, 'shipment').id
else:
o_shipping_id = o_customer_id
else:#old way
syncid_customer = self.env['syncid.reference'].search([('scope','=', None),('source','=',1),('model','=',80),('source_id','=',m_customer_id)])
if syncid_customer:
o_customer_id = syncid_customer[0].odoo_id
else:
o_customer_id = self.create_partner(order).id
#TODO:billing
m_billing_address_id = order['billing_address_id']
syncid_billing = self.env['syncid.reference'].search([('scope','=', None),('source','=',1),('model','=',80),('source_id','=',m_billing_address_id)])
if syncid_billing:
o_billing_id = self.create_partner_address(order['billing_address'], o_customer_id, 'update', syncid_billing[0].odoo_id)
else:
o_billing_id = self.create_partner_address(order['billing_address'], o_customer_id, 'create', None).id
#TODO:shipping
m_shipping_addess_id = order['shipping_address_id']
syncid_shipping = self.env['syncid.reference'].search([('scope','=', None),('source','=',1),('model','=',80),('source_id','=',m_shipping_addess_id)])
if syncid_shipping:
o_shipping_id = self.create_partner_address(order['shipping_address'], o_customer_id, 'update', syncid_shipping[0].odoo_id)
else:
o_shipping_id = self.create_partner_address(order['shipping_address'], o_customer_id, 'create', None).id
#Create sale order:
saleorder_data = {}
saleorder_data['name'] = i
saleorder_data['partner_id'] = o_customer_id
saleorder_data['partner_invoice_id'] = o_billing_id
saleorder_data['partner_shipping_id'] = o_shipping_id
saleorder_data['date_order'] = datetime.strptime(order['created_at'], '%Y-%m-%d %H:%M:%S')
#TODO: add payment_mode info to saleorder
o_saleorder = self.env['sale.order'].create(saleorder_data)
#Create sale order lines data:
bundle_product = None
to_ignore = []
configurable = {}
for line in order['items']:
if line['item_id'] in to_ignore:
continue
saleorder_line_data = {}
saleorder_line_data['price_unit'] = 0
saleorder_line_data['order_id'] = o_saleorder.id
saleorder_line_data['product_uom'] = PRODUCT_UOM
saleorder_line_data['product_uom_qty'] = int(float(line['qty_ordered']))
saleorder_line_data['tax_id'] = [(6, 0, [S_IVA_21S.id])]
#simple, configurable and bundle logic
if line['product_type'] == 'bundle':
bundle_ok = False
bundles_parts = []
to_ignore.append(line['item_id'])
#search all the items of this bundle in the order
for bline in order['items']:
if bline['parent_item_id'] == line['item_id']:
p = self.env['product.product'].search([('default_code', '=', bline['sku']),('active','=',True)])
if p:
bundles_parts.append(p.id)
to_ignore.append(bline['item_id'])
#search for the proper bom and result product and save it
if bundles_parts:
_logger.info('*** entro if bundles_parts')
bundle_product = self.get_bom_product(bundles_parts)
# _logger.info('*** vuelvo get_bom_product', bundle_product)
if bundle_product:
saleorder_line_data['price_unit'] = line['base_original_price']
saleorder_line_data['product_id'] = bundle_product.id
if bundle_product.default_code:
saleorder_line_data['name'] = '[%s] %s' % (bundle_product.default_code, line['name'])
else:
saleorder_line_data['name'] = line['name']
o_saleorder_line = self.env['sale.order.line'].create(saleorder_line_data)
bundle_ok = True
#if everything went OK skip following logic
if bundle_ok:
continue
#else save a sync-error line
else:
saleorder_line_data['product_id'] = 15414 #sync-error product
saleorder_line_data['name'] = '[BUNDLE] ' + line['name']
o_saleorder_line = self.env['sale.order.line'].create(saleorder_line_data)
continue
if line['product_type'] == 'configurable':
configurable[line['item_id']] = float(line['base_original_price'])
continue
if line['product_type'] == 'simple':
if line['parent_item_id']:
if line['parent_item_id'] in configurable:
saleorder_line_data['price_unit'] = configurable[line['parent_item_id']]
else:
if line['base_original_price']:
saleorder_line_data['price_unit'] = float(line['base_original_price'])
else:
if line['base_original_price']:
saleorder_line_data['price_unit'] = float(line['base_original_price'])
product = self.env['product.product'].search([('default_code', '=', line['sku']),('active','=',True)])
if product:
saleorder_line_data['product_id'] = product.id
if product.default_code:
saleorder_line_data['name'] = '[%s] %s' % (product.default_code, line['name'])
else:
saleorder_line_data['name'] = line['name']
else:
saleorder_line_data['product_id'] = 15414 #sync-error product
saleorder_line_data['name'] = line['name']
# saleorder_line_data['name'] = line['name']
o_saleorder_line = self.env['sale.order.line'].create(saleorder_line_data)
#check cod_fee & shipment fee and add it as products
if order['cod_fee']:
saleorder_line_data = {}
saleorder_line_data['order_id'] = o_saleorder.id
saleorder_line_data['name'] = 'Contrarembolso'
saleorder_line_data['product_uom'] = PRODUCT_UOM
saleorder_line_data['product_id'] = 15413 #product 'gastos de envio'
saleorder_line_data['product_uom_qty'] = 1
saleorder_line_data['price_unit'] = float(order['cod_fee'])
saleorder_line_data['tax_id'] = [(6, 0, [S_IVA_21S.id])]
o_saleorder_line = self.env['sale.order.line'].create(saleorder_line_data)
if order['shipping_amount']:
saleorder_line_data = {}
saleorder_line_data['order_id'] = o_saleorder.id
saleorder_line_data['product_uom'] = PRODUCT_UOM
saleorder_line_data['name'] = 'Gastos de envio'
saleorder_line_data['product_id'] = 15413 #product 'gastos de envio'
saleorder_line_data['product_uom_qty'] = 1
saleorder_line_data['price_unit'] = float(order['shipping_amount'])
saleorder_line_data['tax_id'] = [(6, 0, [S_IVA_21S.id])]
o_saleorder_line = self.env['sale.order.line'].create(saleorder_line_data)
#adding payment_mode_id
if order['payment']:
payment_method = self.env['account.payment.mode'].search([('name','=', order['payment']['method'])])
# print payment_method, order['payment']['method']
if payment_method:
o_saleorder.payment_mode_id = payment_method[0]
#adding order comments:
if order['status_history']:
note = '===============================\n'
for j in order['status_history']:
note += 'created_at: '+ j['created_at']
note += '\nentity_name: '+ j['entity_name']
note += '\nstatus: '+ j['status']
note += '\ncomment:'+ str(j['comment'])
note += '\n===============================\n'
o_saleorder.note = note
if order['discount_description']:
saleorder_line_data = {}
saleorder_line_data['order_id'] = o_saleorder.id
saleorder_line_data['product_uom'] = PRODUCT_UOM
saleorder_line_data['name'] = 'Vale web - ' + order['discount_description']
saleorder_line_data['product_id'] = 16716 #product 'VALE WEB'
saleorder_line_data['product_uom_qty'] = 1
saleorder_line_data['price_unit'] = float(order['discount_amount'])
saleorder_line_data['tax_id'] = [(6, 0, [S_IVA_21S.id])]
o_saleorder_line = self.env['sale.order.line'].create(saleorder_line_data)
if order['money_for_points']:
saleorder_line_data = {}
saleorder_line_data['order_id'] = o_saleorder.id
saleorder_line_data['product_uom'] = PRODUCT_UOM
saleorder_line_data['name'] = 'Puntos web ' + str(float(order['money_for_points'])) + ' puntos'
saleorder_line_data['product_id'] = 21653 #product 'PUNTOS WEB'
saleorder_line_data['product_uom_qty'] = 1
saleorder_line_data['price_unit'] = float(order['money_for_points'])
saleorder_line_data['tax_id'] = [(6, 0, [S_IVA_21S.id])]
o_saleorder_line = self.env['sale.order.line'].create(saleorder_line_data)
#PRODUCT BRAND
#PRODUCT BRAND
@api.model
def sync_brands_from_magento(self):
reload(sys)
sys.setdefaultencoding("utf-8")
# check config and do nothing if it's missing some parameter
if not config.domain or \
not config.port or \
not config.user or \
not config.key or \
not config.protocol:
return
#testing
_logger.info('*** Fetching magento brands...')
m = MagentoAPI(config.domain, config.port, config.user, config.key, proto=config.protocol)
magento_brands = m.catalog_product_attribute.info('manufacturer')['options']
for b in magento_brands:
reference = self.env['syncid.reference'].search([('model', '=', 329), ('source', '=', 1), ('source_id', '=' ,b['value'])])
if not reference:
#create brand and syncid
_logger.info('*** creating new brand! %s' % b['label'])
data = {
'name': b['label'],
}
o_brand = self.env['product.brand'].create(data)
data_sync = {
'model': 329,
'source': 1,
'odoo_id': o_brand.id,
'source_id': b['value']
}
o_syncidreference = self.env['syncid.reference'].create(data_sync)
#PRODUCT CATEGORY
#PRODUCT CATEGORY
@api.model
def sync_categorys_from_magento(self):
reload(sys)
sys.setdefaultencoding("utf-8")
# check config and do nothing if it's missing some parameter
if not config.domain or \
not config.port or \
not config.user or \
not config.key or \
not config.protocol:
return
def read_children(item):
if item.has_key('children'):
#print 'item', item['name'], item['category_id']
categories[int(item['category_id'])] = {
'id': int(item['category_id']),
'name': item['name'],
'parent': int(item['parent_id']),
'item': item
}
read_category(item['children'], item)
def read_category(data, parent=None):
if type(data) is list:
for item in data:
read_children(item)
else:
read_children(data)
#testing
_logger.info('*** Fetching magento categorys...')
m = MagentoAPI(config.domain, config.port, config.user, config.key, proto=config.protocol)
# read categories
categories = {}
read_category(m.catalog_category.tree())
# sync categories in Odoo
for i in sorted([i for i in categories.keys()]):
# check syncid referente before creation
reference = self.env['syncid.reference'].search([('model', '=', 184), ('source', '=', 1), ('source_id', '=', i)])
if len(reference) > 1:
raise SystemExit('Category with many references: %s', categories[i]['name'])
if not reference:
data = {
'name': categories[i]['name'],
'active': True,
}
if categories[i]['parent']:
data['parent_id'] = self.env['syncid.reference'].search([('model', '=', 184), ('source', '=', 1), ('source_id', '=', categories[i]['parent'])])[0].odoo_id
_logger.info('*** ** %s %s %s %s' % (categories[i]['id'], categories[i]['name'], categories[i]['parent'], data))
category_id = self.env['product.category'].create(data)
data_sync = {
'model': 184,
'source': 1,
'odoo_id': category_id.id,
'source_id': i
}
sync_id = self.env['syncid.reference'].create(data_sync)
_logger.info('*** new category... %s %s' % (categories[i]['name'], sync_id))
#PRODUCT SYNC
#PRODUCT SYNC
@api.model
def sync_products_from_magento(self):
reload(sys)
sys.setdefaultencoding("utf-8")
# check config and do nothing if it's missing some parameter
if not config.domain or \
not config.port or \
not config.user or \
not config.key or \
not config.protocol:
return
#testing
_logger.info('*** Fetching magento products...')
m = MagentoAPI(config.domain, config.port, config.user, config.key, proto=config.protocol)
reference = self.env['syncid.reference'].search([('model', '=', 190), ('source', '=', 1)], order='id desc')
magento_filter = {'product_id':{'from':reference[0].source_id}}
magento_products = m.catalog_product.list(magento_filter)
_logger.info('*** working...')
con = 1
for p in magento_products:
_logger.info('*** %s' % p['product_id'])
if con % 500 == 0:
_logger.info('*** Syncing magento products (%i - %i)' % (con, len(magento_products)))
con +=1
reference = self.env['syncid.reference'].search([('model', '=', 190), ('source', '=', 1), ('source_id', '=' ,p['product_id'])])
if not reference and p['type'] == 'simple':
#create product and syncid
_logger.info('*** creating new product! %s' % p['name'])
pp = m.catalog_product.info(p['product_id'])
categ_ids = []
categ_id = None
for i in pp['category_ids']:
if i not in ['169']: # error in magento database
category = self.env['syncid.reference'].search([('model', '=', 184), ('source', '=', 1), ('source_id', '=', str(i))])
if len(category) > 1:
raise SystemExit('Product with many categories in syncid: %s' % pp['name'])
elif category:
categ_ids.append((6, 0, [category[0].odoo_id]))
categ_id = category[0].odoo_id
data = {
'default_code': pp['sku'],
'name': pp['name'],
'sale_ok': True,
'purchase_ok': True,
'type': 'product',
'list_price': pp['price'],
'extra_price': pp['special_price'],
'categ_ids': categ_ids,
'categ_id': categ_id or 1,
}
if pp['manufacturer']:
product_brand_id = self.env['syncid.reference'].search([('model', '=', 329), ('source', '=', 1), ('source_id', '=', pp['manufacturer'])])
if product_brand_id:
data['product_brand_id'] = product_brand_id.odoo_id
o_product = self.env['product.template'].create(data)
data_sync = {
'model': 190,
'source': 1,
'odoo_id': o_product.id,
'source_id': pp['product_id'],
}
sync_id = self.env['syncid.reference'].create(data_sync)
_logger.info('*** FINISH')
#STOCK SYNC
#STOCK SYNC
class StockPicking(models.Model):
_inherit = 'stock.picking'
def write(self, cr, uid, ids, vals, context=None):
res = super(StockPicking, self).write(cr, uid, ids, vals, context=context)
if vals.get('carrier_tracking_ref'):
_logger.info('*** %s' % vals)
if vals.get('carrier_tracking_ref') != 'GENERATING...' and vals.get('carrier_tracking_ref'):
for i in self.browse(cr, uid, ids, context=context):
if i.origin and 'MAG' in i.origin and not i.carrier_file_generated:
_logger.info('*** Adding tracking to Magento Order... %s' % i.origin)
m = MagentoAPI(config.domain, config.port, config.user, config.key, proto=config.protocol)
magento_id = i.origin[4:]
try:
shipment = m.sales_order_shipment.create(magento_id)
track = m.sales_order_shipment.addTrack(int(shipment), 'custom', i.carrier_id.name, vals['carrier_tracking_ref'] )
except:
_logger.info('*** ++ Shipment ya existente en magento!! %s' % magento_id)
try:
invoice = m.sales_order_invoice.create(magento_id)
except:
_logger.info('*** ++ Factura ya existente en magento!! %s' % magento_id)
return res
#this controls de stock.moves when working with IN/OUT picking operations
class stock_move(models.Model):
_inherit = 'stock.move'
#inherited method to add MAGENTO STOCK SYNC when a IN/OUT picking operation is validated
def action_done(self, cr, uid, ids, context=None):
_logger.info('*** entering stock_move action_dome - magento update')
result = super(stock_move, self).action_done(cr, uid, ids,
context=context)
destination = 0
products_to_sync = []
products_stock_dict = {}
for move in self.browse(cr, uid, ids, context=context):
if move.picking_id:
destination = move.picking_id.location_dest_id.id
products_to_sync.append(move.product_id.product_tmpl_id.id)
products_stock_dict[move.product_id.product_tmpl_id.id] = move.product_id.qty_available
syncid_obj = self.pool.get("syncid.reference")
product_obj = self.pool.get("product.product")
if destination in [19, 12, 25, 8, 9, 5]:
#update magento stock!
m = MagentoAPI(config.domain, config.port, config.user, config.key, proto=config.protocol)
con = 1
for i in products_to_sync:
_logger.info('*** sync stock %s/%s - %s' % (con, len(products_to_sync), i))
con +=1
domain = [('model', '=', 190), ('source', '=', 1), ('odoo_id', '=' ,i)]
product_syncid_references = syncid_obj.search(cr, uid, domain, context=context)
if product_syncid_references:
product_syncid_reference = syncid_obj.browse(cr, uid, product_syncid_references, context=context)
is_in_stock = '0'
# print product_syncid_reference[0].source_id, products_stock_dict[i]
if products_stock_dict[i] > 0:
is_in_stock = '1'
# print 'is_in_stock', is_in_stock
try:
m.cataloginventory_stock_item.update(product_syncid_reference[0].source_id, {'qty':str(products_stock_dict[i]),'is_in_stock':is_in_stock})
except:
obj = product_syncid_reference[0].object()
raise UserError("Error syncing with magento! Product doesn't exist: %s - %s" % (obj.name, obj.default_code))
return True
return result
#This controls the inventory adjustment
class StockInventory(models.Model):
_inherit = "stock.inventory"
@api.multi
def check_products(self):
m = MagentoAPI(config.domain, config.port, config.user, config.key, proto=config.protocol)
error_msg = 'Products not found in sync table:\n'
error_sync = ''
error_mag = 'Products not found in magento:\n'
for inventory_line in self.line_ids:
domain = [('model', '=', 190), ('source', '=', 1), ('odoo_id', '=' ,inventory_line.product_id.product_tmpl_id.id)]
product_syncid_references = self.env['syncid.reference'].search(domain)
if not product_syncid_references:
error_sync += inventory_line.product_id.name + '\n'
else:
try:
magento_product = m.catalog_product.info(product_syncid_references[0].source_id)
except:
error_mag += inventory_line.product_id.name + ' - ' + inventory_line.product_id.id + ' - ' + product_syncid_references[0].source_id + '\n'
raise exceptions.Warning(error_msg + error_sync + error_mag)
#inherited method to add MAGENTO STOCK SYNC when a Inventory Adjustments is validated
def action_done(self, cr, uid, ids, context=None):
""" Finish the inventory
@return: True
"""
result = super(StockInventory, self).action_done(cr, uid, ids, context=context)
syncid_obj = self.pool.get("syncid.reference")
for inv in self.browse(cr, uid, ids, context=context):
_logger.info('*** Initiating sync stock inventory adjustment - %s to process...' % len(inv.line_ids))
con = 1
m = MagentoAPI(config.domain, config.port, config.user, config.key, proto=config.protocol)
m_check = False
m_plist = []
# if len(inv.line_ids) >1:
# print 'Multiple sync stock inventory adjunstment...Fetching catalog for checks'
# m_check = True
# m_products = m.catalog_product.list()
# for i in m_products:
# m_plist.append(int(i['product_id']))
# print 'Catalog properly fetched!'
for inventory_line in inv.line_ids:
_logger.info('*** Syncing inventory_line %s/%s - %s' % (con, len(inv.line_ids), inventory_line.product_id.id))
con +=1
domain = [('model', '=', 190), ('source', '=', 1), ('odoo_id', '=' ,inventory_line.product_id.product_tmpl_id.id)]
product_syncid_references = syncid_obj.search(cr, uid, domain, context=context)
if product_syncid_references:
product_syncid_reference = syncid_obj.browse(cr, uid, product_syncid_references, context=context)
is_in_stock = '0'
if inventory_line.product_id.qty_available > 0:
is_in_stock = '1'
#add error tolerance
# if not m_check:
if product_syncid_reference:
#found! update it!
m.cataloginventory_stock_item.update(product_syncid_reference[0].source_id, {'qty':str(inventory_line.product_id.qty_available),'is_in_stock':is_in_stock})
# else:
# if int(product_syncid_reference[0].source_id) in m_plist:
# m.cataloginventory_stock_item.update(product_syncid_reference[0].source_id, {'qty':str(inventory_line.product_id.qty_available),'is_in_stock':is_in_stock})
# else:
# #not found! manage error