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

function get_employee_for_report($doj)
{
	$sql = "SELECT * FROM ".TB_PREF."employee_master WHERE resigned=0 AND date_of_joining >= ".db_escape(date2sql($doj));
	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 get_employee_component_value($employee_code,$component,$today)
{
     $sql = "SELECT sc.amount 
             FROM ".TB_PREF."salary_components sc
             LEFT JOIN ".TB_PREF."list_values lv ON
                 lv.id=sc.salary_component_id
             WHERE sc.employee_code = ".db_escape($employee_code)." AND lv.value_code = ".db_escape($component)." 
             AND sc.end_date >=". db_escape(date2sql($today))." AND sc.effective_date <= ". db_escape(date2sql($today))."
             ORDER BY sc.effective_date DESC LIMIT 1"; 
    
    $result = db_query($sql, "Component Rate could not be retrieved");
    return db_fetch($result);  
}

function print_addition_list()
{
    global $path_to_root, $pic_height;
        
//        $payroll_run_id = $_POST['PARAM_0'];
       
	$doj = $_POST['PARAM_0'];

	include_once($path_to_root . "/reporting/includes/excel_report.inc");
	
	$orientation = 'L';
       

        $cols = array(0, 75,375,600,750,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000,2100,2200,2300);
        $headers = array('Emp No - Employee Name','Department','Designation','NIC','Date of Join','No of Days Worked','Basic Salary','Incentive Salary',
                          'Arrears Amount','Fuel','Fuel Reimbursement T','Vehicle Incentive','Meal','Meal Reimbursement T','Single OT','Double OT','Atte. Allowances','Other Allowances',
                          'Other');
        $aligns = array('left',	'left','left','left','left','left','right','right','right','right','right','right','right','right','right','right','right','right','right');
        
//        $cols2 = array(0, 50,105,500,650,860);
//        $headers2 = array('','','','','');
//        $aligns2 = array('left','left','left','left','left');
        
        
                
        $rep = new FrontReport(_('In Addition List'), "InAdditionList", 'A4', 9, $orientation);

        $params = array( 0 => $doj );
        $rep->SetHeaderType('form_956');
        $rep->Font();
        
        $rep->Info($params, $cols, $headers, $aligns);
        $rep->NewPage();
        

        if($doj == null){
            
        }
        
        else{
            $res = get_employee_for_report($doj);
        }
        $basic_total = 0;
        $insentive_total = 0;
        $fuel_all_total = 0;
        $fuel_total = 0;
        $vehicle_insentive_total = 0;
        $meal_total = 0;
        $meal_t_total = 0;
        $single_ot_total = 0;
        $double_total = 0;
        $attendance_total = 0;
       	while ($employee_doj = db_fetch($res))
	{
            $employeee_details = get_employeee_details($employee_doj['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 );
                        $today = date('Y-m-d');
                        $date_of_joining = $employee_doj['date_of_joining'];
                        $dep_name='';
                        $em_designation='';
                        $emp_departments=get_employee_curent_departments($employee_doj['employee_code'], $today);
                        $emp_designations=get_employee_current_designation($employee_doj['employee_code'],$today);
                        
                        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'];
                            }
                        }
                            
                        $rep->TextCol(1, 2, $dep_name);
                        $rep->TextCol(2, 3, $em_designation);
                        $rep->TextCol(3, 4, $employee_doj['nic']);
                        
                        $rep->TextCol(4, 5, sql2date($date_of_joining));
                        $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{
                         
                            $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(5, 6,$years.' Y '. $months.' M '.$days .' D');

                        $basic_salary = get_employee_component_value($employee_doj['employee_code'],'BASIC_SALARY',Today());
                        $insentive_salary = get_employee_component_value($employee_doj['employee_code'],'INCENTIVE',Today());
                        $fuel_allowance = get_employee_component_value($employee_doj['employee_code'],'FUEL_ALLOWANCES',Today());
                        $fuel_t = get_employee_component_value($employee_doj['employee_code'],'FUEL_REIMBURSEMENT_T',Today());
                        $vehicle_insentive = get_employee_component_value($employee_doj['employee_code'],'TRAVELLING_ALLOWANCE',Today());
                        $meal = get_employee_component_value($employee_doj['employee_code'],'MEAL_REIMBURSEMENT',Today());
                        $meal_t = get_employee_component_value($employee_doj['employee_code'],'MEAL_REIMBURSEMENT_T',Today());
                        $single_ot = get_employee_component_value($employee_doj['employee_code'],'SINGLE_OT',Today());
                        $double_ot = get_employee_component_value($employee_doj['employee_code'],'DOUBLE_OT',Today());
                        $attendance = get_employee_component_value($employee_doj['employee_code'],'ATTENDANCE_INCENTIVE',Today());
                        
                        $basic_salary_amount = $insentive_salary_amount = $fuel_allowance_amount = $fuel_t_amount = $vehicle_insentive_amount = $meal_amount = 0;
                        $meal_t_amount = $single_ot_amount = $double_ot_amount = $attendance_amount = 0;
                        
                        if (isset($basic_salary['amount'])) {
                            $basic_salary_amount = $basic_salary['amount'];
                            $basic_total += $basic_salary['amount'];
                        }
                        if (isset($insentive_salary['amount'])) {
                            $insentive_salary_amount = $insentive_salary['amount'];
                            $insentive_total += $insentive_salary['amount'];
                        }
                        if (isset($fuel_allowance['amount'])) {
                            $fuel_allowance_amount = $fuel_allowance['amount'];
                            $fuel_all_total += $fuel_allowance['amount'];
                        }
                        if (isset($fuel_t['amount'])) {
                            $fuel_t_amount = $fuel_t['amount'];
                            $fuel_total += $fuel_t['amount'];
                        }
                        if (isset($vehicle_insentive['amount'])) {
                            $vehicle_insentive_amount = $vehicle_insentive['amount'];
                            $vehicle_insentive_total += $vehicle_insentive['amount'];
                        }
                        if (isset($meal['amount'])) {
                            $meal_amount = $meal['amount'];
                            $meal_total += $meal['amount'];
                        }
                        if (isset($meal_t['amount'])) {
                            $meal_t_amount = $meal_t['amount'];
                            $meal_t_total += $meal_t['amount'];
                        }
                        if (isset($single_ot['amount'])) {
                            $single_ot_amount = $single_ot['amount'];
                            $single_ot_total += $single_ot['amount'];
                        }
                        if (isset($double_ot['amount'])) {
                            $double_ot_amount = $double_ot['amount'];
                            $double_total += $double_ot['amount'];
                        }
                        if (isset($attendance['amount'])) {
                            $attendance_amount = $attendance['amount'];
                            $attendance_total += $attendance['amount'];
                        }
                        
                        $rep->TextCol(6, 7, number_format($basic_salary_amount,2));
                        $rep->TextCol(7, 8, number_format($insentive_salary_amount,2));
                        $rep->TextCol(8, 9, '0.00');
                        $rep->TextCol(9, 10, number_format($fuel_allowance_amount,2) );
                        $rep->TextCol(10, 11, number_format($fuel_t_amount,2) );
                        $rep->TextCol(11, 12, number_format($vehicle_insentive_amount,2) );
                        $rep->TextCol(12, 13, number_format($meal_amount,2) );
                        $rep->TextCol(13, 14, number_format($meal_t_amount,2) );
                        $rep->TextCol(14, 15, number_format($single_ot_amount,2) );
                        $rep->TextCol(15, 16, number_format($double_ot_amount,2) );
                        $rep->TextCol(16, 17, number_format($attendance_amount,2) );
                        $rep->TextCol(17, 18, '0.00');
                        $rep->TextCol(18, 19, '0.00' );
                         
                }
                        $rep->NewLine();  
                        $rep->TextCol(2, 5, "Total");
                        $rep->TextCol(6, 7, number_format($basic_total,2));
                        $rep->TextCol(7, 8, number_format($insentive_total,2));
                        $rep->TextCol(8, 9, '0.00');
                        $rep->TextCol(9, 10, number_format($fuel_all_total,2) );
                        $rep->TextCol(10, 11, number_format($fuel_total,2) );
                        $rep->TextCol(11, 12, number_format($vehicle_insentive_total,2) );
                        $rep->TextCol(12, 13, number_format($meal_total,2) );
                        $rep->TextCol(13, 14, number_format($meal_t_total,2) );
                        $rep->TextCol(14, 15, number_format($single_ot_total,2) );
                        $rep->TextCol(15, 16, number_format($double_total,2) );
                        $rep->TextCol(16, 17, number_format($attendance_total,2) );
                        $rep->TextCol(17, 18, '0.00');
                        $rep->TextCol(18, 19, '0.00' );
                        
                
                 $rep->End();       
	}
        



