9
.. i18n: OpenERP objects can be created from PostgreSQL views. The technique is as follows :
12
OpenERP objects can be created from PostgreSQL views. The technique is as follows :
14
.. i18n: 1. Declare your _columns dictionary. All fields must have the flag **readonly=True.**
15
.. i18n: 2. Specify the parameter **_auto=False** to the OpenERP object, so no table corresponding to the _columns dictionnary is created automatically.
16
.. i18n: 3. Add a method **init(self, cr)** that creates a *PostgreSQL* View matching the fields declared in _columns.
19
1. Declare your _columns dictionary. All fields must have the flag **readonly=True.**
20
2. Specify the parameter **_auto=False** to the OpenERP object, so no table corresponding to the _columns dictionnary is created automatically.
21
3. Add a method **init(self, cr)** that creates a *PostgreSQL* View matching the fields declared in _columns.
23
.. i18n: **Example** The object report_crm_case_user follows this model.
26
**Example** The object report_crm_case_user follows this model.
28
.. i18n: .. code-block:: python
30
.. i18n: class report_crm_case_user(osv.osv):
31
.. i18n: _name = "report.crm.case.user"
32
.. i18n: _description = "Cases by user and section"
33
.. i18n: _auto = False
35
.. i18n: 'name': fields.date('Month', readonly=True),
36
.. i18n: 'user_id':fields.many2one('res.users', 'User', readonly=True, relate=True),
37
.. i18n: 'section_id':fields.many2one('crm.case.section', 'Section', readonly=True, relate=True),
38
.. i18n: 'amount_revenue': fields.float('Est.Revenue', readonly=True),
39
.. i18n: 'amount_costs': fields.float('Est.Cost', readonly=True),
40
.. i18n: 'amount_revenue_prob': fields.float('Est. Rev*Prob.', readonly=True),
41
.. i18n: 'nbr': fields.integer('# of Cases', readonly=True),
42
.. i18n: 'probability': fields.float('Avg. Probability', readonly=True),
43
.. i18n: 'state': fields.selection(AVAILABLE_STATES, 'State', size=16, readonly=True),
44
.. i18n: 'delay_close': fields.integer('Delay to close', readonly=True),
46
.. i18n: _order = 'name desc, user_id, section_id'
48
.. i18n: def init(self, cr):
49
.. i18n: cr.execute("""
50
.. i18n: create or replace view report_crm_case_user as (
52
.. i18n: min(c.id) as id,
53
.. i18n: substring(c.create_date for 7)||'-01' as name,
56
.. i18n: c.section_id,
57
.. i18n: count(*) as nbr,
58
.. i18n: sum(planned_revenue) as amount_revenue,
59
.. i18n: sum(planned_cost) as amount_costs,
60
.. i18n: sum(planned_revenue*probability)::decimal(16,2) as amount_revenue_prob,
61
.. i18n: avg(probability)::decimal(16,2) as probability,
62
.. i18n: to_char(avg(date_closed-c.create_date), 'DD"d" `HH24:MI:SS') as delay_close
65
.. i18n: group by substring(c.create_date for 7), c.state, c.user_id, c.section_id
67
.. i18n: report_crm_case_user()
70
.. code-block:: python
72
class report_crm_case_user(osv.osv):
73
_name = "report.crm.case.user"
74
_description = "Cases by user and section"
77
'name': fields.date('Month', readonly=True),
78
'user_id':fields.many2one('res.users', 'User', readonly=True, relate=True),
79
'section_id':fields.many2one('crm.case.section', 'Section', readonly=True, relate=True),
80
'amount_revenue': fields.float('Est.Revenue', readonly=True),
81
'amount_costs': fields.float('Est.Cost', readonly=True),
82
'amount_revenue_prob': fields.float('Est. Rev*Prob.', readonly=True),
83
'nbr': fields.integer('# of Cases', readonly=True),
84
'probability': fields.float('Avg. Probability', readonly=True),
85
'state': fields.selection(AVAILABLE_STATES, 'State', size=16, readonly=True),
86
'delay_close': fields.integer('Delay to close', readonly=True),
88
_order = 'name desc, user_id, section_id'
92
create or replace view report_crm_case_user as (
95
substring(c.create_date for 7)||'-01' as name,
100
sum(planned_revenue) as amount_revenue,
101
sum(planned_cost) as amount_costs,
102
sum(planned_revenue*probability)::decimal(16,2) as amount_revenue_prob,
103
avg(probability)::decimal(16,2) as probability,
104
to_char(avg(date_closed-c.create_date), 'DD"d" `HH24:MI:SS') as delay_close
107
group by substring(c.create_date for 7), c.state, c.user_id, c.section_id
109
report_crm_case_user()