{{ config(materialized="table", unique_key=["id_booking"]) }} with int_core__booking_to_service as ( select * from {{ ref("int_core__booking_to_service") }} ), int_core__booking_service_detail as ( select * from {{ ref("int_core__booking_service_detail") }} ) select bts.id_booking, bts.id_verification_request, bts.id_accommodation, bts.id_user_product_bundle, bts.id_deal, bts.id_user_host, bts.id_user_guest, bts.booking_status, bts.program_name, bts.booking_created_at_utc, bts.booking_created_date_utc, bts.booking_updated_at_utc, bts.booking_updated_date_utc, bts.booking_check_in_at_utc, bts.booking_check_in_date_utc, bts.booking_check_out_at_utc, bts.booking_check_out_date_utc, bts.booking_number_of_nights, bts.host_currency_code, bts.is_user_in_new_dash, bts.new_dash_version, bts.user_in_new_dash_since_timestamp_at_utc, sum(bsd.service_total_price_in_gbp) as booking_total_price_in_gbp, min(bsd.service_first_chargeable_date_utc) as service_first_chargeable_date_utc, max(bsd.service_last_chargeable_date_utc) as service_last_chargeable_date_utc, min(bsd.service_first_billable_date_utc) as service_first_billable_date_utc, max(bsd.service_last_billable_date_utc) as service_last_billable_date_utc, min(bsd.service_detail_created_at_utc) as service_first_created_at_utc, max(bsd.service_detail_created_at_utc) as service_last_created_at_utc, max(bsd.service_detail_updated_at_utc) as service_last_updated_at_utc, count(distinct bsd.id_booking_service_detail) as number_of_applied_services, count( distinct case when bsd.is_paid_service then bsd.id_booking_service_detail else null end ) as number_of_applied_paid_services, count( distinct case when bsd.is_upgraded_service then bsd.id_booking_service_detail else null end ) as number_of_applied_upgraded_services, count( distinct case when bsd.is_billable_service then bsd.id_booking_service_detail else null end ) as number_of_applied_billable_services, case when sum(bsd.service_total_price_in_gbp) > 0 and min(bsd.service_first_chargeable_date_utc) is not null then true else false end as is_booking_chargeable, case when sum( case when bsd.is_billable_service then bsd.service_total_price_in_gbp else 0 end ) > 0 and min(bsd.service_first_billable_date_utc) is not null then true else false end as is_booking_billable, case when sum(case when bsd.is_missing_currency_code then 1 else 0 end) > 0 then true else false end as is_missing_currency_code_in_service_detail, case when sum(case when bsd.is_booking_cancelled then 1 else 0 end) > 0 then true else false end as is_booking_cancelled, case when bts.id_verification_request is null then false else true end as has_verification_request, case when sum(case when bsd.is_paid_service then 1 else 0 end) > 0 then true else false end as has_paid_services, case when sum(case when bsd.is_upgraded_service then 1 else 0 end) > 0 then true else false end as has_upgraded_services, case when sum(case when bsd.is_billable_service then 1 else 0 end) > 0 then true else false end as has_billable_services, case when sum(case when bsd.service_business_type = 'SCREENING' then 1 else 0 end) > 0 then true else false end as has_screening_service_business_type, case when sum( case when bsd.service_business_type = 'SCREENING' and bsd.service_name <> {{ var("default_service") }} then 1 else 0 end ) > 0 then true else false end as has_upgraded_screening_service_business_type, case when sum( case when bsd.service_business_type = 'DEPOSIT_MANAGEMENT' then 1 else 0 end ) > 0 then true else false end as has_deposit_management_service_business_type, case when sum(case when bsd.service_business_type = 'PROTECTION' then 1 else 0 end) > 0 then true else false end as has_protection_service_business_type, bts.is_missing_id_deal, case when bts.host_currency_code is null then true else false end as is_missing_host_currency_code from int_core__booking_to_service bts inner join int_core__booking_service_detail bsd on bts.id_booking_service_detail = bsd.id_booking_service_detail group by bts.id_booking, bts.id_verification_request, bts.id_accommodation, bts.id_user_product_bundle, bts.id_deal, bts.id_user_host, bts.id_user_guest, bts.booking_status, bts.program_name, bts.booking_created_at_utc, bts.booking_created_date_utc, bts.booking_updated_at_utc, bts.booking_updated_date_utc, bts.booking_check_in_at_utc, bts.booking_check_in_date_utc, bts.booking_check_out_at_utc, bts.booking_check_out_date_utc, bts.booking_number_of_nights, bts.host_currency_code, bts.is_missing_id_deal, bts.is_user_in_new_dash, bts.new_dash_version, bts.user_in_new_dash_since_timestamp_at_utc