<?php
include("modules/EcmProductB2BReports/vtigerConnector.php");
$GLOBALS['db']->query("update ecmproducts set ordered='0',ord_approved='0',ord_created='0',ord_delivered='0'");

$vc=new vtigerConnector();
$vc->start=0;
$vc->limit="0,999999";
$vc->order_by="purchaseorder.duedate";
$vc->sorder="desc";
$vc->where="(purchaseorder.postatus='Created' or purchaseorder.postatus='Approved' or purchaseorder.postatus='Delivered')";
$vc->getData();
$vc->getCount();
$data=$vc->data;
for($i=0;$i<$vc->count;$i++){
	if(eregi("547",$data[$i]['productcode'])){
		//print_r($data[$i]);//
		echo $data[$i]['productcode']." ".$data[$i]['postatus']."<br>";
	}
	if(eregi("_",$data[$i]['productcode'])){
		$e=explode("_",$data[$i]['productcode']);
		$code=$e[0];
	}
	else $code=$data[$i]['productcode'];
	$code=strtoupper($code);
	$ord[$code]+=$data[$i]['quantity'];
	if($data[$i]['postatus']=="Delivered")$ord_delivered[$code]+=$data[$i]['quantity'];
	if($data[$i]['postatus']=="Approved")$ord_approved[$code]+=$data[$i]['quantity'];
	if($data[$i]['postatus']=="Created")$ord_created[$code]+=$data[$i]['quantity'];
}
echo "<br><br>";
foreach($ord as $key=>$value){
	echo $key." ".$value."<br>";
	$GLOBALS['db']->query("update ecmproducts set ordered='".$value."' where code='".$key."'");
}
foreach($ord_created as $key=>$value){
	echo $key." ".$value."<br>";
	$GLOBALS['db']->query("update ecmproducts set ord_created='".$value."' where code='".$key."'");
}
foreach($ord_approved as $key=>$value){
	echo $key." ".$value."<br>";
	$GLOBALS['db']->query("update ecmproducts set ord_approved='".$value."' where code='".$key."'");
}
foreach($ord_delivered as $key=>$value){
	echo $key." ".$value."<br>";
	$GLOBALS['db']->query("update ecmproducts set ord_delivered='".$value."' where code='".$key."'");
}
?>