<?php
error_reporting(E_ERROR);
//header('Content-type: text/html; charset=utf-8');
include_once("class.dbf.php");
include_once("/var/www/crm/config.php");
include_once("helper.php");
global $sugar_config;
$sql=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']);

mysql_query("truncate ecmstockstates");
mysql_query("set character set utf8;"); 

$path = dirname(__FILE__).'/_stany.dbf';
$dbfp = new dbf_class($path);

$num_recp=$dbfp->dbf_num_rec;

for($i=0;$i<$num_recp;$i++){
	$rowp=$dbfp->getRowAssoc($i);
	
	$res = mysql_query("SELECT name, id FROM ecmproducts WHERE index_dod='".trim($rowp['INDEKS'])."'");
	$p = mysql_fetch_assoc($res);
	
	if ((!isset($p['id'])) && ($p['id']=='')) 
	{
	continue;
	}
	$row = array (
		addslashes(create_guid()), //id
		addslashes(""), //name
		addslashes(date("Y-m-d H:i:s")), //date_entered
		addslashes(date("Y-m-d H:i:s")), //date_modified
		addslashes("cf3a00c2-807d-8e12-592e-4fe88f59cbf6"), //modified_user_id
		addslashes("cf3a00c2-807d-8e12-592e-4fe88f59cbf6"), //created_by
		addslashes(""), //description
		addslashes("0"), //deleted
		addslashes("1"), //assigned_user_id
		addslashes("Magazyn główny"), //stock_name
		addslashes("2962f1ad-79f4-a1ad-385e-4f286ad7acb5"), //stock_id
		addslashes($rowp['INDEKS_DOD']), //product_code
		addslashes($p['name']), //product_name
		addslashes($p['id']), //product_id
		addslashes(round($rowp['STAN'],0)), //quantity
		addslashes($rowp['CENA']), //price
		addslashes($rowp['NR_SYSTEM']), //nr_system
		addslashes($rowp['DOSTEPNOSC']), //availability
		
	);
	
	$query = "INSERT INTO ecmstockstates VALUES ('".implode("','", $row)."')";	
	mysql_query($query);
	

}
?>