<?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',
  'NAZWA',
  'K_SYN_WN',
  'K_ANAL_WN',
  'K_SYN_MA',
  'K_ANAL_MA',
  'ZALEZN',
  'S_LICZ',
  'REZ',
  'S_VAT',
  'VAT_NETTO',
  'K_ZAL_WN',
  'K_ZAL_MA',
);

$acc = array();

function addVat($a, $c = false) {
    global $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' => '',
        'position' => $r['KOD'],
        'name' => $r['NAZWA'],
        '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' => $r['ZALEZN'],
        'licz' => $r['S_LICZ'],
        'rez' => $r['REZ'],
        'vat' => $r['S_VAT'],
        'netto' => $r['VAT_NETTO'] ? : '0',
        'zal_wn' => $r['K_ZAL_WN'],
        'zal_ma' => $r['K_ZAL_MA'],
        'type' => '0',
    );

    if(true == array_key_exists((string) $arr['acc_wn'], $acc) ) {
       $arr['acc_wn'] = $acc[$arr['acc_wn']];
    } else {
        $q = 'select id from ecmbankaccounts where code = \'' . $arr['acc_wn'] . '\' 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[(string) $arr['acc_wn']] = $res['id'];
        } else {
            $arr['acc_wn'] = null;
        }
    }
    
    if(true == array_key_exists((string) $arr['acc_ma'], $acc) ) {
       $arr['acc_ma'] = $acc[$arr['acc_ma']];
    } else {
        $q = 'select id from ecmbankaccounts where code = \'' . $arr['acc_ma'] . '\' 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[(string) $arr['acc_ma']] = $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 `ecmfkvattables` 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_og.dbf');
// $dbfp = new dbf_class('import/t_vat_pl.dbf');
// $dbfp = new dbf_class('import/t_vat_sp.dbf');
// $dbfp = new dbf_class('import/t_vat_zk.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);