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

function get_birthdays_for_report($month)
{
	$sql =  "SELECT em.*,"
                ."IFNULL(dp.value_name,'No Department Entered') department_name "
                ."FROM "
                .TB_PREF."employee_master em, "
                .TB_PREF."employee_departments ed ,"
                .TB_PREF."list_values dp WHERE "
                ." em.resigned=0 "
                ." AND em.employee_code = ed.employee_code  AND 
                   ed.start_date <= ".db_escape(date("Y-m-d"))." AND ed.end_date >=".db_escape(date("Y-m-d"))." 
                  AND ed.department_id=dp.id AND em.resigned = 0 ";
        if($month !='0')
        {
                $sql .=" AND MONTH(em.date_of_birth)= ".db_escape($month);
        }
        
        $sql .= " order by MONTH(em.date_of_birth),DAY(em.date_of_birth)";
	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_birthdays()
{
    global $path_to_root, $pic_height;
        
//        $payroll_run_id = $_POST['PARAM_0'];
       
	$month=$_POST['PARAM_0'];
        $destination = $_POST['PARAM_1'];
      
        
	if ($destination)
		include_once($path_to_root . "/reporting/includes/excel_report.inc");
	else
		include_once($path_to_root . "/reporting/includes/pdf_report.inc");
	
	$orientation = 'P';
       

        $cols = array(0,240,375,470,575);
        $headers = array('Emp No - Employee Name','Department','Service Period','Date of Birth');
        $aligns = array('left','left','left','left');
        $rep = new FrontReport(_('Birth Days List'), "BirthDaysList", 'A4', 9, $orientation);
        
     $month_name='';
        switch ($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'; 
        }
        
        
        $params =   array( 0 => $month_name );
        
        
        
       
        
        $rep->SetHeaderType('form_967');
        $rep->Font();
        $rep->Info($params, $cols, $headers, $aligns);
        $rep->NewPage();
        
//        $rep->row - 4;
        $res = get_birthdays_for_report($month);
        //$rep->fontSize -= 2;
        //==============
       	while ($employee_birth_data = db_fetch($res))
	{
            $employeee_details = get_employeee_details($employee_birth_data['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->TextCol(0, 1, $current_employee );
                        
                        $date_of_birth = $employee_birth_data['date_of_birth'];
                        $date_of_joining = $employee_birth_data['date_of_joining'];
                        $today = date('Y-m-d');
                        
                        
                        $years =date_diff2(sql2date($today), sql2date($date_of_joining), 'y');
                        
                        $effective_year = date('Y-m-d', strtotime("+$years years", strtotime($date_of_joining)));
                        $months =date_diff2(sql2date($today), sql2date($effective_year), 'm');
                        if(0>$months)
                        {
                            $years=$years-1; 
                            $effective_year = date('Y-m-d', strtotime("+$years months", strtotime($date_of_joining)));
                            $months = date_diff2(sql2date($today), sql2date($effective_year), 'm');
                            $effective_date = date('Y-m-d', strtotime("+$months months", strtotime($effective_year)));
                        }
                        else{
                         //$months =date_diff2(sql2date($today), sql2date($date_of_joining), 'm');
                         $effective_date = date('Y-m-d', strtotime("+$months months", strtotime($effective_year)));
                        }
                         $days = date_diff2(sql2date($today), sql2date($effective_date), 'd');
                         
                         if(0>$days)
                         {
                                                    
                            $months=$months-1; 
                            $effective_date = date('Y-m-d', strtotime("+$months months", strtotime($effective_year)));
                            $days = abs(date_diff2(sql2date($effective_date),sql2date($today), 'd'));
                           
                         }
                        
                        $rep->TextCol(1, 2,$employee_birth_data['department_name']);
                        $rep->TextCol(2, 3,$years.' Y '. $months.' M '.$days .' D');
                        $rep->TextCol(3, 4, sql2date($date_of_birth));
                        
                        $rep->NewLine();
//                        $rep->NewLine();
                         
                }
                 $rep->End();       
	}
        



