[FIX] website_sale: apply tax position on checkout signup
[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 class product_attribue(osv.Model):
25     # TODO merge product.attribute, mrp.properties product_manufacturer_attributes
26     _name = "product.attribute"
27     _columns = {
28         'name': fields.char('Name', translate=True, required=True),
29         'value_ids': fields.one2many('product.attribute.value', 'attribute_id', 'Values'),
30     }
31
32 class product_attribute_value(osv.Model):
33     _name = "product.attribute.value"
34     _columns = {
35         'attribute_id': fields.many2one('product.attribute', 'attribute', required=True),
36         'name': fields.char('Value', translate=True, required=True),
37     }
38
39 class product_attribute_line(osv.Model):
40     _name = "product.attribute.line"
41     _order = 'attribute_id, value_id'
42     _columns = {
43         'product_tmpl_id': fields.many2one('product.template', 'Product', required=True),
44         'attribute_id': fields.many2one('product.attribute', 'attribute', required=True),
45         'value_id': fields.many2one('product.attribute.value', 'Textual Value'),
46     }
47
48     def onchange_attribute_id(self, cr, uid, ids, attribute_id, context=None):
49         return {'value': {'value_id': False}}
50
51 class product_style(osv.Model):
52     _name = "product.style"
53     _columns = {
54         'name' : fields.char('Style Name', required=True),
55         'html_class': fields.char('HTML Classes'),
56     }
57
58 class product_pricelist(osv.Model):
59     _inherit = "product.pricelist"
60     _columns = {
61         'code': fields.char('Promotional Code'),
62     }
63
64 class product_template(osv.Model):
65     _inherit = ["product.template", "website.seo.metadata"]
66     _order = 'website_published desc, website_sequence desc, name'
67     _name = 'product.template'
68
69     def _website_url(self, cr, uid, ids, field_name, arg, context=None):
70         res = dict.fromkeys(ids, '')
71         for product in self.browse(cr, uid, ids, context=context):
72             res[product.id] = "/shop/product/%s" % (product.id,)
73         return res
74
75     _columns = {
76         'attribute_lines': fields.one2many('product.attribute.line', 'product_tmpl_id', 'Product attributes'),
77         # TODO FIXME tde: when website_mail/mail_thread.py inheritance work -> this field won't be necessary
78         'website_message_ids': fields.one2many(
79             'mail.message', 'res_id',
80             domain=lambda self: [
81                 '&', ('model', '=', self._name), ('type', '=', 'comment')
82             ],
83             string='Website Comments',
84         ),
85         'website_published': fields.boolean('Available in the website'),
86         'website_description': fields.html('Description for the website'),
87         'alternative_product_ids': fields.many2many('product.template','product_alternative_rel','src_id','dest_id', string='Alternative Products', help='Appear on the product page'),
88         'accessory_product_ids': fields.many2many('product.template','product_accessory_rel','src_id','dest_id', string='Accessory Products', help='Appear on the shopping cart'),
89         'website_size_x': fields.integer('Size X'),
90         'website_size_y': fields.integer('Size Y'),
91         'website_style_ids': fields.many2many('product.style', string='Styles'),
92         'website_sequence': fields.integer('Sequence', help="Determine the display order in the Website E-commerce"),
93         'website_url': fields.function(_website_url, string="Website url", type="char"),
94     }
95
96     def _defaults_website_sequence(self, cr, uid, *l, **kwargs):
97         cr.execute('SELECT MAX(website_sequence)+1 FROM product_template')
98         next_sequence = cr.fetchone()[0] or 0
99         return next_sequence
100
101     _defaults = {
102         'website_size_x': 1,
103         'website_size_y': 1,
104         'website_sequence': _defaults_website_sequence,
105         'website_published': False,
106     }
107
108     def website_reorder(self, cr, uid, ids, operation=None, context=None):
109         if operation == "top":
110             cr.execute('SELECT MAX(website_sequence) FROM product_template')
111             seq = (cr.fetchone()[0] or 0) + 1
112         if operation == "bottom":
113             cr.execute('SELECT MIN(website_sequence) FROM product_template')
114             seq = (cr.fetchone()[0] or 0) -1
115         if operation == "up":
116             product = self.browse(cr, uid, ids[0], context=context)
117             cr.execute("""  SELECT id, website_sequence FROM product_template
118                             WHERE website_sequence > %s AND website_published = %s ORDER BY website_sequence ASC LIMIT 1""" % (product.website_sequence, product.website_published))
119             prev = cr.fetchone()
120             if prev:
121                 self.write(cr, uid, [prev[0]], {'website_sequence': product.website_sequence}, context=context)
122                 return self.write(cr, uid, [ids[0]], {'website_sequence': prev[1]}, context=context)
123             else:
124                 return self.website_reorder(cr, uid, ids, operation='top', context=context)
125         if operation == "down":
126             product = self.browse(cr, uid, ids[0], context=context)
127             cr.execute("""  SELECT id, website_sequence FROM product_template
128                             WHERE website_sequence < %s AND website_published = %s ORDER BY website_sequence DESC LIMIT 1""" % (product.website_sequence, product.website_published))
129             next = cr.fetchone()
130             if next:
131                 self.write(cr, uid, [next[0]], {'website_sequence': product.website_sequence}, context=context)
132                 return self.write(cr, uid, [ids[0]], {'website_sequence': next[1]}, context=context)
133             else:
134                 return self.website_reorder(cr, uid, ids, operation='bottom', context=context)
135         return self.write(cr, uid, ids, {'website_sequence': seq}, context=context)
136
137     def img(self, cr, uid, ids, field='image_small', context=None):
138         return "/website/image?model=%s&field=%s&id=%s" % (self._name, field, ids[0])
139
140 class product_product(osv.Model):
141     _inherit = "product.product"
142
143     def _website_url(self, cr, uid, ids, field_name, arg, context=None):
144         res = {}
145         for product in self.browse(cr, uid, ids, context=context):
146             res[product.id] = "/shop/product/%s" % (product.product_tmpl_id.id,)
147         return res
148
149     _columns = {
150         'website_url': fields.function(_website_url, string="Website url", type="char"),
151     }
152
153     def img(self, cr, uid, ids, field='image_small', context=None):
154         temp_id = self.browse(cr, uid, ids[0], context=context).product_tmpl_id.id
155         return "/website/image?model=product.template&field=%s&id=%s" % (field, temp_id)
156
157 # vim:et: