|
| 1 | +use crate::{BankHolidays, CountryCode, Data, Date, Granularity}; |
| 2 | +use log::{debug, warn}; |
| 3 | + |
| 4 | +/// The disk-cached bank-holiday fetcher, re-exported from the foundation crate. |
| 5 | +pub type BankHolidaysFetcher<T = ()> = klirr_foundation::BankHolidaysFetcher<T>; |
| 6 | + |
| 7 | +/// Resolves the public holidays to deduct from billable days for an invoice, |
| 8 | +/// degrading gracefully so holiday lookup never blocks PDF generation. |
| 9 | +/// |
| 10 | +/// Returns an empty set (no deduction) when: |
| 11 | +/// - `worked_holidays` is set (the per-invoice `--worked-holidays` override), |
| 12 | +/// - the rate granularity is `Month`/`Fortnight` (holidays only affect day- and |
| 13 | +/// hour-billed invoices), |
| 14 | +/// - the vendor has not opted into `off_on_bank_holidays`, |
| 15 | +/// - the vendor's free-text country cannot be mapped to an ISO country code, or |
| 16 | +/// - the holiday API request fails (and nothing is cached). |
| 17 | +/// |
| 18 | +/// Otherwise it returns the (possibly cached) holidays for the vendor's country |
| 19 | +/// in the invoice period's year. |
| 20 | +/// |
| 21 | +/// The override and granularity checks come first, so an invoice for which |
| 22 | +/// holidays are irrelevant never triggers a country lookup or network request. |
| 23 | +/// |
| 24 | +/// When `refresh` is `true`, a cache hit is ignored and holidays are re-fetched |
| 25 | +/// from the API (the `--refresh-holidays` flag), picking up any corrections. |
| 26 | +pub fn resolve_bank_holidays( |
| 27 | + data: &Data, |
| 28 | + target_period_end_date: &Date, |
| 29 | + worked_holidays: bool, |
| 30 | + refresh: bool, |
| 31 | +) -> BankHolidays { |
| 32 | + if worked_holidays { |
| 33 | + debug!("--worked-holidays set; deducting no bank holidays for this invoice."); |
| 34 | + return BankHolidays::default(); |
| 35 | + } |
| 36 | + |
| 37 | + let granularity = data.service_fees().rate().granularity(); |
| 38 | + if !matches!(granularity, Granularity::Day | Granularity::Hour) { |
| 39 | + debug!( |
| 40 | + "Rate granularity {granularity:?} is unaffected by bank holidays; \ |
| 41 | + skipping holiday resolution." |
| 42 | + ); |
| 43 | + return BankHolidays::default(); |
| 44 | + } |
| 45 | + |
| 46 | + if !data.service_fees().off_on_bank_holidays() { |
| 47 | + return BankHolidays::default(); |
| 48 | + } |
| 49 | + |
| 50 | + let country_name = data.vendor().postal_address().country(); |
| 51 | + let Some(country_code) = CountryCode::from_country_name(country_name) else { |
| 52 | + warn!( |
| 53 | + "off_on_bank_holidays is enabled but vendor country '{country_name}' could not be \ |
| 54 | + resolved to an ISO country code; skipping bank-holiday deduction." |
| 55 | + ); |
| 56 | + return BankHolidays::default(); |
| 57 | + }; |
| 58 | + |
| 59 | + let year = *target_period_end_date.year(); |
| 60 | + debug!("Resolving bank holidays for {country_code} {year} (refresh: {refresh})."); |
| 61 | + match BankHolidaysFetcher::default().holidays_for(&country_code, year, refresh) { |
| 62 | + Ok(holidays) => holidays, |
| 63 | + Err(error) => { |
| 64 | + warn!( |
| 65 | + "Failed to fetch bank holidays for {country_code} {year}: {error}. \ |
| 66 | + Skipping bank-holiday deduction." |
| 67 | + ); |
| 68 | + BankHolidays::default() |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[cfg(test)] |
| 74 | +mod tests { |
| 75 | + use super::*; |
| 76 | + use crate::{HasSample, ServiceFees}; |
| 77 | + use test_log::test; |
| 78 | + |
| 79 | + /// Builds invoice data with a daily rate, the given `off_on_bank_holidays` |
| 80 | + /// setting, and vendor country, reusing the sample vendor for everything else. |
| 81 | + fn data_with(off_on_bank_holidays: bool, country: &str) -> Data { |
| 82 | + data_with_rate( |
| 83 | + off_on_bank_holidays, |
| 84 | + country, |
| 85 | + crate::Rate::daily(rust_decimal::dec!(100.0)), |
| 86 | + ) |
| 87 | + } |
| 88 | + |
| 89 | + /// As [`data_with`], but with an explicit rate so granularity can be varied. |
| 90 | + fn data_with_rate(off_on_bank_holidays: bool, country: &str, rate: crate::Rate) -> Data { |
| 91 | + let service_fees = ServiceFees::builder() |
| 92 | + .name("Consulting".to_string()) |
| 93 | + .rate(rate) |
| 94 | + .cadence(crate::Cadence::Monthly) |
| 95 | + .off_on_bank_holidays(off_on_bank_holidays) |
| 96 | + .build() |
| 97 | + .unwrap(); |
| 98 | + |
| 99 | + let vendor = crate::CompanyInformation::sample_vendor(); |
| 100 | + let address = vendor |
| 101 | + .postal_address() |
| 102 | + .clone() |
| 103 | + .with_country(country.to_string()); |
| 104 | + let vendor = vendor.with_postal_address(address); |
| 105 | + |
| 106 | + Data::builder() |
| 107 | + .information(crate::ProtoInvoiceInfo::sample()) |
| 108 | + .vendor(vendor) |
| 109 | + .client(crate::CompanyInformation::sample_client()) |
| 110 | + .payment_info(crate::PaymentInformation::sample()) |
| 111 | + .service_fees(service_fees) |
| 112 | + .expensed_periods(crate::ExpensedPeriods::sample()) |
| 113 | + .build() |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn disabled_flag_returns_empty() { |
| 118 | + // Sample data has off_on_bank_holidays = false. |
| 119 | + let data = Data::sample(); |
| 120 | + let period_end = Date::sample(); |
| 121 | + assert!(resolve_bank_holidays(&data, &period_end, false, false).is_empty()); |
| 122 | + } |
| 123 | + |
| 124 | + #[test] |
| 125 | + fn enabled_with_unresolved_country_returns_empty_without_network() { |
| 126 | + // A vendor whose country cannot be mapped must degrade to empty without |
| 127 | + // attempting (or depending on) any network call. |
| 128 | + let data = data_with(true, "Atlantis"); |
| 129 | + assert!(resolve_bank_holidays(&data, &Date::sample(), false, false).is_empty()); |
| 130 | + } |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn worked_holidays_override_wins_even_when_enabled_without_network() { |
| 134 | + // off_on_bank_holidays is on AND the country (Sweden) is resolvable, so |
| 135 | + // without the override this would hit the network. The --worked-holidays |
| 136 | + // override must short-circuit to empty before any country lookup/fetch. |
| 137 | + let data = data_with(true, "Sweden"); |
| 138 | + assert!(resolve_bank_holidays(&data, &Date::sample(), true, false).is_empty()); |
| 139 | + } |
| 140 | + |
| 141 | + #[test] |
| 142 | + fn refresh_alone_degrades_gracefully_for_unresolved_country() { |
| 143 | + // refresh=true still degrades to empty (no panic / no dependency) when |
| 144 | + // the country can't be resolved. |
| 145 | + let data = data_with(true, "Atlantis"); |
| 146 | + assert!(resolve_bank_holidays(&data, &Date::sample(), false, true).is_empty()); |
| 147 | + } |
| 148 | + |
| 149 | + #[test] |
| 150 | + fn monthly_granularity_skips_resolution_without_network() { |
| 151 | + // A fixed monthly rate is unaffected by holidays, so even with the |
| 152 | + // setting on and a resolvable country, no country lookup/fetch happens. |
| 153 | + let data = data_with_rate( |
| 154 | + true, |
| 155 | + "Sweden", |
| 156 | + crate::Rate::monthly(rust_decimal::dec!(50000.0)), |
| 157 | + ); |
| 158 | + assert!(resolve_bank_holidays(&data, &Date::sample(), false, false).is_empty()); |
| 159 | + } |
| 160 | +} |
0 commit comments