8
.. i18n: Open ERP objects can be created from PostgreSQL views. The technique is as follows :
10
Open ERP objects can be created from PostgreSQL views. The technique is as follows :
12
.. i18n: 1. Declare your _columns dictionary. All fields must have the flag **readonly=True.**
13
.. i18n: 2. Specify the parameter **_auto=False** to the Open ERP object, so no table corresponding to the _columns dictionnary is created automatically.
14
.. i18n: 3. Add a method **init(self, cr)** that creates a *PostgreSQL* View matching the fields declared in _columns.
16
1. Declare your _columns dictionary. All fields must have the flag **readonly=True.**
17
2. Specify the parameter **_auto=False** to the Open ERP object, so no table corresponding to the _columns dictionnary is created automatically.
18
3. Add a method **init(self, cr)** that creates a *PostgreSQL* View matching the fields declared in _columns.
20
.. i18n: **Example** The object report_crm_case_user follows this model.
22
**Example** The object report_crm_case_user follows this model.
24
.. i18n: .. code-block:: python
26
.. i18n: class report_crm_case_user(osv.osv):
27
.. i18n: _name = "report.crm.case.user"
28
.. i18n: _description = "Cases by user and section"
29
.. i18n: _auto = False
31
.. i18n: 'name': fields.date('Month', readonly=True),
32
.. i18n: 'user_id':fields.many2one('res.users', 'User', readonly=True, relate=True),
33
.. i18n: 'section_id':fields.many2one('crm.case.section', 'Section', readonly=True, relate=True),
34
.. i18n: 'amount_revenue': fields.float('Est.Revenue', readonly=True),
35
.. i18n: 'amount_costs': fields.float('Est.Cost', readonly=True),
36
.. i18n: 'amount_revenue_prob': fields.float('Est. Rev*Prob.', readonly=True),
37
.. i18n: 'nbr': fields.integer('# of Cases', readonly=True),
38
.. i18n: 'probability': fields.float('Avg. Probability', readonly=True),
39
.. i18n: 'state': fields.selection(AVAILABLE_STATES, 'State', size=16, readonly=True),
40
.. i18n: 'delay_close': fields.integer('Delay to close', readonly=True),
42
.. i18n: _order = 'name desc, user_id, section_id'
44
.. i18n: def init(self, cr):
45
.. i18n: cr.execute("""
46
.. i18n: create or replace view report_crm_case_user as (
48
.. i18n: min(c.id) as id,
49
.. i18n: substring(c.create_date for 7)||'-01' as name,
52
.. i18n: c.section_id,
53
.. i18n: count(*) as nbr,
54
.. i18n: sum(planned_revenue) as amount_revenue,
55
.. i18n: sum(planned_cost) as amount_costs,
56
.. i18n: sum(planned_revenue*probability)::decimal(16,2) as amount_revenue_prob,
57
.. i18n: avg(probability)::decimal(16,2) as probability,
58
.. i18n: to_char(avg(date_closed-c.create_date), 'DD"d" `HH24:MI:SS') as delay_close
61
.. i18n: group by substring(c.create_date for 7), c.state, c.user_id, c.section_id
63
.. i18n: report_crm_case_user()
65
.. code-block:: python
67
class report_crm_case_user(osv.osv):
68
_name = "report.crm.case.user"
69
_description = "Cases by user and section"
72
'name': fields.date('Month', readonly=True),
73
'user_id':fields.many2one('res.users', 'User', readonly=True, relate=True),
74
'section_id':fields.many2one('crm.case.section', 'Section', readonly=True, relate=True),
75
'amount_revenue': fields.float('Est.Revenue', readonly=True),
76
'amount_costs': fields.float('Est.Cost', readonly=True),
77
'amount_revenue_prob': fields.float('Est. Rev*Prob.', readonly=True),
78
'nbr': fields.integer('# of Cases', readonly=True),
79
'probability': fields.float('Avg. Probability', readonly=True),
80
'state': fields.selection(AVAILABLE_STATES, 'State', size=16, readonly=True),
81
'delay_close': fields.integer('Delay to close', readonly=True),
83
_order = 'name desc, user_id, section_id'
87
create or replace view report_crm_case_user as (
90
substring(c.create_date for 7)||'-01' as name,
95
sum(planned_revenue) as amount_revenue,
96
sum(planned_cost) as amount_costs,
97
sum(planned_revenue*probability)::decimal(16,2) as amount_revenue_prob,
98
avg(probability)::decimal(16,2) as probability,
99
to_char(avg(date_closed-c.create_date), 'DD"d" `HH24:MI:SS') as delay_close
102
group by substring(c.create_date for 7), c.state, c.user_id, c.section_id
104
report_crm_case_user()