Your IP : 216.73.217.79


Current Path : /var/www/cesa.co.za/crons/
Upload File :
Current File : /var/www/cesa.co.za/crons/update-qms-cert-status.php

<?php
/**
 * Cron job to update QMSISOCert status in tblMemberFirms based on MSD verification
 * 
 * This script checks if member firms have a verified QMS (Quality Management System)
 * in the MSD (Member Self Declaration) system. If a firm has a verified QMS
 * (ADQID=2 with Verified='Y' or 'C'), it updates tblMemberFirms.QMSISOCert to 'Y'.
 * 
 * Run this daily or as needed to keep QMS certification status in sync with MSD verifications.
 * 
 * @author System
 */

if (!isset($_SERVER['DOCUMENT_ROOT']) || strlen($_SERVER['DOCUMENT_ROOT']) <= 0) {
    $_SERVER['DOCUMENT_ROOT'] = str_replace('/crons', '', getcwd());
}

include_once($_SERVER['DOCUMENT_ROOT'] . "/echasl23/ewcfg7.php");
include_once($_SERVER['DOCUMENT_ROOT'] . "/echasl23/ewmysql7.php");
include_once($_SERVER['DOCUMENT_ROOT'] . "/php/includes/ADQFirms.php");
include_once($_SERVER['DOCUMENT_ROOT'] . "/php/includes/ErrorLog.php");

// $sqlf is initialized in ewmysql7.php

// Set to true for dry-run (test mode) - shows what would be updated without making changes
$DRY_RUN = true;

// Log start
ErrorLog::logInfo('Starting QMS certification status update from MSD verifications' . ($DRY_RUN ? ' (DRY RUN MODE)' : ''));

// Initialize ADQFirms class
$adqFirms = new ADQFirms($sqlf);

if ($DRY_RUN) {
    // Dry-run mode: show what would be updated
    $preview = $adqFirms->syncQMSISOCertStatus(true);
    
    echo "=== DRY RUN MODE - No database changes will be made ===\n\n";
    
    echo "Firms to update QMSISOCert to 'Y' (verified QMS found):\n";
    echo str_repeat("-", 80) . "\n";
    if (count($preview['toUpdate']) > 0) {
        printf("%-10s %-50s %-10s %-10s\n", "FirmID", "Firm Name", "Current", "New");
        echo str_repeat("-", 80) . "\n";
        foreach ($preview['toUpdate'] as $firm) {
            printf("%-10d %-50s %-10s %-10s\n", 
                $firm['FirmID'], 
                substr($firm['FirmName'], 0, 50),
                $firm['CurrentStatus'] ?: 'NULL',
                $firm['NewStatus']
            );
        }
        echo "\nTotal: " . count($preview['toUpdate']) . " firms\n\n";
    } else {
        echo "No firms to update.\n\n";
    }
    
    echo "Firms to reset QMSISOCert to 'N' (no verified QMS found):\n";
    echo str_repeat("-", 80) . "\n";
    if (count($preview['toReset']) > 0) {
        printf("%-10s %-50s %-10s %-10s\n", "FirmID", "Firm Name", "Current", "New");
        echo str_repeat("-", 80) . "\n";
        foreach ($preview['toReset'] as $firm) {
            printf("%-10d %-50s %-10s %-10s\n", 
                $firm['FirmID'], 
                substr($firm['FirmName'], 0, 50),
                $firm['CurrentStatus'] ?: 'NULL',
                $firm['NewStatus']
            );
        }
        echo "\nTotal: " . count($preview['toReset']) . " firms\n\n";
    } else {
        echo "No firms to reset.\n\n";
    }
    
    $totalChanges = count($preview['toUpdate']) + count($preview['toReset']);
    $summary = "DRY RUN SUMMARY: {$totalChanges} firms would be updated (" . count($preview['toUpdate']) . " to 'Y', " . count($preview['toReset']) . " to 'N'), {$preview['skipped']} skipped (no change needed)";
    echo $summary . "\n";
    ErrorLog::logInfo($summary);
} else {
    // Actual update mode
    $stats = $adqFirms->syncQMSISOCertStatus(false);
    
    // Log summary
    $summary = "QMS certification status update completed. Updated: {$stats['updated']}, Skipped: {$stats['skipped']}, Errors: {$stats['errors']}";
    ErrorLog::logInfo($summary);
    echo $summary . "\n";
}