<?php 
include_once("../class/class.MySQL.php");
$db	=new MySQL();
error_reporting(0);
$action = $_GET['act'];

if($action == "dashboard" ){
	$requestedmth = $_GET['month'];
	$requestedyear = $_GET['year'];
	
	
	if(!$requestedmth){
		$requestedmth	 	=date("m",strtotime(" -1 months"));
		 $requestedyear		=date("Y",strtotime(" -1 months"));
		}
		
	
	//echo $requestedmth;
	//$where['MONTH(payment_date)'] = $requestedmth;
	//$where['YEAR(payment_date)'] = $requestedyear;
	$salaryPaidReport = $db->ExecuteSQL("SELECT *, sum(total_paid) as totalPaidAmt FROM payroll_salary_pay WHERE MONTH(payment_date)='$requestedmth' and YEAR(payment_date)='$requestedyear'");
	
	$deductionAndEarningsDetails = $db->ExecuteSQL("SELECT sum(A.deduction_total_amount) as totalDeduction, B.deduction_benefit  FROM payroll_payment_details A
															LEFT JOIN payroll_benifits_deductions B ON A.deduction_id = B.id
															WHERE MONTH(date)='$requestedmth' and YEAR(date)='$requestedyear' GROUP BY B.deduction_benefit"); 
															
															
	$deductionAndEarningsList = $db->ExecuteSQL("SELECT sum(A.deduction_total_amount) as totalDeduction, B.deduction_benefit,B.name  FROM payroll_payment_details A
															LEFT JOIN payroll_benifits_deductions B ON A.deduction_id = B.id
															WHERE MONTH(date)='$requestedmth' and YEAR(date)='$requestedyear' and B.id !=0 GROUP BY B.id"); 
															
													
											
	$totalEarnings = 0;
	$totalDeduction = 0;														
	$totalContribution =0;
	foreach($deductionAndEarningsDetails as $rows){
	
		if($row['deduction_benefit'] == 1){
		if($row['totalDeduction']!==null){
			$totalDeduction = $row['totalDeduction'];
		}
		}else{
			if($row['totalDeduction']!==null){
				$totalEarnings = $row['totalDeduction'];
			}
		}
		
	}
	//print_r($salaryPaidReport);
	$salaryPaidReport['totalDeduction'] = $totalDeduction;
	$salaryPaidReport['totalEarnings'] = $totalEarnings;
	$salaryPaidReport['earningsList']  =$deductionAndEarningsList;
	
	
	echo '{"dashboard":'.json_encode($salaryPaidReport).'}';
		
}
