<?php
/**
 * Created by PhpStorm.
 * User: sunusi
 * Date: 2019-05-05
 * Time: 13:38
 */
$patients = array();
foreach (getPatientsDetails() as $arr) {
//    echo $arr["patientId"];
    $pay_result = array();
    foreach (getPaymentsDetails($arr["patientId"]) as $pay_arr) {
        $pay_result = array_merge($pay_result, [$pay_arr]);
    }
    $arr["payments"] = $pay_result;
    $patients = array_merge($patients, [$arr]);
}

if (!empty($patients)) {
    $curl_response = get_web_page("https://www.uduthctppp.com/api/sync/patients/", $patients);
//print($curl_response);
    $arr_patientId = explode("|", $curl_response);
//    print_r($arr_patientId);
    $arr_patientId = array_filter($arr_patientId);
    $count = 0;
    $ack = false;
    for ($i = 0; $i < count($arr_patientId); $i++) {
        //updating patient records
//        echo $arr_patientId[$i];
        if (!empty(trim($arr_patientId[$i]))) {
            $count = $count + 1;
            if (!$conn->query("UPDATE udut_patients SET patientSyncStatus = '1' WHERE patientId = '" . $arr_patientId[$i] . "' ")) {
                die("Error updating the local database " . $conn->error);
            } else {
                $ack = true;
            }
        }
    }
    if ($ack == true) {
        print($count . " Records Synced successful<br>");
    }
} else {
    print("No new patients record<br>");
}

?>
