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

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

print_late_report();



function get_employee_curent_departments($employee)
{
        $sql = "SELECT  ed.*, 
                    IFNULL(d.value_name,'No Department Entered') department_name                    
                FROM ".TB_PREF."employee_departments ed 
                    INNER JOIN ".TB_PREF."employee_master em ON 
                            ed.employee_code = em.employee_code 
                    INNER JOIN ".TB_PREF."list_values d ON 
                            ed.department_id = d.id                     
                    WHERE ed.start_date <= ".db_escape(date("Y-m-d"))." AND 
                    ed.end_date >= ".db_escape(date("Y-m-d"));
         
         if($employee != "")
         {
             $sql .= " AND ed.employee_code =". db_escape($employee);
             
         }

        $sql .= " ORDER BY ed.start_date DESC LIMIT 1";
        $result=db_query($sql,"an employee could not be retreived");
        return db_fetch($result);
    
    
}

function get_emp_late_report($employee,$from_date,$to_date)
{
	 $sql = "SELECT  a.entry_date,
                    a.from_date_time1,em.*, 
                    st.shift_name,
                    st.start_time1
            FROM ".TB_PREF."attendance a
                    INNER JOIN ".TB_PREF."employee_master em ON 
                            a.employee_code = em.employee_code 
                    INNER JOIN ".TB_PREF."shifts st ON 
                            st.id = a.shift_id 
            WHERE a.entry_date >=". db_escape($from_date)." AND a.entry_date <= ". db_escape($to_date);                   
         
         if($employee != "")
         {
             $sql .= " AND a.employee_code =". db_escape($employee);
             
         }
     
            $sql .= " ORDER BY em.employee_code";
                
        
        return $result=db_query($sql,"an employee attendance details could not be retreived");
//        return db_fetch($result);
}

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 calculatetime($time)
        {
            $h = floor($time /3600);
            $m = floor(($time - $h *3600) / 60);
            
            if(strlen($h) == 1){
                $h = '0'.$h;
            }
            if(strlen($m) == 1){
                $m = '0'.$m;
            }
            
            return $h.":".$m;        
            
        }


function print_late_report()
{
    global $path_to_root, $pic_height;
        
        $employee = $_POST['PARAM_0'];
        $from_date= $_POST['PARAM_1'];
        $to_date = $_POST['PARAM_2'];
        $destination = $_POST['PARAM_3'];
        
	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, 200,350,470,560,640,700,770);
        $headers = array('Emp Code - Employee','Department','Shift','Date','Shift Start','Time In','Late Time Duration');
        $aligns = array('left',	'left','left','left','left','left','right');
        $rep = new FrontReport(_('Late Report'), "Late Report", 'A4', 9, $orientation);
        
        if($employee != "")
        {
            
            $employee_info= get_employeee_details($employee);
            $name = "";
            if($employee_detials=db_fetch($employee_info))
             {
            $name = "[".$employee_detials['employee_code']."] - ".$employee_detials['initials'].
                            " ".$employee_detials['last_name']." (".$employee_detials["first_name"].") ";
             }
             $employeeName = $name;
            
        }
        else
        {
            $employeeName = "All";
            
        }
        
        
                
        $params =   array( 	0 => $employeeName,
                                1 => $from_date,
                                2 => $to_date
                        
                    );
        $rep->SetHeaderType('form_970');
        $rep->Font();
        $rep->Info($params, $cols, $headers, $aligns);
        $rep->NewPage();
                      
                     

        $employee_detials = get_employeee_details($employee);  
        
        while($emp_data=db_fetch($employee_detials))
        {
        
        $result_late_info = get_emp_late_report($emp_data['employee_code'],date2sql($from_date),date2sql($to_date)); 
             
         
             while ($emp_late = db_fetch($result_late_info))
            {
                    
                              
                        $name = "";
                        $name = "[".$emp_late['employee_code']."] - ".$emp_late['initials'].
                            " ".$emp_late['last_name']." (".$emp_late["first_name"].") ";
                                   
                        $current_employee = $name; 
                        $emp_department=get_employee_curent_departments($emp_late['employee_code']);                        
                        $entry_time = date("H:i:s",strtotime($emp_late['from_date_time1']));
                        $shift_start_time=$emp_late['start_time1'];
                        
                        if($entry_time > $shift_start_time)
                        {
                            $rep->TextCol(0, 1, $current_employee );  
                            $rep->TextCol(1, 2, isset($emp_department['department_name']) ? $emp_department['department_name'] : '');
                            $rep->TextCol(2, 3, $emp_late['shift_name']);
                            $rep->TextCol(3, 4, sql2date($emp_late['entry_date']));
                            $rep->TextCol(4, 5, $emp_late['start_time1']);                        
                            $rep->TextCol(5, 6, $entry_time);

                            $late_duration_seconds= time_diff_seconds(sql2date($emp_late['entry_date']),$entry_time,sql2date($emp_late['entry_date']),$shift_start_time);
                            $late_time=calculatetime(abs($late_duration_seconds));

                            $rep->TextCol(6, 7, $late_time);
                            $rep->NewLine();   
                        }
                        
                        
            }
       
        }
$rep->End();  
}




