Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Choosing a random supplier

By
Emmanuel Roldan
on 6/17/14, 10:39 AM 820 views

I am trying to create a method that will assign a random supplier when you place the order.

I know that this is the code that openerp uses to choose the default supplier, 


    def _get_main_product_supplier(self, cr, uid, product, context=None):
        """Determines the main (best) product supplier for ``product``,
        returning the corresponding ``supplierinfo`` record, or False
        if none were found. The default strategy is to select the
        supplier with the highest priority (i.e. smallest sequence).

        :param browse_record product: product to supply
        :rtype: product.supplierinfo browse_record or False
        """
        sellers = [(seller_info.sequence, seller_info)
                       for seller_info in product.seller_ids or []
                       if seller_info and isinstance(seller_info.sequence, (int, long))]
        return sellers and sellers[0][1] or False

    def _calc_seller(self, cr, uid, ids, fields, arg, context=None):
        result = {}
        for product in self.browse(cr, uid, ids, context=context):
            main_supplier = self._get_main_product_supplier(cr, uid, product, context=context)
            result[product.id] = {
                'seller_info_id': main_supplier and main_supplier.id or False,
                'seller_delay': main_supplier.delay if main_supplier else 1,
                'seller_qty': main_supplier and main_supplier.qty or 0.0,
                'seller_id': main_supplier and main_supplier.name.id or False
            }
        return result

and i need it to change it to choose it randomly.

what is your question?

Hannes Smit
on 6/17/14, 10:51 AM

i need choose a random supplier instead of the one with the highest priority.

Emmanuel Roldan
on 6/17/14, 11:30 AM

I know that this is the code that openerp uses to choose the default supplier, def _get_main_product_supplier(self, cr, uid, product, context=None): """Determines the main (best) product supplier for ``product``, returning the corresponding ``supplierinfo`` record, or False if none were found. The default strategy is to select the supplier with the highest priority (i.e. smallest sequence). :param browse_record product: product to supply :rtype: product.supplierinfo browse_record or False """ sellers = [(seller_info.sequence, seller_info) for seller_info in product.seller_ids or [] if seller_info and isinstance(seller_info.sequence, (int, long))] return sellers and sellers[0][1] or False def _calc_seller(self, cr, uid, ids, fields, arg, context=None): result = {} for product in self.browse(cr, uid, ids, context=context): main_supplier = self._get_main_product_supplier(cr, uid, product, context=context) result[product.id] = { 'seller_info_id': main_supplier and main_supplier.id or False, 'seller_delay': main_supplier.delay if main_supplier else 1, 'seller_qty': main_supplier and main_supplier.qty or 0.0, 'seller_id': main_supplier and main_supplier.name.id or False } return result and i need it to change it to choose it randomly.

Emmanuel Roldan
on 6/17/14, 11:40 AM
0
Hannes Smit
On 6/18/14, 4:07 AM

Emmanuel,

You should try something like the code below:

 

    import random

    ...
    ...

    def _get_main_product_supplier(self, cr, uid, product, context=None):
        """Determines the main (best) product supplier for ``product``,
        returning the corresponding ``supplierinfo`` record, or False
        if none were found. The default strategy is to select the
        supplier with the highest priority (i.e. smallest sequence).

        :param browse_record product: product to supply
        :rtype: product.supplierinfo browse_record or False
        """
        sellers = [(seller_info.sequence, seller_info)
                       for seller_info in product.seller_ids or []
                       if seller_info and isinstance(seller_info.sequence, (int, long))]
        return sellers and sellers[0][1] or False

    def _get_random_product_supplier(self, cr, uid, product, context=None):
        """Determines the main (best) product supplier for ``product``,
        returning the corresponding ``supplierinfo`` record, or False
        if none were found. The default strategy is to select the
        supplier with the highest priority (i.e. smallest sequence).

        :param browse_record product: product to supply
        :rtype: product.supplierinfo browse_record or False
        """
        sellers = [(seller_info.sequence, seller_info)
                       for seller_info in product.seller_ids or []
                       if seller_info and isinstance(seller_info.sequence, (int, long))]
        return sellers and random.choice(sellers)

    def _calc_seller(self, cr, uid, ids, fields, arg, context=None):
        result = {}
        for product in self.browse(cr, uid, ids, context=context):
            # main_supplier = self._get_main_product_supplier(cr, uid, product, context=context)
            main_supplier = self._get_random_product_supplier(cr, uid, product, context=context)
            result[product.id] = {
                'seller_info_id': main_supplier and main_supplier.id or False,
                'seller_delay': main_supplier.delay if main_supplier else 1,
                'seller_qty': main_supplier and main_supplier.qty or 0.0,
                'seller_id': main_supplier and main_supplier.name.id or False
            }
        return result

 

 

I'm still not sure why you want to achieve this...

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 6/17/14, 10:39 AM
Seen: 820 times
Last updated: 3/16/15, 8:10 AM