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
//! [`OrdersBuilder`] — fluent type-safe builder for ORDERS 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 OrdersBuilderInner {
release: Release,
sender_id: Option<String>,
receiver_id: Option<String>,
sender_agency: AgencyCode,
receiver_agency: AgencyCode,
message_ref: String,
document_code: Option<String>,
document_id: Option<String>,
/// `SG1 RFF+Z13` — the Prüfidentifikator. **Not** BGM DE 1004, which the
/// AHB reserves for the Dokumentennummer.
pruefidentifikator: Option<u32>,
document_date: Option<String>,
location: Option<String>,
// Additive ESA-Bestellung/Abbestellung (PID 17007/17008) content.
/// SG1 references as `(1153 qualifier, 1154 value)`. An ESA Bestellung
/// carries `RFF+AAG` (the Angebotsnummer) *and* `RFF+Z13`; an Abbestellung
/// carries `RFF+ACW` *and* `RFF+Z13`. One slot cannot hold both.
references: Vec<(String, String)>,
/// `DTM+203` Ausführungsdatum — **Muss** on 17007/17008.
ausfuehrungsdatum: Option<String>,
/// `IMD+7081` — `Z01` Start Abo / `Z02` Ende Abo / `Z03` ohne Abo.
abonnement: Option<String>,
item_description: Option<String>,
}
/// Fluent builder for `ORDERS` (Purchase Order) messages.
///
/// Wire type string: `ORDERS:D:09B:UN:{release}`.
///
/// # Type-state
///
/// [`build`](OrdersBuilder::build) is only available once both
/// [`sender`](OrdersBuilder::sender) and [`receiver`](OrdersBuilder::receiver)
/// have been called.
///
/// # Example
///
/// ```rust,no_run
/// use edi_energy::Release;
/// use edi_energy::builders::OrdersBuilder;
///
/// let msg = OrdersBuilder::new(Release::new("1.4b"))
/// .sender("4012345000023")
/// .receiver("9900357000004")
/// .build()?;
///
/// assert_eq!(msg.sender().unwrap().party_id.as_deref(), Some("4012345000023"));
/// # Ok::<(), edi_energy::Error>(())
/// ```
#[derive(Debug, Clone)]
#[must_use = "Builder must be consumed via .build() or .serialize()"]
pub struct OrdersBuilder<S = Unset, R = Unset> {
_ph: PhantomData<fn() -> (S, R)>,
inner: OrdersBuilderInner,
}
impl OrdersBuilder<Unset, Unset> {
/// Create a builder targeting the given EDI@Energy release.
pub fn new(release: Release) -> Self {
Self {
_ph: PhantomData,
inner: OrdersBuilderInner {
release,
sender_id: None,
receiver_id: None,
sender_agency: AgencyCode::Bdew,
receiver_agency: AgencyCode::Bdew,
message_ref: "1".to_owned(),
document_code: None,
document_id: None,
pruefidentifikator: None,
document_date: None,
location: None,
references: Vec::new(),
ausfuehrungsdatum: None,
abonnement: None,
item_description: None,
},
}
}
}
impl<S, R> OrdersBuilder<S, R> {
fn transition<S2, R2>(self) -> OrdersBuilder<S2, R2> {
OrdersBuilder {
_ph: PhantomData,
inner: self.inner,
}
}
/// Set the message sender's market-participant identifier.
pub fn sender(mut self, id: impl Into<String>) -> OrdersBuilder<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>) -> OrdersBuilder<S, Set> {
self.inner.receiver_id = Some(id.into());
self.transition()
}
/// Override the agency code for the sender's party identifier.
///
/// Default: [`AgencyCode::Bdew`] (`"293"`). Use [`AgencyCode::Etso`] (`"305"`)
/// for TSO/ÜNB parties that carry a 16-char EIC code.
pub fn sender_agency(mut self, agency: crate::AgencyCode) -> Self {
self.inner.sender_agency = agency;
self
}
/// Override the agency code for the receiver's party identifier.
///
/// Default: [`AgencyCode::Bdew`] (`"293"`).
pub fn receiver_agency(mut self, agency: crate::AgencyCode) -> Self {
self.inner.receiver_agency = agency;
self
}
/// Override the BGM document type code (DE 1001).
pub fn document_code(mut self, code: impl Into<String>) -> Self {
self.inner.document_code = Some(code.into());
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
}
/// Set the Prüfidentifikator, emitted as `SG1 RFF+Z13:<pid>`.
///
/// The AHBs give BGM DE 1004 as the **Dokumentennummer** and put the
/// Prüfidentifikator in its own reference group, so this never touches BGM.
pub fn pruefidentifikator(mut self, pid: u32) -> Self {
self.inner.pruefidentifikator = Some(pid);
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 location (MaLo-ID / ZPB / NeLo-ID) the order addresses.
///
/// Emits `LOC+172+<id>`. An ESA Bestellung/Abbestellung (`WiM` Teil 2 UC 4.1
/// Nr. 3 / UC 4.3 Nr. 1) names the location whose values are (un)ordered.
pub fn location(mut self, id: impl Into<String>) -> Self {
self.inner.location = Some(id.into());
self
}
/// Add an SG1 reference `RFF+<qual>:<value>`.
///
/// Additive. The ESA order PIDs need two (`AAG`/`ACW` plus `Z13`).
pub fn reference(mut self, qualifier: impl Into<String>, value: impl Into<String>) -> Self {
self.inner.references.push((qualifier.into(), value.into()));
self
}
/// Set `DTM+203` Ausführungsdatum (`CCYYMMDDHHMM`).
///
/// **Muss** on ORDERS 17007/17008 (ORDERS AHB 1.1b §4.15): on a Bestellung
/// it is when the delivery is to start, on an Abbestellung when it stops.
pub fn ausfuehrungsdatum(mut self, ccyymmddhhmm: impl Into<String>) -> Self {
self.inner.ausfuehrungsdatum = Some(ccyymmddhhmm.into());
self
}
/// Set the `IMD+7081` Abonnement code — `Z01` (Start Abo), `Z02` (Ende
/// Abo) or `Z03` (ohne Abo).
///
/// **Muss** on ORDERS 17007/17008. It is also what decides which EBD the
/// MSB's ORDRSP must cite (`E_0256` for Z01/Z03, `E_0254` for Z02).
pub fn abonnement(mut self, code: impl Into<String>) -> Self {
self.inner.abonnement = Some(code.into());
self
}
/// Set a free-form item description — emits `IMD+A`.
///
/// Distinct from [`abonnement`](Self::abonnement), which emits the coded
/// `IMD++<7081>` the ESA order PIDs require.
pub fn item_description(mut self, text: impl Into<String>) -> Self {
self.inner.item_description = Some(text.into());
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);
let code = self.inner.document_code.as_deref().unwrap_or("");
// BGM DE 1004 is the **Dokumentennummer**, never the Prüfidentifikator
// (ORDERS AHB 1.1b).
let doc_id = self
.inner
.document_id
.as_deref()
.unwrap_or(&self.inner.message_ref);
emit_comp!(
w,
"UNH",
[&self.inner.message_ref],
["ORDERS", "D", "09B", "UN", self.inner.release.as_str()]
);
emit_seg!(w, "BGM", code, doc_id);
// `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"]);
// `DTM+203` Ausführungsdatum, format 303 (CCYYMMDDHHMMZZZ).
if let Some(ausf) = &self.inner.ausfuehrungsdatum {
emit_comp!(w, "DTM", ["203", &super::ccyymmddhhmm_utc(ausf), "303"]);
}
// Abonnement — `IMD++<7081>`. DE 7081 sits in C272 (element 2), which
// is why DE 7077 (element 1) stays empty.
if let Some(code) = &self.inner.abonnement {
emit_comp!(w, "IMD", [""], [code]);
}
// Free-form item description (7077 = A), for the non-ESA order PIDs.
if self.inner.abonnement.is_none() && self.inner.item_description.is_some() {
emit_comp!(w, "IMD", ["A"]);
}
// ── SG1: references (RFF+AAG / RFF+ACW / RFF+Z13) ────────────────────
if let Some(pid) = self.inner.pruefidentifikator {
emit_comp!(w, "RFF", ["Z13", &pid.to_string()]);
}
for (q, v) in &self.inner.references {
emit_comp!(w, "RFF", [q, v]);
}
// ── SG2: parties + location ──────────────────────────────────────────
if let Some(id) = &self.inner.sender_id {
emit_comp!(
w,
"NAD",
["MS"],
[id, "", self.inner.sender_agency.as_str()]
);
}
if let Some(id) = &self.inner.receiver_id {
emit_comp!(
w,
"NAD",
["MR"],
[id, "", self.inner.receiver_agency.as_str()]
);
}
if let Some(loc) = &self.inner.location {
emit_seg!(w, "LOC", "172", loc);
}
// Section control — ORDERS requires UNS between header and summary.
emit_seg!(w, "UNS", "D");
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 OrdersBuilder<Set, Set> {
/// Build and return a fully-parsed [`crate::messages::orders::OrdersMessage`].
///
/// # Errors
///
/// Returns an [`Error`] if EDIFACT serialization or parsing fails.
pub fn build(self) -> Result<crate::messages::orders::OrdersMessage, 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::orders::OrdersMessage::from_parts(
segments,
message_ref.as_str(),
assoc_code.as_str(),
None,
))
}
}