Files
crm.e5.pl/modules/EcmStockStates/stany.php
2024-04-27 09:23:34 +02:00

191 lines
8.0 KiB
PHP
Executable File

<?php
require_once("config.php");
$sql1=mysql_connect($sugar_config['dbconfig']['db_host_name'],$sugar_config['dbconfig']['db_user_name'],$sugar_config['dbconfig']['db_password']);
mysql_select_db($sugar_config['dbconfig']['db_name']);
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 !!!'; 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"; 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;
$handle = fopen($memoname, "r");
if (!$handle) { echo "Cannot read DBT file"; 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;
}
}//End of Class
$dbf = new dbf_class('_stany.dbf');
$num_rec=$dbf->dbf_num_rec;
$num_field=$dbf->dbf_num_field;
mysql_query("truncate table ecmstockstates");
mysql_query("truncate table ecmstockoperations");
mysql_query("truncate table ecmstockdocins");
mysql_query("truncate table ecmstockdocinitems");
include_once("modules/EcmProducts/EcmProduct.php");
include_once("modules/EcmStockDocIns/EcmStockDocIn.php");
$esdi=new EcmStockDocIn();
$esdi->format_all_fields();
$esdi->name="Bilans otwarcia";
$esdi->status="accepted";
$esdi->parent_id=36;
$esdi->register_date="17.09.2009";
$esdi->ecmlanguage="pl_pl";
$esdi->currency_id="PLN";
$esdi->assigned_user_id=1;
$esdi->modified_user_id=1;
$esdi->stock_id="9758747e-5a0e-8b62-04a7-48a923cb3b9a";
$esdi->stock_name="Stock 1";
$tw=mysql_query("select id from ecmdocumenttemplates where account_id='36'");
while($tr=mysql_fetch_array($tw))$tid=$tr['id'];
$esdi->template_id=$tid;
$esdi->template_name="E5 Polska Sp. z o. o.";
$esdi->number=$esdi->generateNumber();
$esdi->document_no=$esdi->formatNumber();
$esdiid=$esdi->save();
for($i=0; $i<$num_rec; $i++){
$row = $dbf->getRow($i);
$stan=$row[8];
$cena=$row[13];
$zamowione=$row[17];
$pw=mysql_query("select name,id,code,vat_id,vat_name,vat_value from ecmproducts where code like '".$row[0]."' and deleted='0'");
$r=mysql_fetch_array($pw);
$name=$r['name'];
$code=$r['code'];
$pid=$r['id'];
echo "[".$name." ".$code." ".mysql_num_rows($pw)."]<br>";
if(mysql_num_rows($pw)<=0){
$prod=new EcmProduct();
$prod->code=$row[0];
$code=$row[0];
$prod->name=$row[1];
$name=$row[1];
$pid=$prod->save();
echo "[dodano produkt: ".$name." ".$code."]<br>";
}
if($stan>0){
if(mysql_query("insert into ecmstockdocinitems values('".create_guid()."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."','1','1','1','0','".$esdiid."','".$pid."','".$k."','".$code."','".$name."','".$stan."','".$cena."','0','".($cena*$stan)."','1','szt.','".$r['vat_id']."','".$r['vat_name']."','".$r['vat_value']."','1','1','PLN')"))echo "items ok<br>";
else echo "items no<br>";
if(mysql_query("insert into ecmstockstates values('".create_guid()."','name','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."','1','1','opis','0','1','Stock 1','9758747e-5a0e-8b62-04a7-48a923cb3b9a','".$name."','".$pid."','".$stan."','".$cena."','".$row[0]."');"))echo "states ok<br>";
else echo "states no<br>";
if(mysql_query("insert into ecmstockoperations values('".create_guid()."','name','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."','1','1','opis','0','1','9758747e-5a0e-8b62-04a7-48a923cb3b9a','".$pid."','".$stan."','".date("Y-m-d H:i:s")."','".$cena."','EcmStockDocIns','".$esdiid."',NULL,'".$esdiid."','0','Stock 1','".$name."','".$esdi->document_no."','".$code."');"))echo "ops ok<br>";
else echo "ops no<br>";
}
echo mysql_error();
$k++;
}
mysql_close($sql1);
?>