<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

// include '../../vendor/autoload.php';
// include '../../common/generalWs.php';
include '/var/www/html/utilities/cron_hunity/vendor/autoload.php';
include '/var/www/html/utilities/cron_hunity/common/generalWs.php';

$obj_function = new coFunction();
$obj_bdmysql = new coBdmysql();
$conn = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNOM); /* Database connection end */
$conn->set_charset('utf8mb4');
session_start();

$date = new DateTime();
$date = $date->format('Y-m-d');

$ws_path = "https://wshunitydev.gosmartcrm.com:4000/ws";
$jsondebug = "/var/www/html/utilities/cron_hunity/cron/jsondebug.log";
//$jsondebug = "jsondebug.log";

error_log("\n".date('d/m/Y h:i:s')."\n".print_r("Ejecución Cron 2024",true), 3, $jsondebug);

$sql = "SELECT naa.*, ap.id as id_application, pd.payment_date FROM noti_app_anulcancel naa 
        INNER JOIN payment_due pd ON pd.id = naa.id_payment_due 
        INNER JOIN application ap ON ap.id = pd.id_application 
        WHERE naa.date_notification <= '$date' AND (naa.status = 1 OR naa.status = 3)";
$query = $obj_bdmysql->query($sql, $conn);

//print_r($sql);exit;

error_log("\n".date('d/m/Y h:i:s')."\n".print_r($query,true), 3, $jsondebug);

if(is_array($query)){
    foreach($query as $value){
        $id_noti_app_anulcancel = $value['id'];
        $id_payment_due = $value['id_payment_due'];
        $type = $value['type'];
        $date_notification = $value['date_notification'];
        $note = $value['note'];
        $id_application = $value['id_application'];
        $payment_date = $value['payment_date'];
        $template = "";
        $days = "";

        if ($type == 1 OR $type == 2) {
            $template = 5;
            $days = $type == 1 ? 60 : 30;
        }else if($type == 3){
            $template = 6;
        }

        $url = $ws_path."/payment/sendnotificationcronjob";
        $jsonData = [
            "id_payment_due" => $id_payment_due,
            "template" => $template,
            "days" => $days
        ];
        $curl_notification = $obj_function->curlSetOpt($url, $jsonData);  
        //print_r($curl_notification);exit;
        if ($curl_notification['code'] == 200) {

            if ($type == 3) {
                $campo = "id_status = 4";
                $where = "id = $id_application";
                $obj_bdmysql->update("application", $campo, $where, $conn);

                $campo = "status = 9";
                $where = "id_application = $id_application AND status != 2";
                $obj_bdmysql->update("payment_due", $campo, $where, $conn);

                $campo = "id_application, id_status_app, note";
                $valor = "$id_application, 4, 'Cancelada automáticamente después de 90 días sin pagar una cuota.'";
                $obj_bdmysql->insert("history_app_status", $campo, $valor, $conn);

                $sql = "UPDATE noti_app_anulcancel naa 
                        INNER JOIN payment_due pd ON pd.id = naa.id_payment_due 
                        SET naa.status = 4 
                        WHERE pd.id_application = $id_application";
                $query = $obj_bdmysql->query($sql, $conn);
            }

            $campo = "status = 2"; 
            $where = "id = $id_noti_app_anulcancel";
            $query = $obj_bdmysql->update("noti_app_anulcancel", $campo, $where, $conn);

        }else{
            $campo = "status = 3"; 
            $where = "id = $id_noti_app_anulcancel";
            $query = $obj_bdmysql->update("noti_app_anulcancel", $campo, $where, $conn);
        }
    }
}

?>
