<?php


function can_process_tool_usage_costs()
{ 
    
    if (!(is_date($_POST['cutoff_date']))) 
    {
        $message = ( _("Entered date is not a valid date."));
        $set_focus = ('cutoff_date');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
    
    if (!is_date_in_fiscalyear($_POST['cutoff_date'])) {
        $message = (_("The entered date is not in fiscal year."));
        $set_focus = ('cutoff_date');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
        
    $msg = is_accounting_period_open($_POST['cutoff_date']);
    if ($msg['success'] !== true)
    {
        $message = ($msg['message']);
        $set_focus = ('cutoff_date');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
    
    
    return array("success"=>true, "set_focus"=>"", "message"=>"");
    
}

function calculate_tool_usage_costs($cutoff_date_)
{
    $current_user_id = $_SESSION['wa_current_user']->user;
            
    $cutoff_date = date2sql($cutoff_date_);

    $result = get_open_tool_issues_for_cost_calculation($cutoff_date);
    while($asset_issue_h = db_fetch($result)){
        $asset_issue_id = $asset_issue_h['id'];        

        $caucar_msg = calculate_tool_usage_charges_for_tool_issue($asset_issue_h, $cutoff_date_);        
        if($caucar_msg["success"] !== true){
            return $caucar_msg;
        }

        $charge_asset_issue = $caucar_msg["asset_issue_charge"];
        
        $tasks_summary = calculate_tool_issue_task_remaining_totals($asset_issue_id); 

        $at_msg = reallocate_to_tasks(ST_TOOL_ISSUE, $asset_issue_id, 
                                $tasks_summary, $current_user_id);
        if($at_msg['success'] !== true){
            return $at_msg;
        }
    
        $cauct_msg = charge_tool_usage_costs_to_tasks($asset_issue_id, $current_user_id);
        if($cauct_msg['success'] !== true){
            return $cauct_msg;
        }
        
        $asset_issue_fully_charged = 0;
        
        if($asset_issue_h['fully_returned']){
            if(check_tool_issue_fully_charged($asset_issue_id)){
                $asset_issue_fully_charged = 1;
            }
        }
            
        $n = update_tool_issue_charges_calculation_summary($asset_issue_id, $cutoff_date, $asset_issue_fully_charged, 
                                                            $charge_asset_issue, $asset_issue_h['row_state']);
        if($n !== 1){
            return array("success" => false, "message" => "Asset Issue $asset_issue_id has been updated by another user.");
        }
    }
    return array("success" => true, "message" => "");
}


function calculate_tool_usage_charges_for_tool_issue($asset_issue_h, $cutoff_date_)
{
    $charge_no = $charge_issue_h = 0;
    
    $cutoff_date = date2sql($cutoff_date_);
    
    $charged_upto_date = $asset_issue_h['charged_upto_date'];
    if(substr($charged_upto_date, 0, 4) == "0000"){
        $charged_upto_date =  INFINITE_START_DATE;
    }
    $charged_upto_date_ = sql2date($charged_upto_date);
    $asset_issue_id = $asset_issue_h['id'];
    
    add_to_tool_usage_charges_log("NEW", $asset_issue_id, 0, 
                                    0, $charge_no, 
                                    '', '', '', 0, 0, 0);
            
    $aid_result = get_open_tool_issue_detail_records_for_charging($asset_issue_id);
    while($asset_issue_detail = db_fetch($aid_result)){
        
        $asset_rate = $asset_issue_detail['rate'];
        
        $asset_issue_detail_from_date_ = sql2date($asset_issue_detail['from_date']);
        $issue_from_date_ = $asset_issue_detail_from_date_;
        if(date1_greater_date2($charged_upto_date_, $asset_issue_detail_from_date_)){
            $issue_from_date_ = $charged_upto_date_;
        }

        $period_days = date_diff2($cutoff_date_, $issue_from_date_, 'd');
        if(!($period_days > 0)){
            continue;
        }   

        $total_issued_units = $period_days * $asset_issue_detail['quantity'];    

        $total_returned_units = $returned_units = $returned_days = 0;
        $all_return_records_calculated = true;
        
        $artnd_result = get_tool_return_detail_records_of_tool_issue_line($asset_issue_detail['id']);
        while($asset_return_detail = db_fetch($artnd_result)){
            $returned_units = $returned_days = 0;

            if($asset_return_detail['returned_date'] <= $cutoff_date){
                // Asset has been returned on or before the cutoff date
                $asset_return_detail_from_date_ = sql2date($asset_return_detail['returned_date']);
                $returned_from_date_ = $asset_return_detail_from_date_;
                if(date1_greater_date2($charged_upto_date_, $asset_return_detail_from_date_)){
                    $returned_from_date_ = $charged_upto_date_;
                }
                $returned_days = date_diff2($cutoff_date_, $returned_from_date_, 'd');  
//                    if asset issue quantity fully retuned
//                    $all_return_records_calculated = true;
            }else{
                // Asset is retrned but after the cutoff date
                $returned_from_date_ = '';
                $returned_days = 0;
                    $all_return_records_calculated = false;
            }


            if(!($returned_days > 0)){
                continue;
            }

            $returned_units = $returned_days * $asset_return_detail['quantity'];                   
            $total_returned_units += $returned_units;
   
            add_to_tool_usage_charges_log("RETURN", $asset_issue_id, $asset_issue_detail['id'], 
                                            $asset_return_detail['id'], $charge_no, 
                                            $asset_return_detail['returned_date'], $returned_from_date_, $cutoff_date, 
                                            $returned_days, $asset_return_detail['quantity'], $asset_rate);
        }


        $asset_issue_detail_fully_charged =  false;
        if(!($asset_issue_detail['quantity'] > $asset_issue_detail['returned_qty'])){
            // all the quantities of asset issue have been returned
            if($all_return_records_calculated){
            // all returns have been done before the cutoff_date.
                $asset_issue_detail_fully_charged = true;
            }
        }

        $charging_units = $total_issued_units - $total_returned_units;
        if($charging_units > 0 ){
            $charge_aissu_d = $charging_units * $asset_rate;
        }
        else{
            $charge_aissu_d = 0;
        }

        if($charge_aissu_d > 0 ){
            if($charge_no == 0){
                $charge_no = create_next_tool_usage_charge_no($cutoff_date, $asset_issue_id);
            }

            save_tool_usage_charge_details($charge_no, $asset_issue_detail['id'], date2sql($issue_from_date_), $charging_units, $asset_rate);
        }

        add_to_tool_usage_charges_log("ISSUE", $asset_issue_id, $asset_issue_detail['id'], 
                                        $asset_return_detail['id'], $charge_no, 
                                        $asset_issue_detail['from_date'], date2sql($issue_from_date_), $asset_issue_detail['to_date'], 
                                        $returned_days, $asset_return_detail['quantity'], $asset_issue_detail['rate']);


        $charge_issue_h += $charge_aissu_d;
        update_tool_issue_detail_charged_summary($asset_issue_detail['id'], $charging_units, $asset_issue_detail_fully_charged);

    }
    
    return array("success" => true,"asset_issue_charge" => $charge_issue_h, "message" => "");
    

}

function charge_tool_usage_costs_to_tasks($asset_issue_id, $current_user_id)
{
    $result = get_open_tool_usage_charge_nos($asset_issue_id);
    while($auc_id = db_fetch($result)){
        $asset_usage_charge_id = $auc_id['id'];
        $tasks_summary = calculate_tool_usage_charges_task_totals($asset_usage_charge_id); 
        
        $cct_msg = charge_cost_to_tasks(ST_TOOL_USAGE_CHARGE, $asset_usage_charge_id, $tasks_summary, $current_user_id);              

        if($cct_msg['success'] !== true)
        {
            return $cct_msg;
        }
    
        $n = update_tool_usage_costs_charged($asset_usage_charge_id);
        if($n != 1)
        {
            return array("success" => false, "tran_no" => $asset_usage_charge_id, 
                "message" => "Asset usage charge $asset_usage_charge_id has been updated by another user.");
        }
        
    }
    
    return array("success" => true, "message" => ""); 
    
}

function calculate_tool_usage_charges_task_totals($asset_usage_charge_id)
{
    $task_costs = array();

    $result = get_tool_usage_charges_task_totals($asset_usage_charge_id);

    while($row = db_fetch($result))
    {            
        if($row['task_cost'] > 0){
            add_to_task_cost($task_costs, $row['task_id'], $row['task_cost'], $row['task_cost'], $row['task_cost']);
        }
    }

    get_task_cost_details($task_costs);

    return $task_costs;
}




function update_tool_request_cost_allocations($charge_areq, $areq_h)
{
    if($charge_areq > 0){
            //        calculate_tool_request_task_remaining_totals($ar_no);
            //        allocate_to_tasks
//        charge_cost_to_tasks($tran_type, $tran_no, $tasks_charging_summary, $current_user_id);
        }
        else{
            if($areq_h['returned'] == 1){ //all assets retuned
            // deallocate costs;
                // updated_asset_fully_chanrged
            }
        }
        $tool_request_total_charge = 2;
        $tool_request_total_units_charged = 2;
//        update_tool_request_charge();
}




?>