<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_SALARY_DEDUCTION';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$path_to_root = "..";

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/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/payroll/includes/db/salary_deductions_db.inc");
include_once($path_to_root . "/includes/db/module_settings_db.inc");
//include_once($path_to_root . "/payroll/includes/db/salary_deductions_accounts_setup_db.inc");

$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 = "Salary Deductions"));

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

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

/*
function get_status_name($status_code)
{
    switch ($status_code)
    {
        case 'N':
            return "New";
            break;
        case 'U':
            return "Updated";
            break;
        case 'C':
            return "Calculating";
            break;
        case 'L':
            return "Calculated";
            break;
        case 'S':
            return "Calculation Stopped";
            break;
        case 'P':
            return "Processing";
            break;
        case 'R':
            return "Processed";
            break;
        case 'T':
            return "Processing Stopped";
            break;
        default:
            return "Unknown Status";
            break;

    }
    
}
 * 
 */

//function make_payment($id)
//{
//    $myrow = get_salary_deduction_details($id);
//    
//    $account_codes = get_salary_advance_account_detail($myrow['type_id']);
//    if(!$account_codes){
//        display_error( _("Could not retrieve payment account details for this slary advance type. Not possible to make the payment."));
//    }
//    
//    if(($account_codes['debit_code'] != "" && $account_codes['credit_code'] == ""))
//    {
//        display_error( _("Only the debit account is defined in for this salary advance type. Not possible to make the payment."));
//
//    }
//    elseif($account_codes['debit_code'] == "" && $account_codes['credit_code'] != ""){
//        display_error( _("Only the credit account is defined in for this salary advance type. Not possible to make the payment."));
//    }
//    else{
//        pay_salary_advance($id, $_POST['employee_code'], $myrow['salary_deduction'], 
//                        $account_codes['debit_code'], $account_codes['credit_code']);
//    }
//    
//}


function check_data($Mode, $selected_id)
{	
        if (!isset($_POST['employee_code']) || strlen($_POST['employee_code']) < 1)
   	{
            display_error( _("Please select an employee."));
            set_focus('employee_code');
            return false;
   	}
        
        if (!check_num('salary_deduction',0) || ! (input_num('salary_deduction') > 0))
   	{
            display_error( _("Amount entered is not numeric or negative."));
            set_focus('salary_deduction');
            return false;
   	}
        
        if (!is_date($_POST['due_date']))
   	{
            display_error( _("Due date entered is not a valid date."));
            set_focus('due_date');
            return false;
   	}
    
    $outside = is_outside_payroll_entries_allowed_date_range($_POST['due_date']);
    if($outside["success"] !== true)
   	{
            display_error( _("Due date: ".$outside['message']));
            set_focus('due_date');
            return false;
   	}
    
    if($Mode == "ADD_ITEM"){
        $rec = get_salary_deduction_existing_record($_POST['employee_code'], $_POST['type_id'], input_num('salary_deduction'), $_POST['due_date']);
        if ($rec)
        {
                display_error( _("There is an identical salary deduction entered for current employee."));
                set_focus('salary_addition');
                return false;
        }
    }
    
        return true;
    
}
//--------------------------------------------------------------------------------------------------


if (($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') && check_data($Mode, $selected_id))
{

    if ($Mode == 'ADD_ITEM') 
    {
            $rec_id = add_salary_deduction($_POST['employee_code'], $_POST['type_id'],
                                    input_num('salary_deduction'), 
                                    $_POST['due_date'], $_POST['comments'], 'N');

            if($rec_id > 0){
                display_notification(_("Salary deduction has been added."));
            }
            else{
                display_error(_("Salary deduction could not be added."));
            }
    } 
    if ($Mode == 'UPDATE_ITEM') 
    {
            $recs = update_salary_deduction($selected_id, $_POST['employee_code'], 
                                    $_POST['type_id'], input_num('salary_deduction'), 
                                    $_POST['due_date'], $_POST['comments'], 'U');

            $selected_id = -1;
            
            if($recs == 1){
                display_notification(_("Salary deduction has been updated."));
            }
            else{
                display_error(_("Salary deduction could not be updated."));
            }
    }
    $Mode = 'RESET';

}

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

//if(isset($_POST['pay_salary_advance']))
//{
//    make_payment($selected_id);
//    
//}

if ($Mode == 'Delete')
{
        $recs = delete_salary_deduction($selected_id, $_POST['employee_code']);
        
        if($recs == 1){
                display_notification(_("Salary deduction has been deleted."));
        }
        else{
                display_error(_("Salary deduction could not be deleted."));
        }

	$Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
}

if (isset($_POST['_selected_id_update']) )
{
	$selected_id = $_POST['selected_id'];
	$Ajax->activate('_page_body');
}

//if (list_updated('employee_code')) 
	$Ajax->activate('price_table');
//--------------------------------------------------------------------------------------------------

start_form();

    start_table();
        emoplyees_list_cells(_("Select an Employee:"), 'employee_code', null, false, true, check_value('show_inactive'));
    end_table(2);

    $result = get_salary_deductions($_POST['employee_code']);
    
    div_start('price_table');
    
    if (db_num_rows($result) == 0)
    {
    	display_note(_("There are no salary deductions created for the selected employee"));
    }
    else
    {
        start_table(TABLESTYLE, "width=60%");

		$th = array(_("Type Name"), _("Amount"), _("Due Date"), _("Comment"), _("Status"),"", "");

        table_header($th);

        $k = $j = 0; //row colour counter

        while ($myrow = db_fetch($result))
        {
            alt_table_row_color($k);

            //label_cell($myrow["id"]);
            label_cell($myrow["type_name"]);
            amount_cell($myrow["salary_deduction"]);
            label_cell(sql2date($myrow["due_date"]),"align=center");
            label_cell($myrow["comments"]);
            label_cell($myrow["status_name"]);
                
            if($myrow["status_code"] == "N" || $myrow["status_code"] == "U" || 
                    $myrow["status_code"] == "O"){
                edit_button_cell("Edit".$myrow['id'], _("Edit"));
                delete_button_cell("Delete".$myrow['id'], _("Delete"));
            }
//            elseif($myrow["status_code"] == "A"){
//                button_cell("Edit".$myrow['id'], _("Edit"), "Pay", ICON_DOC);
//                label_cell("");
//            }
            else{
                label_cell("");
                label_cell("");
            }
            
            end_row();

            $j++;
            If ($j == 12)
            {
            	$j = 1;
        		table_header($th);
            } //end of page full new headings
        } //end of while loop

        end_table();
    }
    div_end();

//-----------------------------------------------------------------------------------------------
    $_POST['id'] = 0;
    $_POST['salary_deduction'] = 0;
    $_POST['due_date'] = "";
    $_POST['comments'] = "";
    $_POST['type_id'] = "";
    
$dec2 = 6;
if ($Mode =='Edit')
{
    $myrow = get_salary_deduction_details($selected_id);

    $_POST['id'] = $myrow["id"];
    $_POST['salary_deduction'] = $myrow["salary_deduction"];
    $_POST['due_date'] = sql2date($myrow["due_date"]);
    $_POST['comments'] = $myrow["comments"];
    $_POST['type_id'] = $myrow["type_id"];
    $_POST['status_code'] = $myrow['status_code'];
    
    hidden('id', $_POST['id']); 
}

br();

hidden('selected_id', $selected_id);

start_table(TABLESTYLE2);

value_list_row("Type:", "type_id", "SALARY_DEDUCTIONS", $_POST['type_id']);
amount_row('Amount:', 'salary_deduction', $_POST['salary_deduction'], null, null, 2);
date_row("Due Date:", "due_date", $_POST['due_date']);
textarea_row('Comments:', 'comments', $_POST['comments'], 35, 2);

if($selected_id > 0){
    set_focus('salary_deduction');
}
end_table(1);

//if(isset($_POST['status_code']) && $_POST['status_code'] == "A"){
//    submit("pay_salary_advance", "Pay",true, _('Pay Salary Advance'), 'default');
//    submit('RESET', _("Cancel"), true, _('Cancel edition'), 'cancel');
//}
//else{
//    submit_add_or_update_center($selected_id == -1, '', 'both');
//}

submit_add_or_update_center($selected_id == -1, '', 'both');

end_form(2);
end_page(@$_GET['popup'], false, false);

?>