<?php
if ($_SERVER['REMOTE_ADDR'] != '46.139.14.111') {
  die();
}
die();

mb_internal_encoding("UTF-8");

include("config.php");

$arrFiles = array("leads/Letoltott_szamlazz.hu_kimeno_szamlak_2018_Erika.csv");

foreach ($arrFiles as $file) {
  foreach (file($file) as $k => $sor) {
    if ($k < 1) continue;

    $mezo = explode(";", $sor);
    $emails = explode(";", $mezo[8]);
    if (is_array($emails) && count($emails)) {
      $email = trim($emails[0]);
      $msh->query("SELECT DISTINCT(nUserID) FROM `" . CRMADATBAZIS . "`.`u_emails` WHERE `email`='" . $email . "';");
      $user = $msh->fetchAssoc();
      if (!is_array($user) || count($user) == 0) {
        $tomb = array('hiba' => 'nem talált usert az email címhez: ' . $email);
      } else if (count($user) == 1) {
        $msh->query("SELECT * FROM `" . CRMADATBAZIS . "`.`users` WHERE `nUserID`='" . $user[0]['nUserID'] . "' LIMIT 1;");
        $u = $msh->fetchAssoc();
        if (!empty($u)) {
          $u = $u[0];

          $tomb = array();
          if (trim($u['nInvoiceCtrID']) == '')                          $tomb['nInvoiceCtrID'] = 36;
          if (trim($u['strInvoiceName']) == '' && trim($mezo[3]) > '')    $tomb['strInvoiceName'] = utf8_encode($mezo[3]);
          if (trim($u['strInvoiceZip']) == '' && trim($mezo[5]) > '')     $tomb['strInvoiceZip'] = utf8_encode($mezo[5]);
          if (trim($u['strInvoiceCity']) == '' && trim($mezo[6]) > '')    $tomb['strInvoiceCity'] = utf8_encode($mezo[6]);
          if (trim($u['strInvoiceAddress']) == '' && trim($mezo[7]) > '') $tomb['strInvoiceAddress'] = utf8_encode($mezo[7]);
          if (trim($u['strInvoiceEMail']) == '' && $email > '')           $tomb['strInvoiceEMail'] = utf8_encode($email);
          if (trim($u['strTaxNumber']) == '' && trim($mezo[4]) > '')      $tomb['strTaxNumber'] = $mezo[4];

          if (count($tomb)) {
            $msh->update(CRMADATBAZIS . ".`users`", $tomb, array('nUserID' => $user[0]['nUserID']));
            print "nUserID:" . $user[0]['nUserID'] . "<br />";
            print_r($tomb);
            print "<br /><br />";
          }
        } else {
          $tomb = array('hiba' => 'nem talált usert: ' . $user[0]['nUserID']);
        }
      } else {
        $tomb = array('hiba' => 'több user tartozik az email címhez: ' . $email);
      }
    } else {
      $tomb = array('hiba' => 'hiányzó email cím: ' . $mezo[9]);
    }
  }
}
/*
$arrFiles=array("leads/szamlazz.csv");

foreach ($arrFiles as $file)
{
  foreach (file($file) as $k=>$sor)
  {
    $mezo=explode(";", $sor);

    if ($k<=2 || (int)$mezo[0]==0) continue;
    
    $hlNums=explode(",", $mezo[9]);
    $msh->query("SELECT * FROM `".CRMADATBAZIS."`.`hardlock` WHERE `hlNum`='".(int)$hlNums[0]."' LIMIT 1;");
    $user=$msh->fetchAssoc();
    $tomb=array(
      "nInvoiceCtrID"=>36,
      "strInvoiceName"=>utf8_encode($mezo[1]),
      "strInvoiceZip"=>$mezo[2],
      "strInvoiceCity"=>utf8_encode($mezo[3]),
      "strInvoiceAddress"=>utf8_encode($mezo[4]),
      "strInvoiceEMail"=>utf8_encode($mezo[15]),
      "strInvoiceEAccount"=>'Y',
      "strTaxNumber"=>$mezo[19],
    );
    
    $msh->update(CRMADATBAZIS.".`users`",$tomb,array('nUserID'=>$user[0]['hlUser']));
    print "nUserID:".$user[0]['hlUser']."<br />";print_r($tomb); print "<br />Kulcsok: ".implode(",",$hlNums)."<br /><br />";
  }
}
*/
