<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Overtime_report extends MY_Controller
{
	function __construct()
	{
		parent::__construct();	
		$this->load->model('Admin/model_company');
		$this->load->model('CRM/model_crm');
		$this->load->model('CRM/country');
		$this->load->model('CRM/general_model');
		$this->load->model('hr/OT/model_ot');

		$this->logged_user = $this->session->userdata('user');

		if($this->logged_user)
		{
			$this->login->checkModuleAccess(4); //HR
			$this->logged_module = $this->session->userdata('module_data');
		}
		else
		{
			redirect(base_url()); 
		}	 
	}

	function myOTSubmissionList_print($mode=false)
	{
		$date = date("d-m-Y");

		$header = array();
		$header['title'] = 'MY OT SUBMISSION LIST AS AT ' . $date;
		//excel file data
		// $year     = ($year != "") ? $year : 'ALL';
		// $month 	  = ($month != "") ? date('F', mktime(0, 0, 0, $month, 10)) : 'ALL';

		// $headerData['rptTitle'] = 'MY OT SUBMISSION LIST AS AT ' . $date;
		// $headerData['leftContent'] = array(
		// 	'year' ." : ". $year,
		// 	'month' ." : ". $month
		// );

		// $bodyData['rptDataDetail'] = $OTSubmissionList;
		// $bodyData['tableHeader'] = array(
		// 	'Year',
		// 	'Month',
		// 	'Total Hours Applied',
		// 	'Total Hours Approved'
		// );
		// $bodyData['selectFields'] = array(
		// 	'year', 
		// 	'MONTHNAME(STR_TO_DATE(month, "%m"))',
		// 	'totalHoursApplied', 
		// 	'totalHoursApproved'
		// );
		// $bodyData['excelTitle'] = "MY OT SUBMISSION LIST AS AT " . $date;

		// $footerData = array();
		//die(debug($this->session->userdata));

		if(!$mode){
			$this->load->view('hr/report/header.php', $header);
			$this->load->view('hr/overtime/report/ot_submission_list.php', $header);
			$this->load->view('hr/overtime/report/footer.php');
		}else{
			// $this->load->helper('excel');
			// excel($headerData, array($bodyData), $footerData);
		}
	}

	function myClaimSubmissionList_print($mode=false)
	{
		$date = date("d-m-Y");

		$header = array();
		$header['title'] = 'MY CLAIM SUBMISSION LIST AS AT ' . $date;

		if(!$mode){
			$this->load->view('hr/report/header.php', $header);
			$this->load->view('hr/overtime/report/claim_submission_list.php', $header);
			$this->load->view('hr/overtime/report/footer.php');
		}else{
			//excel
		}
	}

	function otSummary_print($mode=false)
	{
		$date = date("d-m-Y");

		$header = array();
		$header['title'] = 'OT SUMMARY BY DEPARTMENT AS AT ' . $date;

		if(!$mode){
			$this->load->view('hr/report/header.php', $header);
			$this->load->view('hr/overtime/report/ot_summary.php', $header);
			$this->load->view('hr/overtime/report/footer.php');
		}else{
			//excel
		}
	}

	function adminOTSubmissionList_print($mode=false)
	{
		$date = date("d-m-Y");

		$header = array();
		$header['title'] = 'ADMIN OT SUBMISSION LIST AS AT ' . $date;

		if(!$mode){
			$this->load->view('hr/report/header.php', $header);
			$this->load->view('hr/overtime/report/admin_ot_summary.php', $header);
			$this->load->view('hr/overtime/report/footer.php');
		}else{
			//excel
		}
	}

	public function getStaffByDept()
	{
		$dept_id = $this->input->post('dept_id');
    	$this->load->model('hr/OT/model_ot');
    	$data['stafflist']=$this->model_ot->staff_list($dept_id);

    	print json_encode(array("status"=>"success", "message"=>$data['stafflist']));
	}

	function OTReport()
	{
		$data['role']		 = $this->logged_module['role'];
		$data['page_setup']  = $this->template->pageSetup('HR', 'home', 'Overtime');

		$data['company'] 	 = $this->model_company->getCompanies(1);
		$data['month']		 = $this->model_crm->getMonth();
		$data['yearList']    = $this->db->query('select distinct year from hr__ot')->result();
		$data['department']  = $this->model_company->getDepartments();
		$data['staff']		 = $this->model_user->getUsers();
		$data['staff1']		 = $this->model_user->getUsers();
		$data['clientList']	 = $this->model_crm->getCustomer();
		$data['otDetail'] 	 = $this->model_ot->getOTDetailAll();

		$this->template->view('hr/overtime/report/ot_report', $data);
	}

	public function fnLoadOTDetailList()
	{
		$this->load->model('CRM/general_model');

		$department_id 	= $this->input->post('department_id');
		$staff_id 		= $this->input->post('staff_id');
		$dayType_id		= $this->input->post('dayType_id');
		$status 		= $this->input->post('status');
		$client_id 		= $this->input->post('client_id');
		$start_date 	= $this->input->post('start_date');
		$end_date 		= $this->input->post('end_date');
		$svdepartment_id = $this->input->post('svdepartment_id');
		$sv_id 			= $this->input->post('sv_id');
		$is_delete 		= 0;

		if($department_id!=0){
			$filter['b.department_id'] = $department_id;
		}

		if($staff_id!=0){
			$filter['a.peopleID'] = $staff_id;
		}

		if($svdepartment_id!=0){
			$filter['a.svDeptID'] = $svdepartment_id;
		}

		if($sv_id!=0){
			$filter['a.svID'] = $sv_id;
		}

		if($dayType_id!=0){
			$filter['a.dayType'] = $dayType_id;
		}

		if($status!=-1){
			$filter['a.status'] = $status;
		}

		if($client_id!=0){
			$filter['a.clientID'] = $client_id;
		}

		if($start_date!=0){
			$filter3['a.otDate'] = date('Y-m-d', strtotime($start_date));
		} else {
			$filter3 = false;
		}

		if($end_date!=0){
			$filter4['a.otDate'] = date('Y-m-d', strtotime($end_date));
		} else {
			$filter4 = false;
		}

		if($is_delete==0){
			$filter['a.is_delete'] = 0;
		}

		$filter2 = false;
		$filter5 = false;
		$filter6 = false;

		$select = array('a.*, TIME_FORMAT(a.startTime, "%h:%i %p") as startTime, TIME_FORMAT(a.endTime, "%h:%i %p") as endTime, DATE_FORMAT(a.otDate, "%d/%m/%Y") as otDate, b.name as staffName, concat(c.name,"[",d.name,"]") as deptName, if(e.id > 0, e.name, "Others") as clientName, i.name as status_by_name, j.name as svName, k.name as svDeptName');

		$otDetailList = $this->general_model->get_byFilter2(
			'hr__ot__detail a',
			$select,
			$filter,
			$filter2,
			$filter3,
			$filter4,
			$filter5,
			$filter6,
			array(
				array('user__employment_detail b', 'b.user_id = a.peopleID','left'),
				array('company__department c', 'c.id = b.department_id','left'),
				array('company__branch d', 'd.id = c.branch_id','left'),
				array('customer e', 'e.id = a.clientID','left'),
				array('user i','i.id = a.status_by','left'),
				array('user j','j.id = a.svID','left'),
				array('company__department k','k.id = a.svDeptID','left')
			),
			array(
				array('a.id'),
				'ASC'
			)
		);

		$query = $this->db->last_query();

		if ($otDetailList) {
	    	echo json_encode(array('status' => 'success', 'message' => $otDetailList, 'query' => $query));
		} else {
		    echo json_encode(array('status' => 'failed', 'message' => 'No OT Details Found.', 'query' => $query));
		}
	}

	function printOvertimeReport($department_id,$staff_id,$dayType_id,$status,$client_id,$start_date,$end_date,$svdepartment_id,$sv_id)
	{
		$this->load->model('CRM/general_model');

		$date = date("d-m-Y");
		$is_delete 	= 0;

		if($department_id!=0){
			$filter['b.department_id'] = $department_id;
		}

		if($staff_id!=0){
			$filter['a.peopleID'] = $staff_id;
		}

		if($svdepartment_id!=0){
			$filter['a.svDeptID'] = $svdepartment_id;
		}

		if($sv_id!=0){
			$filter['a.svID'] = $sv_id;
		}

		if($dayType_id!=0){
			$filter['a.dayType'] = $dayType_id;
		}

		if($status!="-1"){
			$filter['a.status'] = $status;
		}

		if($client_id!=0){
			$filter['a.clientID'] = $client_id;
		}

		if($start_date!=0){
			$filter3['a.otDate'] = date('Y-m-d', strtotime($start_date));
		} else {
			$filter3 = false;
		}

		if($end_date!=0){
			$filter4['a.otDate'] = date('Y-m-d', strtotime($end_date));
		} else {
			$filter4 = false;
		}

		if($is_delete==0){
			$filter['a.is_delete'] = 0;
		}

		$filter2 = false;
		$filter5 = false;
		$filter6 = false;

		$select = array('concat(c.name,"[",d.name,"]") as deptName, b.name as staffName, DATE_FORMAT(a.otDate, "%d/%m/%Y") as otDate, if(e.id > 0, e.name, "Others") as clientName, a.task, (CASE WHEN a.mealAllowance=1 THEN "Claimable (Included)" WHEN a.mealAllowance=2 THEN "Claimable (Not Included)" ELSE "Unclaimable" END) as mealAllowance, concat("<b>Start : </b>", TIME_FORMAT(a.startTime, "%h:%i %p"), "<br><b>End : </b>", TIME_FORMAT(a.endTime, "%h:%i %p"), "<br><b>Break : </b>", a.refreshment, " minutes") as startTime, (CASE WHEN a.dayType=1 THEN "Normal Day" WHEN a.dayType=2 THEN "Rest Day" WHEN a.dayType=3 THEN "Public Holiday" WHEN a.dayType=4 THEN "Weekend" WHEN a.dayType=5 THEN "Weekdays" END) as dayType, a.totalHoursApplied, concat(j.name, "<br><b> - ", k.name, "</b>") as svDeptName,
			(CASE WHEN a.status=1 THEN concat("Approved <br><i><small>Remarks : ", a.remarks, "</small></i><br><i><small>Approved by : ", i.name, "</small></i>")
			 WHEN a.status=2 THEN concat("Rejected <br><i><small>Remarks : ", a.remarks, "</small></i><br><i><small>Rejected by : ", i.name, "</small></i>")
			 ELSE concat("Pending <br><i><small>Remarks : ", a.remarks, "</small></i>") END) as status');

		$otDetailList = $this->general_model->get_byFilter2(
			'hr__ot__detail a',
			$select,
			$filter,
			$filter2,
			$filter3,
			$filter4,
			$filter5,
			$filter6,
			array(
				array('user__employment_detail b', 'b.user_id = a.peopleID','left'),
				array('company__department c', 'c.id = b.department_id','left'),
				array('company__branch d', 'd.id = c.branch_id','left'),
				array('customer e', 'e.id = a.clientID','left'),
				array('user i','i.id = a.status_by','left'),
				array('user j','j.id = a.svID','left'),
				array('company__department k','k.id = a.svDeptID','left')
			),
			array(
				array('a.id'),
				'ASC'
			)
		); 

		if($dayType_id ==1){
			$dayType = "Normal Day";
		}else if($dayType_id ==2){
			$dayType = "Rest Day";
		}else if($dayType_id ==3){
			$dayType = "Public Holiday";
		}else if($dayType_id ==4){
			$dayType = "weekend";
		}else if($dayType_id ==5){
			$dayType = "Weekdays";
		}else{
			$dayType = "ALL";
		}

		if($status ==1){
			$statusWord = "Approved";
		}else if($status ==2){
			$statusWord = "Rejected";
		}else if($status ==0){
			$statusWord = "Pending";
		}else{
			$statusWord = "ALL";
		}

		if($client_id > 0){
			$client = $this->model_crm->getCustomer($client_id);
		}else if($client_id ==-1) {
			$client = "Others";
		}else{
			$client = "ALL";
		}

		$body = array(
				'table' => array(
					'header' => array(
						'Department',
						'Staff Name',
						'Date',
						'Client',
						'Task',
						'Meal Allowance',
						'Time',
						'Day Type',
						'Total Hours',
						'Supervisor',
						'Status'
					),
					'right-column' => array(9),
					'total-column' => array(),
					'body' => $otDetailList,
					'footer' => array(
						'title' => 'Total',
						'total-column' => array(9,10,11)
					)
				),
				'filter' => array(					
					'Department' 	=> ($department_id != 0) ? $this->model_ot->getDepartments(NULL, $department_id)[0]->branch_name: "ALL",
					'Staff Name' 	=> ($staff_id != 0) ? $this->model_ot->getStaffName($staff_id)[0]->name : 'ALL',
					'Day Type' 		=> $dayType,
					'Status' 		=> $statusWord,
					'Client'		=> $client,
					'Start Date'	=> ($start_date != 0) ? $start_date : 'ALL',
					'End Date'		=> ($end_date != 0) ? $end_date : 'ALL',
					'SV Department' => ($svdepartment_id != 0) ? $this->model_ot->getDepartments(NULL, $svdepartment_id)[0]->branch_name: "ALL",
					'SV Name' 		=> ($sv_id != 0) ? $this->model_ot->getStaffName($sv_id)[0]->name : 'ALL',
				)
			);

		$header = array();
		$header['title'] = 'OVERTIME DETAILS REPORT AS AT ' . $date;

		$footerData = array();

		$this->load->view('hr/overtime/report/header.php', $header);
		$this->load->view('hr/overtime/report/printList.php', $body);
		$this->load->view('hr/overtime/report/footer.php');
	}

	function claimReport()
	{
		$data['role']		 	= $this->logged_module['role'];
		$data['page_setup']  	= $this->template->pageSetup('HR', 'home', 'Overtime');

		$data['company'] 	 	= $this->model_company->getCompanies(1);
		$data['month']		 	= $this->model_crm->getMonth();
		$data['yearList']    	= $this->db->query('select distinct year from hr__ot')->result();
		$data['department']  	= $this->model_company->getDepartments();
		$data['businessEntity'] = $this->model_company->getBusinesses();
		$data['staff']		 	= $this->model_user->getUsers();
		$data['clientList']	 	= $this->model_crm->getCustomer();
		$data['claimDetail'] 	= $this->model_ot->getClaimDetailAll();

		$this->template->view('hr/overtime/report/claim_report', $data);
	}

	public function fnLoadClaimDetailList()
	{
		$this->load->model('CRM/general_model');

        $be_id 			= $this->input->post('be_id');
		$department_id 	= $this->input->post('department_id');
		$staff_id 		= $this->input->post('staff_id');
		$status 		= $this->input->post('status');
		$paid_status 	= $this->input->post('paid_status');
		$month 			= $this->input->post('month');
		$year 			= $this->input->post('year');
		$is_delete 		= 0;

		if($be_id!=0){
			$filter['b.business_entity_id'] = $be_id;
		}

		if($department_id!=0){
			$filter['b.department_id'] = $department_id;
		}

		if($staff_id!=0){
			$filter['a.peopleID'] = $staff_id;
		}

		if($status!=-1){
			$filter['a.claim_status'] = $status;
		}

		if($paid_status!=-1){
			$filter['a.paid_status'] = $paid_status;
		}

		if($month!=0){
			$filter['a.month'] = $month;
		}

		if($year!=0){
			$filter['a.year'] = $year;
		}

		if($is_delete==0){
			$filter['a.is_delete'] = 0;
		}

		$filter2 = false;
		$filter3 = false;
		$filter4 = false;
		$filter5 = false;
		$filter6 = false;

		$select = array('a.*, b.name as staffName, concat(c.name,"[",d.name,"]") as deptName, e.name as be_name');

		$claimDetailList = $this->general_model->get_byFilter2(
			'hr__ot a',
			$select,
			$filter,
			$filter2,
			$filter3,
			$filter4,
			$filter5,
			$filter6,
			array(
				array('user__employment_detail b', 'b.user_id = a.peopleID','left'),
				array('company__department c', 'c.id = b.department_id','left'),
				array('company__branch d', 'd.id = c.branch_id','left'),
				array('company__business_entity e', 'e.id = b.business_entity_id','left')

			),
			array(
				array('a.id'),
				'ASC'
			)
		);

		$query = $this->db->last_query();

		if ($claimDetailList) {
	    	echo json_encode(array('status' => 'success', 'message' => $claimDetailList, 'query' => $query));
		} else {
		    echo json_encode(array('status' => 'failed', 'message' => 'No OT Details Found.', 'query' => $query));
		}
	}

	function printClaimReport($be_id,$department_id,$staff_id,$claim_status,$paid_status,$month,$year)
	{
		$this->load->model('CRM/general_model');

		$date = date("d-m-Y");
		$is_delete 	= 0;

		if($be_id!=0){
			$filter['b.business_entity_id'] = $be_id;
		}

		if($department_id!=0){
			$filter['b.department_id'] = $department_id;
		}

		if($staff_id!=0){
			$filter['a.peopleID'] = $staff_id;
		}

		if($claim_status!=-1){
			$filter['a.claim_status'] = $claim_status;
		}

		if($paid_status!=-1){
			$filter['a.paid_status'] = $paid_status;
		}

		if($month!=0){
			$filter['a.month'] = $month;
		}

		if($year!=0){
			$filter['a.year'] = $year;
		}

		if($is_delete==0){
			$filter['a.is_delete'] = 0;
		}

		$filter2 = false;
		$filter3 = false;
		$filter4 = false;
		$filter5 = false;
		$filter6 = false;

		$select = array('e.name as be_name, concat(c.name,"[",d.name,"]") as deptName, b.name as staffName, a.year, MONTHNAME(concat("2000-",a.month,"-01")) as month, a.totalHoursApproved, a.totalConvertToLeave, a.totalClaim, (CASE WHEN a.claim_status=1 THEN "SUBMITTED" WHEN a.claim_status=2 THEN "VERIFIED" ELSE "PENDING" END) as claim_status, (CASE WHEN a.paid_status=1 THEN "PAID" ELSE "UNPAID" END) as paid_status');

		$claimDetailList = $this->general_model->get_byFilter2(
			'hr__ot a',
			$select,
			$filter,
			$filter2,
			$filter3,
			$filter4,
			$filter5,
			$filter6,
			array(
				array('user__employment_detail b', 'b.user_id = a.peopleID','left'),
				array('company__department c', 'c.id = b.department_id','left'),
				array('company__branch d', 'd.id = c.branch_id','left'),
				array('company__business_entity e', 'e.id = b.business_entity_id','left')

			),
			array(
				array('a.id'),
				'ASC'
			)
		); 

		if($claim_status ==1){
			$statusWord = "SUBMITTED";
		}else if($claim_status ==2){
			$statusWord = "VERIFIED";
		}else if($claim_status ==0){
			$statusWord = "PENDING";
		}else{
			$statusWord = "ALL";
		}

		if($paid_status ==1){
			$paid_statusWord = "PAID";
		}else if($paid_status ==0){
			$paid_statusWord = "UNPAID";
		}else{
			$paid_statusWord = "ALL";
		}

		$body = array(
				'table' => array(
					'header' => array(
						'Business Entity',
						'Department',
						'Staff Name',
						'Year',
						'Month',
						'Total Hours Verified',
						'Total Convert CO (Hours)',
						'Total Claim (RM)',
						'Claim Status',
						'Paid Status'
					),
					'right-column' => array(6,7,8),
					'total-column' => array(),
					'body' => $claimDetailList,
					'footer' => array(
						'title' => 'Total',
						'total-column' => array(6,7,8,9,10)
					)
				),
				'filter' => array(			
					'Business Entity' => ($be_id != 0) ? $this->model_ot->getBusinesses(NULL, $be_id)[0]->name: "ALL",		
					'Department' 	=> ($department_id != 0) ? $this->model_ot->getDepartments(NULL, $department_id)[0]->branch_name: "ALL",
					'Staff Name' 	=> ($staff_id != 0) ? $this->model_ot->getStaffName($staff_id)[0]->name : 'ALL',
					'Year' 			=> ($year !=0) ? $year : "ALL",
					'Month' 		=> ($month !=0) ? date("F", mktime(0, 0, 0, $month, 10)) : "ALL",
					'Claim Status'	=> $statusWord,
					'Paid Status'	=> $paid_statusWord
				)
			);

		$header = array();
		$header['title'] = 'OVERTIME CLAIM DETAILS REPORT AS AT ' . $date;

		$footerData = array();

		$this->load->view('hr/overtime/report/header.php', $header);
		$this->load->view('hr/overtime/report/printList.php', $body);
		$this->load->view('hr/overtime/report/footer.php');
	}

	function printMyOTClaim($otID)
	{
		$user_id 	  				= $_SESSION['user']['id'];
		$userDetail 				= $this->model_user->getUsers($user_id);
		$data['name'] 				= $userDetail['name'];
		$data['ic_no'] 				= $userDetail['ic_no'];
		$data['staff_id'] 			= $userDetail['staff_id'];
		$data['department_name']	= $userDetail['department_name'];
		$data['position_name']		= $userDetail['title'];
		$data['position']			= $userDetail['position_name'];
		$data['otID']				= $otID;
		$data['rsOt'] 				= $this->model_general->fetchSingle('hr__ot ', array('*'), array('ID' => $otID));
		$data['basicSalary'] 		= $this->model_ot->getBasicSalary($user_id);

		$data['OTSummaryByDept'] 		  = $this->model_ot->getOTSummaryByDept($otID);
		$data['prev_balanceCarryForward'] = $this->model_ot->get_prev_balanceCarryForward($user_id);

		$select = array('*');
		$where  = array('id' => $otID);
		$join   = array();
		$order  = array();
		$data['otMain'] = $this->model_general->fetchAll('hr__ot', $select, $where, $join, $order);

		$month = date("F", mktime(0, 0, 0, $data['otMain'][0]['month'], 10));
		$year  = $data['otMain'][0]['year'];

		$select = array('a.*, d.id as deptID, d.name as deptName, b.name as name, e.name as clientName, b.ic_no as ic, b.staff_id as staffID, f.position as position, g.totalHoursApproved, g.totalHoursApplied as totalHoursAppliedTotal, h.totalClaim, h.totalConvertToLeave, h.claim_status, h.paid_status, g.normal, g.rest, g.public, g.weekend, g.weekdays, g.NormalMeal, g.RestMeal, g.PublicMeal, g.WeekendMeal, g.WeekdaysMeal, i.name as status_by_name');
		$where = array( 'a.otID' => $otID,'a.is_delete' => 0);
		$join = array(array('user b','b.id = a.peopleID','left'), 
			array('user__employment_detail c', 'c.user_id = b.id', 'left'),
			array('company__department d', 'd.id = c.department_id', 'left'), 
			array('view_ot g', 'g.otID = a.otID', 'left'), 
			array('customer e', 'e.id = a.clientID', 'left'), 
			array('hr__ot h', 'h.id = a.otID', 'left'),
			array('company__position f', 'f.id = c.position_id', 'left'),
			array('user i','i.id = a.status_by','left'));
		$order = array();

		$data['otLists'] = $this->model_general->fetchAll('hr__ot__detail a', $select, $where, $join, $order);
		
		if($data['otLists'][0]['claim_status'] == 1){
			$claim_status = 'SUBMITTED';
		} else if($data['otLists'][0]['claim_status'] == 2){
			$claim_status = 'VERIFIED';
		} else{
			$claim_status = 'PENDING';
		}

		if($data['otLists'][0]['paid_status'] == 1){
			$paid_status = 'PAID';
		} else{
			$paid_status = 'UNPAID';
		}

		$header['title'] = 'MY OVERTIME CLAIM FOR ' . $month .' '.$year;
		$header['month'] = $month;
		$header['year']  = $year;
		$header['claim_status']  = $claim_status;
		$header['paid_status']   = $paid_status;

		$this->load->view('hr/report/header.php', $header);

		if($data['rsOt']['year'] == '2020' && $data['rsOt']['month'] >= 8){
			$this->load->view('hr/overtime/report/ot_claim_print_adjust.php', $data);
		}else if($data['rsOt']['year'] > '2020'){
			$this->load->view('hr/overtime/report/ot_claim_print_adjust_2021.php', $data);
		}else{
			$this->load->view('hr/overtime/report/ot_claim_print.php', $data);
		}

		$this->load->view('hr/overtime/report/footer.php');
	}

	function printOTClaim($otID,$user_id)
	{
		//$user_id 	  				= $_SESSION['user']['id'];
		$userDetail 				= $this->model_user->getUsers($user_id);
		$data['name'] 				= $userDetail['name'];
		$data['ic_no'] 				= $userDetail['ic_no'];
		$data['staff_id'] 			= $userDetail['staff_id'];
		$data['department_name']	= $userDetail['department_name'];
		$data['position_name']		= $userDetail['title'];
		$data['position']			= $userDetail['position_name'];
		$data['otID']				= $otID;
		$data['rsOt'] 				= $this->model_general->fetchSingle('hr__ot ', array('*'), array('ID' => $otID));
		$data['basicSalary'] 		= $this->model_ot->getBasicSalary($user_id);
		$data['prev_balanceCarryForward'] = $this->model_ot->get_prev_balanceCarryForward($user_id);

		$data['OTSummaryByDept'] = $this->model_ot->getOTSummaryByDept($otID);
		
		$select = array('*');
		$where  = array('id' => $otID);
		$join   = array();
		$order  = array();
		$data['otMain'] = $this->model_general->fetchAll('hr__ot', $select, $where, $join, $order);

		$month = date("F", mktime(0, 0, 0, $data['otMain'][0]['month'], 10));
		$year  = $data['otMain'][0]['year'];

		$select = array('a.*, d.id as deptID, d.name as deptName, b.name as name, e.name as clientName, b.ic_no as ic, b.staff_id as staffID, f.position as position, g.totalHoursApproved, g.totalHoursApplied as totalHoursAppliedTotal, h.totalClaim, h.totalConvertToLeave, h.claim_status, h.paid_status, g.normal, g.rest, g.public, g.weekend, g.weekdays, g.NormalMeal, g.RestMeal, g.PublicMeal, g.WeekendMeal, g.WeekdaysMeal, i.name as status_by_name, j.name as svName, k.name as svDeptName');
		$where = array( 'a.otID' => $otID,'a.is_delete' => 0);
		$join = array(array('user b','b.id = a.peopleID','left'), 
			array('user__employment_detail c', 'c.user_id = b.id', 'left'),
			array('company__department d', 'd.id = c.department_id', 'left'), 
			array('view_ot g', 'g.otID = a.otID', 'left'), 
			array('customer e', 'e.id = a.clientID', 'left'), 
			array('hr__ot h', 'h.id = a.otID', 'left'),
			array('company__position f', 'f.id = c.position_id', 'left'),
			array('user i','i.id = a.status_by','left'),
			array('user j','j.id = a.svID','left'),
			array('company__department k','k.id = a.svDeptID','left'));
		$order = array();

		$data['otLists'] = $this->model_general->fetchAll('hr__ot__detail a', $select, $where, $join, $order);
		
		if($data['otLists'][0]['claim_status'] == 1){
			$claim_status = 'SUBMITTED';
		} else if($data['otLists'][0]['claim_status'] == 2){
			$claim_status = 'VERIFIED';
		} else{
			$claim_status = 'PENDING';
		}

		if($data['otLists'][0]['paid_status'] == 1){
			$paid_status = 'PAID';
		} else{
			$paid_status = 'UNPAID';
		}

		$header['title'] = 'OVERTIME CLAIM FOR ' . $month .' '.$year;
		$header['month'] = $month;
		$header['year']  = $year;
		$header['claim_status']  = $claim_status;
		$header['paid_status']   = $paid_status;

		$this->load->view('hr/report/header.php', $header);

		if($data['rsOt']['year'] == '2020' && $data['rsOt']['month'] >= 8){
			$this->load->view('hr/overtime/report/ot_claim_print_adjust.php', $data);
		}else if($data['rsOt']['year'] > '2020'){
			$this->load->view('hr/overtime/report/ot_claim_print_adjust_2021.php', $data);
		}else{
			$this->load->view('hr/overtime/report/ot_claim_print.php', $data);
		}

		$this->load->view('hr/overtime/report/footer.php');
	}

	function printOTClaimSummary($otID,$user_id)
	{
		//$user_id 	  				= $_SESSION['user']['id'];
		$userDetail 				= $this->model_user->getUsers($user_id);
		$data['name'] 				= $userDetail['name'];
		$data['ic_no'] 				= $userDetail['ic_no'];
		$data['staff_id'] 			= $userDetail['staff_id'];
		$data['department_name']	= $userDetail['department_name'];
		$data['position_name']		= $userDetail['title'];
		$data['position']			= $userDetail['position_name'];
		$data['otID']				= $otID;
		$data['rsOt'] 				= $this->model_general->fetchSingle('hr__ot ', array('*'), array('ID' => $otID));
		$data['basicSalary'] 		= $this->model_ot->getBasicSalary($user_id);
		$data['prev_balanceCarryForward'] = $this->model_ot->get_prev_balanceCarryForward($user_id);

		$data['OTSummaryByDept'] = $this->model_ot->getOTSummaryByDept($otID);

		$select = array('*');
		$where  = array('id' => $otID);
		$join   = array();
		$order  = array();
		$data['otMain'] = $this->model_general->fetchAll('hr__ot', $select, $where, $join, $order);

		$month = date("F", mktime(0, 0, 0, $data['otMain'][0]['month'], 10));
		$year  = $data['otMain'][0]['year'];

		$select = array('a.*, d.id as deptID, d.name as deptName, b.name as name, e.name as clientName, b.ic_no as ic, b.staff_id as staffID, f.position as position, g.totalHoursApproved, g.totalHoursApplied as totalHoursAppliedTotal, h.totalClaim, h.totalConvertToLeave, h.claim_status, h.paid_status, g.normal, g.rest, g.public, g.weekend, g.weekdays, g.NormalMeal, g.RestMeal, g.PublicMeal, g.WeekendMeal, g.WeekdaysMeal, i.name as status_by_name, j.name as svName, k.name as svDeptName');
		$where = array( 'a.otID' => $otID,'a.is_delete' => 0);
		$join = array(array('user b','b.id = a.peopleID','left'), 
			array('user__employment_detail c', 'c.user_id = b.id', 'left'),
			array('company__department d', 'd.id = c.department_id', 'left'), 
			array('view_ot g', 'g.otID = a.otID', 'left'), 
			array('customer e', 'e.id = a.clientID', 'left'), 
			array('hr__ot h', 'h.id = a.otID', 'left'),
			array('company__position f', 'f.id = c.position_id', 'left'),
			array('user i','i.id = a.status_by','left'),
			array('user j','j.id = a.svID','left'),
			array('company__department k','k.id = a.svDeptID','left'));
		$order = array();

		$data['otLists'] = $this->model_general->fetchAll('hr__ot__detail a', $select, $where, $join, $order);
		
		if($data['otLists'][0]['claim_status'] == 1){
			$claim_status = 'SUBMITTED';
		} else if($data['otLists'][0]['claim_status'] == 2){
			$claim_status = 'VERIFIED';
		} else{
			$claim_status = 'PENDING';
		}

		if($data['otLists'][0]['paid_status'] == 1){
			$paid_status = 'PAID';
		} else{
			$paid_status = 'UNPAID';
		}

		$header['title'] = 'OVERTIME CLAIM SUMMARY FOR ' . $month .' '.$year;
		$header['month'] = $month;
		$header['year']  = $year;
		$header['claim_status']  = $claim_status;
		$header['paid_status']   = $paid_status;

		$this->load->view('hr/report/header.php', $header);

		if($data['rsOt']['year'] == '2020' && $data['rsOt']['month'] >= 8){
			$this->load->view('hr/overtime/report/ot_claim_print2_adjust.php', $data);
		}else if($data['rsOt']['year'] > '2020'){
			$this->load->view('hr/overtime/report/ot_claim_print2_adjust_2021.php', $data);
		}else{
			$this->load->view('hr/overtime/report/ot_claim_print2.php', $data);
		}

		$this->load->view('hr/overtime/report/footer.php');
	}

	function crossJobReport($sv_deptID=null,$month_submission=null,$year_submission=null)
	{

		if(!empty($sv_deptID) && $sv_deptID != 'ALL'){
			$jobDeptID = $sv_deptID;
		} else {
			$jobDeptID = null;
		}

		if(!empty($year_submission) && $year_submission != 'ALL'){
			$year = $year_submission;
		} else if($year_submission == 'ALL'){
			$year = null;
		} else {
			$year = date('Y');
		}

		if(!empty($month_submission) && $month_submission != 'ALL'){
			$month = $month_submission;
		} else if($month_submission == 'ALL'){
			$month = null;
		} else {
			$month = date('m');
		}
		
		$data['filter_month']   = $month;
		$data['filter_year']    = $year;
		$data['filter_dept']    = $jobDeptID;
		$data['role']		 	= $this->logged_module['role'];
		$data['page_setup']  	= $this->template->pageSetup('HR', 'home', 'Overtime');
		$data['company'] 	 	= $this->model_company->getCompanies(1);
		$data['month']		 	= $this->model_crm->getMonth();
		$data['yearList']    	= $this->db->query('select distinct year from hr__ot')->result();
		$data['department']  	= $this->model_company->getDepartments();
		$data['cross_job_dept'] = $this->model_ot->getCrossJobDept($jobDeptID,$month,$year);

		$this->template->view('hr/overtime/report/crossjob_claim_report', $data);
	} 

	function printCrossJobReport($sv_deptID=null,$month_submission=null,$year_submission=null)
	{

		if(!empty($sv_deptID) && $sv_deptID != 'ALL'){
			$jobDeptID = $sv_deptID;
		} else {
			$jobDeptID = null;
		}

		if(!empty($year_submission) && $year_submission != 'ALL'){
			$year = $year_submission;
		} else if($year_submission == 'ALL'){
			$year = null;
		} else {
			$year = date('Y');
		}

		if(!empty($month_submission) && $month_submission != 'ALL'){
			$month = $month_submission;
		} else if($month_submission == 'ALL'){
			$month = null;
		} else {
			$month = date('m');
		}
		
		$data['filter_month']   = $month;
		$data['filter_year']    = $year;
		$data['filter_dept']    = $jobDeptID;
		$data['cross_job_dept'] = $this->model_ot->getCrossJobDept($jobDeptID,$month,$year);

		$month2 = date("F", mktime(0, 0, 0, $month_submission, 10));

		$header['title'] = 'OVERTIME CROSS JOB SUMMARY FOR ' . strtoupper($month2) .' '.$year;
		$header['month'] = strtoupper($month2);
		$header['year']  = $year;
		$header['dept']  = $jobDeptID;

		$this->load->view('hr/report/header.php', $header);
		$this->load->view('hr/overtime/report/ot_crossjobclaim_print.php', $data);
		$this->load->view('hr/overtime/report/footer.php');
	}

}
