=== Batch Expiry Stock ===
Contributors: starlink1207
Tags: lot number, expiry date, batch tracking, fefo, inventory
Requires at least: 6.5
Tested up to: 7.1
Requires PHP: 7.4
Stable tag: 1.1.1
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Lot numbers and expiry dates for WooCommerce stock. Ships the earliest-expiring lot first (FEFO) and finds recalled lots in seconds.

== Description ==

Batch Expiry Stock is made for small WooCommerce shops that sell food, cosmetics, supplements, pet food or anything else with a lot (batch) number and a best-before date.

Record each delivery as a lot, and every order automatically takes units from the lot that expires first. You always know which lot went to which customer.

= Why this plugin =

* **True FEFO, not just FIFO.** Units leave from the lot with the earliest expiry date, whatever order the deliveries arrived in — and this is in the free version.
* **Recall lookup is free too.** Type a lot number and get every order, customer, email and phone that received it.
* **Exact restore.** Cancelled orders and restocked refunds go back to the very same lots the units came from, not to "some lot".
* **Atomic updates.** Every quantity change is a single conditional database update, so two orders at the same moment can never take the same units, and an order is never deducted twice.
* **Plain about the limits.** The free version covers simple products. Pro adds lots for variations of variable products.

**Goods received**

* Add lots (lot number, expiry date, quantity, received date, note) from the product edit screen or from **WooCommerce > Batches**.
* Optional stock sync: the product stock quantity always equals the total of its lots.

**First-expired, first-out (FEFO) deduction**

* When WooCommerce reduces stock for an order, units are taken from the earliest-expiring lots first.
* The lots and quantities used are saved on each order line.
* An order is never deducted twice. Cancelled orders and restocked refunds go back to the exact lots they came from.
* Expired lots can be skipped automatically.
* Quantity updates are atomic, so two simultaneous orders cannot take the same units.
* Exclude orders from lot deduction by payment method or by "created via" source (for example marketplace or fulfilment-centre orders shipped from someone else's warehouse). The order gets a note "Lot deduction skipped (excluded …)".

**Stock mismatch check**

* When a product's WooCommerce stock no longer equals the total of its lots (changed by Quick Edit, the mobile app, the REST API or an import), the Batches tab shows a "Stock mismatch" badge and the dashboard widget counts it.
* One-click fix: set the stock to the lot total, or put the difference into an "ADJUSTMENT" lot. Stock is never changed silently.

**Lots on orders and packing slips**

* Lot numbers and expiry dates appear on the admin order screen, in order emails and in My Account (customer display can be turned off).
* A printable packing slip lists every line with its lots and expiry dates.
* Works with packing slip plugins that print order item meta.

**Expiry overview**

* The "Expiring" tab lists lots that are already expired or expire within N days (30 by default).
* A dashboard widget shows the counts and the next lots to expire.

**Recall lookup**

* Search a lot number to see every order, customer, email, phone and quantity that received it.

**Light and theme-friendly**

* Ordinary pages load no plugin stylesheet or script, and the lot features add no database queries there.
* Admin styles load only on the plugin's own screens, the product and order edit screens and the dashboard.
* Storefront labels use your theme's fonts, colours and palette, and they work on dark themes, small screens and right-to-left languages.

All dates use your site's time zone. Works with High-Performance Order Storage (HPOS) and the block checkout. Lots are stored in their own database table.

Pro adds lots for each variation of variable products, a once-a-day email of expired and expiring lots, CSV import of goods received plus lot and recall CSV exports, a "Best before" date on product pages taken from the next lot to ship, automatic removal of expired lots from stock with a disposal log, lot reassignment per order line from the order screen (with an order note of old and new lots), near-expiry clearance with tiered automatic sale prices and an "Expiring soon" badge (off by default, prices restored automatically), and recall emails to affected customers with a notification log and a printable recall report.

== Installation ==

1. Install and activate WooCommerce.
2. Upload the `batch-expiry-stock` folder to `/wp-content/plugins/` or install the plugin from the Plugins screen, then activate it.
3. Go to **WooCommerce > Batches > Settings** and choose whether product stock should follow the lot totals.
4. Add lots from a product's "Lots & expiry dates" box or from **WooCommerce > Batches**.
5. Make sure "Manage stock?" is enabled for the product (stock sync turns it on for you).

== Frequently Asked Questions ==

= When are units taken from a lot? =

At the moment WooCommerce reduces the order's stock (normally when the order becomes Processing or On hold). The plugin follows exactly the quantity WooCommerce took.

= What happens when there is not enough lot stock? =

The available lots are used first. The rest is marked "No lot" on the order line and an order note explains it, so you can fix it before shipping.

= What happens on a cancellation or a refund? =

When WooCommerce puts stock back (cancelled order or a refund with "Restock refunded items"), the units return to the same lots they were taken from.

= Which products are supported? =

Simple products. Variable products are supported in Pro.

= Are expired lots sold? =

Not when "Skip expired lots" is on (the default). Expired lots are listed on the Expiring tab so you can deal with them.

= How do I keep marketplace or FBA orders out of my lots? =

In **WooCommerce > Batches > Settings**, list their payment method IDs under "Skip lots for payment methods" or their source under "Skip lots for order sources". Those orders are not deducted from lots and get an order note saying so. WooCommerce still changes the product stock, so the stock mismatch check will show the difference until you fix it the way you prefer.

= What does "Stock mismatch" mean? =

The product's WooCommerce stock differs from the total of its sellable lots, usually because stock was edited somewhere else. Nothing is changed automatically. On the Batches tab you can set the stock to the lot total, or add the extra units to an "ADJUSTMENT" lot (or take missing units out of the lots).

= Can I change which lot an order used? =

Yes, in Pro: open the order, expand "Reassign lots" for the line in the "Lots & expiry dates" box, change the quantities and update the order. Units move between lots in one step and an order note records the old and new lots and who changed them.

= Can I hide lot numbers from customers? =

Yes. Turn off "Show lots to customers" in the settings. Staff still see lots in the admin and on the packing slip.

= Does the plugin slow down my shop? =

No. Ordinary pages load no plugin files and run no extra database queries. Small version flags are read together with WordPress's own settings. The stock mismatch scan on the dashboard is cached and recalculated when lots, stock or settings change. Pro storefront labels load one small stylesheet only on pages that show a label.

= Will the labels match my theme? =

Yes. The Pro "Best before" and "Expiring soon" labels use your theme's font, text colour and colour palette with subtle borders. Themes can override them easily because the styles have very low priority.

= What is removed when I delete the plugin? =

The plugin's settings, its database tables (lots, allocations and the Pro disposal and recall logs) and the lot data stored on orders and order lines. Clearance sale prices are put back when the plugin is deactivated.

== Screenshots ==

1. Batches screen with every lot, its expiry date, quantity and state.
2. Lots on the product screen: add deliveries and see expired, expiring and in-date lots.
3. Lots used for an order line, saved on the order (first-expired, first-out).
4. Recall lookup: every order and customer that received a lot.

== Changelog ==

= 1.1.1 =
* Performance: no extra database queries on ordinary pages (schema version flags are now autoloaded; older installs switch over on the next admin visit).
* Performance: the stock mismatch count on the dashboard is cached and cleared whenever lots, product stock or settings change.
* Performance: the Batches list loads its products in one query; the admin script is deferred.
* Design: admin boxes use the standard WordPress card, numbers and dates follow the site locale, and empty screens say what to do next.
* Pro: "Best before" and "Expiring soon" labels follow the theme (fonts, colours, dark backgrounds, RTL) with an inline icon. Their stylesheet loads only when a label is shown.
* Pro: the daily task is scheduled from admin and cron requests only.

= 1.1.0 =
* New: exclude orders from lot deduction by payment method or "created via" source, with an order note.
* New: stock mismatch check — badge on the Batches tab, count on the dashboard widget and one-click fixes (set stock to lots, or adjustment lot).
* Pro: reassign the lots of an order line from the order screen.
* Pro: near-expiry clearance with editable discount tiers and an "Expiring soon" badge.
* Pro: recall emails to affected customers with a log, order notes and a printable recall report.

= 1.0.1 =
* Hardened output escaping, input sanitizing and database queries.
* 14-day free trial of Pro from the plugin screen.

= 1.0.0 =
* First release: lots with expiry dates, FEFO deduction, restore on cancel and refund, order and packing slip display, expiring list and dashboard widget, recall lookup.
