=== Maillon PO — PDF, Excel & Email Purchase Orders to WooCommerce ===
Contributors: maillon
Tags: woocommerce, b2b, purchase order, wholesale, pdf
Requires at least: 6.5
Tested up to: 6.7
Requires PHP: 8.0
Stable tag: 1.0.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Convert B2B PDF, Excel (.xlsx), CSV, Email, and cXML Purchase Orders into verified WooCommerce orders with customer SKU memory and HPOS support.

== Description ==

Stop manually re-typing B2B Purchase Orders into WooCommerce.

**Maillon PO for WooCommerce** turns incoming buyer Purchase Orders — PDFs, Excel spreadsheets (`.xlsx`), CSV/TSV sheets, pasted email orders, or cXML `OrderRequest` files — into verified WooCommerce **On Hold**, **Pending**, or **Draft** orders in seconds.

Built specifically for wholesalers, distributors, and B2B WooCommerce merchants:

* **Zero external PHP dependencies:** Reads `.xlsx` (native `ZipArchive` + `SimpleXML`), `.csv`/`.tsv`, digital `.pdf` text streams, cXML `OrderRequest`, and plain-text emails directly on your server.
* **Per-Customer SKU Memory & Pack Multipliers:** Trade buyers rarely use your store SKUs. Map a customer's internal code (for example `BYG-EPI-09` → your SKU `EPI-GANT-09` × `12` per carton) once, and Maillon PO automatically recognizes it on every future order from that buyer.
* **Built-in Financial Guardrails:**
  * **Duplicate PO Detection** (HPOS-native lookup on `_maillon_po_number`) prevents double-shipping the same PO.
  * **Price Discrepancy Alerts** highlight any line where the buyer's PO unit price differs by more than `1%` from your WooCommerce store price, letting you pick Store Price or PO Price line-by-line.
  * **Header Total Verification** warns when the declared PO total differs from the sum of extracted lines.
  * **Live Stock Alerts** warn immediately if requested effective units exceed available WooCommerce inventory.
* **Private File Vault & Audit Trail:** Original PO files are stored in a protected directory (`wp-content/uploads/maillon-po-private/` with `.htaccess` deny rules, randomized filenames, and magic-byte validation) and attached directly to the WooCommerce Order Edit screen via an HPOS-compatible meta box.
* **B2B Customer Upload Portal:** Drop the `[maillon_po_upload]` shortcode onto any logged-in My Account or Wholesale page so trade buyers can submit their own PO files into your admin review queue with role checks and daily rate limiting.
* **100% HPOS Compatible:** Built exclusively on WooCommerce CRUD APIs (`wc_create_order`, `wc_get_orders`, `wc_get_products`) and declares `custom_order_tables` compatibility.

== Installation ==

1. Ensure WooCommerce 8.5+ is installed and active.
2. Upload the `maillon-po-for-woocommerce` folder to `/wp-content/plugins/` or install the `.zip` via **Plugins → Add New → Upload Plugin**.
3. Activate **Maillon PO for WooCommerce**.
4. Open **WooCommerce → Maillon PO** to upload or paste your first Purchase Order.
5. (Optional) Add `[maillon_po_upload]` to a private B2B customer page to collect PO uploads from logged-in trade buyers.

== External Services ==

By default, **Maillon PO makes zero external network requests** — all Excel, CSV, TSV, cXML, email text, and digital PDF parsing runs 100% locally on your WordPress server.

Optionally, if you receive scanned image-only PDFs, a store administrator may explicitly enable **Google Gemini 2.5 Flash (Bring Your Own API Key)** under **WooCommerce → Maillon PO → Settings**:

* **Service:** Google Gemini API (`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent`)
* **When it is called:** Only when the administrator switches AI OCR from `Disabled` to `Google Gemini 2.5 Flash`, provides their own API key, AND uploads a PDF whose local text extraction finds no order lines.
* **Data sent:** The uploaded Purchase Order PDF (or extracted text) and a JSON extraction prompt, authenticated with the administrator's own API key.
* **Provider policies:** [Google AI Terms of Service](https://ai.google.dev/gemini-api/terms) and [Google Privacy Policy](https://policies.google.com/privacy).

== Frequently Asked Questions ==

= Does Maillon PO support WooCommerce High-Performance Order Storage (HPOS)? =

Yes. Maillon PO declares full `custom_order_tables` compatibility and uses WooCommerce CRUD APIs (`wc_create_order`, `wc_get_orders`, `wc_get_products`) with zero direct `wp_posts` order queries.

= Do I need an AI API key or subscription to use Maillon PO? =

No. Excel (`.xlsx`), CSV, TSV, cXML, plain-text emails, and digital text-based PDFs are parsed 100% locally on your server with no API key required. Optional Gemini AI OCR is disabled by default and only needed if your buyers send scanned paper photos.

= Are uploaded buyer Purchase Orders publicly accessible by URL? =

No. Uploaded files are stored in `wp-content/uploads/maillon-po-private/` protected by `.htaccess` deny rules, `index.php`, randomized 32-character hex filenames, and capability-checked (`manage_woocommerce`) nonce-verified downloads.

= Can buyers submit Purchase Orders themselves? =

Yes. Place `[maillon_po_upload]` on any page. Only logged-in users with allowed roles (configurable in Settings) can submit files, protected by WordPress nonces and a configurable daily rate limit.

== Changelog ==

= 1.0.0 =
* Initial release.
* Pure-PHP parsers for `.xlsx`, `.csv`, `.tsv`, digital `.pdf`, cXML `OrderRequest`, and plain-text emailed POs.
* Per-customer SKU alias and pack-multiplier memory (`wp_maillon_po_mappings`).
* Duplicate PO check, >1% unit price discrepancy alerts, declared total verification, and stock checks.
* HPOS-native order creation and Source Purchase Order meta box on WooCommerce Order Edit screens.
* Logged-in B2B upload portal shortcode `[maillon_po_upload]` with admin review queue and email alerts.
* Optional opt-in Gemini 2.5 Flash BYOK OCR fallback for scanned PDFs.
