<?php
	$eName = $_POST['eName'];
	$scID = intval($_POST['scID']);
	$q=[];
	if(isset($_POST['eName'])){
		$eName=$_POST['eName'];
		$q[]="eName like '%".$eName."%'";   
	}
	$co=$db->get_rowData($general->table(49),'coID',1);
	$coData=$general->getJsonFromString($co['coData']);
	$needSMS=1;
	$smsData=[];
	if(isset($coData['dueLastSMS'])){
		if($coData['dueLastSMS']>strtotime('-7 day')){
			$needSMS=0;
		}
	}
	$q[]= 'isActive in(0,1)';
	$sq='where '.implode(' and ',$q);
	$employees=$db->selectAll($general->table(74),$sq.' order by eName asc'); 
	$general->arrayIndexChange($employees,'eID');   
	$designations=$db->selectAll($general->table(72));
	$general->arrayIndexChange($designations,'edID');
	$tBalance=0;
	$tCommissionBalance=0;
	$tsalaryBalance=0;
	$tDBalance=0;
	$TDBalance=0;

	if(!empty($employees)){
		$employeesArray=[];
		foreach($employees as $e){
			$balance=$acc->employeeDueBalance($e['eID'],$scID);
			if($balance!=0){
				$employeesArray[$e['eID']]['balance']= $balance;
				$employeesArray[$e['eID']]['e']= $e;

			}
		}
		//print_r($employeesArray);
		$jArray['k']= $employeesArray;
		$serial=1;
		foreach($employeesArray as $eID=>$e){
			$SalaryBalance=0;
			if($e['e']['hID']>0){
				$SalaryBalance=-$acc->headBalance($e['e']['hID']);
			}


			$balance=$acc->employeeDueBalance($e['e']['eID'],$scID);

			$commissionBalance=$acc->employeeCommissionBalance($e['e']['eID'],$scID);
			$tDBalance=$balance+$commissionBalance;
			$tBalance+=$balance;
			$tCommissionBalance+=$commissionBalance;
			$TDBalance+=$tDBalance;
			if($needSMS==1){
				if($balance>10){
					$smsData[$eID]=$balance;
				}
			}
			$rData[]=[
				's'=>$serial++,
				'e'=>'<a href="javascript:void()" class="btn btn-success" onclick="dueCollectInit('.$e['e']['eID'].')">'. $e['e']['eName'].'</a>',
				'm'=>@$e['e']['eMobile'],
				'd'=>$designations[$e['e']['edID']]['edTitle'],
				'b'=>$general->numberFormat($balance),
				'ds'=>'<a href="?mdl=dueStatment&eID= '.$e['e']['eID'].'" target="_blank" class="btn btn-outline-warning">Statment</a>',
				'c'=>$general->numberFormat($commissionBalance),
				'cs'=>'<a href="?mdl=commissionStatment&eID= '.$e['e']['eID'].'" target="_blank" class="btn btn-outline-primary">Statment</a>',
				'tb'=>$general->numberFormat($tDBalance),


			];
		}
	}
	$rData[]=[
		's' => '',
		'e' => ['t'=>'Total','b'=>1],
		'm'=> ['t'=>'','b'=>1],
		'd' => ['t'=>'','b'=>1],
		'b' => ['t'=>$general->numberFormat($tBalance),'b'=>1],
		'bs' => ['t'=>'','b'=>1],
		'c' => ['t'=>$general->numberFormat($tCommissionBalance),'b'=>1],
		'cs' => ['t'=>'','b'=>1],
		'tb' => ['t'=>$general->numberFormat($TDBalance),'b'=>1],

	];

	$fileName='dealerDueCollect'.TIME.rand(0,999).'.txt';
	$report_data=array(
		'name'      => 'dealerDueCollect',
		'title'     => 'Dealer Due Collect',
		//'info'      => '',
		'fileName'  => $fileName,
		'head'=>array(
			array('title'=>"SN"             ,'key'=>'s'),
			array('title'=>"Employee"       ,'key'=>'e'),
			//array('title'=>"Designation"    ,'key'=>'d'    ,'hw'=>7,'al'=>'r'),
			array('title'=>"Mobile"         ,'key'=>'m'),
			array('title'=>"Designation"     ,'key'=>'d'  ),
			array('title'=>"Due Balance"        ,'key'=>'b'     ,'al'=>'r'),
			array('title'=>"Due Statment"        ,'key'=>'ds'     ,'al'=>'r'),
			array('title'=>"Commission"        ,'key'=>'c'     ,'al'=>'r'),
			array('title'=>"Commission Statment"        ,'key'=>'cs'     ,'al'=>'r'),
			array('title'=>"Total Balance"        ,'key'=>'tb'     ,'al'=>'r'),



		),
		'data'=>$rData
	);
	// print_r($rData);
	$jArray[__LINE__]=$report_data;
	$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;
?>