<?
include_once("modules/EcmProductReports/vtigerConnector.php");
include_once("modules/EcmInsideOrders/EcmInsideOrder.php");
mysql_query("delete e,p from ecminsideorders as e inner join ecmproducts_ecminsideorders as p on p.ecminsideorder_id=e.id where e.order_id!='' or e.order_id is not null");
function updateOrder($id,$data){
	$r=mysql_fetch_array(mysql_query("select id from ecminsideorders where order_id='".$id."'"));
	$iid=$r['id'];
	$p=new EcmInsideOrder();
	if($iid){
		$p->retrieve($iid);
	}
	$p->assigned_user_id=1;
	$p->template_id="97700b0d-fbe9-e366-4016-4b260f058a47";
	$p->template_name="e5 Polska Sp. z o. o.";
	$p->name=$data['subject'];
	$p->status=$data['postatus'];
	$p->tracking_no=$data['tracking_no'];
	$p->register_date=$data['duedate'];
	$p->delivery_date=$data['deliverydate'];
	$r=mysql_fetch_array(mysql_query("select * from accounts where name like 'e5 Limited'"));
	$p->parent_id=$r['id'];
	$p->parent_name=$r['name'];
	$p->parent_address_street=$r['billing_address_street'];
	$p->parent_address_city=$r['billing_address_city'];
	$p->parent_address_postalcode=$r['billing_address_postalcode'];
	$p->parent_address_country=$r['billing_address_country'];
	$p->ecmlanguage="pl_pl";
	$p->currency_id="PLN";
	$iid=$p->save();
	mysql_query("update ecminsideorders set tracking_no='".$data['tracking_no']."',order_id='".$id."',delivery_date='".$data['deliverydate']."',register_date='".$data['duedate']."' where id='".$iid."'");
}
function addLine($data){
	$ro=mysql_fetch_array(mysql_query("select id from ecminsideorders where order_id='".$data['purchaseorderid']."'"));
	$rpp=mysql_fetch_array(mysql_query("select max(product_position) as max from ecmproducts_ecminsideorders where ecminsideorder_id='".$ro['id']."'"));
	$pos=$rpp['max'];
	$rp=mysql_fetch_array(mysql_query("select id,code,name,carton_volume_meter,pieces_per_carton,moq from ecmproducts where code like '".$data['productcode']."'"));
	$rv=mysql_fetch_array(mysql_query("select id,name,value from ecmvats where value='0'"));
	$arr['id']=create_guid();
	$arr['date_entered']=date("Y-m-d H:i:s");
	$arr['date_modified']=date("Y-m-d H:i:s");
	$arr['modified_user_id']=1;
	$arr['assigned_user_id']=1;
	$arr['created_by']=1;
	$arr['deleted']=0;
	$arr['ecminsideorder_id']=$ro['id'];
	$arr['ecmproduct_id']=$rp['id'];
	$arr['product_name']=$rp['name'];
	$arr['product_code']=$rp['code'];
	$arr['product_quantity']=$data['quantity'];
	$arr['product_price']=$data['unit_price'];
	$arr['product_discount']=0;
	$arr['product_total']=$data['quantity']*$data['unit_price'];
	$arr['product_unit_id']=1;
	$arr['product_unit_name']="szt.";
	$arr['product_vat_id']=$rv['id'];
	$arr['product_vat_name']=$rv['name'];
	$arr['product_vat_value']=$rv['value'];
	$arr['product_category_id']="";
	$arr['product_currency_id']="PLN";
	$arr['product_cbm']=$rp['carton_volume_meter']/$rp['pieces_per_carton'];
	$arr['product_moq']=$rp['moq'];
	$arr['product_position']=$pos++;
	foreach($arr as $k=>$v){
		$s[]=$k."='".$v."'";
	}
	mysql_query("insert into ecmproducts_ecminsideorders set ".implode(",",$s));
	mysql_query("update ecminsideorders set total=total+".($data['quantity']*$data['unit_price'])." where id='".$ro['id']."'");
}
$vc=new vtigerConnector();
$vc->start=1;
$vc->pp=9999999;
$vc->limit="0,".$vc->pp;
$vc->order_by="purchaseorder.duedate";
$vc->sorder="asc";

$wh[]="purchaseorder.postatus!=''";
$wh[]="(purchaseordercf.cf_528!='test')";

$vc->where=@implode(" and ",$wh);
$vc->getData();
$cnt=$vc->getCount();
$data=$vc->data;
//echo '<pre>';
//print_r($data);
//echo '</pre>';
foreach($data as $d){
	$arr[$d['purchaseorderid']]=$d;
}
foreach($arr as $k=>$v){
	updateOrder($k,$v);
}
foreach($data as $d){
	addLine($d);
}
echo "Orders updated: ".count($arr);
?>