product_product.py 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
# -*- encoding: utf-8 -*-
##############################################################################
#
#    Product - Average Consumption Module for Odoo
#    Copyright (C) 2021-Today GRAP (http://www.grap.coop)
#    @author Damien Moulard
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp import models, fields, api

class ProductProduct(models.Model):
    _inherit = "product.product"

    @api.model
    def name_search(self, name='', args=None, operator='ilike', limit=100):
        res = super(ProductProduct, self).name_search(
             name=name, args=args, operator=operator, context=self._context, limit=limit)

        # Supplier id in self._context doesn't seem to be used in regular name_search,
        #   so don't use it here neither for consistency
        # print(self._context)

        try:
            # Context: asking for purchasable products: search with supplier code
            if args[0][0] == 'purchase_ok':
                sql = """
                SELECT pt.name, pp.id, psi.product_code
                FROM product_template pt
                INNER JOIN product_product pp ON pt.id = pp.product_tmpl_id
                INNER JOIN product_supplierinfo psi ON pt.id = psi.product_tmpl_id
                WHERE pt.purchase_ok IS TRUE
                AND psi.product_code ILIKE '{}%'
                LIMIT {}
                """.format(name, limit)

                self.env.cr.execute(sql)
                psi = self.env.cr.dictfetchall()

                # Complete res to <limit> results,
                #  and/or replace in result to have at least 3 results from this search
                nb_to_add = limit-len(res)
                nb_to_replace = 0
                if len(res) == limit:
                    nb_to_replace = 3
                    nb_to_add = 0
                elif len(res) == limit-1 :
                    nb_to_replace = 2
                    nb_to_add = 1
                elif len(res) == limit-2 :
                    nb_to_replace = 1
                    nb_to_add = 2

                # Replace last elements, if there are elements to replace
                psi_i = 0
                i = 0
                while i < nb_to_replace and psi_i < len(psi):
                    psi_item = [
                        psi[psi_i]['id'],
                        '[ref four. ' + psi[psi_i]['product_code'] + '] '
                            + psi[psi_i]['name']
                    ]
                    res[limit-nb_to_replace+i] = psi_item
                    i += 1
                    psi_i += 1

                # Add elements to complete to <limit> results
                i = 0
                while i < nb_to_add and psi_i < len(psi):
                    psi_item = [
                        psi[psi_i]['id'],
                        '[ref four. ' + psi[psi_i]['product_code'] + '] '
                            + psi[psi_i]['name']
                    ]
                    res.append(psi_item)
                    i += 1
                    psi_i += 1

        except Exception as e:
            # Error is likely that there is no context: search normally
            print(e)

        return res