<?php

echo PHP_EOL;

#set_time_limit(9999999999);

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

mysql_select_db('crm');
mysql_query('SET NAMES utf8;');

function getmicrotime() {
    list($usec, $sec) = explode(' ', microtime());
    return ((float) $usec + (float) $sec);
}

function addVat($r, $c = false) {
    //'01-' . str_pad($r['MIESIAC'], 2, '0', STR_PAD_LEFT) . '-' .  str_pad($r['ROK'], 2, '0', STR_PAD_LEFT),
    $now = new DateTime();
   
    $dateVat = clone $now;
    $dateVat->setDate(substr($r['D_KSIEG'], 0, 4), substr($r['D_KSIEG'], 4, 2), substr($r['D_KSIEG'], 6, 2));
    $dateVat->setTime(0, 0, 0);
    
    $dateDocument = clone $now;
    $dateDocument->setDate(substr($r['DAT_DOK'], 0, 4), substr($r['DAT_DOK'], 4, 2), substr($r['DAT_DOK'], 6, 2));
    $dateDocument->setTime(0, 0, 0);
    
    $vatDate = clone $now;
    $vatDate->setDate(2000 + $r['ROK'], $r['MIESIAC'], 1); // temp + 2000 (?)
    $vatDate->setTime(0, 0, 0);
    
    $arr = array(
        'id' => create_guid(),
        'date_entered' => date('Y-m-d H:i:s'),
        //'date_modified' => null,
        'date_vat' => $dateVat->format('Y-m-d H:i:s'),
        'date_document' => $dateDocument->format('Y-m-d H:i:s'),
        //'modified_user_id' => null,
        'created_by' => 'd9c0007b-1247-5e82-31b6-4f168a01d290', // ems
        'description' => $r['OPIS_1'] . PHP_EOL . $r['OPIS_2'],
        //'deleted' => null,
        'vat_date' => $vatDate->format('Y-m-d H:i:s'),
        'vat_name' => $r['NR_DOK'],
        'vat_number' => $r['NR_SYSt'],
        'vat_amount' => $r['KWOTA'],
        'vat_acc_wn' => $r['K_SYN_WN'] . (($analWn = $r['K_ANAL_WN']) ? ('-' . $analWn) : ''),
        'vat_acc_ma' => $r['K_SYN_MA'] . (($analMa = $r['K_ANAL_MAt']) ? ('-' . $analMa) : ''),
        'vat_position' => $r['POZ_DOKt'],
        'vat_symbol' => $r['SYM_DOK'],
        'vat_identifier' => $r['ID_DOK'],
    ); 

    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, $v);
    }

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

    /* 
        echo $query . PHP_EOL;
        exit;  
    */
    
    mysql_query($query);

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

    if(true == $c)
        echo $arr['id'] . PHP_EOL;
        
    return true;
}

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

$dbfp = new dbf_class('pk__13__.dbf');
$num_recp = $dbfp->dbf_num_rec;

for ($i = 0; $i < $num_recp; $i++) {
    $rowp = $dbfp->getRowAssoc($i);

    // echo var_export($rowp, true); exit;
    $add += addVat($rowp) ? 1 : 0;
}

$start = getmicrotime();
$koniec = getmicrotime();

echo 'Podsumowanie:' . PHP_EOL . 'W dbf: ' . $num_recp . PHP_EOL . 'Dodano: ' . $add . PHP_EOL;
echo 'Czas trawania:' . (($koniec - $start) / 60) . ' minut';

mysql_close($sql);

// nr_sys
// ident_k
// sym_dok
// nr_dok
// poz_dok
// wys_k_wn
// wys_k_ma
// spar_tr
// miesiac
// kwota
// id_dok
// opis_1
// opis_2
// rezerwa
// zm_spar
// kwota_wal
// symb_wal
// kurs_wal

// Nr sys
// Id
// Symb
// Nr dok.
// Pozycja.
// KONTO Wn
// KONTO Ma
// Wskaźnik
// Miesiąc
// Kwota
// Ident.ksi©g.
// Opis 1 cz©˜†
// Opis 2 cz©˜†
// Rezerwa
// Tryb parowania
// Kwota w walucie
// Symb
// Kurs waluty
