-
Notifications
You must be signed in to change notification settings - Fork 21
/
xero__balance_sheet_report.sql
67 lines (56 loc) · 2.49 KB
/
xero__balance_sheet_report.sql
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
with calendar as (
select *
from {{ ref('xero__calendar_spine') }}
), ledger as (
select *
from {{ ref('xero__general_ledger') }}
), organization as (
select *
from {{ var('organization') }}
), year_end as (
select
case
when cast(extract(year from current_date) || '-' || financial_year_end_month || '-' || financial_year_end_day as date) >= current_date
then cast(extract(year from current_date) || '-' || financial_year_end_month || '-' || financial_year_end_day as date)
else case when financial_year_end_month = 2 and financial_year_end_day = 29
then cast(extract(year from {{ dbt.dateadd('year', -1, 'current_date') }}) || '-' || financial_year_end_month || '-28' as date) -- Necessary for organizations with a reported fiscal year end of 02-29 as the previous year will not be a leap year and must be the 28th.
else cast(extract(year from {{ dbt.dateadd('year', -1, 'current_date') }}) || '-' || financial_year_end_month || '-' || financial_year_end_day as date)
end
end as current_year_end_date,
source_relation
from organization
), joined as (
select
calendar.date_month,
case
when ledger.account_class in ('ASSET','EQUITY','LIABILITY') then ledger.account_name
when ledger.journal_date <= {{ dbt.dateadd('year', -1, 'year_end.current_year_end_date') }} then 'Retained Earnings'
else 'Current Year Earnings'
end as account_name,
case
when ledger.account_class in ('ASSET','EQUITY','LIABILITY') then ledger.account_code
else null
end as account_code,
case
when ledger.account_class in ('ASSET','EQUITY','LIABILITY') then ledger.account_id
else null
end as account_id,
case
when ledger.account_class in ('ASSET','EQUITY','LIABILITY') then ledger.account_type
else null
end as account_type,
case
when ledger.account_class in ('ASSET','EQUITY','LIABILITY') then ledger.account_class
else 'EQUITY'
end as account_class,
ledger.source_relation,
sum(ledger.net_amount) as net_amount
from calendar
inner join ledger
on calendar.date_month >= cast({{ dbt.date_trunc('month', 'ledger.journal_date') }} as date)
cross join year_end
where year_end.source_relation = ledger.source_relation
{{ dbt_utils.group_by(7) }}
)
select *
from joined