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

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

print_dep_salary_expences();

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)
{
        $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 AND 
                ed.start_date <= '$end_date' AND ed.end_date >= '$end_date'
                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($department,$start_date,$to_date, $payroll_run_id)
{
//	 $sql = "SELECT 
//                        em.*, 
//                        IFNULL(dn.value_code,'NO_DEPARTMENT')  department_code, 
//                        IFNULL(dn.value_name,'No Department Entered') department_name
//                 FROM ".TB_PREF."vw_payroll_employees vpe 
//                        INNER JOIN ".TB_PREF."employee_master em ON 
//                            vpe.employee_code = em.employee_code 
//                        LEFT JOIN ".TB_PREF."employee_departments ed ON 
//                            em.employee_code = ed.employee_code    
//                        LEFT JOIN ".TB_PREF."list_values dn ON
//                            ed.department_id=dn.id AND ed.start_date <= ".db_escape($to_date)." AND ed.end_date >= ".db_escape($to_date)."                                                  
//                 WHERE  1=1 AND em.date_of_joining <= ".db_escape($to_date)." AND em.date_of_resignation >= ".db_escape($start_date)." 
//                            AND vpe.payroll_run_id = ".db_escape($payroll_run_id);

        $sql = "SELECT em.*, 
                        IFNULL((SELECT ed1.department_id FROM ".TB_PREF."employee_departments ed1   
                                   WHERE vpe.employee_code = ed1.employee_code 
                                            AND ed1.start_date <= ".db_escape($to_date)." 
                                            AND ed1.end_date >= ".db_escape($to_date)."
                                   ORDER BY ed1.start_date DESC LIMIT 1), 0) department_id,
                        IFNULL((SELECT value_code FROM ".TB_PREF."list_values dn WHERE dn.id = department_id), 'NO_DEPARTMENT') department_code, 
                        IFNULL((SELECT value_name FROM ".TB_PREF."list_values dn WHERE dn.id = department_id), 'No Department Entered') department_name                         
                FROM ".TB_PREF."vw_payroll_employees vpe 
                        INNER JOIN ".TB_PREF."employee_master em ON 
                            vpe.employee_code = em.employee_code                                                                 
                WHERE  1=1 
                            AND vpe.payroll_run_id = ".db_escape($payroll_run_id);
     
     
        if($department != "-1")
        {
          $sql .= " HAVING  department_id = ".db_escape($department);
        }
                 
        $sql .= " ORDER BY department_code ,em.employee_code ";
             
        return  $result= db_query($sql,"Employee details could not be retreived");
}

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['TRAVELLING_ALLOWANCE_BANKING_INCENTIVE'] = 0;
     $arr['FUEL_ALLOWANCES_FUEL_REIMBURSEMENT_T'] =0;
     $arr['MEAL_REIMBURSEMENT_MEAL_REIMBURSEMENT_T']=0;
     $arr['ATTENDANCE_INCENTIVE']=0;
     $arr['SITE_EXPENSES']=0;
     $arr['ACCOMMODATION']=0;
     $arr['SPECIAL_PAYMENT_SPECIAL_PAYMENT1']=0;
     $arr['SINGLE_OT']=0;
     $arr['DOUBLE_OT']=0;
     $arr['DRIVING_ALLOWANCE']=0;
     $arr['COMMISSION']=0;
     $arr['INCENTIVE']=0;
     $arr['BANKING_INCENTIVE_II_VEHICLE_REIMBURSEMEN_BANKING_INCENTIVE_I']=0;
     $arr['INCENTIVE_ARREARS']=0;
     $arr['NO_PAY_REFUND']=0;
     $arr['SALARY_ARREARS']=0;
     $arr['SAVINGS_COMMISSION']=0;
     $arr['SPECIAL_ATTENDANCE']=0;
     $arr['POWDER_COATING_ALL']=0;
     
 }
 
 function initialize_std_deductions_array(&$arr)
 {
     $arr['INSURANCE'] = 0;
     $arr['WELFARE'] = 0;
     $arr['OTHER_DEDUCTIOIN_OTHER_DEDUCTIOIN_2'] = 0;
     $arr['MOBILE_DEDUCTION_1']=0;
     $arr['SALARY_ADVANCES']=0;
     $arr['DATA_OVER_LIMIT']=0;
     $arr['Water_Electricity']=0;
     $arr['WELFARE_REGISTRATION']=0;
     $arr['SALARY_ADVANCE']=0;
    //     $arr['LOAN_DEDUCTIONS']=0;
     $arr['COMP_EPF_8']=0;
     $arr['PAYE']=0;
     $arr['LOAN_INSTALLMENTS'] = 0;
     $arr['EPF_DEDUCTION'] = 0;
    //     $arr['COMP_STAMP_DUTY']=0;
     
 }
 
 
  function initialize_other_deductions_array(&$arr)
 {

     $arr['PANELTY']=0;
     $arr['NO_PAY_DEDUCTIONS']=0;
     $arr['EMP-COVID-19'] = 0;
     
 }
 
   function initialize_statutory_deductions_array(&$arr)
 {

     $arr['EPF 12%'] =0;
     $arr['ETF 3%'] =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_department_total(&$dept, &$emp)
 {

    foreach ($dept as $key => $value) {
        $dept[$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_department_sub_totals(&$rep, $dept, $component_names, $grand_totals = false) // type (STD, OTHR, STAT, GRND)
 {
    if($grand_totals==true)
    {
        $rep->NewPage();
        $rep->NewLine();
        $rep->fontSize += 1;
        $rep->font('b');
        $rep->TextCol(0, 1, "All Department Totals" );
        $rep->fontSize -=1;
        $rep->font('');
        $rep->NewLine(2);
        
        $rep->fontSize += 1;
        $rep->font('b');
        $rep->TextCol(0, 1, "Salary Additions" );
        $rep->fontSize -=1;
        $rep->font('');
        $rep->NewLine();
        
    }
    else
    {
        $rep->fontSize += 1;
        $rep->font('b');
        $rep->TextCol(0, 1, "Salary Additions" );
        $rep->fontSize -=1;
        $rep->font('');
        $rep->NewLine();
    }
    $i=0;
    $dept_additions_total = 0;
    foreach ($dept as $key => $value) {
        
        if($key == 'TRAVELLING_ALLOWANCE_BANKING_INCENTIVE'){
           $rep->TextCol($i, $i+1,"Traveling Reimbursement");
           $rep->TextCol($i+2, $i+3, number_format($dept[$key],2));
           $dept_additions_total += $dept[$key];
           $rep->NewLine();
        }
        elseif($key == 'FUEL_ALLOWANCES_FUEL_REIMBURSEMENT_T'){
           $rep->TextCol($i, $i+1, "Fuel Reimbursement");
           $rep->TextCol($i+2, $i+3, number_format($dept[$key],2));
           $dept_additions_total += $dept[$key];
           $rep->NewLine();
        }
        elseif($key == 'BANKING_INCENTIVE_II_VEHICLE_REIMBURSEMEN_BANKING_INCENTIVE_I'){
           $rep->TextCol($i, $i+1, "Incentive - Bank");
           $rep->TextCol($i+2, $i+3, number_format($dept[$key],2));
           $dept_additions_total += $dept[$key];
           $rep->NewLine();
        }
        elseif($key == 'MEAL_REIMBURSEMENT_MEAL_REIMBURSEMENT_T'){
           $rep->TextCol($i, $i+1, "Meal Reimbursement");
           $rep->TextCol($i+2, $i+3, number_format($dept[$key],2));
           $dept_additions_total += $dept[$key];
           $rep->NewLine();
        }
        elseif($key == 'SPECIAL_PAYMENT_SPECIAL_PAYMENT1'){
           $rep->TextCol($i, $i+1, "Special Payment");
           $rep->TextCol($i+2, $i+3, number_format($dept[$key],2));
           $dept_additions_total += $dept[$key];
           $rep->NewLine();
        }
        
        else {
            $name = '';
            $amount = 0;
            if(isset($component_names[$key])){
                $name = $component_names[$key];
                $amount = $dept[$key];
                $rep->TextCol($i, $i+1, $name);
                $rep->TextCol($i+2, $i+3, number_format($amount,2));
                $rep->NewLine();
            }
            
            if(isset($dept[$key])){
                $dept_additions_total += $dept[$key];
            }
           
        }
        
        
    }
    if($grand_totals== true)
    {
          
            $rep->font('b');
            $rep->TextCol(1, 2, "Total Additions");
            $rep->TextCol(2 ,3, number_format($dept_additions_total,2));
        
            $rep->font('');
            $rep->NewLine();
    }
    else
    {
            $rep->font('b');
            $rep->TextCol(1, 2, "Department Additions Total");
            $rep->TextCol(2 ,3, number_format($dept_additions_total,2));
            $rep->NewLine();
            $rep->font('');
    }
    
    return $dept_additions_total;
 }
 
 function display_department_all_deductions_total(&$rep,$dept_deduc_total)
 {
            //$rep->NewLine();
            $rep->font('b');
            $rep->TextCol(1, 2, "Department Deductions Total");
            $rep->TextCol(2 ,3, number_format($dept_deduc_total,2));        
            $rep->font('');
            $rep->NewLine();
     
 }
 
 function display_net_salary(&$rep,$net_salary)
 {

            $rep->font('b');
            $rep->TextCol(1, 2, "Net Salary");
            $rep->TextCol(2 ,3, number_format($net_salary,2));        
            $rep->font('');
            $rep->NewLine();
     
 }
 
 function display_net_earnings(&$rep,$net_earnings)
 {
            $rep->NewLine();
            $rep->font('b');
            $rep->TextCol(0, 1, "Net Earnings");
            $rep->TextCol(2 ,3, number_format($net_earnings,2));        
            $rep->font('');
            $rep->NewLine();
     
 }
 
 function display_total_salary_cost(&$rep,$total_salary)
 {
//            $rep->NewLine();
            $rep->font('b');
            $rep->TextCol(1, 2, "Total Salary Cost");
            $rep->TextCol(2 ,3, number_format($total_salary,2));        
            $rep->font('');
            $rep->NewLine();
     
 }
 
function display_salary_deduction_heading(&$rep)
{
            $rep->NewLine();
            $rep->fontSize += 1;           

            $rep->font('b');
            $rep->TextCol(0, 1, "Salary Deductions" );
            $rep->fontSize -=1;
            $rep->font('');
            $rep->NewLine();
    
}
 
 
 
 function display_department_deduc_sub_totals(&$rep, $dept, $component_names,$grand_totals = false)
 {
      
    switch ($grand_totals) {
        case "STD":
            $total_text_heading = "Other Deductions";
            break;
        case "OTHR":
            $total_text_heading = "Deductions from Salary Cost";
            break;

        default:
           $total_text_heading = "";
           break;
    }
       if($total_text_heading=="")
       {

       }
       else
       {     
            $rep->font('BI');
            $rep->TextCol(0, 1, $total_text_heading);
            $rep->font('');
            $rep->NewLine();
       }

    $i=0;
    $dept_deductions_total = 0;
    $dept_additions_total=0;
//    $dept[$key]=0;
    
    foreach ($dept as $key => $value) {
        if($key == 'OTHER_DEDUCTIOIN_OTHER_DEDUCTIOIN_2'){
           $rep->TextCol($i, $i+1, "Other Deductions");
           $rep->TextCol($i+2, $i+3, number_format($dept[$key],2));
           $dept_deductions_total += $dept[$key];
           $rep->NewLine();
        }
        elseif($key == 'EMP-COVID-19'){
           $rep->TextCol($i, $i+1, "Incentive Deduction");
           $rep->TextCol($i+2, $i+3, number_format($dept[$key],2));
           $dept_deductions_total += $dept[$key];
           $rep->NewLine();
        }
        elseif($key == 'EPF 12%'){
           $rep->TextCol($i, $i+1, "EPF Employer (12%)");
           $rep->TextCol($i+2, $i+3, number_format($dept[$key],2));
           $dept_deductions_total += $dept[$key];
           $rep->NewLine();
        }
        elseif($key == 'ETF 3%'){
           $rep->TextCol($i, $i+1, "ETF Employer (3%)");
           $rep->TextCol($i+2, $i+3, number_format($dept[$key],2));
           $dept_deductions_total += $dept[$key];
           $rep->NewLine();
        }
        elseif($key == 'Water_Electricity'){
           $rep->TextCol($i, $i+1, "Water / Electricity");
           $rep->TextCol($i+2, $i+3, number_format($dept[$key],2));
           $dept_deductions_total += $dept[$key];
           $rep->NewLine();
        }
        else
        {
            $name = '';
            $amount = 0.00;
            if(isset($component_names[$key])){
                $name = $component_names[$key];
                $amount = $dept[$key];
                $rep->TextCol($i, $i+1, $name);
                $rep->TextCol($i+2, $i+3, number_format($amount,2));
                $rep->NewLine();
            }
           
            if(isset($dept[$key])){
                $dept_deductions_total += $dept[$key];
            }
            
        }
    }
    

    
    switch ($grand_totals) {
        case "STD":
            $sub_total_text = "Other Deductions Total";
            break;
        case "OTHR":
            $sub_total_text = "Deductions From Salary Cost Total";
            break;
        


        default:
           $sub_total_text = "";
           break;
    }
       if($sub_total_text=="")
       {

       }
       else
       {
        
            $rep->TextCol(1, 2, $sub_total_text);
            $rep->font('b');
            $rep->TextCol(2 ,3, number_format($dept_deductions_total,2));   
            $rep->font('');
            $rep->NewLine();
       }
   
   return $dept_deductions_total;
   
 }
 
 
function populate_employee_salary_array($payroll_run_id, $employee_code,&$dep_head_count)
{
    if($employee_code == '0430'){
        $rrr=6;
    }
 
    $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;
    }
    
    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 print_dep_salary_expences()
{
    global $path_to_root, $pic_height;
        
    $payroll_run_id = $_POST['PARAM_0'];
    $department = $_POST['PARAM_1'];
    $destination = $_POST['PARAM_2'];
	if ($destination)
		include_once($path_to_root . "/reporting/includes/excel_report.inc");
        else
		include_once($path_to_root . "/reporting/includes/pdf_report.inc");

	$orientation = 'P';

        $cols = array(0, 270,420,500);
        $headers = array('Component','','Amount');
        $aligns = array('left','left','right');
   	$rep = new FrontReport(_('Department Salary Expenses'), "DepartmentSalaryExpenses", 'A4', 9, $orientation);
        
         if($department != "-1")
        {
                  
        $dep= get_departments($department);
        $department_name=$dep['department_name'];
        
        }
        else
        {
           $department_name ="All";
        } 
        
         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";
        } 
        $params =   array( 	0 => $payrol_run_name,
                                1 => $department_name
                                        );
                
                //$rep->formData['doctype'] = "form_950";
        $rep->SetHeaderType('form_975');
        $rep->Font();
        $rep->Info($params, $cols, $headers, $aligns);

//        $rep->NewPage();
        $current_department = "";
        $period_start_date = $period_end_date = date2sql(Today());
        $payroll_data =get_payroll_run_details($payroll_run_id);
            if (isset($payroll_data['id']) && $payroll_data['id'] > 0) {
                $period_start_date = $payroll_data['period_start_date'];
                $period_end_date = $payroll_data['period_end_date'];
            }
        
        $dept = array();
        $emp = array();
        
        $dept_std_deduct = array();
        $emp_std_deduct = array();
        
        $dept_othr_deduct = array();
        $emp_othr_deduct = array();
        
        $dept_stat_deduct = array();
        $emp_stat_deduct = array();
        
        $gtotal = array();
        $gtotal_std_deduct = array();
        $gtotal_othr_deduct = array();
        $gtotal_stat_deduct = array();
                    
        initialize_additions_array($dept);
        initialize_additions_array($emp);
        
        initialize_std_deductions_array($dept_std_deduct);
        initialize_std_deductions_array($emp_std_deduct);
        
        initialize_other_deductions_array($dept_othr_deduct);
        initialize_other_deductions_array($emp_othr_deduct);
        
        initialize_statutory_deductions_array($dept_stat_deduct);
        initialize_statutory_deductions_array($emp_stat_deduct);
        
        initialize_additions_array($gtotal);
        initialize_std_deductions_array($gtotal_std_deduct);
        initialize_other_deductions_array($gtotal_othr_deduct);
        initialize_statutory_deductions_array($gtotal_stat_deduct);

        $sub_total = 0;
        $dept_additions_total = 0;
        $dept_std_deduct_sub_total=0;
        $dept_othr_deduct_sub_total=0;
        $dept_stat_deduct_sub_total=0;
        $dep_total_deduction=0;
        
        $all_dept_std_deduct_gd_total=0;
        $all_dept_othr_deduct_gd_total=0;
        $all_dept_stat_deduct_gd_total=0;
        $all_dept_additions_total=0;
        $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($department, $period_start_date, $period_end_date, $payroll_run_id);
        $grand_head_count=0; 
            while ($dep_salary_row = db_fetch($result_dep_salary))
            {
                $department_code = $dep_salary_row['department_code'];
                if($department_code = 'WATER'){
                    $sss= 99;
                }
                if($dep_salary_row['department_code'] == "NO_DEPARTMENT"){
                    if($NO_DEPARTMENT_new_page){
                        $rep->NewPage();
                        $NO_DEPARTMENT_new_page = false;
                    }
                    $rep->TextCol(0, 1, $dep_salary_row['department_code']." - ".$dep_salary_row['department_name'] );
                    $rep->NewLine();
                    $rep->TextCol(0, 1, $dep_salary_row['employee_code']);
                    $rep->NewLine();
                }

                if(!isset($dep_salary_row['department_code']) || strlen($dep_salary_row['department_code']) == 0){
                    $rep->NewPage(); 
                    $rep->NewLine();
                    $rep->TextCol(0, 1, $dep_salary_row['department_code']." - ".$dep_salary_row['department_name'] );
                    $rep->NewLine();
                }

                if($current_department != $dep_salary_row['department_code'])
                {    
                       
                    if(strlen($current_department)>0){
                        
                        if($current_department == "WATER"){
                            $ww = 99;
                        }
                        $dept_additions_total = display_department_sub_totals($rep, $dept, $com_names,false);
                        display_salary_deduction_heading($rep);
                        $dept_othr_deduct_sub_total = display_department_deduc_sub_totals($rep, $dept_othr_deduct, $com_names,"OTHR");
                        $dept_std_deduct_sub_total = display_department_deduc_sub_totals($rep, $dept_std_deduct, $com_names,"STD");
                        
                        $dep_total_deduction=$dept_std_deduct_sub_total+$dept_othr_deduct_sub_total;
                        display_department_all_deductions_total($rep,$dep_total_deduction);
                        display_net_salary($rep,$dept_additions_total-$dep_total_deduction);
                        display_net_earnings($rep,$dept_additions_total-$dept_othr_deduct_sub_total);
                        
                        ////////
                        $dept_stat_deduct_sub_total = display_department_deduc_sub_totals($rep, $dept_stat_deduct, $com_names,false);
                        display_total_salary_cost($rep,$dept_additions_total-$dept_othr_deduct_sub_total+$dept_stat_deduct_sub_total);
                        display_dept_head_count($rep,$dep_head_count);
                        
                        $dept_additions_total = 0;
                        $dept_std_deduct_sub_total=0;
                        $dept_othr_deduct_sub_total=0;
                        $dept_stat_deduct_sub_total=0;
                        $dep_total_deduction=0;
                        
                        add_to_department_total($gtotal, $dept);
                        add_to_department_total($gtotal_std_deduct, $dept_std_deduct);
                        add_to_department_total($gtotal_othr_deduct, $dept_othr_deduct);
                        add_to_department_total($gtotal_stat_deduct, $dept_stat_deduct);
                    
                        initialize_additions_array($dept);
                        initialize_std_deductions_array($dept_std_deduct);
                        initialize_other_deductions_array($dept_othr_deduct);
                        initialize_statutory_deductions_array($dept_stat_deduct);
                        $grand_head_count +=$dep_head_count;
                        
                    }
                    display_department_sub_heading($rep, $dep_salary_row);
                    $rep->NewLine();
                    
                    $current_department = $dep_salary_row['department_code'];
                    
                    add_to_department_total($dept, $emp);
                    add_to_department_total($dept_std_deduct, $emp_std_deduct);
                    add_to_department_total($dept_othr_deduct, $emp_othr_deduct);
                    add_to_department_total($dept_stat_deduct, $emp_stat_deduct);
                    $dep_head_count=0;
                     
                }

                initialize_additions_array($emp);
                initialize_std_deductions_array($emp_std_deduct);
                initialize_other_deductions_array($emp_othr_deduct);
                initialize_statutory_deductions_array($emp_stat_deduct);
                
        
                $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 = "TRAVELLING_ALLOWANCE,VEHICLE_REIMBURSEMEN,BANKING_INCENTIVE";
                $emp['TRAVELLING_ALLOWANCE_BANKING_INCENTIVE'] += get_employee_salary_component_value($salary, "TRAVELLING_ALLOWANCE") + 
                                                                                       get_employee_salary_component_value($salary, "BANKING_INCENTIVE");

                $component = "FUEL_ALLOWANCES,FUEL_REIMBURSEMENT_T";
                $emp['FUEL_ALLOWANCES_FUEL_REIMBURSEMENT_T'] += get_employee_salary_component_value($salary, "FUEL_ALLOWANCES") + 
                                                                get_employee_salary_component_value($salary, "FUEL_REIMBURSEMENT_T");
                
                $component = "MEAL_REIMBURSEMENT,MEAL_REIMBURSEMENT_T";
                $emp['MEAL_REIMBURSEMENT_MEAL_REIMBURSEMENT_T'] += get_employee_salary_component_value($salary, "MEAL_REIMBURSEMENT") + 
                                                                                       get_employee_salary_component_value($salary, "MEAL_REIMBURSEMENT_T");

                $component = "ATTENDANCE_INCENTIVE";
                $emp['ATTENDANCE_INCENTIVE'] += get_employee_salary_component_value($salary, $component);
                
                $component = "SITE_EXPENSES";
                $emp['SITE_EXPENSES'] += get_employee_salary_component_value($salary, $component);
                
            
                $component = "ACCOMMODATION";
                $emp['ACCOMMODATION'] += get_employee_salary_component_value($salary, $component);
                
                
                $component = "SPECIAL_PAYMENT,SPECIAL_PAYMENT1";
                $emp['SPECIAL_PAYMENT_SPECIAL_PAYMENT1'] += get_employee_salary_component_value($salary, "SPECIAL_PAYMENT") + 
                                                            get_employee_salary_component_value($salary, "SPECIAL_PAYMENT1");
                
                $component = "SINGLE_OT";
                $emp['SINGLE_OT'] += get_employee_salary_component_value($salary, $component);
                
                $component = "DOUBLE_OT";
                $emp['DOUBLE_OT'] += get_employee_salary_component_value($salary, $component);
                
                $component = "DRIVING_ALLOWANCE";
                $emp['DRIVING_ALLOWANCE'] += get_employee_salary_component_value($salary, $component);
                
                $component = "COMMISSION";
                $emp['COMMISSION'] += get_employee_salary_component_value($salary, $component);
                
                $component = "INCENTIVE";
                $emp['INCENTIVE'] += get_employee_salary_component_value($salary, $component);
                
                $component = "BANKING_INCENTIVE_II,VEHICLE_REIMBURSEMEN,BANKING_INCENTIVE_I";
                $emp['BANKING_INCENTIVE_II_VEHICLE_REIMBURSEMEN_BANKING_INCENTIVE_I'] += get_employee_salary_component_value($salary, "BANKING_INCENTIVE_II") + 
                                                                                         get_employee_salary_component_value($salary, "VEHICLE_REIMBURSEMEN") +
                                                                                         get_employee_salary_component_value($salary, "BANKING_INCENTIVE_I");
                
                $component = "INCENTIVE_ARREARS";
                $emp['INCENTIVE_ARREARS'] += get_employee_salary_component_value($salary, $component);
                
                $component = "NO_PAY_REFUND";
                $emp['NO_PAY_REFUND'] += get_employee_salary_component_value($salary, $component);
                
                $component = "SALARY_ARREARS";
                $emp['SALARY_ARREARS'] += get_employee_salary_component_value($salary, $component);
                
                $component = "SAVINGS_COMMISSION";
                $emp['SAVINGS_COMMISSION'] += get_employee_salary_component_value($salary, $component);
                
                $component = "SPECIAL_ATTENDANCE";
                $emp['SPECIAL_ATTENDANCE'] += get_employee_salary_component_value($salary, $component);
                
                $component = "POWDER_COATING_ALL";
                $emp['POWDER_COATING_ALL'] += get_employee_salary_component_value($salary, $component);
                
                
                 $component = "INSURANCE";
                $emp_std_deduct['INSURANCE'] += get_employee_salary_component_value($salary, $component);
                
                $component = "WELFARE";
                $emp_std_deduct['WELFARE'] += get_employee_salary_component_value($salary, $component);
                
                $component = "OTHER_DEDUCTIOIN_OTHER_DEDUCTIOIN_2";
                $emp_std_deduct['OTHER_DEDUCTIOIN_OTHER_DEDUCTIOIN_2'] += get_employee_salary_component_value($salary, "OTHER_DEDUCTIOIN") + 
                                                            get_employee_salary_component_value($salary, "OTHER_DEDUCTIOIN_2");
                
                $component = "EMP-COVID-19";
                $emp_othr_deduct['EMP-COVID-19'] += get_employee_salary_component_value($salary, $component);
                
                $component = "EPF 12%";
                $emp_stat_deduct['EPF 12%'] += get_employee_salary_component_value($salary, $component);
                
                $component = "ETF 3%";
                $emp_stat_deduct['ETF 3%'] += get_employee_salary_component_value($salary, $component);
                
                $component = "PAYE";
                $emp_std_deduct['PAYE'] += get_employee_salary_component_value($salary, $component);
                
                $component = "MOBILE_DEDUCTION_1";
                $emp_std_deduct['MOBILE_DEDUCTION_1'] += get_employee_salary_component_value($salary, $component);
                
                $component = "SALARY_ADVANCES";
                $emp_std_deduct['SALARY_ADVANCES'] += get_employee_salary_component_value($salary, $component);
                
                $component = "DATA_OVER_LIMIT";
                $emp_std_deduct['DATA_OVER_LIMIT'] += get_employee_salary_component_value($salary, $component);
                
                $component = "PANELTY";
                $emp_othr_deduct['PANELTY'] += get_employee_salary_component_value($salary, $component);
                
                 
                $component = "SALARY_ADVANCE";
                $emp_std_deduct['SALARY_ADVANCE'] += get_employee_salary_component_value($salary, $component);
                
                
                $component = "Water_Electricity";
                $emp_std_deduct['Water_Electricity'] += get_employee_salary_component_value($salary, $component);
                
                $component = "WELFARE_REGISTRATION";
                $emp_std_deduct['WELFARE_REGISTRATION'] += get_employee_salary_component_value($salary, $component);
                
                $component = "NO_PAY_DEDUCTIONS";
                $emp_othr_deduct['NO_PAY_DEDUCTIONS'] += get_employee_salary_component_value($salary, $component);
                
                $component = "LOAN_INSTALLMENTS";
                $emp_std_deduct['LOAN_INSTALLMENTS'] += get_employee_salary_component_value($salary, $component);
                
                $component = "EPF_DEDUCTION";
                $emp_std_deduct['EPF_DEDUCTION'] += get_employee_salary_component_value($salary, $component);
                
                $component = "COMP_EPF_8";
                $emp_std_deduct['COMP_EPF_8'] += get_employee_salary_component_value($salary, $component);
                
                
                
                //                $emp_std_deduct['LOAN_DEDUCTIONS'] += get_employee_loan_deduc_result($payroll_run_id,$dep_salary_row['employee_code']);
                
               
                
                add_to_department_total($dept, $emp);
                add_to_department_total($dept_std_deduct, $emp_std_deduct);
                add_to_department_total($dept_othr_deduct, $emp_othr_deduct);
                
                ///////
                add_to_department_total($dept_stat_deduct, $emp_stat_deduct);
//                $head_count +=1;
            }
            if($current_department == 'WATER'){
                $rr= 33;
            }
            $grand_head_count +=$dep_head_count;
                
             if($department != '-1')
            {
                $dept_additions_total = display_department_sub_totals($rep, $dept, $com_names,false);
                display_salary_deduction_heading($rep);
                $dept_othr_deduct_sub_total = display_department_deduc_sub_totals($rep, $dept_othr_deduct, $com_names,"OTHR");
                $dept_std_deduct_sub_total = display_department_deduc_sub_totals($rep, $dept_std_deduct, $com_names,"STD");

                $dep_total_deduction=$dept_std_deduct_sub_total+$dept_othr_deduct_sub_total;
                display_department_all_deductions_total($rep,$dep_total_deduction);
                display_net_salary($rep,$dept_additions_total-$dep_total_deduction);
                display_net_earnings($rep,$dept_additions_total-$dept_othr_deduct_sub_total);
                $dept_stat_deduct_sub_total = display_department_deduc_sub_totals($rep, $dept_stat_deduct, $com_names,false);
                display_total_salary_cost($rep,$dept_additions_total-$dept_othr_deduct_sub_total+$dept_stat_deduct_sub_total);
                display_dept_head_count($rep,$dep_head_count);
            
            }    
            else
            {
                $dept_additions_total = display_department_sub_totals($rep, $dept, $com_names,false);
                display_salary_deduction_heading($rep);
                $dept_othr_deduct_sub_total = display_department_deduc_sub_totals($rep, $dept_othr_deduct, $com_names,"OTHR");
                $dept_std_deduct_sub_total = display_department_deduc_sub_totals($rep, $dept_std_deduct, $com_names,"STD");

                add_to_department_total($gtotal, $dept);
                add_to_department_total($gtotal_std_deduct, $dept_std_deduct);
                add_to_department_total($gtotal_othr_deduct, $dept_othr_deduct);
                $temp_dept_stat_deduct = $dept_stat_deduct;
                add_to_department_total($gtotal_stat_deduct, $dept_stat_deduct);

                $dep_total_deduction=$dept_std_deduct_sub_total+$dept_othr_deduct_sub_total;
                display_department_all_deductions_total($rep,$dep_total_deduction);

                display_net_salary($rep,$dept_additions_total-$dep_total_deduction);

                display_net_earnings($rep,$dept_additions_total-$dept_othr_deduct_sub_total);

                $dept_stat_deduct_sub_total = display_department_deduc_sub_totals($rep, $temp_dept_stat_deduct, $com_names,false);

                display_total_salary_cost($rep,$dept_additions_total-$dept_othr_deduct_sub_total+$dept_stat_deduct_sub_total);

                display_dept_head_count($rep,$dep_head_count);

                $rep->NewLine();
            }
            
            if($department== '-1')
            {
                $all_dept_additions_total = display_department_sub_totals($rep, $gtotal, $com_names,true);
                display_salary_deduction_heading($rep);
                $all_dept_othr_deduct_gd_total += display_department_deduc_sub_totals($rep, $gtotal_othr_deduct, $com_names,"OTHR");
                $all_dept_std_deduct_gd_total += display_department_deduc_sub_totals($rep, $gtotal_std_deduct, $com_names,"STD");


                $all_dept_total_deduction=$all_dept_std_deduct_gd_total+$all_dept_othr_deduct_gd_total;

                display_department_all_deductions_total($rep,$all_dept_total_deduction);

                display_net_salary($rep,$all_dept_additions_total-$all_dept_total_deduction);

                display_net_earnings($rep,$all_dept_additions_total-$all_dept_othr_deduct_gd_total);

                $all_dept_stat_deduct_gd_total += display_department_deduc_sub_totals($rep, $gtotal_stat_deduct, $com_names,false);

                display_total_salary_cost($rep,$all_dept_additions_total-$all_dept_othr_deduct_gd_total+$all_dept_stat_deduct_gd_total);
                display_dept_head_count($rep,$grand_head_count);
            
            }
            
            $rep->SetTextColor(255, 0, 0);
            $rep->TextCol(0, 3, "If a new salary component is added this report should be amended." );
            $rep->SetTextColor(0, 0, 0);

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


?>
