<?php
	$dRange=date('d-m-Y').' to '.date('d-m-Y');
	if(isset($_POST['dRange'])){
		if($_POST['dRange']!=''){
			$dRange=$_POST['dRange'];
		}
	}
	$reportInfo=array();
	$reportInfo[]='Date : '.$dRange;
	$general->getFromToFromString($dRange,$from,$to);
	$jArray[__LINE__]=$from;
	$jArray[__LINE__]=$general->make_date($from,'time');
	$jArray[__LINE__]=$to;
	$jArray[__LINE__]=$general->make_date($to,'time');
	$vType  = intval(@$_POST['vType']);
	$cnID   = intval(@$_POST['cnID']);
	$hID    = intval(@$_POST['hID']);
	$uID    = intval(@$_POST['uID']);
	$scID  = 0;//intval(@$_POST['scID']);
	$isSummary= 0;
	$tData=$acc->cashFlowReport($from,$to,$hID,$uID,$vType,'',$jArray);
	$jArray[__LINE__]=$tData;
	$rData=array();
	$balance=0;
	$totalDr=0;
	$totalCr=0;
	$wob=0;

	$cashHead=$acc->getSystemHead(AH_CASH);
	$openingBalance=$acc->headBalance($cashHead,strtotime('-1 second',$from),$scID,$uID);
	$balance+=$openingBalance;
	$withoutOpaningBbalance=0;

	$rData[]=array(
		's' => '',
		'h'=> array('t'=>'Opening Balance','b'=>1,'col'=>4),
		'u' => array('t'=>false),
		'dt'=> array('t'=>false),
		'p' => array('t'=>false),
		'i' => '',
		'o' => '',
		'wb' => '',
		'b' => array('t'=>$general->numberFormat($openingBalance),'b'=>1),
		'r' => ''
	);

	$dStatus=$db->permission(90);
	$canPrint=0;//$db->permission(100);
	if(isset($tData['transactions'])){
		$uIDs=array();
		$jArray[__LINE__]=$tData['transactions'];
		$veIDs=array();
		foreach($tData['transactions'] as $tr){
			$uIDs[$tr['createdBy']]=$tr['createdBy'];
			$veIDs[$tr['voucher_id']]=$tr['voucher_id'];
		}
		//$jArray[__LINE__]=$veIDs;
		$allVouchers=$db->selectAll('a_voucher_entry','where id in('.implode(',',$veIDs).')');
		$general->arrayIndexChange($allVouchers,'id');
		$allUsers=$db->selectAll('users','where id in('.implode(',',$uIDs).')');
		$general->arrayIndexChange($allUsers,'id');

		foreach($tData['transactions'] as $k=>$tr){
			$tData['transactions'][$k]['type']=$allVouchers[$tr['voucher_id']]['type'];
		}
		$general->arrayContentShow($tData);
		$total=1;
		$heads      = $tData['heads'];

		$canAppChe = true;

		foreach($tData['transactions'] as $tr){
			$totalDr+=$tr['debit'];
			$totalCr+=$tr['credit'];
			$balance+=$tr['debit'];
			$balance-=$tr['credit'];
			$bl=$tr['debit'];
			$bl-=$tr['credit'];
			
			$withoutOpaningBbalance+=$bl;


			$data=array(
				's' => $total++,
				'h' => $heads[$tr['head_id']]['title'],
				//'h' => $heads[$tr['hID']]['hTitle'].' '.$tr['veID'].' '.$tr['vType'],
				'u' => @$allUsers[$tr['createdBy']]['username'],
				'dt'=> date('d-m-Y H:i',$tr['time']),
				'p' => $tr['note'],
				'i' => $general->numberFormat($tr['debit']),
				'o' => $general->numberFormat($tr['credit']),
				'wb' => $general->numberFormat($withoutOpaningBbalance),
				'b' => $general->numberFormat($balance),
			);


			if($dStatus){
				if(in_array($tr['type'],REMOVE_ABLE_VOUCHER_TYPE)){
					$data['r']='<button  onclick="voucherRemove('.$tr['voucher_id'].')">X</button>';
				}
				else{
					$data['r']='';//$tr['vType'];
				}
			}
			if($canPrint){
				$data['pr']='<a href="?print=voucher&voucher_id='.$tr['voucher_id'].'" target="blank">Print</a>';
			}
			$rData[]=$data;

		}
	}
	$rData[]=array(
		's' => '',
		'h'=> array('t'=>'Total','b'=>1,'col'=>4),
		'u' => array('t'=>false),
		'dt'=> array('t'=>false),
		'p' => array('t'=>false),
		'i' => ['t'=>$general->numberFormat($totalDr),'b'=>1],
		'o' => ['t'=>$general->numberFormat($totalCr),'b'=>1],
		'wb' => ['t'=>$general->numberFormat($withoutOpaningBbalance),'b'=>1],
		'b' => ['t'=>$general->numberFormat($balance),'b'=>1],
		'r' => ''
	);


	$fileName='cashflowreport_'.TIME.rand(0,999).'.txt';
	$headData=array(
		array('title'=>"#"          ,'key'=>'s' ,'w'=>5 ,'hw'=>4),
		array('title'=>"Head"       ,'key'=>'h' ,'w'=>20,'hw'=>8),
		array('title'=>"User"       ,'key'=>'u' ,'w'=>10,'hw'=>8),
		array('title'=>"Date time"  ,'key'=>'dt','w'=>20,'hw'=>12),
		array('title'=>"Description",'key'=>'p' ,'w'=>20),
		array('title'=>"In"         ,'key'=>'i' ,'w'=>20,'hw'=>8    ,'al'=>'r'),
		array('title'=>"Out"        ,'key'=>'o' ,'w'=>20,'hw'=>8    ,'al'=>'r'),
		array('title'=>"Balance without opening "        ,'key'=>'wb' ,'w'=>20,'hw'=>8    ,'al'=>'r'),
		array('title'=>"Balance"    ,'key'=>'b' ,'w'=>20,'hw'=>8    ,'al'=>'r')
	);
	if($canPrint){
		$headData[]=array('title'=>"Print",'key'=>'pr' ,'w'=>20,'noForExcel'=>1);
	}
	if($dStatus){
		$headData[]=array('title'=>"X" ,'key'=>'r' ,'hw'=>1,'noForExcel'=>1);
	}

	$report_data=array(
		'name'      => 'Cash_Flow_Report'.date('d_m_Y',$from).'_'.date('d_m_Y',$to),
		'title'     => 'Cash Flow Report',
		'info'      => $reportInfo,
		'fileName'  => $fileName,
		'head'      => $headData,
		'data'      => $rData
	);

	$gAr['report_data']= $report_data;

	textFileWrite(json_encode($report_data),ROOT_DIR.'/print_file/'.$fileName);
	$jArray['html']     = $general->fileToVariable(ROOT_DIR.'/ajax/report.phtml');

	$jArray['status']=1;
