fiscal-core 0.7.2

Core types, tax calculations, and XML builder for Brazilian fiscal documents
Documentation
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Attach cancellation event response to an authorized `<nfeProc>` XML.

use crate::FiscalError;
use crate::xml_utils::extract_xml_tag_value;

use super::helpers::{dom_reserialize_nfe_proc, extract_all_tags, extract_tag};

/// Cancellation event type code (`110111`).
const EVT_CANCELA: &str = "110111";
/// Cancellation by substitution event type code (`110112`).
const EVT_CANCELA_SUBSTITUICAO: &str = "110112";

/// Valid status codes for cancellation event matching.
///
/// - `135` — Event registered and linked
/// - `136` — Event registered but not linked
/// - `155` — Already cancelled (late)
const VALID_CANCEL_STATUSES: &[&str] = &["135", "136", "155"];

/// Attach a cancellation event response to an authorized `<nfeProc>` XML,
/// marking the NF-e as locally cancelled.
///
/// This mirrors the PHP `Complements::cancelRegister()` method. The function
/// searches the `cancel_event_xml` for `<retEvento>` elements whose:
/// - `cStat` is in `[135, 136, 155]` (valid cancellation statuses)
/// - `tpEvento` is `110111` (cancellation) or `110112` (cancellation by substitution)
/// - `chNFe` matches the access key in the authorized NF-e's `<protNFe>`
///
/// When a matching `<retEvento>` is found, it is appended inside the
/// `<nfeProc>` element (before the closing `</nfeProc>` tag).
///
/// If no matching cancellation event is found, the original NF-e XML is
/// returned unchanged (same behavior as the PHP implementation).
///
/// # Arguments
///
/// * `nfe_proc_xml` - The authorized NF-e XML containing `<nfeProc>` with `<protNFe>`.
/// * `cancel_event_xml` - The SEFAZ cancellation event response XML containing `<retEvento>`.
///
/// # Errors
///
/// Returns [`FiscalError::XmlParsing`] if:
/// - The `nfe_proc_xml` does not contain `<protNFe>` (not an authorized NF-e)
/// - The `<protNFe>` does not contain `<chNFe>`
pub fn attach_cancellation(
    nfe_proc_xml: &str,
    cancel_event_xml: &str,
) -> Result<String, FiscalError> {
    // Validate the NF-e has a protNFe with a chNFe
    let prot_nfe = extract_tag(nfe_proc_xml, "protNFe").ok_or_else(|| {
        FiscalError::XmlParsing(
            "Could not find <protNFe> in NF-e XML — is this an authorized NF-e?".into(),
        )
    })?;

    let ch_nfe = extract_xml_tag_value(&prot_nfe, "chNFe")
        .ok_or_else(|| FiscalError::XmlParsing("Could not find <chNFe> inside <protNFe>".into()))?;

    // Search for matching retEvento in the cancellation XML
    let ret_eventos = extract_all_tags(cancel_event_xml, "retEvento");

    let mut matched_ret_evento: Option<&str> = None;

    for ret_evento in &ret_eventos {
        let c_stat = match extract_xml_tag_value(ret_evento, "cStat") {
            Some(v) => v,
            None => continue,
        };
        let tp_evento = match extract_xml_tag_value(ret_evento, "tpEvento") {
            Some(v) => v,
            None => continue,
        };
        let ch_nfe_evento = match extract_xml_tag_value(ret_evento, "chNFe") {
            Some(v) => v,
            None => continue,
        };

        if VALID_CANCEL_STATUSES.contains(&c_stat.as_str())
            && (tp_evento == EVT_CANCELA || tp_evento == EVT_CANCELA_SUBSTITUICAO)
            && ch_nfe_evento == ch_nfe
        {
            matched_ret_evento = Some(ret_evento.as_str());
            break;
        }
    }

    // Re-serialize via DOM-like logic to match PHP DOMDocument::saveXML().
    // PHP always re-serializes even when no match is found — reordering
    // xmlns before versao and emitting an XML declaration + newline.
    dom_reserialize_nfe_proc(nfe_proc_xml, matched_ret_evento)
}

#[cfg(test)]
mod tests {
    use super::*;

    // ── attach_cancellation tests ─────────────────────────────────────

    #[test]
    fn attach_cancellation_appends_matching_ret_evento() {
        let nfe_proc = concat!(
            r#"<?xml version="1.0" encoding="UTF-8"?>"#,
            r#"<nfeProc versao="4.00" xmlns="http://www.portalfiscal.inf.br/nfe">"#,
            r#"<NFe><infNFe versao="4.00" Id="NFe35260112345678000199550010000000011123456780">"#,
            r#"<ide/></infNFe></NFe>"#,
            r#"<protNFe versao="4.00"><infProt>"#,
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<cStat>100</cStat><nProt>135220000009921</nProt>"#,
            r#"</infProt></protNFe>"#,
            r#"</nfeProc>"#
        );

        let cancel_xml = concat!(
            r#"<retEnvEvento><retEvento versao="1.00"><infEvento>"#,
            r#"<cStat>135</cStat>"#,
            r#"<xMotivo>Evento registrado e vinculado a NF-e</xMotivo>"#,
            r#"<tpEvento>110111</tpEvento>"#,
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<nProt>135220000009999</nProt>"#,
            r#"</infEvento></retEvento></retEnvEvento>"#
        );

        let result = attach_cancellation(nfe_proc, cancel_xml).unwrap();

        // Must contain the retEvento inside nfeProc
        assert!(
            result.contains("<retEvento"),
            "Result should contain <retEvento>"
        );
        assert!(
            result.contains("<tpEvento>110111</tpEvento>"),
            "Result should contain cancellation event type"
        );
        // The retEvento should appear before </nfeProc>
        let ret_pos = result.find("<retEvento").unwrap();
        let close_pos = result.rfind("</nfeProc>").unwrap();
        assert!(ret_pos < close_pos, "retEvento should be before </nfeProc>");
        // Original content should be preserved
        assert!(result.contains("<protNFe"));
        assert!(result.contains("<NFe>"));
    }

    #[test]
    fn attach_cancellation_ignores_non_matching_ch_nfe() {
        let nfe_proc = concat!(
            r#"<nfeProc versao="4.00" xmlns="http://www.portalfiscal.inf.br/nfe">"#,
            r#"<NFe/>"#,
            r#"<protNFe versao="4.00"><infProt>"#,
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<cStat>100</cStat>"#,
            r#"</infProt></protNFe>"#,
            r#"</nfeProc>"#
        );

        let cancel_xml = concat!(
            r#"<retEvento versao="1.00"><infEvento>"#,
            r#"<cStat>135</cStat>"#,
            r#"<tpEvento>110111</tpEvento>"#,
            r#"<chNFe>99999999999999999999999999999999999999999999</chNFe>"#,
            r#"<nProt>135220000009999</nProt>"#,
            r#"</infEvento></retEvento>"#
        );

        let result = attach_cancellation(nfe_proc, cancel_xml).unwrap();
        // No matching chNFe — should NOT contain retEvento, but still
        // re-serialized like PHP (declaration + xmlns before versao)
        assert!(
            !result.contains("<retEvento"),
            "Should not contain retEvento"
        );
        assert!(
            result.starts_with("<?xml version=\"1.0\"?>\n"),
            "Should have XML declaration (no encoding since input had none)"
        );
        assert!(
            result
                .contains(r#"<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00">"#),
            "Should reorder xmlns before versao"
        );
    }

    #[test]
    fn attach_cancellation_ignores_wrong_tp_evento() {
        let nfe_proc = concat!(
            r#"<nfeProc versao="4.00" xmlns="http://www.portalfiscal.inf.br/nfe">"#,
            r#"<NFe/>"#,
            r#"<protNFe versao="4.00"><infProt>"#,
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<cStat>100</cStat>"#,
            r#"</infProt></protNFe>"#,
            r#"</nfeProc>"#
        );

        let cancel_xml = concat!(
            r#"<retEvento versao="1.00"><infEvento>"#,
            r#"<cStat>135</cStat>"#,
            r#"<tpEvento>110110</tpEvento>"#, // CCe, not cancellation
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<nProt>135220000009999</nProt>"#,
            r#"</infEvento></retEvento>"#
        );

        let result = attach_cancellation(nfe_proc, cancel_xml).unwrap();
        // Wrong tpEvento — should NOT contain retEvento
        assert!(
            !result.contains("<retEvento"),
            "Should not contain retEvento"
        );
        assert!(
            result.starts_with("<?xml version=\"1.0\"?>\n"),
            "Should have XML declaration"
        );
    }

    #[test]
    fn attach_cancellation_ignores_rejected_status() {
        let nfe_proc = concat!(
            r#"<nfeProc versao="4.00" xmlns="http://www.portalfiscal.inf.br/nfe">"#,
            r#"<NFe/>"#,
            r#"<protNFe versao="4.00"><infProt>"#,
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<cStat>100</cStat>"#,
            r#"</infProt></protNFe>"#,
            r#"</nfeProc>"#
        );

        let cancel_xml = concat!(
            r#"<retEvento versao="1.00"><infEvento>"#,
            r#"<cStat>573</cStat>"#, // Rejected status
            r#"<tpEvento>110111</tpEvento>"#,
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<nProt>135220000009999</nProt>"#,
            r#"</infEvento></retEvento>"#
        );

        let result = attach_cancellation(nfe_proc, cancel_xml).unwrap();
        // Rejected status — should NOT contain retEvento
        assert!(
            !result.contains("<retEvento"),
            "Should not contain retEvento"
        );
        assert!(
            result.starts_with("<?xml version=\"1.0\"?>\n"),
            "Should have XML declaration"
        );
    }

    #[test]
    fn attach_cancellation_accepts_status_155() {
        let nfe_proc = concat!(
            r#"<nfeProc versao="4.00" xmlns="http://www.portalfiscal.inf.br/nfe">"#,
            r#"<NFe/>"#,
            r#"<protNFe versao="4.00"><infProt>"#,
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<cStat>100</cStat>"#,
            r#"</infProt></protNFe>"#,
            r#"</nfeProc>"#
        );

        let cancel_xml = concat!(
            r#"<retEvento versao="1.00"><infEvento>"#,
            r#"<cStat>155</cStat>"#,
            r#"<tpEvento>110111</tpEvento>"#,
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<nProt>135220000009999</nProt>"#,
            r#"</infEvento></retEvento>"#
        );

        let result = attach_cancellation(nfe_proc, cancel_xml).unwrap();
        assert!(result.contains("<retEvento"));
    }

    #[test]
    fn attach_cancellation_accepts_substituicao_110112() {
        let nfe_proc = concat!(
            r#"<nfeProc versao="4.00" xmlns="http://www.portalfiscal.inf.br/nfe">"#,
            r#"<NFe/>"#,
            r#"<protNFe versao="4.00"><infProt>"#,
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<cStat>100</cStat>"#,
            r#"</infProt></protNFe>"#,
            r#"</nfeProc>"#
        );

        let cancel_xml = concat!(
            r#"<retEvento versao="1.00"><infEvento>"#,
            r#"<cStat>135</cStat>"#,
            r#"<tpEvento>110112</tpEvento>"#,
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<nProt>135220000009999</nProt>"#,
            r#"</infEvento></retEvento>"#
        );

        let result = attach_cancellation(nfe_proc, cancel_xml).unwrap();
        assert!(
            result.contains("<tpEvento>110112</tpEvento>"),
            "Should accept cancellation by substitution"
        );
    }

    #[test]
    fn attach_cancellation_rejects_missing_prot_nfe() {
        let nfe_xml = "<NFe><infNFe/></NFe>";
        let cancel_xml = "<retEvento/>";
        let err = attach_cancellation(nfe_xml, cancel_xml).unwrap_err();
        assert!(matches!(err, FiscalError::XmlParsing(_)));
    }

    #[test]
    fn attach_cancellation_rejects_missing_ch_nfe_in_prot() {
        let nfe_proc = concat!(
            r#"<nfeProc><protNFe versao="4.00"><infProt>"#,
            r#"<cStat>100</cStat>"#,
            r#"</infProt></protNFe></nfeProc>"#
        );
        let cancel_xml = "<retEvento/>";
        let err = attach_cancellation(nfe_proc, cancel_xml).unwrap_err();
        assert!(matches!(err, FiscalError::XmlParsing(_)));
    }

    /// Byte-for-byte parity test: Rust output must match what PHP
    /// `Complements::cancelRegister()` produces for the same inputs.
    ///
    /// PHP uses `DOMDocument::saveXML()` which:
    /// 1. Emits `<?xml version="1.0" encoding="UTF-8"?>` + `\n`
    /// 2. Reorders `xmlns` before `versao` on `<nfeProc>`
    /// 3. Appends `<retEvento>` as last child of `<nfeProc>`
    #[test]
    fn attach_cancellation_parity_with_php() {
        // Input: authorized nfeProc (as produced by join_xml / PHP join())
        let nfe_proc = concat!(
            r#"<?xml version="1.0" encoding="UTF-8"?>"#,
            r#"<nfeProc versao="4.00" xmlns="http://www.portalfiscal.inf.br/nfe">"#,
            r#"<NFe><infNFe versao="4.00" Id="NFe35260112345678000199550010000000011123456780">"#,
            r#"<ide/></infNFe></NFe>"#,
            r#"<protNFe versao="4.00"><infProt>"#,
            r#"<digVal>abc</digVal>"#,
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<cStat>100</cStat>"#,
            r#"<xMotivo>Autorizado</xMotivo>"#,
            r#"<nProt>135220000009921</nProt>"#,
            r#"</infProt></protNFe>"#,
            r#"</nfeProc>"#
        );

        let cancel_xml = concat!(
            r#"<retEnvEvento><retEvento versao="1.00"><infEvento>"#,
            r#"<cStat>135</cStat>"#,
            r#"<xMotivo>Evento registrado e vinculado a NF-e</xMotivo>"#,
            r#"<tpEvento>110111</tpEvento>"#,
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<nProt>135220000009999</nProt>"#,
            r#"</infEvento></retEvento></retEnvEvento>"#
        );

        let result = attach_cancellation(nfe_proc, cancel_xml).unwrap();

        // Expected output from PHP DOMDocument::saveXML():
        // - declaration with encoding="UTF-8" followed by \n
        // - <nfeProc xmlns="..." versao="..."> (xmlns first)
        // - inner content unchanged
        // - <retEvento> appended before </nfeProc>
        let expected = concat!(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
            r#"<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00">"#,
            r#"<NFe><infNFe versao="4.00" Id="NFe35260112345678000199550010000000011123456780">"#,
            r#"<ide/></infNFe></NFe>"#,
            r#"<protNFe versao="4.00"><infProt>"#,
            r#"<digVal>abc</digVal>"#,
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<cStat>100</cStat>"#,
            r#"<xMotivo>Autorizado</xMotivo>"#,
            r#"<nProt>135220000009921</nProt>"#,
            r#"</infProt></protNFe>"#,
            r#"<retEvento versao="1.00"><infEvento>"#,
            r#"<cStat>135</cStat>"#,
            r#"<xMotivo>Evento registrado e vinculado a NF-e</xMotivo>"#,
            r#"<tpEvento>110111</tpEvento>"#,
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<nProt>135220000009999</nProt>"#,
            r#"</infEvento></retEvento>"#,
            "</nfeProc>\n"
        );

        assert_eq!(result, expected);
    }

    /// Parity test for no-match case: PHP still re-serializes through saveXML().
    #[test]
    fn attach_cancellation_no_match_still_reserializes_like_php() {
        let nfe_proc = concat!(
            r#"<?xml version="1.0" encoding="UTF-8"?>"#,
            r#"<nfeProc versao="4.00" xmlns="http://www.portalfiscal.inf.br/nfe">"#,
            r#"<NFe/>"#,
            r#"<protNFe versao="4.00"><infProt>"#,
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<cStat>100</cStat>"#,
            r#"</infProt></protNFe>"#,
            r#"</nfeProc>"#
        );

        let cancel_xml = concat!(
            r#"<retEnvEvento><retEvento versao="1.00"><infEvento>"#,
            r#"<cStat>135</cStat>"#,
            r#"<tpEvento>110111</tpEvento>"#,
            r#"<chNFe>99999999999999999999999999999999999999999999</chNFe>"#,
            r#"<nProt>135220000009999</nProt>"#,
            r#"</infEvento></retEvento></retEnvEvento>"#
        );

        let result = attach_cancellation(nfe_proc, cancel_xml).unwrap();

        // PHP DOMDocument::saveXML() output (no retEvento appended):
        let expected = concat!(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
            r#"<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00">"#,
            r#"<NFe/>"#,
            r#"<protNFe versao="4.00"><infProt>"#,
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<cStat>100</cStat>"#,
            r#"</infProt></protNFe>"#,
            "</nfeProc>\n"
        );

        assert_eq!(result, expected);
    }

    #[test]
    fn attach_cancellation_picks_first_matching_from_multiple_ret_eventos() {
        let nfe_proc = concat!(
            r#"<nfeProc versao="4.00" xmlns="http://www.portalfiscal.inf.br/nfe">"#,
            r#"<NFe/>"#,
            r#"<protNFe versao="4.00"><infProt>"#,
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<cStat>100</cStat>"#,
            r#"</infProt></protNFe>"#,
            r#"</nfeProc>"#
        );

        let cancel_xml = concat!(
            r#"<retEnvEvento>"#,
            // First: wrong chNFe
            r#"<retEvento versao="1.00"><infEvento>"#,
            r#"<cStat>135</cStat><tpEvento>110111</tpEvento>"#,
            r#"<chNFe>99999999999999999999999999999999999999999999</chNFe>"#,
            r#"<nProt>111111111111111</nProt>"#,
            r#"</infEvento></retEvento>"#,
            // Second: correct match
            r#"<retEvento versao="1.00"><infEvento>"#,
            r#"<cStat>135</cStat><tpEvento>110111</tpEvento>"#,
            r#"<chNFe>35260112345678000199550010000000011123456780</chNFe>"#,
            r#"<nProt>222222222222222</nProt>"#,
            r#"</infEvento></retEvento>"#,
            r#"</retEnvEvento>"#
        );

        let result = attach_cancellation(nfe_proc, cancel_xml).unwrap();
        assert!(result.contains("<nProt>222222222222222</nProt>"));
        // Should only have one retEvento (the matching one)
        assert_eq!(result.matches("<retEvento").count(), 1);
    }
}