<?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_leave_inquiry();

function get_leave_for_report($from,$to,$employee,$leave_type,$no_pays,$status_code)
{
	 $sql = "SELECT 
                e.employee_code,
                li.value_name leave_type_name, 
                l.from_date,
                ftc.value_name from_time_name,
                l.to_date,
                ttc.value_name to_time_name,
                l.leave_days,
                l.is_revoke,
                l.comment,
                ls.value_name leave_status_name, 
                l.nopay_days,
                l.payroll_run_id  from "
                 .TB_PREF."leaves l ,"
                 .TB_PREF."employee_master e ,"
                 .TB_PREF."leave_entitlements le ,"
                 .TB_PREF."list_values li ,"
                 .TB_PREF."list_values ls ,"
                 .TB_PREF."list_values ftc,"
                 .TB_PREF."list_values ttc "
                 ."Where l.from_date >= ".db_escape($from). " AND l.to_date <= ".db_escape($to);
         if($employee != "")
         {
           $sql .= " AND e.employee_code = ".db_escape($employee);
         }
        if($leave_type != "-1")
         {
          $sql .= " AND li.id= ".db_escape($leave_type);            
         }  
         if($status_code != "-1")
         {
          $sql .= " AND l.status_code = ". db_escape($status_code);            
         }
         if($no_pays)
         {
          $sql .= " AND l.nopay_days >0 " ;   
             
         }  
         
        $sql .= " AND l.employee_code=e.employee_code AND
                 l.leave_entitlement_id=le.id AND
                 le.leave_type_id=li.id AND 
                 l.status_code = ls.value_code AND
                 ls.list_code = 'ATTENDANCE_STATUS' AND 
                 l.from_time_code = ftc.value_code AND
                 ftc.list_code = 'WORKING_TIME' AND 
                 l.to_time_code = ttc.value_code AND ttc.list_code = 'WORKING_TIME' order by  e.employee_code , l.from_date "; 
        return  db_query($sql,"an employee could not be retreived");
}



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 print_leave_inquiry()
{
    global $path_to_root, $pic_height;
        
        $from = $_POST['PARAM_0'];
	$to = $_POST['PARAM_1'];
        $employee = $_POST['PARAM_2'];
        $leave_type = $_POST['PARAM_3'];
        $status_code = $_POST['PARAM_4'];
        $no_pays = $_POST['PARAM_5'];
        $destination = $_POST['PARAM_6'];
        
	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, 175, 275,330,420,480,550,575,620,680,720,770);
        $headers = array('Emp No - Employee','Leave Type','From Date','From Session','To Date','To Session','Days','Revoke','Status','NoPay Days','Comment');
        $aligns = array('left',	'left','left','left','left','left','center','center','left','left','center');
        $rep = new FrontReport(_('Leave Inquiry'), "LeaveInquiry", 'A4', 9, $orientation);
        
        if($employee != "")
        {
            
            $empData = get_employeee_details($employee);
            $name = "";
            if($empData){
                    $name = "[".$empData['employee_code']."] - ".$empData['initials'].
                            " ".$empData['last_name']." (".$empData["first_name"].") ";
                                   }
                $employee_name = $name;
            
        }
        else
        {
            $employee_name = "All";
            
        }
        
        if($leave_type != "-1")
        {
            $sql = "SELECT  
                li.value_name leave_type_name
                FROM 
                ".TB_PREF."leaves l ,"
                 .TB_PREF."leave_entitlements le ,"
                 .TB_PREF."list_values li WHERE l.leave_entitlement_id=le.id AND "
                    . "le.leave_type_id=li.id AND li.id=".db_escape($leave_type);
            
            $result = db_query($sql, "Leave Types could not be retrieved");

        $lea=db_fetch($result);
        $leave_type_name=$lea['leave_type_name'];
        }
        else
        {
           $leave_type_name ="All";
        }
        
       if($status_code != "-1")
        {
            $sql = "SELECT  
                ls.value_name StatusCodeName
                FROM 
                ".TB_PREF."leaves l ,"
                 .TB_PREF."list_values ls Where l.status_code = ls.value_code AND ls.list_code = 'ATTENDANCE_STATUS' AND l.status_code=".db_escape($status_code);
            
        $result = db_query($sql, "Leave Types could not be retrieved");

        $StC=db_fetch($result);
        $st_code=$StC['StatusCodeName'];
        
        }
        else
        {
           $st_code ="All";
        } 
        if($no_pays)
        {
            $nop="Yes";
            
        }
        else
        {
             $nop="No";
            
        }
        
        $params =   array( 	0 => $from,
                                1 => $to,
                                2 => $employee_name,
                                3 => $leave_type_name,
                                4 => $st_code,
                                5 => $nop
                    );
        $rep->SetHeaderType('form_958');
        $rep->Font();
        $rep->Info($params, $cols, $headers, $aligns);
        $rep->NewPage();
        $result_Leave = get_leave_for_report(date2sql($from), date2sql($to),$employee,$leave_type,$no_pays,$status_code);
         while ($employee_leaves = db_fetch($result_Leave))
	{
            
            $employeee_details = get_employeee_details($employee_leaves['employee_code']);
                $name = "";
                if($employeee_details){
                    $name = "[".$employeee_details['employee_code']."] - ".$employeee_details['initials'].
                            " ".$employeee_details['last_name']." (".$employeee_details["first_name"].") ";
                                   }
                $current_employee = $name;

                
                                
                        $rep->NewLine(); 
            //                        $rep->Font("italic");
                         if($employee_leaves['is_revoke']==0) 
                         {
                             $revoke= "No";
                         }
                         else
                         {
                             $revoke= "Yes";
                         }
                        
                        $rep->TextCol(0, 1, $current_employee );
                        $rep->TextCol(1, 2, $employee_leaves['leave_type_name']);
                        $rep->TextCol(2, 3, $employee_leaves['from_date']);
                        $rep->TextCol(3, 4, $employee_leaves['from_time_name']);
                        $rep->TextCol(4, 5, $employee_leaves['to_date']);
                        $rep->TextCol(5, 6, $employee_leaves['to_time_name']);
                        $rep->TextCol(6, 7, $employee_leaves['leave_days']);
                        $rep->TextCol(7, 8, $revoke);
                        $rep->TextCol(8, 9, $employee_leaves['leave_status_name']);
                        $rep->TextCol(9, 10, $employee_leaves['nopay_days']);
                        $rep->TextCol(10, 11, $employee_leaves['comment']);
                        $rep->NewLine();
                       
                         
                }
            
       
                 $rep->End();       
	}
        



