<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PROCESS_ASSET_DEPRICIATION';
$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/ui.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/assets/includes/db/approve_asset_run_calculation_db.inc");
//below file should have been included by the system already. But it is not. So included manually. Once the issues found remove this inclusion
include_once($path_to_root . "/assets/includes/approve_asset_run_calculation.inc");
include_once($path_to_root . "/assets/includes/db/asset_depreciation_run_db.inc");

$js = "";
if ($SysPrefs->use_popup_windows){
	$js .= get_js_open_window(900, 500);
    }
    
page(_($help_context = "Process Asset Depreciation Calculation"), false, false, "", $js);
simple_page_mode(true);
//--------------------------------------------------------------------------------------------------
//function get_calculated_salary_additions($process_id, $employee_code, $payroll_run_id, $status_code){
//    
//    $s = "";
//    $result = select_calculated_salary_additions($process_id, $employee_code, $payroll_run_id, $status_code);
//    while ($myrow = db_fetch($result))
//    {
//        $s .= sql2date($myrow["due_date"])." - "
//                .$myrow["salary_addition"]."<br>";
//    }
//    return $s;
//}
//function get_calculated_salary_deductions($process_id, $employee_code, $payroll_run_id, $status_code){
//    
//    $s = "";
//    $result = select_calculated_salary_deductions($process_id, $employee_code, $payroll_run_id, $status_code);
//    while ($myrow = db_fetch($result))
//    {
//        $s .= sql2date($myrow["due_date"])." - "
//                .$myrow["salary_deduction"]."<br>";
//    }
//    return $s;
//}
//function get_calculated_nopay_deductions($employee_code, $payroll_run_id, $status_code){
//    
//    $s = "";
//    $result = select_calculated_nopay_deductions($employee_code, $payroll_run_id, $status_code);
//    while ($myrow = db_fetch($result))
//    {
//        $s .= sql2date($myrow["from_date"])." - ".sql2date($myrow["to_date"])." - "
//                .$myrow["nopay_days"]." - "
//                .$myrow["nopay_amount"]."<br>";
//    }
//    return $s;
//}
//function get_calculated_worktime_deficit_deductions($employee_code, $payroll_run_id, $status_code){
//    
//    $s = "";
//    $result = select_calculated_worktime_deficit_deductions($employee_code, $payroll_run_id, $status_code);
//    while ($myrow = db_fetch($result))
//    {
//        $s .= sql2date($myrow["from_date_time1"])." - "
//                .substr($myrow["deducted_deficit_time"],-8,5)." - "
//                .$myrow["deficit_amount"]."<br>";
//    }
//    return $s;
//}
//function get_paid_ot_amounts($employee_code, $payroll_run_id, $status_code){
//    
//    $s = "";
//    $result = select_paid_ot_amounts($employee_code, $payroll_run_id, $status_code);
//    while ($myrow = db_fetch($result))
//    {
//        $s .= sql2date($myrow["from_date_time1"])." - "
//                .substr($myrow["granted_ot_time"],-8,5)." - "
//                .$myrow["ot_amount"]."<br>";
//    }
//    return $s;
//}
//function get_calculated_loan_installments($employee_code, $payroll_run_id, $status_code){
//    
//    $s = "";
//    $result = select_calculated_loan_installments($employee_code, $payroll_run_id, $status_code);
//    while ($myrow = db_fetch($result))
//    {
//        $s .= $myrow["loan_description"]." ("
//                .$myrow["loan_amount"].") - "
//                .$myrow["this_installment"]."<br>";
//    }
//
//    return $s;
//}
//function get_calculated_ot_amounts($employee_code, $payroll_run_id, $status_codes){
//    
//    $s = "";
//    $result = select_calculated_ot_amounts($employee_code, $payroll_run_id, $status_codes);
//    while ($myrow = db_fetch($result))
//    {
//        $s .= sql2date($myrow["from_date_time1"])." - "
//                .substr($myrow["granted_ot_time"],-8,5)." - "
//                .$myrow["ot_amount"]."<br>";
//    }
//
//    return $s;
//}
function can_process($run_id)
{	
    
    $adr = get_asset_depreciation_run_details($run_id);
    
    $period_end_date = $adr['period_end_date'];
    
    if (!is_date_in_fiscalyear($period_end_date, true)) {
        display_error(_("The entered date is not in fiscal year.")); 
        return false;
    }
        
    $msg = is_accounting_period_open($period_end_date, true);
    if ($msg['success'] !== true)
    {
        display_error($msg['message']); 
        return false;
    }
    
//    if (!check_num('upto', 0))
//    {
//        display_error( _("Up To value entered was not numeric or positive."));
//        set_focus('upto');
//        return false;
//    }
//
//    if (!check_num('calculation_value', 0))
//    {
//        display_error( _("Calculation value entered was not numeric or positive."));
//        set_focus('calculation_value');
//        return false;
//    }
//    
//    if (!check_num('fixed_value'))
//    {
//        display_error( _("Fixed value entered was not numeric"));
//        set_focus('fixed_value');
//        return false;
//    }    
    return true;
}
/*function dispaly_payroll_employee_result($asset_run)
{
        $asset_run_id = $asset_run['id'];
    
        $result = select_payroll_employee_result($asset_run_id);
    
        start_table(TABLESTYLE, "width=60%");

	$th = array(_("ID"), _("Component Code"), _("Component Name"),
                        _("Amount"), _("Comment"));

        $k = $j = 0; //row colour counter
        $previous_employee_code = "";

        while ($myrow = db_fetch($result))
        {
            $employee_code = $myrow["employee_code"];
            if($previous_employee_code != $employee_code){
                table_header($th);
                
                $name_row = select_employee_name($employee_code);
                $emp_name = "[".$employee_code."] - ".$name_row["initials"]." ".$name_row["last_name"]." (".$name_row["first_name"].")";
                label_cell("<b>".$emp_name."</b>", "align=center colspan=5");
            }
            alt_table_row_color($k);
            
            $process_type = $myrow['process_type'];
            $process_id = $myrow['process_id'];
            $component = $myrow['component'];

            label_cell($myrow["id"]);
            label_cell($myrow["component"]);
            label_cell($myrow["process_name"]);   

            $dec = 2;       
            qty_cell($myrow["value"], false, $dec);
            label_cell(nl2br($myrow["comment"])); 
            
            $details = "";*/
            
//            switch ($process_type){
//                case 'WORKTIME_DEFICITS':
//                    if($payroll_run['status'] == "C" || $payroll_run['status'] == "L"){
//                            $status_code = 'L';}
//                    elseif($payroll_run['status'] == "P" || $payroll_run['status'] == "R"){
//                            $status_code = 'Y';}
//
//                    //$details = get_calculated_worktime_deficit_deductions($employee_code, $payroll_run_id, $status_code);
//                    break;
//                case 'SALARY_DEDUCTIONS':
//                    if($payroll_run['status'] == "C" || $payroll_run['status'] == "L"){
//                            $status_code = 'L';}
//                    elseif($payroll_run['status'] == "P" || $payroll_run['status'] == "R"){
//                            $status_code = 'Y';}
//
//                    //$details = get_calculated_salary_deductions($process_id, $employee_code, $payroll_run_id, $status_code);
//                    break;
//                case 'SALARY_ADDITIONS':
//                    if($payroll_run['status'] == "C" || $payroll_run['status'] == "L"){
//                            $status_code = 'L';}
//                    elseif($payroll_run['status'] == "P" || $payroll_run['status'] == "R" || $payroll_run['status'] == "T"){
//                            $status_code = 'Y';}
//
//                    //$details = get_calculated_salary_additions($process_id, $employee_code, $payroll_run_id, $status_code);
//                    break;
//                case 'COMMON_PAYROLL_PROCESSES':    
//                    switch ($component){
//                        case 'LOAN_INSTALLMENTS':
//                            if($payroll_run['status'] == "C" || $payroll_run['status'] == "L"){
//                                    $status_code = 'L';}
//                            elseif($payroll_run['status'] == "P" || $payroll_run['status'] == "R"){
//                                    $status_code = 'Y';}
//
//                            //$details = get_calculated_loan_installments($employee_code, $payroll_run_id, $status_code);
//                            break;
//                        case 'NO_PAY_DEDUCTIONS':
//                            if($payroll_run['status'] == "C" || $payroll_run['status'] == "L"){
//                                    $status_code = 'L';}
//                            elseif($payroll_run['status'] == "P" || $payroll_run['status'] == "R"){
//                                    $status_code = 'Y';}
//
//                            //$details = get_calculated_nopay_deductions($employee_code, $payroll_run_id, $status_code);
//                            break;
//                        case 'OT_CALCULATIONS':
//                            if($payroll_run['status'] == "C" || $payroll_run['status'] == "L"){
//                                    $status_codes = "'L'";}
//                            elseif($payroll_run['status'] == "P" || $payroll_run['status'] == "R"){
//                                    $status_codes = "'D', 'S', 'Y'";}
//
//                            //$details = get_calculated_ot_amounts($employee_code, $payroll_run_id, $status_codes);
//                            break;
//                        case 'GET_OT_PAYMENTS':
//                            if($payroll_run['status'] == "C" || $payroll_run['status'] == "L"){
//                                    $status_code = 'S';}
//                            elseif($payroll_run['status'] == "P" || $payroll_run['status'] == "R"){
//                                    $status_code = 'Y';}
//                            //$details = get_paid_ot_amounts($employee_code, $payroll_run_id, $status_code);
//                            break;
//
//                        default:
//                }
//                default:
//            }
            
            //label_cell($details);

//            end_row();
//            
//            $previous_employee_code = $myrow["employee_code"];
//
//        } //end of while loop
//
//	end_table();
//}

function display_asset_run_summary($asset_run_id)
{               
//        $asset_run_id = $asset_run['asset_run_id'];
        
        $result = get_asset_depreciation_run_summary($asset_run_id);    
        
        start_table(TABLESTYLE, "width=50%");

	$th = array("Id", _("Asset"), _("Current Value"),
                    _("Depreciation Value"), _("New Value"), _("Processed"));

        table_header($th);
        
        $k = $j = 0; //row colour counter

        while ($myrow = db_fetch($result))
        {
            alt_table_row_color($k);

            label_cell($myrow["id"], "align=center");
            label_cell($myrow["asset_code"]." - ".$myrow["description"]." (".$myrow["asset_id"].")");
            label_cell($myrow["current_value"], "align=right");                
            label_cell($myrow["depreciation_value"], "align=right"); 
           
            $new_value = $myrow["current_value"] - $myrow["depreciation_value"];   
            label_cell($new_value,  "align=right");  
            
            $processed = 0;
            if($myrow["processed_by"] > 0){
                $processed = 1;
            }
            label_cell(get_yes_no_name($processed),  "align=center");                       
         
            $j++;
            end_row();

        } //end of while loop

	end_table();
}

if(isset($_POST['process_asset_depreciation_calculation']) && can_process($_POST['asset_run_id'])) 
{       
    $padc_msg = process_asset_depreciation_calculation($_POST['asset_run_id']); 
    if($padc_msg['success'] !== true){
        display_error($padc_msg['message']);
    }
    else{
        display_notification($padc_msg['message']);
    }
}

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

if ($Mode == 'RESET')
{
    $selected_id = -1;
    unset($_POST);
}

global $Ajax;
        
$Ajax->activate('code_table');

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



start_form();
//if (!isset($_POST['table_id']))
//        $_POST['table_id'] = get_global_table_id ();

br(1);

start_table(TABLESTYLE_NOBORDER);
asset_depreciation_run_list_row("Asset Depreciation Run:", 'asset_run_id', false,"", true,"all");
end_table();

//$asset_run = get_asset_run_id_from_asset_run_details($_POST['asset_run_id']);

//$asset_run_id = $asset_run['asset_run_id'];

br(2);

div_start('code_table');
	
//dispaly_payroll_employee_result($asset_run);        
      
display_asset_run_summary($_POST['asset_run_id']);        
        

//-----------------------------------------------------------------------------------------------
br();

if ($selected_id != '') {

} 
 
br(1);

$run = get_asset_depreciation_run_details($_POST['asset_run_id']);
if(is_array($run)){
    if($run['status'] == "L" || $run['status'] == "P"){ 
        submit_center_first('process_asset_depreciation_calculation', _("Process Asset Depreciation Calculation"), '', 'default');
    }
}

div_end();


end_form(3);
end_page();

?>
