<?php
/****************************************
/*  Author 	: Kvvaradha
/*  Module 	: Extended HRM
/*  E-mail 	: admin@kvcodes.com
/*  Version : 1.0
/*  Http 	: www.kvcodes.com
/*  Monthly Summary 
*****************************************/
$page_security = 'SA_OPEN';

$path_to_root="../../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/gl/includes/gl_db.inc");

global $path_to_root, $systypes_array, $kv_empl_gender;

	// if(isset($_GET['rep_v'])){
	// 	include($path_to_root . "/modules/ExtendedHRM/includes/Payroll.inc");
	// }
    $year = (isset($_POST['PARAM_0']) ? $_POST['PARAM_0'] : (isset($_GET['PARAM_0']) ? $_GET['PARAM_0'] : 1));
    $month = (isset($_POST['PARAM_1']) ? $_POST['PARAM_1'] : (isset($_GET['PARAM_1']) ? $_GET['PARAM_1'] : 01));
    $result_type = (isset($_POST['PARAM_2']) ? $_POST['PARAM_2'] : (isset($_GET['PARAM_2']) ? $_GET['PARAM_2'] : 0));
	$comment = (isset($_POST['PARAM_3']) ? $_POST['PARAM_3'] : (isset($_GET['PARAM_3']) ? $_GET['PARAM_3'] : ''));;
    $destination = (isset($_POST['PARAM_4']) ? $_POST['PARAM_4'] : (isset($_GET['PARAM_4']) ? $_GET['PARAM_4'] : ''));
    $_POST['REP_ID'] = 806; 
    $dec = user_price_dec();
	//-----------------------------
	
    	//employer
		$emplr_esic_id= GetSingleValue('kv_empl_allowances', 'id', array('esic' => 1, 'type' => 'Employer Contribution'));

		$empl_basic_id= GetSingleValue('kv_empl_allowances', 'id', array('basic' => 1, 'type' => 'Earnings'));

		// $empl_bonus_id= GetSingleValue('kv_empl_allowances', 'id', array('bonu' => $empl_basic_id*8.33%, 'type' => 'Reimbursement'));
		
		$emplr_pf_id= GetSingleValue('kv_empl_allowances', 'id', array('pf' => 1, 'type' => 'Employer Contribution'));
		//$emplr_tax_id= GetSingleValue('kv_empl_allowances', 'id', array('Tax' => 1, 'type' => 'Deductions'));
		//employee
		$empl_esic_id= GetSingleValue('kv_empl_allowances', 'id', array('esic' => 1, 'type' => 'Deductions'));
		$empl_pf_id= GetSingleValue('kv_empl_allowances', 'id', array('pf' => 1, 'type' => 'Deductions'));
	
		if($emplr_esic_id && $emplr_pf_id && $empl_esic_id && $empl_pf_id){
			$sql =" SELECT info.empl_id, CONCAT_WS(' ', info.empl_firstname, info.empl_lastname) AS Name , job.bank_name, job.acc_no, job.branch_detail, job.ifsc, job.ESIC, job.PF,salary.days_worked, salary.gross, salary.adv_sal, salary.date,salary.from_date, salary.to_date, salary.net_pay
			, `salary`.`".$emplr_esic_id."`, `salary`.`".$empl_esic_id."`, `salary`.`".$empl_basic_id."`, `salary`.`".$empl_pf_id."`, `salary`.`".$emplr_pf_id."` FROM ".TB_PREF."kv_empl_info as info, ".TB_PREF."kv_empl_job as job, ".TB_PREF."kv_empl_salary  as salary
			WHERE info.empl_id = job.empl_id AND info.empl_id = salary.empl_id AND salary.month = ".$month." AND salary.year=".$year;
		}	 else {
			$sql =" SELECT info.empl_id, CONCAT_WS(' ', info.empl_firstname, info.empl_lastname) AS Name , job.bank_name, job.acc_no, job.branch_detail, job.ifsc, job.ESIC, job.PF, salary.days_worked, salary.to_date, salary.net_pay
			 FROM ".TB_PREF."kv_empl_info as info, ".TB_PREF."kv_empl_job as job, ".TB_PREF."kv_empl_salary  as salary
			WHERE info.empl_id = job.empl_id AND info.empl_id = salary.empl_id AND salary.month = ".$month." AND salary.year=".$year;						
		}
		
		$result = db_query($sql, _("can't get result"));
	if (db_num_rows($result) > 0)	{
	
		if ($destination)
		include_once($path_to_root . "/modules/ExtendedHRM/reports/excel_report.inc");
		else
		include_once($path_to_root . "/modules/ExtendedHRM/reports/pdf_report.inc");

		$orientation = 'L';		
			
			if($result_type == 'bank'){
				$cols = array(0,55, 180, 330, 430,540, 650);	
				$headers = array(_("S.NO"), _("Name"), _("Account No"), _("IFSC"), _("Salary"), _("Month"));
				$aligns = array('left',	'left', 'left','left', 'left', 'right', 'right');
				$title = _("Payroll Report").' - '.kv_month_name_by_id($month);
			} elseif($result_type == 'esic'){
				$cols = array(0,65, 200, 270, 380, 560, 720); 
				$headers = array(_("IP Number"), _("IP Name"), _("No of Days"), _("Total Monthly Wages"), _("Reason Code for Zero workings days"),_("Last Working Day"));
				$aligns = array('left',	'left','left', 'left', 'left', 'left', 'Left');
				$title =_("ESIC Report").' - '.kv_month_name_by_id($month);
			} elseif($result_type == 'pf'){
				// $cols = array(0,35, 135, 230, 340,520);	
				$cols = array(0,55, 125, 200, 260, 320, 365, 430, 510, 610, 660, 720);
				$headers = array(_("ID"), _("Employee Name"), _("GROSS WAGES"), _("EPF WAGES"),_("EPS WAGES"), _("EDLI WAGES"), _("EPF CONTRI REMITTED"), _("EPS CONTRI REMITTED"), _("EPF EPS DIFF REMITTED"), _("NCP DAYS"), _("REFUND OF ADVANCES"));
				$aligns = array('left',	'left', 'left', 'left','left', 'left',	'right', 'right','right', 'right', 'right', 'right');
				// $aligns = array('left',	'left', 'left','left', 'right', 'right');
				$title =_("PF Report").' - '.kv_month_name_by_id($month);
			}
			
// GetRow('kv_empl_salary',array($row['empl_id']));


			$rep = new FrontReport($title, $title, user_pagesize(), 9, $orientation);
	   
			$rep->SetHeaderType('Header');	
		    $rep->Font();			
		    $rep->Info(null, $cols, $headers, $aligns);	
		    $rep->NewPage();
			
			$total=$total1 =$total2= 0;
			$serial_no = 1;
			while ($row = db_fetch_assoc($result)) {			

				$rep->NewLine(1, 2);
				
				if($result_type == 'bank'){
					$rep->TextCol(0, 1, $serial_no);
					$rep->TextCol(1, 2, $row['Name']);
					$rep->TextCol(2, 3, $row['acc_no']);
					// $rep->TextCol(4, 5, $row['branch_detail']);
					$rep->TextCol(3, 4, $row['ifsc']);
					$rep->TextCol(4, 5, number_format2($row['net_pay'], 2));
					$rep->TextCol(5, 6, $row['to_date']);
					$total += $row['net_pay'];
					$serial_no++;
				}elseif($result_type == 'esic'){
				
					$rep->TextCol(0, 1, $row['ESIC']);
					$rep->TextCol(1, 2, $row['Name']);
					$rep->TextCol(2, 3, $row['days_worked']);
					$rep->TextCol(3, 4, $row['gross']);
					$rep->TextCol(4, 5, 0);
					$rep->TextCol(5, 6, $row['to_date']);
					// $rep->TextCol(4, 5, number_format2($row[$emplr_esic_id], 2));
					// $$total1 += $row[$emplr_esic_id];
					// $rep->TextCol(4, 5, number_format2($row[$empl_esic_id], 2));
					// $$total2 += $row[$empl_esic_id];
				}elseif($result_type == 'pf'){ 
					// $rep->TextCol(0, 1, $row['empl_id']);
					$rep->TextCol(0, 1, $row['PF']);
					$rep->TextCol(1, 2, $row['Name']);
					$rep->TextCol(2, 3, $row['gross']);
					$rep->TextCol(3, 4, $row[$empl_basic_id]);
					$rep->TextCol(4, 5, $row[$empl_basic_id]);
					$rep->TextCol(5, 6, $row[$empl_basic_id]);
					$rep->TextCol(6, 7, $row[$emplr_pf_id]);
					$rep->TextCol(7, 8, round($row[$empl_basic_id] * 0.0833));
					$rep->TextCol(8, 9, $row[$emplr_pf_id] - round($row[$empl_basic_id] * 0.0833));
					$rep->TextCol(9, 10, $row['days_worked']);
					$rep->TextCol(10, 11, $row['adv_sal']);
					// $rep->TextCol(11, 12, number_format2($row[$emplr_pf_id], 2));
					// $total1 += $row[$emplr_pf_id];
					// $rep->TextCol(12, 13, number_format2($row[$empl_pf_id], 2));
					// $total2 += $row[$empl_pf_id];
				}
			}
			$rep->NewLine(2);
			$rep->row = 160;
			if($result_type == 'bank'){
				$rep->TextCol(3,4, _("Salary Payable")." :");
				$rep->TextCol(4, 5,	number_format2($total,$dec));
			}elseif($result_type == 'esic'){
				// $rep->TextCol(2,3, _("Net Amount")." :");
				// $rep->TextCol(3, 4,	number_format2($total1,$dec));
				// $rep->TextCol(4, 5,	number_format2($total2,$dec));
			}elseif($result_type == 'pf'){ 
				// $rep->TextCol(2,3, _("Net Amount")." :");
				// $rep->TextCol(3, 4,	number_format2($total1,$dec));
				// $rep->TextCol(4, 5,	number_format2($total2,$dec));

			}

			$rep->NewLine(1);
			$rep->row = 180;	
			if($comment){				
				$rep->SetTextColor(0, 0, 0);
				$rep->Text(40, _("Comments"),0,0,65);
				$rep->Text(200, $comment,0,0,65);  //$rep->NewLine(2);	
			}		
			$rep->Line(65, 0.00001,0,0);		
			
		if ($rep->row < $rep->bottomMargin )
			$rep->NewPage();	
		$rep->End(); //1, 'Payslip ');
	}else{
		display_warning(_("No Payroll Entry Found For Selected Period."));
	}
?>