#[doc(inline)]
pub use carrier_core::count::Explanation;
use core::fmt;
use carrier_core::count::{Berlin, Day, backward, forward, next_midnight, start_of, working_hours};
use jiff::civil::Date;
use jiff::{SignedDuration, Timestamp};
use crate::calendar::Calendar;
use crate::{Geschaeftsfall, Role};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Rule {
pub id: &'static str,
pub source: &'static str,
pub text: &'static str,
}
macro_rules! rules {
($($name:ident = $id:literal, $source:literal, $text:literal;)+) => {
pub mod rules {
use super::Rule;
$(
#[doc = $text]
pub const $name: Rule = Rule { id: $id, source: $source, text: $text };
)+
}
};
}
rules! {
VA_LEAD = "va.lead", "Anl. 1 V5.4.1 §2.5",
"A VA-KUE-MRN or VA-KUE-ORN asks for a date at least 20 AT ahead, a VA-RRNP at least 7 AT.";
VA_ANSWER = "va.answer", "Anl. 1 V5.4.1 §2.5, §2.6.1",
"The EKPabg answers a pre-agreement request within 3 AT after the positive TEQ.";
WECHSELTERMIN = "ruem-va.date", "Anl. 1 V5.4.1 §2.5",
"An agreed VA-KUE switch date is at least 15 AT ahead and not before the requested date.";
TEQ = "teq.escalation", "Anl. 1 V5.4.1 §2.6.1, §2.11.1",
"Without a technical receipt 2 hours after sending, the sender escalates.";
STR_AUF_LEAD = "str-auf.lead", "Anl. 1 V5.4.1 §2.9.1, §2.9.2",
"A cancellation (Aufhebung) arrives at least 3 AT before the agreed switch date.";
STR_AEN_LEAD = "str-aen.lead", "Anl. 1 V5.4.1 §2.10",
"A cancellation for change (Änderung) arrives at least 8 AT before the agreed switch date.";
TVS_LEAD = "tvs.lead", "Anl. 1 V5.4.1 §2.11",
"A postponement arrives at least 3 AT before the agreed switch date (24 hours on working days when EKPauf and EKPabg share the network operator).";
ANSWER_24H = "answer.24h", "Anl. 1 V5.4.1 §2.9.1, §2.10, §2.11",
"A cancellation or postponement is answered within 24 hours, counted on working days, after the positive TEQ.";
AKM_TR_REMINDER = "akm-tr.reminder", "Anl. 1 V5.4.1 §2.6.2",
"Without an AKM-TR, the EKPabg reminds 10 AT before the switch date.";
AKM_TR_ESCALATION = "akm-tr.escalation", "Anl. 1 V5.4.1 §2.6.2",
"Without an AKM-TR 4 AT before the switch date, the EKPabg escalates.";
NEW_VA = "str-aen.new-va", "Anl. 1 V5.4.1 §2.10",
"After an agreed change, the EKPauf submits the new pre-agreement by 7 AT before the switch date, or the first result counts as cancelled.";
LATER_PORTING = "rrnp.later", "Anl. 1 V5.4.1 §2.3",
"A later porting (VA-RRNP) is agreed if the number was switched off at most 65 AT before.";
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DeadlineKind {
TeqEscalation,
VaAnswer,
Answer24h,
StornoAufhebungLatest,
StornoAenderungLatest,
TerminverschiebungLatest,
AkmTrReminder,
AkmTrEscalation,
NewVorabstimmung,
LaterPorting,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Deadline {
pub kind: DeadlineKind,
pub party: Role,
pub due: Timestamp,
pub rule: Rule,
pub explanation: Explanation,
}
impl Deadline {
#[must_use]
pub fn is_overdue(&self, now: Timestamp) -> bool {
now >= self.due
}
}
impl fmt::Display for Deadline {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: by {} — {} ({})", self.party, Berlin(self.due), self.rule.text, self.rule.source)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EarliestDate {
pub date: Date,
pub rule: Rule,
pub explanation: Explanation,
}
impl fmt::Display for EarliestDate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "not before {} — {} ({})", Day(self.date), self.rule.text, self.rule.source)
}
}
#[must_use]
pub fn earliest_kundenwunschtermin(fall: Geschaeftsfall, sent_at: Timestamp, cal: &Calendar) -> Option<EarliestDate> {
let n = match fall {
Geschaeftsfall::VaKueMrn | Geschaeftsfall::VaKueOrn => 20,
Geschaeftsfall::VaRrnp => 7,
_ => return None,
};
let (date, explanation) = forward(cal, "pre-agreement sent", sent_at, n);
Some(EarliestDate { date, rule: rules::VA_LEAD, explanation })
}
#[must_use]
pub fn earliest_wechseltermin(answered_at: Timestamp, requested: Date, cal: &Calendar) -> EarliestDate {
let (date, explanation) = forward(cal, "RUEM-VA sent", answered_at, 15);
EarliestDate { date: date.max(requested), rule: rules::WECHSELTERMIN, explanation }
}
#[must_use]
pub fn teq_escalation(sent_at: Timestamp, sender: Role) -> Deadline {
Deadline {
kind: DeadlineKind::TeqEscalation,
party: sender.other(),
due: carrier_core::count::shift(sent_at, SignedDuration::from_hours(2)),
rule: rules::TEQ,
explanation: Explanation {
start: "sent",
from: sent_at,
counted: vec![],
skipped: vec![],
method: "2 hours of elapsed time",
},
}
}
#[must_use]
pub fn va_answer(teq_at: Timestamp, cal: &Calendar) -> Deadline {
let (day, explanation) = forward(cal, "positive TEQ", teq_at, 3);
Deadline {
kind: DeadlineKind::VaAnswer,
party: Role::EkpAbg,
due: next_midnight(day),
rule: rules::VA_ANSWER,
explanation,
}
}
#[must_use]
pub fn answer_24h(teq_at: Timestamp, answering: Role, cal: &Calendar) -> Deadline {
let (due, explanation) = working_hours(cal, "positive TEQ", teq_at, 24, true);
Deadline { kind: DeadlineKind::Answer24h, party: answering, due, rule: rules::ANSWER_24H, explanation }
}
#[must_use]
pub fn storno_aufhebung_latest(wechseltermin: Date, sender: Role, cal: &Calendar) -> Deadline {
lead(DeadlineKind::StornoAufhebungLatest, sender, wechseltermin, 3, rules::STR_AUF_LEAD, cal)
}
#[must_use]
pub fn storno_aenderung_latest(wechseltermin: Date, sender: Role, cal: &Calendar) -> Deadline {
lead(DeadlineKind::StornoAenderungLatest, sender, wechseltermin, 8, rules::STR_AEN_LEAD, cal)
}
#[must_use]
pub fn terminverschiebung_latest(wechseltermin: Date, same_network_operator: bool, cal: &Calendar) -> Deadline {
if same_network_operator {
let (due, explanation) = working_hours(cal, "agreed switch date", start_of(wechseltermin), 24, false);
return Deadline {
kind: DeadlineKind::TerminverschiebungLatest,
party: Role::EkpAuf,
due,
rule: rules::TVS_LEAD,
explanation,
};
}
lead(DeadlineKind::TerminverschiebungLatest, Role::EkpAuf, wechseltermin, 3, rules::TVS_LEAD, cal)
}
#[must_use]
pub fn akm_tr_reminder(wechseltermin: Date, cal: &Calendar) -> Deadline {
lead(DeadlineKind::AkmTrReminder, Role::EkpAuf, wechseltermin, 10, rules::AKM_TR_REMINDER, cal)
}
#[must_use]
pub fn akm_tr_escalation(wechseltermin: Date, cal: &Calendar) -> Deadline {
lead(DeadlineKind::AkmTrEscalation, Role::EkpAuf, wechseltermin, 4, rules::AKM_TR_ESCALATION, cal)
}
#[must_use]
pub fn new_vorabstimmung(wechseltermin: Date, cal: &Calendar) -> Deadline {
lead(DeadlineKind::NewVorabstimmung, Role::EkpAuf, wechseltermin, 7, rules::NEW_VA, cal)
}
#[must_use]
pub fn later_porting(abgeschaltet: Date, cal: &Calendar) -> Deadline {
let (day, explanation) = forward(cal, "number switched off", start_of(abgeschaltet), 65);
Deadline {
kind: DeadlineKind::LaterPorting,
party: Role::EkpAuf,
due: next_midnight(day),
rule: rules::LATER_PORTING,
explanation,
}
}
fn lead(kind: DeadlineKind, party: Role, x: Date, n: u32, rule: Rule, cal: &Calendar) -> Deadline {
let (day, explanation) = backward(cal, "agreed switch date", x, n);
Deadline { kind, party, due: start_of(day), rule, explanation }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn extreme_timestamps_do_not_panic() {
assert_eq!(teq_escalation(Timestamp::MAX, Role::EkpAuf).due, Timestamp::MAX);
let cal = Calendar::wbci();
let _ = va_answer(Timestamp::MAX, &cal);
let _ = answer_24h(Timestamp::MAX, Role::EkpAbg, &cal);
let _ = earliest_wechseltermin(Timestamp::MAX, Date::MAX, &cal);
let _ = storno_aufhebung_latest(Date::MAX, Role::EkpAuf, &cal);
let _ = later_porting(Date::MAX, &cal);
let _ = earliest_kundenwunschtermin(Geschaeftsfall::VaKueMrn, Timestamp::MIN, &cal);
}
}