=== Fakturuj.si for WooCommerce ===
Contributors: fakturujsi
Tags: woocommerce, invoicing, fakturuj.si, invoices, billing
Requires at least: 5.8
Tested up to: 7.1
Requires PHP: 7.4
Stable tag: 1.3.0
WC requires at least: 6.0
WC tested up to: 11.3
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Automatically create invoices in Fakturuj.si from your WooCommerce orders.

== Description ==

**Fakturuj.si for WooCommerce** connects your WooCommerce store with [Fakturuj.si](https://fakturuj.si), a simple invoicing platform. When a customer places an order, an invoice is automatically created in Fakturuj.si.

= Features =

* Invoice created the moment an order enters a status you pick (also right at checkout: pick "Pending payment"), once per order
* Prices with VAT exactly as the shopper paid (never VAT added twice): every line is gross-first, VAT rate from WooCommerce taxes
* Shipping and fees as their own lines, variable symbol = order number (or the digits of a sequential order number), payment method from the gateway
* Due date (issue + N days), delivery date = order date, document language from the storefront locale
* Paid orders create paid invoices; an invoice created before the payment is marked paid in Fakturuj.si when the order is paid
* IČO / DIČ / IČ DPH read from any checkout: the fields of the common Slovak, Czech and EU plugins (classic and block checkout) are recognised by themselves; optional own fields for shops that have none
* Product, shipping and fee names printed as plain text (HTML tags and entities in a product title never reach the invoice); variation attributes that are not in the name are added in brackets
* e-invoice (Peppol) ready: with e-invoicing active in Fakturuj.si the invoice reaches a Slovak company automatically; a switch keeps it waiting for a manual send
* Products to the price list: one button sends the published products (simple products and every variation) to the price list in Fakturuj.si, with the price exactly as stored in the shop and the VAT rate of the product's tax class; sending again updates the same items, optional automatic update after a product is saved, WP-CLI `wp fasi sync-products [--dry-run]`
* Orders in Fakturuj.si (optional): the shop's orders appear in Fakturuj.si, a state changed there is written back to the shop, the invoice is issued once through the order
* Order page: invoice number, Open PDF, preview, E-mail to customer, Create again
* Customers: My account > Invoices with PDF download, PDF link on the order detail
* Optional e-mail of the PDF to the customer, optional numbering series
* Full WooCommerce HPOS (High-Performance Order Storage) compatibility, Action Scheduler for the API calls, logging via WooCommerce > Status > Logs

= How it works =

1. Install and activate the plugin
2. Go to WooCommerce → Settings → Fakturuj.si
3. Enter your Fakturuj.si API URL and API Key
4. Set the Company ID ("Test connection" lists the IDs your key may use), the trigger statuses and the options you need
5. Done! Invoices are created automatically when orders reach the configured status.

== Company fields (IČO, DIČ, IČ DPH) ==

WooCommerce has no fields for the company ID (IČO), the tax ID (DIČ) and the VAT ID (IČ DPH). A checkout plugin adds them, and this plugin reads them from the order.

= Recognised automatically =

Nothing to set up when the fields come from one of these plugins, on the classic and on the block checkout:

* Webikon: Faktúry pre WooCommerce and SuperFaktura WooCommerce (order fields `billing_company_wi_id`, `billing_company_wi_tax`, `billing_company_wi_vat`, with or without a leading underscore, and the block checkout fields ending in `billing-company-wi-id`, `-tax`, `-vat`)
* Webikon: Nastavenia SK/CZ pre WooCommerce (`_wc_nastavenia_skcz_billing_company_id`, `_tax_id`, `_vat_id`)
* Kybernaut IČO DIČ and WPify Woo (`_billing_ic`, `_billing_dic`, `_billing_dic_dph`, block fields `wpify/ic`, `wpify/dic`, `wpify/dic-dph`)
* Fakturovo, Nimble.help SK/CZ Store Tools, Allimedia ICO Lookup, ARES IČO a DIČ v pokladně, IČO priradenie
* VAT number plugins: WooCommerce EU VAT Number, Aelia EU VAT Assistant, EU/UK VAT Validation Manager, European VAT Compliance Assistant, Germanized Pro, YITH, German Market

The "buying as a company" box of these plugins is respected: when the shopper did not tick it, the order is invoiced to a private person even if old company numbers are still stored in the customer profile.

Fields of a plugin that is not on the list are found when their key names the value (for example `company_ico`, `firma-dic`, `vat_number`) and the value has the right shape.

The values are cleaned before they are sent: spaces and labels typed into the field are removed, a Slovak IČ DPH without the prefix gets `SK`, a Slovak IČ DPH typed into the DIČ field (`SK2020317068`) is used as the VAT ID as well, a missing Slovak DIČ is taken from the IČ DPH. A Czech DIČ (`CZ12345678`) stays a DIČ: a Czech buyer who does not pay VAT has one too, so the VAT ID is sent only from a field that names it. A value that is not a valid number is left out and named in an order note, so one mistyped number never blocks the invoice.

= Our own fields =

For a shop without such fields: WooCommerce > Settings > Fakturuj.si > IČO, DIČ, IČ DPH > "Company fields at the checkout".

* Off (default): the checkout is not changed.
* Automatic: a box "I am buying as a company" with IČO, DIČ and IČ DPH is shown only when no other plugin adds such fields.
* Always: the fields are always shown.

The fields are never required. They work on the classic checkout and on the block checkout (WooCommerce 8.9 and newer), are remembered in the customer account, can be edited on the order page and are printed with the billing address in the order e-mails. Keep WooCommerce's own "Company name" field visible (Optional). A shopper who fills that field without ticking the box is still invoiced as that company.

= Naming your own meta key =

When your checkout stores the numbers under a key the plugin does not find, type the key into "Order meta key of the IČO / DIČ / IČ DPH" on the same settings page (several keys can be separated by commas). Developers can also correct the result with the filter `fasi_wc_company_ids` (arguments: the result array and the order).

= The diagnostic =

"Check the last orders" on the settings page lists, for the last 10 orders, the company, the three numbers the invoice would carry, the field each one comes from and fields that look like such a number but were not recognised. The same information is on every order page in the Fakturuj.si box, and an order note says which field each number came from when the invoice is created.

== Products in the price list ==

WooCommerce > Settings > Fakturuj.si > Price list. The button sends every published simple product and every variation of a variable product (the variable parent itself, grouped and external products are not sent). The run works in the background in batches of 200 and the page shows the progress and the last result: created, updated, unchanged, skipped and why.

* Name: the product title as one plain line; a variation is the parent name with its attributes.
* Code: the product's own SKU. A variation without its own SKU has no code (it never takes the parent's).
* Price: exactly the stored price, with VAT when the shop enters prices with tax, without VAT otherwise. Choose the regular price (default) or the current price.
* VAT rate: the base rate of the product's tax class; 0 when taxes are off or the product is not taxable.
* An item that already exists in Fakturuj.si with the same code is adopted instead of duplicated; when several items share the code the product is skipped and named.
* The automatic update (off by default) sends a saved product about a minute later.
* Invoices created from orders are not linked to the price-list items and never move stock.

== Orders in Fakturuj.si ==

WooCommerce > Settings > Fakturuj.si > Orders, switch "Transfer orders to Fakturuj.si" (off by default). With the switch off the plugin behaves exactly as before: it only issues invoices.

= What is sent =

* Every order created from the moment you switch the transfer on, and every later change of it (status, lines, addresses). Older orders are never sent on their own: choose them in the orders list (bulk action "Send to Fakturuj.si") or run `wp fasi push-orders --days=30` or `wp fasi push-orders --ids=12,13`.
* The order appears in Fakturuj.si under Orders with the number your customer knows, the buyer (one contact per customer, with IČO / DIČ / IČ DPH from the checkout), the lines, shipping and fees, and a link back to the order in your shop.
* Sending runs in the background (Action Scheduler, group fakturuj-si), never while the customer is paying. An order that did not change is not sent again. Unfinished checkouts (drafts) are not sent; an order moved to the trash is shown as cancelled.

= The two status tables =

* Shop status to the state in Fakturuj.si. Defaults: Pending payment = Created, On hold = Accepted, Processing = In progress, Completed = Completed, Cancelled, Failed and Trash = Cancelled, Refunded = Returned. Every status of your shop is listed, including custom ones; each can be set to "do not change".
* State in Fakturuj.si to the shop status. Defaults: Completed = Completed, Cancelled = Cancelled; Accepted, In progress, Returned and Complaint = do not change. WooCommerce treats Processing and Completed as paid (payment date, customer e-mail, stock), and with "Mark as paid" on the invoice follows, so Accepted and In progress are yours to map: choose Processing for them only when an order in that state is always paid in your shop. Refunded and Pending payment can never be chosen here, because WooCommerce would refund the whole order or put the stock back.

= What is written back to the shop =

* A state somebody chooses in Fakturuj.si is written to the shop order within about a minute (at once when your shop can be reached from the internet), with an order note that names Fakturuj.si.
* Important: the shop then does what it always does for that status. Completing an order in Fakturuj.si completes it in WooCommerce, so WooCommerce e-mails the customer "order completed" and counts the order as paid. Cancelling puts the stock back.
* When the shop changed the order in the meantime (for example it was cancelled in the shop while somebody completed it in Fakturuj.si), the shop wins: its status stays and Fakturuj.si follows it.
* An order that is refunded, failed or cancelled in the shop is never re-opened from Fakturuj.si.
* An invoice made from the order in Fakturuj.si is linked to the shop order: the order page shows it with the PDF buttons and the customer finds it under My account > Invoices. The status of the order is not touched by that.

= One invoice per order =

With the transfer on, the invoice is always issued through the order: "create the invoice when the order enters status X" and the button on the order page ask Fakturuj.si to issue the invoice of that order, and Fakturuj.si issues it only when the order has none. So it does not matter whether the shop or a person in Fakturuj.si asks first: there is one invoice. Payment method, variable symbol, due date, "mark as paid", the e-invoice switch and "e-mail to customer" work as they do without the transfer.

= Looking after it =

* The section Orders shows when the last order was sent, when the shop last asked for changes, the last wake-up call from Fakturuj.si, how many orders wait and the last error. "Synchronise now" sends what waits and asks for changes at once.
* The order page shows the link to the order in Fakturuj.si, its state, the time it was last sent and a "Send now" button.
* `wp fasi pull-changes` asks for changes from the command line.
* If your shop cannot be reached from the internet (a local or password-protected site), nothing is lost: the shop asks for changes itself every minute.

== Installation ==

1. Upload the `fakturuj-si-woocommerce` folder to `/wp-content/plugins/`
2. Activate the plugin through the 'Plugins' menu in WordPress
3. Go to WooCommerce → Settings → Fakturuj.si to configure

== Changelog ==

= 1.3.0 =
* IČO / DIČ / IČ DPH are read from any checkout: the fields of Webikon (Faktúry pre WooCommerce, SuperFaktura, Nastavenia SK/CZ), Kybernaut, WPify, Fakturovo, Nimble, ARES and the EU VAT number plugins, on the classic and on the block checkout, plus any field whose key names the value. Until now the Webikon fields and the Kybernaut IČ DPH were not picked up.
* The "buying as a company" box decides: a private purchase is never invoiced with company numbers left in the customer profile.
* Values are cleaned and checked before they are sent; a number the API would refuse is left out and named in an order note instead of blocking the invoice.
* New: optional own fields "I am buying as a company", IČO, DIČ, IČ DPH for the classic and the block checkout (Off by default).
* New: settings section "IČO, DIČ, IČ DPH" with your own meta keys and the diagnostic "Check the last orders"; the order page shows the buyer's numbers and the field each came from.
* Product, shipping and fee names are printed as one plain line: a product title with `<br>` or `&amp;` no longer shows the tag on the invoice. Variation attributes that are not part of the name are added in brackets.
* "E-mail to customer" sends the invoice to the e-mail of the order as well, also when the invoice was put on an existing contact.
* The default invoice note and the fallback names follow the language of the document.
* New: settings section "Price list" with the button "Send products to the price list". Published simple products and every variation of a variable product become price-list items in Fakturuj.si (name as plain text, the product's own SKU as the code, the stored price with or without VAT as the shop enters it, the VAT rate from the tax class, your unit). A product is recognised by its ID in the shop, so a second run changes nothing and a changed price updates the same item. Products without a price are skipped and listed. Stock is not transferred; products deleted in the shop stay in the price list.
* New: optional automatic update of the price list about a minute after a product is saved (off by default), sent in batches of 200 in the background.
* New: WP-CLI command `wp fasi sync-products [--dry-run]`.
* New: transfer of orders (settings section "Orders", off by default). The shop's orders appear in Fakturuj.si with the customer's order number, the buyer and the lines; a state changed in Fakturuj.si is written back to the shop with an order note (completing an order there makes WooCommerce e-mail the customer); the invoice is issued once through the order, whoever asks first. Two status tables with sensible defaults, a state line and "Synchronise now" in the settings, the bulk action "Send to Fakturuj.si" in the orders list, the order link and "Send now" on the order page.
* New: WP-CLI commands `wp fasi push-orders [--days=N] [--ids=1,2]` and `wp fasi pull-changes`.
* Orders: the states Accepted and In progress chosen in Fakturuj.si change nothing in the shop by default. "Processing" is a paid status in WooCommerce, so an unpaid bank-transfer order would be marked paid and its invoice with it; you can still map them in the status table.
* Orders: an order that is refunded, failed or cancelled in the shop is never re-opened from Fakturuj.si.
* Own fields: when the box "I am buying as a company" is not ticked but WooCommerce's own company field is filled, the order is invoiced to that company as before, not to a private person.
* A Czech DIČ is no longer copied into the IČ DPH: the VAT id is sent only from a field that names it.
* Price list: products are recognised per shop, so two shops on one Fakturuj.si account never overwrite each other's items. An item whose code (SKU) already belongs to another product is skipped and listed.
* Declared compatible with the block cart and checkout; tested with WooCommerce 11.3.

= 1.2.0 =
* Fixed: the plugin read the invoice id from the wrong field of the API answer, so it never remembered the invoice it created - every trigger status change created another invoice and the order page kept saying "No invoice created yet".
* Prices with VAT exactly as paid (gross-first lines), shipping and fees as lines, variable symbol = order number, order reference, payment method from the gateway, due date, delivery date, document language.
* Paid orders create paid invoices; invoices created before the payment are marked paid later; one invoice per order even when the status flips (Action Scheduler + idempotency key).
* IČO / DIČ / IČ DPH from the billing meta of the common SK/CZ checkout plugins; private persons recognised.
* e-invoice (Peppol) switch; the default language setting.
* Order page: Open PDF, preview, E-mail to customer, Create again; My account > Invoices with PDF download and a PDF link on the order detail.
* Settings: the API key is never shown again once stored; the API URL must be https; "Test connection" lists the companies the key may use.
* Slovak and Czech translations of the whole plugin.

= 1.1.1 =
* API validation errors (INVALID_BODY) now list the rejected fields in the order note, e.g. "companyId (Invalid ObjectId)".

= 1.1.0 =
* HPOS (High-Performance Order Storage) compatibility fixed - the order
  screen no longer fatals; the meta box uses the public WooCommerce API.
* Invoice trigger is configurable: any set of order statuses, including
  custom statuses added by other plugins.
* Optional numbering series: route e-shop invoices into a dedicated
  Fakturuj.si numbering series.


= 1.0.0 =
* Initial release
* Automatic invoice creation from WooCommerce orders
* Settings page in WooCommerce settings
* Connection test functionality
* Order meta box showing invoice info
