mirror of
https://gitlab.com/cief-data/dbt_cloud.git
synced 2026-08-19 04:14:00 +00:00
Transaction Invoice stg file
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
/* DOCS
|
||||
Invoice is issued by CIEF to customers for customer book keeping.
|
||||
An invoice is a document that consolidates the total amount for multiple orders within a single booking.
|
||||
Currency shown in invoice is the converted currency.
|
||||
|
||||
For booking_id with multiple invoices, take latest updated_datetime as true.
|
||||
The duplicates are a bug as confirmed by omair.
|
||||
*/
|
||||
|
||||
-- IMPORTS
|
||||
WITH transactions AS (
|
||||
SELECT * FROM {{ ref('base_exchange__transactions') }}
|
||||
),
|
||||
|
||||
transaction_types AS (
|
||||
SELECT * FROM {{ ref('seed_exchange__transaction_types') }}
|
||||
),
|
||||
|
||||
bookings AS (
|
||||
SELECT * FROM {{ ref('base_exchange__bookings') }}
|
||||
),
|
||||
|
||||
payment_methods AS (
|
||||
SELECT * FROM {{ ref('seed_exchange__payment_methods') }}
|
||||
),
|
||||
|
||||
status AS (
|
||||
SELECT * FROM {{ ref('seed_exchange__status') }}
|
||||
WHERE category = 'DEFAULT'
|
||||
),
|
||||
|
||||
|
||||
-- LOGIC
|
||||
transactions_join_transaction_types_payment_methods_bookings_status AS (
|
||||
|
||||
SELECT
|
||||
bookings.booking_marking_id,
|
||||
bookings.fix_value,
|
||||
bookings.fix_currency_id,
|
||||
transactions.transaction_id AS transaction_invoice_id,
|
||||
transactions.owner_id AS booking_id,
|
||||
transactions.issuer_user_id AS issuer_company_id,
|
||||
transactions.receiver_user_id AS receiver_company_id,
|
||||
transactions.base_currency_id,
|
||||
transactions.quote_currency_id,
|
||||
COALESCE(transaction_types.name, transactions.transaction_type::string) AS transaction_type,
|
||||
COALESCE(payment_methods.name, transactions.payment_method::string) AS payment_method,
|
||||
COALESCE(status.name, transactions.status::string) AS status,
|
||||
transactions.bill_number AS invoice_number,
|
||||
transactions.base_value,
|
||||
transactions.quote_value,
|
||||
transactions.base_to_quote_currency_exchange_rate,
|
||||
transactions.base_tax,
|
||||
transactions.base_service_charge,
|
||||
|
||||
transactions.expired_datetime,
|
||||
transactions.deleted_datetime,
|
||||
transactions.created_datetime,
|
||||
transactions.updated_datetime
|
||||
|
||||
FROM transactions
|
||||
|
||||
LEFT JOIN transaction_types
|
||||
ON (transactions.transaction_type = transaction_types.id)
|
||||
|
||||
LEFT JOIN payment_methods
|
||||
ON (transactions.payment_method = payment_methods.id)
|
||||
|
||||
LEFT JOIN status
|
||||
ON (transactions.status = status.id)
|
||||
|
||||
LEFT JOIN bookings
|
||||
ON (transactions.owner_id = bookings.booking_id)
|
||||
|
||||
WHERE
|
||||
transactions.owner_type = 'App\\Models\\Booking'
|
||||
AND
|
||||
transactions.transaction_type = '2' --INVOICE
|
||||
|
||||
),
|
||||
|
||||
remove_deleted_records AS (
|
||||
SELECT
|
||||
*
|
||||
FROM transactions_join_transaction_types_payment_methods_bookings_status
|
||||
WHERE deleted_datetime IS NULL
|
||||
|
||||
),
|
||||
|
||||
remove_duplicated_records AS (
|
||||
SELECT
|
||||
*,
|
||||
ROW_NUMBER() OVER (PARTITION BY booking_id ORDER BY updated_datetime DESC) AS row_index,
|
||||
|
||||
'{{ modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")) }}' AS _dbt_ran_datetime
|
||||
|
||||
FROM remove_deleted_records
|
||||
|
||||
QUALIFY
|
||||
row_index = 1
|
||||
),
|
||||
|
||||
|
||||
-- FINAL
|
||||
final__stg_exchange__invoices AS (
|
||||
|
||||
SELECT
|
||||
-- ids
|
||||
transaction_invoice_id,
|
||||
booking_id,
|
||||
booking_marking_id,
|
||||
issuer_company_id,
|
||||
receiver_company_id,
|
||||
base_currency_id,
|
||||
quote_currency_id,
|
||||
fix_currency_id,
|
||||
|
||||
-- dimensions
|
||||
invoice_number,
|
||||
transaction_type,
|
||||
payment_method,
|
||||
status,
|
||||
|
||||
-- measures
|
||||
fix_value,
|
||||
base_value,
|
||||
quote_value,
|
||||
base_to_quote_currency_exchange_rate,
|
||||
base_tax,
|
||||
base_service_charge,
|
||||
|
||||
-- date/times
|
||||
deleted_datetime,
|
||||
created_datetime,
|
||||
expired_datetime,
|
||||
updated_datetime,
|
||||
|
||||
-- metadata
|
||||
_dbt_ran_datetime
|
||||
|
||||
FROM remove_duplicated_records
|
||||
|
||||
)
|
||||
|
||||
SELECT * FROM final__stg_exchange__invoices
|
||||
Reference in New Issue
Block a user