<?php
/**********************************************************************
***********************************************************************/


$page_security = 'SA_PAYROLL_MODULE_SETUP';
$path_to_root = "../..";
include($path_to_root . "/includes/session.inc");
include_once ($path_to_root . "/trace.php");
$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(900, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();
	
page(_($help_context = "Payroll Module Setup"), @$_REQUEST['popup'], false, "", $js);

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 . "/includes/db/module_settings_db.inc");
include_once($path_to_root . "/includes/ui/ui_lists_custom.inc");


$user_comp = user_company();
$module = "payroll";

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

if (isset($_GET['payroll_id']))
{
	$_POST['payroll_id'] = $_GET['payroll_id'];
}


$Ajax->activate('details');
$Ajax->activate('controls');


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

function can_process_module_parameters()
{
        if(!is_time($_POST['from_time1']))
        {
            display_error("In1 Time is not a valid time.");
            set_focus('from_time1');       
            return false;
        }
        if(!is_time($_POST['to_time1']))
        {
            display_error("Out1 Time is not a valid time.");
            set_focus('to_time1');       
            return false;
        }
        
        if (!check_num('ot_rounddown_value',1,60)) {
            display_error(_("OT round-down minutes has to be numeric and should be between 1 and 60."));
            set_focus('ot_rounddown_value');
            return false;
	}
        
        if (!check_num('deficit_roundup_value',1)) {
            display_error(_("Deficit round-up minutes has to be numeric and should be 1 or greater."));
            set_focus('deficit_roundup_value');
            return false;
	}
    
    if (!is_date($_POST['entries_allowed_from_date'])) {
            display_error(_("Entries allowed from date is not a valid date."));
            set_focus('entries_allowed_from_date');
            return false;
	}
    
    if (!is_date($_POST['entries_allowed_to_date'])) {
            display_error(_("Entries allowed to date is not a valid date."));
            set_focus('entries_allowed_to_date');
            return false;
	}
    
        
        return true;
           
}

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

  
if(isset($_POST['update_set_up']) && can_process_module_parameters() == true)
{
        global $module;

        update_module_parameter($module, "ot_rounddown_value",$_POST['ot_rounddown_value']);
        update_module_parameter($module, "deficit_roundup_value",$_POST['deficit_roundup_value']);
        update_module_parameter($module, "entries_allowed_from_date", date2sql($_POST['entries_allowed_from_date']));
        update_module_parameter($module, "entries_allowed_to_date", date2sql($_POST['entries_allowed_to_date']));

        update_module_parameter($module, "task_id",$_POST['task_id']);
        update_module_parameter($module, "shift_id",$_POST['shift_id']);
        update_module_parameter($module, "continuous_work",$_POST['continuous_work']);
        update_module_parameter($module, "saturday_work_time_id",$_POST['saturday_work_time_id']);
        update_module_parameter($module, "sunday_work_time_id",$_POST['sunday_work_time_id']);
        update_module_parameter($module, "from_time1",$_POST['from_time1']);
        update_module_parameter($module, "to_time1",$_POST['to_time1']);
        update_module_parameter($module, "use_defailt_times",$_POST['use_defailt_times']);
        
        //update_module_parameter($module, "mobile_bill_deduction_type_id",$_POST['mobile_bill_deduction_type_id']);
        
        update_module_parameter($module, "epf_reg_no",$_POST['epf_reg_no']);
        update_module_parameter($module, "epf_payment_bank",$_POST['epf_payment_bank']);
        update_module_parameter($module, "epf_reference_salary_total",$_POST['epf_reference_salary_total']);
        update_module_parameter($module, "banking_salary_total",$_POST['banking_salary_total']);

        $_POST['id'] = 0;
        display_notification(_("Payroll settings have been updated."));
}



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

function general_settings($payroll_id)
{
    global $module;
    

    $_POST['ot_rounddown_value'] = get_module_parameter_value($module, "ot_rounddown_value");
    $_POST['deficit_roundup_value'] = get_module_parameter_value($module, "deficit_roundup_value");
    $_POST['entries_allowed_from_date'] = sql2date(get_module_parameter_value($module, "entries_allowed_from_date"));
    $_POST['entries_allowed_to_date'] = sql2date(get_module_parameter_value($module, "entries_allowed_to_date"));
    
    $_POST['task_id'] = get_module_parameter_value($module, "task_id");
    $_POST['shift_id'] = get_module_parameter_value($module, "shift_id");
    $_POST['continuous_work'] = get_module_parameter_value($module, "continuous_work");
    $_POST['saturday_work_time_id'] = get_module_parameter_value($module, "saturday_work_time_id");
    $_POST['sunday_work_time_id'] = get_module_parameter_value($module, "sunday_work_time_id");
    $_POST['from_time1'] = get_module_parameter_value($module, "from_time1");
    $_POST['to_time1'] = get_module_parameter_value($module, "to_time1");
    $_POST['use_defailt_times'] = get_module_parameter_value($module, "use_defailt_times");
    
    $_POST['mobile_bill_deduction_type_id'] = get_module_parameter_value($module, "mobile_bill_deduction_type_id");
    
    $_POST['epf_reg_no'] = get_module_parameter_value($module, "epf_reg_no");
    $_POST['epf_payment_bank'] = get_module_parameter_value($module, "epf_payment_bank");
    $_POST['epf_reference_salary_total'] = get_module_parameter_value($module, "epf_reference_salary_total");
    $_POST['banking_salary_total'] = get_module_parameter_value($module, "banking_salary_total");
    
    
    
    br(2);
    
    start_outer_table(TABLESTYLE2);

    table_section(1);

    table_section_title(_("General"));
    small_amount_row("OT Round-down Minutes:", "ot_rounddown_value", $_POST['ot_rounddown_value'], null, null,0);
    small_amount_row("Deficit Round-up Minutes:", "deficit_roundup_value", $_POST['deficit_roundup_value'], null, null,0);
    date_row("Entries Allowed From:", 'entries_allowed_from_date');
    date_row("Entries Allowed To:", 'entries_allowed_to_date');
    
    table_section_title(_("Attendance"));
    tasks_all_list_row("Default task", "task_id", $_POST['task_id']);
    shift_list_row("Default Shift:", "shift_id", $_POST['shift_id']);
    yesno_list_row("Continuous Work", "continuous_work", $_POST['continuous_work'], "Yes", "No");
    weekend_work_time_list_row("6th day (Saturday) Work Time:", "saturday_work_time_id", $_POST['saturday_work_time_id']);
    weekend_work_time_list_row("7th day (Sunday) Work Time:", "sunday_work_time_id", $_POST['sunday_work_time_id']);
    time_row("Default In Time: ", "from_time1", $_POST["from_time1"], 5);
    time_row("Default Out Time: ", "to_time1", $_POST["to_time1"], 5);
    yesno_list_row("Use Defailt Times", "use_defailt_times", $_POST['use_defailt_times'], "Yes", "No");
    
//    table_section_title(_("Mobile Bill Allowance"));
//    value_list_row("Mobile Bill Allowance Over Used Deduction Type:", 'mobile_bill_deduction_type_id', "SALARY_DEDUCTIONS");
    
    table_section_title(_("Printing"));
    text_row("EPF Reg. No:", "epf_reg_no", $_POST['epf_reg_no'],20,20);
    text_row("EPF Payment Bank:", "epf_payment_bank", $_POST['epf_payment_bank'],20,20);
    value_list_code_row("EPF Reference Salary Total:", "epf_reference_salary_total", "SALARY_TOTALS", $_POST['epf_reference_salary_total']);
    value_list_code_row("Banking Salary Total:", "banking_salary_total", "SALARY_TOTALS", $_POST['banking_salary_total']);

    end_outer_table(1);

    br(1);


        submit_center('update_set_up', _("Update settings"), true, '', 'default');
    
    
}

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

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

start_form(true);

br();

div_start('details');

$payroll_id = 9999;

if (!$payroll_id)
	unset($_POST['_tabs_sel']); // force settings tab for new customer

tabbed_content_start('tabs', array(
		'settings' => array(_('&General settings'), $payroll_id),
		'salary_advances_accounts_setup' => array(_('&Salary Advances Accounts'), $payroll_id),
	));
	
	switch (get_post('_tabs_sel')) {
		default:
                case 'settings':
                        general_settings($payroll_id); 
			break;
                case 'salary_advances_accounts_setup':
			$_GET['payroll_id'] = $payroll_id;
			$_GET['popup'] = 1;
			include_once($path_to_root."/payroll/salary_advances_accounts_setup.php");
			break;
	};
br();
tabbed_content_end();

div_end();


hidden('popup', @$_REQUEST['popup']);

end_form();

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

end_page(@$_REQUEST['popup']);
?>
