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 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 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 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 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).