<?php

/*******************************************************************************
 * A cl_history t�bl�ba �sszegy�lt bejegyz�seket viszi �t a crm rendszerbe   
 ******************************************************************************/
if (!isset($config)) {
  require_once('config.php');
}

if (!isset($jmdb)) {
  $jmdb = ((isset($_GET['jmdb']) && in_array($_GET['jmdb'], array('jml', 'eng'))) ? $_GET['jmdb'] : 'jml');
}

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

$msh2->query("SELECT * FROM cl_history WHERE nUserID IS NULL OR nEventID IS NULL;");
$res = $msh2->fetchAssoc();
if (!empty($res)) {
  foreach ($res as $row) {
    $nUserID = 0;

    //  ha ki van t�ltve a user_id mez� (be volt jelentkezve a weboldalon)
    if ((int)$row['user_id'] > 0) {
      //    joomla user t�bl�b�l kikeresi a crm-ben haszn�lt id-t
      $msh2->query("SELECT * FROM jml_users WHERE id=" . (int)$row['user_id'] . ";");
      $user = $msh2->fetchAssoc();
      $nUserID = $user[0]['nUserID'];

      //    ha nem tal�lja, akkor email alapj�n keresi a crm rendszerben
      if ((int)$nUserID == 0) {
        $msh->query("SELECT * FROM cl_hlusers.u_emails WHERE email='" . $row['email'] . "';");
        $user = $msh->fetchAssoc();
        if (!empty($user)) {
          //        ha email alapj�n megtal�lja a crm-ben, akkor kit�lti a hi�nyz� crm id-t a joomla user t�b�ban  
          $nUserID = $user[0]['nUserID'];
          if ((int)$nUserID > 0) {
            $msh2->update("jml_users", array('nUserID' => (int)$nUserID), array('id' => (int)$row['user_id']));
          }
        }
      }
    }
    //  ha nincs kit�ltve a user_id mez� (nem volt bejelentkezve a weboldalon)
    else {
      //    r�keres crm-ben, hogy van-e m�r olyan email, amit megadott
      $msh->query("SELECT * FROM cl_hlusers.u_emails WHERE email='" . $row['email'] . "' LIMIT 1;");
      $user = $msh->fetchAssoc();
      if (!empty($user)) {
        $nUserID = (int)$user[0]['nUserID'];
      }
    }

    //  ha nem tal�lta meg a crm-ben haszn�lt id-t, akkor l�trehoz egy usert a crm-ben 
    if ((int)$nUserID == 0) {
      $msh->insert("`cl_hlusers`.`users`", array(
        'strName' => $row['name'],
        'strEMail' => $row['email'],
        'nManagerID' => $nManagerID,
        'dateInsert' => $row['date_insert'],
        'deleted' => 0,
        'old_db' => $row['old_db'], //'clusers'.($jmdb=='eng' ? '_eng' : ''),
      ));
      $nUserID = $msh->getInsertedId();

      if ((int)$nUserID > 0) {
        $msh->insert("`cl_hlusers`.`u_emails`", array(
          'email' => $row['email'],
          'nUserID' => $nUserID,
          'default' => 0,
          'active' => 1,
          'deleted' => 0,
        ));
      }
    }

    //  ha megvan crm-ben haszn�lt id, akkor be�rja a u_history t�bl�ba az esem�nyt,
    if ((int)$nUserID > 0) {
      $msh->insert("`cl_hlusers`.`u_history`", array(
        "nUserID" => $nUserID,
        "nTopicID" => $row['topic_id'],
        "dateEvent" => substr($row['date_event'], 0, 10),
        "strPlace" => $strPlace,
        "strInfo" => $row['info'],
        "nManagerID" => $nManagerID,
        "insertDate" => $row['date_insert'],
      ));
      $nEventID = $msh->getInsertedId();

      //    �s a cl_history-ba vissza�rja az �jonan kapott nEventID-t       
      $msh2->update("cl_history", array('nUserID' => $nUserID, 'nEventID' => $nEventID), array('id' => $row['id']));
    }
  }
}
