From 775cc58358da8cacca3df6ca3acf8718e62800a5 Mon Sep 17 00:00:00 2001 From: CIEF ACC1 Date: Wed, 22 Nov 2023 16:08:39 +0000 Subject: [PATCH] Bank Slip --- .../stg_exchange__customer_payment_proof.sql | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 models/staging/exchange/stg_exchange__customer_payment_proof.sql diff --git a/models/staging/exchange/stg_exchange__customer_payment_proof.sql b/models/staging/exchange/stg_exchange__customer_payment_proof.sql new file mode 100644 index 0000000..024bb83 --- /dev/null +++ b/models/staging/exchange/stg_exchange__customer_payment_proof.sql @@ -0,0 +1,86 @@ +-- IMPORT +WITH documents AS ( + SELECT * FROM {{ ref('base_exchange__documents') }} +), + +seed_status AS ( + SELECT * FROM {{ ref('seed_exchange__status') }} + WHERE category = 'DEFAULT' +), + + +-- LOGIC +documents_join_status AS ( + + SELECT + documents.document_id, + documents.owner_id AS transaction_order_id, + documents.approver_user_id, + documents.document_type, + seed_status.name AS status, + IFF(seed_status.name = 'REJECTED', documents.updated_datetime, null) AS rejected_datetime, + IFF(seed_status.name = 'APPROVED', documents.updated_datetime, null) AS approved_datetime, + documents.created_datetime, + documents.updated_datetime, + documents.deleted_datetime + + FROM documents + + LEFT JOIN seed_status + ON (documents.status = seed_status.id) + + WHERE + documents.document_type = 'CUSTOMER_PAYMENT_PROOF' + +), + +/* +Few orders in year 2021 has separate rows for +when status = 'Pending Verfication' and when status = 'Approved' +*/ +filter_latest_documents AS ( + + SELECT + *, + ROW_NUMBER() OVER (PARTITION BY transaction_order_id ORDER BY updated_datetime DESC) AS row_number_index, + + '{{ modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")) }}' AS _dbt_ran_datetime + + FROM documents_join_status + + QUALIFY + row_number_index = 1 + +), + + +-- FINAL +final__stg_exchange__customer_payment_proof AS ( + + SELECT + -- ids + document_id, + transaction_order_id, + approver_user_id, + + -- dimensions + document_type, + status, + + -- measures + + -- date/times + created_datetime, + approved_datetime, + updated_datetime, + rejected_datetime, + deleted_datetime, + + -- metadata + _dbt_ran_datetime + + FROM filter_latest_documents + +) + +SELECT * FROM final__stg_exchange__customer_payment_proof \ No newline at end of file