revert changes 2324
[odoo/odoo.git] / addons / product / pricelist.py
index a8509f1..a1bec9c 100644 (file)
@@ -2,7 +2,7 @@
 ##############################################################################
 #
 #    OpenERP, Open Source Management Solution  
-#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
+#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
 #    $Id$
 #
 #    This program is free software: you can redistribute it and/or modify
@@ -26,6 +26,7 @@ from osv import fields, osv
 from _common import rounding
 import time
 from tools import config
+from tools.misc import ustr
 from tools.translate import _
 
 class price_type(osv.osv):
@@ -36,14 +37,11 @@ class price_type(osv.osv):
         sale and purchase prices based on some fields of the product.
     """
     def _price_field_get(self, cr, uid, context={}):
-        import tools
-        cr.execute('select name, field_description, model from ir_model_fields where model in (%s,%s) and ttype=%s order by name', ('product.product', 'product.template', 'float'))
+        mf = self.pool.get('ir.model.fields')
+        ids = mf.search(cr, uid, [('model','in', (('product.product'),('product.template'))), ('ttype','=','float')], context=context)
         res = []
-        for field in cr.dictfetchall():
-            desc = tools.translate(cr, field['model'] + ',' + field['name'], 'field', context.get('lang', False) or 'en_US')
-            if not desc:
-                desc = field['field_description']
-            res.append((field['name'], desc))
+        for field in mf.browse(cr, uid, ids, context=context):
+            res.append((field.name, field.field_description))
         return res
 
     def _get_currency(self, cr, uid, ctx):
@@ -58,7 +56,7 @@ class price_type(osv.osv):
     _columns = {
         "name" : fields.char("Price Name", size=32, required=True, translate=True, help="Name of this kind of price."),
         "active" : fields.boolean("Active"),
-        "field" : fields.selection(_price_field_get, "Product Field", required=True, help="Associated field in the product form."),
+        "field" : fields.selection(_price_field_get, "Product Field", size=32, required=True, help="Associated field in the product form."),
         "currency_id" : fields.many2one('res.currency', "Currency", required=True, help="The currency the field is expressed in."),
     }
     _defaults = {
@@ -75,7 +73,7 @@ class product_pricelist_type(osv.osv):
     _name = "product.pricelist.type"
     _description = "Pricelist Type"
     _columns = {
-        'name': fields.char('Name',size=64, required=True),
+        'name': fields.char('Name',size=64, required=True, translate=True),
         'key': fields.char('Key', size=64, required=True, help="Used in the code to select specific prices based on the context. Keep unchanged."),
     }
 product_pricelist_type()
@@ -94,8 +92,19 @@ class product_pricelist(osv.osv):
         'version_id': fields.one2many('product.pricelist.version', 'pricelist_id', 'Pricelist Versions'),
         'currency_id': fields.many2one('res.currency', 'Currency', required=True),
     }
+    
+    def name_get(self, cr, uid, ids, context={}):
+        result= []
+        if not all(ids):
+            return result
+        for pl in self.browse(cr, uid, ids, context):
+            name = pl.name + ' ('+ pl.currency_id.name + ')'
+            result.append((pl.id,name))
+        return result
+    
+
     def _get_currency(self, cr, uid, ctx):
-        comp = self.pool.get('res.users').browse(cr,uid,uid).company_id
+        comp = self.pool.get('res.users').browse(cr, uid, uid).company_id
         if not comp:
             comp_id = self.pool.get('res.company').search(cr, uid, [])[0]
             comp = self.pool.get('res.company').browse(cr, uid, comp_id)
@@ -243,52 +252,42 @@ class product_pricelist_version(osv.osv):
     _columns = {
         'pricelist_id': fields.many2one('product.pricelist', 'Price List',
             required=True, select=True),
-        'name': fields.char('Name', size=64, required=True),
-        'active': fields.boolean('Active'),
+        'name': fields.char('Name', size=64, required=True, translate=True),
+        'active': fields.boolean('Active',
+            help="When a version is duplicated it is set to non active, so that the " \
+            "dates do not overlaps with original version. You should change the dates " \
+            "and reactivate the pricelist"),
         'items_id': fields.one2many('product.pricelist.item',
             'price_version_id', 'Price List Items', required=True),
-        'date_start': fields.date('Start Date', help="Starting date for validity of this pricelist version."),
-        'date_end': fields.date('End Date', help="Ending date for validity of this pricelist version."),
+        'date_start': fields.date('Start Date', help="Starting date for this pricelist version to be valid."),
+        'date_end': fields.date('End Date', help="Ending date for this pricelist version to be valid."),
     }
     _defaults = {
         'active': lambda *a: 1,
     }
 
-    #
-    # TODO: improve this function ?
-    #
+    # We desactivate duplicated pricelists, so that dates do not overlap
+    def copy(self, cr, uid, id, default=None,context={}):
+        if not default: default= {}
+        default['active'] = False
+        return super(product_pricelist_version, self).copy(cr, uid, id, default, context)
+
     def _check_date(self, cursor, user, ids):
         for pricelist_version in self.browse(cursor, user, ids):
             if not pricelist_version.active:
                 continue
+            where = []
+            if pricelist_version.date_start:
+                where.append("((date_end>='%s') or (date_end is null))" % (pricelist_version.date_start,))
+            if pricelist_version.date_end:
+                where.append("((date_start<='%s') or (date_start is null))" % (pricelist_version.date_end,))
+
             cursor.execute('SELECT id ' \
                     'FROM product_pricelist_version ' \
-                    'WHERE ((date_start <= %s AND %s <= date_end ' \
-                            'AND date_end IS NOT NULL) ' \
-                        'OR (date_end IS NULL AND date_start IS NOT NULL ' \
-                            'AND date_start <= %s) ' \
-                        'OR (date_start IS NULL AND date_end IS NOT NULL ' \
-                            'AND %s <= date_end) ' \
-                        'OR (date_start IS NULL AND date_end IS NULL) ' \
-                        'OR (%s = \'0000-01-01\' AND date_start IS NULL) ' \
-                        'OR (%s = \'0000-01-01\' AND date_end IS NULL) ' \
-                        'OR (%s = \'0000-01-01\' AND %s = \'0000-01-01\') ' \
-                        'OR (%s = \'0000-01-01\' AND date_start <= %s) ' \
-                        'OR (%s = \'0000-01-01\' AND %s <= date_end)) ' \
-                        'AND pricelist_id = %s ' \
+                    'WHERE '+' and '.join(where) + (where and ' and ' or '')+
+                        'pricelist_id = %s ' \
                         'AND active ' \
-                        'AND id <> %s', (pricelist_version.date_end or '0000-01-01',
-                            pricelist_version.date_start or '0000-01-01',
-                            pricelist_version.date_end or '0000-01-01',
-                            pricelist_version.date_start or '0000-01-01',
-                            pricelist_version.date_start or '0000-01-01',
-                            pricelist_version.date_end or '0000-01-01',
-                            pricelist_version.date_start or '0000-01-01',
-                            pricelist_version.date_end or '0000-01-01',
-                            pricelist_version.date_start or '0000-01-01',
-                            pricelist_version.date_end or '0000-01-01',
-                            pricelist_version.date_end or '0000-01-01',
-                            pricelist_version.date_start or '0000-01-01',
+                        'AND id <> %s', (
                             pricelist_version.pricelist_id.id,
                             pricelist_version.id))
             if cursor.fetchall():
@@ -296,7 +295,7 @@ class product_pricelist_version(osv.osv):
         return True
 
     _constraints = [
-        (_check_date, 'You can not have 2 pricelist version that overlaps!',
+        (_check_date, 'You cannot have 2 pricelist versions that overlap!',
             ['date_start', 'date_end'])
     ]
 
@@ -304,14 +303,11 @@ product_pricelist_version()
 
 class product_pricelist_item(osv.osv):
     def _price_field_get(self, cr, uid, context={}):
-        cr.execute('select id,name from product_price_type where active')
-        import tools
+        pt = self.pool.get('product.price.type')
+        ids = pt.search(cr, uid, [], context=context)
         result = []
-        for line in cr.fetchall():
-            transl_name = tools.translate(cr, 'product.price.type,name', 'model', ('lang' in context) and context['lang'] or 'en_US', line[1])
-            if not transl_name:
-                transl_name = line[1]
-            result.append((line[0], transl_name))
+        for line in pt.browse(cr, uid, ids, context=context):
+            result.append((line.id, line.name))
 
         result.append((-1, _('Other Pricelist')))
         result.append((-2, _('Partner section of the product form')))
@@ -333,9 +329,9 @@ class product_pricelist_item(osv.osv):
         'product_id': fields.many2one('product.product', 'Product', ondelete='cascade', help="Set a product if this rule only apply to one product. Keep empty for all products"),
         'categ_id': fields.many2one('product.category', 'Product Category', ondelete='cascade', help="Set a category of product if this rule only apply to products of a category and his childs. Keep empty for all products"),
 
-        'min_quantity': fields.integer('Min. Quantity', required=True, help="The rule only apply if the partner buys/sells more than this quantity."),
+        'min_quantity': fields.integer('Min. Quantity', required=True, help="The rule only applies if the partner buys/sells more than this quantity."),
         'sequence': fields.integer('Sequence', required=True),
-        'base': fields.selection(_price_field_get, 'Based on', required=True, size=-1, help="The mode of computation of the price for this rule."),
+        'base': fields.selection(_price_field_get, 'Based on', required=True, size=-1, help="The mode for computing the price for this rule."),
         'base_pricelist_id': fields.many2one('product.pricelist', 'If Other Pricelist'),
 
         'price_surcharge': fields.float('Price Surcharge',
@@ -345,11 +341,11 @@ class product_pricelist_item(osv.osv):
             digits=(16, int(config['price_accuracy'])),
             help="Sets the price so that it is a multiple of this value.\n" \
               "Rounding is applied after the discount and before the surcharge.\n" \
-              "To have prices that ends by 9.99, set rounding 10, surcharge -0.01" \
+              "To have prices that end in 9.99, set rounding 10, surcharge -0.01" \
             ),
-        'price_min_margin': fields.float('Price Min. Margin',
+        'price_min_margin': fields.float('Min. Price Margin',
             digits=(16, int(config['price_accuracy']))),
-        'price_max_margin': fields.float('Price Max. Margin',
+        'price_max_margin': fields.float('Max. Price Margin',
             digits=(16, int(config['price_accuracy']))),
     }
     def product_id_change(self, cr, uid, ids, product_id, context={}):