<?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 . "/inventory/includes/inventory_db.inc");
include_once($path_to_root . "/includes/db/manufacturing_db.inc");
include_once($path_to_root . "/payroll/includes/db/employees_db.inc");
include_once($path_to_root . "/payroll/includes/db/payroll_run_db.inc");
include_once($path_to_root . "/includes/db/module_settings_db.inc");

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

print_epf_cform();

function get_gross_salary_total($payroll_run_id, $component)
{
        $sql = "SELECT value gross_salary_total  
                FROM ".TB_PREF."payroll_run_details 
                WHERE payroll_run_id = ".db_escape($payroll_run_id)." AND 
                        employee_code = '' AND 
                        component = ".db_escape($component); 
                         
   	$result = db_query($sql, "Gross salary total could not be retrieved");

        $gross_salary = db_fetch($result);
        if($gross_salary){
            return $gross_salary['gross_salary_total'];
        }
        else{
            return 0;
        }
}

function get_epf_cform_info($employee,$valid_salaries,$released_salaries,$hold_salaries,$payroll_run_id,$rep,$print,&$epf_20_total,&$epf_12_total,&$epf_8_total,&$grand_total)
{
    
    $epf_12_total = $epf_8_total = $epf_20_total =$grand_total= 0;
                
            $employee_data = get_patroll_employees($payroll_run_id, $employee); 
                
               
                while ($employee_details = db_fetch($employee_data))
                {
                    
                    
                    $employer_contri=0;
                    $employee_contri=0;
                    $total_earnings=0;
                    $total_contri=0;
                    $salary_hold_details = get_salary_hold_details($payroll_run_id, $employee_details['employee_code']);
                    if($salary_hold_details){
                        if($salary_hold_details['hold_salary'] == 1){
                            if($hold_salaries == 1){
                                
                            }
                            else{
                                continue;
                            }
                        }
                        else{
                            if($released_salaries == 1){
                                
                            }
                            else{
                                continue;
                            }
                        }
                    }
                    else{
                        if($valid_salaries == 1){
                                
                        }
                        else{
                            continue;
                        }
                    }
                    
                if(strlen(trim($employee_details['employee_code'])) < 1){
                    continue;
                }
                     $res = get_employee_epf_salary_components($payroll_run_id,$employee_details['employee_code']);
                       while($employee_salary = db_fetch($res))
                       {
                           if($employee_salary['component']=='SALARY_FOR_EPF') 
                           {
                               $total_earnings=doubleval($employee_salary['value']);
                            
                           }
                           if($employee_salary['component']=='EMP_EPF12')
                           {
                               $employer_contri=doubleval($employee_salary['value']);
                               
                           }
                           if($employee_salary['component']=='EMP_EPF8')
                           {
                               $employee_contri=abs(doubleval($employee_salary['value']));
                               
                           }
                          
                           $total_contri= $employer_contri+ $employee_contri;
                            
                       }
                       
                        $emp_details=get_emp_info($employee_details['employee_code']);
                        $employee_name = $emp_details['initials']." ".$emp_details['last_name']." ";
                        if($print)
                        {
                            $rep->fontSize -= 1;
                            $rep->TextCol($i=0, $i+=1, $employee_name);
                            $rep->TextCol($i, $i+=1, $emp_details['nic']);
                            $rep->TextCol($i, $i+=1, $emp_details['mobile_no']);
                            $rep->TextCol($i, $i+=1, $emp_details['epf_no']);
                            $rep->TextCol($i, $i+=1, number_format($total_contri,2));
                            $rep->TextCol($i, $i+=1, number_format($employer_contri,2));
                            $rep->TextCol($i, $i+=1, number_format($employee_contri,2));
                            $rep->TextCol($i, $i+=1, number_format($total_earnings,2));
                            $rep->fontSize += 1;
                            $rep->NewLine();   
                        }
                        draw_table_row_lines($rep);
                        
                        $epf_20_total += $total_contri;
                        $epf_12_total += $employer_contri;
                        $epf_8_total += $employee_contri;
                        $grand_total += $total_earnings;
                        
                        

                }
 
                
}

function get_salary_hold_details($payroll_run_id, $employee)
{
    $sql = "SELECT hs.* 
                FROM ".TB_PREF."hold_salary hs
                WHERE hs.employee_code = ".db_escape($employee)." AND
                        hs.payroll_run_id = ".db_escape($payroll_run_id);
        
   	$result = db_query($sql, "hold salary  details could not be retrieved");

        return db_fetch($result);
}

function get_payroll_run_name($payroll_run_id)
{
    $payroll_run = get_payroll_run_details($payroll_run_id);
    if($payroll_run){
        return $payroll_run['description'];
    }
    else{
        return "";
    }     
}

function get_employee_epf_salary_components($payroll_run_id, $employee) {
    $sql = "SELECT * 
            FROM ".TB_PREF."payroll_run_details 
            WHERE payroll_run_id = ".db_escape($payroll_run_id)." 
                AND component IN ('SALARY_FOR_EPF', 'EMP_EPF8', 'EMP_EPF12')";
    
        if ($employee) {
            $sql .= " AND employee_code = ".db_escape($employee);
        }      
        
   	return db_query($sql, "EPF salary components could not be retrieved");
}

function get_emp_info($employee)
{
        $sql = "SELECT * FROM ".TB_PREF."employee_master WHERE  employee_code=".db_escape($employee);
                    
                       
	$result = db_query($sql,"an employee could not be retreived");
   	

        return db_fetch($result);
}

function draw_table_row_lines($rep)
{
    if (! (isset($rep->cols) && count($rep->cols) > 0)) {
        return;
    } 
        
        $v1 = $rep->row + $rep->lineHeight;
        $v2 = $rep->row - 2;
        
        
        $h0 = $rep->cols[0]-5;
        $h1 = $rep->cols[1]-5;
        $h2 = $rep->cols[2]-5;
        $h3 = $rep->cols[3]-5;
        $h4 = $rep->cols[4]+5;
        $h5 = $rep->cols[5]+5;
        $h6 = $rep->cols[6]+5;
        $h7 = $rep->cols[7]+5;
        $h8 = $rep->cols[8]+5;
        
        $rep->LineTo($h0, $v2, $h8, $v2);
        
        $rep->LineTo($h0, $v1, $h0, $v2);
        $rep->LineTo($h1, $v1, $h1, $v2);
        $rep->LineTo($h2, $v1, $h2, $v2);
        $rep->LineTo($h3, $v1, $h3, $v2);
        $rep->LineTo($h4, $v1, $h4, $v2);
        $rep->LineTo($h5, $v1, $h5, $v2);
        $rep->LineTo($h6, $v1, $h6, $v2);
        $rep->LineTo($h7, $v1, $h7, $v2);
        $rep->LineTo($h8, $v1, $h8, $v2);
}


function get_patroll_employees($payroll_run_id, $employee_code) {
    $sql = "SELECT DISTINCT pr.employee_code 
            FROM ".TB_PREF."payroll_run_details pr 
                JOIN ".TB_PREF."employee_master ep
                    ON pr.employee_code = ep.employee_code 
            WHERE pr.payroll_run_id = ".db_escape($payroll_run_id)." 
                AND ep.epf_no > 0 ";
    
        if ($employee_code !='') {
            $sql .= " AND pr.employee_code = ".db_escape($employee_code);
        }
                
	return $result = db_query($sql, "Payroll employees could not be retreived");
}

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

function print_epf_cform()
{
    global $path_to_root, $pic_height;
        
    //return;
        $employee = $_POST['PARAM_0'];
        $valid_salaries = $_POST['PARAM_1'];
        $released_salaries = $_POST['PARAM_2'];
        $hold_salaries = $_POST['PARAM_3'];
        $payroll_run_id = $_POST['PARAM_4'];
        $destination = $_POST['PARAM_5'];

	if ($destination)
		include_once($path_to_root . "/reporting/includes/excel_report.inc");
	else
		include_once($path_to_root . "/reporting/includes/pdf_report.inc");

	//include_once($path_to_root . "/reporting/includes/pdf_report.inc");

	$orientation = 'P';
        $component = get_module_parameter_value("payroll", "epf_reference_salary_total"); //"SALARY_TOTAL_2";

        $cols = array($x=-5, $x+=195, $x+=60, $x+=55, $x+=40, $x+=45, $x+=45, $x+=45, $x+=45);
        $headers = array("Employee's Name",'NIC No', 'Mobile No','Member No','Total','Employer','Employee','Tot. Earn');
        $aligns = array('left','left','left', 'center', 'right', 'right', 'right', 'right');

   	$rep = new FrontReport(_('EPF C-Form'), "EpfCForm", 'A4', 9, $orientation);

        //    $rep->Font();
        //    $rep->Info($params, $cols, $headers, $aligns);
        //    $rep->NewPage();
        $print=true;
        get_epf_cform_info($employee,$valid_salaries,$released_salaries,$hold_salaries,$payroll_run_id,$rep,$print=false,$epf_20_total,$epf_12_total,$epf_8_total,$grand_total);
                $epf_reg_no = get_module_parameter_value("payroll", "epf_reg_no");
                $month = get_payroll_run_name($payroll_run_id);
                //$total_contribution = get_gross_salary_total($payroll_run_id, $component) * 0.2;
                $surcharges = 0;
                $total_remitance = $epf_20_total + $surcharges;
                $cheque_no = ".................................................";
                $epf_payment_bank = ".................................................";
               // $epf_payment_bank = get_module_parameter_value("payroll", "epf_payment_bank");;

                $params =   array('epf_reg_no' => $epf_reg_no,
                                    'month' => $month,
                                    'total_contribution' => number_format($epf_20_total,2),
                                    'surcharges' => number_format($surcharges,2),
                                    'total_remitance' => number_format($total_remitance,2),
                                    'cheque_no' => $cheque_no,
                                    'epf_payment_bank' => $epf_payment_bank);

                //$rep->formData['doctype'] = "form_950";
                $rep->SetHeaderType('form_952');
                $rep->Font();
                $rep->Info($params, $cols, $headers, $aligns);
                $rep->NewPage();
                
                draw_table_row_lines($rep);
                
                $content_start_row = $rep->row;   
                get_epf_cform_info($employee,$valid_salaries,$released_salaries,$hold_salaries,$payroll_run_id,$rep,$print=true,$epf_20_total,$epf_12_total,$epf_8_total,$grand_total);
                
//                $epf_12_total = $epf_8_total = $epf_20_total =$grand_total= 0;
//                
//                $employee_data=get_patroll_employees($payroll_run_id, $employee); 
//                
//               
//                while ($employee_details = db_fetch($employee_data))
//                {
//                    
//                    
//                    $employer_contri=0;
//                    $employee_contri=0;
//                    $total_earnings=0;
//                    $total_contri=0;
//                    $salary_hold_details = get_salary_hold_details($payroll_run_id, $employee_details['employee_code']);
//                    if($salary_hold_details){
//                        if($salary_hold_details['hold_salary'] == 1){
//                            if($hold_salaries == 1){
//                                
//                            }
//                            else{
//                                continue;
//                            }
//                        }
//                        else{
//                            if($released_salaries == 1){
//                                
//                            }
//                            else{
//                                continue;
//                            }
//                        }
//                    }
//                    else{
//                        if($valid_salaries == 1){
//                                
//                        }
//                        else{
//                            continue;
//                        }
//                    }
//                    
//                if(strlen(trim($employee_details['employee_code'])) < 2){
//                    continue;
//                }
//                     $res = get_employee_epf_salary_components($payroll_run_id,$employee_details['employee_code']);
//                       while($employee_salary = db_fetch($res))
//                       {
//                           if($employee_salary['component']=='SALARY_FOR_EPF')
//                           {
//                               $total_earnings=doubleval($employee_salary['value']);
//                            
//                           }
//                           if($employee_salary['component']=='EPF 12%')
//                           {
//                               $employer_contri=doubleval($employee_salary['value']);
//                               
//                           }
//                           if($employee_salary['component']=='COMP_EPF_8')
//                           {
//                               $employee_contri=abs(doubleval($employee_salary['value']));
//                               
//                           }
//                          
//                           $total_contri= $employer_contri+ $employee_contri;
//                            
//                       }
//                       
//                        $emp_details=get_emp_info($employee_details['employee_code']);
//                        $employee_name = $emp_details['initials']." ".$emp_details['last_name']." ";
//                        $rep->TextCol(0, 1, $employee_name);
//                        $rep->TextCol(1, 2, $emp_details['nic']);
//                        $rep->TextCol(2, 3, $emp_details['epf_no']);
//                        $rep->TextCol(3, 4, number_format($total_contri,2));
//                        $rep->TextCol(4, 5, number_format($employer_contri,2));
//                        $rep->TextCol(5, 6, number_format($employee_contri,2));
//                        $rep->TextCol(6, 7, number_format($total_earnings,2));
//                       
//                        $rep->NewLine();   
//                
//                        draw_table_row_lines($rep);
//                        
//                        $epf_20_total += $total_contri;
//                        $epf_12_total += $employer_contri;
//                        $epf_8_total += $employee_contri;
//                        $grand_total += $total_earnings;
//
//                }
                
                $rep->NewLine();
                
                $rep->Font('bold');
                $rep->fontSize -= 1;
                $rep->TextCol(0, 1, "Total");
                $rep->TextCol($y=4, $y+=1, number_format($epf_20_total,2));
                $rep->TextCol($y, $y+=1, number_format($epf_12_total,2));
                $rep->TextCol($y, $y+=1, number_format($epf_8_total,2));
                $rep->TextCol($y, $y+=1, number_format($grand_total,2));
                $rep->fontSize += 1;
                $rep->Font();
                draw_table_row_lines($rep);
                
                //$rep->Line($rep->row-2,1);
                $rep->Font('bold');
                $rep->NewLine(3);
                $rep->Text($rep->leftMargin + 10,"I certify that the information given above is true and correct.");
                $rep->NewLine(3);
                $rep->Text($rep->leftMargin + 10,"--------------------------------------");
                $rep->NewLine();
                $rep->Text($rep->leftMargin + 10,"Signature of the Employer");
                $rep->NewLine(2);
                $rep->fontSize -= 1;
                $rep->Text($rep->leftMargin + 10,"* Please write Employer's EPF Reg No on the reverse of the cheque");
                $rep->fontSize += 1;
                $rep->NewLine();
                $rep->Font();
        
        


	
	$rep->NewLine();
    $rep->End();
}

?>
