<?php

/*******************************************************************************
 * A file download-ok     
 ******************************************************************************/
if (!isset($config)) {
  require_once('config.php');
  require_once('libraries/phpmailer/PHPMailerAutoload.php');
}

$months_before = 6;
$nManagerID = 36;
$strPlace = 'web';

if (date('H', time()) == '12') {
  // magyar summ  
  $msh->query("SELECT nUserStatus,COUNT(*) AS darab FROM (
    SELECT u.nUserID,u.strName,u.nUserStatus,h.changeDate FROM users u
    LEFT OUTER JOIN (SELECT nUserID,MAX(changeDate) AS changeDate FROM u_userstatus_history WHERE val=4 GROUP BY nUserID ORDER BY nUserID) h ON h.nUserID=u.nUserID
    WHERE u.old_db='clusers' AND u.nUserStatus=4 AND h.changeDate IS NOT NULL AND h.changeDate<=DATE_ADD(CURRENT_TIMESTAMP,interval-6 MONTH)
    UNION
    SELECT u.nUserID,u.strName,u.nUserStatus,h.changeDate FROM users u
    LEFT OUTER JOIN (SELECT nUserID,MAX(changeDate) AS changeDate FROM u_userstatus_history WHERE val=5 GROUP BY nUserID ORDER BY nUserID) h ON h.nUserID=u.nUserID
    WHERE u.old_db='clusers' AND u.nUserStatus=5 AND h.changeDate IS NOT NULL AND h.changeDate<=DATE_ADD(CURRENT_TIMESTAMP,interval-6 MONTH) 
    UNION
    SELECT u.nUserID,u.strName,u.nUserStatus,h.changeDate FROM users u
    LEFT OUTER JOIN (SELECT nUserID,MAX(changeDate) AS changeDate FROM u_userstatus_history WHERE val=6 GROUP BY nUserID ORDER BY nUserID) h ON h.nUserID=u.nUserID
    WHERE u.old_db='clusers' AND u.nUserStatus=6 AND h.changeDate IS NOT NULL AND h.changeDate<=DATE_ADD(CURRENT_TIMESTAMP,interval-6 MONTH)
    UNION
    SELECT u.nUserID,u.strName,u.nUserStatus,h.changeDate FROM users u
    LEFT OUTER JOIN (SELECT nUserID,MAX(changeDate) AS changeDate FROM u_userstatus_history WHERE val=8 GROUP BY nUserID ORDER BY nUserID) h ON h.nUserID=u.nUserID
    WHERE u.old_db='clusers' AND u.nUserStatus=8 AND h.changeDate IS NOT NULL AND h.changeDate<=DATE_ADD(CURRENT_TIMESTAMP,interval-6 MONTH)
    UNION
    SELECT u.nUserID,u.strName,u.nUserStatus,h.changeDate FROM users u
    LEFT OUTER JOIN (SELECT nUserID,MAX(changeDate) AS changeDate FROM u_userstatus_history WHERE val=9 GROUP BY nUserID ORDER BY nUserID) h ON h.nUserID=u.nUserID
    WHERE u.old_db='clusers' AND u.nUserStatus=9 AND h.changeDate IS NOT NULL AND h.changeDate<=DATE_ADD(CURRENT_TIMESTAMP,interval-6 MONTH)
    ORDER BY nUserStatus
  ) y  GROUP BY nUserStatus;");
  $hun = $msh->fetchAssoc();

  // angol summ
  $msh->query("SELECT nUserStatus,COUNT(*) AS darab FROM (
    SELECT u.nUserID,u.strName,u.nUserStatus,h.changeDate FROM users u
    LEFT OUTER JOIN (SELECT nUserID,MAX(changeDate) AS changeDate FROM u_userstatus_history WHERE val=4 GROUP BY nUserID ORDER BY nUserID) h ON h.nUserID=u.nUserID
    WHERE u.old_db='clusers_eng' AND u.nUserStatus=4 AND h.changeDate IS NOT NULL AND h.changeDate<=DATE_ADD(CURRENT_TIMESTAMP,interval-6 MONTH)
    UNION
    SELECT u.nUserID,u.strName,u.nUserStatus,h.changeDate FROM users u
    LEFT OUTER JOIN (SELECT nUserID,MAX(changeDate) AS changeDate FROM u_userstatus_history WHERE val=5 GROUP BY nUserID ORDER BY nUserID) h ON h.nUserID=u.nUserID
    WHERE u.old_db='clusers_eng' AND u.nUserStatus=5 AND h.changeDate IS NOT NULL AND h.changeDate<=DATE_ADD(CURRENT_TIMESTAMP,interval-6 MONTH) 
    UNION
    SELECT u.nUserID,u.strName,u.nUserStatus,h.changeDate FROM users u
    LEFT OUTER JOIN (SELECT nUserID,MAX(changeDate) AS changeDate FROM u_userstatus_history WHERE val=6 GROUP BY nUserID ORDER BY nUserID) h ON h.nUserID=u.nUserID
    WHERE u.old_db='clusers_eng' AND u.nUserStatus=6 AND h.changeDate IS NOT NULL AND h.changeDate<=DATE_ADD(CURRENT_TIMESTAMP,interval-6 MONTH)
    UNION
    SELECT u.nUserID,u.strName,u.nUserStatus,h.changeDate FROM users u
    LEFT OUTER JOIN (SELECT nUserID,MAX(changeDate) AS changeDate FROM u_userstatus_history WHERE val=8 GROUP BY nUserID ORDER BY nUserID) h ON h.nUserID=u.nUserID
    WHERE u.old_db='clusers_eng' AND u.nUserStatus=8 AND h.changeDate IS NOT NULL AND h.changeDate<=DATE_ADD(CURRENT_TIMESTAMP,interval-6 MONTH)
    UNION
    SELECT u.nUserID,u.strName,u.nUserStatus,h.changeDate FROM users u
    LEFT OUTER JOIN (SELECT nUserID,MAX(changeDate) AS changeDate FROM u_userstatus_history WHERE val=9 GROUP BY nUserID ORDER BY nUserID) h ON h.nUserID=u.nUserID
    WHERE u.old_db='clusers_eng' AND u.nUserStatus=9 AND h.changeDate IS NOT NULL AND h.changeDate<=DATE_ADD(CURRENT_TIMESTAMP,interval-6 MONTH)
    ORDER BY nUserStatus
  ) y  GROUP BY nUserStatus;");
  $eng = $msh->fetchAssoc();

  // Visszasorolás
  $msh->query("
    SELECT u.nUserID,u.strName,u.nUserStatus,h.changeDate FROM users u
    LEFT OUTER JOIN (SELECT nUserID,MAX(changeDate) AS changeDate FROM u_userstatus_history WHERE val=4 GROUP BY nUserID ORDER BY nUserID) h ON h.nUserID=u.nUserID
    WHERE u.nUserStatus=4 AND h.changeDate IS NOT NULL AND h.changeDate<=DATE_ADD(CURRENT_TIMESTAMP,interval-6 MONTH)
    UNION
    SELECT u.nUserID,u.strName,u.nUserStatus,h.changeDate FROM users u
    LEFT OUTER JOIN (SELECT nUserID,MAX(changeDate) AS changeDate FROM u_userstatus_history WHERE val=5 GROUP BY nUserID ORDER BY nUserID) h ON h.nUserID=u.nUserID
    WHERE u.nUserStatus=5 AND h.changeDate IS NOT NULL AND h.changeDate<=DATE_ADD(CURRENT_TIMESTAMP,interval-6 MONTH) 
    UNION
    SELECT u.nUserID,u.strName,u.nUserStatus,h.changeDate FROM users u
    LEFT OUTER JOIN (SELECT nUserID,MAX(changeDate) AS changeDate FROM u_userstatus_history WHERE val=6 GROUP BY nUserID ORDER BY nUserID) h ON h.nUserID=u.nUserID
    WHERE u.nUserStatus=6 AND h.changeDate IS NOT NULL AND h.changeDate<=DATE_ADD(CURRENT_TIMESTAMP,interval-6 MONTH)
    UNION
    SELECT u.nUserID,u.strName,u.nUserStatus,h.changeDate FROM users u
    LEFT OUTER JOIN (SELECT nUserID,MAX(changeDate) AS changeDate FROM u_userstatus_history WHERE val=8 GROUP BY nUserID ORDER BY nUserID) h ON h.nUserID=u.nUserID
    WHERE u.nUserStatus=8 AND h.changeDate IS NOT NULL AND h.changeDate<=DATE_ADD(CURRENT_TIMESTAMP,interval-6 MONTH)
    UNION
    SELECT u.nUserID,u.strName,u.nUserStatus,h.changeDate FROM users u
    LEFT OUTER JOIN (SELECT nUserID,MAX(changeDate) AS changeDate FROM u_userstatus_history WHERE val=9 GROUP BY nUserID ORDER BY nUserID) h ON h.nUserID=u.nUserID
    WHERE u.nUserStatus=9 AND h.changeDate IS NOT NULL AND h.changeDate<=DATE_ADD(CURRENT_TIMESTAMP,interval-6 MONTH)
    ORDER BY nUserStatus;
    ");
  $all = $msh->fetchAssoc();

  if (count($all)) {
    foreach ($all as $key => $row) {
      $msh->query("UPDATE " . CRMADATBAZIS . ".users SET nUserStatus=3 WHERE nUserID=" . $row['nUserID'] . " LIMIT 1;");
      $msh->insert(CRMADATBAZIS . ".`u_userstatus_history`", array(
        'nUserID' => $row['nUserID'],
        'val' => 3,
        'changeDate' => date('Y-m-d H:i:s'),
        'nManagerID' => $nManagerID,
      ));
    }
  }

  // összesített levél küldése a magyar módosításokról
  if (count($hun)) {
    $msg = "<p>Helló, adminisztrátor!<br /><br />Az alábbiakban láthatod a visszasorások összesített listáját.</p>
          <table border=1 cellspacing=1 cellpadding=1>
            <tr><th>Eredeti státusz</th><th>Darab</th></tr>";
    foreach ($hun as $key => $row) {
      $msg .= "<tr><td>" . $row['nUserStatus'] . "&nbsp;</td><td>" . $row['darab'] . "&nbsp;</td></tr>";
    }
    $msg .= "</table>";

    $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');
    $mail->addAddress('webdeveloper@cadline.hu');
    $mail->Subject = 'Összesített jelentés a visszasorolásokról';
    $mail->msgHTML($msg);
    $mail->AltBody = strip_tags($msg);
    $mail->send();
    unset($mail);
  }

  // összesített levél küldése az angol módosításokról
  if (count($eng)) {
    $msg = "<p>Hello administrator,<br /><br />The following is a summary list of downgrades.</p>
          <table border=1 cellspacing=1 cellpadding=1>
            <tr><th>Original status</th><th>Count</th></tr>";
    foreach ($eng as $key => $row) {
      $msg .= "<tr><td>" . $row['nUserStatus'] . "&nbsp;</td><td>" . $row['darab'] . "&nbsp;</td></tr>";
    }
    $msg .= "</table>";

    $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@archlinexp.com');
    $mail->addAddress('webdeveloper@cadline.hu');
    $mail->Subject = 'Summary list of downgrades';
    $mail->msgHTML($msg);
    $mail->AltBody = strip_tags($msg);
    $mail->send();
    unset($mail);
  }
}
