MDH Bakery POS - Third-Party Goods Separation Update
====================================================

BASELINE USED
-------------
This build was created from the latest complete Bakery POS ZIP supplied with the
latest engrgolk_bakery SQL export.

PURPOSE
-------
Third-party finished goods are now managed separately from bakery-manufactured
products while continuing to use the same finished-goods inventory, stock
transfers, POS sales, returns and financial reporting foundation.

IMPORTANT INSTALLATION ORDER
----------------------------
1. Back up the current /public_html/bakery/ application folder.
2. Export a complete backup of the current engrgolk_bakery database.
3. In phpMyAdmin, select the existing engrgolk_bakery database.
4. Import ONLY:
       /sql/013_third_party_goods.sql
5. Confirm that the migration completes successfully.
6. Upload bakery_third_party_goods_FULL_DIRECT_EXTRACT.zip to:
       /public_html/bakery/
7. Extract it there and overwrite the matching application files.
8. Close any already-open Bakery POS browser tab and reopen the application once
   so the new asset/service-worker version becomes active.

DO NOT import sql/001_master_schema.sql into the existing live database.
That file is synchronized only for a future clean installation.

WHAT THE DATABASE MIGRATION DOES
--------------------------------
- Adds bpos_purchases.purchase_type with RAW_MATERIAL as the default.
- Existing purchase records therefore remain Raw Material purchases.
- Allows bpos_purchase_items.material_id to be NULL.
- Adds bpos_purchase_items.product_id for third-party finished goods.
- Adds the product foreign key/index used by third-party purchase lines.
- Adds third_party.view, third_party.manage and third_party.purchase permissions.
- Assigns the new permissions to the standard system roles as documented in the
  migration.

No existing product, stock, sale, user, supplier, price-history or production
records are deleted by the migration.

NEW THIRD-PARTY WORKFLOW
------------------------
Third-Party Goods
    -> create/manage externally purchased finished item
    -> purchase from supplier
    -> receive into bpos_finished_product_batches
    -> post PURCHASE movement to bpos_product_stock_ledger
    -> set selling price in Third-Party Pricing
    -> transfer through the existing Finished Goods transfer workflow
    -> sell through the existing POS
    -> use existing returns and Phase 10 financial/stock reports

The actual received unit cost is calculated per purchase line as:

    (Qty x Unit Purchase Price - Line Discount + Line Other Cost) / Qty

That cost is stored on the finished-goods batch and stock ledger so the normal
POS FEFO cost snapshot / COGS flow continues to work.

SEPARATION FROM BAKERY PRODUCTION
---------------------------------
Bakery -> Products now manages MANUFACTURED products only.
Third-Party Goods has its own goods master page and non-BAKERY categories.
Third-party goods do not use Recipes, Production Planning, Material Issue,
Production Output or Production Costing.

SUPPLIER / PAYMENT MODEL
------------------------
The existing supplier master remains shared.
Raw-material supplier payments continue to show only RAW_MATERIAL purchases.
Third-Party Supplier Payments shows only THIRD_PARTY purchases.
The underlying bpos_supplier_payments table remains shared so supplier-level
reporting and auditability are preserved.

EXISTING THIRD-PARTY DATA
-------------------------
Existing bpos_products rows already marked THIRD_PARTY are preserved and will
appear automatically on the new Third-Party Goods page after installation.
Existing finished-goods stock is not recreated or duplicated.
Existing opening/adjustment stock continues unchanged.

PERMISSIONS
-----------
New permissions:
- third_party.view
- third_party.manage
- third_party.purchase

Default assignments added by the migration:
- SUPER_ADMIN: view/manage/purchase
- ADMIN: view/manage/purchase
- BAKERY_MANAGER: view/manage/purchase
- STOCK_KEEPER: view/purchase

Barcode generation still requires barcode.manage.
Pricing changes still require price.manage.
Supplier payment entry still requires supplier.payments.

NO AUTHENTICATION CHANGES
-------------------------
Login, Email OTP, Auth.php, bootstrap session behavior and database credentials
were not changed by this update.

TESTING RECOMMENDATION
----------------------
After installation, test in this order:
1. Create a Third-Party category.
2. Create a Third-Party Good.
3. Record a supplier purchase with one or more batches.
4. Verify Finished Goods stock and unit cost.
5. Record/complete supplier payment.
6. Set Third-Party Pricing.
7. Transfer stock to an outlet.
8. Sell the item through POS.
9. Verify stock deduction, COGS/profit and purchase/payment reports.
10. Test an expiry-tracked third-party good and FEFO sale behavior.

ROLLBACK
--------
If a problem occurs, restore both the application backup and the database backup
together. Do not restore only one side because the application and schema must
remain synchronized.
