~unifield-team/unifield-wm/us-826

« back to all changes in this revision

Viewing changes to purchase_msf/purchase_msf.py

  • Committer: Matthieu Dietrich
  • Date: 2011-07-22 11:27:00 UTC
  • mto: This revision was merged to the branch mainline in revision 229.
  • Revision ID: matthieu.dietrich@geneva.msf.org-20110722112700-pxnmrctduwhie98g
UF-331: [IMP] Add manufacturers text fields

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
import time
31
31
 
32
32
 
 
33
#
 
34
# Model definition
 
35
#
 
36
 
33
37
class purchase_order_line(osv.osv):
34
 
    '''
35
 
    information from product are repacked
36
 
    '''
 
38
 
37
39
    _inherit = 'purchase.order.line'
38
 
    
39
 
    def create(self, cr, uid, vals, context=None):
40
 
        '''
41
 
        update the name attribute if a product is selected
42
 
        '''
43
 
        prod_obj = self.pool.get('product.product')
44
 
        if vals.get('product_id'):
45
 
            vals.update(name=prod_obj.browse(cr, uid, vals.get('product_id'), context=context).name,)
46
 
        elif vals.get('comment'):
47
 
            vals.update(name=vals.get('comment'),)
48
 
            
49
 
        return super(purchase_order_line, self).create(cr, uid, vals, context=context)
50
 
    
51
 
    def write(self, cr, uid, ids, vals, context=None):
52
 
        '''
53
 
        update the name attribute if a product is selected
54
 
        '''
55
 
        prod_obj = self.pool.get('product.product')
56
 
        if vals.get('product_id'):
57
 
            vals.update(name=prod_obj.browse(cr, uid, vals.get('product_id'), context=context).name,)
58
 
        elif vals.get('comment'):
59
 
            vals.update(name=vals.get('comment'),)
60
 
            
61
 
        return super(purchase_order_line, self).write(cr, uid, ids, vals, context=context)
 
40
    #_name = 'purchase.order.line'
 
41
    _description = 'Purchase Order Line modified for MSF'
62
42
    
63
43
    def _get_manufacturers(self, cr, uid, ids, field_name, arg, context=None):
64
 
        '''
65
 
        get manufacturers info
66
 
        '''
67
44
        result = {}
68
45
        for record in self.browse(cr, uid, ids, context=context):
69
46
            result[record.id] = {
72
49
                                 'third_manufacturer_id': False,
73
50
                                }
74
51
            po_supplier = record.order_id.partner_id
75
 
            if record.product_id:
76
 
                for seller_id in record.product_id.seller_ids:
77
 
                    if seller_id.name == po_supplier:
78
 
                        result[record.id] = {
79
 
                                             'manufacturer_id': seller_id.manufacturer_id.id,
80
 
                                             'second_manufacturer_id': seller_id.second_manufacturer_id.id,
81
 
                                             'third_manufacturer_id': seller_id.third_manufacturer_id.id,
82
 
                                             }
83
 
                        break
 
52
            for seller_id in record.product_id.seller_ids:
 
53
                if seller_id.name == po_supplier:
 
54
                    result[record.id] = {
 
55
                                         'manufacturer_id': seller_id.manufacturer_id.id,
 
56
                                         'second_manufacturer_id': seller_id.second_manufacturer_id.id,
 
57
                                         'third_manufacturer_id': seller_id.third_manufacturer_id.id,
 
58
                                        }
 
59
                    break
84
60
 
85
61
        return result
86
62
    
87
 
    def _getProductInfo(self, cr, uid, ids, field_name, arg, context=None):
88
 
        '''
89
 
        compute function fields related to product identity
90
 
        '''
91
 
        prod_obj = self.pool.get('product.product')
 
63
    def _getProductInfo(self, cr, uid, ids, field_name, arg, context):
 
64
        
 
65
        # ACCESS to product_id ??
 
66
        
92
67
        # the name of the field is used to select the data to display
93
68
        result = {}
94
69
        for i in ids:
95
 
            result[i] = {}
96
 
            for f in field_name:
97
 
                result[i].update({f:False,})
98
 
                
99
 
        for obj in self.browse(cr, uid, ids, context=context):
100
 
            # default values
101
 
            internal_code = False
102
 
            internal_name = False
103
 
            supplier_code = False
104
 
            supplier_name = False
105
 
            if obj.product_id:
106
 
                prod = obj.product_id
107
 
                # new fields
108
 
                internal_code = prod.default_code
109
 
                internal_name = prod.name
110
 
                # filter the seller list - only select the seller which corresponds
111
 
                # to the supplier selected during PO creation
112
 
                # if no supplier selected in product, there is no specific supplier info
113
 
                if prod.seller_ids:
114
 
                    partner_id = obj.order_id.partner_id.id
115
 
                    sellers = filter(lambda x: x.name.id == partner_id, prod.seller_ids)
116
 
                    if sellers:
117
 
                        seller = sellers[0]
118
 
                        supplier_code = seller.product_code
119
 
                        supplier_name = seller.product_name
120
 
            # update dic
121
 
            result[obj.id].update(internal_code=internal_code,
122
 
                                  internal_name=internal_name,
123
 
                                  supplier_code=supplier_code,
124
 
                                  supplier_name=supplier_name,
125
 
                                  )
126
 
        
 
70
            result[i] = i
 
71
            
127
72
        return result
128
73
 
129
 
    _columns = {'internal_code': fields.function(_getProductInfo, method=True, type='char', size=1024, string='Internal code', multi='get_vals',),
130
 
                'internal_name': fields.function(_getProductInfo, method=True, type='char', size=1024, string='Internal name', multi='get_vals',),
131
 
                'supplier_code': fields.function(_getProductInfo, method=True, type='char', size=1024, string='Supplier code', multi='get_vals',),
132
 
                'supplier_name': fields.function(_getProductInfo, method=True, type='char', size=1024, string='Supplier name', multi='get_vals',),
133
 
                # new colums to display product manufacturers linked to the purchase order supplier
134
 
                'manufacturer_id': fields.function(_get_manufacturers, method=True, type='many2one', relation="res.partner", string="Manufacturer", store=False, multi="all"),
135
 
                'second_manufacturer_id': fields.function(_get_manufacturers, method=True, type='many2one', relation="res.partner", string="Second Manufacturer", store=False, multi="all"),
136
 
                'third_manufacturer_id': fields.function(_get_manufacturers, method=True, type='many2one', relation="res.partner", string="Third Manufacturer", store=False, multi="all"),
137
 
                }
 
74
    _columns = {
 
75
        #function test column
 
76
        #'temp': fields.function(_getProductInfo,
 
77
        #                        type='char',
 
78
        #                        obj=None,
 
79
        #                        method=True,
 
80
        #                        string='Test function'),
 
81
        #new column internal_code
 
82
        'internal_code': fields.char('Internal code', size=256),
 
83
        #new column internal_name
 
84
        'internal_name': fields.char('Internal name', size=256),
 
85
        #new column supplier_code
 
86
        'supplier_code': fields.char('Supplier code', size=256),
 
87
        #new column supplier_name
 
88
        'supplier_name': fields.char('Supplier name', size=256),
 
89
        # new colums to display product manufacturers linked to the purchase order supplier
 
90
        'manufacturer_id': fields.function(_get_manufacturers, method=True, type='many2one', relation="res.partner", string="Manufacturer", store=False, multi="all"),
 
91
        'second_manufacturer_id': fields.function(_get_manufacturers, method=True, type='many2one', relation="res.partner", string="Second Manufacturer", store=False, multi="all"),
 
92
        'third_manufacturer_id': fields.function(_get_manufacturers, method=True, type='many2one', relation="res.partner", string="Third Manufacturer", store=False, multi="all"),
 
93
    }
 
94
    
 
95
    _defaults = {
 
96
    }
 
97
    
 
98
    
 
99
    def product_id_change(self, cr, uid, ids, pricelist, product, qty, uom,
 
100
            partner_id, date_order=False, fiscal_position=False, date_planned=False,
 
101
            name=False, price_unit=False, notes=False):
 
102
        
 
103
        # 1. call the original method
 
104
        result = super(purchase_order_line, self).product_id_change(
 
105
                                                                      cr, uid, ids, pricelist, product, qty, uom,
 
106
                                                                      partner_id, date_order=False, fiscal_position=False,
 
107
                                                                      date_planned=False, name=False, price_unit=False,
 
108
                                                                      notes=False)
 
109
        
 
110
        # when we erase the field, it is empty, so no product information but modified
 
111
        if not product:
 
112
            # reset values
 
113
            result['value'].update({'name': False,
 
114
                          'internal_code': False,
 
115
                          'internal_name': False,
 
116
                          'supplier_code': False,
 
117
                          'supplier_name': False})
 
118
            return result
 
119
        
 
120
        # 2. complete the new fields
 
121
        # - internal code
 
122
        # - internal name
 
123
        # - supplier code
 
124
        # - supplier name
 
125
        
 
126
        #@@@override@purchase>purchase.py>purchase_order_line.product_id_change : copied from original method, is everything needed ?
 
127
        if partner_id:
 
128
            lang=self.pool.get('res.partner').read(cr, uid, partner_id, ['lang'])['lang']
 
129
        context={'lang':lang}
 
130
        context['partner_id'] = partner_id
 
131
        
 
132
        prod = self.pool.get('product.product').browse(cr, uid, product, context=context)
 
133
        #@@@end
 
134
        
 
135
        # new fields
 
136
        internal_code = prod.default_code
 
137
        internal_name = prod.name
 
138
        supplier_code = False
 
139
        supplier_name = False
 
140
        
 
141
        # filter the seller list - only select the seller which corresponds
 
142
        # to the supplier selected during PO creation
 
143
        # if no supplier selected in product, there is no specific supplier info
 
144
        if prod.seller_ids:
 
145
            sellers = filter(lambda x: x.name.id == partner_id, prod.seller_ids)
 
146
            if sellers:
 
147
                seller = sellers[0]
 
148
                supplier_code = seller.product_code
 
149
                supplier_name = seller.product_name
 
150
            
 
151
        # 3 .modify the description ('name' attribute)
 
152
        result['value'].update({'name': internal_name,
 
153
                      'internal_code': internal_code,
 
154
                      'internal_name': internal_name,
 
155
                      'supplier_code': supplier_code,
 
156
                      'supplier_name': supplier_name})
 
157
        
 
158
        # return the dictionary to update the view
 
159
        return result
 
160
        
138
161
 
139
162
purchase_order_line()
140
163