iKOOP Commission Management System - UPDATE v3.1
================================================

UPDATE: AutoCount Cloud Accounting payment sync + password reset access fix

UPGRADE FROM
------------
Designed to upgrade an existing iKOOP v3.0 installation.

BEFORE UPDATING
---------------
1. Back up the full website folder.
2. Export/backup the MySQL database.
3. Keep config/database.php and storage/installed.lock.

INSTALL UPDATE
--------------
1. Upload iKOOP-UPDATE-v3.1-AutoCount-Password-Fix.zip into the current iKOOP document root.
2. Extract and choose Overwrite / Replace existing files.
3. DO NOT delete config/database.php.
4. DO NOT delete storage/installed.lock.
5. Refresh/login. The v3.1 migration runs automatically once.

PASSWORD RESET FIX
------------------
After a user signs in with a temporary password and saves a new password, the system now sends the user to the first page their Role / Permission allows.
This fixes the "Access denied. Your role does not have permission for this action" screen caused by redirecting every user to Dashboard even when Dashboard permission was not assigned.

LOGIN BRANDING
--------------
The login screen now automatically uses the logo saved in Company / Template when available. Existing files in uploads/ are not overwritten by this update.

AUTOCOUNT CLOUD ACCOUNTING
--------------------------
New menu:
  Administration > Integrations

Configuration fields:
- Account Book ID
- Key ID
- API Key (stored encrypted; not displayed back)
- Enable / Disable integration
- Auto update Project Customer Payment status
- Require Project amount to match AutoCount invoice amount
- Sync interval

AutoCount API permissions required:
- Get Invoice
- Get Invoice Listing (Simple) - used by Test Connection

The integration is READ-ONLY. It does not create or edit accounting records in AutoCount.

PROJECT LINKING
---------------
Open:
  Projects > Project > AutoCount Cloud Accounting

Enter the exact AutoCount Invoice No. such as:
  I-000123

Then click:
  Sync Now

The system stores:
- AutoCount invoice amount
- amount paid
- outstanding amount
- customer/debtor information
- sync state
- last sync date/time

When automatic payment update is enabled:
- no payment -> AWAITING PAYMENT
- some payment -> PARTIAL
- outstanding balance = 0 -> FULLY PAID

When FULLY PAID, the project becomes eligible for the normal commission Payment Batch flow, subject to normal project setup rules.

SAFETY CONTROLS
---------------
- Invoice amount matching is ON by default. If the project amount and AutoCount amount differ, the payment gate is not changed automatically.
- A cancelled AutoCount invoice is flagged for review.
- If a payment is reversed before commission is paid, AutoCount sync can move the project back to Partial/Awaiting.
- If commission is already PAID and AutoCount later shows outstanding, the historical payment is NOT auto-reversed; the project is flagged REVIEW REQUIRED.
- A draft Payment Batch cannot be marked PAID if any project is no longer Fully Paid.
- One AutoCount invoice number can only be linked to one project.

AUTOMATIC SYNC / CPANEL CRON
----------------------------
A manual Sync Now and Sync Linked Projects Now are included.

For automatic checking even when nobody is logged in, create a cPanel Cron Job every 5 minutes using the command shown inside:
  Administration > Integrations > Automatic Sync

The cron script is:
  cron/autocount-sync.php

The script respects the Sync Interval saved in Integrations, and syncs up to 50 oldest-linked projects per run to avoid excessive API requests.

FILES / STORAGE
---------------
The API Key is encrypted using a server-side key created automatically at:
  storage/integration-secret.key

Do not share or delete that file after AutoCount credentials have been saved. Include storage/ when backing up/migrating the system.

PHP
---
Target / tested syntax: PHP 8.4.
