Add php files
This commit is contained in:
117
modules/EcmFkVats/import/class.dbf.php
Executable file
117
modules/EcmFkVats/import/class.dbf.php
Executable 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;
|
||||
}
|
||||
|
||||
}
|
||||
182
modules/EcmFkVats/import/helper.php
Executable file
182
modules/EcmFkVats/import/helper.php
Executable 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;
|
||||
}
|
||||
102
modules/EcmFkVats/import/import-vats.php
Executable file
102
modules/EcmFkVats/import/import-vats.php
Executable file
@@ -0,0 +1,102 @@
|
||||
<?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_VAT',
|
||||
'S_VAT',
|
||||
'PROC_VAT',
|
||||
'ZM_VAT',
|
||||
'PLUS_SV_SP',
|
||||
'PLUS_SV_ZK',
|
||||
);
|
||||
|
||||
function addVat($a, $c = false) {
|
||||
global $def;
|
||||
|
||||
$now = new DateTime();
|
||||
|
||||
$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_VAT'],
|
||||
'symbol' => $r['S_VAT'],
|
||||
'percentage' => $r['PROC_VAT'],
|
||||
'zm' => $r['ZM_VAT'],
|
||||
'sp' => $r['PLUS_SV_SP'],
|
||||
'zk' => $r['PLUS_SV_ZK'],
|
||||
);
|
||||
|
||||
// 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 `ecmfkvats` 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.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);
|
||||
Reference in New Issue
Block a user