Debian Codegraph @

addons/point_of_sale/report/pos_order_report.py

raw
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 from odoo import fields, models, tools
5
6
7 class ReportPosOrder(models.Model):
8 _name = 'report.pos.order'
9 _description = "Point of Sale Orders Report"
10 _auto = False
11 _order = 'date desc'
12 _rec_name = 'order_id'
13
14 date = fields.Datetime(string='Order Date', readonly=True)
15 order_id = fields.Many2one('pos.order', string='Order', readonly=True)
16 partner_id = fields.Many2one('res.partner', string='Customer', readonly=True)
17 product_id = fields.Many2one('product.product', string='Product', readonly=True)
18 product_tmpl_id = fields.Many2one('product.template', string='Product Template', readonly=True)
19 state = fields.Selection(
20 [('draft', 'New'), ('paid', 'Paid'), ('done', 'Posted'), ('cancel', 'Cancelled')],
21 string='Status', readonly=True)
22 user_id = fields.Many2one('res.users', string='User', readonly=True)
23 price_total = fields.Float(string='Total Price', readonly=True)
24 price_sub_total = fields.Float(string='Subtotal w/o discount', readonly=True)
25 price_subtotal_excl = fields.Float(string='Subtotal w/o Tax', readonly=True)
26 total_discount = fields.Float(string='Total Discount', readonly=True)
27 average_price = fields.Float(string='Average Price', readonly=True, aggregator="avg")
28 company_id = fields.Many2one('res.company', string='Company', readonly=True)
29 nbr_lines = fields.Integer(string='Sale Line Count', readonly=True)
30 product_qty = fields.Integer(string='Product Quantity', readonly=True)
31 journal_id = fields.Many2one('account.journal', string='Journal', readonly=True)
32 delay_validation = fields.Integer(string='Delay Validation', readonly=True)
33 product_categ_id = fields.Many2one('product.category', string='Product Category', readonly=True)
34 pos_categ_id = fields.Many2one('pos.category', string='Point of Sale Category', readonly=True)
35 invoiced = fields.Boolean(readonly=True)
36 config_id = fields.Many2one('pos.config', string='Point of Sale', readonly=True)
37 pricelist_id = fields.Many2one('product.pricelist', string='Pricelist', readonly=True)
38 session_id = fields.Many2one('pos.session', string='Session', readonly=True)
39 margin = fields.Float(string='Margin', readonly=True)
40 payment_method_id = fields.Many2one('pos.payment.method', string='Payment Method', readonly=True)
41
42 def _select(self):
43 return """
44 -- The purpose of this CTE is to map each "pos_order_line" to the "payment_method_id" corresponding to its "pos_order"
45 -- considering we always show the first "payment_method_id"
46 WITH payment_method_by_order_line AS (
47 SELECT
48 pol.id AS pos_order_line_id,
49 pm.pos_order_id as pos_order_id,
50 (array_agg(pm.payment_method_id ORDER BY pm.id ASC))[1] AS payment_method_id
51 FROM pos_order_line pol
52 LEFT JOIN pos_order po ON (po.id = pol.order_id)
53 LEFT JOIN pos_payment pm ON (pm.pos_order_id=po.id)
54 GROUP BY pol.id, pm.pos_order_id
55 ),
56 first_pos_category AS (
57 SELECT
58 pt.id AS product_template_id,
59 (array_agg(pc.id))[1] AS id
60 FROM product_template pt
61 LEFT JOIN pos_category_product_template_rel pcpt ON (pt.id = pcpt.product_template_id)
62 LEFT JOIN pos_category pc ON (pcpt.pos_category_id = pc.id)
63 GROUP BY pt.id
64 )
65 SELECT
66 l.id AS id,
67 1 AS nbr_lines, -- number of lines in order line is always 1
68 s.date_order AS date,
69 ROUND((l.price_subtotal) / CASE COALESCE(s.currency_rate, 0) WHEN 0 THEN 1.0 ELSE s.currency_rate END, cu.decimal_places) AS price_subtotal_excl,
70 l.qty AS product_qty,
71 l.qty * l.price_unit / COALESCE(NULLIF(s.currency_rate, 0), 1.0) AS price_sub_total,
72 ROUND((l.price_subtotal_incl) / COALESCE(NULLIF(s.currency_rate, 0), 1.0), cu.decimal_places) AS price_total,
73 (l.qty * l.price_unit) * (l.discount / 100) / COALESCE(NULLIF(s.currency_rate, 0), 1.0) AS total_discount,
74 CASE
75 WHEN l.qty * u.factor = 0 THEN NULL
76 ELSE (l.qty*l.price_unit / COALESCE(NULLIF(s.currency_rate, 0), 1.0))/(l.qty * u.factor)::decimal
77 END AS average_price,
78 cast(to_char(date_trunc('day',s.date_order) - date_trunc('day',s.create_date),'DD') AS INT) AS delay_validation,
79 s.id as order_id,
80 s.partner_id AS partner_id,
81 s.state AS state,
82 s.user_id AS user_id,
83 s.company_id AS company_id,
84 s.sale_journal AS journal_id,
85 l.product_id AS product_id,
86 pt.categ_id AS product_categ_id,
87 p.product_tmpl_id,
88 ps.config_id,
89 s.pricelist_id,
90 s.session_id,
91 s.account_move IS NOT NULL AS invoiced,
92 (l.price_subtotal * CASE WHEN s.is_refund THEN -1 ELSE 1 END) - COALESCE(l.total_cost,0) / COALESCE(NULLIF(s.currency_rate, 0), 1.0) AS margin,
93 pm.payment_method_id AS payment_method_id,
94 fpc.id AS pos_categ_id
95
96 """
97
98 def _from(self):
99 return """
100 FROM pos_order_line AS l
101 INNER JOIN pos_order s ON (s.id=l.order_id)
102 LEFT JOIN product_product p ON (l.product_id=p.id)
103 LEFT JOIN product_template pt ON (p.product_tmpl_id=pt.id)
104 LEFT JOIN uom_uom u ON (u.id=pt.uom_id)
105 LEFT JOIN pos_session ps ON (s.session_id=ps.id)
106 LEFT JOIN res_company co ON (s.company_id=co.id)
107 LEFT JOIN res_currency cu ON (co.currency_id=cu.id)
108 LEFT JOIN payment_method_by_order_line pm ON (pm.pos_order_line_id=l.id)
109 LEFT JOIN pos_payment_method ppm ON (pm.payment_method_id=ppm.id)
110 LEFT JOIN first_pos_category fpc ON (pt.id = fpc.product_template_id)
111 """
112
113 def _group_by(self):
114 return ""
115
116 def init(self):
117 tools.drop_view_if_exists(self.env.cr, self._table)
118 self.env.cr.execute("""
119 CREATE OR REPLACE VIEW %s AS (
120 %s
121 %s
122 )
123 """ % (self._table, self._select(), self._from())
124 )