=== Fortuna Order Manager ===
Contributors: markusstiewe
Tags: woocommerce, orders, order number, shipping, bestellungen
Donate link: https://woocon.de/
Requires at least: 6.0
Tested up to: 7.0
Requires PHP: 8.0
Stable tag: 1.6.1
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Custom order overview with number series, shipping status, and inline editing for WooCommerce.

== Description ==

Fortuna Order Manager replaces the standard WooCommerce order view with a powerful custom interface. Custom sequential order numbers, a freely configurable shipping status, and inline editing directly in the overview make day-to-day order handling significantly more efficient.

**Order overview:**

* Full replacement view for the WooCommerce order list
* Columns: order number, date, WC status, shipping status, customer, amount, actions
* Inline editing of WC status and shipping status with a single click — no page change required
* Sortable columns (date, status, amount)
* Status filter tabs and free-text search (name, email, order number)
* Pagination

**Number series:**

* Custom, gap-free order numbers (e.g. ORD-00001)
* Freely configurable prefix, suffix, increment, and padding length
* Multiple number series can be used depending on order type
* Numbers are only assigned on regular order status transitions (not for drafts)
* Gap check: already-assigned numbers are automatically skipped
* The order number appears in all WooCommerce areas: list, detail view, emails

**Order status management:**

* Manage label, badge color, and sort order for all 7 built-in WooCommerce order statuses
* Add custom order statuses beyond the built-in ones (properly registered with WooCommerce — usable everywhere: order list, filters, edit screen)
* Built-in statuses cannot be deleted, only relabeled/recolored/reordered; custom ones can be deleted at any time
* One status can be marked as "Default" — applied only to orders newly created manually in wp-admin ("Add order"); the regular checkout flow is never affected, payment gateways keep full control of the status they set

**Shipping status:**

* Custom shipping status per order: Pending / Picked / Shipped / Delivered / Returned
* Color-coded badges in the overview

**Draft reminder (churned customers):**

* Automatic email reminder for customers with orders in "Draft" status
* Configurable delay in days from the creation date
* Customizable subject line, email body with placeholders, and optional CC address
* Each order is reminded only once (idempotent sending)
* Test email function directly in the backend

**Draft cleanup:**

* Automatic deletion of draft orders after X days
* Disables WooCommerce's own 1-day cleanup when active
* Last run time shown in the backend

**Dashboard widget:**

* Overview card in the WordPress dashboard: today's orders, revenue, open orders, drafts
* Can be enabled/disabled in the settings

**PDF attachments for WooCommerce emails:**

* Reads PDFs (e.g. terms & conditions, right of withdrawal, privacy policy) from the `email-attachments/` subfolder
* Enable per email type (New order, Order processing, Completed, etc.) via checkbox
* Configuration directly within the WooCommerce email settings under "Additional content"
* Populate the folder via FTP/SFTP; new files appear immediately as selectable options

**Customer accounts:**

* "Customers" subpage in the Bestell Manager menu
* Manually create a regular WooCommerce customer account (email, name, optional company, address, ZIP, city, phone) — the username is always generated automatically (from the company name, or the email address if no company is given); no notification email is sent; the form is collapsed by default
* "Customers" list at the bottom of the page — all existing customer accounts with a direct "Edit" link to the standard WordPress user-edit screen, plus a "Delete" button (confirmation required); WordPress sends no notification email to the customer on deletion
* "Sammelrechnung" Ja/Nein column in that list (see "Collective invoice" below) — set at creation or on the customer's own profile screen
* Guest orders (no linked account) are listed grouped by email address, with order count, total, and last order date
* One-click conversion of a guest to a registered account: reuses an existing WordPress user for that email if one already exists (no duplicate account is created) and links every matching guest order to it
* Username unlocked for editing: WordPress core silently ignores any change to the username on the standard profile screen — a new "Change username" field on every user's profile (Users → Edit User) makes it actually editable

**Collective invoice (Sammelrechnung):**

* Triggered by a dedicated order status "Sammelrechnung" — just select it like any other status
* Automatically allows adding further line items (normally locked once an order leaves "Pending payment"/"On hold")
* Daily line items are added via WooCommerce's native "Add item(s)" flow, each automatically tagged with the current day-of-month as its position (editable afterwards for corrections) — identical products on different days are never merged, they stay listed one below another sorted by day
* Closes automatically on the last calendar day of the month, or manually via an "Abschließen" button — closing only changes the order status to "Completed"; order totals and tax calculation are never touched
* Being a genuinely custom status, it never triggers WooCommerce's automatic "mark as paid" behavior that built-in statuses like "Processing" do
* Optional per-customer flag "Sammelrechnung-Kunde" (Customers page, or the customer's "Edit User" profile screen): every subsequent order this customer places is automatically merged into their open monthly Sammelrechnung right after checkout — no manual admin step needed. The first order of a new month automatically becomes the new running Sammelrechnung. The merged-away original order is permanently deleted, and the standard "Processing"/"On hold" customer emails are skipped for these orders (they're merged, not standalone)

**WLP license protection integrated.**

== Installation ==

1. Upload the plugin directory `Bestell_Manager_V1` to `/wp-content/plugins/`.
2. Activate the plugin via the "Plugins" menu (WooCommerce must be active).
3. Create a number series under "Orders → Number Series".
4. Configure shipping status and reminder options under "Orders → Settings".
5. Enter your license key under Settings → License.

== Frequently Asked Questions ==

= Does the plugin completely replace the WooCommerce order list? =
Yes, the "Orders" menu item in the WooCommerce area is replaced by Bestell Manager's own view. The WooCommerce order detail page remains unchanged.

= Can I use multiple number series? =
Yes, under "Orders → Number Series" you can create any number of number series with different prefixes and suffixes.

= Are existing WooCommerce order numbers overwritten? =
No. Existing orders without a BM number receive an order number on their next status change. Existing BM numbers are not changed.

= How often is the draft reminder sent? =
Exactly once per order, regardless of how long the cleanup period is set to. Sending is safeguarded by an order meta flag.

= Does the plugin work with WooCommerce HPOS? =
Yes, all database access is done via `wc_get_orders()` and the WooCommerce Order API — fully HPOS-compatible.

== Screenshots ==

1. Custom order overview with inline status editing
2. Number series management
3. Settings page with draft reminder and cleanup
4. Dashboard widget with daily overview
5. Shipping status badges in the order list

== Changelog ==

= 1.6.1 =
* The plugin's own admin menu (Bestellungen, Kunden, Status, Nummernkreise, Einstellungen, E-Mail-Anhänge) and every save/delete/update action behind it now use a dedicated `bm_manage` capability instead of the shared WooCommerce `manage_woocommerce` capability, so a role/menu-management plugin can grant a non-admin, non-Shop-Manager role access to order handling specifically without also unlocking every other WooCommerce management screen; existing Administrator and Shop Manager access is preserved automatically via a one-time migration on plugin load/activation
* New: "Customers" subpage — manually create WooCommerce customer accounts and convert guest orders (grouped by email) into registered accounts with a single click; an existing account for that email is reused instead of creating a duplicate, and all matching guest orders are linked to it
* New: username is now editable on the standard WordPress "Edit User" profile screen (Users → Edit User) — a new field bypasses WordPress core's restriction that otherwise silently discards changes to the username
* "Customers" page: the "Add new customer" form now collapses by default and gained address/ZIP/city/phone fields; a new "Customers" list at the bottom shows all existing accounts with "Edit" and "Delete" actions — deletion sends no notification to the customer
* New: "Sammelrechnung" (collective monthly invoice) — a dedicated order status "Sammelrechnung" (manageable like any other status under the new "Status" page, see below) is the sole trigger: selecting it on an order enables adding further line items (WooCommerce normally locks an order against that once it leaves "Pending payment"/"On hold"), shows a dedicated day-of-month position field for each line item (defaults to the current day, editable afterwards) so identical products on different days stay separate and sorted by day, and reveals a "Sammelrechnung abschließen" button. Closing — via that button, or automatically on the last calendar day of the month — only changes the status to "Completed"; totals and tax calculation are never touched. Being a genuinely custom status rather than WooCommerce's built-in "Processing", it also never triggers WooCommerce's automatic "mark as paid" side effect.
* New: "Status" subpage — manage label, badge color, and sort order for all order statuses (including the 7 WooCommerce built-in ones), and add custom order statuses; one status can be marked as the default for orders newly created manually in wp-admin (never affects the regular checkout flow)
* Bugfix: WooCommerce's order-editability check was being filtered under the wrong hook name (`woocommerce_order_is_editable` instead of the actual `wc_order_is_editable`) — this affected both the Sammelrechnung editability override and the existing "All orders editable" option (Settings), which has silently had no effect since it was introduced; both now use the correct filter name.
* Bugfix: "Sammelrechnung" — the internal `_bm_item_day` line-item meta was showing up as a raw "_bm_item_day: …" entry in WooCommerce's own generic item-meta list (visible when editing a line item); it is now hidden from that list via `woocommerce_hidden_order_itemmeta` since the day is already shown in the dedicated "Pos." column.
* "Sammelrechnung abschließen" area also shows a "Remove paid marking" button whenever a still-open collective invoice carries an incorrect "Paid on …" date (e.g. left over from an earlier development iteration) — lets an admin clear it manually, without any automatic/blanket removal that could undo a deliberate "Mark order as paid" action
* New: "Sammelrechnung-Kunde" per-customer flag (Customers page and the customer's "Edit User" profile screen) — every order a flagged customer places is automatically merged into their open monthly Sammelrechnung right after checkout (on the order-received page, once payment/stock handling is fully done — never during checkout itself, to avoid interfering with payment gateways), and the now-empty original order is permanently deleted; if no Sammelrechnung is open yet, the new order becomes the running one directly. The standard "Processing order"/"On hold order" customer emails are skipped for these orders. Unflagged customers are completely unaffected.
* New: Sammelrechnungen for flagged customers now get their own number in the format "KD-00042-08/2026" (customer number + month/year of the invoice) instead of a regular order number — the customer number is assigned immediately when the flag is set (customer creation or the profile checkbox), from the existing "customer" (KD-) number series, unused until now.
* New: orders that get automatically merged into an existing Sammelrechnung, or promoted to become one, no longer consume a number from the regular order (ORD-) series at all — the main order counter is now completely unaffected by Sammelrechnung activity for flagged customers. Orders manually set to "Sammelrechnung" status on non-flagged customers (or guest orders) via the native status dropdown are unaffected and keep getting a normal ORD- number.
* "Customers" list: new "Kundennummer" column, shown for accounts flagged "Sammelrechnung-Kunde"
* Bugfix: the "Status" page's default-status auto-apply could have reapplied itself onto an open or already-closed Sammelrechnung on every subsequent edit, since its "already handled" signal relied on the order having a regular order number — Sammelrechnungen for flagged customers now legitimately never get one; the check now also recognizes an order as a (former or current) Sammelrechnung directly.
* Bugfix: bundled WLP license client — once a trial expired (or a license became invalid) without a key entered, the plugin's own admin menu disappeared entirely and the only way back to the license field was a dismissible admin notice link; a real, always-visible admin menu item ("… — Lizenz benötigt") now appears automatically whenever the plugin is blocked, so the license key stays reachable by simply browsing the admin menu. Also fixed: the notice's direct link to the (hidden, non-blocked-state) license page could itself land on "Sorry, you are not allowed to access this page." on some WordPress versions — it's now registered as a real top-level page that's immediately hidden again, instead of a page with an empty parent slug.
* Bugfix: bundled WLP license client no longer enqueues admin/js/wlp-admin.js a second time under its own script handle — on sites also running the WLP License Manager server plugin (e.g. woocon.de), this caused every delegated click handler in that file to register twice, silently toggling UI state on and back off on click
* WLP client updated: support for a dismissible self-promotion banner (configurable per product in the WLP admin, reappears after X days)
* New: "Email Attachments" subpage in the Bestell Manager menu — PDF upload, file list (name, date), and deletion without confirmation prompt, directly in the backend
* Code quality: bundled WLP license client — output escaping applied at the point of echo, heredoc syntax replaced with string concatenation, missing translator comments added for WordPress Plugin Check compliance

= 1.5.0 =
* New: PDF attachments for WooCommerce emails — PDFs from the plugin subfolder `email-attachments/` are enabled individually per email type via checkbox
* Configuration directly within the WooCommerce email settings (WooCommerce → Settings → Emails → [email type] → "PDF Attachments" section)
* The `email-attachments/.htaccess` file protects files from direct HTTP access

= 1.4.0 =
* Bugfix: shipping cost calculation — the entered value is now correctly treated as a gross amount; the net amount is derived by back-calculation (gross ÷ (1 + tax rate)) and passed to WooCommerce
* The shipping line now shows the gross amount (incl. VAT) instead of the net amount

= 1.3.0 =
* Settings: new "Product thumbnail size" option — configurable width/height (px) of the product image in the WooCommerce product list ("Image" column), default 36 px

= 1.2.0 =
* Trash tab in the order overview (next to "Draft") — shows deleted orders
* Restore button for trashed orders — restores the previous status
* "All orders editable" option in settings — lifts the WooCommerce lock for completed/refunded orders

= 1.1.0 =
* Purchase notice banner in the license field (configurable via WLP admin)
* Dashboard widget (can be toggled in settings)
* Draft cleanup: automatic deletion after X days, WooCommerce's own cleanup is disabled
* Draft reminder: CC field, test email function
* "Reminder is sent only ONCE per order" notice in the UI
* Number assignment: no number for "Draft" status; gap-free sequencing ensured via DB check
* Bugfix: trial notice suppressed for both licensed and free plugins

= 1.0.0 =
* Initial release
* Custom order overview with inline editing
* Number series and shipping status
* WLP license protection integrated

== Upgrade Notice ==

= 1.6.1 =
* WLP client updated: support for a dismissible self-promotion banner (configurable per product in the WLP admin, reappears after X days)
Recommended update: new "Email Attachments" subpage for managing PDF attachments directly in the WordPress backend.

= 1.5.0 =
Recommended update: PDF attachments for WooCommerce emails. Place PDFs in `email-attachments/` and enable them per email type under WooCommerce → Emails.

= 1.3.0 =
Recommended update: product image size in the WooCommerce product list is now freely configurable.

= 1.1.0 =
Recommended update: new dashboard widget, draft cleanup, and test email. License notices now display the plugin name correctly.
