use std::marker::PhantomData;
use edifact_rs::Writer;
use crate::AgencyCode;
use crate::utilmd_codes::{
AntwortStatus, IDE_VORGANG, Produktpaket, STS_STATUS_ANTWORT, STS_TRANSAKTIONSGRUND,
Transaktionsgrund,
};
use crate::{Error, Lokationstyp, Pruefidentifikator, Release};
use super::{Set, Unset, bytes_to_segments};
#[derive(Debug, Clone, Default)]
struct UtilmdTransactionSpec {
ide_qualifier: String,
vorgangsnummer: String,
process_dates: Vec<(String, String)>,
transaktionsgrund: Option<Transaktionsgrund>,
antwort: Option<AntwortStatus>,
antwort_dritter: Option<crate::utilmd_codes::DritterAntwortStatus>,
free_texts: Vec<(String, String)>,
agr: Option<(String, String)>,
locations: Vec<(String, String)>,
references: Vec<(String, String)>,
referenz_vorgangsnummer: Option<String>,
produktpakete: Vec<Produktpaket>,
merkmale: Vec<(String, String)>,
customer_nads: Vec<(String, String)>,
named_nads: Vec<(String, Vec<String>, String)>,
}
#[derive(Debug, Clone)]
struct UtilmdBuilderInner {
release: Release,
pruefidentifikator: Option<Pruefidentifikator>,
sender_id: Option<String>,
receiver_id: Option<String>,
sender_agency: Option<AgencyCode>,
receiver_agency: Option<AgencyCode>,
message_ref: String,
document_code: String,
document_date: Option<String>,
rff_entries: Vec<(String, String)>,
transactions: Vec<UtilmdTransactionSpec>,
}
#[derive(Debug, Clone)]
#[must_use = "Builder must be consumed via .build() or .serialize()"]
pub struct UtilmdBuilder<S = Unset, R = Unset> {
_ph: PhantomData<fn() -> (S, R)>,
inner: UtilmdBuilderInner,
}
impl UtilmdBuilder<Unset, Unset> {
pub fn new(release: Release) -> Self {
Self {
_ph: PhantomData,
inner: UtilmdBuilderInner {
release,
pruefidentifikator: None,
sender_id: None,
receiver_id: None,
sender_agency: None,
receiver_agency: None,
message_ref: "1".to_owned(),
document_code: "E01".to_owned(),
document_date: None,
rff_entries: Vec::new(),
transactions: Vec::new(),
},
}
}
}
impl<S, R> UtilmdBuilder<S, R> {
fn transition<S2, R2>(self) -> UtilmdBuilder<S2, R2> {
UtilmdBuilder {
_ph: PhantomData,
inner: self.inner,
}
}
pub fn sender(mut self, id: impl Into<String>) -> UtilmdBuilder<Set, R> {
self.inner.sender_id = Some(id.into());
self.transition()
}
pub fn receiver(mut self, id: impl Into<String>) -> UtilmdBuilder<S, Set> {
self.inner.receiver_id = Some(id.into());
self.transition()
}
pub fn sender_agency(mut self, agency: crate::AgencyCode) -> Self {
self.inner.sender_agency = Some(agency);
self
}
pub fn receiver_agency(mut self, agency: crate::AgencyCode) -> Self {
self.inner.receiver_agency = Some(agency);
self
}
pub fn pruefidentifikator(mut self, pid: Pruefidentifikator) -> Self {
self.inner.pruefidentifikator = Some(pid);
self
}
pub fn message_ref(mut self, reference: impl Into<String>) -> Self {
self.inner.message_ref = reference.into();
self
}
pub fn document_code(mut self, code: impl Into<String>) -> Self {
self.inner.document_code = code.into();
self
}
pub fn document_date(mut self, date: impl Into<String>) -> Self {
self.inner.document_date = Some(date.into());
self
}
pub fn rff(mut self, qualifier: impl Into<String>, reference: impl Into<String>) -> Self {
self.inner
.rff_entries
.push((qualifier.into(), reference.into()));
self
}
pub fn transaction(self, vorgangsnummer: impl Into<String>) -> UtilmdTransactionBuilder<S, R> {
self.transaction_with_qualifier(IDE_VORGANG, vorgangsnummer)
}
pub fn list_transaction(self, list_id: impl Into<String>) -> UtilmdTransactionBuilder<S, R> {
self.transaction_with_qualifier(crate::utilmd_codes::IDE_LISTE, list_id)
}
pub fn transaction_with_qualifier(
self,
ide_qualifier: impl Into<String>,
vorgangsnummer: impl Into<String>,
) -> UtilmdTransactionBuilder<S, R> {
UtilmdTransactionBuilder {
parent: self,
spec: UtilmdTransactionSpec {
ide_qualifier: ide_qualifier.into(),
vorgangsnummer: vorgangsnummer.into(),
..Default::default()
},
}
}
}
fn emit_sg6<W: std::io::Write>(
w: &mut Writer<W>,
pid_str: &str,
tx: &UtilmdTransactionSpec,
) -> Result<(), Error> {
if !pid_str.is_empty() {
emit_comp!(w, "RFF", ["Z13", pid_str]);
}
if let Some(referenz) = &tx.referenz_vorgangsnummer {
emit_comp!(w, "RFF", ["TN", referenz]);
}
for (rff_q, rff_ref) in &tx.references {
emit_comp!(w, "RFF", [rff_q, rff_ref]);
}
Ok(())
}
fn emit_sg8_produktpakete<W: std::io::Write>(
w: &mut Writer<W>,
tx: &UtilmdTransactionSpec,
) -> Result<(), Error> {
use crate::utilmd_codes::produkt;
for paket in &tx.produktpakete {
emit_seg!(
w,
"SEQ",
produkt::SEQ_PRODUKTPAKET,
&paket.paket_id.to_string()
);
for p in &paket.produkte {
emit_comp!(
w,
"PIA",
[produkt::PIA_ERFORDERLICHES_PRODUKT],
[&p.produkt_code, produkt::PIA_TYP_PRODUKT]
);
emit_seg!(w, "CCI", produkt::CCI_PRODUKTEIGENSCHAFT);
if let Some(eigenschaft) = &p.eigenschaft {
emit_comp!(w, "CAV", [produkt::CAV_EIGENSCHAFT, "", "", eigenschaft]);
}
if let Some(wert) = &p.wert {
emit_comp!(w, "CAV", [produkt::CAV_WERT, "", "", wert]);
}
}
}
for (idx, paket) in tx.produktpakete.iter().enumerate() {
emit_seg!(
w,
"SEQ",
produkt::SEQ_PRIORISIERUNG,
&paket.paket_id.to_string()
);
emit_seg!(
w,
"CCI",
produkt::CCI_UMSETZUNGSGRAD,
"",
"",
paket.umsetzung.code()
);
if tx.produktpakete.len() > 1
&& let Some(prio) = produkt::PRIORITAET.get(idx)
{
emit_seg!(w, "CAV", prio);
}
}
Ok(())
}
fn emit_sg4<W: std::io::Write>(
w: &mut Writer<W>,
pid_str: &str,
tx: &UtilmdTransactionSpec,
) -> Result<(), Error> {
emit_seg!(w, "IDE", &tx.ide_qualifier, &tx.vorgangsnummer);
for (qualifier, date_val) in &tx.process_dates {
let fmt = sg4_dtm_format(qualifier);
let value = if fmt == "303" {
super::ccyymmddhhmm_utc(date_val)
} else {
date_val.clone()
};
emit_comp!(w, "DTM", [qualifier, &value, fmt]);
}
if let Some(grund) = &tx.transaktionsgrund {
let ergaenzung = grund.ergaenzung.as_deref().unwrap_or("");
match grund.befristet.as_deref() {
Some(befristet) => emit_seg!(
w,
"STS",
STS_TRANSAKTIONSGRUND,
"",
&grund.grund,
ergaenzung,
befristet
),
None if !ergaenzung.is_empty() => {
emit_seg!(
w,
"STS",
STS_TRANSAKTIONSGRUND,
"",
&grund.grund,
ergaenzung
);
}
None => emit_seg!(w, "STS", STS_TRANSAKTIONSGRUND, "", &grund.grund),
}
}
if let Some(antwort) = &tx.antwort {
if let Some(cl) = antwort.codeliste.as_deref() {
emit_comp!(w, "STS", [STS_STATUS_ANTWORT], [""], [&antwort.code, cl]);
} else {
emit_seg!(w, "STS", STS_STATUS_ANTWORT, "", &antwort.code);
}
}
if let Some(dritter) = &tx.antwort_dritter {
let referenz = dritter.referenz_lokation.as_deref().unwrap_or("");
if let Some(objekt) = dritter.objekt.as_deref() {
emit_comp!(
w,
"STS",
[crate::utilmd_codes::STS_ANTWORT_DRITTER],
[referenz],
[&dritter.code, &dritter.codeliste],
[objekt]
);
} else {
emit_comp!(
w,
"STS",
[crate::utilmd_codes::STS_ANTWORT_DRITTER],
[referenz],
[&dritter.code, &dritter.codeliste]
);
}
}
for (ftx_q, ftx_text) in &tx.free_texts {
emit_comp!(w, "FTX", [ftx_q], [""], [""], [ftx_text]);
}
if let Some((svc_req, resp_type)) = &tx.agr {
emit_comp!(w, "AGR", [svc_req, resp_type]);
}
for (loc_q, loc_id) in &tx.locations {
emit_comp!(w, "LOC", [loc_q], [loc_id]);
}
emit_sg6(w, pid_str, tx)?;
for (klassentyp, wert) in &tx.merkmale {
emit_seg!(w, "CCI", klassentyp, "", wert);
}
emit_sg8_produktpakete(w, tx)?;
for (nad_q, parts, format) in &tx.named_nads {
let mut c080: Vec<&str> = parts.iter().map(String::as_str).take(5).collect();
c080.resize(5, "");
c080.push(format.as_str());
w.write_composites(
"NAD",
&[&[nad_q.as_str()][..], &[""][..], &[""][..], &c080[..]],
)
.map_err(Error::Parse)?;
}
for (nad_q, nad_id) in &tx.customer_nads {
emit_comp!(
w,
"NAD",
[nad_q],
[nad_id, "", super::agency_for(None, nad_id)]
);
}
Ok(())
}
impl<S, R> UtilmdBuilder<S, R> {
fn to_bytes(&self) -> Result<Vec<u8>, Error> {
let pid_str = self
.inner
.pruefidentifikator
.map(|p| format!("{:05}", p.as_u32()))
.unwrap_or_default();
let dtm_val = self
.inner
.document_date
.as_deref()
.map_or_else(super::now_ccyymmddhhmm, str::to_owned);
let mut buf = Vec::new();
let mut w = Writer::new(&mut buf);
emit_comp!(
w,
"UNH",
[&self.inner.message_ref],
["UTILMD", "D", "11A", "UN", self.inner.release.as_str()]
);
emit_seg!(w, "BGM", &self.inner.document_code, &pid_str, "9");
emit_comp!(w, "DTM", ["137", &super::ccyymmddhhmm_utc(&dtm_val), "303"]);
for (qualifier, reference) in &self.inner.rff_entries {
emit_comp!(w, "RFF", [qualifier, reference]);
}
if let Some(id) = &self.inner.sender_id {
emit_comp!(
w,
"NAD",
["MS"],
[id, "", super::agency_for(self.inner.sender_agency, id)]
);
}
if let Some(id) = &self.inner.receiver_id {
emit_comp!(
w,
"NAD",
["MR"],
[id, "", super::agency_for(self.inner.receiver_agency, id)]
);
}
for tx in &self.inner.transactions {
emit_sg4(&mut w, &pid_str, tx)?;
}
w.finish_unt(&self.inner.message_ref)
.map_err(Error::Parse)?;
Ok(buf)
}
pub fn serialize(self) -> Result<Vec<u8>, Error> {
self.to_bytes()
}
}
impl UtilmdBuilder<Set, Set> {
pub fn build(self) -> Result<crate::messages::utilmd::UtilmdMessage, Error> {
let pid = self
.inner
.pruefidentifikator
.map(super::super::pruefidentifikator::Pruefidentifikator::as_u32);
let message_ref = self.inner.message_ref.clone();
let assoc_code = self.inner.release.as_str().to_owned();
let segments = bytes_to_segments(&self.to_bytes()?)?;
Ok(crate::messages::utilmd::UtilmdMessage::from_parts(
segments,
message_ref.as_str(),
assoc_code.as_str(),
pid,
))
}
}
#[derive(Debug)]
#[must_use = "Sub-builder must be finalized with .done()"]
pub struct UtilmdTransactionBuilder<S = Unset, R = Unset> {
parent: UtilmdBuilder<S, R>,
spec: UtilmdTransactionSpec,
}
impl<S, R> UtilmdTransactionBuilder<S, R> {
pub fn transaktionsgrund(mut self, grund: Transaktionsgrund) -> Self {
self.spec.transaktionsgrund = Some(grund);
self
}
pub fn antwort(mut self, antwort: AntwortStatus) -> Self {
self.spec.antwort = Some(antwort);
self
}
pub fn date(mut self, qualifier: impl Into<String>, value: impl Into<String>) -> Self {
self.spec
.process_dates
.push((qualifier.into(), value.into()));
self
}
pub fn location(mut self, lokationstyp: Lokationstyp, id: impl Into<String>) -> Self {
self.spec
.locations
.push((lokationstyp.qualifier_code().to_owned(), id.into()));
self
}
pub fn marktlokation(self, malo_id: impl Into<String>) -> Self {
self.location(Lokationstyp::Marktlokation, malo_id)
}
pub fn messlokation(self, melo_id: impl Into<String>) -> Self {
self.location(Lokationstyp::Messlokation, melo_id)
}
pub fn referenz_vorgangsnummer(mut self, vorgangsnummer: impl Into<String>) -> Self {
self.spec.referenz_vorgangsnummer = Some(vorgangsnummer.into());
self
}
pub fn merkmal(mut self, klassentyp: impl Into<String>, wert: impl Into<String>) -> Self {
self.spec.merkmale.push((klassentyp.into(), wert.into()));
self
}
pub fn produktpaket(mut self, paket: Produktpaket) -> Self {
self.spec.produktpakete.push(paket);
self
}
pub fn reference(mut self, qualifier: impl Into<String>, ref_id: impl Into<String>) -> Self {
self.spec.references.push((qualifier.into(), ref_id.into()));
self
}
pub fn customer(mut self, party_qualifier: impl Into<String>, id: impl Into<String>) -> Self {
self.spec
.customer_nads
.push((party_qualifier.into(), id.into()));
self
}
pub fn named_party(
mut self,
party_qualifier: impl Into<String>,
name_parts: impl IntoIterator<Item = String>,
name_format: impl Into<String>,
) -> Self {
self.spec.named_nads.push((
party_qualifier.into(),
name_parts.into_iter().collect(),
name_format.into(),
));
self
}
pub fn kunde_des_lf(
self,
name_parts: impl IntoIterator<Item = String>,
name_format: impl Into<String>,
) -> Self {
self.named_party(
crate::utilmd_codes::nad::KUNDE_DES_LF,
name_parts,
name_format,
)
}
pub fn beteiligter_marktpartner(self, mp_id: impl Into<String>) -> Self {
self.customer(crate::utilmd_codes::nad::ZUGEHOERIGE_PARTEI, mp_id)
}
pub fn antwort_dritter(mut self, dritter: crate::utilmd_codes::DritterAntwortStatus) -> Self {
self.spec.antwort_dritter = Some(dritter);
self
}
pub fn free_text(mut self, text_function: impl Into<String>, text: impl Into<String>) -> Self {
self.spec
.free_texts
.push((text_function.into(), text.into()));
self
}
pub fn agr(
mut self,
service_requirement: impl Into<String>,
response_type: impl Into<String>,
) -> Self {
self.spec.agr = Some((service_requirement.into(), response_type.into()));
self
}
pub fn done(mut self) -> UtilmdBuilder<S, R> {
self.parent.inner.transactions.push(self.spec);
self.parent
}
}
fn sg4_dtm_format(qualifier: &str) -> &'static str {
match qualifier {
"154" => "102",
"Z10" => "106",
_ => "303",
}
}