fiscal_core/xml_builder/
mod.rs1pub 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
41fn 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 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 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(tech));
152 }
153
154 let inf_nfe = tag(
155 "infNFe",
156 &[
157 ("xmlns", NFE_NAMESPACE),
158 ("versao", NFE_VERSION),
159 ("Id", &inf_nfe_id),
160 ],
161 TagContent::Children(inf_children),
162 );
163
164 let xml = format!(
165 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>{}",
166 tag(
167 "NFe",
168 &[("xmlns", NFE_NAMESPACE)],
169 TagContent::Children(vec![inf_nfe])
170 ),
171 );
172
173 Ok(InvoiceXmlResult { xml, access_key })
174}