use jiff::civil::Date;
use crate::model::{
Abbm, AbbmTr, AkmTr, Anfrage, Erlm, Meldung, MeldungKopf, Position, PositionAbbm, PositionRuemVa, RuemVa, Storno,
Teq, Terminverschiebung, UebernahmeLeitung, Vorabstimmung,
};
use crate::{CarrierCode, Freitext, Meldungscode, Meldungstext};
fn kopf(anfrage: &Anfrage) -> MeldungKopf {
MeldungKopf {
absender: anfrage.ekp().of(anfrage.sender().other()).clone(),
vorabstimmungs_id: anfrage.vorabstimmungs_id().clone(),
ekp: anfrage.ekp().clone(),
geschaeftsfall: anfrage.geschaeftsfall(),
}
}
impl RuemVa {
#[must_use]
pub fn zustimmung(va: &Vorabstimmung, wechseltermin: Date) -> Self {
let code = if wechseltermin == va.kundenwunschtermin { Meldungscode::Zwa } else { Meldungscode::Nat };
Self {
kopf: kopf(&Anfrage::Vorabstimmung(va.clone())),
positionen: vec![Position::new(code).into()],
ressourcen: Vec::new(),
technologie: None,
wechseltermin,
rufnummernportierung: None,
}
}
#[must_use]
pub fn with_position(mut self, position: impl Into<PositionRuemVa>) -> Self {
self.positionen.push(position.into());
self
}
}
impl Abbm {
#[must_use]
pub fn ablehnung(anfrage: &Anfrage, code: Meldungscode) -> Self {
let (storno_id, aenderungs_id) = match anfrage {
Anfrage::Storno(s) => (Some(s.storno_id.clone()), None),
Anfrage::Terminverschiebung(t) => (None, Some(t.aenderungs_id.clone())),
Anfrage::Vorabstimmung(_) => (None, None),
};
Self {
kopf: kopf(anfrage),
positionen: vec![Position::new(code).into()],
wechseltermin: None,
storno_id,
aenderungs_id,
begruendung: None,
}
}
#[must_use]
pub fn with_position(mut self, position: impl Into<PositionAbbm>) -> Self {
self.positionen.push(position.into());
self
}
#[must_use]
pub fn with_begruendung(mut self, begruendung: Freitext) -> Self {
self.begruendung = Some(begruendung);
self
}
}
impl Erlm {
#[must_use]
pub fn storno_ok(storno: &Storno) -> Self {
Self {
kopf: kopf(&Anfrage::Storno(storno.clone())),
positionen: vec![Position::new(Meldungscode::StornoOk)],
wechseltermin: None,
storno_id: Some(storno.storno_id.clone()),
aenderungs_id: None,
}
}
#[must_use]
pub fn terminverschiebung_ok(tvs: &Terminverschiebung) -> Self {
Self {
kopf: kopf(&Anfrage::Terminverschiebung(tvs.clone())),
positionen: vec![Position::new(Meldungscode::TerminverschiebungOk)],
wechseltermin: Some(tvs.neuer_kundenwunschtermin),
storno_id: None,
aenderungs_id: Some(tvs.aenderungs_id.clone()),
}
}
}
impl AkmTr {
#[must_use]
pub fn uebernahme(ruem_va: &RuemVa, leitungen: Vec<UebernahmeLeitung>, sicherer_hafen: bool) -> Self {
let mut kopf = ruem_va.kopf.clone();
kopf.absender = kopf.ekp.auf.clone();
Self {
kopf,
positionen: vec![Position::new(Meldungscode::AntwortAufnehmend)],
ressourcenuebernahme: !leitungen.is_empty(),
uebernahme_leitung: leitungen,
sicherer_hafen,
pki_auf: None,
}
}
}
impl AbbmTr {
#[must_use]
pub fn ablehnung(akm_tr: &AkmTr, code: Meldungscode) -> Self {
let mut kopf = akm_tr.kopf.clone();
kopf.absender = kopf.ekp.abg.clone();
Self { kopf, positionen: vec![Position::new(code)] }
}
}
impl Teq {
#[must_use]
pub fn for_anfrage(anfrage: &Anfrage, code: Meldungscode) -> Self {
let (storno_id, aenderungs_id) = match anfrage {
Anfrage::Storno(s) => (Some(s.storno_id.clone()), None),
Anfrage::Terminverschiebung(t) => (None, Some(t.aenderungs_id.clone())),
Anfrage::Vorabstimmung(_) => (None, None),
};
Self {
absender: anfrage.ekp().of(anfrage.sender().other()).clone(),
vorabstimmungs_id: anfrage.vorabstimmungs_id().clone(),
positionen: vec![Position::new(code)],
storno_id,
aenderungs_id,
}
}
#[must_use]
pub fn for_meldung(meldung: &Meldung, code: Meldungscode) -> Self {
let k = meldung.kopf();
let receiver = k.ekp.role_of(&k.absender).map_or_else(|| k.ekp.auf.clone(), |r| k.ekp.of(r.other()).clone());
Self {
absender: receiver,
vorabstimmungs_id: k.vorabstimmungs_id.clone(),
positionen: vec![Position::new(code)],
storno_id: meldung.storno_id().cloned(),
aenderungs_id: meldung.aenderungs_id().cloned(),
}
}
#[must_use]
pub fn fehler(
absender: CarrierCode,
vorabstimmungs_id: crate::VorabstimmungsId,
code: Meldungscode,
text: Option<Meldungstext>,
) -> Self {
let position = match text {
Some(text) => Position::with_text(code, text),
None => Position::new(code),
};
Self { absender, vorabstimmungs_id, positionen: vec![position], storno_id: None, aenderungs_id: None }
}
}