Add php files

This commit is contained in:
2025-05-12 15:44:39 +00:00
parent c951760058
commit 82d5804ac4
9534 changed files with 2638137 additions and 0 deletions

View File

@@ -0,0 +1,117 @@
<?php
class dbf_class
{
var $dbf_num_rec; // Number of records in the file
var $dbf_num_field; // Number of columns in each row
var $dbf_names = array(); // Information on each column ['name'],['len'],['type']
// These are private....
var $_raw; // The raw input file
var $_rowsize; // Length of each row
var $_hdrsize; // Length of the header information (offset to 1st record)
var $_memos; // The raw memo file (if there is one).
function dbf_class($filename)
{
if (! file_exists($filename)) {
echo 'Not a valid DBF file !!!(1)';
exit();
}
$tail = substr($filename, - 4);
if (strcasecmp($tail, '.dbf') != 0) {
echo 'Not a valid DBF file !!!';
exit();
}
// Read the File
$handle = fopen($filename, "r");
if (! $handle) {
echo "Cannot read DBF file - tutaj";
exit();
}
$filesize = filesize($filename);
$this->_raw = fread($handle, $filesize);
fclose($handle);
// Make sure that we indeed have a dbf file...
if (! (ord($this->_raw[0]) == 3 || ord($this->_raw[0]) == 131) && ord($this->_raw[$filesize]) != 26) {
echo 'Not a valid DBF file !!!';
exit();
}
// 3= file without DBT memo file; 131 ($83)= file with a DBT.
$arrHeaderHex = array();
for ($i = 0; $i < 32; $i ++) {
$arrHeaderHex[$i] = str_pad(dechex(ord($this->_raw[$i])), 2, "0", STR_PAD_LEFT);
}
// Initial information
$line = 32; // Header Size
// Number of records
$this->dbf_num_rec = hexdec($arrHeaderHex[7] . $arrHeaderHex[6] . $arrHeaderHex[5] . $arrHeaderHex[4]);
$this->_hdrsize = hexdec($arrHeaderHex[9] . $arrHeaderHex[8]); // Header Size+Field Descriptor
// Number of fields
$this->_rowsize = hexdec($arrHeaderHex[11] . $arrHeaderHex[10]);
$this->dbf_num_field = floor(($this->_hdrsize - $line) / $line); // Number of Fields
// Field properties retrieval looping
for ($j = 0; $j < $this->dbf_num_field; $j ++) {
$name = '';
$beg = $j * $line + $line;
for ($k = $beg; $k < $beg + 11; $k ++) {
if (ord($this->_raw[$k]) != 0) {
$name .= $this->_raw[$k];
}
}
$this->dbf_names[$j]['name'] = $name; // Name of the Field
$this->dbf_names[$j]['len'] = ord($this->_raw[$beg + 16]); // Length of the field
$this->dbf_names[$j]['type'] = $this->_raw[$beg + 11];
}
/*
* if (ord($this->_raw[0])==131) { //See if this has a memo file with it... //Read the File $tail=substr($tail,-1,1); //Get the last character... if ($tail=='F'){ //See if upper or lower case $tail='T'; //Keep the case the same } else { $tail='t'; } $memoname = substr($filename,0,strlen($filename)-1).$tail; $memoname = $filemane; echo $memoname.'<br>'; $handle = fopen($memoname, "r"); if (!$handle) { echo "Cannot read DBT file - tutaj"; exit; } $filesize = filesize($memoname); $this->_memos = fread ($handle, $filesize); fclose ($handle);
*/
}
function getRow($recnum)
{
$memoeot = chr(26) . chr(26);
$rawrow = substr($this->_raw, $recnum * $this->_rowsize + $this->_hdrsize, $this->_rowsize);
$rowrecs = array();
$beg = 1;
if (ord($rawrow[0]) == 42) {
return false; // Record is deleted...
}
for ($i = 0; $i < $this->dbf_num_field; $i ++) {
$col = trim(substr($rawrow, $beg, $this->dbf_names[$i]['len']));
if ($this->dbf_names[$i]['type'] != 'M') {
$rowrecs[] = $col;
} else {
$memobeg = $col * 512; // Find start of the memo block (0=header so it works)
$memoend = strpos($this->_memos, $memoeot, $memobeg); // Find the end of the memo
$rowrecs[] = substr($this->_memos, $memobeg, $memoend - $memobeg);
}
$beg += $this->dbf_names[$i]['len'];
}
return $rowrecs;
}
function getRowAssoc($recnum)
{
$rawrow = substr($this->_raw, $recnum * $this->_rowsize + $this->_hdrsize, $this->_rowsize);
$rowrecs = array();
$beg = 1;
if (ord($rawrow[0]) == 42) {
return false; // Record is deleted...
}
for ($i = 0; $i < $this->dbf_num_field; $i ++) {
$col = trim(substr($rawrow, $beg, $this->dbf_names[$i]['len']));
if ($this->dbf_names[$i]['type'] != 'M') {
$rowrecs[$this->dbf_names[$i]['name']] = $col;
} else {
$memobeg = $col * 512; // Find start of the memo block (0=header so it works)
$memoend = strpos($this->_memos, $memoeot, $memobeg); // Find the end of the memo
$rowrecs[$this->dbf_names[$i]['name']] = substr($this->_memos, $memobeg, $memoend - $memobeg);
}
$beg += $this->dbf_names[$i]['len'];
}
return $rowrecs;
}
}

View File

@@ -0,0 +1,182 @@
<?php
function create_guid()
{
$microTime = microtime();
list ($a_dec, $a_sec) = explode(" ", $microTime);
$dec_hex = sprintf("%x", $a_dec * 1000000);
$sec_hex = sprintf("%x", $a_sec);
ensure_length($dec_hex, 5);
ensure_length($sec_hex, 6);
$guid = "";
$guid .= $dec_hex;
$guid .= create_guid_section(3);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= $sec_hex;
$guid .= create_guid_section(6);
return $guid;
}
function create_guid_section($characters)
{
$return = "";
for ($i = 0; $i < $characters; $i ++) {
$return .= sprintf("%x", mt_rand(0, 15));
}
return $return;
}
function ensure_length(&$string, $length)
{
$strlen = strlen($string);
if ($strlen < $length) {
$string = str_pad($string, $length, "0");
} else
if ($strlen > $length) {
$string = substr($string, 0, $length);
}
}
function ch($str)
{
return iconv("IBM852", "UTF-8", $str);
}
function checkCharset($testString, $targetString)
{
$out = false;
$encoding = array(
"ASCII",
"ISO-8859-1",
"ISO-8859-2",
"ISO-8859-3",
"ISO-8859-4",
"ISO-8859-5",
"ISO-8859-7",
"ISO-8859-9",
"ISO-8859-10",
"ISO-8859-13",
"ISO-8859-14",
"ISO-8859-15",
"ISO-8859-16",
"KOI8-R",
"KOI8-U",
"KOI8-RU",
"CP1250",
"CP1251",
"CP1252",
"CP1253",
"CP1254",
"CP1257",
"CP850",
"CP866",
"Mac Roman",
"Mac CentralEurope",
"Mac Iceland",
"Mac Croatian",
"Mac Romania",
"Mac Cyrillic",
"Mac Ukraine",
"Mac Greek",
"Mac Turkish",
"Macintosh",
"ISO-8859-6",
"ISO-8859-8",
"CP1255",
"CP1256",
"CP862",
"Mac Hebrew",
"Mac Arabic",
"EUC-JP",
"SHIFT_JIS",
"CP932",
"ISO-2022-JP",
"ISO-2022-JP-2",
"ISO-2022-JP-1",
"EUC-CN",
"HZ",
"GBK",
"GB18030",
"EUC-TW",
"BIG5",
"CP950",
"BIG5-HKSCS",
"ISO-2022-CN",
"ISO-2022-CN-EXT",
"EUC-KR",
"CP949",
"ISO-2022-KR",
"JOHAB",
"ARMSCII-8",
"Georgian-Academy",
"Georgian-PS",
"KOI8-T",
"TIS-620",
"CP874",
"MacThai",
"MuleLao-1",
"CP1133",
"VISCII",
"TCVN",
"CP1258",
"HP-ROMAN8",
"NEXTSTEP",
"UTF-8",
"UCS-2",
"UCS-2BE",
"UCS-2LE",
"UCS-4",
"UCS-4BE",
"UCS-4LE",
"UTF-16",
"UTF-16BE",
"UTF-16LE",
"UTF-32",
"UTF-32BE",
"UTF-32LE",
"UTF-7",
"C99",
"JAVA",
"UCS-2-INTERNAL",
"UCS-4-INTERNAL",
"CP437",
"CP737",
"CP775",
"CP850",
"CP852",
"CP853",
"CP855",
"CP857",
"CP858",
"CP860",
"CP861",
"CP863",
"CP865",
"CP869",
"CP1125",
"CP864",
"EUC-JISX0213",
"Shift_JISX0213",
"ISO-2022-JP-3",
"TDS565",
"RISCOS-LATIN1"
);
foreach ($encoding as $v) {
if (iconv($v, "utf-8", $testString) === $targetString) {
$out = $v;
}
}
return $out;
}

View File

@@ -0,0 +1,252 @@
<?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(
'TYP_REJ',
'KOD_VAT',
'NAZWA',
'TYT_1',
'KOL1_1',
'KOL2_1',
'TYT_2',
'KOL1_2',
'KOL2_2',
'TYT_3',
'KOL1_3',
'KOL2_3',
'TYT_4',
'KOL1_4',
'KOL2_4',
'TYT_5',
'KOL1_5',
'KOL2_5',
'K_SYN',
'K_ANAL',
'S_UPORZ_1',
'S_UPORZ_2',
'S_UPORZ_3',
'S_UPORZ_4',
'S_UPORZ_5',
'KOD_KS',
'ID_DOK',
'S_UPORZ_UE',
'NR1_SKL',
'NR2_SKL',
'TEMP_VAT',
'DRUK_SN_1',
'DRUK_SN_2',
'DRUK_SN_3',
'DRUK_SN_4',
'DRUK_SN_5',
'SPR_DOK',
'DAT_DOK_KP',
);
$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' => '',
'type' => $r['TYP_REJ'],
'position' => $r['KOD_VAT'],
'name' => $r['NAZWA'],
/*
'title_1' => $r['TYT_1'],
'col_1_1' => $r['KOL1_1'],
'col_2_1' => $r['KOL2_1'],
'title_2' => $r['TYT_2'],
'col_1_2' => $r['KOL1_2'],
'col_2_2' => $r['KOL2_2'],
'title_3' => $r['TYT_3'],
'col_1_3' => $r['KOL1_3'],
'col_2_3' => $r['KOL2_3'],
'title_4' => $r['TYT_4'],
'col_1_4' => $r['KOL1_4'],
'col_2_4' => $r['KOL2_4'],
'title_5' => $r['TYT_5'],
'col_1_5' => $r['KOL1_5'],
'col_2_5' => $r['KOL2_5'],
*/
'acc' => $wn = ($r['K_SYN'] . (($anal = $r['K_ANAL']) ? ('-' . $anal): '')),
/*
'order_1' => $r['S_UPORZ_1'],
'order_2' => $r['S_UPORZ_2'],
'order_3' => $r['S_UPORZ_3'],
'order_4' => $r['S_UPORZ_4'],
'order_5' => $r['S_UPORZ_5'],
*/
'ident_k' => $r['KOD_KS'],
'id_dok' => $r['ID_DOK'],
'order_ue' => $r['S_UPORZ_UE'],
'nr_1' => $r['NR1_SKL'],
'nr_2' => $r['NR2_SKL'],
'temp_vat' => $r['TEMP_VAT'],
/*
'p_1' => $r['DRUK_SN_1'],
'p_2' => $r['DRUK_SN_2'],
'p_3' => $r['DRUK_SN_3'],
'p_4' => $r['DRUK_SN_4'],
'p_5' => $r['DRUK_SN_5'],
*/
'spr_dok' => $r['SPR_DOK'],
'kp_date' => $r['DAT_DOK_KP'],
'orientation' => 'L',
);
/*
*/
if(true == array_key_exists((string) $arr['acc'], $acc) ) {
$arr['acc'] = $acc[$arr['acc']];
} else {
$q = 'select id from ecmbankaccounts where code = \'' . $arr['acc'] . '\' LIMIT 1;';
// echo var_export($q, true) . PHP_EOL;
$res = mysql_fetch_assoc(mysql_query($q));
if(true == is_array($res)) {
$arr['acc'] = $acc[(string) $arr['acc']] = $res['id'];
} else {
$arr['acc'] = null;
}
}
// echo var_export($r, true);
// echo PHP_EOL . PHP_EOL;
// echo var_export($arr, true);
// exit;
for($i = 1, $iCounter = 5; $i <= $iCounter; $i++) {
$arr2 = 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' => '',
'title' => $r['TYT_' . $i],
'ecmfkvatkind_id' => $arr['id'],
'order' => $r['S_UPORZ_' . $i],
'p' => $r['DRUK_SN_' . $i],
);
for($j = 1, $jCounter = 2; $j <= $jCounter; $j++) {
$tmp = $r['KOL' . $j . '_' . $i];
$matches = preg_split('/([ \.:])*\|([ \.:])*/', $tmp, -1, PREG_SPLIT_NO_EMPTY);
// echo PHP_EOL . PHP_EOL;
// echo var_export($matches, true);
// exit;
$arr2['col_' . $j] = base64_encode(serialize($matches));
$arr2['col_' . $j . '_translations'] = base64_encode(serialize($matches));
}
$arr2 = array_filter($arr2, 'strlen');
if(strlen(@$arr2['title']) == 0) {
continue;
}
$in2 = array();
foreach ($arr2 as $k2 => $v2) {
$in2[] = sprintf('`%s` = \'%s\'', $k2, ch($v2));
}
// echo PHP_EOL . PHP_EOL;
// echo var_export($arr2, true);
// exit;
$query2 = 'INSERT INTO `ecmfkvatkinditems` SET ' . implode(', ', $in2) . ';';
// echo PHP_EOL . $query2 . PHP_EOL . PHP_EOL;
// exit;
mysql_query($query2);
// echo PHP_EOL . PHP_EOL;
// echo var_export($arr2, true);
// exit;
}
$arr = array_filter($arr, 'strlen');
$in = array();
foreach ($arr as $k => $v) {
$in[] = sprintf('`%s` = \'%s\'', $k, ch($v));
}
$query = 'INSERT INTO `ecmfkvatkinds` 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/rej_vat.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);