~openerp-community/openobject-addons/galtys

« back to all changes in this revision

Viewing changes to budget_report/budget_entries_view_noanalytics.sql

  • Committer: jan at asus
  • Date: 2013-10-03 13:01:35 UTC
  • mfrom: (22.1.1 galtysaddons)
  • Revision ID: jan@asus-20131003130135-q43yuj59t0r9yh94
budget wit no analytics

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
-- View: budget_entries_report
 
2
 
 
3
-- DROP VIEW budget_entries_report;
 
4
 
 
5
CREATE OR REPLACE VIEW budget_entries_report AS 
 
6
 SELECT min(a.id) AS id,
 
7
        count(DISTINCT a.id) AS nbr,
 
8
        to_char(a.date::timestamp with time zone, 'YYYY'::text) AS year,
 
9
        to_char(a.date::timestamp with time zone, 'MM'::text) AS month,
 
10
        a.company_id,
 
11
        rc.currency_id,
 
12
        cbl.crossovered_budget_id,
 
13
        cbl.general_budget_id,
 
14
        sum(a.debit - a.credit) AS amount,
 
15
        sum(a.quantity) AS unit_amount,
 
16
        round(sum(cbl.planned_amount) / count(DISTINCT a.id)::numeric, 2) AS planned_amount,
 
17
        case when round(sum(cbl.planned_amount) / count(DISTINCT a.id)::numeric, 2) = 0 then 0 else round(sum(a.debit - a.credit) / (round(sum(cbl.planned_amount) / count(DISTINCT a.id)::numeric, 2) / 100::numeric), 0) end AS variance
 
18
   FROM account_move_line a, crossovered_budget cb, crossovered_budget_lines cbl, account_budget_post abp, res_company rc
 
19
  WHERE abp.id = cbl.general_budget_id AND cb.id = cbl.crossovered_budget_id
 
20
    AND a.date >= cbl.date_from AND a.date <= cbl.date_to AND rc.id = cbl.company_id 
 
21
    AND (EXISTS ( SELECT 'X'
 
22
           FROM account_budget_rel abr
 
23
          WHERE abr.budget_id = abp.id AND a.account_id = abr.account_id))
 
24
  GROUP BY to_char(a.date::timestamp with time zone, 'YYYY'::text), to_char(a.date::timestamp with time zone, 'MM'::text), a.company_id, rc.currency_id, a.account_id, cbl.crossovered_budget_id, cbl.general_budget_id
 
25
 
 
26
UNION
 
27
 SELECT min(cbl.id) AS id,
 
28
        0 AS nbr,
 
29
        to_char(cbl.date_from::timestamp with time zone, 'YYYY'::text) AS year,
 
30
        to_char(cbl.date_from::timestamp with time zone, 'MM'::text) AS month,
 
31
        cbl.company_id,
 
32
        rc.currency_id,
 
33
        cbl.crossovered_budget_id,
 
34
        cbl.general_budget_id,
 
35
        0 AS amount,
 
36
        0 AS unit_amount,
 
37
        sum(cbl.planned_amount) AS planned_amount, 
 
38
        0 AS variance
 
39
   FROM crossovered_budget cb, crossovered_budget_lines cbl, account_budget_post abp, res_company rc
 
40
  WHERE abp.id = cbl.general_budget_id AND cb.id = cbl.crossovered_budget_id AND rc.id = cbl.company_id
 
41
    AND (not EXISTS ( SELECT 'X'
 
42
           FROM account_move_line aml, account_budget_rel abr
 
43
          WHERE abr.budget_id = abp.id AND aml.account_id = abr.account_id AND aml.date >= cbl.date_from AND aml.date <= cbl.date_to))
 
44
  GROUP BY to_char(cbl.date_from::timestamp with time zone, 'YYYY'::text), to_char(cbl.date_from::timestamp with time zone, 'MM'::text),
 
45
           cbl.company_id, rc.currency_id, cbl.crossovered_budget_id, cbl.general_budget_id;
 
46
 
 
47
ALTER TABLE budget_entries_report
 
48
  OWNER TO ???????;