<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_TOOL_ISSUE';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$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 . "/includes/approval_functions.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/includes/common_functions.inc");
include_once($path_to_root . "/taxes/db/tax_groups_db.inc");
include_once($path_to_root . "/assets/includes/db/tool_issues_db.inc");
include_once($path_to_root . "/includes/ui/ui_lists_group_companies.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/assets/includes/db/tool_requests_db.inc");
include_once($path_to_root . "/assets/includes/db/assets_db.inc");
include_once($path_to_root . "/includes/project_cost_functions.inc");

        
$js = '';
if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}

page(_($help_context = "Issue Tools"), false, false, "", $js);

//------------------------------------------------------------------------------------------------------
simple_page_mode(true);

$ar_no = -1;
 
if (isset($_GET['ARNo']))
{
    $_POST['ARNo'] = $_GET['ARNo'];
    set_session_variable("ST_TOOL_ISSUE_ar_no", $_POST['ARNo']);
}
     
if (isset($_POST['ARNo']) && $_POST['ARNo'] > 0){
    $ar_no = $_POST['ARNo'];
}
else{
    $ar_no = get_session_variable("ST_TOOL_ISSUE_ar_no");    
}

if (isset($_GET['Added_AI_Id'])) 
{ 
    $trans_no = $_GET['Added_AI_Id'];
    $trans_type = ST_TOOL_ISSUE;
    $message = "Tool Issue No $trans_no has been created.";
    
	display_notification_centered($message);
        
	display_note(print_document_link($trans_no, _("&Print this tool issue"), true, $trans_type), 0, 0);

	hyperlink_no_params("$path_to_root/assets/inquiry/outstanding_tool_requests_inquiry.php", _("Create another &tool issue"));

//	hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");

	display_footer_exit();
}


if (isset($_GET['Closed_AREQ_Id'])) 
{ 
    $trans_no = $_GET['Closed_AREQ_Id'];
    $trans_type = ST_TOOL_REQUEST;
    $message = "Tool Request No $trans_no has been closed.";
    
	display_notification_centered($message);
        
	display_note(print_document_link($trans_no, _("&Print this Tool Issue"), true, $trans_type), 0, 0);

	hyperlink_no_params("$path_to_root/assets/inquiry/tool_request_inquiry.php", _("Close another &Tool Request."));

//	hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");

	display_footer_exit();
}

function can_process_ai($ar_header)
{
    $ar_no = $ar_header['id'];
    $cph_msg = can_process_ai_header($ar_header);
    if($cph_msg["success"] !== true){
        return $cph_msg;
    }
            
    $result_details = get_tool_request_details($ar_no);     
   
    $s_lines =0;
    while($row_d = db_fetch($result_details)){
        
        if((isset($_POST["selected_".$row_d["id"]])) && ($_POST["selected_".$row_d["id"]] == true))
                 
        {
            $cpl_msg = can_process_ai_line($ar_no, $row_d['id']);
                if($cpl_msg["success"] !== true){
                return $cpl_msg;
            }
            
            $s_lines++;
        }       
         
    }
    if($s_lines == 0){
        $message = "No Lines Selected";
        return array("success" => false, "focus_to" => '', "message" => $message);
    }
    
   return array("success" => true, "message" => '');
}


function can_process_ai_header($areq_header)
{ 
    global $Refs;
   
    $areq_id = $areq_header['id'];
    
    if(!($areq_header['approval_status'] == AS_APPROVED || $areq_header['approval_status'] == AS_APPROVED)){        
        $message = ("Status of tool request does not allow any further issues against it.");
        $set_focus = '';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
      
    if(!$Refs->is_valid($_POST['reference'])){
        $message = ("Please enter a reference.");
        $set_focus = 'reference';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);  
    }    
 
    $ref_msg = reference_exists($_POST['reference'], ST_TOOL_ISSUE);
    if($ref_msg){
        $message = $ref_msg;
        $set_focus = '';  
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);       
    }
    
    
    if (!($_POST['project_id'] > 0))
    {
        $message = "Please select a Project.";
        $set_focus = 'project_id';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    if($_POST['location'] == '')
    {
        $message = "Please select a Location.";
        $set_focus = 'location';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }       
    
    if(!(strlen($_POST['employee_code']) > 1))
    {
        $message = "Please select an employee.";
        $set_focus = 'employee_code';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);        
    }      

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

        
function can_process_ai_line($ar_no, $line_id)
{
    $error_prefix = "Line Id ".$line_id." : ";
    $issuing_asset_id = $_POST['issuing_asset_id'."_".$line_id];
    
    if (!($issuing_asset_id > 0))
    {
        $message = $error_prefix."Please select an tool.";
        $set_focus = 'issuing_asset_id'."_".$line_id;
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    if(!(check_num('no_of_days'."_".$line_id, 0.00001)))
    {
        $message = $error_prefix."No of charging units should be greater than zero and positive.";
        $set_focus = 'no_of_days'."_".$line_id;
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);  
    }
    
    if(!is_date($_POST['issued_from_date_'.$line_id]))
    {
        $message = $error_prefix."Entered issued from date is not a valid date.";
        $set_focus = 'issued_from_date_'.$line_id;
        return array("success" => false, "focus_to" => $set_focus, "message" => $message); 
    }
    
    if(!is_date($_POST['issued_to_date_'.$line_id]))
    {
        $message = $error_prefix."Entered issued to date is not a valid date.";
        $set_focus = 'issued_to_date_'.$line_id;
        return array("success" => false, "focus_to" => $set_focus, "message" => $message); 
    }
    
    if(date1_greater_date2($_POST['issued_from_date_'.$line_id], $_POST['issued_to_date_'.$line_id]))
    {
        $message = $error_prefix."Entered issued from date cannot be greater than issued to date.";
        $set_focus = 'issued_to_date_'.$line_id;
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);        
    }
    
    
    $issuing_asset = get_asset($issuing_asset_id);
    $multiple_items = $issuing_asset['multiple_items'];

    if(!(check_num('issuing_quantity'."_".$line_id, 0.00001)))
    {
        $message = $error_prefix."Issuing quantity should be greater than zero.";
        $set_focus = 'issuing_quantity'."_".$line_id;
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);  
    }
    
            
    $asset_req_details = get_tool_request_detail_line($line_id);    
    if(input_num('no_of_days'."_".$line_id) > $asset_req_details['no_of_days']){
        $message = $error_prefix."No of charging units can not be higher than the reqested charging units of ".$asset_req_details['no_of_days'];
        $set_focus = 'issuing_quantity'."_".$line_id;
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);  
    }
  
    $requested_qty = $asset_req_details['quantity'];        
    $issued_qty = get_asset_total_issued_qty($line_id);
//    $issued_qty = $asset_req_details['issued_qty'];
    
//        $issuing_asset_details = get_asset($_POST['issuing_asset_id'."_".$line_id]);
//        $multiple_items = $issuing_asset_details['multiple_items'];
        
    if($multiple_items == 1){
        $issuing_qty = $_POST['issuing_quantity'."_".$line_id];  
    }else{
        $issuing_qty = 1;
    }
        
      
    $total_issued_qty = $issuing_qty + $issued_qty;
           
    if($total_issued_qty > $requested_qty){
        $ar_line = get_tool_request_detail_line($line_id);
        $requesting_asset = get_asset($ar_line['asset_id']);

        $error_prefix2 = "Requesting Tool (".$requesting_asset['asset_id'].") - ".$requesting_asset['asset_code']." - ".$requesting_asset['description']." - ";
        $msg = " Total of issuing quantity ($issuing_qty) and already issued quantity ($issued_qty) cannot be greater than requested quantity ($requested_qty).";
        
        $message = $error_prefix.$error_prefix2.$msg;
        $set_focus = 'issuing_quantity'."_".$line_id;
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);  
    }
        
    return array("success" => true, "message" => "");  
    
}


function save_ai_header($ar_no)
{
    $ai_id = add_asset_issue_header($ar_no, $_POST['reference'], $_POST['project_id'], $_POST['group_company_code'], $_POST['employee_code'],
                                            $_POST['location'], $_POST['date'], $_POST['comment']);
                                              
    if($ai_id > 0){
        return array("success" => true, 'trans_no' => $ai_id, "message" => "Tool issue $ai_id has been saved.");
    }
    else{
        return array("success" => false, 'trans_no' => '', "message" => "Could not save Tool issue header.");
    }
}


function save_ai_details($ar_no, $ai_id, $project_id)
{    
    $ard_details = get_tool_request_detail_records($ar_no);      
          
    while ($a_row = db_fetch($ard_details))
    {
        if((isset($_POST['selected_'.$a_row['id']])) && ($_POST['selected_'.$a_row['id']] == true))                   
        {        
            $source_line_id =  $a_row['id'];
            $issuing_asset_id = $_POST['issuing_asset_id'."_".$source_line_id];     

            $rate_category_id = $a_row['rate_category_id'];
            $rate = $a_row['rate'];   
            
            $task_id = $a_row['task_id'];
            $site_id = $_POST['site_id'."_".$source_line_id];

            $from_date = $_POST['issued_from_date'."_".$source_line_id];

            $to_date = $_POST['issued_to_date'."_".$source_line_id];

//            $no_of_days = date_diff2($to_date, $from_date, 'd') + 1;
            $no_of_days = input_num('no_of_days'."_".$source_line_id);  
                    
            $issuing_comment = $_POST['issuing_comment'."_".$source_line_id];
            $project_asset = $_POST['project_asset'."_".$source_line_id]; 

            $issuing_qty = input_num('issuing_quantity'."_".$source_line_id);                 

            $asset_issue_line_id = add_asset_issue_line($source_line_id, $ai_id, $issuing_asset_id,
                                                $rate_category_id, $rate,  
                                                $task_id, $site_id, $from_date,
                                                $to_date, $no_of_days, 
                                                $issuing_comment, $project_asset, $issuing_qty);  

            $issued_qty = get_asset_total_issued_qty($source_line_id);

            $n = update_tool_request_line_issued_qty($source_line_id, $issued_qty);

            if($n != 1){
                return array("success" => false, 'trans_no' => $ai_id, "message" => "Another user has updated tool request line $source_line_id.");
            }      
            
            add_asset_move(ST_TOOL_ISSUE, $ai_id, $asset_issue_line_id, $_POST['reference'], 
                        $from_date, $issuing_asset_id, $_POST['location'], $site_id, 
                        $task_id, -$issuing_qty, $project_id);
        }
    }
    
    return array("success" => true, 'trans_no' => $ai_id, "message" => "");
}




function save_ai($ar_no, $close)
{
    $areq_header = get_tool_request_header($ar_no);
    
    $cppd_msg = can_process_ai($areq_header);
    if($cppd_msg['success'] !== true){
        display_error($cppd_msg['message']);
        return;
    }

        
    begin_transaction();
        
    $saih_msg = save_ai_header($ar_no);
    if($saih_msg["success"] !== true){
        cancel_transaction();
        $Mode = "RESET";
        display_error($cppd_msg['message']);        
        return;
    }

    $ai_id = $saih_msg['trans_no'];    
    $msg = save_ai_details($ar_no, $ai_id, $areq_header['project_id']);
    if($msg["success"] !== true){
        cancel_transaction();
        $Mode = "RESET";
        display_error($msg['message']);        
        return;
    }
        
    if(check_all_tool_request_lines_fully_issued($ar_no)){
//        $n2 = update_tool_request_status($ar_no, AS_COMPLETED, $areq_header['row_state']);
        $ar_complte_msg = direct_update_document_status(ST_TOOL_REQUEST, $ar_no, AS_COMPLETED, $areq_header['row_state']);    
        if($ar_complte_msg["success"] !== true){
            return array("success" => false, 'trans_no' => $ai_id, "message" => "Another user has updated tool request $ar_no.");
        }
    }
    else{
        if($close){
            $ar_complte_msg = direct_update_document_status(ST_TOOL_REQUEST, $ar_no, AS_CLOSED, $areq_header['row_state']);    
            if($ar_complte_msg["success"] !== true){
                return array("success" => false, 'trans_no' => $ai_id, "message" => "Another user has updated tool request $ar_no.");
            }
        }
    }
            
    $current_user_id = $_SESSION['wa_current_user']->user;    
    if($close){
        $rara_msg = remove_tool_request_allocations($ar_no, $current_user_id);
        if($rara_msg["success"] !== true){
            cancel_transaction();
            $Mode = "RESET";
            display_error($rara_msg['message']);        
            return;
        }
    }
    else{
        $raar_msg = reallocate_tool_request($ar_no, $current_user_id);
        if($raar_msg["success"] !== true){
            cancel_transaction();
            $Mode = "RESET";
            display_error($raar_msg['message']);        
            return;
        }
    }
    
    $aai_msg = allocate_asset_issue($ai_id, $current_user_id);
    if($aai_msg["success"] !== true){
        cancel_transaction();
        $Mode = "RESET";
        display_error($aai_msg['message']);        
        return;
    }
    
    commit_transaction();
    clear_session_variable("ST_TOOL_ISSUE_ar_no");
    meta_forward($_SERVER['PHP_SELF'], "Added_AI_Id=$ai_id");

}

function reallocate_tool_request($tool_request_id, $current_user_id)
{
    $tasks_summary = calculate_tool_request_task_remaining_totals($tool_request_id); 

    $at_msg = reallocate_to_tasks(ST_TOOL_REQUEST, $tool_request_id, 
                            $tasks_summary, $current_user_id);
    return $at_msg;
}

function allocate_asset_issue($tool_request_id, $current_user_id)
{
    $tasks_summary = calculate_tool_issue_task_remaining_totals($tool_request_id); 

    if(sizeof($tasks_summary) > 0){
        $at_msg = reallocate_to_tasks(ST_TOOL_ISSUE, $tool_request_id, 
                            $tasks_summary, $current_user_id);
    }
    else{
        return array('success' => true, 'message' => "");
    }
    return $at_msg;
}

function remove_tool_request_allocations($tool_request_id, $current_user_id)
{
    $tasks_summary = calculate_tool_request_task_remaining_totals($tool_request_id); 

    $at_msg = remove_task_allocations(ST_TOOL_REQUEST, $tool_request_id, 
                            $tasks_summary, $current_user_id);
    return $at_msg;
}


function close_ar($ar_no)
{
    $areq_header = get_tool_request_header($ar_no);
    
//    $cppd_msg = can_process_ai($areq_header);
//    if($cppd_msg['success'] !== true){
//        display_error($cppd_msg['message']);
//        return;
//    }

        
    begin_transaction();        
    
    $tasks_summary = calculate_tool_request_task_remaining_totals($ar_no); 
    $current_user_id = $_SESSION['wa_current_user']->user;    
    $rta_msg = remove_task_allocations(ST_TOOL_REQUEST, $ar_no, $tasks_summary, $current_user_id);
    if($rta_msg["success"] !== true){
        cancel_transaction();
        display_error($rta_msg['message']);        
        return;
    }

    $n2 = update_tool_request_status($ar_no, AS_CLOSED, $areq_header['row_state']);
    if($n2 != 1){
        $message = "Another user has updated tool request $ar_no.";
        cancel_transaction();
        display_error($message);        
        return;
    }
            
    commit_transaction();
    clear_session_variable("ST_TOOL_ISSUE_ar_no");
    meta_forward($_SERVER['PHP_SELF'], "Closed_AREQ_Id=$ar_no");

}


function display_ai_header($ar_no)
{
    global $Refs;
     
    if($ar_no>0){
        
        $ar_header = get_tool_request_header($ar_no);
        $_POST['project_id'] = $ar_header['project_id'];
        
        $project_id = $_POST['project_id'];
        $user_id = $_SESSION['wa_current_user']->user;
        $cap = can_access_project($project_id, $user_id);
        if($cap['success'] !== true) 
        {
            display_error($cap["message"]);
            set_focus('project_id');
            display_footer_exit();
            exit;
        }
              
//        if(!isset($_POST['employee_code'])){
//            $_POST['employee_code'] = $ar_header['employee_code'];
//        }
        if(!isset($_POST['reference'])){
            $_POST['reference'] = $ar_header['reference'];
        }
        
        if(!isset($_POST['location'])){
            $_POST['location'] = $ar_header['location'];
        }

        if(!isset($_POST['tran_date'])){
            $_POST['tran_date'] = sql2date($ar_header['date']);
        }

        if(!isset($_POST['project_description'])){
            $_POST['project_description'] = $ar_header['project_description'];
        }
        
        if(!isset($_POST['comment'])){
            $_POST['comment'] = $ar_header['comment'];
        }
        
        if(!isset($_POST['group_company_code'])){
            $_POST['group_company_code'] = $ar_header['company_code'];
        }
        
        if(!isset($_POST['employee_code'])){
            $_POST['employee_code'] = $ar_header['employee_code'];
        }
    }

        
    start_outer_table(TABLESTYLE2, "width=50%"); // outer table
    start_table(TABLESTYLE2);  
        table_section(1, "50%");
            text_row("Reference:", 'reference', $Refs->get_next(ST_TOOL_ISSUE), null, null);   
        
            if($ar_no>0){
                label_row("Project:", $_POST['project_description']);   
                hidden('project_id', $_POST['project_id']);
            }
            else{
               project_list_row(_("Project:"), 'project_id', null, _('Select a project'), true); 
            }
            
        group_companies_list_row("Company:", 'group_company_code', null, false, true);    
        group_company_employees_simple_list_row(_("Select an Employee:"), 'employee_code', $_POST['group_company_code'],'', "Please select an employee", "Select an employee", false);    
      
            
            asset_locations_list_row("Location:", 'location', null, 'Select Location', false);           
      
            date_row("Date:", "date");

            textarea_row("Comment:", 'comment', null, 35, 2); 
    

//    end_table(1);
    end_outer_table(1); // outer table

}


function display_ai_details($ar_no)
{  
    global $project_invoice_line_types, $Ajax;    
       
    $result = get_tool_request_details($ar_no);      
                              
    start_table(TABLESTYLE, "width=98%");
    $th = array(_("ID"), _('Requested Tool'),_('Rate Category'),
                _("Req. From Date"),_("Req. To Date"),_(""),
                _("Project Asset"), _("Req. Qty"), _("Issd. Qty"),_("Req.Comment"), 
                 "");                            

    table_header($th);
    $th = array(_(""), _('Issuing Tool'),_('No of Units (Per Tool)'),
                _("Issu. From Date"),_("Issu. To Date"),_("Job"),
                _("Project Asset"), _(""), _("Issuing Qty"),_("Req.Comment"), 
                 "");                            

    table_header($th);
    $k = 0; //row colour counter
   // $total = 0;
    
    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);

        label_cell($myrow["id"], "align=right");

//        label_cell('Requested Tool'); 
        
        $asset_name = $myrow["asset_code"]." - ".$myrow["asset_description"]." - (".$myrow["asset_id"].")";
        label_cell($asset_name);                 
      
        label_cell($myrow["asset_rate_description"]);                     

        label_cell(sql2date($myrow["from_date"]));
        label_cell(sql2date($myrow["to_date"]));
        
        label_cell($myrow["site_description"]);  

        label_cell(get_yes_no_name($myrow['project_asset']), "align=center");

        $requested_qty = $myrow['quantity'];
        qty_cell($requested_qty, false, 0);
          
        $issued_qty = $myrow['issued_qty'];
        qty_cell($issued_qty, false, 0);
        
        label_cell($myrow["line_comment"]);                     
        
        label_cell('');
        
       
        $Ajax->activate('issuing_asset_id');        
        
        if(!isset($_POST['issuing_asset_id'."_".$myrow['id']])){
            $_POST['issuing_asset_id'."_".$myrow['id']] = $myrow['asset_id'];
        }else{
            $_POST['issuing_asset_id'."_".$myrow['id']] = $_POST['issuing_asset_id'."_".$myrow['id']];
        }
        
        end_row();
        
        //Issuing asset line
        alt_table_row_color($k);
        
        label_cell(''); 
        
        
        
        
//        label_cell('Issuing Tool'); 
        
        echo "<td>";
//            echo tool_list('issuing_asset_id'."_".$myrow['id'], null, false, true);
            echo asset_list('issuing_asset_id'."_".$myrow['id'], null, false, true); 
        echo "</td>";        
                     
//        label_cell('sfghsfgsg'); 
        set_post_not_set_numeric('no_of_days'."_".$myrow['id'], $myrow['no_of_days']);
        qty_cells(null, 'no_of_days'."_".$myrow['id'], null, null, null, 0);    
        
        $_POST['issued_from_date'."_".$myrow['id']] = sql2date($myrow["from_date"]);
        date_cells(null, 'issued_from_date'."_".$myrow['id']); 
        
        $_POST['issued_to_date'."_".$myrow['id']] = sql2date($myrow["to_date"]);    
        date_cells(null, 'issued_to_date'."_".$myrow['id']);     
        
        project_sites_list_cells(null, 'site_id'."_".$myrow['id'], $_POST['project_id'], null, '');
        
//        yesno_list_cells(null, 'project_asset_'.$myrow['id'], null, false);   
        label_cell(get_yes_no_name($myrow['project_asset']), "align=center");
        hidden('project_asset_'.$myrow['id'], $myrow['project_asset']);
         
        label_cell("");
        
        $issuing_asset = get_asset($_POST['issuing_asset_id'."_".$myrow['id']]);
        $multiple_items = $issuing_asset['multiple_items'];
        
        $suggested_issuing_qty = $requested_qty - $issued_qty;     
        if($multiple_items == 1){
            if(list_updated('issuing_asset_id'."_".$myrow['id'])){
                $_POST['issuing_quantity'."_".$myrow['id']] = $suggested_issuing_qty;
            }
            
            if(!(isset($_POST['issuing_quantity'."_".$myrow['id']]) && $_POST['issuing_quantity'."_".$myrow['id']] > 0)){
                $_POST['issuing_quantity'."_".$myrow['id']] = $suggested_issuing_qty;
            }
            small_qty_cells(null, 'issuing_quantity'."_".$myrow['id'], null, null, null, 0);           
        }else{
            if($suggested_issuing_qty > 1){
                $suggested_issuing_qty = 1;
            }
            $_POST['issuing_quantity'."_".$myrow['id']] = $suggested_issuing_qty;
            hidden('issuing_quantity'."_".$myrow['id'], $suggested_issuing_qty);
            label_cell($_POST['issuing_quantity'."_".$myrow['id']], "align=right");
        }
                
               
        $ai_details = get_asset_issue_detail_record($myrow['id']);
        
        if(isset($ai_details['ar_line_id']) && $ai_details['ar_line_id'] > 0){
            $_POST['issuing_comment'."_".$myrow['id']] = $ai_details['line_comment'];
        }
        
        text_cells(null, 'issuing_comment'."_".$myrow['id']);
        
//        if($myrow['ai_line_id'] > 0){
//            label_cell("");    
//        }
//        else{
            check_cells(null, 'selected_'.$myrow['id'], null, false);
//        }
        
        $k++;                                      
   
        end_row();        
        
        start_row();
        label_cell(spaces(2), "colspan=10");
        end_row();
      
    }            
}


if(isset($_POST['AddAssetIssue'])){      
       
    save_ai($ar_no, false);
    
}

if(isset($_POST['AddAssetIssueCloseRequest'])){      
       
    save_ai($ar_no, true);
    
}

if(isset($_POST['CloseAssetRequest'])){      
       
    close_ar($ar_no);
    
}



if ($Mode == 'RESET')
{
	$selected_id = -1;
}
    $Ajax->activate('_page_body');
    $Ajax->activate('pd_items');
//--------------------------------------------------------------------------------------------------------------
if (!@$_GET['popup'])
start_form();

$pi_header = display_ai_header($ar_no);   


br(2);

//------------------------------------------------------------------------------------------------------------
         
div_start('pd_items');
display_ai_details($ar_no);
 
div_end();

end_table(1);
//-----------------------------------------------------------------------------------------------------------
      
    submit_center_first('AddAssetIssue', _("Issue Tools"), '', 'default');
    
    br(2);
    
    submit_center_first('CloseAssetRequest', _("Close Request"), '', 'default');
    submit_center_last('AddAssetIssueCloseRequest', _("Issue Tools & Close Request"), '', 'default');


br();


if (!@$_GET['popup'])
{
    end_form();
    end_page(@$_GET['popup'], false, false);        
}



?>
