mirror of
https://gitlab.com/cief-data/dbt_cloud.git
synced 2026-08-25 07:14:15 +00:00
fix service_type
This commit is contained in:
@@ -1,26 +1,33 @@
|
||||
-- VARIABLES
|
||||
{% set var_service_type = [
|
||||
'1688 PAYMENT',
|
||||
'1 DAY TRANSFER',
|
||||
'3 DAYS TRANSFER',
|
||||
'Enterprise to Enterprise 公打公'] %}
|
||||
{% set service_types = dbt_utils.get_column_values(
|
||||
table=ref('fct_exchange__transaction_orders'),
|
||||
column='service_type')
|
||||
%}
|
||||
|
||||
{% set var_working_days_including_base_date = [
|
||||
'add_1_working_day_included_base_date',
|
||||
'add_1_working_day_included_base_date',
|
||||
'add_3_working_day_included_base_date',
|
||||
'add_7_working_day_included_base_date'] %}
|
||||
-- working days mapping for each service type
|
||||
{% set service_type_working_day_mapping = {
|
||||
'1 DAY TRANSFER': 1,
|
||||
'1688 PAYMENT': 1,
|
||||
'3 DAYS TRANSFER': 3,
|
||||
'ENTERPRISE TO ENTERPRISE 公打公': 7
|
||||
} %}
|
||||
|
||||
{% set var_working_days_excluding_base_date = [
|
||||
'add_1_working_day_excluded_base_date',
|
||||
'add_1_working_day_excluded_base_date',
|
||||
'add_3_working_day_excluded_base_date',
|
||||
'add_7_working_day_excluded_base_date'] %}
|
||||
{% set service_type_work_day_dict={} %}
|
||||
|
||||
{% for service_type, working_day in service_type_working_day_mapping.items() %}
|
||||
|
||||
{% set service_type_work_day_dict = service_type_work_day_dict.update(
|
||||
{service_type: {
|
||||
'include_base': 'add_' ~ working_day ~ '_working_day_included_base_date',
|
||||
'exclude_base': 'add_' ~ working_day ~ '_working_day_excluded_base_date'
|
||||
}}) %}
|
||||
|
||||
{% endfor %}
|
||||
|
||||
|
||||
-- IMPORT
|
||||
WITH orders AS (
|
||||
SELECT * FROM {{ ref('fct_exchange__transaction_orders') }}
|
||||
SELECT * FROM {{ ref('fct_exchange__transaction_orders') }}
|
||||
),
|
||||
|
||||
companies AS (
|
||||
@@ -39,67 +46,47 @@ users AS (
|
||||
SELECT * FROM {{ ref('dim_exchange__users') }}
|
||||
),
|
||||
|
||||
--LOGIC
|
||||
|
||||
-- LOGIC
|
||||
fct_and_dim_joins AS (
|
||||
|
||||
SELECT
|
||||
orders.booking_id,
|
||||
orders.booking_marking_id,
|
||||
orders.order_id,
|
||||
orders.cost_id,
|
||||
orders.company_id,
|
||||
orders.transaction_status,
|
||||
orders.document_status,
|
||||
|
||||
companies.name AS company_name,
|
||||
companies.company_marking_id AS company_marking_id,
|
||||
companies.autocount_id AS company_autocount_id,
|
||||
companies.company_type AS company_type,
|
||||
companies.business_type AS company_business_type,
|
||||
companies.exchange_rate_segment AS company_exchange_rate_segment,
|
||||
companies.is_migrated_company AS is_migrated_company,
|
||||
companies.country_name AS company_country_name,
|
||||
companies.state_name AS company_state_name,
|
||||
companies.district_name AS company_district_name,
|
||||
companies.postcode AS company_postcode,
|
||||
companies.has_wallet AS company_has_wallet,
|
||||
companies.latitude AS company_latitude,
|
||||
companies.longitude AS company_longitude,
|
||||
companies.company_created_datetime,
|
||||
companies.company_lifetime_value,
|
||||
companies.m_score_lifetime AS company_m_score_lifetime,
|
||||
|
||||
orders.supplier_company_id,
|
||||
orders.user_id,
|
||||
orders.bank_id,
|
||||
|
||||
fix_currencies.currency_name AS fix_currency_name,
|
||||
|
||||
quote_currencies.currency_name AS quote_currency_name,
|
||||
|
||||
base_currencies.currency_name AS base_currency_name,
|
||||
|
||||
orders.currency_vendor_bank_id,
|
||||
orders.transaction_status,
|
||||
orders.document_status,
|
||||
orders.service_type,
|
||||
orders.booking_status,
|
||||
orders.order_status,
|
||||
orders.cost_status,
|
||||
orders.order_payment_method,
|
||||
orders.order_payment_reference,
|
||||
orders.order_bill_number,
|
||||
orders.cost_transaction_type,
|
||||
orders.cost_payment_method,
|
||||
orders.cost_payment_reference,
|
||||
orders.cost_bill_number,
|
||||
orders.purchase_order_number,
|
||||
orders.invoice_number,
|
||||
orders.is_first_time_booking_company,
|
||||
orders.is_first_time_booking_company_completed,
|
||||
orders.is_first_time_booking_user,
|
||||
orders.is_first_time_booking_user_completed,
|
||||
orders.estimate_booking_base_to_quote_currency_exchange_rate,
|
||||
orders.estimate_booking_quote_value,
|
||||
orders.estimate_booking_base_value,
|
||||
orders.estimate_booking_value_rm,
|
||||
orders.booking_created_datetime,
|
||||
orders.booking_approved_datetime,
|
||||
orders.booking_completed_datetime,
|
||||
orders.booking_suspended_datetime,
|
||||
|
||||
orders.order_id,
|
||||
orders.order_payment_method,
|
||||
orders.order_status,
|
||||
orders.order_payment_reference,
|
||||
orders.order_bill_number,
|
||||
orders.is_first_time_order_company,
|
||||
orders.is_first_time_order_company_completed,
|
||||
orders.is_first_time_order_user,
|
||||
orders.is_first_time_order_user_completed,
|
||||
orders.order_base_to_quote_currency_exchange_rate,
|
||||
orders.estimate_booking_quote_value,
|
||||
orders.estimate_booking_base_value,
|
||||
orders.estimate_booking_value_rm,
|
||||
orders.estimate_booking_base_to_quote_currency_exchange_rate,
|
||||
orders.order_base_value,
|
||||
orders.order_base_service_charge,
|
||||
orders.order_base_tax,
|
||||
@@ -108,94 +95,35 @@ fct_and_dim_joins AS (
|
||||
orders.order_service_charge_rm,
|
||||
orders.order_tax_rm,
|
||||
orders.total_order_value_rm,
|
||||
|
||||
order_created_dates.first_day_of_week AS order_created_week,
|
||||
orders.order_created_datetime,
|
||||
orders.order_pending_submission_datetime,
|
||||
orders.order_pending_verification_datetime,
|
||||
|
||||
-- Estimated delivery datetime based on CIEF internal SLA (cutoff time 4pm)
|
||||
CASE
|
||||
{% for (loop_service_type, working_day_with_base_date, working_day_without_base_date) in zip(var_service_type, var_working_days_including_base_date, var_working_days_excluding_base_date) %}
|
||||
|
||||
WHEN orders.order_created_datetime >= '2023-01-01' AND orders.service_type = '{{loop_service_type}}' AND DATE_PART(HOUR, orders.order_pending_verification_datetime) < 16
|
||||
THEN TO_TIMESTAMP(order_created_dates.{{working_day_with_base_date}} || ' 23:59:59 +08:00')
|
||||
WHEN orders.order_created_datetime >= '2023-01-01' AND orders.service_type = '{{loop_service_type}}' AND DATE_PART(HOUR, orders.order_pending_verification_datetime) >= 16
|
||||
THEN TO_TIMESTAMP(order_created_dates.{{working_day_without_base_date}} || ' 23:59:59 +08:00')
|
||||
|
||||
{% endfor %}
|
||||
|
||||
ELSE NULL
|
||||
|
||||
END AS estimated_order_delivery_datetime_website_sla,
|
||||
|
||||
--- Estimated delivery datetime based on customer expectation
|
||||
CASE
|
||||
{% for (loop_service_type, working_day_with_base_date, working_day_without_base_date) in zip(var_service_type, var_working_days_including_base_date, var_working_days_excluding_base_date) %}
|
||||
|
||||
WHEN orders.order_created_datetime >= '2023-01-01' AND orders.service_type = '{{loop_service_type}}' AND DATE_PART(HOUR, orders.order_pending_verification_datetime) < 16
|
||||
THEN TO_TIMESTAMP(order_created_dates.{{working_day_with_base_date}} || ' ' || (orders.order_pending_verification_datetime::TIME)::STRING )
|
||||
WHEN orders.order_created_datetime >= '2023-01-01' AND orders.service_type = '{{loop_service_type}}' AND DATE_PART(HOUR, orders.order_pending_verification_datetime) >= 16
|
||||
THEN TO_TIMESTAMP(order_created_dates.{{working_day_without_base_date}} || ' ' || (orders.order_pending_verification_datetime::TIME)::STRING )
|
||||
|
||||
{% endfor %}
|
||||
|
||||
ELSE NULL
|
||||
|
||||
END AS estimated_order_delivery_datetime_customer_expectation,
|
||||
|
||||
-- On time delivery boolean (CIEF internal SLA)
|
||||
CASE
|
||||
-- On-time boolean is null when order is not complete
|
||||
WHEN account_verified_payment_datetime IS NULL THEN NULL
|
||||
WHEN estimated_order_delivery_datetime_website_sla > orders.operation_uploaded_bank_slip_datetime THEN 1
|
||||
ELSE 0
|
||||
|
||||
END AS is_on_time_delivery_website_sla,
|
||||
|
||||
-- On time delivery boolean (Customer Expectation)
|
||||
CASE
|
||||
-- On-time boolean is null when order is not complete
|
||||
WHEN account_verified_payment_datetime IS NULL THEN NULL
|
||||
WHEN estimated_order_delivery_datetime_customer_expectation > orders.operation_uploaded_bank_slip_datetime THEN 1
|
||||
ELSE 0
|
||||
|
||||
END AS is_on_time_delivery_customer_expectation,
|
||||
|
||||
orders.order_approved_datetime,
|
||||
orders.order_completed_datetime,
|
||||
orders.order_rejected_datetime,
|
||||
orders.order_suspended_datetime,
|
||||
orders.order_expected_expired_datetime,
|
||||
orders.order_expired_datetime,
|
||||
|
||||
orders.cost_id,
|
||||
orders.supplier_company_id,
|
||||
supplier_companies.name AS supplier_company_name,
|
||||
orders.currency_vendor_bank_id,
|
||||
orders.cost_transaction_type,
|
||||
orders.cost_payment_method,
|
||||
orders.cost_status,
|
||||
orders.cost_payment_reference,
|
||||
orders.cost_bill_number,
|
||||
orders.order_base_to_quote_currency_exchange_rate,
|
||||
orders.cost_base_value,
|
||||
orders.cost_quote_value,
|
||||
orders.cost_base_to_quote_currency_exchange_rate,
|
||||
orders.cost_base_tax,
|
||||
orders.cost_base_service_charge,
|
||||
orders.cost_value_rm,
|
||||
orders.cost_tax_rm,
|
||||
orders.cost_service_charge_rm,
|
||||
orders.total_cost_value_rm,
|
||||
orders.purchase_order_number,
|
||||
orders.invoice_number,
|
||||
orders.cost_base_to_quote_currency_exchange_rate,
|
||||
orders.booking_created_datetime,
|
||||
orders.booking_approved_datetime,
|
||||
orders.booking_completed_datetime,
|
||||
orders.booking_suspended_datetime,
|
||||
orders.order_created_datetime,
|
||||
orders.order_pending_submission_datetime,
|
||||
orders.order_pending_verification_datetime,
|
||||
orders.order_approved_datetime,
|
||||
orders.order_completed_datetime,
|
||||
orders.order_rejected_datetime,
|
||||
orders.order_suspended_datetime,
|
||||
orders.order_expected_expired_datetime,
|
||||
orders.order_expired_datetime,
|
||||
orders.order_deleted_datetime,
|
||||
orders.cost_created_datetime,
|
||||
orders.cost_pending_submission_datetime,
|
||||
orders.cost_pending_verification_datetime,
|
||||
orders.cost_approved_datetime,
|
||||
orders.cost_completed_datetime,
|
||||
orders.cost_rejected_datetime,
|
||||
|
||||
orders.customer_made_booking_datetime,
|
||||
orders.customer_placed_order_datetime,
|
||||
orders.customer_made_payment_datetime,
|
||||
@@ -209,6 +137,78 @@ fct_and_dim_joins AS (
|
||||
orders.account_rejected_payment_datetime,
|
||||
orders.next_order_created_datetime,
|
||||
|
||||
companies.name AS company_name,
|
||||
companies.company_marking_id AS company_marking_id,
|
||||
companies.autocount_id AS company_autocount_id,
|
||||
companies.company_type AS company_type,
|
||||
companies.business_type AS company_business_type,
|
||||
companies.exchange_rate_segment AS company_exchange_rate_segment,
|
||||
companies.is_migrated_company AS is_migrated_company,
|
||||
companies.country_name AS company_country_name,
|
||||
companies.state_name AS company_state_name,
|
||||
companies.district_name AS company_district_name,
|
||||
companies.postcode AS company_postcode,
|
||||
companies.has_wallet AS company_has_wallet,
|
||||
companies.latitude AS company_latitude,
|
||||
companies.longitude AS company_longitude,
|
||||
companies.m_score_lifetime AS company_m_score_lifetime,
|
||||
companies.company_created_datetime,
|
||||
companies.company_lifetime_value,
|
||||
|
||||
fix_currencies.currency_name AS fix_currency_name,
|
||||
|
||||
quote_currencies.currency_name AS quote_currency_name,
|
||||
|
||||
base_currencies.currency_name AS base_currency_name,
|
||||
|
||||
order_created_dates.first_day_of_week AS order_created_week,
|
||||
|
||||
supplier_companies.name AS supplier_company_name,
|
||||
|
||||
-- Estimated delivery datetime based on CIEF internal SLA (cutoff time 4pm)
|
||||
CASE
|
||||
{% for (service_type, working_day) in service_type_work_day_dict.items() %}
|
||||
|
||||
WHEN orders.order_created_datetime >= '2023-01-01' AND orders.service_type = '{{service_type}}' AND DATE_PART(HOUR, orders.order_pending_verification_datetime) < 16
|
||||
THEN TO_TIMESTAMP(order_created_dates.{{working_day['include_base']}} || ' 23:59:59 +08:00')
|
||||
WHEN orders.order_created_datetime >= '2023-01-01' AND orders.service_type = '{{service_type}}' AND DATE_PART(HOUR, orders.order_pending_verification_datetime) >= 16
|
||||
THEN TO_TIMESTAMP(order_created_dates.{{working_day['exclude_base']}} || ' 23:59:59 +08:00')
|
||||
|
||||
{% endfor %}
|
||||
|
||||
ELSE NULL
|
||||
END AS estimated_order_delivery_datetime_website_sla,
|
||||
|
||||
--- Estimated delivery datetime based on customer expectation
|
||||
CASE
|
||||
{% for (service_type, working_day) in service_type_work_day_dict.items() %}
|
||||
|
||||
WHEN orders.order_created_datetime >= '2023-01-01' AND orders.service_type = '{{service_type}}' AND DATE_PART(HOUR, orders.order_pending_verification_datetime) < 16
|
||||
THEN TO_TIMESTAMP(order_created_dates.{{working_day['include_base']}} || ' ' || (orders.order_pending_verification_datetime::TIME)::STRING )
|
||||
WHEN orders.order_created_datetime >= '2023-01-01' AND orders.service_type = '{{service_type}}' AND DATE_PART(HOUR, orders.order_pending_verification_datetime) >= 16
|
||||
THEN TO_TIMESTAMP(order_created_dates.{{working_day['exclude_base']}} || ' ' || (orders.order_pending_verification_datetime::TIME)::STRING )
|
||||
|
||||
{% endfor %}
|
||||
|
||||
ELSE NULL
|
||||
END AS estimated_order_delivery_datetime_customer_expectation,
|
||||
|
||||
-- On time delivery boolean (CIEF internal SLA)
|
||||
CASE
|
||||
-- On-time boolean is null when order is not complete
|
||||
WHEN account_verified_payment_datetime IS NULL THEN NULL
|
||||
WHEN estimated_order_delivery_datetime_website_sla > orders.operation_uploaded_bank_slip_datetime THEN 1
|
||||
ELSE 0
|
||||
END AS is_on_time_delivery_website_sla,
|
||||
|
||||
-- On time delivery boolean (Customer Expectation)
|
||||
CASE
|
||||
-- On-time boolean is null when order is not complete
|
||||
WHEN account_verified_payment_datetime IS NULL THEN NULL
|
||||
WHEN estimated_order_delivery_datetime_customer_expectation > orders.operation_uploaded_bank_slip_datetime THEN 1
|
||||
ELSE 0
|
||||
END AS is_on_time_delivery_customer_expectation,
|
||||
|
||||
'{{ modules.datetime.datetime.now(modules.pytz.timezone("Asia/Kuala_Lumpur")) }}' AS _dbt_ran_datetime
|
||||
|
||||
FROM orders
|
||||
@@ -230,10 +230,12 @@ fct_and_dim_joins AS (
|
||||
|
||||
LEFT JOIN currencies AS base_currencies
|
||||
ON (orders.base_currency_id = base_currencies.currency_id)
|
||||
|
||||
),
|
||||
|
||||
--FINAL
|
||||
final__rep_exchange__daily_orders AS (
|
||||
|
||||
SELECT
|
||||
-- ids
|
||||
order_id,
|
||||
@@ -329,12 +331,12 @@ final__rep_exchange__daily_orders AS (
|
||||
order_rejected_datetime,
|
||||
order_suspended_datetime,
|
||||
order_expired_datetime,
|
||||
order_deleted_datetime,
|
||||
cost_created_datetime,
|
||||
cost_pending_submission_datetime,
|
||||
cost_pending_verification_datetime,
|
||||
cost_approved_datetime,
|
||||
cost_completed_datetime,
|
||||
cost_rejected_datetime,
|
||||
booking_created_datetime,
|
||||
booking_approved_datetime,
|
||||
booking_completed_datetime,
|
||||
|
||||
Reference in New Issue
Block a user