facturx_laravel_rust/lib.rs
1//! French e-invoicing (facture électronique) for Laravel Rust: Factur-X invoices, credit notes and
2//! received-invoice checks.
3//!
4//! Issue Factur-X (ZUGFeRD-compatible) PDF/A-3 invoices that carry the EN 16931 XML
5//! (profile EN 16931 only). French rules are checked before anything is issued: SIREN/SIRET, VAT
6//! number, mandatory mentions, VAT franchise (art. 293 B), reverse charge. Also credit notes (avoirs),
7//! deposit invoices (acomptes), discounts, gap-free numbering in your database, archive and mail of
8//! the PDF, and inspection of invoices you receive. It talks to your plateforme agréée through the
9//! AFNOR XP Z12-013 API ([`platform`]), and writes e-reporting files ([`ereporting`]) and
10//! life-cycle statuses ([`status`]).
11//!
12//! The pages of the package are in its `docs/` folder, which `laravel facturx:docs` prints (index:
13//! `docs/llms.txt`); `laravel facturx:skill` installs a skill for coding agents. Every task below is
14//! a compiled program in `examples/`.
15//!
16//! # Issue an invoice
17//!
18//! Register `FacturXServiceProvider` in `src/app.rs`, describe the seller in `.env` (see
19//! [Configuration](#configuration)), then render the invoice from a handler:
20//!
21#![cfg_attr(feature = "pdf", doc = "```")]
22#![cfg_attr(not(feature = "pdf"), doc = "```ignore")]
23//! use laravel::container::Inject;
24//! use facturx_laravel_rust::{Address, Customer, Date, FacturX, Invoice, InvoicePdf, Line, Nature};
25//! use rust_decimal::dec;
26//!
27//! pub async fn show(facturx: Inject<FacturX>) -> laravel::Result<InvoicePdf> {
28//! let customer = Customer::business(
29//! "Globex SAS",
30//! "222222226",
31//! Address::new("5 avenue des Tilleuls", "75011", "Paris"),
32//! );
33//! let invoice = Invoice::new("F-2026-00042", Date::new(2026, 12, 2)?, customer)
34//! .nature(Nature::Services) // mandatory: Goods, Services or Mixed
35//! .line(Line::new("Accompagnement, décembre", dec!(1), dec!(1500), dec!(20)));
36//!
37//! facturx.pdf(&invoice).await
38//! }
39//! ```
40//!
41//! # Credit note (avoir)
42//!
43//! [`Invoice::credit_note`] names the invoice it corrects and is owed to the customer.
44//!
45//! # Deposit invoice (acompte) and final invoice
46//!
47//! [`Invoice::deposit`] issues the acompte; [`Invoice::deducts_deposit`] takes it back on the final
48//! invoice as a line of quantity −1, before VAT and per rate, so the final invoice states only the
49//! VAT still owed.
50//!
51//! # Discounts
52//!
53//! [`Discount::percent`] and [`Discount::amount`], on a [`Line`] or on the whole invoice.
54//!
55//! # VAT cases
56//!
57//! The franchise (`FACTURX_VAT_FRANCHISE`), VAT on debits (`FACTURX_VAT_ON_DEBITS`), and
58//! [`Exemption::reverse_charge`], [`Exemption::eu_reverse_charge`],
59//! [`Exemption::intra_community`] and [`Exemption::export`] for lines at 0 %.
60//!
61//! # Subscriptions
62//!
63//! [`Invoice::period`] states the period an invoice covers (BG-14) and [`Invoice::contract`] the
64//! contract (BT-12). [`Invoice::payment`] takes [`Payment::direct_debit`], a SEPA direct debit under
65//! the customer's mandate (with `FACTURX_SEPA_CREDITOR_ID`), or [`Payment::card_ending`], a bank
66//! card; a SEPA transfer to `FACTURX_IBAN` otherwise.
67//!
68//! # Marketplaces and billing mandates
69//!
70//! [`Payment::intermediary`] names the marketplace that collected the payment as payee (BG-10);
71//! [`Invoice::issued_by`] states the third party that issues under a billing mandate;
72//! [`Invoice::self_billed`] makes a self-billed invoice (types 389, 261, 500, « Autofacturation »).
73//!
74//! # Plateforme agréée, e-reporting, statuses
75//!
76//! [`platform::Platform`] sends invoices, statuses and reports, follows their acknowledgement,
77//! collects what you receive and reads the e-invoicing directory (`FACTURX_PLATFORM_*`).
78//! [`ereporting::TransactionsReport`] and [`ereporting::PaymentsReport`] write flux 10: B2C sales
79//! per day, international invoices and their payments. [`status::Status`] writes the CDAR
80//! statuses: 212 Encaissée, 210 Refusée and the optional ones.
81//!
82//! # Number invoices without gaps
83//!
84//! `FacturX::next_number`, with the `database` feature: `F-2026-00042`, in the transaction that saves
85//! the invoice.
86//!
87//! # Archive and mail
88//!
89//! [`FacturX::archive`] files the PDF on a disk and [`FacturX::mail`] sends it (feature `pdf`).
90//!
91//! # Validate a received invoice
92//!
93//! [`FacturX::inspect`] reads a Factur-X or `ZUGFeRD` PDF, CII or UBL and checks it against the
94//! EN 16931 rules of [`en16931`] and, for a French seller or buyer, the French rules of [`french`]
95//! (codes `FR-*`: identifiers, VAT numbers and rates, number format, decimals, dates, currency,
96//! directory addresses, due date, franchise). Those are this crate's own checks, not the full
97//! "BR-FR" schematron of the French flow (FNFE-MPE, XP Z12-012).
98//!
99//! With the feature `official-rules`, `OfficialRules::inspect` runs that schematron too, in its
100//! version 1.4.0.04 (pure Rust, no Java), when a received invoice must meet every rule of the
101//! French flow; `laravel facturx:rules` downloads it once from its publisher.
102//!
103//! # Configuration
104//!
105//! The seller and the defaults come from `.env`. Required to issue an invoice:
106//! `FACTURX_SELLER_NAME`, `FACTURX_SELLER_SIREN`, `FACTURX_SELLER_STREET`,
107//! `FACTURX_SELLER_POST_CODE`, `FACTURX_SELLER_CITY`, and `FACTURX_SELLER_VAT_NUMBER` on an
108//! invoice that charges VAT; [`Error::SellerNotConfigured`] names what is missing.
109//!
110//! Optional: `FACTURX_SELLER_SIRET`, `FACTURX_SELLER_LEGAL_FORM`, `FACTURX_SELLER_REGISTRATION`,
111//! `FACTURX_SELLER_ELECTRONIC_ADDRESS`, `FACTURX_SELLER_EMAIL`, `FACTURX_SELLER_PHONE`,
112//! `FACTURX_SELLER_COUNTRY`, `FACTURX_IBAN`, `FACTURX_BIC`, `FACTURX_SEPA_CREDITOR_ID`,
113//! `FACTURX_PAYMENT_DAYS`,
114//! `FACTURX_VAT_FRANCHISE`, `FACTURX_VAT_FRANCHISE_MENTION`, `FACTURX_VAT_ON_DEBITS`,
115//! `FACTURX_LATE_PENALTIES`, `FACTURX_EARLY_PAYMENT_DISCOUNT`, `FACTURX_TEMPLATE`,
116//! `FACTURX_PREFIX`, `FACTURX_CREDIT_NOTE_PREFIX`, `FACTURX_DEPOSIT_PREFIX`, `FACTURX_DISK` and
117//! `FACTURX_DIRECTORY`; for the platform, `FACTURX_PLATFORM_FLOW_URL`,
118//! `FACTURX_PLATFORM_DIRECTORY_URL`, `FACTURX_PLATFORM_TOKEN` (or `FACTURX_PLATFORM_TOKEN_URL`,
119//! `FACTURX_PLATFORM_CLIENT_ID`, `FACTURX_PLATFORM_CLIENT_SECRET`),
120//! `FACTURX_PLATFORM_ORGANIZATION_ID` and `FACTURX_PLATFORM_ID`. Their defaults and meaning are in `docs/setup.md` (`laravel facturx:docs`
121//! prints where the pages are).
122//!
123//! # Test with `FacturX::fake()`
124//!
125//! [`FacturX::fake`] checks for real, records instead of rendering, and numbers in memory. Fake the
126//! disk and the mailer with `laravel::storage::fake("local")` and `laravel::mail::fake()`.
127//!
128//! # What it does not do
129//!
130//! Produce the MINIMUM, BASIC and EXTENDED profiles, or talk to a platform that has only its own
131//! API.
132//!
133//! The invoice model and the rules are [`en16931`]'s and [`en16931_formats`]'s, re-exported for
134//! anything this crate does not wrap.
135
136mod commands;
137mod config;
138mod date;
139mod decimal;
140pub mod ereporting;
141mod error;
142mod format;
143pub mod french;
144pub mod identifiers;
145mod mapping;
146mod mentions;
147mod model;
148#[cfg(feature = "database")]
149mod numbering;
150pub mod platform;
151mod provider;
152mod service;
153mod skill;
154pub mod status;
155mod template;
156pub mod view;
157
158pub use config::FacturXConfig;
159pub use date::today;
160pub use error::Error;
161pub use model::{
162 Address, CodedNote, Corrects, Customer, DEFAULT_EARLY_PAYMENT_DISCOUNT, DEFAULT_LATE_PENALTIES,
163 DEFAULT_TEMPLATE, DEFAULT_VAT_FRANCHISE_MENTION, Delivery, DepositPaid, Discount, Exemption,
164 Invoice, Kind, Line, Nature, Payment, Period, Prefixes, Seller, Settings,
165};
166pub use provider::FacturXServiceProvider;
167#[cfg(feature = "pdf")]
168pub use service::InvoicePdf;
169#[cfg(feature = "official-rules")]
170pub use service::OfficialRules;
171pub use service::{FacturX, Inspection, Official, OfficialFinding, OfficialReport, Prepared};
172
173pub use en16931;
174pub use en16931::Date;
175pub use en16931_formats;
176pub use rust_decimal::Decimal;