[MIG][IMP] product: migrate and clean product pricelist report
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<openerp>
|
||||
<data>
|
||||
<odoo>
|
||||
<template id="report_pricelist">
|
||||
<t t-call="report.html_container">
|
||||
<t t-call="report.internal_layout">
|
||||
@@ -10,15 +9,15 @@
|
||||
<div class="row mt32 mb32">
|
||||
<div class="col-xs-3">
|
||||
<strong>Price List Name</strong>:<br/>
|
||||
<span t-esc="get_pricelist(data['form']['price_list'])"/>
|
||||
<span t-esc="data['pricelist'].name"/>
|
||||
</div>
|
||||
<div class="col-xs-3">
|
||||
<strong>Currency</strong>:<br/>
|
||||
<span t-esc="get_currency(data['form']['price_list'])"/>
|
||||
<span t-esc="data['pricelist'].currency_id.name"/>
|
||||
</div>
|
||||
<div class="col-xs-3">
|
||||
<strong>Print date</strong>:<br/>
|
||||
<t t-esc="formatLang(time.strftime('%Y-%m-%d'), date=True)"/>
|
||||
<t t-esc="datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -28,34 +27,32 @@
|
||||
<th>
|
||||
<strong>Description</strong>
|
||||
</th>
|
||||
<t t-foreach="get_titles(data['form'])" t-as="k">
|
||||
<th t-if="k.has_key('qty1')"><strong t-esc="k['qty1']"/></th>
|
||||
<th t-if="k.has_key('qty2')"><strong t-esc="k['qty2']"/></th>
|
||||
<th t-if="k.has_key('qty3')"><strong t-esc="k['qty3']"/></th>
|
||||
<th t-if="k.has_key('qty4')"><strong t-esc="k['qty4']"/></th>
|
||||
<th t-if="k.has_key('qty5')"><strong t-esc="k['qty5']"/></th>
|
||||
<t t-foreach="data['quantities']" t-as="quantity">
|
||||
<th><strong t-esc="'%s units' % quantity"/></th>
|
||||
</t>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<t t-foreach="get_categories(objects,data['form'])" t-as="c">
|
||||
<t t-foreach="data['categories_data']" t-as="categ_data">
|
||||
<tr>
|
||||
<td colspan="100">
|
||||
<strong t-esc="c['name']"/>
|
||||
<strong t-esc="categ_data['category'].name"/>
|
||||
</td>
|
||||
</tr>
|
||||
<tr t-foreach="c['products']" t-as="p">
|
||||
<tr t-foreach="categ_data['products']" t-as="product">
|
||||
<td>
|
||||
<t t-if="p.get('code')">
|
||||
[<span t-esc="p['code']"/>]
|
||||
<t t-if="product.code">
|
||||
[<span t-esc="product.code"/>]
|
||||
</t>
|
||||
<span t-esc="p['name']"/>
|
||||
<span t-esc="product.name"/>
|
||||
</td>
|
||||
<td t-if="p.has_key('qty1') and p['qty1']"><strong t-esc="p['qty1']"/></td>
|
||||
<td t-if="p.has_key('qty2') and p['qty2']"><strong t-esc="p['qty2']"/></td>
|
||||
<td t-if="p.has_key('qty3') and p['qty3']"><strong t-esc="p['qty3']"/></td>
|
||||
<td t-if="p.has_key('qty4') and p['qty4']"><strong t-esc="p['qty4']"/></td>
|
||||
<td t-if="p.has_key('qty5') and p['qty5']"><strong t-esc="p['qty5']"/></td>
|
||||
<t t-foreach="data['quantities']" t-as="quantity">
|
||||
<td><strong t-esc="categ_data['prices'][product.id][quantity]"
|
||||
t-esc-options="{
|
||||
'widget': 'monetary',
|
||||
'display_currency': data['pricelist'].currency_id}"/>
|
||||
</td>
|
||||
</t>
|
||||
</tr>
|
||||
</t>
|
||||
</tbody>
|
||||
@@ -64,5 +61,4 @@
|
||||
</t>
|
||||
</t>
|
||||
</template>
|
||||
</data>
|
||||
</openerp>
|
||||
</odoo>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<openerp>
|
||||
<odoo>
|
||||
<data>
|
||||
<template id="report_simple_label">
|
||||
<div class="col-xs-4" style="padding:0;">
|
||||
@@ -44,4 +44,4 @@
|
||||
</t>
|
||||
</template>
|
||||
</data>
|
||||
</openerp>
|
||||
</odoo>
|
||||
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<openerp>
|
||||
<odoo>
|
||||
<data>
|
||||
<report
|
||||
id="report_product_label"
|
||||
@@ -26,4 +26,4 @@
|
||||
file="product.report_pricelist"
|
||||
menu="False"/>
|
||||
</data>
|
||||
</openerp>
|
||||
</odoo>
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<openerp>
|
||||
<data>
|
||||
<odoo>
|
||||
<template id="report_producttemplatelabel">
|
||||
<t t-call="report.html_container">
|
||||
<div class="page">
|
||||
@@ -14,5 +13,4 @@
|
||||
</div>
|
||||
</t>
|
||||
</template>
|
||||
</data>
|
||||
</openerp>
|
||||
</odoo>
|
||||
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<openerp>
|
||||
<odoo>
|
||||
<data noupdate="0">
|
||||
|
||||
<record id="group_sale_pricelist" model="res.groups">
|
||||
@@ -70,4 +70,4 @@
|
||||
</record>
|
||||
|
||||
</data>
|
||||
</openerp>
|
||||
</odoo>
|
||||
|
||||
Reference in New Issue
Block a user