<?php 
$db=$GLOBALS['db'];
if(isset($_REQUEST['date_from'])){
	$from = new DateTime($_REQUEST['date_from']);
} else {
	$from = new DateTime(date("01.m.Y"));
}

if(isset($_REQUEST['date_to'])){
	$to = new DateTime($_REQUEST['date_to']);
} else {
	$to = new DateTime(date("t.m.Y"));
}

$executed=0;

if(isset($_REQUEST['submit'])){
	$query="select * from send_report where send_date>='".$from->format('Y-m-d')."' and send_date<='".$to->format('Y-m-d')."'";
	if($_REQUEST['number']!=''){
		$query.=" and number='".$_REQUEST["number"]."'";
	}
	if($_REQUEST['account_id']!=''){
		$query.=" and parent_id='".$_REQUEST["account_id"]."'";
	}
	if($_REQUEST['empty']!=''){
		$query.=" and number!=''";
	}
	$zap=$db->query($query);

	if($zap->num_rows>0){
		$no=1;
		while($row=$db->fetchByAssoc($zap)){
			$query2="select * from send_report_items where send_report_id='".$row['id']."' order by payment_date desc";
			$zap2=$db->query($query2);
			$no2=1;
			while($row2=$db->fetchByAssoc($zap2)){
				$row2['no']=$no2;
				$no2++;
				$row['records'][]=$row2;
			}
			$row['no']=$no;
			$p = new Account();
			$p->retrieve_by_string_fields (array('index_dbf'=>$row['parent_id']));

			$row['parent_name']=$p->name;
			$data[]=$row;
			$no++;
			unset($p);
		}
	} else {
		$data=null;
	}
	$executed=1;
}

$smarty = new Sugar_Smarty ();
$smarty->assign("data", $data);
$smarty->assign("empty", $_REQUEST['empty']);
$smarty->assign("executed", $executed);
$smarty->assign("MOD", $mod_strings);
$smarty->assign("date_from_value", $from->format('d.m.Y'));
$smarty->assign("date_to_value", $to->format('d.m.Y'));
$output = $smarty->fetch('modules/EcmReports/tpls/RaportSMS.tpl');
echo $output;
?>