<?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 . "/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"));

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

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();


br();


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

function check_data($id)
{
        if (!check_num("seq_no".$id, 0))
        {
            display_error( _("Calculation sequence number entered was not numeric or positive."));
            set_focus('seq_no');
            return false;
        }
        
        return true;

}

function save_payroll_calculation_seqs($payroll_id) {
    $saved_rec_count = 0;
    
    if (! ($payroll_id > 0)) {
        return array('success' => false, 'message' => 'Please select a payroll.');
    }
    
    $saved_result = get_payroll_calculation_seqs_by_payroll_id($payroll_id);    
        $saved_recs = db_num_rows($saved_result);
        
        begin_transaction();
        
            if ($saved_recs > 0) {
                $dl_af_rows = delete_calculation_seqs($payroll_id);
                    if ($saved_recs != $dl_af_rows) {
                        cancel_transaction();
                        
                        return array('success' => false, 'message' => 'Could not delete payroll seq.');
                    }
            }
        
            $result = get_calculation_seqs($payroll_id);
                while ($myrow = db_fetch($result)) {
                    if (check_data($myrow["id"])) {
                        $seq_no = input_num("seq_no".$myrow["id"]);
                        $print_ref = get_post("print_ref".$myrow["id"]);
                        $print_seq = input_num("print_seq".$myrow["id"], 0);
                    } else {
                        $seq_no = 0;
                        $print_ref = 0;
                        $print_seq = 0;
                    }

                    $insert_id = add_calculation_seq($_POST['payroll_id'], $_POST["list_value_id".$myrow["id"]], $seq_no, $myrow["value_code"], $print_ref, $print_seq, 
                                                     $_POST["active".$myrow["id"]]);
                        if (! ($insert_id > 0)) {
                            cancel_transaction();
                            
                            $component = $myrow['value_name'];

                            return array('success' => false, 'message' => "Could not add calculation seq for component $component.");
                        }
                        
                    $saved_rec_count++;
                }
                
                if (! ($saved_rec_count > 0)) {
                    return array('success' => false, 'message' => "Could not add any calculation seq.");
                }
                
        commit_transaction();
                
        return array('success' => true, 'message' => "");
}

if (isset($_POST['UPDATE_SEQ'])) {
    $msg = save_payroll_calculation_seqs($_POST['payroll_id']);
        if ($msg['success'] === false) {
            display_error($msg['message']);
        } else {
            display_notification('Calculation sequence has been updated.');
        }
        $Mode = 'RESET';
}

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

//if ($Mode == 'Delete')
//{
//        return;
//    
//        delete_statutory_deduction($selected_id);
//	display_notification(_("The selected statutory deduction has been deleted."));
//	$Mode = 'RESET';
//}

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

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

$Ajax->activate('price_table');

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

$heading_text = "Calculation Seq.";


display_heading($heading_text);

$result = get_calculation_seqs($_POST['payroll_id']);

div_start('price_table');

br();

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

$th = array(
     _("Seq. No"), _("Component Type"), _("Component Code"), _("Component Name"), _("Print Ref."), _("Print Seq."), _("Active"));

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

$previous_rec_status = 0;

while ($myrow = db_fetch($result))
{
//        $_POST["seq_no".$myrow["id"]] = 0;
                
        if($myrow["seq_no"] == 0){
            $seq_no = 0;
        }
        else{
            $seq_no = $myrow["seq_no"];
        }
        
        if($myrow["print_ref"] == ''){
            $print_ref = '';
        }
        else{
            $print_ref = $myrow["print_ref"];
        }
        
        if($myrow["print_seq"] == 0){
            $print_seq = 0;
        }
        else{
            $print_seq = $myrow["print_seq"];
        }
        
        if($previous_rec_status == 1 && $myrow["active"] == 0){
            table_section_title("Inactive Processes", 7);
            
        }

	alt_table_row_color($k);

        //label_cell($myrow["id"]);
        hidden("list_value_id".$myrow["id"], $myrow["id"]);
        //qty_cells(null,"seq_no".$myrow["id"],$seq_no,null,null,0) ;
        amount_cells_ex(null,"seq_no".$myrow["id"], 6, 10, $seq_no, null, null, 0);
        //($myrow["seq_no"]);
        label_cell($myrow["list_name"]);
        label_cell($myrow["value_code"]);
        label_cell($myrow["value_name"]);
        //amount_cells_ex(null,"print_ref".$myrow["id"], 6, 10, $print_ref, null, null, 0);
        //text_cells_ex(null, "print_ref".$myrow["id"], $print_ref, 15, 15);
        text_cells(null, "print_ref".$myrow["id"], $print_ref, 15, 15);
        amount_cells_ex(null,"print_seq".$myrow["id"], 6, 10, $print_seq, null, null, 0);
        yesno_list_cells(null, "active".$myrow["id"],$myrow["active"], "Yes", "No" );
        if($myrow["active"] == 1)
            $active = "Yes";
        else {
            $active = "No";
        }
        //label_cell($active,"align=center");

 	//edit_button_cell("Edit".$myrow['id'], _("Edit"));
        
        end_row();
        
        $previous_rec_status = $myrow["active"];

}

end_table();

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

br(2);

start_table(TABLESTYLE, "width=30%");
label_row(null,"<b>"."Salary Printing Report References"."</b>");
label_row(null,"Basic Earnings: BE");
label_row(null,"Basic Deductions: BD");
label_row(null,"Basic Salary Final: BASIC_FINAL");
label_row(null,"Earnings for EPF: SALARY_FOR_EPF");
label_row(null,"EPF Employer: EPF12");
label_row(null,"ETF Employer: ETF3");
label_row(null,"Incentives Earnings: IE");
label_row(null,"Incentives Deductions: ID");
label_row(null,"Incentives Salary Final: INCENTIVE_FINAL");
label_row(null,"Common Earnings: CE");
label_row(null,"Common Deductions: CD");

end_table(2 );

submit_center('UPDATE_SEQ', _("Update"), true, '', 'default');

br();

div_end();

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