-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
br_deliverable_cost_report.py
120 lines (113 loc) · 5.16 KB
/
br_deliverable_cost_report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# -*- coding: utf-8 -*-
# © 2016 Elico Corp (https://www.elico-corp.com).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openerp import tools
from openerp import fields, models
class BusinessRequirementDeliverableCostReport(models.Model):
_name = "business.requirement.deliverable.cost.report"
_description = "Deliverable Cost Report"
_auto = False
br_name = fields.Char('Bus. Requirement', readonly=True)
responsible_id = fields.Many2one('res.users', 'Responsible', readonly=True)
partner_id = fields.Many2one('res.partner',
'Customer',
readonly=True)
project_id = fields.Many2one('project.project',
'Master Project',
readonly=True)
priority = fields.Selection([('0', 'Low'), ('1', 'Normal'),
('2', 'High')],
'Priority',
readonly=True)
state = fields.Selection(
[('draft', 'Draft'),
('confirmed', 'Confirmed'),
('approved', 'Approved'),
('stakeholder_approval', 'Stakeholder Approval'),
('in_progress', 'In progress'),
('done', 'Done'),
('cancel', 'Cancel'),
('drop', 'Drop'),
],
'Status',
readonly=True,
)
dlv_description = fields.Text('Deliverable Description', readonly=True)
dlv_product = fields.Many2one('product.product',
'Deliverable Product',
readonly=True)
res_description = fields.Text('Resource Description', readonly=True)
res_product = fields.Many2one('product.product',
'Resource Product',
readonly=True)
br_count = fields.Integer('BR Count', readonly=True)
dlv_count = fields.Integer('Deliverable Count', readonly=True)
res_count = fields.Integer('Resource Count', readonly=True)
dlv_qty = fields.Float('Deliverable Qty', readonly=True)
res_qty = fields.Float('Resource Qty', readonly=True)
sale_price = fields.Float('Sale Price', readonly=True)
total_revenue = fields.Float('Total Revenue', readonly=True)
cost_price = fields.Float('Cost price', readonly=True)
total_cost = fields.Float('Total cost', readonly=True)
gross_profit = fields.Float('Gross Profit', readonly=True)
def _select(self):
select_str = """
SELECT
br.id,
(select CONCAT('[',name,']', description) from
business_requirement where id=br.id) AS br_name,
br.responsible_id,
br.partner_id,
br.project_id,
br.priority,
br.state,
(select name from business_requirement_deliverable dlv where
dlv.business_requirement_id = br.id) as dlv_description,
(select product_id from business_requirement_deliverable dlv
where dlv.business_requirement_id = br.id) as dlv_product,
(select product_id from business_requirement_resource res
where id = br.id) as res_product,
(select name from business_requirement_resource res where
id = br.id) as res_description,
(select count(id) from business_requirement) as br_count,
(select count(id) from business_requirement_deliverable)
as dlv_count,
(select count(*) from business_requirement_resource res
where res.business_requirement_id = br.id) as res_count,
(select sum(qty) from business_requirement_resource)
as res_qty,
dlv.qty as dlv_qty,
dlv.sale_price_unit as sale_price,
(dlv.sale_price_unit * dlv.qty) as total_revenue,
(select sum(unit_price) from business_requirement_resource)
as cost_price,
((select sum(unit_price) from business_requirement_resource)
* (select sum(qty) from business_requirement_resource))
as total_cost,
((dlv.sale_price_unit * dlv.qty) - ((select sum(unit_price)
from business_requirement_resource) * (select sum(qty) from
business_requirement_resource))) as gross_profit
"""
return select_str
def _from(self):
from_str = """
FROM
business_requirement br,
business_requirement_deliverable dlv,
business_requirement_resource res
where br.id = dlv.business_requirement_id and
br.id = res.business_requirement_id
"""
return from_str
def _group_by(self):
group_by_str = """
GROUP BY
br_count,br.id,dlv.qty,dlv.sale_price_unit
"""
return group_by_str
def init(self, cr):
tools.drop_view_if_exists(cr, self._table)
cr.execute("""CREATE or REPLACE VIEW %s as (
%s %s %s
)""" % (self._table, self._select(), self._from(),
self._group_by()))