<?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 . "/payroll/includes/db/employees_db.inc");
include_once($path_to_root . "/payroll/includes/db/employee_departments_db.inc");

print_emp_detials();

function get_employeee_details($employee)
{
        $sql = "SELECT em.* 
                FROM ".TB_PREF."employee_master em "
               ."WHERE em.resigned=0 ";
          
          if($employee != "")
          {
               $sql .= " AND em.employee_code = ".db_escape($employee);
          }
            
      
   	return $result = db_query($sql, "Employee details could not be retrieved");

        
}

function get_employees_for_report($from_date,$to_date)
{
	$sql =  "SELECT em.*,
                        lv.value_name em_type 
                 FROM ".TB_PREF."employee_master em ,"
                        .TB_PREF."list_values lv 
                 WHERE em.employee_type_id=lv.id AND em.resigned=1 
                       AND em.date_of_resignation >= ".db_escape(date2sql($from_date))." AND 
                       em.date_of_resignation <= ".db_escape(date2sql($to_date));
               
              
               
                
        $sql .=" order by em.employee_code";
        
    
        return db_query($sql,"an employee could not be retreived");

}

function get_employee_current_designation($employee,$to_date)
{
    
        $sql = "SELECT IFNULL(edl.designation,'No Designation Entered') designation
            FROM ".TB_PREF."employment_details edl,
                 ".TB_PREF."employee_master em                
            WHERE em.employee_code = edl.employee_code AND 
                    edl.contract_start_date <= ".db_escape($to_date)." AND 
                    edl.contract_end_date >= ".db_escape($to_date)." AND 
                    em.employee_code =". db_escape($employee)."
                    ORDER BY edl.contract_start_date DESC LIMIT 1";
                
        
        $result=db_query($sql,"an employee could not be retreived");
        return db_fetch($result);
    
}

function get_employee_job_category($employee,$to_date)
{
    
        $sql = "SELECT IFNULL(lv.value_name,'No Job Category Entered') job_category
            FROM ".TB_PREF."employment_details edl,
                 ".TB_PREF."employee_master em,  
                 ".TB_PREF."list_values lv   
            WHERE em.employee_code = edl.employee_code AND 
                    edl.contract_start_date <= ".db_escape($to_date)." AND 
                    edl.contract_end_date >= ".db_escape($to_date)." AND 
                    em.employee_code =". db_escape($employee)." AND lv.id = edl.job_category_id 
                    ORDER BY edl.contract_start_date DESC LIMIT 1";
                
        
        $result=db_query($sql,"an job category could not be retreived");
        return db_fetch($result);
    
}

function print_emp_detials()
{
    global $path_to_root, $pic_height;
        
        $from_date = $_POST['PARAM_0'];
        $to_date= $_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 = 'L';
       

        $cols = array(0,50,220,350,500,600,700);
        $headers = array('Emp No', 'Employee Name','Department','Designation','Employee Type','Contract Start Date','Contract End Date');
        $aligns = array('left','left','left','left','left','left','left');
        $rep = new FrontReport(_('Leavers'), "Leavers", 'A4', 9, $orientation);
        
         $params =   array( 	0 => $from_date,
                                1 => $to_date
                                
                    );
        
        $rep->SetHeaderType('form_978');
        $rep->Font();
        $rep->Info($params, $cols, $headers, $aligns);
        $rep->NewPage();
                      
         $res = get_employees_for_report($from_date,$to_date);
    
       	while ($employee_data = db_fetch($res))
	{
//            $employeee_details = get_employeee_details($employee_data['employee_code']);
                $name = "";
                if($employee_data){
                    $name = $employee_data['initials'].
                            " ".$employee_data['last_name']." (".$employee_data["first_name"].") ";
                                   }
                $current_employee = $name;

                $department_data=get_employee_curent_departments($employee_data['employee_code'],date2sql($to_date)); // link with the list values
                $designation_data=get_employee_current_designation($employee_data['employee_code'],date2sql($to_date));
                $job_cat_data=get_employee_job_category($employee_data['employee_code'],date2sql($to_date));
                                
//                        $rep->NewLine(); 
                        $rep->TextCol(0, 1, $employee_data['employee_code'] );
                        $rep->TextCol(1, 2, $current_employee );
                        
                        $date_of_joining = $employee_data['date_of_joining'];
                        $date_resigned = $employee_data['date_of_resignation'];
                        $dep="No Department Entered";
                        $desi="No Desigation Entered";
                        if($department_data['department_name'] !='')
                        {
                            $rep->TextCol(2, 3,$department_data['department_name'],-2);
                        }
                        else
                        {
                           $rep->TextCol(2, 3,$dep); 
                        }
                        if($designation_data['designation'] !='')
                        {
                        $rep->TextCol(3, 4,$designation_data['designation'],-2);
                        }
                        else
                        {
                           $rep->TextCol(3, 4,$desi);  
                        }
                        $rep->TextCol(4, 5, $employee_data['em_type']);
//                        $rep->TextCol(5, 6, $job_cat_data['job_category']);
                        
                        $emp_contract_data = get_employement_record($employee_data['employee_code']);
                        
                        $rep->TextCol(5, 6, sql2date($emp_contract_data['contract_start_date']));
                        $rep->TextCol(6, 7, sql2date($emp_contract_data['contract_end_date']));
//                        $rep->TextCol(6,7, sql2date($date_of_joining));
                        
//                        if($employee_data['date_of_resignation']=='3000-01-01')
//                        {
//                        $rep->TextCol(7, 8,'Not Resigned');
//                        }
//                        else
//                        {
//                        $rep->TextCol(7, 8,sql2date($date_resigned));   
//                        }
                       // $rep->TextCol(6, 7,$employee_data['work_email_id']); 
//                        $rep->TextCol(4, 9,$employee_data['epf_no']); 
//                        $rep->TextCol(9, 10,$employee_data['nic']); 
//                        $rep->TextCol(10, 11,sql2date($employee_data['date_of_birth']));
                        
//                        $employee_age=cal_age($employee_data['date_of_birth']);
                                
//                        $rep->TextCol(11, 12,$employee_age);
//                        $gender='';
//                        if($employee_data['gender']=='M')
//                        {
//                            $gender='Male';
//                        }
//                        if($employee_data['gender']=='F')
//                        {
//                             $gender='Female';
//                        }
                        
//                        $rep->TextCol(12, 13,$gender); 
//                        
//                        $marital_status='';
//                        if($employee_data['marital_status']=='M')
//                        {
//                            $marital_status='Married';
//                        }
//                        if($employee_data['marital_status']=='S')
//                        {
//                             $marital_status='Single';
//                        }
//                        
//                        $rep->TextCol(13, 14,$marital_status); 
//                        $rep->TextCol(14, 15,$employee_data['address']);
//                        $rep->TextCol(15, 16,$employee_data['work_telephone_no']);
//                        $rep->TextCol(16, 17,$employee_data['office_mobile_no']); 
                        
                        $rep->NewLine();
                        
//                        $rep->NewLine();
                         
                             

        
       
        }
$rep->End();  
}




