<?php
/**********************************************************************
***********************************************************************/
$path_to_root="..";
$page_security = 'SA_IMPORT_ATTENDANCE_DATA';

include_once($path_to_root . "/includes/db_pager.inc");
include_once($path_to_root . "/includes/session.inc");

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/payroll/includes/db/import_attendance_data_db.inc");
include_once($path_to_root . "/sales/includes/sales_upload_common.inc");
include_once($path_to_root . "/payroll/includes/attendance_calculation.inc");
include_once($path_to_root . "/payroll/includes/db/attendance_db.inc");
include_once($path_to_root . "/payroll/includes/db/shifts_db.inc");
include_once($path_to_root . "/includes/db/module_settings_db.inc");
include_once($path_to_root . "/payroll/includes/process_import_attendance_data.inc");
include_once($path_to_root . "/payroll/includes/db/leaves_db.inc");
include_once($path_to_root . "/payroll/includes/db/employees_db.inc");
include_once($path_to_root . "/payroll/includes/db/shifts_db.inc");
include_once($path_to_root . "/payroll/includes/read_import_attendance_file.inc");
include_once($path_to_root . "/payroll/includes/db/calendars_db.inc");


$processed_values;
$previous_time_entry;
$data;
$day_start_time = "";
$day_end_time = "";
$closed;
$a;
$previous_employee_code;
$k;


if (isset($_GET['vw']))
	$view_id = $_GET['vw'];
else
	$view_id = find_submit('view');

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
page(_($help_context = "Import Attendance Data"), false, false, "", $js);

simple_page_mode(true);
//----------------------------------------------------------------------------------------

$ot_rounddown_value = get_module_parameter_value("payroll", "ot_rounddown_value");
$deficit_roundup_value = get_module_parameter_value("payroll", "deficit_roundup_value");
$default_from_time1 = get_module_parameter_value("payroll", "from_time1");
$default_to_time1 = get_module_parameter_value("payroll", "to_time1");
$use_defailt_times = get_module_parameter_value("payroll", "use_defailt_times");

$payroll_module_parameters['ot_rounddown_value'] = $ot_rounddown_value;
$payroll_module_parameters['deficit_roundup_value'] = $deficit_roundup_value;
$payroll_module_parameters['default_from_time1'] = $default_from_time1;
$payroll_module_parameters['default_to_time1'] = $default_to_time1;
$payroll_module_parameters['use_defailt_times'] = $use_defailt_times;

if (isset($_GET['trans_no']))
	$_POST['trans_no'] = $_GET['trans_no'];

if ($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM')
{
	if ($Mode == 'ADD_ITEM' && (!isset($_FILES['filename']) || $_FILES['filename']['size'] == 0)){
		display_error(_("Select attachment file."));
    }
	else {
		//$content = base64_encode(file_get_contents($_FILES['filename']['tmp_name']));
		$tmpname = $_FILES['filename']['tmp_name'];

		$dir =  company_path()."/attendance_files";
		if (!file_exists($dir))
		{
			mkdir ($dir,0777);
			$index_file = "<?php\nheader(\"Location: ../index.php\");\n?>";
			$fp = fopen($dir."/index.php", "w");
			fwrite($fp, $index_file);
			fclose($fp);
		}

		$filename = basename($_FILES['filename']['name']);
		$filesize = $_FILES['filename']['size'];
		$filetype = $_FILES['filename']['type'];

		// file name compatible with POSIX
		// protect against directory traversal
		if ($Mode == 'UPDATE_ITEM')
		{
		    $row = get_attachment($selected_id);
		    if ($row['filename'] == "")
        		exit();
			$unique_name = $row['unique_name'];
			if ($filename && file_exists($dir."/".$unique_name))
				unlink($dir."/".$unique_name);
		}
		else
			$unique_name = uniqid('');

		//save the file
		move_uploaded_file($tmpname, $dir."/".$unique_name);

		if ($Mode == 'ADD_ITEM')
		{
			add_attendance_file($_POST['description'],
				$filename, $unique_name, $filesize, $filetype);
			display_notification(_("Attendance file record has been inserted.")); 
		}
		else
		{
			update_attendance_file($selected_id, $_POST['description'],
				$filename, $unique_name, $filesize, $filetype); 
			display_notification(_("attendance record has been updated.")); 
		}
	}
	refresh_pager('trans_tbl');
	$Ajax->activate('_page_body');
	$Mode = 'RESET';
}

if ($Mode == 'Delete')
{
	$row = get_attendance_file($selected_id);
	$dir =  company_path()."/attendance_files";
	if (file_exists($dir."/".$row['unique_name'])){
		unlink($dir."/".$row['unique_name']);
    }
    delete_saved_records($selected_id);
	delete_attendance_file($selected_id);	
    $selected_id =-1;
    $Mode = 'RESET';
    
	display_notification(_("Attendance file has been deleted.")); 
	
}

if ($Mode == 'RESET')
{
	unset($_POST['trans_no']);
	unset($_POST['description']);
	$selected_id = -1;
}

function viewing_controls()
{
	global $selected_id;
	
    start_table(TABLESTYLE_NOBORDER);

	start_row();
	systypes_list_cells(_("Type:"), 'filterType', null, true);
	if (list_updated('filterType'))
		$selected_id = -1;;

	end_row();
    end_table(1);

}

function edit_link($row)
{
  	return button('Edit'.$row["id"], _("Edit"), _("Edit"), ICON_VIEW);
}


function delete_link($row)
{
  	return button('Delete'.$row["id"], _("Delete"), _("Delete"), ICON_DELETE);
}





if(isset($_POST['create_records'])){

    $car_msg = create_attendance_records($selected_id, $payroll_module_parameters);
    if($car_msg['success'] == true){
        //display_time_data();
    }
    else{
        display_error($car_msg['message']);
    }
}

if(isset($_POST['save_file_data'])){

    save_file_data($selected_id);
    
}


if(isset($_POST['delete_saved_file_data'])){

    delete_saved_records($selected_id);
    $selected_id = -1;
    
}

//----------------------------------------------------------------------------------------

start_form(true);


br(2);

start_table(TABLESTYLE2);



text_row_ex(_("Description").':', 'description', 40);
file_row(_("Attached File") . ":", 'filename', 'filename');


end_table(1);
hidden('selected_id', $selected_id);
submit_add_or_update_center(true, '', 'process');

br(2);

display_rows();

br();

if ($selected_id != -1)
{
    br();
    display_heading2("File Contents");
    br();
    display_file($selected_id, $payroll_module_parameters);
    br(2);
    
    submit_center_first("save_file_data", "Save File Data");
    submit("create_records", "Create Attendance Records");
    submit_center_last("delete_saved_file_data", "Delete Saved File Data");
    br(2);
}

if(isset($_SESSION['attendance_file_result'])){
    display_result($_SESSION['attendance_file_result']);
    unset($_SESSION['attendance_file_result']);
}

end_form();

end_page();

?>
