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


$page_security = 'SA_PAYROLL_SETTINGS';
$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 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 . "/payroll/includes/db/payroll_settings_db.inc");
include_once($path_to_root . "/includes/ui/ui_lists_custom.inc");


$user_comp = user_company();


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

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

$payroll_id = get_post('payroll_id');

if (list_updated('payroll_id')) {
	$_POST['NewEmployeeCode'] = $payroll_id = get_post('payroll_id');
    //clear_data();
	$Ajax->activate('details');
	$Ajax->activate('controls');
}

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

if (isset($_POST['add_setup']) || isset($_POST['update_set_up']))
{
    if(!check_num('rounding_off_to_amount', 0.00)){        
        display_error("Rounding Off To Amount must be positive and numeric");
        set_focus('rounding_off_to_amount');
    }

    else{
        if(isset($_POST['add_setup'])) 
        {
                insert_payroll_settings($_POST['payroll_id'], $_POST['rounding_off_reference_id'], input_num('rounding_off_to_amount'), 
                                        $_POST['brought_forward_value_id'], $_POST['rounded_off_salary_id'], $_POST['carried_forward_value_id']);


                display_notification(_("Payroll settings have been added."));
        } 
        if(isset($_POST['update_set_up']))
        {
                update_payroll_settings($_POST['id'], $_POST['payroll_id'], $_POST['rounding_off_reference_id'], input_num('rounding_off_to_amount'), 
                                        $_POST['brought_forward_value_id'], $_POST['rounded_off_salary_id'], $_POST['carried_forward_value_id']);

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

}

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

function general_settings($payroll_id)
{
    $myrow = get_payroll_settings($payroll_id);
    
    if($myrow == null){
        $_POST['id'] = 0;
        $_POST['rounding_off_reference_id'] = 0;
        $_POST['rounding_off_to_amount'] = 0;
        $_POST['brought_forward_value_id'] = 0;
        $_POST['rounded_off_salary_id'] = 0;
        $_POST['carried_forward_value_id'] = 0;
    }
    else{
        $_POST['id'] = $myrow['id'];
        $_POST['rounding_off_reference_id'] = $myrow['rounding_off_reference_id'];
        $_POST['rounding_off_to_amount'] = $myrow['rounding_off_to_amount'];
        $_POST['brought_forward_value_id'] = $myrow['brought_forward_value_id'];
        $_POST['rounded_off_salary_id'] = $myrow['rounded_off_salary_id'];
        $_POST['carried_forward_value_id'] = $myrow['carried_forward_value_id'];
    }
    
    hidden('id');
    
    br(2);
    
    start_outer_table(TABLESTYLE2);

    table_section(1);

    table_section_title(_("Item"));
    value_list_row("Rounfing Off Reference Value:", "rounding_off_reference_id", "SALARY_TOTALS", $_POST['rounding_off_reference_id']);
    
    amount_row("Rounding Off To Amount:", "rounding_off_to_amount", $_POST['rounding_off_to_amount']);
    
    value_list_row("Brought Forward Value:", "brought_forward_value_id", "SALARY_ROUNDING_OFF", $_POST['brought_forward_value_id']);
    
    value_list_row("Rounded Off Salary:", "rounded_off_salary_id", "SALARY_TOTALS", $_POST['rounded_off_salary_id']);
    
    value_list_row("Carried Forward Value:", "carried_forward_value_id", "SALARY_ROUNDING_OFF", $_POST['carried_forward_value_id']);

    end_outer_table(1);

    br(1);

    if($_POST['id'] == 0){    
        submit_center('add_setup', _("Insert settings"), true, '', 'default');
    }
    else {
        submit_center('update_set_up', _("Update settings"), true, '', 'default');
    }
    
}

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

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

start_form(true);

if (!isset($_POST['payroll_id']))
        $_POST['payroll_id'] = get_global_payroll_id ();


start_table();
    //echo stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true);
    value_list_row('Select Payroll:', 'payroll_id', 'PAYROLLS', $_POST['payroll_id'],false,true);
    submit_row('Refresh', 'Refresh');
end_table();

//$result = get_all_table_values($_POST['table_id']);

set_global_payroll_id($_POST['payroll_id']);



div_start('details');

$payroll_id = get_post('payroll_id');

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

tabbed_content_start('tabs', array(
		'settings' => array(_('&General settings'), $payroll_id),
        'no_pay_reference' => array(_('&Nopay Reference'), $payroll_id),
                'company_addtions' => array(_('&Addition Salary Components'), $payroll_id),
                'company_deductions' => array(_('&Deduction Salary Components'), $payroll_id),
                'calculation_seq' => array(_('&Calculation Seq.'), $payroll_id),
		'accounts_setup' => array(_('&Accounts Setup'), $payroll_id),
	));
	
	switch (get_post('_tabs_sel')) {
            default:
            case 'settings':    
                general_settings($payroll_id); 
                break;
            
            case 'no_pay_reference':
                $_GET['payroll_id'] = $payroll_id;
                $_GET['popup'] = 1;
                $_GET['employee_code'] = "";
                $_GET['type'] = SCC_ALL_COMPANY_ADDITIONS;
                include_once($path_to_root."/payroll/nopay_references.php"); 
                break;
            
            case 'company_addtions':
                $_GET['payroll_id'] = $payroll_id;
                $_GET['popup'] = 1;
                $_GET['employee_code'] = "";
                $_GET['type'] = SCC_ALL_COMPANY_ADDITIONS;
                include_once($path_to_root."/payroll/salary_components.php");
                break;
            
            case 'company_deductions':
                $_GET['payroll_id'] = $payroll_id;
                $_GET['popup'] = 1;
                $_GET['employee_code'] = "";
                $_GET['type'] = SCC_ALL_COMPANY_DEDUCTIONS;
                include_once($path_to_root."/payroll/salary_components.php");
                break;
            
            case 'calculation_seq':
                $_GET['payroll_id'] = $payroll_id;
                $_GET['popup'] = 1;
                include_once($path_to_root."/payroll/payroll_calculation_seq.php");
                break;
//        case 'accounts_setup':
//			$_GET['payroll_id'] = $payroll_id;
//			$_GET['popup'] = 1;
//			include_once($path_to_root."/payroll/payroll_accounts_setup.php");
//			break;
	}
br();
tabbed_content_end();

div_end();


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

end_form(1);

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

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