<?php

/*******************************************************************************
 * Partnereknek küld ki automatikus levelet
 *
 * Roussis kiszedve: 2016.01.12. F.Zs.
 * 3=>array('name'=>'Mr. Vassilios Roussis','email'=>'v.roussis@it-concept.at','country'=>'41,43,1257,49,1414,30'),
 ******************************************************************************/
if (!isset($config)) {
  require_once('config.php');
  require_once('libraries/phpmailer/PHPMailerAutoload.php');
}

$time = time();
$arrPartners = array(
  0 => array('name' => 'Mr. Libor Čermák', 'email' => 'info@archline.cz', 'country' => '42,421,1403'),
  1 => array('name' => 'CADLINE Software srl', 'email' => 'segreteria@cadlinesw.com', 'country' => '39'),
  2 => array('name' => 'Wannes Malaise', 'email' => 'ornatadesign@hotmail.com', 'country' => '31,32'),
  //  3=>array('name'=>'Mr. Moore','email'=>'larry@byggdata.no','country'=>'47,1376'),
);


if (date('H:i:s', $time) >= '13:15:00' && date('H:i:s', $time) <= '13:15:05') {
  foreach ($arrPartners as $partner) {
    $msg = "";
    $msh->query("
      SELECT h.nEventID,h.nUserID,u.strName,u.strTel,u.strMobil,e.email AS strEmail,u.nCtrID,c.ctrNameEng,u.strZip,u.strCity,u.strStreet,h.nTopicID,t.strTopic_Eng,h.dateEvent,h.strPlace,h.strInfo,DATE(h.insertDate) AS insertDate,h.nManagerID
      FROM " . CRMADATBAZIS . ".u_history h
      LEFT OUTER JOIN " . CRMADATBAZIS . ".users u ON u.nUserID=h.nUserID
      LEFT OUTER JOIN " . CRMADATBAZIS . ".countries c ON u.nCtrID=c.ctrID
      LEFT OUTER JOIN " . CRMADATBAZIS . ".u_topic t ON h.nTopicID=t.nTopicID
      LEFT OUTER JOIN (SELECT email,nUserID FROM " . CRMADATBAZIS . ".u_emails WHERE active=1) e ON u.nUserID=e.nUserID
      WHERE u.nCtrID IN (" . $partner['country'] . ") AND DATE(h.insertDate)='" . date('Y-m-d', $time - 86400) . "'
      ORDER BY nEventID ASC
      LIMIT 999;");
    $res = $msh->fetchAssoc();

    if (count($res)) {
      $msg = "<p>Dear " . $partner['name'] . ",<br /><br />Please find the details from people who did any action on our website www.archlinexp.com:<br />(Program download, read letters, etc.)</p>
            <table border=1 cellspacing=1 cellpadding=1>
              <tr><th>Row</th><th>Name</th><th>Phone</th><th>Mobil</th><th>Email</th><th>Country</th><th>Zip</th><th>City</th><th>Street</th><th>Event date</th><th>Topic</th><th>Info</th></tr>";
      foreach ($res as $key => $row) {
        $msg .= "<tr><td>" . ($key + 1) . "</td><td>" . $row['strName'] . "&nbsp;</td><td>" . $row['strTel'] . "&nbsp;</td><td>" . $row['strMobil'] . "&nbsp;</td><td>" . $row['strEmail'] . "&nbsp;</td><td>" . $row['ctrNameEng'] . "&nbsp;</td><td>" . $row['strZip'] . "&nbsp;</td><td>" . $row['strCity'] . "&nbsp;</td><td>" . $row['strStreet'] . "&nbsp;</td><td>" . $row['dateEvent'] . "</td><td>" . $row['strTopic_Eng'] . "&nbsp;</td><td>" . $row['strInfo'] . "&nbsp;</td></tr>";
      }
      $msg .= "</table><p>Please contact the user directly.</p><p>This email is generated automatically by the CRM system.</p><p>CADLine Kft.</p>";

      $mail = new PHPMailer;
      $mail->CharSet = 'UTF-8';
      //    $mail->isSendmail();
      $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($partner['email']);
      $mail->addBCC('ztoth@cadline.hu');
      $mail->addBCC('webdeveloper@cadline.hu');
      $mail->Subject = 'User action report';
      $mail->msgHTML($msg);
      $mail->AltBody = strip_tags($msg);
      $mail->send();
      unset($mail);
    }
  }
  unset($msg);
}
