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

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
include_once($path_to_root . "/admin/db/list_values_db.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/payroll_db.inc");

if (!@$_GET['popup']){
	page(_($help_context = "Salary Components"));
}


switch ($_GET['type']) {
    case SCC_ALL_COMPANY_ADDITIONS:
        if (!@$_GET['popup']){
            page(_($help_context = "Company Addition Salary Components"));
        }
        $heading_text = "Company Addition Salary Components";
        $salary_component_list_codes = "'COMPANY_ADDITION_SALARY_COMP'";
        $salary_components_types = "'".SCT_COMPANY_ADDITIONS."'";
        $is_deduction = 0;
        $_POST['employee_code'] = "";
        break;
    case SCC_ALL_COMPANY_DEDUCTIONS:
        if (!@$_GET['popup']){
            page(_($help_context = "Company Deduction Salary Components"));
        }
        $heading_text = "Company Deduction Salary Components";
        $salary_component_list_codes = "'COMPANY_DEDUCTION_SALARY_COMP'";
        $salary_components_types = "'".SCT_COMPANY_DEDUCTIONS."'";
        $is_deduction = 1;
        $_POST['employee_code'] = "";
        break;
    case SCC_ALL_GROUP_ADDITIONS:
        if (!@$_GET['popup']){
            page(_($help_context = "Group Addition Salary Components"));
        }
        $heading_text = "Group Addition Salary Components";
        $salary_component_list_codes = "'GROUP_ADDITION_SALARY_COMP'";
        $salary_components_types = "'".SCT_GROUP_ADDITIONS."'";
        $is_deduction = 0;
        $_POST['payroll_id'] = 0;
        break;
    case SCC_ALL_GROUP_DEDUCTIONS:
        if (!@$_GET['popup']){
            page(_($help_context = "Group Deduction Salary Components"));
        }
        $heading_text = "Group Deduction Salary Components";
        $salary_component_list_codes = "'GROUP_DEDUCTION_SALARY_COMP'";
        $salary_components_types = "'".SCT_GROUP_DEDUCTIONS."'";
        $is_deduction = 1;
        $_POST['payroll_id'] = 0;
        break;
    case SCC_ALL_EMPLOYEE_ADDITIONS:
        if (!@$_GET['popup']){
            page(_($help_context = "Employee Addition Salary Components"));
        }
        $heading_text = "Employee Addition Salary Components";
        $salary_component_list_codes = "'EMPLOYEE_ADDITION_SALARY_COMP', 'PERFORMANCE_RATE_INCENTIVES', 'ATTENDANCE_ALLOWANCES', 'PERFORMANCE_VALUE_INCENTIVES'";
        $salary_components_types = SCT_EMPLOYEE_ADDITIONS.",".SCT_PERFORMANCE_RATE_INCENTIVES.",".SCT_ATTENDANCE_ALLOWANCES.",".SCT_PERFORMANCE_VALUE_INCENTIVES;
        $is_deduction = 0;
        $_POST['payroll_id'] = 0;
        break;
    case SCC_ALL_EMPLOYEE_DEDUCTIONS:
        if (!@$_GET['popup']){
            page(_($help_context = "Employee Deduction Salary Components"));
        }
        $heading_text = "Employee Deduction Salary Components";
        $salary_component_list_codes = "'EMPLOYEE_DEDUCTION_SALARY_COMP', 'EMPLOYEE_ALLOWANCES_OVER_USAGE', 'MOBILE_ALLOWANCES_OVER_USAGE', 'WORKTIME_DEFICITS'";
        $salary_components_types = SCT_EMPLOYEE_DEDUCTIONS.",".SCT_EMPLOYEE_ALLOWANCES_OVER_USAGE.",".SCT_MOBILE_OVER_USAGE.",".SCT_WORKTIME_DEFICITS;
        $is_deduction = 1;
        $_POST['payroll_id'] = 0;
        break;
    default:
        $heading_text = "";
        $_POST['employee_code'] = "";
        $is_deduction = 0;
}

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

simple_page_mode(true);
//---------------------------------------------------------------------------------------------------
$input_error = 0;

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

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


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


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

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

set_global_employee_code($_POST['employee_code']);
//----------------------------------------------------------------------------------------------------

br();

if (!@$_GET['popup'])
	start_form();

function clear_data(){
    
    global $selected_id;
    
    unset($_POST['salary_component_id']);
    unset($_POST['salary_component_id']);
    unset($_POST['deduction']);
    unset($_POST['calculation_type']);
    unset($_POST['amount']);
    unset($_POST['salary_total_code']);
    unset($_POST['table_id']);
    unset($_POST['full_amount']);
    unset($_POST['effective_date']);
    unset($_POST['end_date']);
    unset($_POST['inactive']);
    $selected = -1;

}


function check_data($selected_id, $salary_component_id, $salary_component_type, $Mode)
{	
    
    if (strlen($_POST['employee_code']) <= 1)
    {
        display_error("Please select an employee.");
        set_focus('employee_code');
        return false;
    }
    
    
    if(get_post('calculation_type') == "P" || get_post('calculation_type') != "V"){
        if (!check_num('amount', 0)){
            display_error( _("Amount entered was not numeric or positive."));
            set_focus('amount');
            return false;
        }
    }

    if (!is_date($_POST['effective_date']))
    {
        display_error( _("Effective date entered was not a valid date."));
        set_focus('effective_date');
        return false;
    }

    if (!is_date($_POST['end_date']))
    {
        display_error( _("End date entered was not a valid date."));
        set_focus('end_date');
        return false;
    }
    //$salary_component = get_list_value($salary_component_id);
    $result = get_existing_salary_components($_POST['payroll_id'], $_POST['employee_code'], $salary_component_type, $salary_component_id);
    while ($my_row = db_fetch($result)){
        if($Mode == 'UPDATE_ITEM' && $selected_id == $my_row['id']){
            continue;
        }
        if(date1_greater_date2($_POST['effective_date'], sql2date($my_row['end_date']))){
            continue;
        }
        elseif(date1_greater_date2(sql2date($my_row['effective_date']), $_POST['end_date'])){
            continue;
        }
        else{
            display_error( _("There is a same type overlapping salary component with this one."));
            set_focus('effective_date');
            return false;
        }
        
    }
    
    
    return true;

}


function get_salary_component_type($salary_component_id){

    $salary_component = get_list_value($salary_component_id);
    if($salary_component){
        switch ($salary_component['list_code']) {
            case 'COMPANY_ADDITION_SALARY_COMP':
                return SCT_COMPANY_ADDITIONS;
            case 'COMPANY_DEDUCTION_SALARY_COMP':
                return SCT_COMPANY_DEDUCTIONS;
            case 'GROUP_ADDITION_SALARY_COMP':
                return SCT_GROUP_ADDITIONS;
            case 'GROUP_DEDUCTION_SALARY_COMP':
                return SCT_GROUP_DEDUCTIONS;
            case 'EMPLOYEE_ADDITION_SALARY_COMP':
                return SCT_EMPLOYEE_ADDITIONS;
            case 'EMPLOYEE_DEDUCTION_SALARY_COMP':
                return SCT_EMPLOYEE_DEDUCTIONS;
            case 'EMPLOYEE_ALLOWANCES_OVER_USAGE':
                return SCT_EMPLOYEE_ALLOWANCES_OVER_USAGE;
            case 'PERFORMANCE_RATE_INCENTIVES':
                return SCT_PERFORMANCE_RATE_INCENTIVES;
            case 'PERFORMANCE_VALUE_INCENTIVES':
                return SCT_PERFORMANCE_VALUE_INCENTIVES;
            case 'ATTENDANCE_ALLOWANCES':
                return SCT_ATTENDANCE_ALLOWANCES;
            case 'MOBILE_ALLOWANCES_OVER_USAGE':
                return SCT_MOBILE_OVER_USAGE;
            case 'WORKTIME_DEFICITS':
                return SCT_WORKTIME_DEFICITS;
            default:
                return 0;
        }
    }
    return 0;
}

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
{    
    if($_GET['type'] == SCC_ALL_COMPANY_ADDITIONS|| $_GET['type'] == SCC_ALL_COMPANY_DEDUCTIONS){
         $_POST['employee_code'] = "";
    }
    elseif($_GET['type'] == SCC_ALL_GROUP_ADDITIONS || $_GET['type'] == SCC_ALL_GROUP_DEDUCTIONS ||
            $_GET['type'] == SCC_ALL_EMPLOYEE_ADDITIONS || $_GET['type'] == SCC_ALL_EMPLOYEE_DEDUCTIONS){
        $_POST['payroll_id'] = 0;
    }
    else{
        $_POST['employee_code'] = "";
    }

    if(!isset($_POST['salary_total_code']))
        $salary_total_code = "";
    else {
        $salary_total_code = $_POST['salary_total_code'];
    }

    if($_POST['calculation_type'] == "V" ){
        $_POST['table_id'] = 0;
        $salary_total_code = "";
    }
    elseif($_POST['calculation_type'] == "P" ){
        $_POST['table_id'] = 0;
    }
    elseif($_POST['calculation_type'] == "T" ){
        //$salary_total_code = "";
        $_POST['amount'] = 0;
    }
    
    if(isset($_POST['nopay_deduct'])){
        $nopay_deduct = $_POST['nopay_deduct'];
    }
    else {
        $nopay_deduct = 0;
    }
    
    $salary_component_type = get_salary_component_type($_POST['salary_component_id']);
    
    if(check_data($selected_id, $_POST['salary_component_id'], $salary_component_type, $Mode)){ 
        


        if ($selected_id != -1) 
        {
                update_salary_component($selected_id,  $_POST['payroll_id'], $_POST['deduction'], 
                        $_POST['calculation_type'], input_num('amount'), 
                        $salary_total_code, $_POST['table_id'], 
                        $_POST['effective_date'], $_POST['end_date'], $_POST['full_amount'],
                        $nopay_deduct, $_POST['comments'], 
                        $_POST['inactive'],
                        $salary_component_type, $_POST['employee_code'],
                        $_POST['salary_component_id']);

                $msg = _("Salary component has been updated.");
        }
        else
        {

                
                add_salary_component($_POST['payroll_id'], $salary_component_type, $_POST['employee_code'],
                        $_POST['salary_component_id'], $_POST['deduction'], 
                        $_POST['calculation_type'], input_num('amount'), 
                        $salary_total_code, $_POST['table_id'], 
                        $_POST['effective_date'], $_POST['end_date'], $_POST['full_amount'],
                        $nopay_deduct, $_POST['comments'], 
                        $_POST['inactive']);

                $msg = _("New salary component has been added.");
        }
        display_notification($msg);
        $Mode = 'RESET';
        clear_data();
    }

}

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

if ($Mode == 'Delete')
{
    delete_salary_component($selected_id);
	display_notification(_("The selected salary component has been deleted."));
	$Mode = 'RESET';
}

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

if (list_updated('employee_code')) {
	$Ajax->activate('price_table');
	$Ajax->activate('price_details');
}

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



//switch ($_GET['type']) {
//    case "E":
//        $heading_text = "Employee Salary Components";
//        $salary_componet_list_code = "EMPLOYEE_SALARY_COMP";
//        break;
//    case "C":
//        $heading_text = "Common Salary Components";
//        $salary_componet_list_code = "COMMON_SALARY_COMP";
//        break;
//    case "S":
//        $heading_text = "Statutory Deductions";
//        $salary_componet_list_code = "STATUTORY_DEDUCTIONS";
//        break;
//    default:
//        $heading_text = "";
//}
    

//if($_GET['type'] == SCC_ALL_COMPANY_ADDITIONS ||  SCC_ALL_GROUP_ADDITIONS || $_GET['type'] == SCT_EMPLOYEE_ADDITIONS){
//if($_GET['type'] == SCC_ALL_COMPANY_ADDITIONS){
//        $is_deduction = 0;
//        $_POST['employee_code'] = "";
//}
////elseif($_GET['type'] == SCC_ALL_COMPANY_DEDUCTIONS || $_GET['type'] == SCC_ALL_GROUP_DEDUCTIONS || $_GET['type'] == SCT_EMPLOYEE_DEDUCTIONS){
//elseif($_GET['type'] == SCC_ALL_COMPANY_DEDUCTIONS){
//        $_POST['payroll_id'] = 0;
//        $is_deduction = 1;
//}  
//else{
//        $_POST['payroll_id'] = 0;
//        $is_deduction = 1;
//}  

display_heading($heading_text);

$result = get_salary_components($_POST['payroll_id'], $_POST['employee_code'], $salary_components_types);

div_start('price_table');

br();

start_table(TABLESTYLE, "width=90%");

$th = array(
    _("Salary Component"), _("Calculation Type"),
    _("Amount/Percentage"), _("Percentage / Reference Value"), _("Table Name"), 
    _("Effective Date") , _("End Date"),_("Full Amount"), _("Deduct For Nopay"), 
    _("Comments"), _("On Hold"), "", "");

table_header($th);
$k = 0; //row colour counter
$calculated = false;


while ($myrow = db_fetch($result))
{

	alt_table_row_color($k);

    	label_cell($myrow["salary_component_name"]);
        
//        if($myrow["deduction"] == 1)
//            $deduction = "Yes";
//        else {
//            $deduction = "No";
//        }
//        
//        label_cell($deduction);
        
        label_cell($myrow["calculation_type_name"]);
        
        if($myrow["calculation_type"] == "T")
            $amount = "";
        else {
            $amount = abs($myrow["amount"]);
        }
        
        $dec = 2;
        if($myrow["calculation_type"] == "P")
            $dec = 5;
        
        qty_cell($amount, false, $dec);
        
        
        label_cell($myrow["salary_total_name"], "align=center");
        label_cell($myrow["table_name"]);
        label_cell(sql2date($myrow["effective_date"]),"align=center");
        
        if($myrow["end_date"] == INFINITE_END_DATE){
            label_cell("");
        }
        else{
            label_cell(sql2date($myrow["end_date"]),"align=center");
        }
        
        
        $full_amount = "";
        $nopay_deduct = "";
        if($myrow["calculation_type"] == "V"){
            if($myrow["full_amount"] == 1){
                $full_amount = "Yes";
            }
            else {
                $full_amount = "No";
            }
        
            if($myrow["nopay_deduct"] == 1){
                $nopay_deduct = "Yes";
            }
            else {
                $nopay_deduct = "No";
            }
        }
        
        label_cell($full_amount,"align=center");
        label_cell($nopay_deduct,"align=center");
        
        label_cell($myrow["comments"]);
        
        if($myrow["inactive"] == 1)
            $inactive = "Yes";
        else {
            $inactive = "No";
        }
        label_cell($inactive,"align=center");

 	edit_button_cell("Edit".$myrow['id'], _("Edit"));
 	delete_button_cell("Delete".$myrow['id'], _("Delete"));
        
        end_row();

}

end_table();

div_end();
//------------------------------------------------------------------------------------------------

echo "<br>";

if(!isset($_POST['effective_date'] ))  
    $_POST['effective_date'] = "";

if(!isset($_POST['end_date'] ))  
    $_POST['end_date'] = "";

if(!isset($_POST['amount'] ))  
    $_POST['amount'] = 0;

if(!isset($_POST['comments'] ))  
    $_POST['comments'] = "";

if(!isset($_POST['inactive'] ))  
    $_POST['inactive'] = "";


if ($Mode == 'Edit')
{

        $sa_row = get_salary_component_detail($selected_id);

        $_POST['salary_component_id'] = $sa_row["salary_component_id"];
        $_POST['deduction'] = $sa_row["deduction"];
        $_POST['calculation_type'] = $sa_row["calculation_type"];
        $_POST['amount'] = abs($sa_row["amount"]);
        $_POST['salary_total_code'] = $sa_row["salary_total_code"];
        $_POST['table_id'] = $sa_row["table_id"];
        $_POST['effective_date'] = sql2date($sa_row["effective_date"]);
        $_POST['end_date'] = sql2date($sa_row["end_date"]);
        $_POST['full_amount'] = $sa_row["full_amount"];
        $_POST['nopay_deduct'] = $sa_row["nopay_deduct"];
        $_POST['comments'] = $sa_row["comments"];
        $_POST['inactive'] = $sa_row["inactive"];

}
else{
    if(!isset($_POST['salary_component_id'])){
        $_POST['salary_component_id']= 0;
    }
}


hidden('selected_id', $selected_id);
if (@$_GET['popup'])
{
	hidden('_tabs_sel', get_post('_tabs_sel'));
	hidden('popup', @$_GET['popup']);
}
div_start('price_details');
start_table(TABLESTYLE2);

//value_list_row(_("Relationship:"), 'family_member_relationship_id', 'F_MEMBERS');
$_POST['amount'] = abs($_POST['amount']);

//value_list_row("Salary Component:", "salary_component_id", $salary_componet_list_code);
salary_components_list_row("Salary Component", "salary_component_id", $_POST['salary_component_id'], $salary_component_list_codes);

//if($_GET['type'] == "S" || $_GET['type'] == "W"){
//        $_POST['deduction'] = 1;
//        label_row("Deduction:", "Yes");
//        hidden("deduction");
//}
//else{
//    yesno_list_row("Deduction:", "deduction",null,"Yes","No");
//}

hidden('deduction', $is_deduction);

value_list_code_row("Calculation Type:", "calculation_type", "SALARY_CAL_TYPES", null, false, true);

switch (get_post('calculation_type')) {
    default:
    case 'V':
        amount_row("Amount", "amount",$_POST['amount'] , null, null, 2);
            break;
    case 'P':
        amount_row("Percentage", "amount",$_POST['amount'] , null, null, 5);
        value_list_code_row("Percentage Of:", "salary_total_code", "SALARY_TOTALS");
            break;
    case 'T':
        value_list_code_row("Percentage Of:", "salary_total_code", "SALARY_TOTALS");
        value_list_row("Table Id", "table_id", "VALUE_TABLES");
            break;
    case 'R':
        hidden('amount', 0);
        hidden('table_id', 0);
            break;
};
date_row(_('Effective Date:'), 'effective_date');

if($selected_id == -1){
    $_POST['end_date'] = sql2date(INFINITE_END_DATE);
}

date_row(_('End Date:'), 'end_date');

if(get_post('calculation_type') == 'V'){
    yesno_list_row("Full Amount", "full_amount");
    yesno_list_row("Deduct for Nopay", "nopay_deduct", $_POST['nopay_deduct']);
}
else{
    hidden('full_amount', 0);
    hidden('nopay_deduct', 0);
}

text_row("Comments", "comments", $_POST['comments'], 50, 200);
yesno_list_row("On Hold", "inactive", $_POST['inactive']);

end_table(1);

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

if (!@$_GET['popup'])
{
	end_form();
	end_page(@$_GET['popup'], false, false);
}	
?>
