<?php
include_once("modules/EcmProducts/EcmProduct.php");
$w=$GLOBALS['db']->query("select id,code from ecmproducts where deleted='0' and product_active='".$_REQUEST['active']."'");
while($rr=$GLOBALS['db']->fetchByAssoc($w)){
	
	$ecmp=new EcmProduct();
	$ecmp->id=$rr['id'];
	$s0=$ecmp->getSale(array(date("Y-m")));
	$s3=$ecmp->getSale(array(date("Y-m",mktime()-1*30*24*3600),date("Y-m",mktime()-2*30*24*3600),date("Y-m",mktime()-3*30*24*3600)));

	$stock_value=($rr['ems_price']*$rr['ems_qty_in_stock']);

	if(round(($rr['s3']))!=0)$stock_month=(($rr['ems_qty_in_stock']+$rr['ordered'])/round(($rr['s3'])));
	else $stock_month=0;	

	if($s0['sale'])$p0=100*($s0['sale']-$s0['purchase'])/$s0['sale'];
	else $p0=0;
	if($s3['sale'])$p3=100*($s3['sale']-$s3['purchase'])/$s3['sale'];
	else $p3=0;
	echo $rr['code']." ".$s0['sale']." (".$s0['purchase'].") ".$s0['quantity']." ".$p0;
	if($GLOBALS['db']->query("update ecmproducts set q0='".$s0['quantity']."',p0='".$p0."',s0='".$s0['sale']."',q3='".($s3['quantity']/3)."',p3='".$p3."',s3='".($s3['sale']/3)."',stock_month='".$stock_month."',stock_value='".$stock_value."' where id='".$rr['id']."'"))echo " ok";
	else echo " no";
	echo "<br>";
}
?>