=== e-Factura ANAF for WooCommerce ===
Contributors: catcode
Tags: e-factura, anaf, romania, spv, ubl
Requires at least: 6.2
Tested up to: 7.1
Requires PHP: 7.4
WC requires at least: 8.0
WC tested up to: 11.1
Stable tag: 1.0.1
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Romanian e-Factura straight from WooCommerce: UBL invoices under RO_CIUS sent directly to ANAF (SPV) through the official API — no accounting SaaS subscription.

== Description ==

e-Factura ANAF turns WooCommerce orders into electronic invoices in the national format (UBL 2.1, EN 16931 with the Romanian CIUS RO_CIUS 1.0.1) and uploads them to the RO e-Factura system with your own ANAF connection (OAuth, the same way accounting programs do it). You do not need a subscription to an invoicing service in between.

**Free version**

* Invoice for every order of a company with a CUI, created when the order reaches the status you choose (Completed or Processing).
* Direct upload to ANAF (`upload`), status checks (`stareMesaj`) and download of the answer ZIP — the invoice with the Ministry of Finance seal, i.e. the legal original — saved with the order.
* Clear reasons when ANAF rejects an invoice (the error list from the answer), "Rebuild and send" with the same number after you fix the order.
* A retry queue: network problems, ANAF technical errors and rate limits never lose an invoice.
* CUI / CIF and trade register fields at checkout (classic and block checkout), a "Company name" field when WooCommerce hides its own, validation of the CUI check digit.
* Romanian address rules checked before upload: county as ISO 3166-2:RO, SECTOR1–SECTOR6 for Bucharest (the checkout asks Bucharest customers for the sector).
* VAT from the WooCommerce taxes of each order, several rates on one invoice, exempt / zero-rated lines, sellers not registered for VAT (category O), the "TVA la încasare" note.
* Your own number series with a year or month counter; test and production keep separate registers.
* Test (api.anaf.ro/test) and production environments.
* An invoice register with the XML, the ANAF index and messages, and a box on every order.

**Pro (7-day free trial, then a licence)**

* B2C reporting: invoices for consumers without a CUI sent through the B2C upload (`uploadb2c`), with the CNP when the customer gives it or 13 zeros.
* Credit notes (381) from WooCommerce refunds — by item or by amount — with a reference to the invoice.
* Invoices for foreign buyers sent with `extern=DA` (intra-Community supply K, export G).
* The e-Factura PDF rendered by ANAF, in the admin and for the customer in My Account.
* SPV messages: invoices received from suppliers and ANAF answers of the last 60 days, download as ZIP or PDF.
* Bulk "Send to e-Factura" for past orders, CSV export of the register, e-mail alerts (rejected invoices, delivery problems, an ANAF connection that is about to end).

**What you need**

* A company registered in SPV and a qualified digital certificate of a person with SPV rights for its CUI (legal representative, designated representative or authorised person).
* An application registered for the "E-Factura" service in the ANAF developer profile (anaf.ro → Servicii online → Înregistrare utilizatori → Dezvoltatori aplicații), with the callback URL shown in the plugin settings. ANAF gives you a Client ID and a Client Secret.
* Orders in RON.

== Installation ==

1. Install and activate the plugin (WooCommerce must be active).
2. WooCommerce → e-Factura → Settings: fill in the seller data (CUI, name, address, county, IBAN) and the numbering.
3. In the "ANAF connection" section paste the Client ID and Client Secret of your ANAF application, save, then click "Connect to ANAF" and choose your qualified certificate. The access token is valid for 90 days and is renewed automatically with the refresh token (valid for 365 days); after a year connect again.
4. Keep the TEST environment for your first orders, check the results in the Invoices tab, then switch to PRODUCTION.

== Frequently Asked Questions ==

= Is there a test environment? =
Yes: api.anaf.ro/test. It needs the same ANAF connection (a qualified certificate) as production. Invoices sent there have no legal effect. For a shop without a certificate the plugin can simulate ANAF answers: define `CCEF_ANAF_MOCK` as `ok` in wp-config.php (test environment only).

= When is an invoice sent? =
When the order reaches the status you choose, or with "Send to e-Factura" on the order. ANAF expects invoices within 5 working days of issue; the plugin uploads them right away and retries on its own when ANAF cannot be reached.

= What happens to consumer orders? =
In the free version they are not sent. Pro reports them through the B2C upload with the CNP or 13 zeros, as ANAF accepts for consumers who do not identify themselves. Confirm with your accountant which of your sales must be reported.

= ANAF rejected an invoice. What now? =
The reason from the ANAF answer is shown in the register and in the order notes. Correct the order (for example the county or the Bucharest sector) and click "Rebuild and send": the invoice is rebuilt with the same number, which never reached the buyer.

= Where are the ANAF answers kept? =
In the site database (not in a web folder), so no web server can hand them out by URL: they are downloaded only in the admin, and the ANAF PDF by the customer who placed the order. Version 1.0.1 moves the files an earlier version kept in wp-content/uploads/ccef-private-… into the database and removes that folder. Tokens and the Client Secret are stored encrypted with your site's secret keys.

= Other currencies? =
This version sends invoices in RON only; orders in other currencies are skipped with a message.

== Privacy ==

The plugin sends invoice data (seller, buyer, address, CUI or CNP, lines, amounts) to ANAF — the purpose of e-Factura. Licence checks contact catcode.com.ua with the licence key and the site address only.

== Changelog ==

= 1.0.1 =
* The ANAF answers (signed ZIPs, error lists, SPV messages, PDFs) are kept in the database instead of wp-content/uploads, where nginx served them by direct URL. Existing files are moved on update and the folder is removed.

= 1.0.0 =
* First release.
