<?php
/****************************************
/*  Author 	: Kvvaradha
/*  Module 	: Extended HRM
/*  E-mail 	: admin@kvcodes.com
/*  Version : 1.0
/*  Http 	: www.kvcodes.com
*****************************************/
$page_security = 'HR_PAYROLL_INQ';
$path_to_root="../../..";
include_once($path_to_root . "/includes/db_pager.inc");
include($path_to_root . "/includes/session.inc");
add_access_extensions();
include($path_to_root . "/modules/ExtendedHRM/includes/Payroll.inc" );
$version_id = get_company_prefs('version_id');

$js = '';
if($version_id['version_id'] == '2.4.1'){
	if ($SysPrefs->use_popup_windows) 
		$js .= get_js_open_window(900, 500);	

	if (user_use_date_picker()) 
		$js .= get_js_date_picker();
	
}else{
	if ($use_popup_windows)
		$js .= get_js_open_window(900, 500);
	if ($use_date_picker)
		$js .= get_js_date_picker();
}
 
include($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/date_functions.inc");
page(_("Advance Salary Inquiry"));
 
 simple_page_mode(true);

check_db_has_employees(sprintf(_("There is no employee in this system. Kindly Open '%s' to update it"), "<a href='".$path_to_root."/modules/ExtendedHRM/manage/employees.php'>"._("Add And Manage Employees")."</a>"));
//----------------------------------------------------------------------------------------
	$new_item = get_post('selected_id')=='' || get_post('cancel') ;
	if (isset($_GET['selected_id'])){
		$_POST['selected_id'] = $_GET['selected_id'];
	}
	$selected_id = get_post('selected_id');
	 if (list_updated('selected_id')) {
		$_POST['dept_id'] = $selected_id = get_post('selected_id');
	    $Ajax->activate('details');
	}
	if(isset($_POST['Delete'])) {

		foreach($_POST as $empls =>$val) {	
			if($empls > 0){	
			Delete('kv_empl_salary_advance', array('id' => $empls));
			// Delete('gl_trans', array('type' => ST_EMPLOYEE_ADVANCE, 'type_no' => $empls));
			Update('gl_trans', array('type' => ST_EMPLOYEE_ADVANCE, 'type_no' => $empls), array('amount' => 0));
			display_notification(_("Selected Payroll Entries Deleted Successfully"));
			}
		}
	}
//----------------------------------------------------------------------------------------
	start_form(true);
		start_table(TABLESTYLE_NOBORDER);
			echo '<tr>';
			 	department_list_cells(_("Select a Department")." :", 'selected_id', null,	_("No Department"), true, check_value('show_inactive'));
				employee_list_cells(_("Select an Employee"). " :", 'empl_id', null,	_("Select an Employee"), true, check_value('show_inactive'), false, false,true);
				$new_item = get_post('selected_id')=='';
		 	echo '</tr>';
	 	end_table(1);
		if (get_post('_show_inactive_update') || get_post('empl_id') ){		
			$Ajax->activate('details');
			set_focus('empl_id');
		}
	
		div_start('details');		
			$joins = [['join' => 'INNER', 'table_name' => 'kv_empl_info AS info', 'conditions' => '`info`.`empl_id` = `salary`.`empl_id`'],
					['join' => 'LEFT', 'table_name' => 'fiscal_year AS fr', 'conditions' => '`fr`.`id` = `salary`.`year`']]; 
			$where =[];
			if(get_post('empl_id')!='') {
				if(get_post('selected_id') > 0){
					$joins [] =['join' => 'INNER', 'table_name' => 'kv_empl_job AS job', 'conditions' => '`job`.`empl_id` = `salary`.`empl_id`'];

					$where['`job`.`department`'] = get_post('selected_id');
				}else {
					$where['`info`.`empl_id`'] = get_post('empl_id');
				}
			} else {
				if(get_post('selected_id') > 0){
					$joins [] =['join' => 'INNER', 'table_name' => 'kv_empl_job AS job', 'conditions' => '`job`.`empl_id` = `salary`.`empl_id`'];
					$where['`job`.`department`'] = get_post('selected_id');				
				}
			}
			$columns = array('`salary`.`id`,  `info`.`empl_id`, `salary`.`payslip_id`, CONCAT(`info`.`empl_firstname`, `info`.`empl_lastname`) AS name, `salary`.`date`, `salary`.`amount`, `salary`.`inactive`, `salary`.`payslip_id_adv`, `fr`.`begin`, `fr`.`end`,`salary`.`payment`');
			$sql = GetDataJoin('kv_empl_salary_advance AS salary',  $joins, $columns, $where, array('salary.date' => 'desc'), false, false,true);

			$cols = array(
            checkbox('', 'check_all', null, true) => array('name' => 'id', 'fun' => 'check_box_row'),
            _("ID") => array('name'=>'empl_id'),
            _("#Payslip") => array('name'=>'payslip_id','fun' =>'payslip_link'),
            _("Name") => array('name'=>'name'),
            _("Date") => array('name'=>'date', 'type' => 'date'),
            // _("Month") => array('name'=>'month', 'fun' => 'month_name'),
            // _("Year") => array('name'=>'year', 'fun' => 'year_format'),
            _("Amount") => array('name'=>'amount'), 
            _("Status") => array('name'=>'inactive', 'fun' => 'status_fn'), 
            _("#Payslip_Adv") => array('name'=>'payslip_id_adv','fun' =>'payslip_adv_link'),//, 'type' => 'year_format'),
             //, 'type' => 'year_format'),
        	);
            			
            $cols[] = array('insert'=>true, 'fun'=>'gl_link');
            $cols[] = array('insert'=>true, 'fun'=>'view_link');
	        $table =& new_db_pager('info', $sql, $cols, 'kv_empl_salary_advance' );
	        $table->width = "98%";
	        display_db_pager($table);

			echo "<br>";
		submit_js_confirm('Delete', _('You are about to void this Advance salary.\nDo you want to continue?'));
		submit_center('Delete', _("Void"), true, 'cancel');
		div_end();
	end_form(); 
end_page();
 
 function status_fn($row){
 	return ($row['inactive'] == 0 ? 'Active' : 'Completed');
 } 

 function month_name($row){
 	if($row['month'] > 0)
 		return kv_month_name_by_id($row['month']);
 } 

 function year_format($row){
 	return sql2date($row['begin']).' - '.sql2date($row['end']);
 }
function gl_link($row){
	return get_gl_view_str(ST_BANKPAYMENT, $row['payment'],'', ICON_GL, '',ST_EMPLOYEE_ADVANCE);
}
 
function view_link($row){
	return hrm_viewer_link(ST_EMPLOYEE_ADVANCE, $row['id'], '', ICON_VIEW);
}

function payslip_link($row){
	if($row['payslip_id'] > 0)
	return hrm_viewer_link(ST_EMPLOYEE, $row['payslip_id'], '');
}

function payslip_adv_link($row){
	if($row['payslip_id_adv'] > 0)
	return hrm_viewer_link(ST_EMPLOYEE, $row['payslip_id_adv'], '');
}
 function check_box_row($row){
    if(list_updated('check_all')){
        if(isset($_POST['check_all'])){
            $_POST['_'.$row['id']] = 1;
        }       
   }
   	if(($row['inactive'] == 0) && (!$row['payslip_id_adv'] > 0))
    return checkbox('',$row['id'], null);
}
?>