From 0281e6e72eac43d178368e3d045f3219fbb97a61 Mon Sep 17 00:00:00 2001 From: Joaquin Ossa Date: Thu, 1 Aug 2024 12:58:36 +0200 Subject: [PATCH 1/4] Included the exchange rate to GBP in the model --- .../core/int_core__host_booking_fees.sql | 19 ++++++++++++++----- models/intermediate/core/schema.yaml | 4 ++++ .../core/core__host_booking_fees.sql | 1 + models/reporting/core/schema.yaml | 4 ++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/models/intermediate/core/int_core__host_booking_fees.sql b/models/intermediate/core/int_core__host_booking_fees.sql index bb0e8dc..76d8949 100644 --- a/models/intermediate/core/int_core__host_booking_fees.sql +++ b/models/intermediate/core/int_core__host_booking_fees.sql @@ -7,7 +7,10 @@ with int_core__duplicate_bookings as ( select * from {{ ref("int_core__duplicate_bookings") }} ), - int_core__unified_user as (select * from {{ ref("int_core__unified_user") }}) + int_core__unified_user as (select * from {{ ref("int_core__unified_user") }}), + int_daily_currency_exchange_rates as ( + select * from {{ ref("int_daily_currency_exchange_rates") }} + ) select b.id_booking, b.id_user_guest, @@ -17,14 +20,20 @@ select coalesce(db.is_duplicate_booking, false) as is_duplicate_booking, bce.booking_fee_local, uu.account_currency_iso4217, + dcer.rate as rate_to_gbp, bce.booking_fee_charge_at_utc, bce.booking_fee_charge_date_utc from stg_core__booking b left join int_core__booking_charge_events bce on b.id_booking = bce.id_booking left join stg_core__booking_state bs on b.id_booking_state = bs.id_booking_state left join int_core__duplicate_bookings db on b.id_booking = db.id_booking +left join int_core__unified_user uu on lower(b.id_user_host) = lower(uu.id_user) +-- We user 'lower' because the id_user_host can be found in capital letters or not +-- depending on the table and Postgres is case sensitive left join - int_core__unified_user uu on lower(b.id_user_host) = lower(uu.id_user) - -- We user 'lower' because the id_user_host can be found in capital letters or not - -- depending on the table and Postgres is case sensitive - + int_daily_currency_exchange_rates dcer + on ( + dcer.from_currency = uu.account_currency_iso4217 + and dcer.rate_date_utc = bce.booking_fee_charge_date_utc + ) +where dcer.to_currency = 'GBP' diff --git a/models/intermediate/core/schema.yaml b/models/intermediate/core/schema.yaml index a925eb4..42b482a 100644 --- a/models/intermediate/core/schema.yaml +++ b/models/intermediate/core/schema.yaml @@ -1643,6 +1643,10 @@ models: data_type: character varying description: "Currency used by host/pm/platform users." + - name: rate_to_gbp + data_type: numeric + description: "Exchange rate of the used currency to GBP on the charged date" + - name: booking_fee_charge_at_utc data_type: timestamp without time zone description: | diff --git a/models/reporting/core/core__host_booking_fees.sql b/models/reporting/core/core__host_booking_fees.sql index 5961b10..2757af2 100644 --- a/models/reporting/core/core__host_booking_fees.sql +++ b/models/reporting/core/core__host_booking_fees.sql @@ -11,6 +11,7 @@ select is_duplicate_booking as is_duplicate_booking, booking_fee_local as booking_fee_local, account_currency_iso4217 as account_currency_iso4217, + rate_to_gbp as rate_to_gbp, booking_fee_charge_at_utc as booking_fee_charge_at_utc, booking_fee_charge_date_utc as booking_fee_charge_date_utc from int_core__host_booking_fees diff --git a/models/reporting/core/schema.yaml b/models/reporting/core/schema.yaml index f7ec4e4..dd82d98 100644 --- a/models/reporting/core/schema.yaml +++ b/models/reporting/core/schema.yaml @@ -1045,6 +1045,10 @@ models: data_type: character varying description: "Currency used by host/pm/platform users." + - name: rate_to_gbp + data_type: numeric + description: "Exchange rate of the used currency to GBP on the charged date" + - name: booking_fee_charge_at_utc data_type: timestamp without time zone description: | From 25e657014adb5cfb947823a1a18c1f959d63ebbd Mon Sep 17 00:00:00 2001 From: Joaquin Ossa Date: Thu, 1 Aug 2024 14:32:45 +0200 Subject: [PATCH 2/4] Modified filter and table used --- .../core/int_core__host_booking_fees.sql | 14 ++++++-------- models/intermediate/core/schema.yaml | 4 ++-- models/reporting/core/core__host_booking_fees.sql | 2 +- models/reporting/core/schema.yaml | 4 ++-- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/models/intermediate/core/int_core__host_booking_fees.sql b/models/intermediate/core/int_core__host_booking_fees.sql index 76d8949..246c904 100644 --- a/models/intermediate/core/int_core__host_booking_fees.sql +++ b/models/intermediate/core/int_core__host_booking_fees.sql @@ -8,9 +8,7 @@ with select * from {{ ref("int_core__duplicate_bookings") }} ), int_core__unified_user as (select * from {{ ref("int_core__unified_user") }}), - int_daily_currency_exchange_rates as ( - select * from {{ ref("int_daily_currency_exchange_rates") }} - ) + int_simple_exchange_rates as (select * from {{ ref("int_simple_exchange_rates") }}) select b.id_booking, b.id_user_guest, @@ -20,7 +18,7 @@ select coalesce(db.is_duplicate_booking, false) as is_duplicate_booking, bce.booking_fee_local, uu.account_currency_iso4217, - dcer.rate as rate_to_gbp, + ser.rate * bce.booking_fee_local as booking_fee_in_gbp, bce.booking_fee_charge_at_utc, bce.booking_fee_charge_date_utc from stg_core__booking b @@ -31,9 +29,9 @@ left join int_core__unified_user uu on lower(b.id_user_host) = lower(uu.id_user) -- We user 'lower' because the id_user_host can be found in capital letters or not -- depending on the table and Postgres is case sensitive left join - int_daily_currency_exchange_rates dcer + int_simple_exchange_rates ser on ( - dcer.from_currency = uu.account_currency_iso4217 - and dcer.rate_date_utc = bce.booking_fee_charge_date_utc + ser.from_currency = uu.account_currency_iso4217 + and ser.rate_date_utc = bce.booking_fee_charge_date_utc + and ser.to_currency = 'GBP' ) -where dcer.to_currency = 'GBP' diff --git a/models/intermediate/core/schema.yaml b/models/intermediate/core/schema.yaml index 42b482a..dc720ce 100644 --- a/models/intermediate/core/schema.yaml +++ b/models/intermediate/core/schema.yaml @@ -1643,9 +1643,9 @@ models: data_type: character varying description: "Currency used by host/pm/platform users." - - name: rate_to_gbp + - name: booking_fee_in_gbp data_type: numeric - description: "Exchange rate of the used currency to GBP on the charged date" + description: "The fee to apply to the booking, in GBP" - name: booking_fee_charge_at_utc data_type: timestamp without time zone diff --git a/models/reporting/core/core__host_booking_fees.sql b/models/reporting/core/core__host_booking_fees.sql index 2757af2..6ad0d69 100644 --- a/models/reporting/core/core__host_booking_fees.sql +++ b/models/reporting/core/core__host_booking_fees.sql @@ -11,7 +11,7 @@ select is_duplicate_booking as is_duplicate_booking, booking_fee_local as booking_fee_local, account_currency_iso4217 as account_currency_iso4217, - rate_to_gbp as rate_to_gbp, + booking_fee_in_gbp as booking_fee_in_gbp, booking_fee_charge_at_utc as booking_fee_charge_at_utc, booking_fee_charge_date_utc as booking_fee_charge_date_utc from int_core__host_booking_fees diff --git a/models/reporting/core/schema.yaml b/models/reporting/core/schema.yaml index dd82d98..67987f1 100644 --- a/models/reporting/core/schema.yaml +++ b/models/reporting/core/schema.yaml @@ -1045,9 +1045,9 @@ models: data_type: character varying description: "Currency used by host/pm/platform users." - - name: rate_to_gbp + - name: booking_fee_in_gbp data_type: numeric - description: "Exchange rate of the used currency to GBP on the charged date" + description: "The fee to apply to the booking, in GBP" - name: booking_fee_charge_at_utc data_type: timestamp without time zone From 02ddac31677e4d2db372477047d427720d37905c Mon Sep 17 00:00:00 2001 From: Joaquin Ossa Date: Thu, 1 Aug 2024 16:15:48 +0200 Subject: [PATCH 3/4] Added a coalesce for the booking_fee_in_gbp calculation for old cases where there is no preferred currency for the user_guest --- models/intermediate/core/int_core__host_booking_fees.sql | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/models/intermediate/core/int_core__host_booking_fees.sql b/models/intermediate/core/int_core__host_booking_fees.sql index 246c904..15566ac 100644 --- a/models/intermediate/core/int_core__host_booking_fees.sql +++ b/models/intermediate/core/int_core__host_booking_fees.sql @@ -8,7 +8,8 @@ with select * from {{ ref("int_core__duplicate_bookings") }} ), int_core__unified_user as (select * from {{ ref("int_core__unified_user") }}), - int_simple_exchange_rates as (select * from {{ ref("int_simple_exchange_rates") }}) + int_simple_exchange_rates as (select * from {{ ref("int_simple_exchange_rates") }}), + stg_core__country as (select * from {{ ref("stg_core__country") }}), select b.id_booking, b.id_user_guest, @@ -18,7 +19,9 @@ select coalesce(db.is_duplicate_booking, false) as is_duplicate_booking, bce.booking_fee_local, uu.account_currency_iso4217, - ser.rate * bce.booking_fee_local as booking_fee_in_gbp, + coalesce( + ser.rate * bce.booking_fee_local, bce.booking_fee_local + ) as booking_fee_in_gbp, bce.booking_fee_charge_at_utc, bce.booking_fee_charge_date_utc from stg_core__booking b From a82ab121f18f0ec65103407622d2a6222cd580de Mon Sep 17 00:00:00 2001 From: Joaquin Ossa Date: Thu, 1 Aug 2024 16:18:25 +0200 Subject: [PATCH 4/4] Deleted an extra line --- models/intermediate/core/int_core__host_booking_fees.sql | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/models/intermediate/core/int_core__host_booking_fees.sql b/models/intermediate/core/int_core__host_booking_fees.sql index 15566ac..020975a 100644 --- a/models/intermediate/core/int_core__host_booking_fees.sql +++ b/models/intermediate/core/int_core__host_booking_fees.sql @@ -8,8 +8,7 @@ with select * from {{ ref("int_core__duplicate_bookings") }} ), int_core__unified_user as (select * from {{ ref("int_core__unified_user") }}), - int_simple_exchange_rates as (select * from {{ ref("int_simple_exchange_rates") }}), - stg_core__country as (select * from {{ ref("stg_core__country") }}), + int_simple_exchange_rates as (select * from {{ ref("int_simple_exchange_rates") }}) select b.id_booking, b.id_user_guest,