<?php
// minden nap 6:00-kor			- hibás programindítások listája

if (!class_exists('crm_hardlock')) require_once("common_cadline_libraries/crm_hardlock.class.php");
require_once('config.mail.php');
require_once('libraries/phpmailer/PHPMailerAutoload.php');

$tegnap = date('Y-m-d', time() - (60 * 60 * 24));
$sql = "SELECT pwd, error FROM " . CRMADATBAZIS . ".`hibajelento` WHERE substring(datum, 1, 10) = '{$tegnap}' GROUP BY pwd ORDER BY pwd ASC";
crm_database::getInstance()->query($sql);
$kulcsokobjarray = crm_database::getInstance()->fetchAssoc();
$kulcsok = array();

foreach ($kulcsokobjarray as $obj) {
    $object = (object)$obj;
    $ctrName = "ures";
    crm_database::getInstance()->query("SELECT hlCtrID, hlUser FROM " . CRMADATBAZIS . ".hardlock WHERE hlNum='" . substr($object->pwd, 0, 6) . "'");
    $hardlock = crm_database::getInstance()->fetchNext();
    $h = (object)$hardlock;

    if ($h->hlCtrID) {
        crm_database::getInstance()->query("SELECT ctrName FROM " . CRMADATBAZIS . ".countries WHERE ctrID=" . $h->hlCtrID);
        $orszag = crm_database::getInstance()->fetchNext();
        $o = (object)$orszag;
        $ctrName = $o->ctrName;
    }
    if (is_numeric($h->hlUser)) {
        crm_database::getInstance()->query("SELECT strName FROM " . CRMADATBAZIS . ".users WHERE nUserID=" . $h->hlUser);
        $user = crm_database::getInstance()->fetchNext();
        $u = (object)$user;
        $user = $u->strName;
    } else
        $user = $h->hlUser;

    $hibahun = $hibaeng = "";
    if (strstr($object->error, "error: nincs ilyen password")) {
        $hibahun = "nincs ilyen Sorozatszám";
        $hibaeng = "no Serial code in CRM database";
        $support = "Check user figures and send order to CadLine";
    } elseif (strstr($object->error, "error: nem aktiv a program")) {
        $hibahun = "nem aktiv a program";
        $hibaeng = "this Serial code is not active";
        $support = "Check why? (Maybe prohibited, but old hardlock was not sent back to office.)";
    } elseif (strstr($object->error, "error: nincs vasarlasi idopont")) {
        $hibahun = "nincs vasarlasi idopont a CRM-ben";
        $hibaeng = "no selling date setup in database";
        $support = "Check the User payments and ask new activation code from CadLine";
    } elseif (strstr($object->error, "error: hianyzo kod")) {
        $hibahun = "hianyzo Aktiválási kod";
        $hibaeng = "no Activation code in CRM";
        $support = "Check the User payments and ask new activation code from CadLine";
    } elseif (strstr($object->error, "error: masik gep van regisztralva")) {
        $hibahun = "masik gep van regisztralva - Feloldás kell";
        $hibaeng = "this is already registered on another PC";
        $support = "Check it, maybe user tried to download more times. <b>If its OK, ask to release from CadLine (after that user can register again and can use for more 60 days!)</b> Its your decision.";
    } elseif (strstr($object->error, "error: nincs kifizetve")) {
        $hibahun = "nincs kifizetve, aktiválási kód lejárt";
        $hibaeng = "the program is not paid in CRM system";
        $support = "Check the payment and ask new activation from CadLine";
    } elseif (strstr($object->error, "error: tul sok telepites")) {
        $hibahun = "túl sok telepités - Feloldás kell";
        $hibaeng = "the program already registered ";
        $support = "Check, discuss with user why tried it twice, and <b>If its OK, ask to release from CadLine (after that user can register again and can use for more 60 days!)</b> Its your decision.";
    }

    preg_match('/\d{16}/', $object->error, $serialmatch);

    crm_database::getInstance()->query("SELECT * FROM " . CRMADATBAZIS . ".`h_programs` WHERE `prPass` = '" . $object->pwd . "' ORDER BY `prID` DESC LIMIT 1");
    $arrDealer = crm_database::getInstance()->fetchAssoc();

    $ujhibaskulcs = array();
    $ujhibaskulcs['kulcs']   = $object->pwd;
    $ujhibaskulcs['serial']  = $serialmatch[0];
    $ujhibaskulcs['ctrName'] = $ctrName;
    $ujhibaskulcs['user']    = $user;
    $ujhibaskulcs['hibahun'] = $hibahun;
    $ujhibaskulcs['hibaeng'] = $hibaeng;
    $ujhibaskulcs['support'] = $support;
    $ujhibaskulcs['dealer'] = $arrDealer[0]['prDealer'];
    $kulcsok[]   = $ujhibaskulcs;
}

$htmlkulcsok = "<table>\n";
$htmlkulcsok .=  "<tr>\n";
$htmlkulcsok .=  "\t<th>Hardlock No</th>\n";
$htmlkulcsok .=  "\t<th>Serial No</th>\n";
$htmlkulcsok .=  "\t<th>Country</th>\n";
$htmlkulcsok .=  "\t<th>Dealer</th>\n";
$htmlkulcsok .=  "\t<th>User name</th>\n";
$htmlkulcsok .=  "\t<th>error message</th>\n";
//$htmlkulcsok .=  "\t<th>hibaüzenet</th>\n";
$htmlkulcsok .=  "\t<th>How should you support ?</th>\n";
$htmlkulcsok .=  "</tr>\n";

foreach ($kulcsok as $kulcs) {
    $htmlkulcsok .=  "<tr>\n";
    $htmlkulcsok .=  "\t<td>{$kulcs['kulcs']}</td>\n";
    $htmlkulcsok .=  "\t<td>{$kulcs['serial']}</td>\n";
    $htmlkulcsok .=  "\t<td>{$kulcs['ctrName']}</td>\n";
    $htmlkulcsok .=  "\t<td>{$kulcs['dealer']}</td>\n";
    $htmlkulcsok .=  "\t<td>{$kulcs['user']}</td>\n";
    $htmlkulcsok .=  "\t<td>{$kulcs['hibaeng']}</td>\n";
    //$htmlkulcsok .=  "\t<td>{$kulcs['hibahun']}</td>\n";
    $htmlkulcsok .=  "\t<td>{$kulcs['support']}</td>\n";
    $htmlkulcsok .=  "</tr>\n";
}
$htmlkulcsok .= "</table>";

// $kulcslista = implode(", ",$kulcsok);

$message = "Ez egy automatikusan generált CRM rendszer üzenet. Azokról a kulcsokról, melyeknél tegnap ({$tegnap}) olyan programindítás történt, amire hiba volt a válasz:<br /><br />
    Dear Administrator,<br /><br />
    According to our system report please find the errors from yesterday below." .
    $htmlkulcsok .= "<br /><br />Best Regards,<br /><br />Anikó Huszár";

$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->CharSet = 'UTF-8';
$mail->Host = $mailconfig['Host'];
$mail->Username = $mailconfig['Username'];
$mail->Password = $mailconfig['Password'];
$mail->SMTPSecure = $mailconfig['SMTPSecure'];
$mail->Port = $mailconfig['Port'];
$mail->isHTML(TRUE);
$mail->setFrom(DEFAULT_EMAIL);
// $mail->addAddress('webdeveloper@cadline.hu');
// $mail->addAddress('aniko.huszar@cadline.hu');
$mail->addAddress('office@cadline.hu');
$mail->Subject = 'Hibas programinditasok';
$mail->msgHTML($message);
$mail->AltBody = strip_tags($message);
$mail->send();
unset($mail);

/*
// 2017.06.07. F.Zs.: levélküldő objektum lecserélve, mert nem működött
		$conf = new JConfig();
		$email = new CI_Email();
		$email->initialize();
		$email->setFrom('info@cadline.hu', 'CadLine');
		$email->subject('Hibas programinditasok');
		$email->message($message);
		$email->send('aniko.huszar@cadline.hu', 'smtp');
*/
