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

	Released under the terms of the GNU General Public License, GPL,
***********************************************************************/
$page_security = 'SA_ITEMSVALREP';
// ----------------------------------------------------------------
// $ Revision:	2.0 $
// Creator:	Joe Hunt
// date_:	2005-05-19
// Title:	Stock Check Sheet
// ----------------------------------------------------------------
$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/data_checks.inc");
include_once($path_to_root . "/gl/includes/gl_db.inc");
include_once($path_to_root . "/inventory/includes/inventory_db.inc");
include_once($path_to_root . "/payroll/includes/payroll_calculation/pc_functions.inc");
include_once($path_to_root . "/payroll/includes/db/payroll_calculation_db.inc");
include_once($path_to_root . "/admin/db/list_values_db.inc");
include_once($path_to_root . "/payroll/includes/db/salary_totals_db.inc");
include_once($path_to_root . "/payroll/includes/db/employees_db.inc");
include_once($path_to_root . "/payroll/includes/db/employee_departments_db.inc");
include_once($path_to_root . "/payroll/includes/db/job_details_db.inc");


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

print_signature_sheet();

function get_payroll_employees($payroll_run_id, $from_employee, $to_employee)
{
	$sql = " SELECT DISTINCT prd.employee_code 
                 FROM ".TB_PREF."payroll_run_details prd, 
                        ".TB_PREF."payroll_runs pr   
                 WHERE prd.payroll_run_id = pr.id AND 
                        prd.employee_code <> '' AND 
                        pr.id = ".db_escape($payroll_run_id)  ;
        
        if(strlen($from_employee) > 0){
            $sql .= " AND prd.employee_code >= ".db_escape($from_employee);
        }
        
        if(strlen($to_employee) > 0){
            $sql .= " AND prd.employee_code <= ".db_escape($to_employee);
        }
        
	$sql .= " ORDER BY prd.employee_code";
	return db_query($sql, "Could not retreive payroll employess");
}

function get_componant_names($payroll_run_id)
{
        $sql = "SELECT lv.list_code, lv.value_code, lv.value_name  
                FROM ".TB_PREF."list_values lv 
                WHERE lv.list_code IN(".PAYROLL_LISTS.")";
              
    
   	$result = db_query($sql, "Payslip details could not be retrieved");

        while($myrow=db_fetch($result)){
            $componanet_names[$myrow['value_code']] = $myrow['value_name'];
        }
         
        $componanet_names["LOAN_DEDUCTIONS"] = "Loan Deductions";
        return $componanet_names;
}


function get_employer_contribution($payroll_run_id, $employee)
{
        $sql = "SELECT prd.*, lv.list_code, lv.value_name  
                FROM ".TB_PREF."payroll_run_details prd, 
                        ".TB_PREF."payroll_runs pr, 
                        ".TB_PREF."list_values lv,  
                        ".TB_PREF."payroll_calculation_seq pcs 
                WHERE prd.payroll_run_id = pr.id AND 
                        pr.payroll_id = pcs.payroll_id AND 
                        pcs.process_code = prd.component AND  
                        pcs.process_code = lv.value_code AND 
                        lv.list_code IN('COMMON_SALARY_COMP','STATUTORY_DEDUCTIONS') AND 
                        prd.payroll_run_id = ".db_escape($payroll_run_id)." AND 
                        prd.employee_code = ".  db_escape($employee)." 
                ORDER BY pcs.seq_no";
    
   	$result = db_query($sql, "Payslip details could not be retrieved");

        return $result;
}


function get_employeee_details($employee, $end_date=null)
{
    $sql = "SELECT em.* , IFNULL(dn.value_name,' --') department_name
            FROM ".TB_PREF."employee_master em
                LEFT JOIN ".TB_PREF."employee_departments ed ON 
                    em.employee_code = ed.employee_code ";
        
        if ($end_date !== null) {
            $sql .= " AND ed.start_date <= '$end_date' AND ed.end_date >= '$end_date' ";
        }
            
        $sql .= " 
                LEFT JOIN ".TB_PREF."list_values dn ON
                    ed.department_id=dn.id 
                WHERE em.employee_code = ".db_escape($employee);
        
   	$result = db_query($sql, "Employee details could not be retrieved");

        return db_fetch($result);
}


function get_payroll_run_details($payroll_run_id)
{
        $sql = "SELECT * 
                FROM ".TB_PREF."payroll_runs  
                WHERE id = ".  db_escape($payroll_run_id);
        
   	$result = db_query($sql, "Payroll run details could not be retrieved");

        return db_fetch($result);
}


function print_dep_sal_details($rep, $comp_name, $component_value)
{
    $rep->NewLine();
    $rep->TextCol(0, 1, $comp_name);
    $value = number_format($component_value,2);
    $rep->TextCol(1, 2, $value);
}

function display_department_sub_heading($rep, $dep_salary_row)
{
    $rep->NewPage(); 
    $rep->NewLine(); 
    $rep->Font('bold'); 
    $rep->TextCol(0, 1, $dep_salary_row['department_code']." - ".$dep_salary_row['department_name'] );
    $rep->Font(''); 
    $rep->NewLine(); 
}

function get_employees_for_salary_report($payroll_run_id, $filtering_employee_from,$filtering_employee_to)
{
	$sql = " SELECT DISTINCT prd.employee_code
                 FROM ".TB_PREF."payroll_run_details prd
                 WHERE prd.payroll_run_id =".db_escape($payroll_run_id);
        
        if(strlen($filtering_employee_from) > 0){
            $sql .= " AND prd.employee_code >= ".db_escape($filtering_employee_from);
        }
         if(strlen($filtering_employee_to) > 0){
            $sql .= " AND prd.employee_code <= ".db_escape($filtering_employee_to);
        }
	$sql .= " ORDER BY prd.employee_code";
	return db_query($sql, "Could not retreive payroll employess");
}


function get_employee_salary_component_result($payroll_run_id,$employee, $components_in)
{
        $sql = "SELECT SUM(value) component_sum   
                FROM ".TB_PREF."payroll_run_details 
                WHERE payroll_run_id = ".$payroll_run_id." AND component IN (".$components_in.") 
                AND employee_code=".db_escape($employee);
            
        
   	$result = db_query($sql, "ETF salary components could not be retrieved");

        $value = db_fetch($result);
        if($value){
            return $value['component_sum'];
        }
        else{
            return 0;
        }
}


function get_departments($department)  
 {
            $sql = "SELECT  
                ls.value_name department_name
                FROM 
                " .TB_PREF."list_values ls WHERE ls.id=".db_escape($department);
            
       $result = db_query($sql, "Status could not be retrieved");
       return db_fetch($result);
 }
 
 function initialize_additions_array(&$arr)
 {
     $arr['BASIC_SALARY'] = 0;
     $arr['BRA_1'] = 0;
     $arr['BRA_2'] = 0;
//     $arr['SALARY_FOR_OT'] = 0;
//     $arr['SALARY_TOTAL_2'] = 0;
//     $arr['APPLICABLE_INSENTIVE'] =0;
//     $arr['SALARY_ARREARS']=0;
//     $arr['INCENTIVE']=0;
     $arr['BANKING_INCENTIVE_II']=0;
     $arr['BANKING_INCENTIVE']=0;
     $arr['BANKING_INCENTIVE_I']=0;
     $arr['TOTAL_BASIC_TOTAL'] = 0;
//     $arr['TOTAL_SALARY']=0;
//     $arr['NO_PAY_DEDUCTIONS']=0;
//     $arr['SALARY_FOR_EPF']=0;
       $arr['COMP_EPF_8']=0;
       $arr['PAYE']=0;
       $arr['COMP_PAYE']=0;
       $arr['BASIC_SALARY_NET']=0;
//       $arr['COMP_EPF_8']=0;
    
    
//     $arr['OVER_TIME']=0;
//     $arr['SINGLE_OT_HOURS']=0;
//     $arr['SINGLE_OT_RATE']=0;
//     $arr['SINGLE_OT_AMOUNT']=0;
//     $arr['DOUBLE_OT_HOURS']=0;
//     $arr['DOUBLE_OT_RATE']=0;
//     $arr['DOUBLE_OT_AMOUNT']=0;
     $arr['OT_TOTAL']=0;
     $arr['VEHICLE_REIMBURSEMEN']=0;
     $arr['INCENTIVE_ARREARS']=0;
     $arr['ACCOMMODATION']=0;
     $arr['FUEL_ALLOWANCES']=0;
     $arr['FUEL_REIMBURSEMENT_T']=0;
     
     $arr['MEAL_REIMBURSEMENT']=0;
     $arr['MEAL_REIMBURSEMENT_T']=0;
     $arr['SITE_EXPENSES']=0;
     $arr['TRAVELLING_ALLOWANCE']=0;
     $arr['COMMISSION']=0;
     $arr['SAVINGS_COMMISSION']=0;
     $arr['DRIVING_ALLOWANCE']=0;
     
     $arr['SPECIAL_ATTENDANCE']=0;
//     $arr['SALES_COMMISSION']=0;
     $arr['SPECIAL_PAYMENT']=0;
     $arr['SPECIAL_PAYMENT1']=0;
     $arr['ATTENDANCE_INCENTIVE']=0;
     $arr['NO_PAY_REFUND']=0;
     $arr['LOAN_INSTALLMENTS']=0;
     
     $arr['LOAN_INSTALLMENTS']=0;
     $arr['DATA_OVER_LIMIT']=0;
     $arr['MOBILE_DEDUCTION_1']=0;
     $arr['WATER_ELECTRICITY']=0;
     $arr['INSURANCE']=0;
     $arr['WELFARE_REGISTRATION']=0;
     $arr['WELFARE']=0;
     
//     $arr['SALARY_ADVANCE']=0;
      $arr['PANELTY']=0;
     $arr['SALARY_ADVANCES']=0;
    
     $arr['EPF_DEDUCTION']=0;
//     $arr['OTHER_DEDUCTIOIN']=0;
//     $arr['OTHER_DEDUCTIOIN_2']=0;
//     $arr['EPF_12%']=0;
//     $arr['ETF_3%']=0;
//     $arr['DEDUCTIONS_FROM_BASIC']=0;
     
     $arr['BANKING_INCENT_TOTAL']=0;
//     $arr['BASIC_NOPAY_DAYS']=0;
//     $arr['NOPAY_1_RATE']=0;
//     $arr['NOPAY_1_AMOUNT']=0;
//     $arr['INSENTIVE_NOPAY_DAYS']=0;
//     $arr['NOPAY_2_RATE']=0;
//     $arr['NOPAY_2_AMOUNT']=0;
     
     $arr['ADDONS_FROM_BASIC']=0;
     $arr['ADDONS_TO_INSENTIVE']=0;
     $arr['TOTAL_ADDONS']=0;
     
     $arr['NET_PAY_TO_BANK']=0;
//     $arr['NET_INSENTIVE']=0;
//     $arr['ROUNDING_OFF']=0;
//     $arr['BF']=0;
 }

 
 
 
 function get_employee_loan_amount($payroll_run_id,$employee_code)
 {
    $sql="SELECT  SUM(li.installment_amount) loan_amount
          FROM " .TB_PREF."loan_installments li
          LEFT JOIN ".TB_PREF."loans lo ON
                  lo.id=li.loan_id
          WHERE li.status_code='Y' AND li.payroll_run_id=".db_escape($payroll_run_id)." AND lo.employee_code=".db_escape($employee_code);
              

       $result = db_query($sql, "Loan deduction amount can not be retrieved");            
       
       $value = db_fetch($result);
        if($value){
            return $value['loan_amount'];
        }
        else{
            return 0;
        }
     
 }
 
 function add_to_report_totals(&$rpt_totals, &$emp)
 {

    foreach ($rpt_totals as $key => $value) {
        $rpt_totals[$key] += abs($emp[$key]);
        $emp[$key] = 0;        
    }
    
 }
 
 function get_employee_loan_deduc_result($payroll_run_id,$employee_code)
 {
     $loan_amount=get_employee_loan_amount($payroll_run_id,$employee_code);
     return $loan_amount;
 }
 
 function display_component_values(&$rep, $dept, $component_names, $grand_totals = false) // type (STD, OTHR, STAT, GRND)
 {
//     $i=5;
//     foreach ($dept as $key => $value) {
//         
//          $rep->TextCol($i, $i+1, $key);
//         
//          $i++;
//     }
//     $rep->NewLine();
    $dept_additions_total = 0;
    foreach ($dept as $key => $value) {
        
        if($key == 'BASIC_SALARY'){
           $rep->TextCol(5, 6, number_format($dept[$key],2));

        }
        elseif($key == 'BRA_1'){
//           $rep->TextCol($i, $i+1, "Fuel Reimbursement");
           $rep->TextCol(6, 7, number_format($dept[$key],2));

        }
        elseif($key == 'BRA_2'){
           $rep->TextCol(7, 8, number_format($dept[$key],2));

        }
//        elseif($key == 'SALARY_FOR_OT'){
//           $rep->TextCol(8, 9, number_format($dept[$key],2));
//
//        }
        elseif($key == 'BANKING_INCENTIVE_II'){
           $rep->TextCol(8, 9, number_format($dept[$key],2));

        }
         elseif($key == 'BANKING_INCENTIVE'){
           $rep->TextCol(9, 10, number_format($dept[$key],2));

        }
          elseif($key == 'BANKING_INCENTIVE_I'){
           $rep->TextCol(10, 11, number_format($dept[$key],2));

        }
         elseif($key == 'TOTAL_BASIC_TOTAL'){
           $rep->TextCol(11, 12, number_format($dept[$key],2));

        }
//        elseif($key == 'SALARY_FOR_EPF'){
//           $rep->TextCol(12, 13, number_format($dept[$key],2));
//
//        }
        
         elseif($key == 'COMP_EPF_8'){
           $rep->TextCol(12, 13, number_format($dept[$key],2));

        }
        elseif($key == 'COMP_PAYE' || $key == 'PAYE'){
           $rep->TextCol(13, 14, number_format($dept[$key],2));

        }
        
        
//        elseif($key == 'DEDUCTIONS_FROM_BASIC'){
//           $rep->TextCol(15, 16, number_format($dept[$key],2));
//
//        }
        elseif($key == 'BASIC_SALARY_NET'){
           $rep->TextCol(14, 15, number_format($dept[$key],2));

        }

        
        
    }

 }
 

 

 
 
function populate_employee_salary_array($payroll_run_id, $employee_code,&$dep_head_count)
{
    $salary = array();
    $result = get_employee_salary_components($payroll_run_id, $employee_code);
    if(db_num_rows($result)>0)
    {
        $dep_head_count +=1;
    }
    while($myrow=db_fetch($result)){
        $salary[$myrow['component']] = $myrow['value'];
    }

    return $salary;
}


function get_employee_salary_component_value($salary, $component)
{
    if(!is_array($salary)){
        return 0;
    }
    
     if($component == 'SINGLE_OT_HRS'){
        $value_m = substr($salary['amount_achieved'] ,strpos($salary['amount_achieved'],'.'));
        $value_h = explode(".", $salary['amount_achieved'], 2);
        $first = $value_h[0];
        if($value_m >0)
        {
            $minutes = $value_m *60;
        }
        else
        {
            $minutes = '00';
        }
        $hrs = $first.":".$minutes;
        return $hrs;
     }
    
    foreach ($salary as $key => $value) {
        if($key == $component){
            $x = $salary[$key];
            return $x;
        }
        
    }

        return 0;
    
    
}

function display_dept_head_count(&$rep,$head_count)
{
    $rep->NewLine();
    $rep->font('b');
    $rep->TextCol(1, 2, "Head Count  :  ".$head_count);   
    $rep->font('');
    $rep->NewLine();
    
    
}

function get_employee_ot_hrs($employee_code,$ot_type,$payroll_id)
{
    $sql = "SELECT * FROM ".TB_PREF."performance_incentives_details
            WHERE employee_code = ".db_escape($employee_code)." AND component = ".db_escape($ot_type)." 
            AND payroll_run_id=". db_escape($payroll_id); 
    
    $result = db_query($sql, "OT Details could not be retrieved");
    return db_fetch($result);    
    
}

function get_employee_ot_rate($employee_code,$ot_type,$period_end_date)
{
     $sql = "SELECT sc.amount 
             FROM ".TB_PREF."salary_components sc
             LEFT JOIN ".TB_PREF."list_values lv ON
                 lv.id=sc.salary_component_id
             WHERE sc.employee_code = ".db_escape($employee_code)." AND lv.value_code = ".db_escape($ot_type)." 
             AND sc.end_date >=". db_escape($period_end_date)." AND sc.effective_date <= ". db_escape($period_end_date)."
             ORDER BY sc.effective_date DESC LIMIT 1"; 
    
    $result = db_query($sql, "OT Rate could not be retrieved");
    return db_fetch($result);  
}

function build_header_names(&$s, &$a, &$column_positions)
{

    $a = array_merge(array("left"),
                    array("left"),
                    array("left"),
                    array("left"),
                    array("left"), 
                    array("right"),
                    array("right"),
                    array("right"),
                    array("right"), 
                    array("right"), 
                    array("right"), 
                    array("right"),
                    array("right"),
                    array("right"),
//                    array("right"),
//                    array("right"),
                    array("right"));
    $s = array_merge(array("Employee No"),
                    array("Name"), 
                    array("Department"),
                    array("Designation"),
                    array("Signature"),
                    array("Basic Salary"),
                    array("BRA 1"), 
                    array("BRA 2"),
//                    array("Total Basic Pay(Salary for OT)"),
                    array("Incentive - Bank(BANKING_INCENTIVE_II)"),
                    array("Vehicle Reimbursement(Banking Incentive)"),
                    array("Meal Reimbursement II(Banking Incentive I)"),
                     array("Total Basic (Salary for EPF + Single OT + Double OT)"),
//                    array("Total Basic (SALARY_FOR_EPF)"),
                    array("8% EPF (COMP_EPF_8)"),
                    array("Paye Tax(COMP_PAYE)"),
//                    array("Deductions From Basic"),
                     array("Take Home Basic(Net salary basic)")
//                    array("Net Pay Basic")
            );
    
   
    $column_positions= array_merge(array(0,100,300,450,700,850,1000,1100,1200,1400,1500,1600,1700,1800,1900,2000));

}

function get_employee_net_pay_to_bank($emp,$bank_basic_salary,$bank_incentive_salary)
{
    if($bank_basic_salary == 1 && $bank_incentive_salary == 0 )
    {
        $net_pay_value = $emp['BASIC_SALARY_NET'];
        return $net_pay_value;
    }
    if($bank_basic_salary == 0 && $bank_incentive_salary == 1 )
    {
        $net_pay_value = $emp['TAKEHOME_INCENTIVE'];
        return $net_pay_value;
    }
    if($bank_basic_salary == 1 && $bank_incentive_salary == 1 )
    {
        $net_pay_value = $emp['TAKEHOME_INCENTIVE'] + $emp['BASIC_SALARY_NET'];
        return $net_pay_value;
    }
    else{
        
        return $net_pay_value = 0;
    }
    
//     return $net_pay_value;
    
}

function get_employee_net_pay_to_hand($emp,$bank_basic_salary,$bank_incentive_salary)
{
    if($bank_basic_salary == 1 && $bank_incentive_salary == 0 )
    {
        $net_pay_hand = $emp['TAKEHOME_INCENTIVE'];
        return $net_pay_hand;
    }
    if($bank_basic_salary == 0 && $bank_incentive_salary == 1 )
    {
        $net_pay_hand = $emp['BASIC_SALARY_NET'];
        return $net_pay_hand;
    }
    if($bank_basic_salary == 0 && $bank_incentive_salary == 0 )
    {
        $net_pay_hand = $emp['TAKEHOME_INCENTIVE'] + $emp['BASIC_SALARY_NET'];
        return $net_pay_hand;
    }
    else{
        
        return $net_pay_hand = 0;
    }
    
//     return $net_pay_hand;
    
}
//----------------------------------------------------------------------------------------------------

function print_signature_sheet()
{
    global $path_to_root, $pic_height;
    $payroll_run_id = $_POST['PARAM_0'];
    $filtering_employee_from = $_POST['PARAM_1'];
    $filtering_employee_to = $_POST['PARAM_2'];
    $valid_salaries = $_POST['PARAM_3'];
    $released_salaries = $_POST['PARAM_4'];
    $hold_salaries = $_POST['PARAM_5'];
    
    
//    $payroll_run_id = $_POST['PARAM_0'];
//    $department = $_POST['PARAM_1'];
//    $destination = $_POST['PARAM_2'];

	include_once($path_to_root . "/reporting/includes/excel_report.inc");
       

	$orientation = 'L';
        
        $headers = array();
        $aligns = array();
        $cols = array();
        
        build_header_names($headers, $aligns, $cols);

//        $cols = array(0, 270,420,500);
//        $headers = array('Component','','Amount');
//        $aligns = array('left','left','right');
   	$rep = new FrontReport(_('Salary Sheet'), "SalarySheet", 'A3', 9, $orientation);
        
       
        
         if($payroll_run_id != "-1")
        {
                  
            $payroll_rows= get_payroll_run_details($payroll_run_id);           
            $payrol_run_name=sql2date($payroll_rows['period_start_date'])." - ".sql2date($payroll_rows['period_end_date'])." - ".$payroll_rows['description'];
        
        }
        else
        {
           $payrol_run_name ="None";
        } 
        if($filtering_employee_from != '')
        {
           
            $employee_detials = get_employeee_details($filtering_employee_from);          

            $filtering_employ_from = "[".$employee_detials['employee_code']."] - ".$employee_detials['initials'].
                                " ".$employee_detials['last_name']." (".$employee_detials["first_name"].") ";
        }else{
            $filtering_employ_from = "All";
        }
         if($filtering_employee_to != '')
        {   
            $employee_detials = get_employeee_details($filtering_employee_to);          
            $filtering_employ_to = "[".$employee_detials['employee_code']."] - ".$employee_detials['initials'].
                                " ".$employee_detials['last_name']." (".$employee_detials["first_name"].") ";
        }else{
            $filtering_employ_to = "All";
        }
        $valid_sal ='';
        $released_sal = '';
        $hold_sal= '';
        if($valid_salaries != 0){$valid_sal = 'Yes';}
        else {$valid_sal = 'No';}
        if($released_salaries != 0){$released_sal = 'Yes';}
        else {$released_sal = 'No';}
         if($hold_salaries != 0){$hold_sal = 'Yes';}
        else {$hold_sal = 'No';}
        $params =   array( 	0 => $payrol_run_name,
                                1 => $filtering_employ_from,
                                2 => $filtering_employ_to,
                                3 => $valid_sal,
                                4 => $released_sal,
                                5 => $hold_sal
                                        );
                
                //$rep->formData['doctype'] = "form_950";
        $rep->SetHeaderType('form_976');
        $rep->Font();
        $rep->Info($params, $cols, $headers, $aligns);

        $rep->NewPage();
//        $current_department = "";
        $payroll_data =get_payroll_run_details($payroll_run_id);

        
        
        $rpt_totals= array();
        $emp = array();
        
      
        
       

        initialize_additions_array($rpt_totals);
        initialize_additions_array($emp);
        

        $dep_head_count=0;
        
        $com_names = get_componant_names($payroll_run_id);
                
//        $NO_DEPARTMENT_new_page = true;
        $result_dep_salary= get_employees_for_salary_report($payroll_run_id,$filtering_employee_from,$filtering_employee_to);
        $grand_head_count=0; 
        $amount_single_ot = 0;
        $amount_double_ot = 0;
            while ($dep_salary_row = db_fetch($result_dep_salary))
            {
               if(strlen($dep_salary_row['employee_code']) != 0)
               {
                $emp_name = get_employee($dep_salary_row['employee_code']);
                $name = $emp_name['initials']." ".$emp_name['last_name']." (".$emp_name["first_name"].") "; 
                
                $department_name = $emp_designation_name = '';
                $depatment_details = get_employee_curent_departments($dep_salary_row['employee_code'], $payroll_rows['period_end_date']);
                    if (isset($depatment_details['id']) && $depatment_details['id'] > 0) {
                        $department_name = $depatment_details['department_name'];
                    }
                $emp_designation_details= get_employee_current_designation($dep_salary_row['employee_code'], $payroll_rows['period_end_date']);
                    if (isset($emp_designation_details['id']) && $emp_designation_details['id'] > 0) {
                        $emp_designation_name = $emp_designation_details['designation'];
                    }

                $rep->textcol(0, 1, $dep_salary_row['employee_code']);
                $rep->textcol(1, 2, $name);
                $rep->textcol(2, 3, $department_name);
                $rep->textcol(3, 4, $emp_designation_name);
                $rep->textcol(4, 5, " ");

               
                initialize_additions_array($emp);
                
        
                $salary = "";
                $salary = populate_employee_salary_array($payroll_run_id, $dep_salary_row['employee_code'],$dep_head_count);

                $component = "BASIC_SALARY";
                $emp['BASIC_SALARY'] = get_employee_salary_component_value($salary, $component);

                $component = "BRA_1";
                $emp['BRA_1'] = get_employee_salary_component_value($salary, $component);
                
                $component = "BRA_2";
                $emp['BRA_2'] = get_employee_salary_component_value($salary, $component);
                
//                $component = "SALARY_FOR_OT";
//                $emp['SALARY_FOR_OT'] = get_employee_salary_component_value($salary, $component);
                
                $component = "SALARY_TOTAL_2";
                $emp['APPLICABLE_INSENTIVE'] = get_employee_salary_component_value($salary, $component);
                
                
                $component = "BANKING_INCENTIVE_II";
                $emp['BANKING_INCENTIVE_II'] = get_employee_salary_component_value($salary, "BANKING_INCENTIVE_II");
                
                $component = "BANKING_INCENTIVE";
                $emp['BANKING_INCENTIVE'] = get_employee_salary_component_value($salary, "BANKING_INCENTIVE");
                
                $component = "BANKING_INCENTIVE_I";
                $emp['BANKING_INCENTIVE_I'] = get_employee_salary_component_value($salary, "BANKING_INCENTIVE_I");

                $component = "TOTAL_BASIC_TOTAL";
                $emp['TOTAL_BASIC_TOTAL'] = get_employee_salary_component_value($salary, 'SALARY_FOR_EPF')+ 
                                            get_employee_salary_component_value($salary, 'SINGLE_OT') +
                                            get_employee_salary_component_value($salary, 'DOUBLE_OT');
                
                $component = "SALARY_TOTAL_1";
                $emp['SALARY_TOTAL_1'] = get_employee_salary_component_value($salary, "SALARY_TOTAL_1");
                
                $component = "NO_PAY_DEDUCTIONS";
                $emp['NO_PAY_DEDUCTIONS'] = get_employee_salary_component_value($salary, "NO_PAY_DEDUCTIONS");

                $component = "SALARY_FOR_EPF";
                $emp['SALARY_FOR_EPF'] = get_employee_salary_component_value($salary, "SALARY_FOR_EPF");
                
                $component = "COMP_EPF_8";
                $emp['COMP_EPF_8'] = get_employee_salary_component_value($salary, "COMP_EPF_8");
                
            
//                $component = "COMP_PAYE";
//                $emp['COMP_PAYE'] = get_employee_salary_component_value($salary, "COMP_PAYE");
                $emp['PAYE'] = get_employee_salary_component_value($salary, "PAYE");
                $emp['PAYE'] += get_employee_salary_component_value($salary, "COMP_PAYE");
                
                $component = "ADDONS_TO_EPF_SALARY";
                $emp['ADDONS_TO_EPF_SALARY'] = get_employee_salary_component_value($salary, "ADDONS_TO_EPF_SALARY") ;
                
                $component = "OVER_TIME";
                $emp['OVER_TIME'] = get_employee_salary_component_value($salary, "OVER_TIME");
                
//---------------------------------------------------------------------------------------------------------------------------------------
                $single_ot_hrs= get_employee_ot_hrs($dep_salary_row['employee_code'],'SINGLE_OT',$payroll_run_id);
                $component = "SINGLE_OT_HRS";
                $emp['SINGLE_OT_HOURS'] = get_employee_salary_component_value($single_ot_hrs, $component);                
                
                $single_ot_rate = get_employee_ot_rate($dep_salary_row['employee_code'],'SINGLE_OT',$payroll_data['period_end_date']);
                $component = "SINGLE_OT_RATE";
                $emp['SINGLE_OT_RATE'] = get_employee_salary_component_value($single_ot_rate, $component);  
                
                
                $component = "SINGLE_OT";
                $emp['SINGLE_OT_AMOUNT'] = get_employee_salary_component_value($salary, $component);
                
  //---------------------------------------------------------------------------------------------------------------------------------------
                $double_ot_hrs_value = 0;
                $double_ot_hrs= get_employee_ot_hrs($dep_salary_row['employee_code'],'DOUBLE_OT',$payroll_run_id);
                $component = "DOUBLE_OT_HRS";
                    if (isset($double_ot_hrs['id']) && $double_ot_hrs['id'] > 0) {
                        $double_ot_hrs_value = $double_ot_hrs['amount_achieved'];
                    }
                $emp['DOUBLE_OT_HRS'] = get_employee_salary_component_value($double_ot_hrs_value, $component);  
                
                $double_rate_value = 0;
                $double_ot_rate = get_employee_ot_rate($dep_salary_row['employee_code'],'DOUBLE_OT',$payroll_data['period_end_date']);
                $component = "DOUBLE_OT_RATE";
                    if (isset($double_ot_rate['amount'])) {
                        $double_rate_value = $double_ot_rate['amount'];
                    }
                $emp['DOUBLE_OT_RATE'] = get_employee_salary_component_value($double_rate_value, $component);  
                
                
                $component = "DOUBLE_OT";
                $emp['DOUBLE_OT_AMOUNT'] = get_employee_salary_component_value($salary, $component);
                
  //---------------------------------------------------------------------------------------------------------------------------------------
      
                $component = "OT_TOTAL";
                $emp['OT_TOTAL'] = get_employee_salary_component_value($salary, "OT_TOTAL");
                
                $component = "VEHICLE_REIMBURSEMEN";
                $emp['VEHICLE_REIMBURSEMEN'] = get_employee_salary_component_value($salary, "VEHICLE_REIMBURSEMEN");
                
                $component = "INCENTIVE_ARREARS";
                $emp['INCENTIVE_ARREARS'] = get_employee_salary_component_value($salary, "INCENTIVE_ARREARS");
                
                $component = "ACCOMMODATION";
                $emp['ACCOMMODATION'] = get_employee_salary_component_value($salary, "ACCOMMODATION");
                
                $component = "FUEL_ALLOWANCES";
                $emp['FUEL_ALLOWANCES'] = get_employee_salary_component_value($salary, "FUEL_ALLOWANCES");
                
                $component = "FUEL_REIMBURSEMENT_T";
                $emp['FUEL_REIMBURSEMENT_T'] = get_employee_salary_component_value($salary, "FUEL_REIMBURSEMENT_T");
                
                $component = "MEAL_REIMBURSEMENT_T";
                $emp['MEAL_REIMBURSEMENT_T'] = get_employee_salary_component_value($salary, "MEAL_REIMBURSEMENT_T");
                
                $component = "SITE_EXPENSES";
                $emp['SITE_EXPENSES'] = get_employee_salary_component_value($salary, "SITE_EXPENSES");
                
                $component = "TRAVELLING_ALLOWANCE";
                $emp['TRAVELLING_ALLOWANCE'] = get_employee_salary_component_value($salary, "TRAVELLING_ALLOWANCE");
                
                $component = "COMMISSION";
                $emp['COMMISSION'] = get_employee_salary_component_value($salary, "COMMISSION");
                
                
                $component = "SAVINGS_COMMISSION";
                $emp['SAVINGS_COMMISSION'] = get_employee_salary_component_value($salary, "SAVINGS_COMMISSION");
                
                $component = "DRIVING_ALLOWANCE";
                $emp['DRIVING_ALLOWANCE'] = get_employee_salary_component_value($salary, "DRIVING_ALLOWANCE");
                
                $component = "SPECIAL_ATTENDANCE";
                $emp['SPECIAL_ATTENDANCE'] = get_employee_salary_component_value($salary, "SPECIAL_ATTENDANCE");
                
                $component = "SPECIAL_PAYMENT";
                $emp['SPECIAL_PAYMENT'] = get_employee_salary_component_value($salary, "SPECIAL_PAYMENT");
                
                $component = "SPECIAL_PAYMENT1";
                $emp['SPECIAL_PAYMENT1'] = get_employee_salary_component_value($salary, "SPECIAL_PAYMENT1");
                
                $component = "ATTENDANCE_INCENTIVE";
                $emp['ATTENDANCE_INCENTIVE'] = get_employee_salary_component_value($salary, "ATTENDANCE_INCENTIVE");
                
                $component = "NO_PAY_REFUND";
                $emp['NO_PAY_REFUND'] = get_employee_salary_component_value($salary, $component);
                
                $component = "LOAN_INSTALLMENTS";
                $emp['LOAN_INSTALLMENTS'] = get_employee_salary_component_value($salary, $component);
                
                $component = "DATA_OVER_LIMIT";
                $emp['DATA_OVER_LIMIT'] = get_employee_salary_component_value($salary, $component);
                
                 
                $component = "MOBILE_DEDUCTION_1";
                $emp['MOBILE_DEDUCTION_1'] = get_employee_salary_component_value($salary, $component);
                
                
                $component = "Water_Electricity";
                $emp['Water_Electricity'] = get_employee_salary_component_value($salary, $component);
                
                $component = "WELFARE_REGISTRATION";
                $emp['WELFARE_REGISTRATION'] = get_employee_salary_component_value($salary, $component);
                
                $component = "INSURANCE";
                $emp['INSURANCE'] = get_employee_salary_component_value($salary, $component);
                
                $component = "WELFARE_REGISTRATION";
                $emp['WELFARE_REGISTRATION'] = get_employee_salary_component_value($salary, $component);
                
                $component = "WELFARE";
                $emp['WELFARE'] = get_employee_salary_component_value($salary, $component);
                
                $component = "SALARY_ADVANCE";
                $emp['SALARY_ADVANCE'] = get_employee_salary_component_value($salary, $component);
                
                $component = "PANELTY";
                $emp['PANELTY'] = get_employee_salary_component_value($salary, $component);
                
                 $component = "SALARY_ADVANCES";
                $emp['SALARY_ADVANCES'] = get_employee_salary_component_value($salary, $component);
                
                $component = "EPF_DEDUCTION";
                $emp['EPF_DEDUCTION'] = get_employee_salary_component_value($salary, $component);
                
                $component = "OTHER_DEDUCTIOIN";
                $emp['OTHER_DEDUCTIOIN'] = get_employee_salary_component_value($salary, $component);
                
                $component = "OTHER_DEDUCTIOIN_2";
                $emp['OTHER_DEDUCTIOIN_2'] = get_employee_salary_component_value($salary, $component);
                
                $component = "EPF 12%";
                $emp['EPF 12%'] = get_employee_salary_component_value($salary, $component);
                
                $component = "ETF 3%";
                $emp['ETF 3%'] = get_employee_salary_component_value($salary, $component);
                
                $component = "BANKING_INCENT_TOTAL";
                $emp['BANKING_INCENT_TOTAL'] = get_employee_salary_component_value($salary, $component);
                
                $component = "ADDONS_TO_BASIC";
                $emp['ADDONS_FROM_BASIC'] = get_employee_salary_component_value($salary, $component);
                
                $component = "ADDONS_TO_INCENTIVE";
                $emp['ADDONS_TO_INSENTIVE'] = get_employee_salary_component_value($salary, $component);                
                
                $emp['TOTAL_ADDONS'] = get_employee_salary_component_value($salary, "ADDONS_TO_BASIC")+
                                       get_employee_salary_component_value($salary, "ADDONS_TO_INCENTIVE");
                
                $emp['DEDUCTIONS_FROM_BASIC'] = get_employee_salary_component_value($salary, "ADDONS_TO_EPF_SALARY")+ get_employee_salary_component_value($salary, "COMP_PAYE")+  
                                       get_employee_salary_component_value($salary, "NO_PAY_DEDUCTIONS");
                
                $component = "TAKEHOME_BASIC";
                $emp['BASIC_SALARY_NET'] = get_employee_salary_component_value($salary, $component); 
                
                $component = "TAKEHOME_INCENTIVE";
                $emp['TAKEHOME_INCENTIVE'] = get_employee_salary_component_value($salary, $component); 
                
                $bank_basic_salary = $bank_incentive_salary = 0;
                if (isset($dep_salary_row['bank_basic_salary'])) {
                    $bank_basic_salary = $dep_salary_row['bank_basic_salary'];
                }
                if (isset($dep_salary_row['bank_incentive_salary'])) {
                    $bank_incentive_salary = $dep_salary_row['bank_incentive_salary'];
                }
                $emp['NET_PAY_TO_BANK'] = get_employee_net_pay_to_bank($emp, $bank_basic_salary, $bank_incentive_salary);
                
                $emp['NET_INSENTIVE'] = get_employee_net_pay_to_hand($emp, $bank_basic_salary, $bank_incentive_salary);

                $component = "ROUNDING_OFF";
                $emp['ROUNDING_OFF'] = get_employee_salary_component_value($salary, $component); 
                
                $component = "BROUGHT_FORWARD";
                $emp['BF'] = get_employee_salary_component_value($salary, $component); 
                
                $dept_additions_total = display_component_values($rep, $emp, $com_names,false);
                add_to_report_totals($rpt_totals, $emp);
                 
                $rep->NewLine();
               }
               
            }
           
            $dept_additions_total = display_component_values($rep, $rpt_totals, $com_names,false);
            $rep->NewLine();
            $rep->SetTextColor(255, 0, 0);
            $rep->TextCol(0, 6, "If a new salary component is added this report should be amended." );
            $rep->SetTextColor(0, 0, 0);

	$rep->NewLine();
    $rep->End();
}


?>
