diff --git a/addons/purchase/report/purchase_report.py b/addons/purchase/report/purchase_report.py index 6b68cecf1a5..4c59f9ea002 100644 --- a/addons/purchase/report/purchase_report.py +++ b/addons/purchase/report/purchase_report.py @@ -92,26 +92,23 @@ class purchase_report(osv.osv): s.company_id as company_id, l.product_id, t.categ_id as category_id, - (case when u.uom_type not in ('reference') then - (select id from product_uom where uom_type='reference' and category_id = 1) - else - u.id - end) as product_uom, + t.uom_id as product_uom, s.location_id as location_id, - sum(l.product_qty/u.factor) as quantity, + sum(l.product_qty/u.factor*u2.factor) as quantity, extract(epoch from age(s.date_approve,s.date_order))/(24*60*60)::decimal(16,2) as delay, extract(epoch from age(l.date_planned,s.date_order))/(24*60*60)::decimal(16,2) as delay_pass, count(*) as nbr, - (l.price_unit*l.product_qty*u.factor)::decimal(16,2) as price_total, - avg(100.0 * (l.price_unit*l.product_qty*u.factor) / NULLIF(t.standard_price*l.product_qty*u.factor, 0.0))::decimal(16,2) as negociation, + (l.price_unit*l.product_qty)::decimal(16,2) as price_total, + avg(100.0 * (l.price_unit*l.product_qty) / NULLIF(t.standard_price*l.product_qty/u.factor*u2.factor, 0.0))::decimal(16,2) as negociation, - sum(t.standard_price*l.product_qty*u.factor)::decimal(16,2) as price_standard, - (sum(l.product_qty*l.price_unit)/NULLIF(sum(l.product_qty*u.factor),0.0))::decimal(16,2) as price_average + sum(t.standard_price*l.product_qty/u.factor*u2.factor)::decimal(16,2) as price_standard, + (sum(l.product_qty*l.price_unit)/NULLIF(sum(l.product_qty/u.factor*u2.factor),0.0))::decimal(16,2) as price_average from purchase_order s left join purchase_order_line l on (s.id=l.order_id) left join product_product p on (l.product_id=p.id) left join product_template t on (p.product_tmpl_id=t.id) left join product_uom u on (u.id=l.product_uom) + left join product_uom u2 on (u2.id=t.uom_id) where l.product_id is not null group by s.company_id, @@ -139,7 +136,9 @@ class purchase_report(osv.osv): s.warehouse_id, u.uom_type, u.category_id, - u.id + t.uom_id, + u.id, + u2.factor ) """) purchase_report() diff --git a/addons/sale/report/sale_report.py b/addons/sale/report/sale_report.py index e4f917ba063..b266b5b6391 100644 --- a/addons/sale/report/sale_report.py +++ b/addons/sale/report/sale_report.py @@ -38,7 +38,7 @@ class sale_report(osv.osv): ('10', 'October'), ('11', 'November'), ('12', 'December')], 'Month', readonly=True), 'day': fields.char('Day', size=128, readonly=True), 'product_id': fields.many2one('product.product', 'Product', readonly=True), - 'uom_name': fields.char('Reference UoM', size=128, readonly=True), + 'product_uom': fields.many2one('product.uom', 'UoM', readonly=True), 'product_uom_qty': fields.float('# of Qty', readonly=True), 'partner_id': fields.many2one('res.partner', 'Partner', readonly=True), @@ -67,51 +67,41 @@ class sale_report(osv.osv): tools.drop_view_if_exists(cr, 'sale_report') cr.execute(""" create or replace view sale_report as ( - select el.*, - -- (select count(1) from sale_order_line where order_id = s.id) as nbr, - (select 1) as nbr, - s.date_order as date, - s.date_confirm as date_confirm, - to_char(s.date_order, 'YYYY') as year, - to_char(s.date_order, 'MM') as month, - to_char(s.date_order, 'YYYY-MM-DD') as day, - s.partner_id as partner_id, - s.user_id as user_id, - s.shop_id as shop_id, - s.company_id as company_id, - extract(epoch from avg(date_trunc('day',s.date_confirm)-date_trunc('day',s.create_date)))/(24*60*60)::decimal(16,2) as delay, - s.state, - s.shipped, - s.shipped::integer as shipped_qty_1, - s.pricelist_id as pricelist_id, - s.project_id as analytic_account_id + select + min(l.id) as id, + l.product_id as product_id, + t.uom_id as product_uom, + sum(l.product_uom_qty / u.factor * u2.factor) as product_uom_qty, + sum(l.product_uom_qty * l.price_unit * (100.0-l.discount) / 100.0) as price_total, + 1 as nbr, + s.date_order as date, + s.date_confirm as date_confirm, + to_char(s.date_order, 'MM') as month, + to_char(s.date_order, 'YYYY-MM-DD') as day, + s.partner_id as partner_id, + s.user_id as user_id, + s.shop_id as shop_id, + s.company_id as company_id, + extract(epoch from avg(date_trunc('day',s.date_confirm)-date_trunc('day',s.create_date)))/(24*60*60)::decimal(16,2) as delay, + s.state, + t.categ_id as categ_id, + s.shipped, + s.shipped::integer as shipped_qty_1, + s.pricelist_id as pricelist_id, + s.project_id as analytic_account_id from - sale_order s, - ( - select l.id as id, - l.product_id as product_id, - (case when u.uom_type not in ('reference') then - (select name from product_uom where uom_type='reference' and category_id=u.category_id) - else - u.name - end) as uom_name, - sum(l.product_uom_qty * u.factor) as product_uom_qty, - sum(l.product_uom_qty * l.price_unit) as price_total, - pt.categ_id, l.order_id - from - sale_order_line l ,product_uom u, product_product p, product_template pt - where u.id = l.product_uom - and pt.id = p.product_tmpl_id - and p.id = l.product_id - group by l.id, l.order_id, l.product_id, u.name, pt.categ_id, u.uom_type, u.category_id) el - where s.id = el.order_id - group by el.id, - el.product_id, - el.uom_name, - el.product_uom_qty, - el.price_total, - el.categ_id, - el.order_id, + sale_order s + left join sale_order_line l on (s.id=l.order_id) + left join product_product p on (l.product_id=p.id) + left join product_template t on (p.product_tmpl_id=t.id) + left join product_uom u on (u.id=l.product_uom) + left join product_uom u2 on (u2.id=t.uom_id) + group by + l.product_id, + l.product_uom_qty, + l.order_id, + t.uom_id, + t.categ_id, s.date_order, s.date_confirm, s.partner_id,