<?php

/*******************************************************************************
 * Non-profit kódokról küld ki automatikus levelet
 ******************************************************************************/
if (!isset($config)) {
  require_once('config.php');
  require_once('libraries/phpmailer/PHPMailerAutoload.php');
}

$time = time();
$datum = (isset($_GET['datum']) ? $_GET['datum'] : date('Y-m-d', $time - 86400));
$arrKeys = array(995, 996, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989);
$arrPartners = array(
  0 => array('name' => 'Administrator', 'email' => 'info@archlinexp.com', 'bcc' => 'webdeveloper@cadline.hu', 'country' => '36,42,44,30,34,38'),
  1 => array('name' => 'Tímea Józsa', 'email' => 'info@cadline.hu', 'bcc' => '', 'country' => '36'),
  2 => array('name' => 'Erika Bogár', 'email' => array('erika.bogar@archlinexp.com'), 'bcc' => '', 'country' => '44'),
  3 => array('name' => 'Mr. Libor Čermák', 'email' => 'info@archline.cz', 'bcc' => 'webdeveloper@cadline.hu', 'country' => '42'),
);

if ((date('H:i:s', $time) >= '08:00:00' && date('H:i:s', $time) <= '08:00:10') || isset($_GET['forced'])) {
  foreach ($arrPartners as $partner) {
    $lastCountry = 0;
    $msh->query("SELECT `kulcs`,p.prPass,MIN(DATE(`datum`)) AS `elso`,h.hlCtrID,c.ctrNameEng,h.hlUser,u.strName,u.strTel,e.email
      FROM `" . CRMADATBAZIS . "`.`h_aktivalas_infok` a
      LEFT OUTER JOIN `" . CRMADATBAZIS . "`.`hardlock` h ON a.`kulcs`=h.`hlNum`
      LEFT OUTER JOIN `" . CRMADATBAZIS . "`.`h_programs` p ON a.`kulcs`=p.`prHlNum`
      LEFT OUTER JOIN `" . CRMADATBAZIS . "`.`users` u ON h.hlUser=u.nUserID
      LEFT OUTER JOIN `" . CRMADATBAZIS . "`.`countries` c ON h.hlCtrID=c.ctrID
      LEFT OUTER JOIN `" . CRMADATBAZIS . "`.`u_emails` e ON h.hlUser=e.nUserID AND e.`default`=1
      WHERE LEFT(`kulcs`,3) IN ('" . implode("','", $arrKeys) . "') AND a.kuldo LIKE 'regisztracio%' AND a.`leiras` LIKE '(megjegyzes) regisztr%' " . ($partner['country'] > '' ? " AND h.hlCtrID IN (" . $partner['country'] . ") " : "") . "
      GROUP BY `kulcs`
      HAVING MIN(DATE(`datum`))='" . $datum . "'
      ORDER BY DATE(`datum`),`kulcs` LIMIT 999;");
    $res = $msh->fetchAssoc();

    $msg = "<p>Dear " . $partner['name'] . ",<br /><br />In the tables below you can find information about users uses Non-profit codes.</p><p>Non-profit code first registration:</p>
          <table border=1 cellspacing=1 cellpadding=1>
          <tr><th>#</th><th>Hardlock</th><th>Serial</th><th>Reg. date</th><th>Country code</th><th>Country</th><th>UserID</th><th>Name</th><th>Phone</th><th>Email</th></tr>";
    foreach ($res as $key => $row) $msg .= "<tr><td>" . ($key + 1) . "</td><td>" . $row['kulcs'] . "&nbsp;</td><td>" . $row['prPass'] . "&nbsp;</td><td>" . $row['elso'] . "&nbsp;</td><td>" . $row['hlCtrID'] . "&nbsp;</td><td>" . $row['ctrNameEng'] . "&nbsp;</td><td>" . $row['hlUser'] . "&nbsp;</td><td>" . $row['strName'] . "&nbsp;</td><td>" . $row['strTel'] . "&nbsp;</td><td>" . $row['email'] . "</td></tr>";
    $msg .= "</table><p>Program starts with Non-profit code, groupped by countries:</p>
          <table border=1 cellspacing=1 cellpadding=1>
          <tr><th>#</th><th>Country code</th><th>Country</th><th>Date</th><th>Start count</th></tr>";

    $msh->query("SELECT DATE(`datum`) AS `datum`,h.hlCtrID,c.ctrNameEng,COUNT(*) AS `inditas`
      FROM `cl_hlusers`.`h_aktivalas_infok` a
      LEFT OUTER JOIN `cl_hlusers`.`hardlock` h ON a.`kulcs`=h.`hlNum`
      LEFT OUTER JOIN `cl_hlusers`.`h_programs` p ON a.`kulcs`=p.`prHlNum`
      LEFT OUTER JOIN `cl_hlusers`.`users` u ON h.hlUser=u.nUserID
      LEFT OUTER JOIN `cl_hlusers`.`countries` c ON h.hlCtrID=c.ctrID
      LEFT OUTER JOIN `cl_hlusers`.`u_emails` e ON h.hlUser=e.nUserID AND e.`default`=1
      WHERE LEFT(`kulcs`,3) IN ('" . implode("','", $arrKeys) . "') AND leiras LIKE 'programindítás%' " . ($partner['country'] > '' ? " AND h.hlCtrID IN (" . $partner['country'] . ") " : "") . "
      GROUP BY `hlCtrID`,DATE(`datum`)
      HAVING DATE(`datum`)='" . $datum . "'
      ORDER BY `hlCtrID`,DATE(`datum`);");
    $res = $msh->fetchAssoc();
    foreach ($res as $key => $row) $msg .= "<tr><td>" . ($key + 1) . "</td><td>" . $row['hlCtrID'] . "&nbsp;</td><td>" . $row['ctrNameEng'] . "&nbsp;</td><td>" . $row['datum'] . "&nbsp;</td><td>" . $row['inditas'] . "&nbsp;</td></tr>";
    $msg .= "</table><p>Summary:</p>
      <table border=1 cellspacing=1 cellpadding=1>
      <tr><th>#</th><th>Country code</th><th>Country</th><th>Registration</th><th>Start count</th></tr>";

    $msh->query("SELECT reg.hlCtrID,reg.ctrNameEng,reg.regisztracio,ind.inditas FROM (
      	SELECT h.hlCtrID,c.ctrNameEng,COUNT(*) AS `inditas`
      	FROM `cl_hlusers`.`h_aktivalas_infok` a
      	LEFT OUTER JOIN `cl_hlusers`.`hardlock` h ON a.`kulcs`=h.`hlNum`
      	LEFT OUTER JOIN `cl_hlusers`.`countries` c ON h.hlCtrID=c.ctrID
      	WHERE LEFT(`kulcs`,3) IN ('" . implode("','", $arrKeys) . "') AND leiras LIKE 'programindítás%'
      	GROUP BY h.`hlCtrID`
      	ORDER BY h.`hlCtrID`) ind
      LEFT OUTER JOIN (
      	SELECT h.hlCtrID,c.ctrNameEng,COUNT(*) AS `regisztracio`
      	FROM `cl_hlusers`.`h_aktivalas_infok` a
      	LEFT OUTER JOIN `cl_hlusers`.`hardlock` h ON a.`kulcs`=h.`hlNum`
      	LEFT OUTER JOIN `cl_hlusers`.`countries` c ON h.hlCtrID=c.ctrID
      	WHERE LEFT(`kulcs`,3) IN ('" . implode("','", $arrKeys) . "') AND a.kuldo LIKE 'regisztracio%' AND a.`leiras` LIKE '(megjegyzes) regisztr%'
      	GROUP BY h.`hlCtrID`
      	ORDER BY h.`hlCtrID`) reg ON ind.hlCtrID=reg.hlCtrID
      WHERE 1 " . ($partner['country'] > '' ? " AND reg.hlCtrID IN (" . $partner['country'] . ") " : "") . "
      ORDER BY reg.hlCtrID;");
    $res = $msh->fetchAssoc();
    foreach ($res as $key => $row) $msg .= "<tr><td>" . ($key + 1) . "</td><td>" . $row['hlCtrID'] . "&nbsp;</td><td>" . $row['ctrNameEng'] . "&nbsp;</td><td>" . $row['regisztracio'] . "&nbsp;</td><td>" . $row['inditas'] . "&nbsp;</td></tr>";
    $msg .= "</table><p>This is an automated email. Please DO NOT reply.</p>";

    //    if ($_SERVER['REMOTE_ADDR']=='188.6.239.155') print $msg."<br /><br />";

    $mail = new PHPMailer;
    $mail->CharSet = 'UTF-8';
    $mail->isSMTP();
    $mail->Host = $mailconfig['Host'];
    $mail->SMTPAuth = true;
    $mail->Username = $mailconfig['Username'];
    $mail->Password = $mailconfig['Password'];
    $mail->SMTPSecure = $mailconfig['SMTPSecure'];
    $mail->Port = $mailconfig['Port'];
    $mail->isHTML(TRUE);
    $mail->setFrom('info@archline.hu');
    if (is_array($partner['email'])) foreach ($partner['email'] as $m) $mail->addAddress($m);
    else $mail->addAddress($partner['email']);
    if (is_array($partner['bcc'])) {
      foreach ($partner['bcc'] as $m) $mail->addBCC($m);
    } else {
      if ($partner['bcc'] > '') $mail->addBCC($partner['bcc']);
    }
    $mail->Subject = 'Non-profit report - ' . $datum;
    $mail->msgHTML($msg);
    $mail->AltBody = strip_tags($msg);
    $mail->send();
    unset($mail);
  }
  unset($msg);
}
