mirror of
https://gitlab.com/cief-data/dbt_cloud.git
synced 2026-08-19 04:14:00 +00:00
PO stg file
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
/* DOCS
|
||||
"IS_AUTO_GENERATED_PURCHASE_ORDER"
|
||||
- 1 indicates PO auto generated by system, having prefix 'XPO'
|
||||
- 0 indicates PO submitted by customer, having prefix 'PO'
|
||||
|
||||
Purchase Order document does not perform currency conversion, and it is a document to be submitted by the customer.
|
||||
Therefore, base currency and quote currency is the same.
|
||||
The PO is based on the fix currency.
|
||||
|
||||
The 'fix currency' feature enables customers to choose their primary currency when placing an order,
|
||||
ensuring that it remains fixed while allowing the converted currency to fluctuate.
|
||||
|
||||
Currency is converted only in invoice.
|
||||
|
||||
Note to downstream user:
|
||||
This table contains duplicate of booking_id, due to deleted entries.
|
||||
Filter out deleted entries and select only status = 'APPROVED' for use.
|
||||
*/
|
||||
|
||||
-- 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 AS purchase_order_currency_id,
|
||||
|
||||
transactions.transaction_id AS transaction_purchase_order_id,
|
||||
transactions.owner_id AS booking_id,
|
||||
transactions.issuer_user_id AS issuer_company_id,
|
||||
transactions.receiver_user_id AS receiver_company_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 purchase_order_number,
|
||||
transactions.base_value AS purchase_order_amount,
|
||||
|
||||
transactions.expired_datetime,
|
||||
transactions.deleted_datetime,
|
||||
transactions.created_datetime,
|
||||
transactions.updated_datetime,
|
||||
|
||||
CASE
|
||||
WHEN purchase_order_number LIKE 'XP%' THEN 1 ELSE 0
|
||||
END AS is_auto_generated_purchase_order
|
||||
|
||||
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 = '7' --PURCHASE_ORDER
|
||||
|
||||
),
|
||||
|
||||
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__purchase_orders AS (
|
||||
|
||||
SELECT
|
||||
-- ids
|
||||
transaction_purchase_order_id,
|
||||
booking_id,
|
||||
booking_marking_id,
|
||||
issuer_company_id,
|
||||
receiver_company_id,
|
||||
purchase_order_currency_id
|
||||
|
||||
-- dimensions
|
||||
is_auto_generated_purchase_order,
|
||||
purchase_order_number,
|
||||
transaction_type,
|
||||
payment_method,
|
||||
status,
|
||||
|
||||
-- measures
|
||||
fix_value,
|
||||
purchase_order_amount,
|
||||
|
||||
-- date/times
|
||||
deleted_datetime,
|
||||
created_datetime,
|
||||
expired_datetime,
|
||||
updated_datetime,
|
||||
|
||||
-- metadata
|
||||
_dbt_ran_datetime
|
||||
|
||||
FROM remove_duplicated_records
|
||||
|
||||
)
|
||||
|
||||
SELECT * FROM final__stg_exchange__purchase_orders
|
||||
Reference in New Issue
Block a user