Skip to main content

fiscal_core/xml_builder/
mod.rs

1//! NF-e/NFC-e XML builder module.
2//!
3//! Provides [`InvoiceBuilder`] — a typestate builder that enforces the
4//! invoice lifecycle at compile time:
5//!
6//! ```text
7//! InvoiceBuilder::new(issuer, env, model)   // Draft
8//!     .series(1)
9//!     .invoice_number(42)
10//!     .add_item(item)
11//!     .recipient(recipient)
12//!     .payments(vec![payment])
13//!     .build()?                              // Built
14//!     .sign_with(|xml| sign(xml))?           // Signed
15//!     .signed_xml()                          // &str
16//! ```
17
18pub mod access_key;
19mod builder;
20pub mod dest;
21pub mod det;
22pub mod emit;
23pub mod ide;
24pub mod optional;
25pub mod pag;
26pub mod tax_id;
27pub mod total;
28pub mod transp;
29
30pub use access_key::build_access_key;
31pub use builder::{Built, Draft, InvoiceBuilder, Signed};
32
33use crate::FiscalError;
34use crate::constants::{NFE_NAMESPACE, NFE_VERSION};
35use crate::newtypes::IbgeCode;
36use crate::state_codes::STATE_IBGE_CODES;
37use crate::tax_icms::{create_icms_totals, merge_icms_totals};
38use crate::types::{AccessKeyParams, InvoiceBuildData, InvoiceXmlResult};
39use crate::xml_utils::{TagContent, tag};
40
41/// Internal XML generation from a fully populated [`InvoiceBuildData`].
42///
43/// Called by [`InvoiceBuilder::build`]; not part of the public API.
44fn generate_xml(data: &InvoiceBuildData) -> Result<InvoiceXmlResult, FiscalError> {
45    let state_ibge = STATE_IBGE_CODES
46        .get(data.issuer.state_code.as_str())
47        .copied()
48        .ok_or_else(|| FiscalError::InvalidStateCode(data.issuer.state_code.clone()))?;
49
50    let numeric_code = access_key::generate_numeric_code();
51    let year_month = access_key::format_year_month(&data.issued_at);
52
53    let ak_params = AccessKeyParams {
54        state_code: IbgeCode(state_ibge.to_string()),
55        year_month,
56        tax_id: data.issuer.tax_id.clone(),
57        model: data.model,
58        series: data.series,
59        number: data.number,
60        emission_type: data.emission_type,
61        numeric_code: numeric_code.clone(),
62    };
63
64    let access_key = build_access_key(&ak_params)?;
65    let inf_nfe_id = format!("NFe{access_key}");
66
67    // Build items and accumulate tax totals
68    let mut icms_totals = create_icms_totals();
69    let mut total_products: i64 = 0;
70    let mut total_ipi: i64 = 0;
71    let mut total_pis: i64 = 0;
72    let mut total_cofins: i64 = 0;
73    let mut total_ii: i64 = 0;
74
75    let mut det_elements = Vec::with_capacity(data.items.len());
76    for item in &data.items {
77        total_products += item.total_price.0;
78        let det_result = det::build_det(item, data)?;
79        merge_icms_totals(&mut icms_totals, &det_result.icms_totals);
80        total_ipi += det_result.v_ipi;
81        total_pis += det_result.v_pis;
82        total_cofins += det_result.v_cofins;
83        total_ii += det_result.v_ii;
84        det_elements.push(det_result.xml);
85    }
86
87    // Assemble infNFe children in schema order
88    let mut inf_children = vec![
89        ide::build_ide(data, state_ibge, &numeric_code, &access_key),
90        emit::build_emit(data),
91    ];
92
93    if let Some(dest_xml) = dest::build_dest(data) {
94        inf_children.push(dest_xml);
95    }
96
97    if let Some(ref w) = data.withdrawal {
98        inf_children.push(optional::build_withdrawal(w));
99    }
100    if let Some(ref d) = data.delivery {
101        inf_children.push(optional::build_delivery(d));
102    }
103    if let Some(ref auths) = data.authorized_xml {
104        for a in auths {
105            inf_children.push(optional::build_aut_xml(a));
106        }
107    }
108
109    inf_children.extend(det_elements);
110
111    inf_children.push(total::build_total(
112        total_products,
113        &icms_totals,
114        &total::OtherTotals {
115            v_ipi: total_ipi,
116            v_pis: total_pis,
117            v_cofins: total_cofins,
118            v_ii: total_ii,
119        },
120        data.ret_trib.as_ref(),
121    ));
122
123    inf_children.push(transp::build_transp(data));
124
125    if let Some(ref billing) = data.billing {
126        inf_children.push(optional::build_cobr(billing));
127    }
128
129    inf_children.push(pag::build_pag(
130        &data.payments,
131        data.change_amount,
132        data.payment_card_details.as_deref(),
133    ));
134
135    if let Some(ref intermed) = data.intermediary {
136        inf_children.push(optional::build_intermediary(intermed));
137    }
138
139    let inf_adic = optional::build_inf_adic(data);
140    if !inf_adic.is_empty() {
141        inf_children.push(inf_adic);
142    }
143
144    if let Some(ref exp) = data.export {
145        inf_children.push(optional::build_export(exp));
146    }
147    if let Some(ref purchase) = data.purchase {
148        inf_children.push(optional::build_purchase(purchase));
149    }
150    if let Some(ref tech) = data.tech_responsible {
151        inf_children.push(optional::build_tech_responsible_with_key(tech, &access_key));
152    }
153
154    // Matches PHP sped-nfe: no xmlns on infNFe (inherited from NFe parent),
155    // Id attribute before versao
156    let inf_nfe = tag(
157        "infNFe",
158        &[("versao", NFE_VERSION), ("Id", &inf_nfe_id)],
159        TagContent::Children(inf_children),
160    );
161
162    let xml = format!(
163        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>{}",
164        tag(
165            "NFe",
166            &[("xmlns", NFE_NAMESPACE)],
167            TagContent::Children(vec![inf_nfe])
168        ),
169    );
170
171    Ok(InvoiceXmlResult { xml, access_key })
172}