WHATSAPP BOT V17 - CREDIT / LEDGER / BILL PAYMENTS
================================================

FILES CHANGED
  includes/sidebar.php               - Hide Receive Payment from sidebar
  adminpanel/credit_customers.php    - Orange/white responsive Credit Customer dashboard
  adminpanel/nayapay_topups.php      - Show wallet or credit purpose
  webhook/message_handler.php       - BILL -> screenshot -> automatic verified settlement
  webhook/bot_flow.php              - NayaPay details use configured method, not fixed account
  nayapay_topup.php                 - Credit verification and atomic ledger settlement
  database/V17_CREDIT_PAYMENTS_UPGRADE.sql - Additive column, safe to re-run

HOW TO UPDATE
1. Backup the LIVE FILES AND MYSQL DATABASE (important for wallet/ledger).
2. Upload ONLY_CHANGED_FILES zip contents into the website root, preserving paths.
3. Import database/V17_CREDIT_PAYMENTS_UPGRADE.sql in phpMyAdmin once.
   (PHP can self-migrate the column if user has ALTER privileges, but SQL is safer.)
4. Ensure Payment Methods -> NayaPay is ACTIVE and receiver account number/title are correct.
5. Ensure Google/Gmail OAuth is connected to the mailbox receiving AUTHENTIC
   NayaPay payment receipt emails. Existing receipt matching is used.
6. Ensure PHP server has executable Tesseract OCR (English pack) and PHP
   exec() or shell_exec() works. Test 'command -v tesseract' on hosting.
   Without OCR, screenshots are saved but money is NEVER cleared automatically.
7. Ensure cron/nayapay_verify.php runs every minute via cPanel cron as PHP CLI:
   /usr/local/bin/php /home/CPANEL_USER/public_html/cron/nayapay_verify.php
   Adjust CLI executable and path to the ACTUAL hosting locations.
8. Test end-to-end with a small REAL payment after backup, and verify
   customers.current_due, customers.total_payments, payments,
   customer_ledger and nayapay_topups all reconcile.

CREDIT CUSTOMER FLOW (customer_type='normal' and status='active')
  WhatsApp: BILL
  Bot: Current bill, live configured NayaPay details + screenshot instruction
  Customer sends full OR partial NayaPay payment and uploads original screenshot
  OCR: Read sender name, one unambiguous transaction amount
  Gmail receipt: Require authenticated message from service@nayapay.com,
    matching sender and exact amount, and an unused unique receipt.
  On successful verification only:
    - Record payment in payments table
    - Add CREDIT row to customer_ledger
    - Increase total_payments by receipt amount
    - Decrease current_due (not wallet_balance)
    - Return payment amount and remaining dues
  If screenshot amount ambiguous, bot asks for PAY 500 (example).
  If proof is missing, Gmail disconnected, no trustworthy receipt, or payment
  is larger than current due, NO AUTOMATIC settlement is performed.
  Pending screenshots are rechecked by the cron task.

  Repeated receipts/WhatsApp messages are guarded by database locks, unique
  Gmail receipt IDs / transaction IDs and WhatsApp webhook de-duplication.

PREPAID WALLET FLOW remains separate:
  TOPUP -> amount -> screenshot -> receipt match -> wallet balance increases.

MANUAL COLLECTION
  Sidebar Receive Payment removed as requested. To record a private payment
  manually: Credit Customers -> customer row -> Record Payment.

SAFETY / LIMITATIONS
- Automatic verification currently depends on receipt format, Gmail
  connection, the sender domain auth checks and successful Tesseract OCR.
- If NayaPay email's format/header changes, payments will stay pending;
  review them through NayaPay Top-ups and Customer Ledger.
- Screenshots alone are never sufficient proof of payment.
- Existing historical customer data will not be regenerated.
- No actual live Gmail, NayaPay receipt or MySQL server is available in
  this coding environment; an end-to-end test on your hosting is required.
- Protect the full ZIP: it may contain the original site's credentials/tokens.
