~eficent/eficent-openerp-project-management/trunk

« back to all changes in this revision

Viewing changes to project_cost_analysis/report/account_analytic_resource_usage.py

  • Committer: jb.eficent
  • Date: 2014-10-21 08:31:22 UTC
  • Revision ID: jordi.ballester@eficent.com-20141021083122-a4h9w7okk3vt344d
Complete module refactorization

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
##############################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6
 
#
7
 
#    This program is free software: you can redistribute it and/or modify
8
 
#    it under the terms of the GNU Affero General Public License as
9
 
#    published by the Free Software Foundation, either version 3 of the
10
 
#    License, or (at your option) any later version.
11
 
#
12
 
#    This program is distributed in the hope that it will be useful,
13
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
#    GNU Affero General Public License for more details.
16
 
#
17
 
#    You should have received a copy of the GNU Affero General Public License
18
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
#
20
 
##############################################################################
21
 
 
22
 
from osv import fields,osv
23
 
import tools
24
 
 
25
 
class report_account_analytic_resource_usage(osv.osv):
26
 
    _name = "report.account.analytic.resource.usage"
27
 
    _description = "Resource Usage Analysis"
28
 
    _auto = False
29
 
    _columns = {        
30
 
        'date': fields.date('Date', readonly=True),
31
 
        'day': fields.char('Day', size=128, readonly=True),
32
 
        'month':fields.selection([('01','January'), ('02','February'), ('03','March'), ('04','April'), ('05','May'), ('06','June'), ('07','July'), ('08','August'), ('09','September'), ('10','October'), ('11','November'), ('12','December')], 'Month', readonly=True),
33
 
        'year': fields.char('Year', size=64, required=False, readonly=True),
34
 
        'period_id': fields.many2one('account.period', 'Period', readonly=True),
35
 
        'account_id': fields.many2one('account.analytic.account', 'Analytic Account', readonly=True),
36
 
        'product_id': fields.many2one('product.product', 'Product', readonly=True),
37
 
        'product_uom_id': fields.many2one('product.uom', 'UoM', readonly=True),
38
 
        'user_id': fields.many2one('res.users', 'User', readonly=True),
39
 
        'amount_plan': fields.float('Planned Amount', readonly=True, help='Calculated by multiplying the quantity and the price given in the Product\'s cost price. Always expressed in the company main currency.'),
40
 
        'amount_commit': fields.float('Commited Amount', readonly=True, help='Calculated by multiplying the quantity and the price given in the Product\'s cost price. Always expressed in the company main currency.'),
41
 
        'unit_amount_plan': fields.float('Planned Quantity', help='Specifies the amount of quantity to count.'),
42
 
        'unit_amount_commit': fields.float('Commited Quantity', help='Specifies the amount of quantity to count.'),
43
 
        'amount_real': fields.float('Real Amount', readonly=True, help='Calculated by multiplying the quantity and the price given in the Product\'s cost price. Always expressed in the company main currency.'),
44
 
        'unit_amount_real': fields.float('Real Quantity', readonly=True, help='Specifies the amount of quantity to count.'),        
45
 
    }
46
 
 
47
 
 
48
 
    def init(self, cr):
49
 
        
50
 
        """ 
51
 
            @param cr: the current row, from the database cursor,
52
 
        """
53
 
        tools.drop_view_if_exists(cr, 'report_account_analytic_resource_usage')
54
 
        
55
 
        cr.execute("""
56
 
            create or replace view report_account_analytic_resource_usage as (
57
 
                
58
 
              SELECT  
59
 
                    ROW_NUMBER() over (order by tot.date) as id,
60
 
                    tot.date as date,
61
 
                    to_char(tot.date, 'YYYY') as year,
62
 
                    to_char(tot.date, 'MM') as month,
63
 
                    to_char(tot.date, 'YYYY-MM-DD') as day,
64
 
                    sum(tot.unit_amount_real) as unit_amount_real, 
65
 
                    sum(tot.unit_amount_plan) as unit_amount_plan, 
66
 
                    sum(tot.unit_amount_commit) as unit_amount_commit,
67
 
                    sum(tot.amount_real) as amount_real, 
68
 
                    sum(tot.amount_plan) as amount_plan,  
69
 
                    sum(tot.amount_commit) as amount_commit,
70
 
                    tot.period_id, 
71
 
                    tot.account_id, 
72
 
                    tot.product_id, 
73
 
                    tot.product_uom_id, 
74
 
                    tot.user_id 
75
 
                FROM
76
 
                    (SELECT
77
 
                         CAST( unit_amount AS FLOAT) AS unit_amount_real, 
78
 
                         CAST( 0 AS FLOAT) AS unit_amount_plan, 
79
 
                         CAST( 0 AS FLOAT) AS unit_amount_commit,
80
 
                         CAST( amount AS FLOAT) AS amount_real, 
81
 
                         CAST( 0 AS FLOAT) AS amount_plan,
82
 
                         CAST( 0 AS FLOAT) AS amount_commit, 
83
 
                         date, 
84
 
                         period_id, 
85
 
                         account_id, 
86
 
                         product_id, 
87
 
                         product_uom_id, 
88
 
                         user_id 
89
 
                    FROM account_analytic_line 
90
 
 
91
 
                UNION ALL
92
 
                    SELECT 
93
 
                        CAST( 0 AS FLOAT) AS unit_amount_real,
94
 
                        CAST( unit_amount AS FLOAT) AS unit_amount_plan,
95
 
                        CAST( 0 AS FLOAT) AS unit_amount_commit, 
96
 
                        CAST( 0 AS FLOAT) AS amount_real, 
97
 
                        CAST( amount AS FLOAT) AS amount_plan, 
98
 
                        CAST( 0 AS FLOAT) AS amount_commit,
99
 
                        date, 
100
 
                        period_id, 
101
 
                        account_id, 
102
 
                        product_id, 
103
 
                        product_uom_id, 
104
 
                        user_id 
105
 
                    FROM account_analytic_line_plan
106
 
                UNION ALL
107
 
                    SELECT 
108
 
                        CAST( 0 AS FLOAT) AS unit_amount_real,
109
 
                        CAST( 0 AS FLOAT) AS unit_amount_plan,
110
 
                        CAST( unit_amount AS FLOAT) AS unit_amount_commit, 
111
 
                        CAST( 0 AS FLOAT) AS amount_real, 
112
 
                        CAST( 0 AS FLOAT) AS amount_plan,
113
 
                        CAST( amount AS FLOAT) AS amount_commit, 
114
 
                        date, 
115
 
                        period_id, 
116
 
                        account_id, 
117
 
                        product_id, 
118
 
                        product_uom_id, 
119
 
                        user_id 
120
 
                    FROM account_analytic_line_commit                    
121
 
                    
122
 
                ) AS tot
123
 
                GROUP BY 
124
 
                    tot.date, 
125
 
                    tot.period_id, 
126
 
                    tot.account_id, 
127
 
                    tot.product_id, 
128
 
                    tot.product_uom_id, 
129
 
                    user_id
130
 
                ORDER BY tot.date
131
 
 
132
 
            )""")               
133
 
 
134
 
        
135
 
report_account_analytic_resource_usage()
136
 
 
137