Skip to content

Commit

Permalink
[ADD] pos_sale: show pos data in sale report
Browse files Browse the repository at this point in the history
  • Loading branch information
hch-odoo committed Mar 8, 2018
1 parent f5f6453 commit a00cd81
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 4 deletions.
110 changes: 110 additions & 0 deletions addons/pos_sale/report/sale_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import tools
from odoo import api, fields, models


class SaleReport(models.Model):
_inherit = "sale.report"

def _with_qry(self):
res = super(SaleReport, self)._with_qry()
res = res + """, pol_tax AS (SELECT line_tax.pos_order_line_id AS pol_id,sum(tax.amount/100) AS amount
FROM account_tax_pos_order_line_rel line_tax
JOIN account_tax tax ON line_tax.account_tax_id = tax.id
GROUP BY line_tax.pos_order_line_id)"""
return res

def _select_pos(self):
select_str = """
SELECT min(l.id) AS id,
l.product_id AS product_id,
t.uom_id AS product_uom,
sum(l.qty * u.factor) AS product_uom_qty,
sum(l.qty * u.factor) AS qty_delivered,
CASE WHEN pos.state = 'invoiced' THEN sum(qty) ELSE 0 END AS qty_invoiced,
CASE WHEN pos.state != 'invoiced' THEN sum(qty) ELSE 0 END AS qty_to_invoice,
COALESCE((COALESCE((l.price_unit * (l.qty * u.factor)) + (l.price_unit * pol_tax.amount * (l.qty * u.factor)), l.price_unit * (l.qty * u.factor))), 1.0) AS price_total,
COALESCE(((l.qty * u.factor) * l.price_unit), 1.0) AS price_subtotal,
CASE WHEN pos.state != 'invoiced' THEN ((l.price_unit * (l.qty * u.factor)) + (l.price_unit * pol_tax.amount)) ELSE 0 END AS amount_to_invoice,
CASE WHEN pos.state = 'invoiced' THEN ((l.price_unit * (l.qty * u.factor)) + (l.price_unit * pol_tax.amount)) ELSE 0 END AS amount_invoiced,
count(*) AS nbr,
pos.name AS name,
pos.date_order AS date,
pos.date_order AS confirmation_date,
pos.state AS state,
pos.partner_id AS partner_id,
pos.user_id AS user_id,
pos.company_id AS company_id,
extract(epoch from avg(date_trunc('day',pos.date_order)-date_trunc('day',pos.create_date)))/(24*60*60)::decimal(16,2) AS delay,
t.categ_id AS categ_id,
pos.pricelist_id AS pricelist_id,
NULL AS analytic_account_id,
config.crm_team_id AS team_id,
p.product_tmpl_id,
partner.country_id AS country_id,
partner.commercial_partner_id AS commercial_partner_id,
(select sum(t.weight*l.qty/u.factor) from pos_order_line l
join product_product p on (l.product_id=p.id)
left join product_template t on (p.product_tmpl_id=t.id)
left join uom_uom u on (u.id=t.uom_id)) AS weight,
(select sum(t.volume*l.qty/u.factor) from pos_order_line l
join product_product p on (l.product_id=p.id)
left join product_template t on (p.product_tmpl_id=t.id)
left join uom_uom u on (u.id=t.uom_id)) AS volume
"""
return select_str

def _from_pos(self):
from_str = """
pos_order_line l
join pos_order pos on (l.order_id=pos.id)
join res_partner partner on pos.partner_id = partner.id
left join pol_tax pol_tax on pol_tax.pol_id=l.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 uom_uom u ON (u.id=t.uom_id)
LEFT JOIN pos_session session ON (session.id = pos.session_id)
LEFT JOIN pos_config config ON (config.id = session.config_id)
left join product_pricelist pp on (pos.pricelist_id = pp.id)
left join currency_rate cr on (cr.currency_id = pp.currency_id and
cr.company_id = pos.company_id and
cr.date_start <= coalesce(pos.date_order, now()) and
(cr.date_end is null or cr.date_end > coalesce(pos.date_order, now())))
"""
return from_str

def _group_by_pos(self):
group_by_str = """
GROUP BY l.order_id,
l.product_id,
l.price_unit,
l.qty,
t.uom_id,
t.categ_id,
pos.name,
pos.date_order,
pos.partner_id,
pos.user_id,
pos.state,
pos.company_id,
pos.pricelist_id,
p.product_tmpl_id,
partner.country_id,
partner.commercial_partner_id,
pol_tax.amount,
u.factor,
config.crm_team_id
"""
return group_by_str

@api.model_cr
def init(self):
tools.drop_view_if_exists(self.env.cr, self._table)
qry = ("""CREATE or REPLACE VIEW %s AS ( %s
%s
FROM ( %s )
%s UNION ALL %s FROM ( %s ) %s
)""" % (self._table, self._with_qry(), self._select(), self._from(), self._group_by(), self._select_pos(), self._from_pos(), self._group_by_pos()))
self.env.cr.execute(qry)
10 changes: 6 additions & 4 deletions addons/sale/report/sale_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ class SaleReport(models.Model):
weight = fields.Float('Gross Weight', readonly=True)
volume = fields.Float('Volume', readonly=True)

def _with_qry(self):
return """WITH currency_rate as (%s)"""% self.env['res.currency']._select_companies_rates()

def _select(self):
select_str = """
WITH currency_rate as (%s)
SELECT min(l.id) as id,
l.product_id as product_id,
t.uom_id as product_uom,
Expand Down Expand Up @@ -78,7 +80,7 @@ def _select(self):
partner.commercial_partner_id as commercial_partner_id,
sum(p.weight * l.product_uom_qty / u.factor * u2.factor) as weight,
sum(p.volume * l.product_uom_qty / u.factor * u2.factor) as volume
""" % self.env['res.currency']._select_companies_rates()
"""
return select_str

def _from(self):
Expand Down Expand Up @@ -124,11 +126,11 @@ def _group_by(self):
def init(self):
# self._table = sale_report
tools.drop_view_if_exists(self.env.cr, self._table)
self.env.cr.execute("""CREATE or REPLACE VIEW %s as (
self.env.cr.execute("""CREATE or REPLACE VIEW %s as (%s
%s
FROM ( %s )
%s
)""" % (self._table, self._select(), self._from(), self._group_by()))
)""" % (self._table, self._with_qry(), self._select(), self._from(), self._group_by()))

class SaleOrderReportProforma(models.AbstractModel):
_name = 'report.sale.report_saleproforma'
Expand Down
3 changes: 3 additions & 0 deletions addons/sale_margin/report/sale_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ class SaleReport(models.Model):

def _select(self):
return super(SaleReport, self)._select() + ", SUM(l.margin / COALESCE(cr.rate, 1.0)) AS margin"

def _select_pos(self):
return super(SaleReport, self)._select_pos() + ", NULL AS margin"
3 changes: 3 additions & 0 deletions addons/sale_stock/report/sale_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ def _select(self):

def _group_by(self):
return super(SaleReport, self)._group_by() + ", s.warehouse_id"

def _select_pos(self):
return super(SaleReport, self)._select_pos() + ", NULL AS warehouse_id"

0 comments on commit a00cd81

Please sign in to comment.