382
382
partner_obj.write(cr, uid, [partner_id], partner_vals)
385
def get_order_lines(self, cr, uid, res, external_referential_id, data_record, key_field, mapping_lines, defaults, context=None):
386
if context is None: context = {}
387
mapping_id = self.pool.get('external.mapping').search(cr,uid,[('model','=','sale.order.line'),('referential_id','=',external_referential_id)])
389
mapping_line_ids = self.pool.get('external.mapping.line').search(cr,uid,[('mapping_id','=',mapping_id[0]),('type','in',['in_out','in'])])
390
mapping_lines = self.pool.get('external.mapping.line').read(cr,uid,mapping_line_ids,['external_field','external_type','in_function'])
393
is_tax_included = defaults.get('price_type', False) == 'tax_included'
394
for line_data in data_record.get('items', []):
395
# Setting the UoM in sale order line as defined in product definition in openerp
396
product_id = self.pool.get('product.product').extid_to_oeid(cr, uid, line_data['product_id'], external_referential_id)
397
product = self.pool.get('product.product').browse(cr, uid, product_id)
398
defaults_line = {'product_uom': product.uom_id.id}
399
#simple VAT tax on order line (else override method):
400
line_tax_vat = float(line_data.get('tax_percent', False) or 0) / 100.0
402
line_tax_ids = self.pool.get('account.tax').search(cr, uid, ['|', ('type_tax_use', '=', 'all'), ('type_tax_use', '=', 'sale'), ('price_include', '=', is_tax_included), ('amount', '>=', line_tax_vat - 0.001), ('amount', '<=', line_tax_vat + 0.001)])
403
if line_tax_ids and len(line_tax_ids) > 0:
404
defaults_line['tax_id'] = [(6, 0, [line_tax_ids[0]])]
405
context.update({'partner_id': res['partner_id'], 'pricelist_id': res['pricelist_id']})
406
if defaults.get('price_type', False) == 'tax_included':
407
context.update({'price_is_tax_included': True})
408
line_val = self.oevals_from_extdata(cr, uid, external_referential_id, line_data, 'item_id', mapping_lines, defaults_line, context)
409
if line_val['product_id']:
410
line_val['type'] = self.pool.get('product.product').read(cr, uid, line_val['product_id'], ['procure_method'], context)['procure_method']
411
if not line_val.has_key('_CANCEL_IMPORT'):
412
lines_vals.append((0, 0, line_val))
413
res['order_line'] = lines_vals
417
386
def add_order_extra_line(self, cr, uid, res, data_record, ext_field, product_ref, defaults, context=None):
418
387
""" Add or substract amount on order as a separate line item with single quantity for each type of amounts like :
437
406
model, product_id = model_data_obj.get_object_reference(cr, uid, *product_ref)
438
407
product = self.pool.get('product.product').browse(cr, uid, product_id, context)
439
is_tax_included = defaults.get('price_type', False) == 'tax_included'
408
is_tax_included = context.get('price_is_tax_included', False)
440
409
amount = float(data_record[ext_field]) * sign
442
411
if ext_tax_field:
443
412
if data_record[ext_tax_field] and float(data_record[ext_tax_field]) != 0:
444
tax_vat = abs(float(data_record[ext_tax_field]) / amount)
445
tax_ids = self.pool.get('account.tax').search(cr, uid, [('price_include', '=', is_tax_included), ('type_tax_use', '=', 'sale'), ('amount', '>=', tax_vat - 0.001), ('amount', '<=', tax_vat + 0.001)])
446
if tax_ids and len(tax_ids) > 0:
447
tax_id = [(6, 0, [tax_ids[0]])]
449
#try to find a tax with less precision
450
tax_ids = self.pool.get('account.tax').search(cr, uid, [('price_include', '=', is_tax_included), ('type_tax_use', '=', 'sale'), ('amount', '>=', tax_vat - 0.01), ('amount', '<=', tax_vat + 0.01)])
451
if tax_ids and len(tax_ids) > 0:
452
tax_id = [(6, 0, [tax_ids[0]])]
413
line_tax_vat_rate = abs(float(data_record[ext_tax_field]) / amount)
414
line_tax_ids = self.pool.get('account.tax').get_tax_from_rate(cr, uid, line_tax_vat_rate, is_tax_included, context)
416
defaults_line['tax_id'] = [(6, 0, line_tax_ids)]
454
418
name = product.name
455
419
if ext_code_field and data_record.get(ext_code_field, False):
481
445
res = self.add_order_extra_line(cr, uid, res, data_record, 'shipping_amount', product_ref, defaults, ctx)
484
def add_gift_certificates(self, cr, uid, res, external_referential_id, data_record, key_field, mapping_lines, defaults, context=None):
448
def add_gift_certificates(self, cr, uid, res, external_referential_id, data_record, defaults, context=None):
485
449
if context is None: context = {}
486
450
if data_record.get('giftcert_amount', False) and float(data_record.get('giftcert_amount', False)) > 0:
487
451
ctx = context.copy()
493
457
res = self.add_order_extra_line(cr, uid, res, data_record, 'giftcert_amount', product_ref, defaults, ctx)
496
def add_discount(self, cr, uid, res, external_referential_id, data_record, key_field, mapping_lines, defaults, context=None):
460
def add_discount(self, cr, uid, res, external_referential_id, data_record, defaults, context=None):
497
461
#TODO fix me rev 476
498
462
#if data_record.get('discount_amount', False) and float(data_record.get('discount_amount', False)) < 0:
499
463
# ctx = context.copy()
504
468
# res = self.add_order_extra_line(cr, uid, res, data_record, 'discount_amount', product_ref, defaults, ctx)
507
def add_cash_on_delivery(self, cr, uid, res, external_referential_id, data_record, key_field, mapping_lines, defaults, context=None):
471
def add_cash_on_delivery(self, cr, uid, res, external_referential_id, data_record, defaults, context=None):
508
472
if context is None: context = {}
509
473
if data_record.get('cod_fee', False) and float(data_record.get('cod_fee', False)) > 0:
510
474
ctx = context.copy()
515
479
res = self.add_order_extra_line(cr, uid, res, data_record, 'cod_fee', product_ref, defaults, ctx)
482
def convert_extdata_into_oedata(self, cr, uid, external_data, external_referential_id, parent_data=None, defaults=None, context=None):
483
res = super(sale_order, self).convert_extdata_into_oedata(cr, uid, external_data, external_referential_id, parent_data=parent_data, defaults=defaults, context=context)
485
external_data = external_data[0]
486
res = self.add_order_shipping(cr, uid, res, external_referential_id, external_data, defaults, context)
487
res = self.add_gift_certificates(cr, uid, res, external_referential_id, external_data, defaults, context)
488
res = self.add_discount(cr, uid, res, external_referential_id, external_data, defaults, context)
489
res = self.add_cash_on_delivery(cr, uid, res, external_referential_id, external_data, defaults, context)
519
492
def merge_parent_item_line_with_child(self, cr, uid, item, items_child, context=None):
520
493
if item['product_type'] == 'configurable':
521
494
#For configurable product all information regarding the price is in the configurable item
545
518
data_record['items'] = items_to_import
546
519
return data_record
549
def get_all_order_lines(self, cr, uid, res, external_referential_id, data_record, key_field, mapping_lines, defaults, context=None):
550
res = self.get_order_lines(cr, uid, res, external_referential_id, data_record, key_field, mapping_lines, defaults, context)
551
res = self.add_order_shipping(cr, uid, res, external_referential_id, data_record, key_field, mapping_lines, defaults, context)
552
res = self.add_gift_certificates(cr, uid, res, external_referential_id, data_record, key_field, mapping_lines, defaults, context)
553
res = self.add_discount(cr, uid, res, external_referential_id, data_record, key_field, mapping_lines, defaults, context)
554
res = self.add_cash_on_delivery(cr, uid, res, external_referential_id, data_record, key_field, mapping_lines, defaults, context)
557
def oevals_from_extdata(self, cr, uid, external_referential_id, data_record, key_field, mapping_lines, defaults, context=None):
521
def oevals_from_extdata(self, cr, uid, external_referential_id, data_record, key_field, mapping_lines, parent_data, defaults, context=None):
558
522
if context is None: context = {}
559
523
if data_record.get('items', False):
560
524
data_record = self.data_record_filter(cr, uid, data_record, context=context)
525
#TODO refactor this code regarding the new feature of sub-mapping in base_external_referential
562
526
if not context.get('one_by_one', False):
563
527
if data_record.get('billing_address', False):
564
528
defaults = self.get_order_addresses(cr, uid, defaults, external_referential_id, data_record, key_field, mapping_lines, defaults, context)
566
res = super(magerp_osv.magerp_osv, self).oevals_from_extdata(cr, uid, external_referential_id, data_record, key_field, mapping_lines, defaults, context)
529
res = super(magerp_osv.magerp_osv, self).oevals_from_extdata(cr, uid, external_referential_id, data_record, key_field, mapping_lines, parent_data, defaults, context)
568
if not context.get('one_by_one', False):
569
if data_record.get('items', False):
571
res = self.get_all_order_lines(cr, uid, res, external_referential_id, data_record, key_field, mapping_lines, defaults, context)
573
#TODO fix me error should be raise correctly in the reporting system
574
res = self.get_all_order_lines(cr, uid, res, external_referential_id, data_record, key_field, mapping_lines, defaults, context)
576
# res = self.get_all_order_lines(cr, uid, res, external_referential_id, data_record, key_field, mapping_lines, defaults, context)
577
#except Exception, e:
578
# print "order has errors with items lines, data are: ", data_record
580
#TODO flag that the order has an error, especially.
531
#Move me in a mapping
532
if not context.get('one_by_one', False):
582
533
if data_record.get('status_history', False) and len(data_record['status_history']) > 0:
583
534
res['date_order'] = data_record['status_history'][len(data_record['status_history'])-1]['created_at']
724
675
result['unchanged_ids'] = unchanged_ids
727
# UPDATE ORDER STATUS FROM MAGENTO TO OPENERP IS UNSTABLE, AND NOT VERY USEFULL. MAYBE IT WILL BE REFACTORED
729
#def oe_update(self,cr, uid, existing_rec_id, vals, data, external_referential_id, defaults, context):
730
#order_line_ids = self.pool.get('sale.order.line').search(cr,uid,[('order_id','=', existing_rec_id)])
731
#self.pool.get('sale.order.line').unlink(cr, uid, order_line_ids)
732
#TODO update order status eventually (that would be easier if they were linked by some foreign key...)
733
#self.oe_status(cr, uid, data, existing_rec_id, context)
734
#return super(magerp_osv.magerp_osv, self).oe_update(cr, uid, existing_rec_id, vals, data, external_referential_id, defaults, context)
736
#def oe_status(self, cr, uid, data, order_id, context):
737
#wf_service = netsvc.LocalService("workflow")
738
#if data.get('status_history', False) and len(data['status_history']) > 0 and data['status_history'][0]['status'] == 'canceled':
739
# wf_service.trg_validate(uid, 'sale.order', order_id, 'cancel', cr)
741
# super(magerp_osv.magerp_osv, self).oe_status(cr, uid, order_id, context)