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

print_salary_issue_notes_instruction_report();

function get_employee_salary_component_amount($payroll_run_id, $employee, $component)
{
        $sql = "SELECT * 
                FROM ".TB_PREF."payroll_run_details 
                WHERE payroll_run_id = ".db_escape($payroll_run_id)." AND 
                         employee_code = ".  db_escape($employee)." AND 
                         component = ".db_escape($component); 
                         
   	$result = db_query($sql, "Salary component amount could not be retrieved");

        return db_fetch($result);
}

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_employees($payroll_run_id, $from_employee, $to_employee,$component)
{
	$sql = " SELECT DISTINCT prd.employee_code,prd.value
                 FROM ".TB_PREF."payroll_run_details prd, 
                        ".TB_PREF."payroll_runs pr    
                 WHERE prd.payroll_run_id = pr.id AND 
                        prd.employee_code <> '' AND 
                        pr.id = ".db_escape($payroll_run_id)." AND prd.component = ".db_escape($component); 
        
        if(strlen($from_employee) > 0){
            $sql .= " AND prd.employee_code >= ".db_escape($from_employee);
        }
        
        if(strlen($to_employee) > 0){
            $sql .= " AND prd.employee_code <= ".db_escape($to_employee);
        }
//         if($is_insentive_salaries ==0){
//            $sql .= " AND em.bank_basic_salary = ".db_escape($is_insentive_salaries);
//        }
//         if($is_insentive_salaries ==1){
//            $sql .= " AND em.bank_incentive_salary = ".db_escape($is_insentive_salaries);
//        }
        
	$sql .= " ORDER BY prd.employee_code";
    
    
    
	return db_query($sql, "Could not retreive payroll employess");
}
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_payroll_run_information($payroll_run){
    
    	$sql = "SELECT * 
                FROM ".TB_PREF."payroll_runs  
                WHERE id = ". db_escape($payroll_run);
        
   	return $result = db_query($sql, "Payroll run details could not be retrieved");
    
    
}

function get_salary_component_information($salary_componant){
    
    	$sql = "SELECT * 
                FROM ".TB_PREF."list_values ls 
                WHERE ls.id = ". db_escape($salary_componant);
        
   	return $result = db_query($sql, "Salary components could not be retrieved");
    
    
}

function get_salary_account_employees($payroll_start_date,$from_employee,$to_employee)
{
     
    
   $sql = " SELECT em.*
                   
            FROM ".TB_PREF."employee_master em WHERE 1 ";
              
        if(strlen($from_employee) > 0){
            $sql .= " AND em.employee_code >= ".db_escape($from_employee);
        }
        if(strlen($to_employee) > 0){
            $sql .= " AND em.employee_code <= ".db_escape($to_employee);
        }

        $sql .= " AND em.date_of_resignation >=".db_escape($payroll_start_date)." ORDER BY  em.employee_code ";
        
        $log_file_name = "rep971";
    write_to_log_file($log_file_name, $sql);
    
	return db_query($sql, "Could not retreive salary account employess");
}
//----------------------------------------------------------------------------------------------------

function print_salary_issue_notes_instruction_report()
{
    global $path_to_root, $pic_height;
        
        $payroll_run_id = $_POST['PARAM_0'];
        $from_employee = $_POST['PARAM_1'];
        $to_employee = $_POST['PARAM_2'];
        $salary_basic = $_POST['PARAM_3'];
        $salary_insentive = $_POST['PARAM_4'];
        $valid_salaries = $_POST['PARAM_5'];
        $released_salaries = $_POST['PARAM_6'];
        $hold_salaries = $_POST['PARAM_7'];
	$destination = $_POST['PARAM_8'];
        
    $log_file_name = "rep971";
            
	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,20,200, 250, 300, 330, 360,390,440,450,490,520,610);
        $headers = array('#','Employee', 'Salary','5000','1000','500','100','50','20','10','Balance');
        $aligns = array('left','Right', 'right', 'center', 'center', 'center', 'center','center','center', 'center','right');

   	$rep = new FrontReport(_('Salary Packing '), "SalaryPacking", 'A4', 9, $orientation);

        $payroll_start_date = $payroll_end_date = date2sql(Today());
        $payroll_run_details = get_payroll_run_details($payroll_run_id);
            if (isset($payroll_run_details['id']) && $payroll_run_details['id'] > 0) {
                $payroll_start_date = $payroll_run_details['period_start_date'];
                $payroll_end_date = $payroll_run_details['period_end_date'];
            }
       
            if($from_employee != "")
            {
                $employee_detials = get_employeee_details($from_employee);          

                $emp_from_name = "[".$employee_detials['employee_code']."] - ".$employee_detials['initials'].
                                " ".$employee_detials['last_name']." (".$employee_detials["first_name"].") ";

            }
            else
            {
                $emp_from_name = "All";

            }
             if($to_employee != "")
            {

                $employee_detials = get_employeee_details($to_employee);          

                $emp_to_name = "[".$employee_detials['employee_code']."] - ".$employee_detials['initials'].
                                " ".$employee_detials['last_name']." (".$employee_detials["first_name"].") ";
            }
            else {
                $emp_to_name = "All";

            }
           if($payroll_run_id != "-1"){

                $result= get_payroll_run_information($payroll_run_id);
                $StC=db_fetch($result);
                $payrol_run_name=sql2date($StC['period_start_date'])." - ".sql2date($StC['period_end_date'])." - ".$StC['description'];
            }
            else{
               $payrol_run_name ="None";
            } 
            if($salary_basic != "") {

                $result_basic= get_salary_component_information($salary_basic);
                $stc=db_fetch($result_basic);
                $salary_component_name=$stc['value_name'];
            }
            else{
               $salary_component_name ="None";
            }  
             if($salary_insentive != "") {

                $result_insentive= get_salary_component_information($salary_insentive);
                $sts=db_fetch($result_insentive);
                $salary_component_insen=$sts['value_name'];
            }
            else{
               $salary_component_insen ="None";
            }  
            if($valid_salaries !=0){
                $valid='Yes';
            }
            else {
                $valid='No';
            }
            if($released_salaries !=0){
                $released='Yes';
            }
            else {
                $released='No';
            }
            if($hold_salaries !=0){
                $hold='Yes';
            }
            else {
                $hold='No';
            }
            
            
                
        $params =   array( 	0 => $payrol_run_name,
                                1 => $emp_from_name,
                                2 => $emp_to_name,
                                3 => $salary_component_name,
                                4 => $salary_component_insen,
                                5 => $valid,
                                6 => $released,
                                7 => $hold
                    );

        //$rep->formData['doctype'] = "form_950";
        $rep->SetHeaderType('form_971');
        $rep->Font();
        $rep->Info($params, $cols, $headers, $aligns);
        $rep->NewPage();  
        
//        $department_id = -1;
        
        $salary_total_basic = get_list_value($salary_basic);
        $salary_total_insentive = get_list_value($salary_insentive);
        
        $component_basic = $component_insentive = '';
        if (isset($salary_total_basic['id']) && $salary_total_basic['id'] > 0) {
            $component_basic = $salary_total_basic['value_code'];
        }
        if (isset($salary_total_insentive['id']) && $salary_total_insentive['id'] > 0) {
            $component_insentive = $salary_total_insentive['value_code'];
        }
        
//        $payroll_run_details = get_payroll_run_details($payroll_run_id);
//        $payroll_start_date = $payroll_run_details['period_start_date'];
        
        $res = get_salary_account_employees($payroll_start_date,$from_employee,$to_employee);
        write_to_log_file($log_file_name, "get_salary_account_employees");
        $final_salary= 0;
        $final_salary_basic = 0;
        $total_salaries = 0;
        $final_salary_inse = 0;
         $seq_no = 1;
        $total_fiveth_notes =0;                
        $total_oneth_notes =0;
        $total_fivhun_notes =0;
        $total_onehun_notes =0;
        $total_remain_salary =0;
        $total_fifty_notes = 0;
        $total_twenty_notes = 0;
        $total_ten_notes = 0;
        while ($employee = db_fetch($res))
        {
             write_to_log_file($log_file_name, "1");
            if($employee['bank_basic_salary'] == 1 && $employee['bank_incentive_salary'] == 0 )
            {
                $salary = get_employee_salary_component_amount($payroll_run_id, $employee['employee_code'], $component_insentive);
                $final_salary = $salary['value'];
            }
            write_to_log_file($log_file_name, "2");
            
            if($employee['bank_incentive_salary'] == 1 && $employee['bank_basic_salary'] == 0 )
            {
                $salary= get_employee_salary_component_amount($payroll_run_id, $employee['employee_code'], $component_basic);
                $final_salary = $salary['value'];
            }
            write_to_log_file($log_file_name, "3");
            
            if($employee['bank_incentive_salary'] == 1 && $employee['bank_basic_salary'] == 1 )
            {
                continue;
            }
            write_to_log_file($log_file_name, "4");
            if($employee['bank_basic_salary'] == 0 && $employee['bank_incentive_salary'] == 0){
                
                $salary_basic = get_employee_salary_component_amount($payroll_run_id, $employee['employee_code'], $component_basic);
                $final_salary_basic = $salary_basic['value'];
                
                $salary_insentive = get_employee_salary_component_amount($payroll_run_id, $employee['employee_code'], $component_insentive);
                $final_salary_inse = $salary_insentive['value'];
                
                $final_salary = $final_salary_basic + $final_salary_inse;
            }
            
            write_to_log_file($log_file_name, "before get_salary_hold_details");
            
                $salary_hold_details = get_salary_hold_details($payroll_run_id, $employee['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;
                        }
                    }
                    
                write_to_log_file($log_file_name, "before final_salary > 0");
                
               if($final_salary > 0)   
               {
                $rep->NewLine();
                $rep->TextCol(0, 1, $seq_no);
                
                $name=$employee['employee_code']." - ".$employee['initials']." ".$employee['last_name']." (".$employee["first_name"].")";
                $rep->TextCol(1, 2, $name);              
                $rep->TextCol(2, 3, number_format($final_salary,2));
                
                
                $fiveth_notes=0;               
                $oneth_notes=0;
                $fivhun_notes=0;
                $onehun_notes=0;
                $fifty_notes=0;
                $twenty_notes=0;
                $ten_notes=0;
                $remain_salary=0;
                

                
                $amount_fiveth_notes=$final_salary/5000;                
                $fiveth_notes=floor($amount_fiveth_notes);
                
                $remain_salary=$final_salary-$fiveth_notes*5000;
                
                if($remain_salary >= 1000)
                {
                    $amount_oneth_notes=$remain_salary/1000;
                    $oneth_notes=floor($amount_oneth_notes);
                    $remain_salary=$remain_salary-$oneth_notes*1000;
                }
                if($remain_salary >= 500)
                {
                    $amount_fivhun_notes=$remain_salary/500;
                    $fivhun_notes=floor($amount_fivhun_notes);
                    $remain_salary=$remain_salary-$fivhun_notes*500;
                }
               if($remain_salary >= 100)
                {
                    $amount_onehun_notes=$remain_salary/100;
                    $onehun_notes=floor($amount_onehun_notes);
                    $remain_salary=$remain_salary-$onehun_notes*100;
                }
                 if($remain_salary >= 50)
                {
                    $amount_fifty_notes=$remain_salary/50;
                    $fifty_notes=floor($amount_fifty_notes);
                    $remain_salary=$remain_salary-$fifty_notes*50;
                }
                if($remain_salary >= 20)
                {
                    $amount_twenty_notes=$remain_salary/20;
                    $twenty_notes=floor($amount_twenty_notes);
                    $remain_salary=$remain_salary-$twenty_notes*20;
                }
                 if($remain_salary >= 10)
                {
                    $amount_ten_notes=$remain_salary/10;
                    $ten_notes=floor($amount_ten_notes);
                    $remain_salary=$remain_salary-$ten_notes*10;
                }
                if($remain_salary < 10)
                {
                    $remain_salary=$remain_salary;
                    
                }
                
                $total_fiveth_notes +=$fiveth_notes;               
                $total_oneth_notes +=$oneth_notes;
                $total_fivhun_notes +=$fivhun_notes;
                $total_onehun_notes +=$onehun_notes;
                $total_fifty_notes +=$fifty_notes;
                $total_twenty_notes +=$twenty_notes;
                $total_ten_notes +=$ten_notes;
                $total_remain_salary +=$remain_salary;
                $total_salaries += $final_salary;

                 
                $rep->TextCol(3, 4, $fiveth_notes);               
                $rep->TextCol(4, 5, $oneth_notes);
                $rep->TextCol(5, 6, $fivhun_notes);
                $rep->TextCol(6, 7, $onehun_notes);
                $rep->TextCol(7, 8, $fifty_notes);
                $rep->TextCol(8, 9, $twenty_notes);
                $rep->TextCol(9, 10, $ten_notes);
                $rep->TextCol(10, 11, number_format($remain_salary,2));
                
                

                $seq_no++;
               }

        }
                $rep->NewLine();
                $rep->Font('b');
                $rep->TextCol(1, 2, 'Total');
                $rep->TextCol(2, 3, number_format($total_salaries,2));   
                $rep->TextCol(3, 4, $total_fiveth_notes);               
                $rep->TextCol(4, 5, $total_oneth_notes);
                $rep->TextCol(5, 6, $total_fivhun_notes);
                $rep->TextCol(6, 7, $total_onehun_notes);
                $rep->TextCol(7, 8, $total_fifty_notes);
                $rep->TextCol(8, 9, $total_twenty_notes);
                $rep->TextCol(9, 10, $total_ten_notes);
                $rep->TextCol(10, 11, number_format($total_remain_salary,2));
                $rep->Font();
        

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

                
}

?>
