-
Notifications
You must be signed in to change notification settings - Fork 39
/
int_quickbooks__invoice_double_entry.sql
213 lines (168 loc) · 7.11 KB
/
int_quickbooks__invoice_double_entry.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
Table that creates a debit record to accounts receivable and a credit record to a specified revenue account indicated on the invoice line.
*/
--To disable this model, set the using_invoice variable within your dbt_project.yml file to False.
{{ config(enabled=var('using_invoice', True)) }}
with invoices as (
select *
from {{ ref('stg_quickbooks__invoice') }}
),
invoice_lines as (
select *
from {{ ref('stg_quickbooks__invoice_line') }}
),
items as (
select
item.*,
parent.income_account_id as parent_income_account_id
from {{ ref('stg_quickbooks__item') }} item
left join {{ ref('stg_quickbooks__item') }} parent
on item.parent_item_id = parent.item_id
and item.source_relation = parent.source_relation
),
accounts as (
select *
from {{ ref('stg_quickbooks__account') }}
),
{% if var('using_invoice_bundle', True) %}
invoice_bundles as (
select *
from {{ ref('stg_quickbooks__invoice_line_bundle') }}
),
bundles as (
select *
from {{ ref('stg_quickbooks__bundle') }}
),
bundle_items as (
select *
from {{ ref('stg_quickbooks__bundle_item') }}
),
income_accounts as (
select *
from accounts
where account_sub_type = '{{ var('quickbooks__sales_of_product_income_reference', 'SalesOfProductIncome') }}'
),
bundle_income_accounts as (
select distinct
coalesce(parent.income_account_id, income_accounts.account_id) as account_id,
coalesce(parent.source_relation, income_accounts.source_relation) as source_relation,
bundle_items.bundle_id
from items
left join items as parent
on items.parent_item_id = parent.item_id
and items.source_relation = parent.source_relation
inner join income_accounts
on income_accounts.account_id = items.income_account_id
and income_accounts.source_relation = items.source_relation
inner join bundle_items
on bundle_items.item_id = items.item_id
and bundle_items.source_relation = items.source_relation
),
{% endif %}
ar_accounts as (
select
account_id,
source_relation
from accounts
where account_type = '{{ var('quickbooks__accounts_receivable_reference', 'Accounts Receivable') }}'
and is_active
and not is_sub_account
),
invoice_join as (
select
invoices.invoice_id as transaction_id,
invoices.source_relation,
invoice_lines.index,
invoices.transaction_date as transaction_date,
{% if var('using_invoice_bundle', True) %}
case when invoice_lines.bundle_id is not null and invoices.total_amount = 0 then invoices.total_amount
else invoice_lines.amount
end as amount,
case when invoice_lines.bundle_id is not null and invoices.total_amount = 0
then (invoices.total_amount * coalesce(invoices.exchange_rate, 1))
else (invoice_lines.amount * coalesce(invoices.exchange_rate, 1))
end as converted_amount,
case when invoice_lines.detail_type is not null then invoice_lines.detail_type
when coalesce(invoice_lines.account_id, invoice_lines.sales_item_account_id, items.parent_income_account_id, items.income_account_id, bundle_income_accounts.account_id) is not null then 'SalesItemLineDetail'
when invoice_lines.discount_account_id is not null then 'DiscountLineDetail'
when coalesce(invoice_lines.account_id, invoice_lines.sales_item_account_id, items.parent_income_account_id, items.income_account_id, bundle_income_accounts.account_id, invoice_lines.discount_account_id) is null then 'NoAccountMapping'
end as invoice_line_transaction_type,
coalesce(invoice_lines.account_id, invoice_lines.sales_item_account_id, items.parent_income_account_id, items.income_account_id, bundle_income_accounts.account_id, invoice_lines.discount_account_id) as account_id,
{% else %}
invoice_lines.amount as amount,
(invoice_lines.amount * coalesce(invoices.exchange_rate, 1)) as converted_amount,
case when invoice_lines.detail_type is not null then invoice_lines.detail_type
when coalesce(invoice_lines.account_id, invoice_lines.sales_item_account_id, items.parent_income_account_id, items.income_account_id) is not null then 'SalesItemLineDetail'
when invoice_lines.discount_account_id is not null then 'DiscountLineDetail'
when coalesce(invoice_lines.account_id, invoice_lines.sales_item_account_id, items.parent_income_account_id, items.income_account_id, invoice_lines.discount_account_id) is null then 'NoAccountMapping'
end as invoice_line_transaction_type,
coalesce(invoice_lines.account_id, invoice_lines.sales_item_account_id, items.income_account_id, invoice_lines.discount_account_id) as account_id,
{% endif %}
coalesce(invoice_lines.sales_item_class_id, invoice_lines.discount_class_id, invoices.class_id) as class_id,
invoices.customer_id,
invoices.department_id,
invoices.created_at,
invoices.updated_at
from invoices
inner join invoice_lines
on invoices.invoice_id = invoice_lines.invoice_id
and invoices.source_relation = invoice_lines.source_relation
left join items
on coalesce(invoice_lines.sales_item_item_id, invoice_lines.item_id) = items.item_id
and invoice_lines.source_relation = items.source_relation
{% if var('using_invoice_bundle', True) %}
left join bundle_income_accounts
on bundle_income_accounts.bundle_id = invoice_lines.bundle_id
and bundle_income_accounts.source_relation = invoice_lines.source_relation
{% endif %}
),
invoice_filter as (
select *
from invoice_join
where invoice_line_transaction_type not in ('SubTotalLineDetail','NoAccountMapping')
),
final as (
select
transaction_id,
invoice_filter.source_relation,
index,
transaction_date,
customer_id,
cast(null as {{ dbt.type_string() }}) as vendor_id,
amount,
converted_amount,
account_id,
class_id,
department_id,
case when invoice_line_transaction_type = 'DiscountLineDetail' then 'debit'
else 'credit'
end as transaction_type,
case when invoice_line_transaction_type = 'DiscountLineDetail' then 'invoice discount'
else 'invoice'
end as transaction_source
from invoice_filter
union all
select
transaction_id,
invoice_filter.source_relation,
index,
transaction_date,
customer_id,
cast(null as {{ dbt.type_string() }}) as vendor_id,
amount,
converted_amount,
ar_accounts.account_id,
class_id,
department_id,
case when invoice_line_transaction_type = 'DiscountLineDetail' then 'credit'
else 'debit'
end as transaction_type,
case when invoice_line_transaction_type = 'DiscountLineDetail' then 'invoice discount'
else 'invoice'
end as transaction_source
from invoice_filter
left join ar_accounts
on ar_accounts.source_relation = invoice_filter.source_relation
)
select *
from final