pub struct InvoiceBuilder<State = Draft> { /* private fields */ }Expand description
Typestate builder for NF-e / NFC-e XML documents.
In the Draft state all setters are available.
Calling build() validates the data and
transitions to Built, which exposes xml()
and access_key().
Calling sign_with() on Built transitions
to Signed, which exposes signed_xml().
Implementations§
Source§impl InvoiceBuilder<Draft>
impl InvoiceBuilder<Draft>
Sourcepub fn new(
issuer: IssuerData,
environment: SefazEnvironment,
model: InvoiceModel,
) -> Self
pub fn new( issuer: IssuerData, environment: SefazEnvironment, model: InvoiceModel, ) -> Self
Create a new builder in the Draft state.
The three arguments are required; everything else has sensible defaults or is optional.
Sourcepub fn invoice_number(self, n: u32) -> Self
pub fn invoice_number(self, n: u32) -> Self
Set the invoice number (default: 1).
Sourcepub fn emission_type(self, et: EmissionType) -> Self
pub fn emission_type(self, et: EmissionType) -> Self
Set the emission type (default: EmissionType::Normal).
Sourcepub fn schema_version(self, sv: SchemaVersion) -> Self
pub fn schema_version(self, sv: SchemaVersion) -> Self
Set the schema version (default: SchemaVersion::PL009).
When PL009, PL_010-exclusive tags (IBS/CBS, IS,
gCompraGov, gPagAntecipado, agropecuario) are silently omitted
even if data is provided.
When PL010, all reform-related tags are emitted
normally.
Sourcepub fn issued_at(self, dt: DateTime<FixedOffset>) -> Self
pub fn issued_at(self, dt: DateTime<FixedOffset>) -> Self
Set the emission date/time (default: now in UTC-3).
Sourcepub fn operation_nature(self, n: impl Into<String>) -> Self
pub fn operation_nature(self, n: impl Into<String>) -> Self
Set the operation nature (default: "VENDA").
Sourcepub fn add_item(self, item: InvoiceItemData) -> Self
pub fn add_item(self, item: InvoiceItemData) -> Self
Add one item to the invoice.
Sourcepub fn items(self, items: Vec<InvoiceItemData>) -> Self
pub fn items(self, items: Vec<InvoiceItemData>) -> Self
Set all items at once (replaces any previously added items).
Sourcepub fn recipient(self, r: RecipientData) -> Self
pub fn recipient(self, r: RecipientData) -> Self
Set the recipient (optional for NFC-e under R$200).
Sourcepub fn payments(self, p: Vec<PaymentData>) -> Self
pub fn payments(self, p: Vec<PaymentData>) -> Self
Set the payment list.
Sourcepub fn change_amount(self, c: Cents) -> Self
pub fn change_amount(self, c: Cents) -> Self
Set the change amount (vTroco).
Sourcepub fn payment_card_details(self, d: Vec<PaymentCardDetail>) -> Self
pub fn payment_card_details(self, d: Vec<PaymentCardDetail>) -> Self
Set card payment details.
Sourcepub fn contingency(self, c: ContingencyData) -> Self
pub fn contingency(self, c: ContingencyData) -> Self
Set contingency data.
Sourcepub fn exit_at(self, dt: DateTime<FixedOffset>) -> Self
pub fn exit_at(self, dt: DateTime<FixedOffset>) -> Self
Set the exit/departure date/time (dhSaiEnt, model 55 only).
Sourcepub fn operation_type(self, v: u8) -> Self
pub fn operation_type(self, v: u8) -> Self
Override the operation type (tpNF, default: 1).
Sourcepub fn purpose_code(self, v: u8) -> Self
pub fn purpose_code(self, v: u8) -> Self
Override the invoice purpose code (finNFe, default: 1).
Sourcepub fn intermediary_indicator(self, v: impl Into<String>) -> Self
pub fn intermediary_indicator(self, v: impl Into<String>) -> Self
Set the intermediary indicator (indIntermed).
Sourcepub fn emission_process(self, v: impl Into<String>) -> Self
pub fn emission_process(self, v: impl Into<String>) -> Self
Set the emission process (procEmi).
Sourcepub fn consumer_type(self, v: impl Into<String>) -> Self
pub fn consumer_type(self, v: impl Into<String>) -> Self
Set the consumer type (indFinal).
Sourcepub fn buyer_presence(self, v: impl Into<String>) -> Self
pub fn buyer_presence(self, v: impl Into<String>) -> Self
Set the buyer presence indicator (indPres).
Sourcepub fn print_format(self, v: impl Into<String>) -> Self
pub fn print_format(self, v: impl Into<String>) -> Self
Set the DANFE print format (tpImp).
Sourcepub fn destination_indicator(self, v: impl Into<String>) -> Self
pub fn destination_indicator(self, v: impl Into<String>) -> Self
Set the destination indicator (idDest): “1” internal, “2” interstate, “3” export.
Sourcepub fn references(self, refs: Vec<ReferenceDoc>) -> Self
pub fn references(self, refs: Vec<ReferenceDoc>) -> Self
Set referenced documents (NFref).
Sourcepub fn transport(self, t: TransportData) -> Self
pub fn transport(self, t: TransportData) -> Self
Set transport data.
Sourcepub fn billing(self, b: BillingData) -> Self
pub fn billing(self, b: BillingData) -> Self
Set billing data (cobr).
Sourcepub fn withdrawal(self, w: LocationData) -> Self
pub fn withdrawal(self, w: LocationData) -> Self
Set the withdrawal/pickup location (retirada).
Sourcepub fn delivery(self, d: LocationData) -> Self
pub fn delivery(self, d: LocationData) -> Self
Set the delivery location (entrega).
Set authorized XML downloaders (autXML).
Sourcepub fn additional_info(self, a: AdditionalInfo) -> Self
pub fn additional_info(self, a: AdditionalInfo) -> Self
Set additional info (infAdic).
Sourcepub fn intermediary(self, i: IntermediaryData) -> Self
pub fn intermediary(self, i: IntermediaryData) -> Self
Set intermediary data (infIntermed).
Sourcepub fn ret_trib(self, r: RetTribData) -> Self
pub fn ret_trib(self, r: RetTribData) -> Self
Set retained taxes (retTrib).
Sourcepub fn tech_responsible(self, t: TechResponsibleData) -> Self
pub fn tech_responsible(self, t: TechResponsibleData) -> Self
Set tech responsible (infRespTec).
Sourcepub fn purchase(self, p: PurchaseData) -> Self
pub fn purchase(self, p: PurchaseData) -> Self
Set purchase data (compra).
Sourcepub fn export(self, e: ExportData) -> Self
pub fn export(self, e: ExportData) -> Self
Set export data (exporta).
Sourcepub fn issqn_tot(self, t: IssqnTotData) -> Self
pub fn issqn_tot(self, t: IssqnTotData) -> Self
Set ISSQN total data (ISSQNtot).
Sourcepub fn agropecuario(self, a: AgropecuarioData) -> Self
pub fn agropecuario(self, a: AgropecuarioData) -> Self
Set agropecuário data (guia de trânsito or defensivos).
Sourcepub fn compra_gov(self, c: CompraGovData) -> Self
pub fn compra_gov(self, c: CompraGovData) -> Self
Set compra governamental data (gCompraGov, PL_010+).
Sourcepub fn pag_antecipado(self, p: PagAntecipadoData) -> Self
pub fn pag_antecipado(self, p: PagAntecipadoData) -> Self
Set pagamento antecipado data (gPagAntecipado, PL_010+).
Sourcepub fn ibs_cbs_tot(self, t: IbsCbsTotData) -> Self
pub fn ibs_cbs_tot(self, t: IbsCbsTotData) -> Self
Set IBS/CBS total data.
Sourcepub fn only_ascii(self, enabled: bool) -> Self
pub fn only_ascii(self, enabled: bool) -> Self
Enable or disable ASCII-only mode.
When enabled, accented characters (common in Brazilian Portuguese) are replaced by their closest ASCII equivalents in the generated XML. For example, “São Paulo” becomes “Sao Paulo”.
This mirrors the PHP Make::setOnlyAscii() method.
Sourcepub fn calculation_method(self, m: CalculationMethod) -> Self
pub fn calculation_method(self, m: CalculationMethod) -> Self
Sourcepub fn v_nf_tot_override(self, v: Cents) -> Self
pub fn v_nf_tot_override(self, v: Cents) -> Self
Override the vNFTot value (PL_010 only).
When set, this value is used instead of the auto-calculated
vNF + vIBS + vCBS + vIS. Matches the PHP tagTotal(vNFTot) API.
Only emitted when schema is PL010 and
IBSCBSTot is present.
Sourcepub fn build(self) -> Result<InvoiceBuilder<Built>, FiscalError>
pub fn build(self) -> Result<InvoiceBuilder<Built>, FiscalError>
Validate and build the XML, transitioning to Built.
§Errors
Returns FiscalError if:
- The issuer state code is unknown
- Tax data is invalid
Source§impl InvoiceBuilder<Built>
impl InvoiceBuilder<Built>
Sourcepub fn access_key(&self) -> &str
pub fn access_key(&self) -> &str
The 44-digit access key.
Sourcepub fn sign_with<F>(
self,
signer: F,
) -> Result<InvoiceBuilder<Signed>, FiscalError>
pub fn sign_with<F>( self, signer: F, ) -> Result<InvoiceBuilder<Signed>, FiscalError>
Sign the XML using the provided signing function.
The signing function receives the unsigned XML and must return
the signed XML or an error. This keeps fiscal-core independent
of the crypto implementation.
§Examples
// Assuming `builder` is an InvoiceBuilder<Built>:
let signed = builder.sign_with(|xml| {
// In real code, call fiscal_crypto::certificate::sign_xml() here.
Ok(format!("{xml}<Signature/>"))
})?;
assert!(signed.signed_xml().contains("<Signature/>"));§Errors
Returns FiscalError if the signing function returns an error.
Source§impl InvoiceBuilder<Signed>
impl InvoiceBuilder<Signed>
Sourcepub fn signed_xml(&self) -> &str
pub fn signed_xml(&self) -> &str
The signed XML string (includes <Signature> element).
Sourcepub fn access_key(&self) -> &str
pub fn access_key(&self) -> &str
The 44-digit access key.
Sourcepub fn unsigned_xml(&self) -> &str
pub fn unsigned_xml(&self) -> &str
The unsigned XML (before signing).