<?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 . "/includes/db/manufacturing_db.inc");

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

print_payslips();

function get_payroll_employees($payroll_run_id, $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($employee) > 0){
            $sql .= " AND prd.employee_code = ".db_escape($employee);
        }
	$sql .= " ORDER BY prd.employee_code";
	return db_query($sql, "Could not retreive payroll employess");
    //pr.status = 'R' AND --- to where clause
}

function get_payslip_datails($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('COMPANY_ADDITION_SALARY_COMP', 
                                    'COMPANY_DEDUCTION_SALARY_COMP', 
                                    'GROUP_ADDITION_SALARY_COMP', 
                                    'GROUP_DEDUCTION_SALARY_COMP',
                                    'EMPLOYEE_ADDITION_SALARY_COMP',
                                    'EMPLOYEE_DEDUCTION_SALARY_COMP',
                                    'EMPLOYEE_ALLOWANCES_OVER_USAGE',
                                    'ATTENDANCE_ALLOWANCES',
                                    'WORKTIME_DEFICITS',
                                    'PERFORMANCE_RATE_INCENTIVES',
                                    'PERFORMANCE_VALUE_INCENTIVES',
                                    'MOBILE_ALLOWANCES_OVER_USAGE',
                                    'SALARY_ADDITIONS',
                                    'SALARY_ADVANCES',
                                    'SALARY_DEDUCTIONS',
                                    'SALARY_GROUP_TOTALS',
                                    'SALARY_TOTALS',
                                    'COMMON_PAYROLL_PROCESSES',
                                    'SALARY_ROUNDING_OFF') 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_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)
{
        $sql = "SELECT em.* 
                FROM ".TB_PREF."employee_master em 
                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 SetPosition(&$rep, &$current_column)
{

    // Check to see if we're at the bottom and should insert a page break
    if ($rep->lineHeight > $rep->row - $rep->bottomMargin)
    {
        if($current_column == 3){
            $current_column = 0;
            $rep->NewPage();
        } 
        else{
            $current_column = $current_column + 3;
            $rep->row = $rep->pageHeight - $rep->topMargin*3.5;
        }
    }    

}


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

function print_payslips()
{
    global $path_to_root, $pic_height;
    $current_column = 0;    
        $payroll_run_id = $_POST['PARAM_0'];
	$filtering_employee = $_POST['PARAM_1'];

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

	$orientation = 'L';

        $cols = array(3, 180, 220, 260, 430, 500);
        $headers = array('Component','Amount','','Component', 'Amount');
        $aligns = array('left',	'right', 'left', 'left', 'right');

   	$rep = new FrontReport(_('Salary Slip'), "SalarySlip", 'A5', 9, $orientation);

        //    $rep->Font();
        //    $rep->Info($params, $cols, $headers, $aligns);
        //    $rep->NewPage();

	$res = get_payroll_employees($payroll_run_id, $filtering_employee);
          
        $rep->Font();
        $rep->Info(null, $cols, $headers, $aligns);
        $rep->NewPage();
	while ($payroll_employee = db_fetch($res))
	{
                $employeee_details = get_employeee_details($payroll_employee['employee_code']);
                $name = "";
                $epf_no = "";
                $nic = "";
                if($employeee_details){
                    $name = "[".$employeee_details['employee_code']."] - ".$employeee_details['first_name']." ".$employeee_details['last_name'];
                    $epf_no = $employeee_details['epf_no'];
                    $nic = $employeee_details['nic'];
                }
                
//                $payroll_run_details = get_payroll_run_details($payroll_run_id);
//                $period = "";
//                if($employeee_details){
//                    $period = $payroll_run_details['period_start_date']." - ".$payroll_run_details['period_end_date'];
//                }
                
                
//                $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;
                
                SetPosition($rep, $current_column);
                $rep->NewLine(); 
   
                $rep->Font('bold');
                $rep->TextCol($current_column, $current_column+1, "Name"." - ".$name);
                $rep->Font();
                
                SetPosition($rep, $current_column); 
                $rep->NewLine(); 
                
                SetPosition($rep, $current_column);
                $rep->NewLine(); 
                $rep->TextCol($current_column, $current_column+1, "Salary Details");
                SetPosition($rep, $current_column);
                $rep->NewLine(); 
                $rep->TextCol($current_column, $current_column+1, "-------------------");
                        
                $payslip_result = get_payslip_datails($payroll_run_id, $payroll_employee['employee_code']);
                
                while ($payslip = db_fetch($payslip_result))
                {


                        SetPosition($rep, $current_column);
                        $rep->NewLine(); 
//                        if($payslip['list_code'] == 'SALARY_TOTALS'){
//                            $rep->TextCol(1, 2, "--------------");
//                            SetPosition($rep, $current_column);
//                        }
                        $rep->TextCol($current_column, $current_column+1, $payslip['value_name']);
                        $rep->TextCol($current_column+1, $current_column+2, $payslip['value']);
//                        if($payslip['list_code'] == 'SALARY_TOTALS'){
//                            SetPosition($rep, $current_column);
//                            $rep->TextCol(1, 2, "--------------");
//                        }
                        
                }
//                $rep->TextCol(1, 2, "============");
                
//                $rep->row = $content_start_row;
                SetPosition($rep, $current_column);
                $rep->NewLine(); 
//                $rep->TextCol($current_column, $current_column+1, "-----------------------------------------------------------------------------");
                SetPosition($rep, $current_column);
                $rep->NewLine(); 
                
                $rep->TextCol($current_column, $current_column+1, "Employer Contributions");
                SetPosition($rep, $current_column);
                $rep->NewLine(); 
                $rep->TextCol($current_column, $current_column+1, "--------------------------------");
                $res2 = get_employer_contribution($payroll_run_id, $filtering_employee);
                $count = 0;
                while ($contribution = db_fetch($res2))
                {
                    SetPosition($rep, $current_column);
                    $rep->NewLine(); 
                    $rep->TextCol($current_column, $current_column+1, $contribution['value_name']);
                    $rep->TextCol($current_column+1, $current_column+2, $contribution['value']);
                    $count = 1;
                }
                if($count == 0){
                    SetPosition($rep, $current_column);
                    $rep->NewLine(); 
                    $rep->TextCol($current_column, 1, "No Employer Contributions");
                }
                SetPosition($rep, $current_column);
                $rep->NewLine(); 
        
	}
        


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

?>
