<?php

/**********************************************************************************
 * eredeti url: http://www.archline.hu/version/elofizkarbxml/ + fajlnev+ / +nyelv *
 **********************************************************************************/

ini_set("max_execution_time", 60);
include("version.php");
include('libraries/Excel2/reader.php');

@header("Content-Type: text/html; charset=UTF-8");

$fajl = '/var/www/hosting/archline.hu/www/fizetesek/' . $_GET['file']; // segment(3);
$nyelv = (isset($_GET['lang']) ? $_GET['lang'] : NULL); //segment(4);
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1251');
$data->read($fajl);
$needInsert = true;

// temp feltoltese
crm_database::getInstance()->query("TRUNCATE TABLE `" . CRMADATBAZIS . "`.`temp_excelfizetes`");
$insertStatement = "INSERT INTO " . CRMADATBAZIS . ".temp_excelfizetes (kulcs,datum,kapcs,info) VALUES";
$numrows = $data->sheets[0]['numRows'];

for ($i = 1; $i <= $numrows; $i++) {
    if (!$data->sheets[0]['cells'][$i][1]) {
        $needInsert = false;
        continue;
    }

    $o1 = $data->sheets[0]['cells'][$i][1];
    $o2 = $data->sheets[0]['cells'][$i][2];
    $o3 = $data->sheets[0]['cells'][$i][3];
    $o4 = $data->sheets[0]['cells'][$i][4];
    $insertStatement = $insertStatement . "(" . $o1 . ",'" . $o2 . "','" . $o3 . "','" . $o4 . "')";
    if ($i < $numrows)
        $insertStatement = $insertStatement . ",";

    $needInsert = true;
}

if ($needInsert || $numrows <= 0)
    crm_database::getInstance()->query($insertStatement);

// valtozasok lekerese
crm_database::getInstance()->query("(SELECT t1.kulcs, t1.datum, t1.kapcs, t1.info, '' originfo
    FROM " . CRMADATBAZIS . ".temp_excelfizetes t1 LEFT JOIN " . CRMADATBAZIS . ".t_excelfizetes t2 ON t1.kulcs = t2.kulcs
    WHERE t1.datum!=t2.datum OR t1.kapcs!=t2.kapcs AND t1.kulcs !='')
    UNION
    (SELECT kulcs, datum, kapcs, 'uj elem' info, info as originfo FROM " . CRMADATBAZIS . ".temp_excelfizetes
    WHERE kulcs NOT IN (SELECT kulcs FROM " . CRMADATBAZIS . ".t_excelfizetes))");
$valtozasok = crm_database::getInstance()->fetchAssoc();

foreach ($valtozasok as $el) {
    $elem = (object)$el;

    $utfizu = Version::_getfizhonap($elem->datum, $elem->kapcs);
    $actDate = Version::_modosit($elem->kulcs, $utfizu, $elem->kapcs, $elem->info);

    $tomb = array(
        "kulcs" => $elem->kulcs,
        "kapcs" => $elem->kapcs,
        "datum" => $elem->datum,
        "info"  => $elem->originfo,
    );

    if ($elem->info == "uj elem")
        crm_database::getInstance()->insert(CRMADATBAZIS . '.t_excelfizetes', $tomb);
    else
        crm_database::getInstance()->update(CRMADATBAZIS . '.t_excelfizetes', $tomb, array('kulcs' => $elem->kulcs));

    $hiInfo = "Excel: " . $elem->kapcs . ", Dátum: " . $elem->datum . " " . $elem->originfo;

    $query = "SELECT * FROM `" . CRMADATBAZIS . "`.`h_history` WHERE hiHlNum = '" . $elem->kulcs . "' AND hiInfo = '" . $hiInfo . "'";
    crm_database::getInstance()->query($query);
    $res = crm_database::getInstance()->fetchAssoc();

    if (empty($res)) {
        $history = array(
            'hiHlNum'   => $elem->kulcs,
            'hiDate'    => date('Y-m-d'),
            'hiManID'   => 36,
            'hiTypeID'  => 3,
            'hiTime'    => date("Y-m-d H:i:s"),
            'hiInfo'    => $hiInfo,
        );

        if ($actDate && $actDate != '') {
            $history['activationDate'] = $actDate;
        }

        crm_database::getInstance()->query("SELECT prOrdNum, prPass FROM `" . CRMADATBAZIS . "`.`h_programs`
            WHERE prVerID >=13 AND prHlNum IS NOT NULL AND prHlNum = (select hlNum FROM `" . CRMADATBAZIS . "`.`hardlock` WHERE `hlNum`='{$elem->kulcs}' AND `hlStat`=1) ORDER BY prID DESC LIMIT 1;");
        $lastProgram = crm_database::getInstance()->fetchNext();

        if (!empty($lastProgram) && $lastProgram['prOrdNum'] != '') {
            $history['prPass'] = $lastProgram['prPass'];
            crm_database::getInstance()->query("SELECT o.id AS id FROM `" . CRMADATBAZIS . "`.`o_orders` o
                WHERE o.order = '" . $lastProgram['prOrdNum'] . "' AND o.output_key = '" . $lastProgram['prPass'] . "' ORDER BY o.id DESC LIMIT 1;");
            $currentOrder = crm_database::getInstance()->fetchNext();

            if (!empty($currentOrder)) {
                $history['orderID'] = $currentOrder['id'];
            }
        }

        crm_database::getInstance()->insert(CRMADATBAZIS . '.h_history', $history);

        print "Valtozott: " . $elem->kulcs . " " . $utfizu . " " . $elem->kapcs;
    }
}

print "ok";
