-
Notifications
You must be signed in to change notification settings - Fork 20
/
xero__general_ledger.sql
129 lines (102 loc) · 3.76 KB
/
xero__general_ledger.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
with journals as (
select *
from {{ var('journal') }}
), journal_lines as (
select *
from {{ var('journal_line') }}
), accounts as (
select *
from {{ var('account') }}
), invoices as (
select *
from {{ var('invoice') }}
{% if var('xero__using_bank_transaction', True) %}
), bank_transactions as (
select *
from {{ var('bank_transaction') }}
{% endif %}
{% if var('xero__using_credit_note', True) %}
), credit_notes as (
select *
from {{ var('credit_note') }}
{% endif %}
), contacts as (
select *
from {{ var('contact') }}
), joined as (
select
journals.journal_id,
journals.created_date_utc,
journals.journal_date,
journals.journal_number,
journals.reference,
journals.source_id,
journals.source_type,
journals.source_relation,
journal_lines.journal_line_id,
accounts.account_code,
accounts.account_id,
accounts.account_name,
accounts.account_type,
journal_lines.description,
journal_lines.gross_amount,
journal_lines.net_amount,
journal_lines.tax_amount,
journal_lines.tax_name,
journal_lines.tax_type,
accounts.account_class,
case when journals.source_type in ('ACCPAY', 'ACCREC') then journals.source_id end as invoice_id,
case when journals.source_type in ('CASHREC','CASHPAID') then journals.source_id end as bank_transaction_id,
case when journals.source_type in ('TRANSFER') then journals.source_id end as bank_transfer_id,
case when journals.source_type in ('MANJOURNAL') then journals.source_id end as manual_journal_id,
case when journals.source_type in ('APPREPAYMENT', 'APOVERPAYMENT', 'ACCPAYPAYMENT', 'ACCRECPAYMENT', 'ARCREDITPAYMENT', 'APCREDITPAYMENT') then journals.source_id end as payment_id,
case when journals.source_type in ('ACCPAYCREDIT','ACCRECCREDIT') then journals.source_id end as credit_note_id
from journals
left join journal_lines
on (journals.journal_id = journal_lines.journal_id
and journals.source_relation = journal_lines.source_relation)
left join accounts
on (accounts.account_id = journal_lines.account_id
and accounts.source_relation = journal_lines.source_relation)
), first_contact as (
select
joined.*,
{% if fivetran_utils.enabled_vars_one_true(['xero__using_bank_transaction','xero__using_credit_note']) %}
coalesce(
invoices.contact_id
{% if var('xero__using_bank_transaction', True) %}
, bank_transactions.contact_id
{% endif %}
{% if var('xero__using_credit_note', True) %}
, credit_notes.contact_id
{% endif %}
)
{% else %}
invoices.contact_id
{% endif %}
as contact_id
from joined
left join invoices
on (joined.invoice_id = invoices.invoice_id
and joined.source_relation = invoices.source_relation)
{% if var('xero__using_bank_transaction', True) %}
left join bank_transactions
on (joined.bank_transaction_id = bank_transactions.bank_transaction_id
and joined.source_relation = bank_transactions.source_relation)
{% endif %}
{% if var('xero__using_credit_note', True) %}
left join credit_notes
on (joined.credit_note_id = credit_notes.credit_note_id
and joined.source_relation = credit_notes.source_relation)
{% endif %}
), second_contact as (
select
first_contact.*,
contacts.contact_name
from first_contact
left join contacts
on (first_contact.contact_id = contacts.contact_id
and first_contact.source_relation = contacts.source_relation)
)
select *
from second_contact