mirror of
https://gitlab.com/cief-data/dbt_cloud.git
synced 2026-08-19 04:14:00 +00:00
1688 cost calculation 0.4% fix
This commit is contained in:
@@ -1,21 +1,38 @@
|
||||
-- VARIABLES
|
||||
{% set order_status = dbt_utils.get_column_values(
|
||||
table=ref('stg_exchange__transaction_order_logs'),
|
||||
column='status')
|
||||
{% set cost_status = dbt_utils.get_column_values(
|
||||
table=ref('stg_exchange__transaction_cost_logs'),
|
||||
column='status')
|
||||
%}
|
||||
|
||||
|
||||
-- IMPORTS
|
||||
WITH transaction_cost_logs AS (
|
||||
SELECT * FROM {{ ref('stg_exchange__transaction_cost_logs') }}
|
||||
SELECT * FROM {{ ref('stg_exchange__transaction_cost_logs') }}
|
||||
),
|
||||
|
||||
transaction_costs AS (
|
||||
SELECT * FROM {{ ref('stg_exchange__transaction_costs') }}
|
||||
),
|
||||
|
||||
currency_vendor_payment_proof_documents AS (
|
||||
SELECT * FROM {{ ref('stg_exchange__currency_vendor_payment_proof') }}
|
||||
),
|
||||
|
||||
temp_bookings AS (
|
||||
SELECT * FROM {{ ref('stg_exchange__bookings') }}
|
||||
),
|
||||
|
||||
temp_orders AS (
|
||||
SELECT * FROM {{ ref('stg_exchange__transaction_orders') }}
|
||||
),
|
||||
|
||||
|
||||
-- LOGIC
|
||||
/*
|
||||
Some order may have multiple same cost status. This is due to the accounting department adjusting the exchange rate.
|
||||
This is particularly true for the 1688 payment, as exchange rate is amended at a later stage instead of during the initial order placement stage.
|
||||
Thus, it is to be assumed that the datetime from first repeat occurence as the true datetime.
|
||||
*/
|
||||
transaction_cost_status_datetime AS (
|
||||
|
||||
SELECT
|
||||
@@ -23,8 +40,8 @@ transaction_cost_status_datetime AS (
|
||||
company_id,
|
||||
MIN(created_datetime) AS cost_created_datetime,
|
||||
|
||||
{%- for status in order_status %}
|
||||
MAX(CASE WHEN status = '{{status}}' THEN updated_datetime END) AS cost_{{status.lower()}}_datetime {%- if not loop.last %},{% endif -%}
|
||||
{%- for status in cost_status %}
|
||||
MIN(CASE WHEN status = '{{status}}' THEN updated_datetime END) AS cost_{{status.lower()}}_datetime {%- if not loop.last %},{% endif -%}
|
||||
{% endfor %}
|
||||
|
||||
FROM transaction_cost_logs
|
||||
@@ -61,19 +78,28 @@ join_cost_and_logs AS (
|
||||
|
||||
transaction_cost_status_datetime.company_id,
|
||||
transaction_cost_status_datetime.cost_created_datetime,
|
||||
transaction_cost_status_datetime.cost_completed_datetime,
|
||||
transaction_cost_status_datetime.cost_pending_submission_datetime,
|
||||
transaction_cost_status_datetime.cost_approved_datetime,
|
||||
transaction_cost_status_datetime.cost_pending_verification_datetime,
|
||||
transaction_cost_status_datetime.cost_rejected_datetime,
|
||||
transaction_cost_status_datetime.cost_suspended_datetime,
|
||||
transaction_cost_status_datetime.cost_expired_datetime
|
||||
transaction_cost_status_datetime.cost_approved_datetime,
|
||||
transaction_cost_status_datetime.cost_completed_datetime,
|
||||
|
||||
currency_vendor_payment_proof_documents.document_type AS cost_document_type,
|
||||
currency_vendor_payment_proof_documents.status AS cost_document_status,
|
||||
currency_vendor_payment_proof_documents.created_datetime AS cost_document_created_datetime,
|
||||
currency_vendor_payment_proof_documents.updated_datetime AS cost_document_updated_datetime,
|
||||
|
||||
ROW_NUMBER() OVER (PARTITION BY transaction_costs.transaction_cost_id ORDER BY cost_document_created_datetime, cost_document_updated_datetime) AS row_number_index
|
||||
|
||||
FROM transaction_costs
|
||||
|
||||
LEFT JOIN transaction_cost_status_datetime
|
||||
ON (transaction_costs.transaction_cost_id = transaction_cost_status_datetime.transaction_cost_id)
|
||||
|
||||
LEFT JOIN currency_vendor_payment_proof_documents
|
||||
ON (transaction_costs.transaction_cost_id = currency_vendor_payment_proof_documents.transaction_cost_id)
|
||||
|
||||
QUALIFY
|
||||
row_number_index = 1
|
||||
),
|
||||
|
||||
remove_deleted_expired_costs AS (
|
||||
@@ -95,9 +121,7 @@ remove_system_error_duplicate_row 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
|
||||
ROW_NUMBER() OVER (PARTITION BY transaction_order_id ORDER BY updated_datetime DESC) AS row_number_index
|
||||
|
||||
FROM remove_deleted_expired_costs
|
||||
|
||||
@@ -106,6 +130,78 @@ remove_system_error_duplicate_row AS (
|
||||
|
||||
),
|
||||
|
||||
/*
|
||||
Temporary CTE to address the issue of 0.4% service charge for 1688 payments
|
||||
Affected supplier_company_id from 2729 and 4548
|
||||
Affected orders are from 3rd-Apr-2023 onwards
|
||||
Service charge is only applied onto the CNY
|
||||
*/
|
||||
temp_cte_1688_cleaning AS (
|
||||
|
||||
SELECT
|
||||
remove_system_error_duplicate_row.transaction_cost_id,
|
||||
remove_system_error_duplicate_row.transaction_order_id,
|
||||
remove_system_error_duplicate_row.supplier_company_id,
|
||||
remove_system_error_duplicate_row.company_id,
|
||||
remove_system_error_duplicate_row.bank_id,
|
||||
remove_system_error_duplicate_row.base_currency_id,
|
||||
remove_system_error_duplicate_row.quote_currency_id,
|
||||
remove_system_error_duplicate_row.transaction_type,
|
||||
remove_system_error_duplicate_row.payment_method,
|
||||
remove_system_error_duplicate_row.status,
|
||||
remove_system_error_duplicate_row.payment_reference,
|
||||
remove_system_error_duplicate_row.bill_number,
|
||||
remove_system_error_duplicate_row.cost_document_type,
|
||||
remove_system_error_duplicate_row.cost_document_status,
|
||||
remove_system_error_duplicate_row.base_value,
|
||||
remove_system_error_duplicate_row.quote_value,
|
||||
remove_system_error_duplicate_row.base_to_quote_currency_exchange_rate,
|
||||
remove_system_error_duplicate_row.base_tax,
|
||||
remove_system_error_duplicate_row.expired_datetime,
|
||||
remove_system_error_duplicate_row.deleted_datetime,
|
||||
remove_system_error_duplicate_row.created_datetime,
|
||||
remove_system_error_duplicate_row.updated_datetime,
|
||||
remove_system_error_duplicate_row.cost_created_datetime,
|
||||
remove_system_error_duplicate_row.cost_pending_submission_datetime,
|
||||
remove_system_error_duplicate_row.cost_pending_verification_datetime,
|
||||
remove_system_error_duplicate_row.cost_approved_datetime,
|
||||
remove_system_error_duplicate_row.cost_completed_datetime,
|
||||
remove_system_error_duplicate_row.cost_document_created_datetime,
|
||||
remove_system_error_duplicate_row.cost_document_updated_datetime,
|
||||
|
||||
temp_bookings.service_type,
|
||||
|
||||
CASE
|
||||
WHEN
|
||||
remove_system_error_duplicate_row.supplier_company_id IN (2729, 4548)
|
||||
-- 2729 = HCK Global
|
||||
-- 4548 = Power Progress
|
||||
AND
|
||||
temp_bookings.service_type = '1688 PAYMENT'
|
||||
AND
|
||||
cost_created_datetime >= '2023-04-03'
|
||||
THEN
|
||||
-- Formula used for service charge in MYR: CNY * 0.4% / exchange rate
|
||||
ROUND(DIV0(
|
||||
remove_system_error_duplicate_row.quote_value * ( 0.4 / 100 ),
|
||||
remove_system_error_duplicate_row.base_to_quote_currency_exchange_rate
|
||||
),4)
|
||||
ELSE
|
||||
remove_system_error_duplicate_row.base_service_charge
|
||||
END AS base_service_charge,
|
||||
|
||||
'{{ modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")) }}' AS _dbt_ran_datetime
|
||||
|
||||
FROM remove_system_error_duplicate_row
|
||||
|
||||
LEFT JOIN temp_orders
|
||||
ON (remove_system_error_duplicate_row.transaction_order_id = temp_orders.transaction_order_id)
|
||||
|
||||
LEFT JOIN temp_bookings
|
||||
ON (temp_orders.booking_id = temp_bookings.booking_id)
|
||||
|
||||
),
|
||||
|
||||
|
||||
-- FINAL
|
||||
final__int_exchange__transaction_cost_get_latest_cost_ids AS (
|
||||
@@ -126,6 +222,8 @@ final__int_exchange__transaction_cost_get_latest_cost_ids AS (
|
||||
status,
|
||||
payment_reference,
|
||||
bill_number,
|
||||
cost_document_type,
|
||||
cost_document_status,
|
||||
|
||||
-- measures
|
||||
base_value,
|
||||
@@ -140,19 +238,18 @@ final__int_exchange__transaction_cost_get_latest_cost_ids AS (
|
||||
created_datetime,
|
||||
updated_datetime,
|
||||
cost_created_datetime,
|
||||
cost_completed_datetime,
|
||||
cost_pending_submission_datetime,
|
||||
cost_approved_datetime,
|
||||
cost_pending_verification_datetime,
|
||||
cost_rejected_datetime,
|
||||
cost_suspended_datetime,
|
||||
cost_expired_datetime,
|
||||
|
||||
cost_approved_datetime,
|
||||
cost_completed_datetime,
|
||||
cost_document_created_datetime,
|
||||
cost_document_updated_datetime,
|
||||
|
||||
-- metadata
|
||||
_dbt_ran_datetime
|
||||
|
||||
FROM remove_system_error_duplicate_row
|
||||
|
||||
FROM temp_cte_1688_cleaning
|
||||
|
||||
)
|
||||
|
||||
SELECT * FROM final__int_exchange__transaction_cost_get_latest_cost_ids
|
||||
Reference in New Issue
Block a user