diff --git a/addons/website_sale/__init__.py b/addons/website_sale/__init__.py index e09c98cff2a..c63bb4c1fb2 100644 --- a/addons/website_sale/__init__.py +++ b/addons/website_sale/__init__.py @@ -1,2 +1,3 @@ import controllers +import product import website_sale diff --git a/addons/website_sale/controllers/main.py b/addons/website_sale/controllers/main.py index 5345e6721d4..7d0d6051c5e 100644 --- a/addons/website_sale/controllers/main.py +++ b/addons/website_sale/controllers/main.py @@ -59,6 +59,7 @@ class Ecommerce(http.Controller): product_obj = request.registry.get('product.product') groups = product_obj.read_group(request.cr, SUPERUSER_ID, [("sale_ok", "=", True), ('website_published', '=', True)], ['public_categ_id'], 'public_categ_id', context=request.context) full_category_ids = [group['public_categ_id'][0] for group in groups if group['public_categ_id']] + for cat_id in category_obj.browse(request.cr, SUPERUSER_ID, full_category_ids, context=request.context): while cat_id.parent_id: cat_id = cat_id.parent_id @@ -246,6 +247,7 @@ class Ecommerce(http.Controller): for line in order.order_line: suggested_ids += [p.id for p in line.product_id and line.product_id.suggested_product_ids or [] for line in order.order_line] suggested_ids = prod_obj.search(request.cr, request.uid, [('id', 'in', suggested_ids)], context=request.context) + # select 3 random products suggested_products = [] while len(suggested_products) < 3 and suggested_ids: diff --git a/addons/website_sale/product.py b/addons/website_sale/product.py new file mode 100644 index 00000000000..344787fe6b7 --- /dev/null +++ b/addons/website_sale/product.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2013-Today OpenERP SA (). +# +# 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 . +# +############################################################################## + +from openerp.osv import osv, fields + + +class product_pricelist(osv.Model): + _inherit = "product.pricelist" + _columns = { + 'code': fields.char('Promotionnal Code', size=64, translate=True), + } + + +class product_template(osv.Model): + _inherit = "product.template" + _order = 'website_published,name' + _columns = { + 'website_published': fields.boolean('Available in the website'), + 'website_description': fields.html('Description for the website'), + 'suggested_product_id': fields.many2one('product.template', 'Suggested For Product'), + 'suggested_product_ids': fields.one2many('product.template', 'suggested_product_id', 'Suggested Products'), + 'website_sizex': fields.selection(map(lambda x: (str(x+1),str(x+1)), range(12)), 'Size X'), + 'website_sizey': fields.selection(map(lambda x: (str(x+1),str(x+1)), range(6)), 'Size Y'), + 'website_product_class': fields.selection([('','Default'), ('oe_image_full','Image Full')], 'Size Y'), + } + _defaults = { + 'website_sizex': '3', + 'website_sizey': '2', + 'website_product_class': '', + } + + def recommended_products(self, cr, uid, ids, context=None): + id = ids[0] + product_ids = [] + query = """ + SELECT sol.product_id + FROM sale_order_line as my + LEFT JOIN sale_order_line as sol + ON sol.order_id = my.order_id + WHERE my.product_id in (%s) + AND sol.product_id not in (%s) + GROUP BY sol.product_id + ORDER BY COUNT(sol.order_id) DESC + LIMIT 10 + """ + cr.execute(query, (id, id)) + for p in cr.fetchall(): + product_ids.append(p[0]) + + # search to apply access rules + product_ids = self.search(cr, uid, [("id", "in", product_ids)], limit=3) + return self.browse(cr, uid, product_ids) + + def img(self, cr, uid, ids, field='image_small', context=None): + return "/website/image?model=%s&field=%s&id=%s" % (self._name, field, ids[0]) + + +class product_product(osv.Model): + _inherit = "product.product" + + def _website_url(self, cr, uid, ids, field_name, arg, context=None): + res = {} + base_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url') + for id in ids: + res[id] = "%s/shop/product/%s/" % (base_url, id) + return res + + _columns = { + 'website_url': fields.function(_website_url, string="Website url"), + } + + def img(self, cr, uid, ids, field='image_small', context=None): + return "/website/image?model=%s&field=%s&id=%s" % (self._name, field, ids[0]) diff --git a/addons/website_sale/website_sale.py b/addons/website_sale/website_sale.py index 9b7342f6ab6..37ad53a0322 100644 --- a/addons/website_sale/website_sale.py +++ b/addons/website_sale/website_sale.py @@ -20,91 +20,29 @@ ############################################################################## from openerp import SUPERUSER_ID -from openerp.osv import osv, fields +from openerp.osv import osv -class product_pricelist(osv.osv): - _inherit = "product.pricelist" - _columns = { - 'code': fields.char('Promotionnal Code', size=64, translate=True), - } - -class product_template(osv.osv): - _inherit = "product.template" - _order = 'website_published,name' - _columns = { - 'website_published': fields.boolean('Available in the website'), - 'website_description': fields.html('Description for the website'), - 'suggested_product_id': fields.many2one('product.template', 'Suggested For Product'), - 'suggested_product_ids': fields.one2many('product.template', 'suggested_product_id', 'Suggested Products'), - 'website_sizex': fields.selection(map(lambda x: (str(x+1),str(x+1)), range(12)), 'Size X'), - 'website_sizey': fields.selection(map(lambda x: (str(x+1),str(x+1)), range(6)), 'Size Y'), - 'website_product_class': fields.selection([('','Default'), ('oe_image_full','Image Full')], 'Size Y'), - } - _defaults = { - 'website_sizex': '3', - 'website_sizey': '2', - 'website_product_class': '', - } - - def recommended_products(self, cr, uid, ids, context=None): - id = ids[0] - product_ids = [] - query = """ - SELECT sol.product_id - FROM sale_order_line as my - LEFT JOIN sale_order_line as sol - ON sol.order_id = my.order_id - WHERE my.product_id in (%s) - AND sol.product_id not in (%s) - GROUP BY sol.product_id - ORDER BY COUNT(sol.order_id) DESC - LIMIT 10 - """ - cr.execute(query, (id, id)) - for p in cr.fetchall(): - product_ids.append(p[0]) - - # search to apply access rules - product_ids = self.search(cr, uid, [("id", "in", product_ids)], limit=3) - return self.browse(cr, uid, product_ids) - - def img(self, cr, uid, ids, field='image_small', context=None): - return "/website/image?model=%s&field=%s&id=%s" % (self._name, field, ids[0]) - -class product_product(osv.osv): - _inherit = "product.product" - - def _website_url(self, cr, uid, ids, field_name, arg, context=None): - res = {} - base_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url') - for id in ids: - res[id] = "%s/shop/product/%s/" % (base_url, id) - return res - - _columns = { - 'website_url': fields.function(_website_url, string="Website url"), - } - - def img(self, cr, uid, ids, field='image_small', context=None): - return "/website/image?model=%s&field=%s&id=%s" % (self._name, field, ids[0]) - -class sale_order(osv.osv): +class sale_order(osv.Model): _inherit = "sale.order" - + def get_total_quantity(self, cr, uid, ids, context=None): order = self.browse(cr, uid, ids[0], context=context) - return sum(l.product_uom_qty for l in (order.order_line or [])) -class sale_order_line(osv.osv): + +class sale_order_line(osv.Model): _inherit = "sale.order.line" - + def _recalculate_product_values(self, cr, uid, ids, product_id=None, context=None): + if context is None: + context = {} user_obj = self.pool.get('res.users') product_id = product_id or ids and self.browse(cr, uid, ids[0], context=context).product_id.id - return self.product_id_change(cr, SUPERUSER_ID, [], + return self.product_id_change( + cr, SUPERUSER_ID, [], pricelist=context.pop('pricelist'), product=product_id, partner_id=user_obj.browse(cr, SUPERUSER_ID, uid).partner_id.id, - context=context)['value'] + context=context + )['value']