<?php

echo PHP_EOL;

#set_time_limit(9999999999);

$sql = mysql_connect('localhost', 'root', 'sgpmk777');

mysql_select_db('crm');
$GLOBALS['db']->query('SET NAMES utf8;');

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

function getnr($nr_sys) {
    $dbfi = new dbf_class('_INDEKSY.DBF');

    $num_rec = $dbfi->dbf_num_rec;

    for ($i = 0; $i < $num_rec; $i++) {
        $indeks = $dbfi->getRowAssoc($i);

        if ($indeks['INDEKS'] == $nr_sys) {
            return ch($indeks['NR_SYSTEM']);
        }
    }

    return 'BRAK INDEKSU';
}

function addBook($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();
   
    $dateBook = clone $now;
    $dateBook->setDate(substr($r['D_KSIEG'], 0, 4), substr($r['D_KSIEG'], 4, 2), substr($r['D_KSIEG'], 6, 2));
    $dateBook->setTime(0, 0, 0);
    
    $dateEcmFkPackage = clone $now;
    $dateEcmFkPackage->setDate(substr($r['DAT_DOK'], 0, 4), substr($r['DAT_DOK'], 4, 2), substr($r['DAT_DOK'], 6, 2));
    $dateEcmFkPackage->setTime(0, 0, 0);
    
    $bookDate = clone $now;
    $bookDate->setDate(2000 + $r['ROK'], $r['MIESIAC'], 1); // temp + 2000 (?)
    $bookDate->setTime(0, 0, 0);
    
    $arr = array(
        'id' => create_guid(),
        'date_entered' => date('Y-m-d H:i:s'),
        //'date_modified' => null,
        'date_book' => $dateBook->format('Y-m-d H:i:s'),
        'date_document' => $dateEcmFkPackage->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,
        'book_date' => $bookDate->format('Y-m-d H:i:s'),
        'book_name' => $r['NR_DOK'],
        'book_number' => $r['NR_SYS'],
        'book_amount' => $r['KWOTA'],
        'book_acc_wn' => $r['K_SYN_WN'] . (($analWn = $r['K_ANAL_WN']) ? ('-' . $analWn) : ''),
        'book_acc_ma' => $r['K_SYN_MA'] . (($analMa = $r['K_ANAL_MA']) ? ('-' . $analMa) : ''),
        'book_position' => $r['POZ_DOK'],
        'book_symbol' => $r['SYM_DOK'],
        'book_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 `ecmfkpackages` SET ' . implode(', ', $in) . ';';

    /*   
        echo $query . PHP_EOL;
        exit;
    */
    
    $GLOBALS['db']->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__08__.DBF');
$num_recp = $dbfp->dbf_num_rec;


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

    $add += addBook($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);