<?php 

function aasort (&$array, $key) {
	$sorter=array();
	$ret=array();
	reset($array);
	foreach ($array as $ii => $va) {
		$sorter[$ii]=$va[$key];
	}
	asort($sorter);
	foreach ($sorter as $ii => $va) {
		$ret[$ii]=$array[$ii];
	}
	$array=$ret;
}

$db=$GLOBALS['db'];
$ss = new Sugar_Smarty ();

$lists=array();
$users=array();
$OrderByDateList=array();

$moduleLabels['accounts']='Kontrahenci';
$moduleLabels['ecmproducts']='Produkty';
$moduleLabels['ecmquotes']='Oferty';
$moduleLabels['ecmsales']='Zamówienia sprzedaży';
$moduleLabels['ecminvoiceouts']='Faktury';
$moduleLabels['ecmprepaymentinvoices']='Faktury zaliczkowe';
$moduleLabels['ecmreceipts']='Paragony';
$moduleLabels['notes']='Notatki';
$moduleLabels['tasks']='Zadania';
$moduleLabels['documents']='Dokumenty';

if($_REQUEST['date_from']=== null)$_REQUEST['date_from']=date('01.m.Y');
if($_REQUEST['date_to']===null)$_REQUEST['date_to']=date('t.m.Y');

$date_from =$_REQUEST['date_from'];
$date_to=$_REQUEST['date_to'];

$querys['accounts']="select id,created_by,date_entered,date_modified,name from accounts where date_entered >='".date('Y-m-d',strtotime($date_from))."' and date_entered <='".date('Y-m-d',strtotime($date_to))."'";

$querys['ecmproducts']="select id,created_by,date_entered,date_modified,name from ecmproducts where date_entered >='".date('Y-m-d',strtotime($date_from))."' and date_entered <='".date('Y-m-d',strtotime($date_to))."'";

$querys['ecmquotes']="select id,created_by,date_entered,date_modified,name from ecmquotes where date_entered >='".date('Y-m-d',strtotime($date_from))."' and date_entered <='".date('Y-m-d',strtotime($date_to))."'";

$querys['ecmsales']="select id,created_by,date_entered,date_modified,name from ecmsales where date_entered >='".date('Y-m-d',strtotime($date_from))."' and date_entered <='".date('Y-m-d',strtotime($date_to))."'";

$querys['ecminvoiceouts']="select id,created_by,date_entered,date_modified,name from ecminvoiceouts where date_entered >='".date('Y-m-d',strtotime($date_from))."' and date_entered <='".date('Y-m-d',strtotime($date_to))."'";

$querys['ecmprepaymentinvoices']="select id,created_by,date_entered,date_modified,name from ecmprepaymentinvoices where date_entered >='".date('Y-m-d',strtotime($date_from))."' and date_entered <='".date('Y-m-d',strtotime($date_to))."'";

$querys['ecmreceipts']="select id,created_by,date_entered,date_modified,name from ecmreceipts where date_entered >='".date('Y-m-d',strtotime($date_from))."' and date_entered <='".date('Y-m-d',strtotime($date_to))."'";

$querys['notes']="select id,created_by,date_entered,date_modified,name from notes where date_entered >='".date('Y-m-d',strtotime($date_from))."' and date_entered <='".date('Y-m-d',strtotime($date_to))."'";

$querys['tasks']="select id,created_by,date_entered,date_modified,name from tasks where date_entered >='".date('Y-m-d',strtotime($date_from))."' and date_entered <='".date('Y-m-d',strtotime($date_to))."'";

$querys['documents']="select id,created_by,date_entered,date_modified,document_number as name from documents where date_entered >='".date('Y-m-d',strtotime($date_from))."' and date_entered <='".date('Y-m-d',strtotime($date_to))."'";

$user_query='select id,first_name,last_name from users where deleted=0';

$ss->assign("date_to", $date_to);
$ss->assign("date_from", $date_from);

if($_REQUEST['process']){

	$zap=$db->query($user_query);
	while($dane=$db->fetchByAssoc($zap)){
		if($dane['id']==1)continue;
		$dane['nr']=1;
		$users[$dane['id']]=$dane;
		$lists[$dane['id']]=null;
		foreach ($querys as $key => $val){
			$lists[$dane['id']][$key] =null;
		}
	}
	
	
	foreach ($querys as $key => $val){
		$nr=1;
		$res=$db->query($val);
		while($dane=$db->fetchByAssoc($res)){
			if($dane['created_by']==1)continue;
			$dane['sort']=date('YmdHis',strtotime($dane['date_entered']));
			$lists[$dane['created_by']][$key][]=$dane;
			$dane['module']=$key;
			$dane['nr']=$users[$dane['created_by']]['nr'];
			$OrderByDateList[$dane['created_by']][]=$dane;
			$users[$dane['created_by']]['nr']++;
		}
	}	
	
	$zap=$db->query($user_query);
	while($dane=$db->fetchByAssoc($zap)){
		if(count($OrderByDateList[$dane['id']])>0){

			
			aasort($OrderByDateList[$dane['id']],"sort");
		
		}
	}
	
	$ss->assign("lists", $lists);
	$ss->assign("OrderByDateList", $OrderByDateList);
	$ss->assign("users", $users);
	$ss->assign("moduleLabels", $moduleLabels);
	$ss->assign("colWidth", round(90/count($moduleLabels),2));
	

	
}



$content = $ss->fetch ( 'modules/EcmReports/tpls/RaportAktywnosci.tpl' );
echo $content;
?>