From 1b4f96cf4067409fa2e526ea8a002ec49007b6f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Thu, 28 Jul 2016 15:46:13 +0200 Subject: [PATCH] [MIG][IMP] product: migrate and clean product pricelist report --- addons/product/models/product.py | 19 +-- addons/product/models/product_pricelist.py | 9 +- addons/product/report/product_pricelist.py | 151 ++++++------------ .../report/product_pricelist_templates.xml | 44 +++-- .../report/product_product_templates.xml | 4 +- addons/product/report/product_reports.xml | 4 +- .../report/product_template_templates.xml | 6 +- addons/product/security/product_security.xml | 4 +- 8 files changed, 92 insertions(+), 149 deletions(-) diff --git a/addons/product/models/product.py b/addons/product/models/product.py index 5a8828c0551..b0e9f7bee86 100644 --- a/addons/product/models/product.py +++ b/addons/product/models/product.py @@ -6,12 +6,12 @@ import time from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DEFAULT_SERVER_DATE_FORMAT -import odoo.addons.decimal_precision as dp - from odoo import api, fields, models, tools, _ from odoo.exceptions import ValidationError from odoo.osv import expression +import odoo.addons.decimal_precision as dp + class ProductCategory(models.Model): _name = "product.category" @@ -309,11 +309,11 @@ class ProductProduct(models.Model): if self.uom_id and self.uom_po_id and self.uom_id.category_id != self.uom_po_id.category_id: self.uom_po_id = self.uom_id - def create(self, cr, uid, vals, context=None): - ctx = dict(context or {}, create_product_product=True) - product_id = super(ProductProduct, self).create(cr, uid, vals, context=ctx) - self._set_standard_price(cr, uid, [product_id], vals.get('standard_price', 0.0), context=context) - return product_id + @api.model + def create(self, vals): + product = super(ProductProduct, self.with_context(create_product_product=True)).create(vals) + product._set_standard_price(vals.get('standard_price', 0.0)) + return product @api.multi def write(self, values): @@ -534,8 +534,9 @@ class ProductProduct(models.Model): # compatibility to remove after v10 - DEPRECATED - def price_get(self, cr, uid, ids, ptype='list_price', context=None): - return self.browse(cr, uid, ids, context=context).price_compute(ptype) + @api.multi + def price_get(self, ptype='list_price'): + return self.price_compute(ptype) @api.multi def _set_standard_price(self, value): diff --git a/addons/product/models/product_pricelist.py b/addons/product/models/product_pricelist.py index a8756f0edd5..b574858c0b9 100644 --- a/addons/product/models/product_pricelist.py +++ b/addons/product/models/product_pricelist.py @@ -18,12 +18,7 @@ class Pricelist(models.Model): return self.env.user.company_id.currency_id.id def _get_default_item_ids(self): - vals = {} - # ProductPricelistItem = self.pool.get('product.pricelist.item') - # fields_list = ProductPricelistItem._defaults.keys() - # vals = ProductPricelistItem.default_get(cr, uid, fields_list, context=context) - vals['compute_price'] = 'formula' - return [[0, False, vals]] + return [[0, False, {'compute_price': 'formula'}]] name = fields.Char('Pricelist Name', required=True, translate=True) active = fields.Boolean('Active', default=True, help="If unchecked, it will allow you to hide the pricelist without removing it.") @@ -373,7 +368,7 @@ class PricelistItem(models.Model): return True @api.constrains('price_min_margin', 'price_max_margin') - def _check_margin(self, cr, uid, ids, context=None): + def _check_margin(self): if any(item.price_min_margin > item.price_max_margin for item in self): raise ValidationError(_('Error! The minimum margin should be lower than the maximum margin.')) return True diff --git a/addons/product/report/product_pricelist.py b/addons/product/report/product_pricelist.py index b2b8e856965..6b9641e7bb2 100644 --- a/addons/product/report/product_pricelist.py +++ b/addons/product/report/product_pricelist.py @@ -1,105 +1,58 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -import time -from openerp.osv import osv -from openerp.report import report_sxw -from openerp.tools import float_round +from odoo import api, models +from odoo.tools import float_round -class product_pricelist(report_sxw.rml_parse): - def __init__(self, cr, uid, name, context): - super(product_pricelist, self).__init__(cr, uid, name, context=context) - self.pricelist=False - self.quantity=[] - self.localcontext.update({ - 'time': time, - 'get_pricelist': self._get_pricelist, - 'get_currency': self._get_currency, - 'get_categories': self._get_categories, - 'get_price': self._get_price, - 'get_titles': self._get_titles, - }) - - def _get_titles(self, form): - lst = [] - vals = {} - qtys = 1 - - for i in range(1,6): - if form['qty'+str(i)]!=0: - vals['qty'+str(qtys)] = str(form['qty'+str(i)]) + ' units' - qtys += 1 - lst.append(vals) - return lst - - def _set_quantity(self, form): - for i in range(1,6): - q = 'qty%d'%i - if form[q] >0 and form[q] not in self.quantity: - self.quantity.append(form[q]) - else: - self.quantity.append(0) - return True - - def _get_pricelist(self, pricelist_id): - pricelist = self.pool.get('product.pricelist').read(self.cr, self.uid, [pricelist_id], ['name'], context=self.localcontext)[0] - return pricelist['name'] - - def _get_currency(self, pricelist_id): - pricelist = self.pool.get('product.pricelist').read(self.cr, self.uid, [pricelist_id], ['currency_id'], context=self.localcontext)[0] - return pricelist['currency_id'][1] - - def _get_categories(self, products, form): - cat_ids=[] - res=[] - self.pricelist = form['price_list'] - self._set_quantity(form) - pro_ids=[] - for product in products: - pro_ids.append(product.id) - if product.categ_id.id not in cat_ids: - cat_ids.append(product.categ_id.id) - - cats = self.pool.get('product.category').name_get(self.cr, self.uid, cat_ids, context=self.localcontext) - if not cats: - return res - for cat in cats: - product_ids=self.pool.get('product.product').search(self.cr, self.uid, [('id', 'in', pro_ids), ('categ_id', '=', cat[0])], context=self.localcontext) - products = [] - for product in self.pool.get('product.product').read(self.cr, self.uid, product_ids, ['name', 'code'], context=self.localcontext): - val = { - 'id':product['id'], - 'name':product['name'], - 'code':product['code'] - } - i = 1 - for qty in self.quantity: - if qty == 0: - val['qty'+str(i)] = 0.0 - else: - val['qty'+str(i)]=self._get_price(self.pricelist, product['id'], qty) - i += 1 - products.append(val) - res.append({'name':cat[1],'products': products}) - return res - - def _get_price(self, pricelist_id, product_id, qty): - sale_price_digits = self.get_digits(dp='Product Price') - pricelist = self.pool.get('product.pricelist').browse(self.cr, self.uid, [pricelist_id], context=self.localcontext)[0] - product = self.pool['product.product'].browse(self.cr, self.uid, product_id, context=self.localcontext) - price = self.pool.get('product.pricelist').get_product_price(self.cr, self.uid, [pricelist_id], product, qty, False, context=self.localcontext) - if price: - price = float_round(price, precision_digits=sale_price_digits) - else: - res = self.pool.get('product.product').read(self.cr, self.uid, [product_id], ['list_price']) - price = float_round(res[0]['list_price'], precision_digits=sale_price_digits) - price = self.formatLang(price, digits=sale_price_digits, currency_obj=pricelist.currency_id) - return price - - -class report_product_pricelist(osv.AbstractModel): +class report_product_pricelist(models.AbstractModel): _name = 'report.product.report_pricelist' - _inherit = 'report.abstract_report' - _template = 'product.report_pricelist' - _wrapped_report_class = product_pricelist + + @api.multi + def render_html(self, data=None): + data = data if data is not None else {} + pricelist = self.env['product.pricelist'].browse(data.get('form', {}).get('price_list', False)) + products = self.env['product.product'].browse(data.get('ids', data.get('active_ids'))) + quantities = self._get_quantity(data) + docargs = { + 'doc_ids': data.get('ids', data.get('active_ids')), + 'doc_model': 'hr.contribution.register', + 'docs': products, + 'data': dict( + data, + pricelist=pricelist, + quantities=quantities, + categories_data=self._get_categories(pricelist, products, quantities) + ), + } + return self.env['report'].render('product.report_pricelist', docargs) + + def _get_quantity(self, data): + return sorted([data['form'][key] for key in data['form'].keys() if key.startswith('qty') and data['form'][key]]) + + def _get_categories(self, pricelist, products, quantities): + categ_data = [] + categories = self.env['product.category'] + for product in products: + categories |= product.categ_id + + for category in categories: + categ_products = products.filtered(lambda product: product.categ_id == category) + prices = {} + for categ_product in categ_products: + prices[categ_product.id] = dict.fromkeys(quantities, 0.0) + for quantity in quantities: + prices[categ_product.id][quantity] = self._get_price(pricelist, categ_product, quantity) + categ_data.append({ + 'category': category, + 'products': categ_products, + 'prices': prices, + }) + return categ_data + + def _get_price(self, pricelist, product, qty): + sale_price_digits = self.env['decimal.precision'].precision_get('Product Price') + price = pricelist.get_product_price(product, qty, False) + if not price: + price = product.list_price + return float_round(price, precision_digits=sale_price_digits) diff --git a/addons/product/report/product_pricelist_templates.xml b/addons/product/report/product_pricelist_templates.xml index 9b990f3c068..59643e7222f 100644 --- a/addons/product/report/product_pricelist_templates.xml +++ b/addons/product/report/product_pricelist_templates.xml @@ -1,6 +1,5 @@ - - + - - + diff --git a/addons/product/report/product_product_templates.xml b/addons/product/report/product_product_templates.xml index 74eb5d473b0..60e11d8d38c 100644 --- a/addons/product/report/product_product_templates.xml +++ b/addons/product/report/product_product_templates.xml @@ -1,5 +1,5 @@ - + - \ No newline at end of file + \ No newline at end of file diff --git a/addons/product/report/product_reports.xml b/addons/product/report/product_reports.xml index 2a124c8f553..11c802515e8 100644 --- a/addons/product/report/product_reports.xml +++ b/addons/product/report/product_reports.xml @@ -1,5 +1,5 @@ - + - + diff --git a/addons/product/report/product_template_templates.xml b/addons/product/report/product_template_templates.xml index 84d911dfbc6..b2390ca5140 100644 --- a/addons/product/report/product_template_templates.xml +++ b/addons/product/report/product_template_templates.xml @@ -1,6 +1,5 @@ - - + - - \ No newline at end of file + \ No newline at end of file diff --git a/addons/product/security/product_security.xml b/addons/product/security/product_security.xml index 161626d5bdb..9aba121200c 100644 --- a/addons/product/security/product_security.xml +++ b/addons/product/security/product_security.xml @@ -1,5 +1,5 @@ - + @@ -70,4 +70,4 @@ - +