[IMP] website_event: split module: remove depends with sale and move event's ticket...
[odoo/odoo.git] / addons / website_sale / models / product.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2013-Today OpenERP SA (<http://www.openerp.com>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from openerp.osv import osv, fields
23
24
25 class product_pricelist(osv.Model):
26     _inherit = "product.pricelist"
27     _columns = {
28         'code': fields.char('Promotionnal Code', size=64, translate=True),
29     }
30
31
32 class product_template(osv.Model):
33     _inherit = ["product.template", "website.seo.metadata"]
34     _order = 'website_published desc, website_sequence desc, name'
35     _name = 'product.template'
36
37     def _website_url(self, cr, uid, ids, field_name, arg, context=None):
38         res = dict.fromkeys(ids, '')
39         base_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url')
40         for product in self.browse(cr, uid, ids, context=context):
41             res[product.id] = "%s/shop/product/%s/" % (base_url, product.id)
42         return res
43
44     _columns = {
45         'website_published': fields.boolean('Available in the website'),
46         'website_description': fields.html('Description for the website'),
47         # TDE TODO FIXME: when website_mail/mail_thread.py inheritance work -> this field won't be necessary
48         'website_message_ids': fields.one2many(
49             'mail.message', 'res_id',
50             domain=lambda self: [
51                 '&', ('model', '=', self._name), ('type', '=', 'comment')
52             ],
53             string='Website Messages',
54             help="Website communication history",
55         ),
56         'suggested_product_id': fields.many2one('product.template', 'Suggested For Product'),
57         'suggested_product_ids': fields.one2many('product.template', 'suggested_product_id', 'Suggested Products'),
58         'website_size_x': fields.integer('Size X'),
59         'website_size_y': fields.integer('Size Y'),
60         'website_style_ids': fields.many2many('website.product.style', 'product_website_style_rel', 'product_id', 'style_id', 'Styles'),
61         'website_sequence': fields.integer('Sequence', help="Determine the display order in the Website E-commerce"),
62         'website_url': fields.function(_website_url, string="Website url", type="char"),
63     }
64
65     def __defaults_website_sequence(self, cr, uid, *kwargs):
66         cr.execute('SELECT MAX(website_sequence) FROM product_template')
67         max_sequence = cr.fetchone()[0] or 0
68         return max_sequence + 1
69
70     _defaults = {
71         'website_size_x': 1,
72         'website_size_y': 1,
73         'website_sequence': __defaults_website_sequence,
74         'website_published': False,
75     }
76
77     def set_sequence_top(self, cr, uid, ids, context=None):
78         cr.execute('SELECT MAX(website_sequence) FROM product_template')
79         max_sequence = cr.fetchone()[0] or 0
80         return self.write(cr, uid, ids, {'website_sequence': max_sequence + 1}, context=context)
81
82     def set_sequence_bottom(self, cr, uid, ids, context=None):
83         cr.execute('SELECT MIN(website_sequence) FROM product_template')
84         min_sequence = cr.fetchone()[0] or 0
85         return self.write(cr, uid, ids, {'website_sequence': min_sequence -1}, context=context)
86
87     def set_sequence_up(self, cr, uid, ids, context=None):
88         product = self.browse(cr, uid, ids[0], context=context)
89         cr.execute("""  SELECT id, website_sequence FROM product_template
90                         WHERE website_sequence > %s AND website_published = %s ORDER BY website_sequence ASC LIMIT 1""" % (product.website_sequence, product.website_published))
91         prev = cr.fetchone()
92         if prev:
93             self.write(cr, uid, [prev[0]], {'website_sequence': product.website_sequence}, context=context)
94             return self.write(cr, uid, [ids[0]], {'website_sequence': prev[1]}, context=context)
95         else:
96             return self.set_sequence_top(cr, uid, ids, context=context)
97
98     def set_sequence_down(self, cr, uid, ids, context=None):
99         product = self.browse(cr, uid, ids[0], context=context)
100         cr.execute("""  SELECT id, website_sequence FROM product_template
101                         WHERE website_sequence < %s AND website_published = %s ORDER BY website_sequence DESC LIMIT 1""" % (product.website_sequence, product.website_published))
102         next = cr.fetchone()
103         if next:
104             self.write(cr, uid, [next[0]], {'website_sequence': product.website_sequence}, context=context)
105             return self.write(cr, uid, [ids[0]], {'website_sequence': next[1]}, context=context)
106         else:
107             return self.set_sequence_bottom(cr, uid, ids, context=context)
108
109     def recommended_products(self, cr, uid, ids, context=None):
110         id = ids[0]
111         product_ids = []
112         query = """
113             SELECT      sol.product_id
114             FROM        sale_order_line as my
115             LEFT JOIN   sale_order_line as sol
116             ON          sol.order_id = my.order_id
117             WHERE       my.product_id in (%s)
118             AND         sol.product_id not in (%s)
119             GROUP BY    sol.product_id
120             ORDER BY    COUNT(sol.order_id) DESC
121             LIMIT 10
122         """
123         cr.execute(query, (id, id))
124         for p in cr.fetchall():
125             product_ids.append(p[0])
126
127         # search to apply access rules
128         product_ids = self.search(cr, uid, [("id", "in", product_ids)], limit=3)
129         return self.browse(cr, uid, product_ids)
130
131     def img(self, cr, uid, ids, field='image_small', context=None):
132         return "/website/image?model=%s&field=%s&id=%s" % (self._name, field, ids[0])
133
134
135 class product_product(osv.Model):
136     _inherit = "product.product"
137
138     def _website_url(self, cr, uid, ids, field_name, arg, context=None):
139         res = {}
140         base_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url')
141         for product in self.browse(cr, uid, ids, context=context):
142             res[product.id] = "%s/shop/product/%s/" % (base_url, product.product_tmpl_id.id)
143         return res
144
145     _columns = {
146         'website_url': fields.function(_website_url, string="Website url", type="char"),
147     }
148
149     def img(self, cr, uid, ids, field='image_small', context=None):
150         temp_id = self.browse(cr, uid, ids[0], context=context).product_tmpl_id.id
151         return "/website/image?model=product.template&field=%s&id=%s" % (field, temp_id)