# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution # Copyright (C) 2004-2010 Tiny SPRL (). # # 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 . # ############################################################################## import time from openerp import tools from openerp.osv import fields, osv from openerp.tools.translate import _ import openerp.addons.decimal_precision as dp class price_type(osv.osv): """ The price type is used to points which field in the product form is a price and in which currency is this price expressed. When a field is a price, you can use it in pricelists to base sale and purchase prices based on some fields of the product. """ def _price_field_get(self, cr, uid, context=None): 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 mf.browse(cr, uid, ids, context=context): res.append((field.name, field.field_description)) return res def _get_field_currency(self, cr, uid, fname, ctx): ids = self.search(cr, uid, [('field','=',fname)], context=ctx) return self.browse(cr, uid, ids, context=ctx)[0].currency_id def _get_currency(self, cr, uid, ctx): 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) return comp.currency_id.id _name = "product.price.type" _description = "Price Type" _columns = { "name" : fields.char("Price Name", required=True, translate=True, help="Name of this kind of price."), "active" : fields.boolean("Active"), "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 = { "active": lambda *args: True, "currency_id": _get_currency } #---------------------------------------------------------- # Price lists #---------------------------------------------------------- class product_pricelist_type(osv.osv): _name = "product.pricelist.type" _description = "Pricelist Type" _columns = { 'name': fields.char('Name', required=True, translate=True), 'key': fields.char('Key', required=True, help="Used in the code to select specific prices based on the context. Keep unchanged."), } class product_pricelist(osv.osv): def _pricelist_type_get(self, cr, uid, context=None): pricelist_type_obj = self.pool.get('product.pricelist.type') pricelist_type_ids = pricelist_type_obj.search(cr, uid, [], order='name') pricelist_types = pricelist_type_obj.read(cr, uid, pricelist_type_ids, ['key','name'], context=context) res = [] for type in pricelist_types: res.append((type['key'],type['name'])) return res _name = "product.pricelist" _description = "Pricelist" _order = 'name' _columns = { 'name': fields.char('Pricelist Name', required=True, translate=True), 'active': fields.boolean('Active', help="If unchecked, it will allow you to hide the pricelist without removing it."), 'type': fields.selection(_pricelist_type_get, 'Pricelist Type', required=True), 'version_id': fields.one2many('product.pricelist.version', 'pricelist_id', 'Pricelist Versions', copy=True), 'currency_id': fields.many2one('res.currency', 'Currency', required=True), 'company_id': fields.many2one('res.company', 'Company'), } def name_get(self, cr, uid, ids, context=None): result= [] if not all(ids): return result for pl in self.browse(cr, uid, ids, context=context): name = pl.name + ' ('+ pl.currency_id.name + ')' result.append((pl.id,name)) return result def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100): if name and operator == '=' and not args: # search on the name of the pricelist and its currency, opposite of name_get(), # Used by the magic context filter in the product search view. query_args = {'name': name, 'limit': limit, 'lang': (context or {}).get('lang') or 'en_US'} query = """SELECT p.id FROM (( SELECT pr.id, pr.name FROM product_pricelist pr JOIN res_currency cur ON (pr.currency_id = cur.id) WHERE pr.name || ' (' || cur.name || ')' = %(name)s ) UNION ( SELECT tr.res_id as id, tr.value as name FROM ir_translation tr JOIN product_pricelist pr ON ( pr.id = tr.res_id AND tr.type = 'model' AND tr.name = 'product.pricelist,name' AND tr.lang = %(lang)s ) JOIN res_currency cur ON (pr.currency_id = cur.id) WHERE tr.value || ' (' || cur.name || ')' = %(name)s ) ) p ORDER BY p.name""" if limit: query += " LIMIT %(limit)s" cr.execute(query, query_args) ids = [r[0] for r in cr.fetchall()] # regular search() to apply ACLs - may limit results below limit in some cases ids = self.search(cr, uid, [('id', 'in', ids)], limit=limit, context=context) if ids: return self.name_get(cr, uid, ids, context) return super(product_pricelist, self).name_search( cr, uid, name, args, operator=operator, context=context, limit=limit) def _get_currency(self, cr, uid, ctx): 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) return comp.currency_id.id _defaults = { 'active': lambda *a: 1, "currency_id": _get_currency } def price_get_multi(self, cr, uid, ids, products_by_qty_by_partner, context=None): return dict((key, dict((key, price[0]) for key, price in value.items())) for key, value in self.price_rule_get_multi(cr, uid, ids, products_by_qty_by_partner, context=context).items()) def price_rule_get_multi(self, cr, uid, ids, products_by_qty_by_partner, context=None): """multi products 'price_get'. @param ids: @param products_by_qty: @param partner: @param context: { 'date': Date of the pricelist (%Y-%m-%d),} @return: a dict of dict with product_id as key and a dict 'price by pricelist' as value """ if not ids: ids = self.pool.get('product.pricelist').search(cr, uid, [], context=context) results = {} for pricelist in self.browse(cr, uid, ids, context=context): subres = self._price_rule_get_multi(cr, uid, pricelist, products_by_qty_by_partner, context=context) for product_id,price in subres.items(): results.setdefault(product_id, {}) results[product_id][pricelist.id] = price return results def _price_get_multi(self, cr, uid, pricelist, products_by_qty_by_partner, context=None): return dict((key, price[0]) for key, price in self._price_rule_get_multi(cr, uid, pricelist, products_by_qty_by_partner, context=context).items()) def _price_rule_get_multi(self, cr, uid, pricelist, products_by_qty_by_partner, context=None): context = context or {} date = context.get('date') or time.strftime('%Y-%m-%d') products = map(lambda x: x[0], products_by_qty_by_partner) currency_obj = self.pool.get('res.currency') product_obj = self.pool.get('product.template') product_uom_obj = self.pool.get('product.uom') price_type_obj = self.pool.get('product.price.type') if not products: return {} version = False for v in pricelist.version_id: if ((v.date_start is False) or (v.date_start <= date)) and ((v.date_end is False) or (v.date_end >= date)): version = v break if not version: raise osv.except_osv(_('Warning!'), _("At least one pricelist has no active version !\nPlease create or activate one.")) categ_ids = {} for p in products: categ = p.categ_id while categ: categ_ids[categ.id] = True categ = categ.parent_id categ_ids = categ_ids.keys() is_product_template = products[0]._name == "product.template" if is_product_template: prod_tmpl_ids = [tmpl.id for tmpl in products] prod_ids = [product.id for product in tmpl.product_variant_ids for tmpl in products] else: prod_ids = [product.id for product in products] prod_tmpl_ids = [product.product_tmpl_id.id for product in products] # Load all rules cr.execute( 'SELECT i.id ' 'FROM product_pricelist_item AS i ' 'WHERE (product_tmpl_id IS NULL OR product_tmpl_id = any(%s)) ' 'AND (product_id IS NULL OR (product_id = any(%s))) ' 'AND ((categ_id IS NULL) OR (categ_id = any(%s))) ' 'AND (price_version_id = %s) ' 'ORDER BY sequence, min_quantity desc', (prod_tmpl_ids, prod_ids, categ_ids, version.id)) item_ids = [x[0] for x in cr.fetchall()] items = self.pool.get('product.pricelist.item').browse(cr, uid, item_ids, context=context) price_types = {} results = {} for product, qty, partner in products_by_qty_by_partner: uom_price_already_computed = False results[product.id] = 0.0 price = False rule_id = False for rule in items: if rule.min_quantity and qty='%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 '+' and '.join(where) + (where and ' and ' or '')+ 'pricelist_id = %s ' \ 'AND active ' \ 'AND id <> %s', ( pricelist_version.pricelist_id.id, pricelist_version.id)) if cursor.fetchall(): return False return True _constraints = [ (_check_date, 'You cannot have 2 pricelist versions that overlap!', ['date_start', 'date_end']) ] class product_pricelist_item(osv.osv): def _price_field_get(self, cr, uid, context=None): pt = self.pool.get('product.price.type') ids = pt.search(cr, uid, [], context=context) result = [] for line in pt.browse(cr, uid, ids, context=context): result.append((line.id, line.name)) result.append((-1, _('Other Pricelist'))) result.append((-2, _('Supplier Prices on the product form'))) return result # Added default function to fetch the Price type Based on Pricelist type. def _get_default_base(self, cr, uid, fields, context=None): product_price_type_obj = self.pool.get('product.price.type') if fields.get('type') == 'purchase': product_price_type_ids = product_price_type_obj.search(cr, uid, [('field', '=', 'standard_price')], context=context) elif fields.get('type') == 'sale': product_price_type_ids = product_price_type_obj.search(cr, uid, [('field','=','list_price')], context=context) else: return -1 if not product_price_type_ids: return False else: pricetype = product_price_type_obj.browse(cr, uid, product_price_type_ids, context=context)[0] return pricetype.id _name = "product.pricelist.item" _description = "Pricelist item" _order = "sequence, min_quantity desc" _defaults = { 'base': _get_default_base, 'min_quantity': lambda *a: 0, 'sequence': lambda *a: 5, 'price_discount': lambda *a: 0, } def _check_recursion(self, cr, uid, ids, context=None): for obj_list in self.browse(cr, uid, ids, context=context): if obj_list.base == -1: main_pricelist = obj_list.price_version_id.pricelist_id.id other_pricelist = obj_list.base_pricelist_id.id if main_pricelist == other_pricelist: return False return True def _check_margin(self, cr, uid, ids, context=None): for item in self.browse(cr, uid, ids, context=context): if item.price_max_margin and item.price_min_margin and (item.price_min_margin > item.price_max_margin): return False return True _columns = { 'name': fields.char('Rule Name', help="Explicit rule name for this pricelist line."), 'price_version_id': fields.many2one('product.pricelist.version', 'Price List Version', required=True, select=True, ondelete='cascade'), 'product_tmpl_id': fields.many2one('product.template', 'Product Template', ondelete='cascade', help="Specify a template if this rule only applies to one product template. Keep empty otherwise."), 'product_id': fields.many2one('product.product', 'Product', ondelete='cascade', help="Specify a product if this rule only applies to one product. Keep empty otherwise."), 'categ_id': fields.many2one('product.category', 'Product Category', ondelete='cascade', help="Specify a product category if this rule only applies to products belonging to this category or its children categories. Keep empty otherwise."), 'min_quantity': fields.integer('Min. Quantity', required=True, help="For the rule to apply, bought/sold quantity must be greater than or equal to minimum quantity specified in this field."), 'sequence': fields.integer('Sequence', required=True, help="Gives the order in which the pricelist items will be checked. The evaluation gives highest priority to lowest sequence and stops as soon as a matching item is found."), 'base': fields.selection(_price_field_get, 'Based on', required=True, size=-1, help="Base price for computation."), 'base_pricelist_id': fields.many2one('product.pricelist', 'Other Pricelist'), 'price_surcharge': fields.float('Price Surcharge', digits_compute= dp.get_precision('Product Price'), help='Specify the fixed amount to add or substract(if negative) to the amount calculated with the discount.'), 'price_discount': fields.float('Price Discount', digits=(16,4)), 'price_round': fields.float('Price Rounding', digits_compute= dp.get_precision('Product Price'), 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 end in 9.99, set rounding 10, surcharge -0.01" \ ), 'price_min_margin': fields.float('Min. Price Margin', digits_compute= dp.get_precision('Product Price'), help='Specify the minimum amount of margin over the base price.'), 'price_max_margin': fields.float('Max. Price Margin', digits_compute= dp.get_precision('Product Price'), help='Specify the maximum amount of margin over the base price.'), 'company_id': fields.related('price_version_id','company_id',type='many2one', readonly=True, relation='res.company', string='Company', store=True) } _constraints = [ (_check_recursion, 'Error! You cannot assign the Main Pricelist as Other Pricelist in PriceList Item!', ['base_pricelist_id']), (_check_margin, 'Error! The minimum margin should be lower than the maximum margin.', ['price_min_margin', 'price_max_margin']) ] def product_id_change(self, cr, uid, ids, product_id, context=None): if not product_id: return {} prod = self.pool.get('product.product').read(cr, uid, [product_id], ['code','name']) if prod[0]['code']: return {'value': {'name': prod[0]['code']}} return {} # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: