-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathint_quickbooks__invoice_join.sql
142 lines (107 loc) · 4.11 KB
/
int_quickbooks__invoice_join.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
130
131
132
133
134
135
136
137
138
139
140
141
142
--To disable this model, set the using_invoice variable within your dbt_project.yml file to False.
{{ config(enabled=var('using_invoice') and var('using_payment', True)) }}
with invoices as (
select *
from {{ ref('stg_quickbooks__invoice') }}
),
invoice_linked as (
select *
from {{ ref('stg_quickbooks__invoice_linked_txn') }}
),
{% if var('using_estimate', True) %}
estimates as (
select *
from {{ ref('stg_quickbooks__estimate') }}
),
{% endif %}
payments as (
select *
from {{ ref('stg_quickbooks__payment') }}
),
payment_lines_payment as (
select *
from {{ ref('stg_quickbooks__payment_line') }}
where invoice_id is not null
),
invoice_est as (
select
invoices.invoice_id,
invoice_linked.estimate_id,
invoices.source_relation
from invoices
left join invoice_linked
on invoices.invoice_id = invoice_linked.invoice_id
and invoices.source_relation = invoice_linked.source_relation
where invoice_linked.estimate_id is not null
),
invoice_pay as (
select
invoices.invoice_id,
invoice_linked.payment_id,
invoices.source_relation
from invoices
left join invoice_linked
on invoices.invoice_id = invoice_linked.invoice_id
and invoices.source_relation = invoice_linked.source_relation
where invoice_linked.payment_id is not null
),
invoice_link as (
select
invoices.*,
invoice_est.estimate_id,
invoice_pay.payment_id
from invoices
left join invoice_est
on invoices.invoice_id = invoice_est.invoice_id
and invoices.source_relation = invoice_est.source_relation
left join invoice_pay
on invoices.invoice_id = invoice_pay.invoice_id
and invoices.source_relation = invoice_pay.source_relation
),
final as (
select
cast('invoice' as {{ dbt.type_string() }}) as transaction_type,
invoice_link.invoice_id as transaction_id,
invoice_link.source_relation,
invoice_link.doc_number,
invoice_link.estimate_id,
invoice_link.department_id,
invoice_link.customer_id as customer_id,
invoice_link.billing_address_id,
invoice_link.shipping_address_id,
invoice_link.delivery_type,
invoice_link.total_amount as total_amount,
(invoice_link.total_amount * coalesce(invoice_link.exchange_rate, 1)) as total_converted_amount,
invoice_link.balance as current_balance,
{% if var('using_estimate', True) %}
coalesce(estimates.total_amount, 0) as estimate_total_amount,
coalesce(estimates.total_amount, 0) * coalesce(estimates.exchange_rate, 1) as estimate_total_converted_amount,
estimates.transaction_status as estimate_status,
{% else %}
cast(null as {{ dbt.type_numeric() }}) as estimate_total_amount,
cast(null as {{ dbt.type_numeric() }}) as estimate_total_converted_amount,
cast(null as {{ dbt.type_string() }}) as estimate_status,
{% endif %}
invoice_link.due_date as due_date,
min(payments.transaction_date) as initial_payment_date,
max(payments.transaction_date) as recent_payment_date,
sum(coalesce(payment_lines_payment.amount, 0)) as total_current_payment,
sum(coalesce(payment_lines_payment.amount, 0) * coalesce(payments.exchange_rate, 1)) as total_current_converted_payment
from invoice_link
{% if var('using_estimate', True) %}
left join estimates
on invoice_link.estimate_id = estimates.estimate_id
and invoice_link.source_relation = estimates.source_relation
{% endif %}
left join payments
on invoice_link.payment_id = payments.payment_id
and invoice_link.source_relation = payments.source_relation
left join payment_lines_payment
on payments.payment_id = payment_lines_payment.payment_id
and payments.source_relation = payment_lines_payment.source_relation
and invoice_link.invoice_id = payment_lines_payment.invoice_id
and invoice_link.source_relation = payment_lines_payment.source_relation
{{ dbt_utils.group_by(17) }}
)
select *
from final