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

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

print_payslips();

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_payslip_datails($payroll_run_id, $employee, $in_string)
{
        $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(".PAYROLL_LISTS.") AND 
                        prd.payroll_run_id = ".db_escape($payroll_run_id)." AND 
                        prd.employee_code = ".  db_escape($employee)." AND 
                        pcs.print_ref IN (".$in_string." )
                ORDER BY pcs.print_seq";
    
   	$result = db_query($sql, "Payslip details could not be retrieved");

        return $result;
}


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 get_salary_hold_details($payroll_run_id, $employee)
{
    $sql = "SELECT hs.* 
                FROM ".TB_PREF."hold_salary hs
                WHERE hs.employee_code = ".db_escape($employee)." AND
                        hs.payroll_run_id = ".db_escape($payroll_run_id);
        
   	$result = db_query($sql, "hold salary  details could not be retrieved");

        return db_fetch($result);
}

function print_payslip_value($rep, $payslip_value_name, $component_value, &$total)
{
    $rep->NewLine();
    $rep->TextCol(0, 1, $payslip_value_name);
    $value = number_format($component_value,2);
    $rep->TextCol(1, 2, $value);
    $total += $component_value;
}

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

function print_payslips()
{
    global $path_to_root, $pic_height;
        
    $payroll_run_id = $_POST['PARAM_0'];
    $from_employee = $_POST['PARAM_1'];
    $to_employee = $_POST['PARAM_2'];
    $valid_salaries = $_POST['PARAM_3'];
    $released_salaries = $_POST['PARAM_4'];
    $hold_salaries = $_POST['PARAM_5'];

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

	$orientation = 'P';

        $cols = array(0, 195, 250, 320, 340, 345);
        $headers = array('Component','Amount','','Component', 'Amount');
        $aligns = array('left',	'right', 'left', 'left', 'right');
   	$rep = new FrontReport(_('Salary Slip'), "SalarySlip", 'SALARY_SLIP', 9, $orientation);

        
        //    $rep->Font();
        //    $rep->Info($params, $cols, $headers, $aligns);
        //    $rep->NewPage();
    
                    
    $period = $end_date = date2sql(Today());
    $payroll_run_details = get_payroll_run_details($payroll_run_id);
        if (isset($payroll_run_details['id']) && $payroll_run_details['id'] > 0) {
            $period = date('F Y', strtotime($payroll_run_details['period_start_date']));
            $end_date = $payroll_run_details['period_end_date'];
        }
    
    
	$res = get_payroll_employees($payroll_run_id, $from_employee, $to_employee);    
	while ($payroll_employee = db_fetch($res))
	{
            
            
            print_employee_payslips($payroll_run_id,$payroll_employee,$hold_salaries,$valid_salaries,$released_salaries,$period,$end_date,$rep,$cols,$headers,$aligns);
//            if($h==0){
//                $h=5;
//            }
//            else{
//                $h=0;
//            }
        

           
	}
        


	//$rep->Line($rep->row - 4);
	$rep->NewLine();
    $rep->End();
}

function print_employee_payslips($payroll_run_id,$payroll_employee,$hold_salaries,$valid_salaries,$released_salaries,$period,$end_date,$rep,$cols,$headers, $aligns)
{
    $employeee_details = get_employeee_details($payroll_employee['employee_code'],$end_date);
                $name = "";
                $epf_no = "";
                $emp_no ="";
                $department="";
                $bank_account_no = 0;
                if($employeee_details){
                    $name = $employeee_details['initials']." ".$employeee_details['last_name']." (".$employeee_details["first_name"].")";
                    $epf_no = $employeee_details['epf_no'];
                    $emp_no = $employeee_details['employee_code'];
                    $department=$employeee_details['department_name'];
                    $bank_account_no = $employeee_details['bank_account_no'];
                    
                    $salary_hold_details = get_salary_hold_details($payroll_run_id, $employeee_details['employee_code']);
                    if($salary_hold_details){
                        if($salary_hold_details['hold_salary'] == 1){
                            if($hold_salaries == 1){
                                
                            }
                            else{
                                return;
                            }
                        }
                        else{
                            if($released_salaries == 1){
                                
                            }
                            else{
                                return;
                            }
                        }
                    }
                    else{
                        if($valid_salaries == 1){
                                
                        }
                        else{
                            return;
                        }
                    }
                }
                

                $params =   array( 	0 => $name,
                                        1 => $epf_no,
                                        2 => $period);
                
                //$rep->formData['doctype'] = "form_950";
                $rep->SetHeaderType('form_950');
                $rep->Font();
                $rep->Info($params, $cols, $headers, $aligns);
                
                $rep->NewPage();
                    
               
              //  $content_start_row = $rep->row;
                $rep->row = 500;
               //                $rep->LineTo(305, 825, 305, 0);
               
//                $rep->SetLineStyle(array("dash" =>"5,25"));
//             $rep->LineTo(305, 825, 305, 0);
                //$report=FrontReport(_('Salary Slip'), "SalarySlip", 'SALARY_SLIP', 9, $orientation);
               //$rep->Text($this->topMargin,$this->bottomMargin);
               $rep->NewLine();
               
                $rep->Font('bold');
                $rep->Text(8,$rep->company['coy_name']);
                $rep->NewLine();                               
                $rep->Text(8,"Employee No:");
                $rep->Text(73,$emp_no);
                $rep->NewLine();
                $rep->Text(8,"EPF No:");
                $rep->Text(73,$epf_no);
                $rep->NewLine();
                $rep->Text(8,"Name:");
                $rep->Text(73,$name);
                $rep->NewLine();
                $rep->Text(8,"Department:");
                $rep->Text(73,$department);
                $rep->NewLine();
                $rep->Text(8, $period);
                $rep->Font('');
                $rep->NewLine(2);
                
                $total = 0;
                $payslip_result = get_payslip_datails($payroll_run_id, $payroll_employee['employee_code'], "'BE','CE'");
                $rep->TextCol(0, 1, "EARNINGS - Basic");
                $rep->NewLine();
                $rep->TextCol(0, 2, "===================================================");
                
                while ($payslip = db_fetch($payslip_result))
                {
                        print_payslip_value($rep, $payslip['value_name'], $payslip['value'], $total);
                }     
                $rep->NewLine();
                $rep->TextCol(0, 1, "TOTAL EARNINGS");
                $rep->TextCol(1, 2, number_format($total,2));
                $rep->NewLine(2);
                
                $total = 0;
                $payslip_result = get_payslip_datails($payroll_run_id, $payroll_employee['employee_code'], "'BD','CD'");
                $rep->TextCol(0, 1, "DEDUCTIONS - Basic");
                $rep->NewLine();
                $rep->TextCol(0, 2, "===================================================");
//                $rep->NewLine();
                
                $INCENTIVE = get_calculated_payroll_db_value('INCENTIVE', $payroll_employee['employee_code'], $payroll_run_id);
                $SALARY_TOTAL_1 = get_calculated_payroll_db_value('SALARY_TOTAL_1', $payroll_employee['employee_code'], $payroll_run_id);
                    
                while ($payslip = db_fetch($payslip_result))
                {
                        $component_value = $payslip['value'];
                        if($payslip['component'] == 'NO_PAY_DEDUCTIONS' && $SALARY_TOTAL_1 != 0){
                            $component_value = $component_value * (($SALARY_TOTAL_1 - $INCENTIVE)/$SALARY_TOTAL_1);
                        }
                        print_payslip_value($rep, $payslip['value_name'], $component_value, $total);
                }
                                            
                $rep->NewLine();
                $rep->TextCol(0, 1, "TOTAL DEDUCTIONS");
                $rep->TextCol(1, 2, number_format($total,2));
                $rep->NewLine();

                $payslip_result = get_payslip_datails($payroll_run_id, $payroll_employee['employee_code'], "'BASIC_FINAL'");

                while ($payslip = db_fetch($payslip_result))
                {
                        $rep->Font('bold');
                        print_payslip_value($rep, $payslip['value_name'], $payslip['value'], $total);
                        $rep->Font('');
                }
                $rep->NewLine();
                
                $payslip_result = get_payslip_datails($payroll_run_id, $payroll_employee['employee_code'], "'SALARY_FOR_EPF'");

                while ($payslip = db_fetch($payslip_result))
                {
                        print_payslip_value($rep, $payslip['value_name'], $payslip['value'], $total);
                }
                
                $payslip_result = get_payslip_datails($payroll_run_id, $payroll_employee['employee_code'], "'EPF12'");

                while ($payslip = db_fetch($payslip_result))
                {
                        print_payslip_value($rep, $payslip['value_name'], $payslip['value'], $total);
                }
                
                $payslip_result = get_payslip_datails($payroll_run_id, $payroll_employee['employee_code'], "'ETF3'");

                while ($payslip = db_fetch($payslip_result))
                {
                        print_payslip_value($rep, $payslip['value_name'], $payslip['value'], $total);
                }
                
                $rep->NewLine(2);
                $rep->TextCol(0, 2, "Remitted to A/C No ".$bank_account_no);
                $rep->NewLine(2);
                $rep->TextCol(0, 1, "Signature : ........................................");
               
                //$rep->NewLine(2);
                
                
                $rep->NewPage();  
                
                
                //$rep->row = $content_start_row;
                $rep->row=500;
                
                $rep->NewLine();

                $rep->fontSize += 1;
                $rep->Font('BI');
                $rep->TextCol(0, 1, $name);
                $rep->NewLine();
                $rep->TextCol(0, 1, $period);
                $rep->NewLine(2);
                //$rep->Font('');
                $rep->Font('');
                
                $rep->Font('italic');   
                $total = 0;
                $payslip_result = get_payslip_datails($payroll_run_id, $payroll_employee['employee_code'], "'IE','CE'");
                $rep->TextCol(0, 1, "Incentives");
                $rep->NewLine();
                $rep->TextCol(0, 2, "---------------------------------------------------------------------------");
                while ($payslip = db_fetch($payslip_result))
                {
                        print_payslip_value($rep, $payslip['value_name'], $payslip['value'], $total);
                }     
                $rep->NewLine();
                $rep->TextCol(0, 1, "TOTAL");
                $rep->TextCol(1, 2, number_format($total,2));
                $rep->NewLine(2);
                
                $total = 0;
                $payslip_result = get_payslip_datails($payroll_run_id, $payroll_employee['employee_code'], "'ID','CD'");
                $rep->TextCol(0, 1, "DEDUCTIONS");
                $rep->NewLine();
                $rep->TextCol(0, 2, "--------------------------------------------------------------------------");
                while ($payslip = db_fetch($payslip_result))
                {
                        $component_value = $payslip['value'];
                        if($payslip['component'] == 'NO_PAY_DEDUCTIONS' && $SALARY_TOTAL_1 != 0){
                            $component_value = $component_value * ($INCENTIVE / $SALARY_TOTAL_1);
                        }
                        print_payslip_value($rep, $payslip['value_name'], $component_value, $total);
                }
                $rep->NewLine();
                $rep->TextCol(0, 1, "TOTAL DEDUCTIONS");
                $rep->TextCol(1, 2, number_format($total,2));
                
                $rep->NewLine();
                $brought_fwd= get_calculated_payroll_db_value("BROUGHT_FORWARD", $payroll_employee['employee_code'], $payroll_run_id);
                $brought_total=get_listvalue_by_code('SALARY_ROUNDING_OFF', "BROUGHT_FORWARD");
               
                if($brought_fwd)
                {
                        $rep->Font('I'); 
                        print_payslip_value($rep, $brought_total['value_name'], $brought_fwd, $total);
                        
                }
                $rep->NewLine();
                
                $payslip_result = get_payslip_datails($payroll_run_id, $payroll_employee['employee_code'], "'INCENTIVE_FINAL'");
                $rep->Font('');   
                while ($payslip = db_fetch($payslip_result))
                {
                        $rep->Font('BI');   
                        print_payslip_value($rep, $payslip['value_name'], $payslip['value'], $total);
                     
                }
                              
                $incentive = get_calculated_payroll_db_value("CARRIED_FORWARD", $payroll_employee['employee_code'], $payroll_run_id);
                $take_home_total=get_listvalue_by_code('SALARY_ROUNDING_OFF', "CARRIED_FORWARD");
                $rep->NewLine();
                if($incentive)
                {
                        $rep->Font('I'); 
                        print_payslip_value($rep, $take_home_total['value_name'], $incentive, $total);
                        
                }
                
                $rep->NewLine(2);
                $rep->TextCol(0, 1, "Signature : ........................................");
                
                $rep->Font('');
                $rep->fontSize -= 1;
}
?>
