NEW INSTALL (cPanel)
1. MySQL Databases: create DB + user, add user to DB with ALL PRIVILEGES.
2. phpMyAdmin: select DB > Import > schema.sql
3. Edit inc.php top: DB_NAME, DB_USER, DB_PASS, CUR, SHOP
4. Upload ALL files (incl. .json, .js, .png, .svg) to public_html/pharmacy. Delete *.sql and README after.
5. Open /pharmacy/login.php -> admin / admin123. Change it at "My Password". Use PHP 8.1+.
6. OPTIONAL demo data: log in as admin, open /pharmacy/seed.php, click Load. Then DELETE seed.php and the sample users.

INSTALL AS APP (needs HTTPS - enable AutoSSL in cPanel)
Android/Chrome/Edge desktop: menu > Install app (or the "Install App" link in the sidebar).
iPhone/iPad Safari: Share > Add to Home Screen.

UPGRADING FROM VERSION 1: back up, import upgrade.sql (NOT schema.sql), overwrite all .php files (keep your inc.php DB settings).
ROLES: admin=all | pharmacist=POS, sales/returns, inventory, batches, POs, suppliers, customers, reports
cashier=POS, sales (view), customers | accountant=sales (view), POs (view), supplier payments, customers, expenses, reports

REPORTS: "Report Center" (center.php) = 13 reports (sales, by product, by cashier, returns, expenses, inventory valuation, expiry,
write-offs, purchase orders, supplier payments, customer credit, collections, profit & loss), each with daily/weekly/monthly/yearly/custom
period, filters (user, customer, supplier, category, payment method, status...), and PDF / CSV / Print. PDF needs no extra libraries.

OFFLINE MODE (POS)
- Needs HTTPS. Each device must open the POS once while online (it saves products, stock, customers on the device).
- Offline sales are saved in the browser database (IndexedDB) and uploaded automatically when online (on reconnect, every 30s, when the app is opened, and via background sync where the browser supports it).
- Every sale has a unique ID, so a retry can never create a duplicate. The sale is removed from the device only after the server confirms it.
- Upgrading from the previous version: import upgrade_offline.sql, then upload all files (keep your inc.php DB settings).
- "Offline Sales & Sync" page (or tap the status chip at the top) shows waiting sales, Sync now, and a JSON backup download.
- Do not clear browser data / use private mode while sales are unsynced.

RETAIL / WHOLESALE: set an optional wholesale price per product (Inventory). In the POS choose Retail or Wholesale before selling; cart prices switch automatically
(products without a wholesale price use the retail price). Sales are labelled, filterable in Sales and the Report Center, and split out in the Summary report.
UPGRADE: import upgrade_wholesale.sql once (after upgrade_offline.sql), then upload all files.

LAB TESTS & FEES: 'Lab Tests & Fees' page (admin/pharmacist) to add tests with a fee (and optional reagent cost). In the POS use the 'Lab tests' tab to add tests to the same cart as medicines.
Lab fees show in Sales, the Summary report, and a 'Lab tests & fees' report in the Report Center. UPGRADE: import upgrade_lab.sql once (after upgrade_wholesale.sql).

HUMAN RESOURCE: new 'hr' user role + sidebar group HR (Dashboard, Employees, Attendance, Leave, Payroll) + Clock In/Out for any staff linked to an employee record.
Payroll: draft -> approve (admin/accountant) -> mark paid, which records ONE 'Salaries' expense (gross + employer NSSF) so profit reports stay correct. NSSF/PAYE rates are editable defaults in Payroll settings - confirm with your authorities.
HR reports (staff, attendance, leave, payroll) are in the Report Center with PDF/CSV. UPGRADE: import upgrade_hr.sql once (after upgrade_lab.sql).

SELLING UNITS (tablet / strip / pack): in Inventory set 'tablets per strip' and 'strips per pack' and a price for each unit (blank tablet/pack price = worked out from the strip price). Stock is kept in tablets; the POS lets you pick Tablet, Strip or Pack per line and deducts the right number of tablets.
Items that are not split (syrups, devices) keep both numbers at 1 and sell as a single item. UPGRADE: import upgrade_units.sql once (after upgrade_hr.sql). Existing stock keeps working; when you first set 'tablets per strip' on a product tick the 'convert' box so its stock and history are re-counted in tablets.

ANIMATIONS: page fade-in, staggered cards/rows, count-up dashboard numbers, animated 7-day sales chart, button ripples/hover, sidebar slide-in, page-change progress bar, POS add-to-cart highlight and sale-complete check animation, sync chip pulse. All disabled automatically for users who set 'reduce motion' in their device. No new database changes.

SETTINGS & BRANDING: Admin > Settings: pharmacy name, logo upload, phones, email, website, address, TIN, currency and receipt footer. They appear on receipts (online and offline), payslips, every print view and all PDF reports. The logo is stored in the 'uploads' folder (created automatically; if your host blocks that, create a writable folder named uploads).
ERASE DATA: Admin > Erase Data (super administrator only, marked with a star on the Users page; the first admin is super by default). Erases chosen transaction data (sales, credit, purchases, stock, expenses, HR records, audit log) but keeps drugs, lab tests, employees, users, suppliers/customers (unless ticked) and settings. Needs password + typing ERASE.
UPGRADE: import upgrade_settings.sql once (after upgrade_units.sql). Backups (SQL) do not include the logo file.

DATABASE LOGIN DETAILS now live in config.php (not inc.php) - keep your config.php when uploading new versions.


AUTO-ORDER: Purchase Orders > 'Auto-order low and out-of-stock items' (also a button on the dashboard Low stock list). One click creates pending purchase orders, one per supplier, for every product at/below its reorder level, sized from the last 30 days of sales (editable days of cover), minus stock already on order, rounded up to whole packs/strips. Review, receive or cancel them as usual. No database changes.

INVENTORY: products can now be deleted (Inventory > Delete). Items with no history are erased; items with sales/purchase history are archived (hidden, restorable under 'Show archived') so reports stay correct. Cost is now entered PER PACK. Product type 'Single item' (syrups, bottles, devices: 1 per pack) hides tablet/strip/pack fields. UPGRADE: import upgrade_inventory.sql once.

RECEIPTS: compact thermal layout with no margins; the page height is fitted to the receipt so no blank paper is fed. Choose 58 mm or 80 mm paper in Settings. In your browser's print dialog set Margins to None / Default and turn off Headers and footers; pick your receipt printer's roll paper size.

PAYMENT ACCOUNTS: Payment Accounts page (admin/accountant) saves your bank and mobile money accounts. In the POS, choosing Mobile Money or Bank shows your saved accounts as tap buttons (the last one used is pre-selected) and requires the Transaction ID, which is saved with the sale, shown on receipts and the sales page, checked for duplicates, and included in reports (Report Center > Payments by method / account). Customer credit receipts and supplier payments use the same accounts and Transaction ID. UPGRADE: import upgrade_payments.sql once.

DASHBOARD vs REPORTS: dashboard cards and the Inventory & Stock Valuation report now use the same calculations (stats.php): active items only, positive batch quantities at each batch's own cost, net sales (after refunds). Click a dashboard card to open the matching report. Archiving a product writes off its remaining stock. If any product's stock total disagrees with its batches the dashboard shows a 'Fix stock totals now' button. PHP and MySQL time zones are now forced to match (TIMEZONE in config.php).

VALUATION CHECK: the Inventory & Stock Valuation report now has a 'Potential profit' column and lists any product whose cost value is higher than its retail value (usually a pack-size / cost-per-pack mix-up). In Inventory > Edit, tick 'Also apply this cost to the stock I already have' to correct the cost of stock already received.

AUDIT FIXES: stock value now uses NET batch quantities (so it always agrees with the Stock column); oversold (negative) batches are settled automatically when new stock is received and by 'Fix stock totals'; editing tablets-per-strip on an item with stock now warns if 'convert' is not ticked; the valuation report explains the likely cause for any product whose cost value exceeds its retail value.


EDIT STOCK: Inventory > Edit now has a 'Stock' box. Change it to the real count. Extra stock is added as a CORRECTION batch (optional expiry); removed stock is taken from the batches expiring first and logged as a 'Stock correction' write-off. Every change is in the Audit Log. No database changes.

DELETE USERS: Admin > Users > Delete. You cannot delete yourself, a super administrator, or the last active admin. A user with no history is erased; a user who has sales/purchases/etc. is archived (name kept on old records and reports, login blocked, username freed) so reports never lose data. No database changes.


CASH & BANK (v21): sidebar 'Cash & Bank' (admin/accountant) and three dashboard cards show Cash at hand, Money in bank and Mobile money. 'Bank the money' moves cash from the counter into a saved bank/mobile money account; 'Withdraw to counter' does the reverse. Admin sets Opening balances once (cash and each account). Cash at hand = cash sales + cash credit payments + withdrawals + opening cash - cash refunds - cash supplier payments - expenses - deposits. Card payments count as bank. Erase Data has a matching 'Cash & bank records' option. UPGRADE: import upgrade_banking.sql once (after upgrade_payments.sql), then upload all files.
INVENTORY: the list is now numbered (#).

BACK-DATED SALES (v22): in the POS (admin and pharmacist only) tick "Back-date this sale", pick the date/time from the paper record and enter the sale as usual. It stays on until unticked so a whole day can be entered in a row. The sale is stored with that date (reports, receipts and the daily summary use it), stock is deducted now, and it is flagged "BACK-DATED" in the Audit Log (action sale_backdated, who entered it and when). Limit: 90 days back. Cashiers do not see the option; to allow them, add 'cashier' in the two role lists (pos.php and the backdate check in sync.php). The sale is credited to whoever enters it. Needs no database change.


INVENTORY BULK IMPORT / EXPORT (v23): Inventory > 'Bulk import / export' (admin, pharmacist; page inventory_io.php). Export gives a full re-importable CSV (all prices, supplier, stock, expiry, optional archived items) plus a blank template. Import is two steps: upload and preview every row (New / Update / No change / Error), then confirm. Matching is by ID, then barcode, then exact name; blank cells leave existing values unchanged; stock can be left, set, or added to; pack-size changes are refused (use Edit); the import is a single transaction and is written to the Audit Log. No database changes.


MULTI-BRANCH VERSION (v24): ONE code base, three modes (Branch Setup, admin): Standalone | Main store | Branch.
 * Every branch is its own installation with its own database, so it sells online or offline independently. The main store connects to branches over HTTPS using one secret key per branch (api_branch.php).
 * Main store: 'All Branches' = live dashboard of every location (sales today / this month, stock value, low stock, expiring, cash at hand, bank, last sale) with drill-down into each branch's stock, expiring batches and recent sales. 'Send Stock' = pick a branch, products and quantities; stock leaves the main store (earliest expiry first, expired never sent) and a printable delivery note is created. A transfer can be cancelled until the branch collects it.
 * Branch: 'Receive Stock' > 'Check main store for incoming stock', open the transfer, adjust quantities if anything arrived short, and press Receive. Stock is added with the original batch number, expiry and cost; unknown products are created automatically. The main store is notified (retried automatically if offline). Shortages are flagged on both sides.
 * Main store still buys stock the normal way (Purchase Orders).
 SETUP: install the system on the main store and on every branch (each its own address + database), import upgrade_branches.sql on EVERY installation, set the mode in Branch Setup, add each branch on the main store, then paste the main store address and that branch's key into the branch. Addresses must be https:// and PHP cURL must be enabled (it normally is on cPanel).


WRITE-OFFS & EXPENSE CATEGORIES (v25): (1) Erase Data (super admin) has a new 'Stock write-offs only' option, optionally limited to records dated on or before a chosen date. It deletes the write-off records, so that loss disappears from profit reports; stock quantities are NOT changed. Admin can also delete a single write-off from Batches > Recent write-offs. Both are written to the Audit Log. (2) Expenses > 'Expense categories': create your own categories (saved in settings, no database change); remove ones you do not use. Old expenses keep their category; 'Salaries' is protected because Payroll uses it.
