<?php

echo PHP_EOL;

$sql = mysql_connect('localhost', 'root', '1ptimu6');
mysql_select_db('crm');

$namesResult = mysql_query('SET NAMES utf8;');

if(false == $namesResult) {
    return;
}

$def = array(
    'KOD',
    'KOD_VAT',
    'K_SYN_WN',
    'K_ANAL_WN',
    'K_SYN_MA',
    'K_ANAL_MA',
    'K_ZAL_WN',
    'K_ZAL_MA',
);

$c = $cVat = $acc = array();

function addVat($a, $c = false) {
    global $c, $cVat, $acc, $def;

    $now = new DateTime();

    // var_export(array_keys($a));
    // exit;

    $r = array_combine($def, $a);

    $arr = array(
        'id' => create_guid(),
        'deleted' => '0',
        'date_entered' => $now->format('Y-m-d'),
        // 'date_modified' => '',
        'created_by' => 'd9c0007b-1247-5e82-31b6-4f168a01d290',
        // 'modified_user_id' => '',

        'ecmfkvattable_id' => $r['KOD'],
        'ecmfkvatkind_id' => $r['KOD_VAT'],

        'acc_wn' => $wn = ($r['K_SYN_WN'] . (($analWn = $r['K_ANAL_WN']) ? ('-' . $analWn): '')),
        'acc_ma' => $ma = ($r['K_SYN_MA'] . (($analMa = $r['K_ANAL_MA']) ? ('-' . $analMa): '')),
        'account_acc_wn' => ($wn == 'RRR') ? '1' : '0',
        'account_acc_ma' => ($ma == 'RRR') ? '1' : '0',
        'zal_wn' => $r['K_ZAL_WN'],
        'zal_ma' => $r['K_ZAL_MA'],
    );

    if(true == array_key_exists($key = (string) $arr['ecmfkvattable_id'], $c) ) {
       $arr['ecmfkvattable_id'] = $c[$keyss];
    } else {
        $q = 'select id from ecmfkvattables where position = \'' . $key . '\' and type = ' . 0 . ' LIMIT 1;';
        // echo var_export($q, true) . PHP_EOL;

        $res = mysql_fetch_assoc(mysql_query($q));

        if(true == is_array($res)) {
            $arr['ecmfkvattable_id'] = $c[$key] = $res['id'];
        } else {
            $arr['ecmfkvattable_id'] = null;
        }
    }

    if(true == array_key_exists($key = (string) $arr['ecmfkvatkind_id'], $cVat) ) {
       $arr['ecmfkvattable_id'] = $cVat[$key];
    } else {
        $q = 'select id from ecmfkvatkinds where position = \'' . $key . '\' LIMIT 1;';
        // echo var_export($q, true) . PHP_EOL;

        $res = mysql_fetch_assoc(mysql_query($q));

        if(true == is_array($res)) {
            $arr['ecmfkvatkind_id'] = $cVat[$key] = $res['id'];
        } else {
            $arr['ecmfkvatkind_id'] = null;
        }
    }

    if(true == array_key_exists($key = (string) $arr['acc_wn'], $acc) ) {
       $arr['acc_wn'] = $acc[$key];
    } else {
        $q = 'select id from ecmbankaccounts where code = \'' . $key . '\' and type = ' . 4 . ' LIMIT 1;';
        // echo var_export($q, true) . PHP_EOL;

        $res = mysql_fetch_assoc(mysql_query($q));

        if(true == is_array($res)) {
            $arr['acc_wn'] = $acc[$key] = $res['id'];
        } else {
            $arr['acc_wn'] = null;
        }
    }

    if(true == array_key_exists($key = (string) $arr['acc_ma'], $acc) ) {
       $arr['acc_ma'] = $acc[$key];
    } else {
        $q = 'select id from ecmbankaccounts where code = \'' . $key . '\' LIMIT 1;';
        // echo var_export($q, true) . PHP_EOL;

        $res = mysql_fetch_assoc(mysql_query($q));

        if(true == is_array($res)) {
            $arr['acc_ma'] = $acc[$key] = $res['id'];
        } else {
            $arr['acc_ma'] = null;
        }
    }

    // echo var_export($r, true);
    // echo PHP_EOL . PHP_EOL;
    // echo var_export($arr, true);
    // exit;

    $arr = array_filter($arr, 'strlen');

    foreach ($arr as $k => $v) {
        $in[] = sprintf('`%s` = \'%s\'', $k, ch($v));
    }

    $query = 'INSERT INTO `ecmfkvattableitems` SET ' . implode(', ', $in) . ';';

    // echo $query . PHP_EOL;
    // exit;

    mysql_query($query);

    if($e = mysql_error()) {
        echo $query;
        echo PHP_EOL . PHP_EOL;
        echo var_export(mysql_error(), true);
        echo PHP_EOL . PHP_EOL;
        return false;
    }

    if(true == $c) {
        echo var_export($arr['id'], true);
        echo PHP_EOL;
    }

    return true;
}

include_once('class.dbf.php');
include_once('helper.php');

$created = 0;
$dbfp = new dbf_class('import/t_vat_ot.dbf');
// $dbfp = new dbf_class('import/t_vat_st.dbf');
// $dbfp = new dbf_class('import/t_vat_zt.dbf');
$num_recp = $dbfp->dbf_num_rec;

$start = microtime(true);

for ($i = 0; $i < $num_recp; $i++) {
    $rowp = $dbfp->getRowAssoc($i);
    $created += addVat($rowp, true) ? 1 : 0;
}

$stop = microtime(true);
$diff = $stop - $start;

echo 'Podsumowanie:' . PHP_EOL;
echo 'W dbf: ' . $num_recp . PHP_EOL;
echo 'Dodano: ' . $created . PHP_EOL;

echo 'Czas trawania:' . ($diff / 60) . ' minut' . PHP_EOL;

mysql_close($sql);