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

« back to all changes in this revision

Viewing changes to msf_partner/partner.py

  • Committer: jf
  • Date: 2011-05-16 09:55:17 UTC
  • mfrom: (129.1.1 unifield-wm)
  • Revision ID: jf@tempo4-20110516095517-giuzv2mouka39jb8
UF-270 Advance return in a currency that is not the functional currency

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
from osv import osv
24
24
from osv import fields
25
 
from msf_partner import PARTNER_TYPE 
26
 
import time
27
25
 
28
26
 
29
27
class res_partner(osv.osv):
30
28
    _name = 'res.partner'
31
29
    _inherit = 'res.partner'
32
 
    
33
 
    def search_in_product(self, cr, uid, obj, name, args, context=None):
34
 
        '''
35
 
        Search function of related field 'in_product'
36
 
        '''
37
 
        if not len(args):
38
 
            return []
39
 
        if context is None:
40
 
            context = {}
41
 
        if not context.get('product_id', False) or 'choose_supplier' not in context:
42
 
            return []
43
 
 
44
 
        supinfo_obj = self.pool.get('product.supplierinfo')
45
 
        sup_obj = self.pool.get('res.partner')
46
 
        res = []
47
 
 
48
 
        info_ids = supinfo_obj.search(cr, uid, [('product_product_ids', '=', context.get('product_id'))])
49
 
        info = supinfo_obj.read(cr, uid, info_ids, ['name'])
50
 
 
51
 
        sup_in = [x['name'] for x in info]
52
 
        
53
 
        for arg in args:
54
 
            if arg[1] == '=':
55
 
                if arg[2]:
56
 
                    res = sup_in
57
 
            else:
58
 
                    res = sup_obj.search(cr, uid, [('id', 'not in', sup_in)])
59
 
        
60
 
        if not res:
61
 
            return [('id', '=', 0)]
62
 
        return [('id', 'in', [x[0] for x in res])]
63
 
        
64
 
    
65
 
    def _set_in_product(self, cr, uid, ids, field_name, arg, context=None):
66
 
        '''
67
 
        Returns according to the context if the partner is in product form
68
 
        '''
69
 
        if context is None:
70
 
            context = {}
71
 
        res = {}
72
 
        
73
 
        product_obj = self.pool.get('product.product')
74
 
        
75
 
        # If we aren't in the context of choose supplier on procurement list
76
 
        if not context.get('product_id', False) or 'choose_supplier' not in context:
77
 
            for i in ids:
78
 
                res[i] = {'in_product': False, 'min_qty': 'N/A', 'delay': 'N/A'}
79
 
        else:
80
 
            product = product_obj.browse(cr, uid, context.get('product_id'))
81
 
            seller_ids = []
82
 
            seller_info = {}
83
 
            # Get all suppliers defined on product form
84
 
            for s in product.seller_ids:
85
 
                seller_ids.append(s.name.id)
86
 
                seller_info.update({s.name.id: {'min_qty': s.min_qty, 'delay': s.delay}})
87
 
            # Check if the partner is in product form
88
 
            for i in ids:
89
 
                if i in seller_ids:
90
 
                    res[i] = {'in_product': True, 'min_qty': '%s' %seller_info[i]['min_qty'], 'delay': '%s' %seller_info[i]['delay']}
91
 
                else:
92
 
                    res[i] = {'in_product': False, 'min_qty': 'N/A', 'delay': 'N/A'}
93
 
 
94
 
        return res
95
 
    
96
 
    def _get_price_info(self, cr, uid, ids, fiedl_name, args, context=None):
97
 
        '''
98
 
        Returns information from product supplierinfo if product_id is in context
99
 
        '''
100
 
        if not context:
101
 
            context = {}
102
 
            
103
 
        partner_price = self.pool.get('pricelist.partnerinfo')
104
 
        res = {}
105
 
        
106
 
        for id in ids:
107
 
            res[id] = {'price_currency': False,
108
 
                       'price_unit': 0.00,
109
 
                       'valide_until_date': False}
110
 
            
111
 
        if context.get('product_id'):
112
 
            for partner in self.browse(cr, uid, ids, context=context):
113
 
                product = self.pool.get('product.product').browse(cr, uid, context.get('product_id'), context=context)
114
 
                uom = context.get('uom', product.uom_id.id)
115
 
                pricelist = partner.property_product_pricelist_purchase
116
 
                context.update({'uom': uom})
117
 
                price_list = self.pool.get('product.product')._get_partner_info_price(cr, uid, product, partner.id, context.get('product_qty', 1.00), pricelist.currency_id.id, time.strftime('%Y-%m-%d'), uom, context=context)
118
 
                if not price_list:
119
 
                    func_currency_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
120
 
                    price = self.pool.get('res.currency').compute(cr, uid, func_currency_id, pricelist.currency_id.id, product.standard_price, round=True, context=context)
121
 
                    res[partner.id] = {'price_currency': pricelist.currency_id.id,
122
 
                                       'price_unit': price,
123
 
                                       'valide_until_date': False}
124
 
                else:
125
 
                    info_price = partner_price.browse(cr, uid, price_list[0], context=context)
126
 
                    partner_currency_id = pricelist.currency_id.id
127
 
                    price = self.pool.get('res.currency').compute(cr, uid, info_price.currency_id.id, partner_currency_id, info_price.price)
128
 
                    currency = partner_currency_id
129
 
                    # Uncomment the following 2 lines if you want the price in currency of the pricelist.partnerinfo instead of partner default currency
130
 
#                    currency = info_price.currency_id.id
131
 
#                    price = info_price.price
132
 
                    res[partner.id] = {'price_currency': currency,
133
 
                                       'price_unit': price,
134
 
                                       'valide_until_date': info_price.valid_till}
135
 
            
136
 
        return res
137
 
 
138
 
## QT : Remove _get_price_unit
139
 
 
140
 
## QT : Remove _get_valide_until_date 
141
30
 
142
31
    _columns = {
143
32
        'manufacturer': fields.boolean(string='Manufacturer', help='Check this box if the partner is a manufacturer'),
144
 
        'partner_type': fields.selection(PARTNER_TYPE, string='Partner type', required=True),
145
 
        'in_product': fields.function(_set_in_product, fnct_search=search_in_product, string='In product', type="boolean", readonly=True, method=True, multi='in_product'),
146
 
        'min_qty': fields.function(_set_in_product, string='Min. Qty', type='char', readonly=True, method=True, multi='in_product'),
147
 
        'delay': fields.function(_set_in_product, string='Delivery Lead time', type='char', readonly=True, method=True, multi='in_product'),
148
 
        'property_product_pricelist_purchase': fields.property(
149
 
          'product.pricelist',
150
 
          type='many2one', 
151
 
          relation='product.pricelist', 
152
 
          domain=[('type','=','purchase')],
153
 
          string="Purchase default currency", 
154
 
          method=True,
155
 
          view_load=True,
156
 
          required=True,
157
 
          help="This currency will be used, instead of the default one, for purchases from the current partner"),
158
 
        'property_product_pricelist': fields.property(
159
 
            'product.pricelist',
160
 
            type='many2one', 
161
 
            relation='product.pricelist', 
162
 
            domain=[('type','=','sale')],
163
 
            string="Field orders default currency", 
164
 
            method=True,
165
 
            view_load=True,
166
 
            required=True,
167
 
            help="This currency will be used, instead of the default one, for field orders to the current partner"),
168
 
        'price_unit': fields.function(_get_price_info, method=True, type='float', string='Unit price', multi='info'),
169
 
        'valide_until_date' : fields.function(_get_price_info, method=True, type='char', string='Valid until date', multi='info'),
170
 
        'price_currency': fields.function(_get_price_info, method=True, type='many2one', relation='res.currency', string='Currency', multi='info'),
 
33
        'partner_type': fields.selection([('internal', 'Internal'), ('section', 'Inter-section'),
 
34
                                          ('external', 'External')], string='Partner type', required=True),
171
35
    }
172
36
 
173
37
    _defaults = {
175
39
        'partner_type': lambda *a: 'external',
176
40
    }
177
41
 
178
 
    def create(self, cr, uid, vals, context=None):
179
 
        if 'partner_type' in vals and vals['partner_type'] in ('internal', 'section', 'esc', 'intermission'):
180
 
            msf_customer = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_internal_customers')
181
 
            msf_supplier = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_internal_suppliers')
182
 
            if msf_customer and not 'property_stock_customer' in vals:
183
 
                vals['property_stock_customer'] = msf_customer[1]
184
 
            if msf_supplier and not 'property_stock_supplier' in vals:
185
 
                vals['property_stock_supplier'] = msf_supplier[1]
186
 
 
187
 
        return super(res_partner, self).create(cr, uid, vals, context=context)
188
 
    
189
 
    def on_change_partner_type(self, cr, uid, ids, partner_type, sale_pricelist, purchase_pricelist):
190
 
        '''
191
 
        Change the procurement method according to the partner type
192
 
        '''
193
 
        price_obj = self.pool.get('product.pricelist')
194
 
        cur_obj = self.pool.get('res.currency')
195
 
        user_obj = self.pool.get('res.users')
196
 
        
197
 
        r = {'po_by_project': 'project'}
198
 
        
199
 
        if not partner_type or partner_type in ('external', 'internal'):
200
 
            r.update({'po_by_project': 'all'})
201
 
        
202
 
        sale_authorized_price = price_obj.search(cr, uid, [('type', '=', 'sale'), ('in_search', '=', partner_type)])
203
 
        if sale_authorized_price and sale_pricelist not in sale_authorized_price:
204
 
            r.update({'property_product_pricelist': sale_authorized_price[0]})
205
 
            
206
 
        purchase_authorized_price = price_obj.search(cr, uid, [('type', '=', 'purchase'), ('in_search', '=', partner_type)])
207
 
        if purchase_authorized_price and purchase_pricelist not in purchase_authorized_price:
208
 
            r.update({'property_product_pricelist_purchase': purchase_authorized_price[0]})
209
 
        
210
 
        if partner_type and partner_type in ('internal', 'section', 'esc', 'intermission'):
211
 
            msf_customer = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_internal_customers')
212
 
            if msf_customer:
213
 
                r.update({'property_stock_customer': msf_customer[1]})
214
 
            msf_supplier = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_internal_suppliers')
215
 
            if msf_supplier:
216
 
                r.update({'property_stock_supplier': msf_supplier[1]})
217
 
        else:
218
 
            other_customer = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_customers')
219
 
            if other_customer:
220
 
                r.update({'property_stock_customer': other_customer[1]})
221
 
            other_supplier = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_suppliers')
222
 
            if other_supplier:
223
 
                r.update({'property_stock_supplier': other_supplier[1]}) 
224
 
        
225
 
        return {'value': r}
226
 
    
227
 
    def search(self, cr, uid, args=None, offset=0, limit=None, order=None, context=None, count=False):
228
 
        '''
229
 
        Sort suppliers to have all suppliers in product form at the top of the list
230
 
        '''
231
 
        supinfo_obj = self.pool.get('product.supplierinfo')
232
 
        if context is None:
233
 
            context = {}
234
 
        if args is None:
235
 
            args = []
236
 
        
237
 
        # Get all supplier
238
 
        tmp_res = super(res_partner, self).search(cr, uid, args, offset, limit, order, context=context, count=count)
239
 
        if not context.get('product_id', False) or 'choose_supplier' not in context or count:
240
 
            return tmp_res
241
 
        else:
242
 
            # Get all supplier in product form
243
 
            args.append(('in_product', '=', True))
244
 
            res_in_prod = super(res_partner, self).search(cr, uid, args, offset, limit, order, context=context, count=count)
245
 
            new_res = []
246
 
 
247
 
            # Sort suppliers by sequence in product form
248
 
            if 'product_id' in context:
249
 
                supinfo_ids = supinfo_obj.search(cr, uid, [('name', 'in', res_in_prod), ('product_product_ids', '=', context.get('product_id'))], order='sequence')
250
 
            
251
 
                for result in supinfo_obj.read(cr, uid, supinfo_ids, ['name']):
252
 
                    try:
253
 
                        tmp_res.remove(result['name'][0])
254
 
                        new_res.append(result['name'][0])
255
 
                    except:
256
 
                        pass
257
 
 
258
 
            #return new_res  # comment this line to have all suppliers (with suppliers in product form at the top of the list)
259
 
 
260
 
            new_res.extend(tmp_res)
261
 
            
262
 
            return new_res
263
 
 
264
42
res_partner()
265
43
 
266
44
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: