From 6c18529317fe7eb990240b6cf7cab3d5d27d75da Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Mon, 25 Mar 2024 17:47:43 +0100 Subject: [PATCH] Take only into account the more expensive option when computing ACSI Apparently, this is how it is done, now. --- deploy/draft_payment.sql | 17 +++++++++++++++-- test/draft_payment.sql | 9 +++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/deploy/draft_payment.sql b/deploy/draft_payment.sql index c4bba9e..b344d91 100644 --- a/deploy/draft_payment.sql +++ b/deploy/draft_payment.sql @@ -118,6 +118,20 @@ begin from unnest(options) as option(campsite_type_option_id, units) ); + with discountable_acsi_option as ( + select day, campsite_type_option_id, units + from ( + select day, campsite_type_option_id, acsi_option.units, row_number() over (partition by day order by cost desc) as rn + from generate_series(arrival_date, departure_date - 1, interval '1 day') as date(day) + join season_calendar on season_range @> date.day::date + join campsite_type_option_cost using (season_id) + join unnest(options) as option(campsite_type_option_id, units) using (campsite_type_option_id) + join acsi_calendar on acsi_card and day::date <@ acsi_range + join acsi_option as acsi_option using (campsite_type_option_id) + ) as discountable + where acsi_card + and rn = 1 + ) insert into payment_option ( payment_id , campsite_type_option_id @@ -133,8 +147,7 @@ begin join campsite_type_option_cost using (season_id) join campsite_type_option using (campsite_type_option_id) join unnest(options) as option(campsite_type_option_id, units) using (campsite_type_option_id) - left join acsi_calendar on acsi_card and day::date <@ acsi_range - left join acsi_option on acsi_option.campsite_type_id = acsi_calendar.campsite_type_id and acsi_option.campsite_type_option_id = campsite_type_option.campsite_type_option_id + left join discountable_acsi_option as acsi_option using (day, campsite_type_option_id) group by campsite_type_option.campsite_type_option_id , option.units , per_night diff --git a/test/draft_payment.sql b/test/draft_payment.sql index 8475a72..8bab09d 100644 --- a/test/draft_payment.sql +++ b/test/draft_payment.sql @@ -98,6 +98,7 @@ values (16, 4, 800) , (20, 8, 590) ; +alter table payment alter column payment_id restart with 26; insert into payment (payment_id, slug, company_id, campsite_type_id, arrival_date, departure_date, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, currency_code, zone_preferences, acsi_card, payment_status, created_at, updated_at) values (22, '7cccfe16-695e-486d-a6a5-1162fb85cafb', 2, 12, current_date + 60, current_date + 62, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'USD', '', false, 'draft', '2024-01-01 01:01:01', '2024-01-01 01:01:01') , (24, '6eeae04c-2fea-4d67-97dc-a4b8a83df99f', 2, 12, current_date + 61, current_date + 62, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'USD', '', false, 'pending', '2024-01-01 02:02:02', '2024-01-01 02:02:02') @@ -161,7 +162,7 @@ select bag_eq( , (2, 14, current_date + 6, current_date + 11, 85000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 86750, 'EUR', 1.0, '', 'draft', current_timestamp, current_timestamp) , (2, 12, current_date + 61, current_date + 62, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'USD', 1.0, '', 'pending', '2024-01-01 02:02:02', '2024-01-01 02:02:02') , (2, 14, current_date + 31, current_date + 36, 85000, 2, 0, 1, 0, 1, 0, 1, 0, 3500, 96000, 'EUR', 0.3, 'under a tree', 'draft', current_timestamp, current_timestamp) - , (2, 12, current_date + 58, current_date + 65, 6000, 3, 12830, 1, 5210, 1, 4180, 1, 1750, 7350, 61130, 'EUR', 0.3, '', 'draft', current_timestamp, current_timestamp) + , (2, 12, current_date + 58, current_date + 65, 6000, 3, 12830, 1, 5210, 1, 4180, 1, 1750, 7350, 62290, 'EUR', 0.3, '', 'draft', current_timestamp, current_timestamp) $$, 'Should have added and updated payments' ); @@ -173,9 +174,9 @@ select bag_eq( , ('b', 16, 2, 0) , ('c', 16, 1, 4000) , ('c', 18, 1, 3500) - , ('c', 16, 2, 8860) - , ('c', 18, 3, 12190) - , ('c', 20, 4, 2760) + , ('c', 16, 2, 8860) -- This is the most expensive, thus the only discounted + , ('c', 18, 3, 13350) -- No discount + , ('c', 20, 4, 2760) -- This is not even in ACSI $$, 'Should have added, updated, and removed payment options' );