Azure D365 Replica Portal - cPanel PHP Project
==============================================

Purpose
-------
This project connects D365 F&O BYOD Azure SQL data to a cPanel MySQL replica and displays it in a professional PHP dashboard.

Current module included:
- Customers from Azure SQL table dbo.CustCustomerV3Staging

Future-ready structure:
- All local MySQL tables start with Azure_D365_
- Module registry table included: Azure_D365_Module_Config
- Saved views table included: Azure_D365_Saved_Views
- Sync log/state tables included for full and incremental sync

Required PHP extensions
-----------------------
1. mysqli
2. sqlsrv

If cPanel does not have sqlsrv, ask hosting support to enable sqlsrv / pdo_sqlsrv for your PHP version.
Without sqlsrv, direct Azure SQL to cPanel sync will not work. In that case, use an Azure API bridge.

Installation
------------
1. Upload folder public_html/d365_portal into your cPanel public_html.
   Final path should be:
   /home/YOUR_CPANEL_USERNAME/public_html/d365_portal

2. Upload folder d365_private outside public_html.
   Final path should be:
   /home/YOUR_CPANEL_USERNAME/d365_private

3. Import sql/Azure_D365_schema.sql into your cPanel database:
   r1uvoa3o_D365_Replica

4. Open /home/YOUR_CPANEL_USERNAME/d365_private/config.php and confirm:
   - MySQL credentials
   - Azure SQL server, database, user, password
   - Timezone Asia/Dubai

5. Open portal:
   https://YOUR_DOMAIN.com/d365_portal/

6. Run first full sync:
   https://YOUR_DOMAIN.com/d365_portal/api/sync_customers.php?mode=full

7. Then use incremental sync:
   https://YOUR_DOMAIN.com/d365_portal/api/sync_customers.php?mode=incremental

Suggested cPanel cron jobs
--------------------------
Full sync once daily at night:
0 2 * * * /usr/local/bin/php /home/YOUR_CPANEL_USERNAME/public_html/d365_portal/api/sync_customers.php mode=full >/dev/null 2>&1

Incremental sync every 30 minutes:
*/30 * * * * /usr/local/bin/php /home/YOUR_CPANEL_USERNAME/public_html/d365_portal/api/sync_customers.php mode=incremental >/dev/null 2>&1

If your PHP CLI does not pass arguments to $_GET, use curl in cron instead:
*/30 * * * * curl -s "https://YOUR_DOMAIN.com/d365_portal/api/sync_customers.php?mode=incremental" >/dev/null 2>&1

Important security note
-----------------------
The config file contains database passwords. Keep d365_private outside public_html.
After testing, change the Azure SQL password because it was shared during setup.

Incremental sync behavior
-------------------------
Incremental mode reads Azure SQL records where SYNCSTARTDATETIME is newer than the last successful sync time. It upserts changed customers into Azure_D365_Customers.

For deleted customers, BYOD incremental exports may not automatically remove rows from the local MySQL replica. If you need delete handling, add a periodic full reconciliation job or a D365 delete/status field strategy.
