~sebastien.beau/magentoerpconnect/refactor_for_code_sprint

« back to all changes in this revision

Viewing changes to magentoerpconnect/sale.py

  • Committer: sebastien beau
  • Date: 2012-02-01 09:36:59 UTC
  • Revision ID: sebastien.beau@akretion.com.br-20120201093659-1atm9d8ferqvuqbh
[REF] refactor code regarding the change done in base_external_referentials, introduction of sub-mapping

Show diffs side-by-side

added added

removed removed

Lines of Context:
382
382
            partner_obj.write(cr, uid, [partner_id], partner_vals)
383
383
        return res
384
384
    
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)])
388
 
        if mapping_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'])
391
 
            if mapping_lines:
392
 
                lines_vals = []
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
401
 
                    if line_tax_vat > 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
414
 
        return res
415
 
 
416
385
 
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 :
436
405
 
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
441
410
        tax_id = []
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]])]
448
 
                else:
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)
 
415
                if line_tax_ids:
 
416
                    defaults_line['tax_id'] = [(6, 0, line_tax_ids)]
453
417
 
454
418
        name = product.name
455
419
        if ext_code_field and data_record.get(ext_code_field, False):
470
434
                                }))
471
435
        return res
472
436
    
473
 
    def add_order_shipping(self, cr, uid, res, external_referential_id, data_record, key_field, mapping_lines, defaults, context=None):
 
437
    def add_order_shipping(self, cr, uid, res, external_referential_id, data_record, defaults, context=None):
474
438
        if context is None: context = {}
475
439
        if data_record.get('shipping_amount', False) and float(data_record.get('shipping_amount', False)) > 0:
476
440
            ctx = context.copy()
481
445
            res = self.add_order_extra_line(cr, uid, res, data_record, 'shipping_amount', product_ref, defaults, ctx)
482
446
        return res
483
447
 
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)
494
458
        return res
495
459
 
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)
505
469
        return res
506
470
 
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)
516
480
        return res
517
481
    
518
 
    
 
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)
 
484
        res=res[0]
 
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)
 
490
        return [res]
 
491
 
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
544
517
        
545
518
        data_record['items'] = items_to_import 
546
519
        return data_record
547
 
    
548
 
    
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)
555
 
        return res
556
520
 
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)
561
 
        
 
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)
565
 
        
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)
567
530
 
568
 
        if not context.get('one_by_one', False):
569
 
            if data_record.get('items', False):
570
 
                if NOTRY:
571
 
                    res = self.get_all_order_lines(cr, uid, res, external_referential_id, data_record, key_field, mapping_lines, defaults, context)
572
 
                else:
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)
575
 
                    #try:
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
579
 
                    #    print e
580
 
                        #TODO flag that the order has an error, especially.
581
 
            
 
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']
584
535
        return res
724
675
            result['unchanged_ids'] = unchanged_ids
725
676
        return result
726
677
 
727
 
# UPDATE ORDER STATUS FROM MAGENTO TO OPENERP IS UNSTABLE, AND NOT VERY USEFULL. MAYBE IT WILL BE REFACTORED 
728
 
 
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)
735
 
 
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)
740
 
        #else:
741
 
        #   super(magerp_osv.magerp_osv, self).oe_status(cr, uid, order_id, context)
742
 
    
 
678
 
743
679
sale_order()
744
680