1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//! [`OrdrespBuilder`] — fluent type-safe builder for ORDRSP messages.
use std::marker::PhantomData;
use edifact_rs::Writer;
use crate::AgencyCode;
use crate::{Error, Release};
use super::{Set, Unset, bytes_to_segments};
#[derive(Debug, Clone)]
struct OrdrespBuilderInner {
release: Release,
sender_id: Option<String>,
receiver_id: Option<String>,
sender_agency: Option<AgencyCode>,
receiver_agency: Option<AgencyCode>,
message_ref: String,
document_id: Option<String>,
document_date: Option<String>,
pruefidentifikator: Option<u32>,
/// BGM DE 1001. Defaults to `7`; the ESA answers use `Z57`.
document_code: Option<String>,
/// SG1 references as `(1153 qualifier, 1154 value)`. Additive: an ESA
/// answer carries the correlation reference *and* `RFF+Z13`.
references: Vec<(String, String)>,
/// `IMD+7081` Abonnement code — Muss on 19011/19012.
abonnement: Option<String>,
// Additive ESA-Antwort (PID 19011-19014) content — ORDRSP carries no LOC.
/// SG2 `AJT` as `(4465 Prüfschritt-Code, 1082 EBD)`.
adjustment: Option<String>,
/// SG2 `AJT` DE 1082 — the Entscheidungsbaum the Prüfschritt comes from.
adjustment_ebd: Option<String>,
adjustment_reason: Option<String>,
item_description: bool,
line_item: bool,
}
/// Fluent builder for `ORDRSP` (Purchase Order Response) messages.
///
/// Wire type string: `ORDRSP:D:10A:UN:{release}`.
///
/// # Type-state
///
/// [`build`](OrdrespBuilder::build) is only available once both
/// [`sender`](OrdrespBuilder::sender) and [`receiver`](OrdrespBuilder::receiver)
/// have been called.
///
/// # Example
///
/// ```rust,no_run
/// use edi_energy::Release;
/// use edi_energy::builders::OrdrespBuilder;
///
/// let msg = OrdrespBuilder::new(Release::new("1.4b"))
/// .sender("9900357000004")
/// .receiver("4012345000023")
/// .document_id("ORDRSP20251001001")
/// .build()?;
///
/// assert_eq!(msg.assoc_code(), "1.4b");
/// # Ok::<(), edi_energy::Error>(())
/// ```
#[derive(Debug, Clone)]
#[must_use = "Builder must be consumed via .build() or .serialize()"]
pub struct OrdrespBuilder<S = Unset, R = Unset> {
_ph: PhantomData<fn() -> (S, R)>,
inner: OrdrespBuilderInner,
}
impl OrdrespBuilder<Unset, Unset> {
/// Create a builder targeting the given EDI@Energy release.
pub fn new(release: Release) -> Self {
Self {
_ph: PhantomData,
inner: OrdrespBuilderInner {
release,
sender_id: None,
receiver_id: None,
sender_agency: None,
receiver_agency: None,
message_ref: "1".to_owned(),
document_id: None,
document_date: None,
pruefidentifikator: None,
document_code: None,
references: Vec::new(),
abonnement: None,
adjustment: None,
adjustment_ebd: None,
adjustment_reason: None,
item_description: false,
line_item: false,
},
}
}
}
impl<S, R> OrdrespBuilder<S, R> {
fn transition<S2, R2>(self) -> OrdrespBuilder<S2, R2> {
OrdrespBuilder {
_ph: PhantomData,
inner: self.inner,
}
}
/// Set the message sender's market-participant identifier.
pub fn sender(mut self, id: impl Into<String>) -> OrdrespBuilder<Set, R> {
self.inner.sender_id = Some(id.into());
self.transition()
}
/// Set the message recipient's market-participant identifier.
pub fn receiver(mut self, id: impl Into<String>) -> OrdrespBuilder<S, Set> {
self.inner.receiver_id = Some(id.into());
self.transition()
}
/// Override the agency code for the sender's party identifier.
///
/// Leave unset and the agency is derived from the MP-ID itself —
/// [`AgencyCode::for_mp_id`]: `99…` → BDEW `293`, `98…` → DVGW `332`, any
/// other 13-digit code → GS1 `9`. Override only for a party whose
/// registered code list differs from what its number implies.
pub fn sender_agency(mut self, agency: crate::AgencyCode) -> Self {
self.inner.sender_agency = Some(agency);
self
}
/// Override the agency code for the receiver's party identifier.
///
/// Derived from the MP-ID when unset — see
/// [`sender_agency`](Self::sender_agency).
pub fn receiver_agency(mut self, agency: crate::AgencyCode) -> Self {
self.inner.receiver_agency = Some(agency);
self
}
/// Set the BGM document identifier.
pub fn document_id(mut self, id: impl Into<String>) -> Self {
self.inner.document_id = Some(id.into());
self
}
/// Override the message reference number. Defaults to `"1"`.
pub fn message_ref(mut self, reference: impl Into<String>) -> Self {
self.inner.message_ref = reference.into();
self
}
/// Set the document date for DTM+137 (`YYYYMMDD`).
pub fn document_date(mut self, date: impl Into<String>) -> Self {
self.inner.document_date = Some(date.into());
self
}
/// Set the Prüfidentifikator (BGM DE 1004) — the routing key of the answer
/// (e.g. 19011 Bestätigung, 19012 Ablehnung for ESA Wertebestellung).
pub fn pruefidentifikator(mut self, pid: u32) -> Self {
self.inner.pruefidentifikator = Some(pid);
self
}
/// Add an SG1 reference `RFF+<qual>:<value>`. Additive.
pub fn reference(mut self, qualifier: impl Into<String>, value: impl Into<String>) -> Self {
self.inner.references.push((qualifier.into(), value.into()));
self
}
/// Set the BGM DE 1001 document code. Defaults to `7`.
///
/// The ESA Wertebestellung answers use `Z57` („Übermittlung von Werten an
/// ESA“, ORDRSP AHB 1.1b §4.15).
pub fn document_code(mut self, code: impl Into<String>) -> Self {
self.inner.document_code = Some(code.into());
self
}
/// Set the `IMD+7081` Abonnement code (`Z01`/`Z02`/`Z03`).
///
/// **Muss** on ORDRSP 19011/19012; it echoes the ORDERS being answered and
/// selects the EBD the `AJT` cites.
pub fn abonnement(mut self, code: impl Into<String>) -> Self {
self.inner.abonnement = Some(code.into());
self
}
/// Set the SG2 adjustment — emits `AJT+<4465 Prüfschritt>+<1082 EBD>`.
///
/// **Muss** on every ESA answer PID: DE 4465 carries the Prüfschritt code
/// and DE 1082 the Entscheidungsbaum it belongs to (`E_0254`, `E_0256`,
/// `E_0257`). Without the EBD the receiver cannot resolve what the code
/// means — the same numeric code lives in several trees.
pub fn adjustment(mut self, code: impl Into<String>, ebd: impl Into<String>) -> Self {
self.inner.adjustment = Some(code.into());
self.inner.adjustment_ebd = Some(ebd.into());
self
}
/// Set the SG2 adjustment reason — emits `FTX+<code>` after the AJT.
///
/// The MIG caps this FTX at two elements, so the reason is carried as the
/// coded 4451 qualifier (`∈ {AAP, ABO, Z27, Z28, Z33}`), not free text.
pub fn adjustment_reason(mut self, ftx_qualifier: impl Into<String>) -> Self {
self.inner.adjustment_reason = Some(ftx_qualifier.into());
self
}
/// Emit a minimal item description `IMD+A` (the AHB requires the segment).
pub fn item_description(mut self) -> Self {
self.inner.item_description = true;
self
}
/// Emit an SG27 line item `LIN+1` (the AHB requires one for PID 19011).
pub fn line_item(mut self) -> Self {
self.inner.line_item = true;
self
}
fn to_bytes(&self) -> Result<Vec<u8>, Error> {
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);
// BGM DE 1004 carries the Prüfidentifikator (profile pid_source =
// BgmDe1004); fall back to `document_id` for non-MaKo callers.
// BGM DE 1004 is the **Dokumentennummer** (ORDRSP AHB 1.1b); the
// Prüfidentifikator has its own `SG1 RFF+Z13`.
let bgm_1004 = self
.inner
.document_id
.as_deref()
.unwrap_or(&self.inner.message_ref);
emit_comp!(
w,
"UNH",
[&self.inner.message_ref],
["ORDRSP", "D", "10A", "UN", self.inner.release.as_str()]
);
// ORDRSP BGM: DE 1001 = 7 (the only value the MIG permits), DE 1004 =
// the Prüfidentifikator, and no third element.
emit_seg!(
w,
"BGM",
self.inner.document_code.as_deref().unwrap_or("7"),
bgm_1004
);
// `DTM+137` Dokumentendatum. Every EDI@Energy AHB gives DE 2379 as
// `303` (`CCYYMMDDHHMMZZZ`) with condition `[931]` fixing the zone to
// `+00`; `[494]` requires the stamp to be the creation moment or
// earlier. There is no Anwendungsfall in any AHB that takes `102`.
emit_comp!(w, "DTM", ["137", &super::ccyymmddhhmm_utc(&dtm_val), "303"]);
// Abonnement — `IMD++<7081>` (DE 7081 sits in C272, element 2).
if let Some(code) = &self.inner.abonnement {
emit_comp!(w, "IMD", [""], [code]);
} else if self.inner.item_description {
// Free-form indicator, for the non-ESA answer PIDs.
emit_comp!(w, "IMD", ["A"]);
}
// ── SG1: references ──────────────────────────────────────────────────
// The MIG lists the Referenz places (`RFF+AAG`, `RFF+ON`, `RFF+AAV`)
// before the Prüfidentifikator's.
for (q, v) in &self.inner.references {
emit_comp!(w, "RFF", [q, v]);
}
if let Some(pid) = self.inner.pruefidentifikator {
emit_comp!(w, "RFF", ["Z13", &pid.to_string()]);
}
// ── SG2: adjustment (Prüfschritt + EBD) + coded reason ───────────────
if let Some(code) = &self.inner.adjustment {
// DE 1082 only where the column lists a Codeliste (19013 marks
// DE 4465 alone).
if let Some(ebd) = self
.inner
.adjustment_ebd
.as_deref()
.filter(|e| !e.is_empty())
{
emit_comp!(w, "AJT", [code], [ebd]);
} else {
emit_comp!(w, "AJT", [code]);
}
}
if let Some(code) = &self.inner.adjustment_reason {
emit_comp!(w, "FTX", [code]);
}
// ── SG3: parties ─────────────────────────────────────────────────────
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)]
);
}
// ── SG27: line item ──────────────────────────────────────────────────
if self.inner.line_item {
emit_seg!(w, "LIN", "1");
}
emit_seg!(w, "UNS", "S");
w.finish_unt(&self.inner.message_ref)
.map_err(Error::Parse)?;
Ok(buf)
}
/// Build and serialize the message to EDIFACT bytes.
///
/// # Errors
///
/// Returns an [`Error`] if serialization fails.
pub fn serialize(self) -> Result<Vec<u8>, Error> {
self.to_bytes()
}
}
impl OrdrespBuilder<Set, Set> {
/// Build and return a fully-parsed [`crate::messages::ordrsp::OrdrespMessage`].
///
/// # Errors
///
/// Returns an [`Error`] if EDIFACT serialization or parsing fails.
pub fn build(self) -> Result<crate::messages::ordrsp::OrdrespMessage, Error> {
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::ordrsp::OrdrespMessage::from_parts(
segments,
message_ref.as_str(),
assoc_code.as_str(),
None,
))
}
}