Skip to content
This repository has been archived by the owner on Mar 1, 2021. It is now read-only.

Commit

Permalink
Merge pull request #3 from fishtown-analytics/fix/do-classes-better
Browse files Browse the repository at this point in the history
Fix/do classes better
  • Loading branch information
jthandy authored Nov 20, 2016
2 parents f204f59 + 5c84568 commit 796ad6b
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 109 deletions.
6 changes: 0 additions & 6 deletions dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ models:
quickbooks_general_ledger:
dist: id
sort: txn_date
quickbooks_parent_class_map:
enabled: false # set to true if this quickbooks install uses classes
base:
materialized: ephemeral
quickbooks_classes:
enabled: false # set to true if this quickbooks install uses classes

vars:
"base.accounts" : "quickbooks.quickbooks_accounts"
Expand All @@ -46,5 +42,3 @@ models:
"base.vendors" : "quickbooks.quickbooks_vendors"
"level_0_id_field" : "_sdc_level_0_id"
"source_key_id_field" : "_sdc_source_key_id"
uses_classes : "false" # set to true if this quickbooks install uses classes

4 changes: 1 addition & 3 deletions models/base/quickbooks_bill_lines.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ select
id::int,
amount,
{{ var('source_key_id_field') }} as bill_id,
{% if var('uses_classes') == "true" %}
nullif(accountbasedexpenselinedetail__classref__value::varchar, '')::bigint as class_id,
{% endif %}
nullif(accountbasedexpenselinedetail__classref__value::varchar, '')::bigint as class_id,
accountbasedexpenselinedetail__accountref__value::int as account_id
from
{{ var('base.bills_line') }}
4 changes: 1 addition & 3 deletions models/base/quickbooks_deposit_lines.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ select
lines._id as id,
lines.deposit_id,
lines.amount,
{% if var('uses_classes') == "true" %}
nullif(lines.depositlinedetail__classref__value::varchar, '')::bigint as class_id,
{% endif %}
nullif(lines.depositlinedetail__classref__value::varchar, '')::bigint as class_id,
nullif(lines.depositlinedetail__accountref__value::varchar, '')::int as account_id,
links.txnid::int as payment_id
from lines
Expand Down
4 changes: 1 addition & 3 deletions models/base/quickbooks_invoice_lines.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ select
amount,
description,
{{ var('source_key_id_field') }}::int as invoice_id,
{% if var('uses_classes') == "true" %}
nullif(salesitemlinedetail__classref__value::varchar, '')::bigint as class_id,
{% endif %}
nullif(salesitemlinedetail__classref__value::varchar, '')::bigint as class_id,
salesitemlinedetail__itemref__value::int as item_id
from
{{ var('base.invoices_lines') }}
Expand Down
4 changes: 1 addition & 3 deletions models/base/quickbooks_journal_lines.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ select
amount,
description,
journalentrylinedetail__accountref__value::integer as account_id,
{% if var('uses_classes') == "true" %}
nullif(journalentrylinedetail__classref__value::varchar, '')::bigint as class_id,
{% endif %}
nullif(journalentrylinedetail__classref__value::varchar, '')::bigint as class_id,
journalentrylinedetail__postingtype as posting_type
from
{{ var('base.journal_entries_line') }}
4 changes: 1 addition & 3 deletions models/base/quickbooks_purchase_lines.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ select
id::int,
amount,
{{ var('source_key_id_field') }}::int as purchase_id,
{% if var('uses_classes') == "true" %}
nullif(accountbasedexpenselinedetail__classref__value::varchar, '')::bigint as class_id,
{% endif %}
nullif(accountbasedexpenselinedetail__classref__value::varchar, '')::bigint as class_id,
accountbasedexpenselinedetail__accountref__value::int as account_id
from
{{ var('base.purchases_line') }}
6 changes: 2 additions & 4 deletions models/tables/quickbooks_general_ledger.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ with unioned as (
amount,
account_id,
transaction_type::varchar(64),
source::varchar(64)
{% if var('uses_classes') == "true" %}
, class_id
{% endif %}
source::varchar(64),
class_id
from {{ref('quickbooks_bill_transactions')}}

union all
Expand Down
31 changes: 17 additions & 14 deletions models/transform/quickbooks_bill_transactions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,31 @@ with bills as (

select bills.id, bills.txn_date, bill_lines.amount,
bill_lines.account_id as payed_to_acct_id,
ap_account_id
{% if var('uses_classes') == "true" %}
, bill_lines.class_id
{% endif %}
ap_account_id,
bill_lines.class_id
from bills
inner join bill_lines on bills.id = bill_lines.bill_id

)

select id, txn_date, amount, payed_to_acct_id as account_id,
'debit'::varchar(16) as transaction_type, 'bill'::varchar(16) as source
{% if var('uses_classes') == "true" %}
, class_id::bigint
{% endif %}
select id,
txn_date,
amount,
payed_to_acct_id as account_id,
'debit'::varchar(16) as transaction_type,
'bill'::varchar(16) as source,
class_id::bigint

from d1

union all

select id, txn_date, amount, ap_account_id,
'credit' as transaction_type, 'bill'
{% if var('uses_classes') == "true" %}
, class_id::bigint
{% endif %}
select
id,
txn_date,
amount,
ap_account_id,
'credit' as transaction_type,
'bill',
class_id::bigint
from d1
30 changes: 19 additions & 11 deletions models/transform/quickbooks_billpayment_transactions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ with bill_payments as (
), d1 as (

select
bp.id, bp.txn_date, bpl.amount, payment_account_id, ap.id as ap_id
bp.id,
bp.txn_date,
bpl.amount,
payment_account_id,
ap.id as ap_id
from bill_payments bp
inner join billpayment_lines bpl on bp.id = bpl.bill_payment_id
join (select id from {{ref('quickbooks_accounts')}} where type = 'Accounts Payable') ap
Expand All @@ -21,21 +25,25 @@ with bill_payments as (
)

select
id, txn_date, amount, payment_account_id as account_id, 'credit' as transaction_type,
'bill payment' as source
{% if var('uses_classes') == "true" %}
, null::bigint as class_id
{% endif %}
id,
txn_date,
amount,
payment_account_id as account_id,
'credit' as transaction_type,
'bill payment' as source,
null::bigint as class_id
from
d1

union all

select
id, txn_date, amount, ap_id, 'debit', 'bill payment'

{% if var('uses_classes') == "true" %}
, null::bigint
{% endif %}
id,
txn_date,
amount,
ap_id,
'debit',
'bill payment',
null::bigint
from
d1
37 changes: 22 additions & 15 deletions models/transform/quickbooks_deposit_transactions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,38 @@ with lines as (

), d1 as (

select deposits.id, deposits.txn_date, lines.amount,
select
deposits.id,
deposits.txn_date,
lines.amount,
deposits.account_id as deposit_to_acct_id,
coalesce(lines.account_id, udf.id) as deposit_from_acct_id
{% if var('uses_classes') == "true" %}
, lines.class_id
{% endif %}

coalesce(lines.account_id, udf.id) as deposit_from_acct_id,
lines.class_id
from deposits
inner join lines on deposits.id = lines.deposit_id
join (select id from {{ref('quickbooks_accounts')}} where subtype = 'UndepositedFunds') udf
on 1 = 1

)

select id, txn_date, amount, deposit_to_acct_id as account_id,
'debit' as transaction_type, 'deposit' as source
{% if var('uses_classes') == "true" %}
, class_id
{% endif %}
select
id,
txn_date,
amount,
deposit_to_acct_id as account_id,
'debit' as transaction_type,
'deposit' as source,
class_id
from d1

union all

select id, txn_date, amount, deposit_from_acct_id, 'credit', 'deposit'
{% if var('uses_classes') == "true" %}
, class_id
{% endif %}
select
id,
txn_date,
amount,
deposit_from_acct_id,
'credit',
'deposit',
class_id
from d1
29 changes: 14 additions & 15 deletions models/transform/quickbooks_invoice_transactions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,34 @@ with invoice_lines as (
invoices.id,
invoices.txn_date,
invoice_lines.amount,
items.account_id
{% if var('uses_classes') == "true" %}
, invoice_lines.class_id
{% endif %}

items.account_id,
invoice_lines.class_id
from invoices
inner join invoice_lines on invoices.id = invoice_lines.invoice_id
inner join items on invoice_lines.item_id = items.id

)

select
id, txn_date, amount, account_id, 'credit' as transaction_type, 'invoice' as source
{% if var('uses_classes') == "true" %}
, class_id
{% endif %}

id,
txn_date,
amount,
account_id,
'credit' as transaction_type,
'invoice' as source,
class_id
from d1

union all

select
d1.id, txn_date, amount,
d1.id,
txn_date,
amount,
ar.id,
'debit' as transaction_type,
'invoice'
{% if var('uses_classes') == "true" %}
, class_id
{% endif %}
'invoice',
class_id
from d1
join (select id from {{ref('quickbooks_accounts')}} where type = 'Accounts Receivable') ar
on 1 = 1
12 changes: 7 additions & 5 deletions models/transform/quickbooks_journal_transactions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ with entries as (
)

select
entries.id, entries.txn_date, lines.amount, account_id, lower(posting_type) as transaction_type,
'journal' as source
{% if var('uses_classes') == "true" %}
, lines.class_id
{% endif %}
entries.id,
entries.txn_date,
lines.amount,
account_id,
lower(posting_type) as transaction_type,
'journal' as source,
lines.class_id
from entries
inner join lines on entries.id = lines.entry_id
24 changes: 14 additions & 10 deletions models/transform/quickbooks_payment_transactions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@ with payments as (

)

select payments.id, txn_date, total as amount, account_id,
select
payments.id,
txn_date,
total as amount,
account_id,
'debit' as transaction_type,
'payment' as source
{% if var('uses_classes') == "true" %}
, null::bigint as class_id
{% endif %}
'payment' as source,
null::bigint as class_id
from payments

union all

select payments.id, txn_date, total, ar.id,
select
payments.id,
txn_date,
total,
ar.id,
'credit',
'payment'
{% if var('uses_classes') == "true" %}
, null::bigint
{% endif %}
'payment',
null::bigint
from payments
join (select id from {{ref('quickbooks_accounts')}} where type = 'Accounts Receivable') ar
on 1 = 1
33 changes: 19 additions & 14 deletions models/transform/quickbooks_purchase_transactions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,31 @@ with purchases as (
case nvl(purchases.credit, false::bool)
when true then 'credit'
else 'debit'
end as payed_to_transaction_type
{% if var('uses_classes') == "true" %}
, purchase_lines.class_id
{% endif %}
end as payed_to_transaction_type,
purchase_lines.class_id
from purchases
inner join purchase_lines on purchases.id = purchase_lines.purchase_id

)

select id, txn_date, amount, payed_from_acct_id as account_id,
select
id,
txn_date,
amount,
payed_from_acct_id as account_id,
payed_from_transaction_type as transaction_type,
'purchase' as source
{% if var('uses_classes') == "true" %}
, class_id
{% endif %}
'purchase' as source,
class_id
from d1

union all
select id, txn_date, amount, payed_to_acct_id,
payed_to_transaction_type, 'purchase'
{% if var('uses_classes') == "true" %}
, class_id
{% endif %}

select
id,
txn_date,
amount,
payed_to_acct_id,
payed_to_transaction_type,
'purchase',
class_id
from d1

0 comments on commit 796ad6b

Please sign in to comment.