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

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

print_completed_employee();

function get_employee_for_report($doj)
{
	$sql = "SELECT * FROM ".TB_PREF."employee_master WHERE date_of_joining >= ".db_escape(date2sql($doj))." AND resigned=0 
                ORDER BY date_of_joining,employee_code ";
	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 display_month_sub_heading($rep, $curr_month,$curr_year)
{
    $month_name='';
        switch ($curr_month)
        {
          case '1':  
           $month_name='January';
           break;
          case '2':
           $month_name='February';  
           break;
          case '3':  
           $month_name='March';
           break;
          case '4':
           $month_name='April';  
           break;
          case '5':  
           $month_name='May';
           break;
          case '6':
           $month_name='June';  
           break;
          case '7':  
           $month_name='July';
           break;
          case '8':
           $month_name='August';  
           break;
          case '9':  
           $month_name='September';
           break;
          case '10':
           $month_name='October';  
           break;
          case '11':  
           $month_name='November';
           break;
          case '12':
           $month_name='December';  
           break;
          
          Default:
           $month_name='All'; 
        }
    $rep->NewLine(); 
    $rep->Font('bold'); 
    $rep->TextCol(0, 1, $month_name." - ".$curr_year );
    $rep->Font(''); 
    $rep->NewLine(); 
}


function print_completed_employee()
{
    global $path_to_root, $pic_height;
        
//        $payroll_run_id = $_POST['PARAM_0'];
        $doj = $_POST['PARAM_0'];
	$months = $_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, 250, 400,600,700,770);
        $headers = array('Emp No - Employee Name','Department','Designation','Date of Join','Due Date');
        $aligns = array('left',	'left','left','left','left');
        
        $cols2 = array(0, 50,105,500,650,860);
        $headers2 = array('','','','','');
        $aligns2 = array('left','left','left','left','left');
        
        $rep = new FrontReport(_('Employees Months Completed '), "EmployeesMonthsCompleted", 'A4', 9, $orientation);

        if(IS_NUMERIC($months)){
            
            $params =   array( 	0 => $doj,
                                1 => $months);
                     
        $rep->SetHeaderType('form_957');
        $rep->Font();
        $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
        $rep->NewPage();
        
         $current_month = "";
        $res = get_employee_for_report($doj);
        while ($employee_Completed = db_fetch($res))
	{
             $employeee_details = get_employeee_details($employee_Completed['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;

                        $date_of_joining = $employee_Completed['date_of_joining'];
                        $duedate = date('Y-m-d', strtotime("+$months months", strtotime($date_of_joining)));
                        $curr_month=date("m",strtotime($duedate));
                        $curr_year=date("Y",strtotime($duedate));
                        
                        $emp=get_employee_curent_departments($employee_Completed['employee_code'], date('Y-m-d'))  ;    
                        $dep_name='';
                        $em_designation='';
                        $emp_departments=get_employee_curent_departments($employee_Completed['employee_code'], date('Y-m-d'));
                        $emp_designations=get_employee_current_designation($employee_Completed['employee_code'], date('Y-m-d'));
                       
                        if (isset($emp_departments['id']) && $emp_departments['id'] > 0) {
                            if($emp_departments['department_name']=='')
                            {
                                $dep_name='No Department Entered';
                            }
                            else
                            {
                               $dep_name= $emp_departments['department_name'];
                            }
                        }
                        
                        if (isset($emp_designations['designation'])) {
                            if($emp_designations['designation']=='')
                            {
                                $em_designation='No Designation Entered';
                            }
                            else
                            {
                                 $em_designation= $emp_designations['designation'];
                            }    
                        }
        
            if($current_month != $curr_month)
                {
                
                  $rep->NewLine();   
                  display_month_sub_heading($rep, $curr_month,$curr_year);
                  $current_month = $curr_month;
               
                }
                   $rep->NewLine();           
                   $rep->TextCol(0, 1, $current_employee );    
                   $rep->TextCol(1, 2, $dep_name );
                   $rep->TextCol(2, 3, $em_designation );
                   $rep->TextCol(3, 4, sql2date($date_of_joining));
                   $rep->TextCol(4, 5, sql2date($duedate));
                   //$rep->NewLine();  
            }
           

                         
                }
        
        
       
    
       	
                 $rep->End();       
	}
        



