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

//include_once($path_to_root . "/inventory/includes/stock_class.inc");
//--------------------------------------------------------------------------------



function display_batch_item_issue_line_details($bii_line_id)
{
    global $daily_item_types_array, $inventory_movement_type_linked_fields;
    
    display_heading("Line Details");
    
    $row = get_view_batch_item_issue_line_details($bii_line_id);
    
    start_outer_table(TABLESTYLE);
        table_section(1, "width=30%");
            label_row("Transaction No", $row['trans_no']);
            label_row("Line Id", $row['id']);
            label_row("Location : ", $row['location_name']);
            
        table_section(2, "width=40%");
            label_row("Item Type : ", $daily_item_types_array[$row['daily_item_type']]);
            label_row("Itemcode : ", $row['stock_id']);
            label_row("Description : ", $row['item_description']);
            label_row("Unit : ", $row['tran_unit_name']);
            label_row("Quantity : ", number_format($row['tran_qty'], 3));
            
        table_section(3, "width=30%");
//        get_movement_type($type_id);
            label_row("Job : ", $row['site_name']);
            $_POST['project_site_id'] = $row['project_site_id'];
            hidden('project_site_id', $_POST['project_site_id']);
            
            label_row("Movement Type : ", $row['movement_type_name']);
            label_row("Linked Field Type : ", $inventory_movement_type_linked_fields[$row['linked_field_type']]);
            $lf_name = get_movement_type_linked_field_values($row['linked_field_type'], $row['linked_field_id'], $row['group_company_code']); 
            label_row("Person : ", $lf_name["value"]);
    
    end_outer_table();
    
    
}

function display_batch_item_issue_pr_lines($source_type_id, $bii_line_id, $from_date, $to_date)
{
	global $Ajax, $systypes_array;
        
    $biid_row = get_batch_item_issue_details_record($bii_line_id);
    $tran_unit_code = $biid_row['tran_unit'];
    $item_code = $biid_row['stock_id'];
    
    $tran_unit_row = get_item_unit($tran_unit_code);
    $tran_unit_name = $tran_unit_row['name'];
    
    $biid_conversion_factor = 1;
    get_convertion_factor2($item_code, $tran_unit_code, $biid_conversion_factor);

    $Ajax->activate('items_table');
    
	display_heading($systypes_array[$source_type_id]);
        
    div_start('items_table');
    start_table(TABLESTYLE, "width=98%");
    

    $th = array("PR<br>Line", "PO<br>Line",_("Item"), 
                _("PR.<br>Qty"),  _("PR<br>Issued<br>Qty."), _("PR<br>Returned<br>Qty"),
                _("PO<br>Qty"),  _("PO<br>Received<br>Qty"),
                _("Allocated"),  _("Available"),
                _("Issuing<br>Qty"), "", "");        

	//if ( count($order->line_items)) $th[] = '';

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

	$id = find_submit('Edit');
        
        $result = get_batch_item_issue_pr_lines($bii_line_id, $from_date, $to_date);
        while ($row = db_fetch($result)) {

            $prefix = $row['id']."_".$row['pr_po_line_id']."_".$row['po_detail_id'];
            $bii_trans_line_id = $target_type_id = $target_type_no = $target_line_id = 0;
            
            $source_type_no = $row['trans_no'];
            $source_line_id = $row['id'];
            $trans_row = get_batch_item_issue_transaction_record($bii_line_id, $source_type_id, $source_type_no, $source_line_id);
            if(is_array($trans_row)){
                $bii_trans_line_id = $trans_row['id'];
                $target_type_id = $trans_row['target_type_id'];
                $target_type_no = $trans_row['target_type_no'];
                $target_line_id = $trans_row['target_line_id'];
                
                set_post_not_set_string($prefix.'_tran_unit', $trans_row['tran_unit']);
                set_post_not_set_numeric($prefix.'_tran_qty', $trans_row['tran_qty']);
                $_POST[$prefix.'_tran_qty'] = $trans_row['tran_qty'];
                set_post_not_set_numeric($prefix.'_project_site_id', $trans_row['project_site_id']);
//                set_post_not_set_numeric($prefix.'_movement_type', $trans_row['movement_type']);
//                set_post_not_set_numeric($prefix.'_linked_field_type', $trans_row['linked_field_type']);
//                set_post_not_set_numeric($prefix.'_linked_field_id', $trans_row['linked_field_id']);
                set_post_not_set_numeric($prefix.'_check_batch_issue_trans_line', 1);
            }
            
            

            $total += ($row['standard_cost'] * $row['quantity']);

            alt_table_row_color($k);                    

            $ids_text = get_trans_view_str(ST_PURCH_REQ, $row['trans_no'], $row['id']."(".$row['trans_no'].")");
//            $pr_allocs_link = get_pr_allocation_ids_string($row['po_detail_id'], $row['pr_po_line_id']);
            $po_link = get_supplier_trans_view_str(ST_PURCHORDER, $row['po_no'], $row['pr_po_line_id']."(".$row['po_no'].")");

            label_cell($ids_text, 'align=center');
//            label_cell($pr_allocs_link, 'align=center');
            label_cell($po_link, 'align=center');

            $item_description = get_stock_item_formatted_name($item_code, $row['descr_editable'], $row['stock_master_description'], $row['item_pr_description']);
            label_cell($item_description);
                    
//            label_cell($row['requested_unit_name']);
            
            $dec = get_qty_dec($item_code);
//                    qty_cell($stock_item->requested_qty/$stock_item->convertion_factor, false, $dec);
            
            
            
            $tran_unit_pr_req_qty = $row['quantity'] / $biid_conversion_factor;
            qty_cell($tran_unit_pr_req_qty, false, $dec);
            
//            $conversion_factor = $row['req_unit_conv_factor'];
            
            $tran_unit_pr_issued_qty = $row['issued_qty'] / $biid_conversion_factor;
            qty_cell($tran_unit_pr_issued_qty, false, $dec);
            
            $tran_unit_pr_returned_qty = $row['returned_qty'] / $biid_conversion_factor;
            qty_cell($tran_unit_pr_returned_qty, false, $dec);
            
            $tran_unit_po_qty = $row['po_ordered_qty'] / $biid_conversion_factor;
            qty_cell($tran_unit_po_qty, false, $dec);
            
            $tran_unit_po_received_qty = $row['po_received_qty'] / $biid_conversion_factor;
            qty_cell($tran_unit_po_received_qty, false, $dec);                
            
            $tran_unit_pr_balance_qty = $tran_unit_pr_req_qty - ($tran_unit_pr_issued_qty - $tran_unit_pr_returned_qty);
            
            $base_unit_allocated_qty = get_batch_item_issue_source_line_allocated_qty($source_type_id, $source_line_id, $bii_trans_line_id);
            $tran_unit_allocated_qty = $base_unit_allocated_qty / $biid_conversion_factor;
            qty_cell($tran_unit_allocated_qty, false, $dec);
            
            $tran_unit_available_qty = $tran_unit_pr_balance_qty - $tran_unit_allocated_qty;
            qty_cell($tran_unit_available_qty, false, $dec);                
                    
                    
            if(get_post($prefix.'_tran_unit') != ''){
                $current_tran_unit = get_post($prefix.'_tran_unit');
            }
            else{
                $current_tran_unit = $row['requested_unit'];
            }
                    
                    
            $alternate_unit = get_alternate_unit_info($item_code, $current_tran_unit);
            if($alternate_unit == false){
                $convertion_factor = 1;
            }
            else{
                $convertion_factor = $alternate_unit['convertion_factor'];
            } 
//            units_list_cells(null, $prefix.'_tran_unit', $item_code, $alternate_unit['alternate_unit'], false, true);
//            $tran_unit = get_post($prefix.'_tran_unit');
            
            $_POST[$prefix.'_tran_unit'] = $tran_unit_code;
            hidden($prefix.'_tran_unit', $tran_unit_code);
                    
            $suggested_issu_qty = 0;
            $line_ref = '';
            display_quantity_cells(get_post('guid'), $source_type_id, $line_ref, $item_code, 
                                    $row['stock_control_method'], $prefix.'_', $suggested_issu_qty, $tran_unit_code, 
                                    $dec, "items_table");
            
            $tran_qty = input_num($prefix.'_tran_qty');
            $tmp_convesion_factor = 1;
            get_convertion_factor2($item_code, $tran_unit_code, $tmp_convesion_factor);
            $base_unit_qty = $tran_qty * $tmp_convesion_factor;
            
            if(isset($_POST[$prefix.'_check_batch_issue_trans_line'])){
                $total_tran_unit_pr_selected_qty += $tran_qty;
            }

//            project_sites_list_cells(null, $prefix.'_project_site_id', $row['project_id'], null,'');
            
            if(!(isset($_POST[$prefix.'_movement_type']) && $_POST[$prefix.'_movement_type'] > 0)){
                $_POST[$prefix.'_movement_type'] = -1;
            }
//            movement_types_list_cells(null, $prefix.'_movement_type', ST_PR_ISSUE, null, '', true);
//                    
//            if (list_updated($prefix.'_movement_type')) {
//                unset($_POST[$prefix.'_linked_field_id']);
//            }
//            
//            $movement_type = get_movement_type($_POST[$prefix.'_movement_type']);
//            if(!is_array($movement_type)){
//                label_cell("");
//                hidden($prefix.'_linked_field_id', 0);
//            }
//            else{
//                switch ($movement_type['linked_field']) {
//                    case IMLF_CUSTOMER:
//                        customer_list_cells(null, $prefix.'_linked_field_id', null, '');
//                        break;
//                    case IMLF_SUPPLIER:
//                        supplier_list_cells(null, $prefix.'_linked_field_id', null, '');
//                        break;
//                    case IMLF_EMPLOYEE:
//                        employees_simple_list_cells(null, $prefix.'_linked_field_id', null, '');
//                        break;
//                    case IMLF_VEHICLE:
//                        vehicles_list_cells(null, $prefix.'_linked_field_id', null, '', true);
//                        break;
//                    case IMLF_ASSET:
//                        label_cell(asset_list($prefix.'_linked_field_id', null, ''));
//                        break;
//                    default:
//                        label_cell("");
//                        hidden($prefix.'_linked_field_id', 0);
//                        break;
//                }
//            }
//            
//            hidden($prefix.'_linked_field_type', $movement_type['linked_field']);                    

            if($target_line_id > 0){
                $target_link = get_supplier_trans_view_str($target_type_id, $target_type_no, $target_line_id."(".$target_type_no.")");
                label_cell($target_link, "colspan=2 align=center");
            }
            else{
                if(!($bii_trans_line_id > 0)){
                    check_cells(null, $prefix.'_check_batch_issue_trans_line', null, false);
                    label_cell("");
                }else{
                    label_cell("");
                    hidden($prefix.'_check_batch_issue_trans_line', 1);
                    delete_button_cell("Delete"."_biit_".$bii_trans_line_id, _("Delete"));  
                }                
            }

            $dec = 0;
				
	}
        
        

//        start_row();
//            label_cell("Total", 'colspan=8 align=right');
//            label_cell("Unit");
//            label_cell($tran_unit_name);
//            qty_cell($total_base_unit_selected_qty, false, 3);
//            label_cell(_(""));
//        end_row();
        
        start_row();
            label_cell($systypes_array[ST_PURCH_REQ]."s allocations (".$tran_unit_name.")", 'colspan=10 align=right'); 
            qty_cell($total_tran_unit_pr_selected_qty, false, 3);
            label_cell(_(""), 'colspan=2');
        end_row();
        start_row();
            $total_base_unit_mr_allocations = get_batch_item_issue_detail_line_allocated_qty($bii_line_id, ST_MATERIAL_REQ);
            label_cell($systypes_array[ST_MATERIAL_REQ]."s allocations (".$tran_unit_name.")", 'colspan=10 align=right'); 
            $total_tran_unit_mr_allocations = $total_base_unit_mr_allocations / $biid_conversion_factor;
            qty_cell($total_tran_unit_mr_allocations, false, 3);
            label_cell(_(""), 'colspan=2');
        end_row();
        start_row();
            $total_base_unit_dii_allocations = get_batch_item_issue_detail_line_allocated_qty($bii_line_id, ST_DAILY_ITEM_ISSUE);
            label_cell($systypes_array[ST_DAILY_ITEM_ISSUE]." allocation (".$tran_unit_name.")", 'colspan=10 align=right'); 
            $total_tran_unit_dii_allocations = $total_base_unit_dii_allocations / $biid_conversion_factor;
            qty_cell($total_tran_unit_dii_allocations, false, 3);
            label_cell(_(""), 'colspan=2');
        end_row();
        start_row();
            $tran_unit_total_allocations = $total_tran_unit_pr_selected_qty + $total_tran_unit_mr_allocations + $total_tran_unit_dii_allocations;
            label_cell("Total allocations (".$tran_unit_name.")", 'colspan=10 align=right'); 
            qty_cell($tran_unit_total_allocations, false, 3);
            label_cell(_(""), 'colspan=2');
        end_row();

    end_table();
	div_end();
        
//        hidden('type_id', $_POST['type_id']); 
//        hidden('line_id', $_POST['line_id']); 
}


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


function can_commit_batch_item_issue_pr_lines($source_type_id, $bii_line_id, $from_date, $to_date)
{
    $biid = get_batch_item_issue_details_record($bii_line_id);
    $tran_unit_biid_qty = $biid['tran_qty'];
    $item_code = $biid['stock_id'];
    $tran_unit_code = $biid['tran_unit'];
    
    $biid_conversion_factor = 1;
    get_convertion_factor2($item_code, $tran_unit_code, $biid_conversion_factor);
    
    $total_tran_unit_selected_qty = 0;
    
    $result = get_batch_item_issue_pr_lines($bii_line_id, $from_date, $to_date);
    while ($row = db_fetch($result)) {
        $prefix = $row['id']."_".$row['pr_po_line_id']."_".$row['po_detail_id'];
        $pr_line_id = $row['id'];
        
        if(!isset($_POST[$prefix.'_check_batch_issue_trans_line'])){
            continue;
        }
        
        $tran_unit = $_POST[$prefix.'_tran_unit'];
        if(!(strlen($tran_unit) > 0)){
            $message = "PR Line No ".$pr_line_id." : Please select a transaction unit";
            $set_focus = $prefix.'_tran_unit';
            return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
        }
        
        if(!check_num($prefix.'_tran_qty', 0.0001)){
            $message = "PR Line No ".$pr_line_id." : Quantity entered should be greater than zero and positive.";
            $set_focus = $prefix.'_tran_qty';
            return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
        }
        
        
            
        $tran_unit_pr_req_qty = $row['quantity'] / $biid_conversion_factor;                  
        $tran_unit_pr_issued_qty = $row['issued_qty'] / $biid_conversion_factor;            
        $tran_unit_pr_returned_qty = $row['returned_qty'] / $biid_conversion_factor;
            
//            $tran_unit_po_qty = $row['po_ordered_qty'] / $conversion_factor;
//            qty_cell($tran_unit_po_qty, false, $dec);
//            
//            $tran_unit_po_received_qty = $row['po_received_qty'] / $conversion_factor;
//            qty_cell($tran_unit_po_received_qty, false, $dec);  
            
        $tran_unit_pr_balance_qty = $tran_unit_pr_req_qty - ($tran_unit_pr_issued_qty - $tran_unit_pr_returned_qty);

        $bii_trans_line_id = 0;
        $source_line_id = $row['id'];
        $source_type_no = $row['trans_no'];
        $trans_row = get_batch_item_issue_transaction_record($bii_line_id, $source_type_id, $source_type_no, $source_line_id);
        if(is_array($trans_row)){
            $bii_trans_line_id = $trans_row['id'];
        }

        $base_unit_allocated_qty = get_batch_item_issue_source_line_allocated_qty($source_type_id, $source_line_id, $bii_trans_line_id);
        $tran_unit_allocated_qty = $base_unit_allocated_qty / $biid_conversion_factor; 

        $tran_unit_available_qty = $tran_unit_pr_balance_qty - $tran_unit_allocated_qty; 
            
        $tran_qty = input_num($prefix.'_tran_qty');
        
        if($tran_qty - $tran_unit_available_qty > 0.000001){
            $message = "PR Line No ".$pr_line_id." : Entered quantity is higher than the available quantity of ".number_format($tran_unit_available_qty, 2).".";
            $set_focus = $prefix.'_tran_qty';
            return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
        }

        $total_tran_unit_selected_qty += $tran_qty;
    }
    
    $base_unit_dii_allocations = get_batch_item_issue_detail_line_allocated_qty($bii_line_id, ST_DAILY_ITEM_ISSUE);
    $base_unit_mr_allocations = get_batch_item_issue_detail_line_allocated_qty($bii_line_id, ST_MATERIAL_REQ);
    $base_unit_other_allocations = $base_unit_dii_allocations + $base_unit_mr_allocations;
    $tran_unit_other_allocations = $base_unit_other_allocations / $biid_conversion_factor; 
    
    $total_tran_unit_selected_qty += $tran_unit_other_allocations;
        
    if($total_tran_unit_selected_qty - $tran_unit_biid_qty > 0.0000001){
        $message = "Total selected quantity of ".number_format($total_tran_unit_selected_qty, 2)." is higher than the required quantity of ".number_format($tran_unit_biid_qty, 2).".";
        $set_focus = "";
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
        
    return array("success"=>true, "set_focus"=>"", "message"=>"");
    
}

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

function handle_save_batch_item_issue_pr_lines($source_type_id, $bii_line_id, $from_date, $to_date)
{
    global $systypes_array;
    
    $biid = get_batch_item_issue_details_record($bii_line_id);
    $type = $biid['type'];
    $trans_no = $biid['trans_no'];
    $biih = get_batch_item_issue_header_record_type_trans_no($type, $trans_no);
    
    if($source_type_id == ST_PURCH_REQ){
        $target_trans_type = ST_PR_ISSUE;
    }elseif($source_type_id == ST_MATERIAL_REQ){
        $target_trans_type = ST_MATERIAL_REQ;
    }elseif($source_type_id == ST_DAILY_ITEM_ISSUE){
        $target_trans_type = ST_DAILY_ITEM_ISSUE;
    }
    
    $matching_movement_type = get_matching_movement_type(ST_DAILY_ITEM_ISSUE, $biid['movement_type'], $biid['linked_field_type'], $target_trans_type);
    if(!is_array($matching_movement_type)){
        $source_movement_type = get_movement_type($biid['movement_type']);
        $message = "No matching movement type found for the transaction type ".$systypes_array[$target_trans_type]." for the source movement type ".$source_movement_type['name'].".";
        display_error($message);
        return;
    }
    
    $movement_type_id = $matching_movement_type['id'];    
    
    if($source_type_id == ST_PURCH_REQ){
        $msg_cc = can_commit_batch_item_issue_pr_lines($source_type_id, $bii_line_id, $from_date, $to_date);
    }elseif($source_type_id == ST_MATERIAL_REQ){
        $msg_cc = can_commit_batch_item_issue_mr_lines($source_type_id, $bii_line_id, $from_date, $to_date);
    }elseif($source_type_id == ST_DAILY_ITEM_ISSUE){
        $msg_cc = can_commit_batch_item_issue_dii_line($source_type_id, $bii_line_id, $from_date, $to_date);
    }
    
    if($msg_cc["success"] !== true){
        display_error($msg_cc["message"]);
        set_focus($msg_cc["set_focus"]);
        return;
    }
    
    begin_transaction();
    
    if($source_type_id == ST_DAILY_ITEM_ISSUE){
        $save_msg = save_batch_item_issue_dii_line($biih, $biid, $source_type_id, $bii_line_id, $from_date, $to_date, $movement_type_id);
    }
    else{
        $save_msg = save_batch_item_issue_pr_lines($biih, $biid, $source_type_id, $bii_line_id, $from_date, $to_date, $movement_type_id);
    }
    
    if($save_msg["success"] !== true){
        cancel_transaction();
        display_error($save_msg["message"]);
        return;
    }else{
        commit_transaction();
        display_notification($save_msg["message"]);
    }
}

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


function save_batch_item_issue_pr_lines($biih, $biid, $source_type_id, $bii_line_id, $from_date, $to_date, $movement_type_id)
{
    global $systypes_array;
//    $biid = get_batch_item_issue_details_record($bii_line_id);
    
    $linked_field_type = $biid['linked_field_type'];
    $linked_field_id = $biid['linked_field_id'];    
    $type = $biid['type'];
    $trans_no = $biid['trans_no'];
    
    $row_state = $biih['row_state'];     
    
    delete_batch_item_issue_detail_transactions($type, $trans_no, $source_type_id, $bii_line_id);
    
//    $c = get_batch_item_issue_transactions_records_count($bii_line_id, $from_date, $to_date);
//    if($c != 0){
//        $message = "Line no ".$bii_line_id." : Could not delete all the selected transaction lines";
//        return array("success"=>false, "trans_no"=>"", "message"=>$message);
//    }
    
    if($source_type_id == ST_PURCH_REQ){
        $result = get_batch_item_issue_pr_lines($bii_line_id, $from_date, $to_date);
    }elseif($source_type_id == ST_MATERIAL_REQ){
        $result = get_batch_item_issue_mr_lines($bii_line_id, $from_date, $to_date);
    }
    
    while ($row = db_fetch($result)) {
        $prefix = $row['id']."_".$row['pr_po_line_id']."_".$row['po_detail_id'];
        
        if(!isset($_POST[$prefix.'_check_batch_issue_trans_line'])){
            continue;
        }
        
        $source_type_no = $row['trans_no'];
        $source_line_id = $row['id'];
        $po_line_id = $row['po_detail_id'];
        
        $tran_unit = $_POST[$prefix.'_tran_unit'];
        $tran_qty = input_num($prefix.'_tran_qty');
        
        $item_code = $row['item_code'];
        $conversion_factor = 1;
        get_convertion_factor2($item_code, $tran_unit, $conversion_factor);
        $base_unit_qty = $tran_qty * $conversion_factor;
        
//        $movement_type_id = $_POST[$prefix.'_movement_type'];
//        $movement_type = get_movement_type($movement_type_id);
//        $linked_field_type = $_POST[$prefix.'_linked_field_type'];  
//        $linked_field_id = $_POST[$prefix.'_linked_field_id'];  
        $project_site_id = $biid['project_site_id'];  
        if($project_site_id < 0){
            $project_site_id = 0;
        }
                    
        add_batch_item_issue_transaction($type, $trans_no, $bii_line_id, 
                        $source_type_id, $source_type_no, $source_line_id, $po_line_id,
                        $tran_unit, $tran_qty, 
                        $conversion_factor, $base_unit_qty, 
                        $movement_type_id, $linked_field_type, $linked_field_id, 
                        $project_site_id);
        
    }
    
    $urs_msg = update_batch_item_issue_row_status($type, $trans_no, $row_state);
    if($urs_msg["success"] !== true){
        return $urs_msg;
    }else{
        return array("success" => true, "message" => "Selected ".$systypes_array[$source_type_id]." lines have been updated.");
    }
}

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

function save_batch_item_issue_dii_line($biih, $biid, $source_type_id, $bii_line_id, $from_date, $to_date, $movement_type_id)
{
    global $systypes_array;
//    $biid = get_batch_item_issue_details_record($bii_line_id);
    
    $linked_field_type = $biid['linked_field_type'];
    $linked_field_id = $biid['linked_field_id'];    
    $type = $biid['type'];
    $trans_no = $biid['trans_no'];
    
    $row_state = $biih['row_state'];     
    
    delete_batch_item_issue_detail_transactions($type, $trans_no, $source_type_id, $bii_line_id);
    
//    $c = get_batch_item_issue_transactions_records_count($bii_line_id, $from_date, $to_date);
//    if($c != 0){
//        $message = "Line no ".$bii_line_id." : Could not delete all the selected transaction lines";
//        return array("success"=>false, "trans_no"=>"", "message"=>$message);
//    }
    
    $prefix = $biid['id']."_";

    $source_type_no = $biid['trans_no'];
    $source_line_id = $biid['id'];
    $po_line_id = 0;

    $tran_unit = $biid['tran_unit'];
    $tran_qty = input_num($prefix.'_tran_qty');
    $tran_unit_cost = input_num($prefix.'tran_unit_cost');
    $task_id = $_POST[$prefix.'task_id'];
    $item_code = $biid['stock_id'];
    $conversion_factor = 1;
    get_convertion_factor2($item_code, $tran_unit, $conversion_factor);
    $base_unit_qty = $tran_qty * $conversion_factor;
        
//        $movement_type_id = $_POST[$prefix.'_movement_type'];
//        $movement_type = get_movement_type($movement_type_id);
//        $linked_field_type = $_POST[$prefix.'_linked_field_type'];  
//        $linked_field_id = $_POST[$prefix.'_linked_field_id'];  
    
    $project_site_id = $biid['project_site_id'];  
    if($project_site_id < 0){
        $project_site_id = 0;
    }
                    
        
    add_batch_item_issue_transaction($type, $trans_no, $bii_line_id, 
                        $source_type_id, $source_type_no, $source_line_id, $po_line_id,
                        $tran_unit, $tran_qty, 
                        $conversion_factor, $base_unit_qty, 
                        $movement_type_id, $linked_field_type, $linked_field_id, 
                        $project_site_id, $task_id, $tran_unit_cost);
        
    
    
    $urs_msg = update_batch_item_issue_row_status($type, $trans_no, $row_state);
    if($urs_msg["success"] !== true){
        return $urs_msg;
    }else{
        return array("success" => true, "message" => "Selected ".$systypes_array[$source_type_id]." lines have been updated.");
    }
}

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

function handle_delete_batch_item_issue_detail_transaction($id){

    delete_batch_item_issue_detail_transaction($id);
    
}

function display_batch_item_issue_mr_lines($source_type_id, $bii_line_id, $from_date, $to_date)
{
    global $Ajax, $systypes_array;
        
    $biid_row = get_batch_item_issue_details_record($bii_line_id);
    $tran_unit_code = $biid_row['tran_unit'];
    $item_code = $biid_row['stock_id'];
    
    $tran_unit_row = get_item_unit($tran_unit_code);
    $tran_unit_name = $tran_unit_row['name'];
    
    $biid_conversion_factor = 1;
    get_convertion_factor2($item_code, $tran_unit_code, $biid_conversion_factor);

    $Ajax->activate('items_table');
    
    display_heading($systypes_array[$source_type_id]);
        
    div_start('items_table');
    start_table(TABLESTYLE, "width=98%");
    

    $th = array("MR<br>Line", _("Item"), 
                _("MR.<br>Qty"),  _("MR<br>Issued Qty."), _("MR<br>Returned Qty"), 
                _("Allocated<br>Qty"),  _("Available<br>Qty"),
                _("Issuing<br>Qty"), "", "");        

	//if ( count($order->line_items)) $th[] = '';

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

	$id = find_submit('Edit');
        
        $result = get_batch_item_issue_mr_lines($bii_line_id, $from_date, $to_date);
        while ($row = db_fetch($result)) {

            $prefix = $row['id']."_".$row['pr_po_line_id']."_".$row['po_detail_id'];
            $bii_trans_line_id = $target_type_id = $target_type_no = $target_line_id = 0;
            
            $source_type_no = $row['mr_no'];
            $source_line_id = $row['id'];
            $trans_row = get_batch_item_issue_transaction_record($bii_line_id, $source_type_id, $source_type_no, $source_line_id);
            if(is_array($trans_row)){
                $bii_trans_line_id = $trans_row['id'];
                $target_type_id = $trans_row['target_type_id'];
                $target_type_no = $trans_row['target_type_no'];
                $target_line_id = $trans_row['target_line_id'];
                
                set_post_not_set_string($prefix.'_tran_unit', $trans_row['tran_unit']);
                set_post_not_set_numeric($prefix.'_tran_qty', $trans_row['tran_qty']);
                $_POST[$prefix.'_tran_qty'] = $trans_row['tran_qty'];
                set_post_not_set_numeric($prefix.'_project_site_id', $trans_row['project_site_id']);
//                set_post_not_set_numeric($prefix.'_movement_type', $trans_row['movement_type']);
//                set_post_not_set_numeric($prefix.'_linked_field_type', $trans_row['linked_field_type']);
//                set_post_not_set_numeric($prefix.'_linked_field_id', $trans_row['linked_field_id']);
                set_post_not_set_numeric($prefix.'_check_batch_issue_trans_line', 1);
            }
            
            

            $total += ($row['standard_cost'] * $row['requested_qty']);

            alt_table_row_color($k);                    

            $ids_text = get_trans_view_str(ST_MATERIAL_REQ, $row['mr_no'], $row['id']."(".$row['mr_no'].")");
//            $pr_allocs_link = get_pr_allocation_ids_string($row['po_detail_id'], $row['pr_po_line_id']);
//            $po_link = get_supplier_trans_view_str(ST_PURCHORDER, $row['po_no'], $row['pr_po_line_id']."(".$row['po_no'].")");

            label_cell($ids_text, 'align=center');
//            label_cell($pr_allocs_link, 'align=center');
//            label_cell($po_link, 'align=center');

            $item_description = get_stock_item_formatted_name($item_code, $row['descr_editable'], $row['stock_master_description']);
            label_cell($item_description);
                    
//            label_cell($row['requested_unit_name']);
            
            $dec = get_qty_dec($item_code);
//                    qty_cell($stock_item->requested_qty/$stock_item->convertion_factor, false, $dec);
            
            
            
            $tran_unit_mr_req_qty = $row['requested_qty'] / $biid_conversion_factor;
            qty_cell($tran_unit_mr_req_qty, false, $dec);
            
//            $conversion_factor = $row['req_unit_conv_factor'];
            
            $tran_unit_mr_issued_qty = $row['issued_qty'] / $biid_conversion_factor;
            qty_cell($tran_unit_mr_issued_qty, false, $dec);
            
            $tran_unit_mr_returned_qty = $row['returned_qty'] / $biid_conversion_factor;
            qty_cell($tran_unit_mr_returned_qty, false, $dec);              
            
            $tran_unit_pr_balance_qty = $tran_unit_mr_req_qty - ($tran_unit_mr_issued_qty - $tran_unit_mr_returned_qty);
            
            $base_unit_allocated_qty = get_batch_item_issue_source_line_allocated_qty($source_type_id, $source_line_id, $bii_trans_line_id);
            $tran_unit_allocated_qty = $base_unit_allocated_qty / $biid_conversion_factor;
            qty_cell($tran_unit_allocated_qty, false, $dec);
            
            $tran_unit_available_qty = $tran_unit_pr_balance_qty - $tran_unit_allocated_qty;
            qty_cell($tran_unit_available_qty, false, $dec);                
                    
                    
            if(get_post($prefix.'_tran_unit') != ''){
                $current_tran_unit = get_post($prefix.'_tran_unit');
            }
            else{
                $current_tran_unit = $row['requested_unit'];
            }
                    
                    
            $alternate_unit = get_alternate_unit_info($item_code, $current_tran_unit);
            if($alternate_unit == false){
                $convertion_factor = 1;
            }
            else{
                $convertion_factor = $alternate_unit['convertion_factor'];
            } 
//            units_list_cells(null, $prefix.'_tran_unit', $item_code, $alternate_unit['alternate_unit'], false, true);
//            $tran_unit = get_post($prefix.'_tran_unit');
            
            $_POST[$prefix.'_tran_unit'] = $tran_unit_code;
            hidden($prefix.'_tran_unit', $tran_unit_code);
                    
            $suggested_issu_qty = 0;
            $line_ref = '';
            display_quantity_cells(get_post('guid'), $source_type_id, $line_ref, $item_code, 
                                    $row['stock_control_method'], $prefix.'_', $suggested_issu_qty, $tran_unit_code, 
                                    $dec, "items_table");
            
            $tran_qty = input_num($prefix.'_tran_qty');
            $tmp_convesion_factor = 1;
            get_convertion_factor2($item_code, $tran_unit_code, $tmp_convesion_factor);
            $base_unit_qty = $tran_qty * $tmp_convesion_factor;
            
            if(isset($_POST[$prefix.'_check_batch_issue_trans_line'])){
                $total_tran_unit_mr_selected_qty += $tran_qty;
            }

//            project_sites_list_cells(null, $prefix.'_project_site_id', $row['project_id'], null,'');
            
            if(!(isset($_POST[$prefix.'_movement_type']) && $_POST[$prefix.'_movement_type'] > 0)){
                $_POST[$prefix.'_movement_type'] = -1;
            }                 

            if($target_line_id > 0){
                $target_link = get_supplier_trans_view_str($target_type_id, $target_type_no, $target_line_id."(".$target_type_no.")");
                label_cell($target_link, "colspan=2 align=center");
            }
            else{
                if(!($bii_trans_line_id > 0)){
                    check_cells(null, $prefix.'_check_batch_issue_trans_line', null, false);
                    label_cell("");
                }else{
                    label_cell("");
                    hidden($prefix.'_check_batch_issue_trans_line', 1);
                    delete_button_cell("Delete"."_biit_".$bii_trans_line_id, _("Delete"));  
                }                
            }

            $dec = 0;	
	}
        
        start_row();
            label_cell($systypes_array[ST_MATERIAL_REQ]."s allocations (".$tran_unit_name.")", 'colspan=7 align=right'); 
            qty_cell($total_tran_unit_mr_selected_qty, false, 3);
            label_cell(_(""), 'colspan=2');
        end_row();
        start_row();
            $total_base_unit_pr_allocations = get_batch_item_issue_detail_line_allocated_qty($bii_line_id, ST_PURCH_REQ);
            label_cell($systypes_array[ST_PURCH_REQ]."s allocations (".$tran_unit_name.")", 'colspan=7 align=right'); 
            $total_tran_unit_pr_allocations = $total_base_unit_pr_allocations / $biid_conversion_factor;
            qty_cell($total_tran_unit_pr_allocations, false, 3);
            label_cell(_(""), 'colspan=2');
        end_row();
        start_row();
            $total_base_unit_dii_allocations = get_batch_item_issue_detail_line_allocated_qty($bii_line_id, ST_DAILY_ITEM_ISSUE);
            label_cell($systypes_array[ST_DAILY_ITEM_ISSUE]." allocation (".$tran_unit_name.")", 'colspan=7 align=right'); 
            $total_tran_unit_dii_allocations = $total_base_unit_dii_allocations / $biid_conversion_factor;
            qty_cell($total_tran_unit_dii_allocations, false, 3);
            label_cell(_(""), 'colspan=2');
        end_row();
        start_row();
            $tran_unit_total_allocations = $total_tran_unit_mr_selected_qty + $total_tran_unit_pr_allocations + $total_tran_unit_dii_allocations;
            label_cell("Total allocations (".$tran_unit_name.")", 'colspan=7 align=right'); 
            qty_cell($tran_unit_total_allocations, false, 3);
            label_cell(_(""), 'colspan=2');
        end_row();

    end_table();
	div_end();
        
//        hidden('type_id', $_POST['type_id']); 
//        hidden('line_id', $_POST['line_id']); 
}

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


function can_commit_batch_item_issue_mr_lines($source_type_id, $bii_line_id, $from_date, $to_date)
{
    $biid = get_batch_item_issue_details_record($bii_line_id);
    $tran_unit_biid_qty = $biid['tran_qty'];
    $item_code = $biid['stock_id'];
    $tran_unit_code = $biid['tran_unit'];
    
    $biid_conversion_factor = 1;
    get_convertion_factor2($item_code, $tran_unit_code, $biid_conversion_factor);
    
    $total_tran_unit_selected_qty = 0;
    
    $result = get_batch_item_issue_mr_lines($bii_line_id, $from_date, $to_date);
    while ($row = db_fetch($result)) {
//        $prefix = $row['id']."_";
        $prefix = $row['id']."_".$row['pr_po_line_id']."_".$row['po_detail_id'];
        $mr_line_id = $row['id'];
        
        if(!isset($_POST[$prefix.'_check_batch_issue_trans_line'])){
            continue;
        }
        
        $tran_unit = $_POST[$prefix.'_tran_unit'];
        if(!(strlen($tran_unit) > 0)){
            $message = "MR Line No ".$mr_line_id." : Please select a transaction unit";
            $set_focus = $prefix.'_tran_unit';
            return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
        }
        
        if(!check_num($prefix.'_tran_qty', 0.0001)){
            $message = "MR Line No ".$mr_line_id." : Quantity entered should be greater than zero and positive.";
            $set_focus = $prefix.'_tran_qty';
            return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
        }
        
        
            
        $tran_unit_mr_req_qty = $row['requested_qty'] / $biid_conversion_factor;                  
        $tran_unit_mr_issued_qty = $row['issued_qty'] / $biid_conversion_factor;            
        $tran_unit_mr_returned_qty = $row['returned_qty'] / $biid_conversion_factor;
            
//            $tran_unit_po_qty = $row['po_ordered_qty'] / $conversion_factor;
//            qty_cell($tran_unit_po_qty, false, $dec);
//            
//            $tran_unit_po_received_qty = $row['po_received_qty'] / $conversion_factor;
//            qty_cell($tran_unit_po_received_qty, false, $dec);  
            
        $tran_unit_mr_balance_qty = $tran_unit_mr_req_qty - ($tran_unit_mr_issued_qty - $tran_unit_mr_returned_qty);

        $bii_trans_line_id = 0;
        $source_line_id = $row['id'];
        $source_type_no = $row['trans_no'];
        $trans_row = get_batch_item_issue_transaction_record($bii_line_id, $source_type_id, $source_type_no, $source_line_id);
        if(is_array($trans_row)){
            $bii_trans_line_id = $trans_row['id'];
        }

        $base_unit_allocated_qty = get_batch_item_issue_source_line_allocated_qty($source_type_id, $source_line_id, $bii_trans_line_id);
        $tran_unit_allocated_qty = $base_unit_allocated_qty / $biid_conversion_factor; 

        $tran_unit_available_qty = $tran_unit_mr_balance_qty - $tran_unit_allocated_qty; 
            
        $tran_qty = input_num($prefix.'_tran_qty');
        
        if($tran_qty - $tran_unit_available_qty > 0.000001){
            $message = "MR Line No ".$mr_line_id." : Entered quantity is higher than the available quantity of ".number_format($tran_unit_available_qty, 2).".";
            $set_focus = $prefix.'_tran_qty';
            return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
        }

        $total_tran_unit_selected_qty += $tran_qty;
    }
    
    $base_unit_dii_allocations = get_batch_item_issue_detail_line_allocated_qty($bii_line_id, ST_DAILY_ITEM_ISSUE);
    $base_unit_pr_allocations = get_batch_item_issue_detail_line_allocated_qty($bii_line_id, ST_PURCH_REQ);
    $base_unit_other_allocations = $base_unit_dii_allocations + $base_unit_pr_allocations;
    $tran_unit_other_allocations = $base_unit_other_allocations / $biid_conversion_factor; 
    
    $total_tran_unit_selected_qty += $tran_unit_other_allocations;
    
    if($total_tran_unit_selected_qty - $tran_unit_biid_qty > 0.0000001){
        $message = "Total selected quantity of ".number_format($total_tran_unit_selected_qty, 2)." is higher than the required quantity of ".number_format($tran_unit_biid_qty, 2).".";
        $set_focus = "";
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
        
    return array("success"=>true, "set_focus"=>"", "message"=>"");
    
}
//---------------------------------------------------------------------------------


function can_commit_batch_item_issue_dii_line($source_type_id, $bii_line_id, $from_date, $to_date)
{
    $biid = get_batch_item_issue_details_record($bii_line_id);
    $tran_unit_biid_qty = $biid['tran_qty'];
    $item_code = $biid['stock_id'];
    $tran_unit_code = $biid['tran_unit'];
    $daily_item_type = $biid['daily_item_type'];
    
    $biid_conversion_factor = 1;
    get_convertion_factor2($item_code, $tran_unit_code, $biid_conversion_factor);
    
//    $total_tran_unit_selected_qty = 0;
    
//    $result = get_batch_item_issue_mr_lines($bii_line_id, $from_date, $to_date);
//    while ($row = db_fetch($result)) {
        $prefix = $biid['id']."_";
        
        if(!check_num($prefix.'_tran_qty', 0.0001)){
            $message = "Quantity entered should be greater than zero and positive.";
            $set_focus = $prefix.'_tran_qty';
            return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
        }
        
        if($daily_item_type == NSIT_NON_STOCK_ITEM){
            if(!(input_num($prefix.'tran_unit_cost') > 0.0001)){
                $message = "Unit cost should be greater than zero and positive.";
                $set_focus = $prefix.'tran_unit_cost';
                return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
            }
        }
        
        if(!($_POST[$prefix.'task_id'] > 0)){
            $message = "Please select a budget.";
            $set_focus = $prefix.'task_id';
            return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
        }

        $bii_trans_line_id = 0;
        $source_line_id = $biid['id'];
        $source_type_no = $biid['trans_no'];
        $trans_row = get_batch_item_issue_transaction_record($bii_line_id, $source_type_id, $source_type_no, $source_line_id);
        if(is_array($trans_row)){
            $bii_trans_line_id = $trans_row['id'];
        }

        $base_unit_pr_allocations = get_batch_item_issue_detail_line_allocated_qty($bii_line_id, ST_PURCH_REQ);
        $base_unit_mr_allocations = get_batch_item_issue_detail_line_allocated_qty($bii_line_id, ST_MATERIAL_REQ);
        $base_unit_other_allocations = $base_unit_pr_allocations + $base_unit_mr_allocations;
        $tran_unit_other_allocations = $base_unit_other_allocations / $biid_conversion_factor; 
        
//        $base_unit_allocated_qty = get_batch_item_issue_source_line_allocated_qty($source_type_id, $source_line_id, $bii_trans_line_id);
//        $tran_unit_allocated_qty = $base_unit_allocated_qty / $biid_conversion_factor; 
            
        $tran_qty = input_num($prefix.'_tran_qty');

        $total_tran_unit_selected_qty = $tran_qty + $tran_unit_other_allocations;
//    }
    
    if($total_tran_unit_selected_qty - $tran_unit_biid_qty > 0.0000001){
        $message = "Total selected quantity of ".number_format($total_tran_unit_selected_qty, 2)." is higher than the required quantity of ".number_format($tran_unit_biid_qty, 2).".";
        $set_focus = "";
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
        
    return array("success"=>true, "set_focus"=>"", "message"=>"");
    
}

function view_batch_item_issue_pr_lines($source_type_id, $bii_line_id, $from_date, $to_date)
{
    global $Ajax, $systypes_array;
    
    $result = get_batch_item_issue_pr_transactions($bii_line_id);
    $n = db_num_rows($result);
    if($n == 0){
        br(1);
        display_note("No ".$systypes_array[ST_PURCH_REQ]." lines selected");
        br(1);
        return;
    }
        
    $biid_row = get_batch_item_issue_details_record($bii_line_id);
    $tran_unit_code = $biid_row['tran_unit'];
    $item_code = $biid_row['stock_id'];
    
    $tran_unit_row = get_item_unit($tran_unit_code);
    $tran_unit_name = $tran_unit_row['name'];
    
    $biid_conversion_factor = 1;
    get_convertion_factor2($item_code, $tran_unit_code, $biid_conversion_factor);

    $Ajax->activate('items_table');
    
    display_heading2($systypes_array[$source_type_id]."s");
        
    div_start('items_table');
    start_table(TABLESTYLE, "width=98%");
    

    $th = array("Id", "PR<br>Line", "PO<br>Line",_("Item"), 
                _("PR.<br>Qty"),  _("PR<br>Issued<br>Qty."), _("PR<br>Returned<br>Qty"), 
                _("Allocated"),  _("Available"),
                _("Issuing<br>Qty"), _("Job"), "");        

	//if ( count($order->line_items)) $th[] = '';

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

	$id = find_submit('Edit');
        
//        $result = get_batch_item_issue_pr_transactions($bii_line_id);
        while ($row = db_fetch($result)) {

//            $prefix = $row['id']."_".$row['pr_po_line_id']."_".$row['po_detail_id'];
            $bii_trans_line_id = 0;
            
            $source_type_no = $row['trans_no'];
            $source_line_id = $row['id'];

            $total += ($row['standard_cost'] * $row['reqested_qty']);

            alt_table_row_color($k);                    

            label_cell($row['biit_id'], 'align=center');
            
            $ids_text = get_trans_view_str(ST_PURCH_REQ, $row['trans_no'], $row['id']."(".$row['trans_no'].")");
//            $pr_allocs_link = get_pr_allocation_ids_string($row['po_detail_id'], $row['pr_po_line_id']);
            $po_link = get_supplier_trans_view_str(ST_PURCHORDER, $row['po_no'], $row['po_line_id']."(".$row['po_no'].")");

            label_cell($ids_text, 'align=center');
//            label_cell($pr_allocs_link, 'align=center');
            label_cell($po_link, 'align=center');

            $item_description = get_stock_item_formatted_name($item_code, $row['descr_editable'], $row['stock_master_description'], $row['item_trans_description']);
            label_cell($item_description);
                    
//            label_cell($row['requested_unit_name']);
            
            $dec = get_qty_dec($item_code);
//                    qty_cell($stock_item->requested_qty/$stock_item->convertion_factor, false, $dec);
            
            
            
            $tran_unit_pr_req_qty = $row['reqested_qty'] / $biid_conversion_factor;
            qty_cell($tran_unit_pr_req_qty, false, $dec);
            
//            $conversion_factor = $row['req_unit_conv_factor'];
            
            $tran_unit_pr_issued_qty = $row['issued_qty'] / $biid_conversion_factor;
            qty_cell($tran_unit_pr_issued_qty, false, $dec);
            
            $tran_unit_pr_returned_qty = $row['returned_qty'] / $biid_conversion_factor;
            qty_cell($tran_unit_pr_returned_qty, false, $dec);
              
            
            $tran_unit_pr_balance_qty = $tran_unit_pr_req_qty - ($tran_unit_pr_issued_qty - $tran_unit_pr_returned_qty);
            
            $base_unit_allocated_qty = get_batch_item_issue_source_line_allocated_qty($source_type_id, $source_line_id, $bii_trans_line_id);
            $tran_unit_allocated_qty = $base_unit_allocated_qty / $biid_conversion_factor;
            qty_cell($tran_unit_allocated_qty, false, $dec);
            
            $tran_unit_available_qty = $tran_unit_pr_balance_qty - $tran_unit_allocated_qty;
            qty_cell($tran_unit_available_qty, false, $dec);                
                    
                    

                $current_tran_unit = $row['requested_unit'];
            
                    
                    
            $alternate_unit = get_alternate_unit_info($item_code, $current_tran_unit);
            if($alternate_unit == false){
                $convertion_factor = 1;
            }
            else{
                $convertion_factor = $alternate_unit['convertion_factor'];
            } 
            
            $tran_qty = $row['tran_qty'];
            qty_cell($tran_qty, false, $dec);
            
            $tmp_convesion_factor = 1;
            get_convertion_factor2($item_code, $tran_unit_code, $tmp_convesion_factor);
            $base_unit_qty = $tran_qty * $tmp_convesion_factor;
            

            $total_tran_unit_selected_qty += $tran_qty;
            

            label_cell($row['project_site_description']);
            
            if($row['target_line_id'] > 0){
                $target_link = get_supplier_trans_view_str($row['target_type_id'], $row['target_type_no'], $row['target_line_id']."(".$row['target_type_no'].")");
                label_cell($target_link, "colspan=2 align=center");
            }else{
                label_cell("");
            }
                              
				
	}
        
        

        start_row();
            label_cell("Total (".$tran_unit_name.")", 'colspan=9 align=right'); 
            qty_cell($total_tran_unit_selected_qty, false, 3);
            label_cell(_(""), "colspan=2");
        end_row();

    end_table();
	div_end();
        
        return $total_tran_unit_selected_qty;
        
//        hidden('type_id', $_POST['type_id']); 
//        hidden('line_id', $_POST['line_id']); 
}

function view_batch_item_issue_mr_lines($source_type_id, $bii_line_id, $from_date, $to_date)
{
    global $Ajax, $systypes_array;
    
    $result = get_batch_item_issue_mr_transactions($bii_line_id);
    $n = db_num_rows($result);
    if($n == 0){
        br(1);
        display_note("No ".$systypes_array[ST_MATERIAL_REQ]." lines selected");
        br(1);
        return;
    }
        
    $biid_row = get_batch_item_issue_details_record($bii_line_id);
    $tran_unit_code = $biid_row['tran_unit'];
    $item_code = $biid_row['stock_id'];
    
    $tran_unit_row = get_item_unit($tran_unit_code);
    $tran_unit_name = $tran_unit_row['name'];
    
    $biid_conversion_factor = 1;
    get_convertion_factor2($item_code, $tran_unit_code, $biid_conversion_factor);

    $Ajax->activate('items_table');
    
    display_heading2($systypes_array[$source_type_id]."s");
        
    div_start('items_table');
    start_table(TABLESTYLE, "width=98%");
    

    $th = array("Id", "MR<br>Line", _("Item"), 
                _("MR.<br>Qty"),  _("MR<br>Issued<br>Qty."), _("MR<br>Returned<br>Qty"), 
                _("Allocated"),  _("Available"),
                _("Issuing<br>Qty"), _("Job"), "");        

	//if ( count($order->line_items)) $th[] = '';

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

	$id = find_submit('Edit');
        
//        $result = get_batch_item_issue_pr_transactions($bii_line_id);
        while ($row = db_fetch($result)) {

//            $prefix = $row['id']."_".$row['pr_po_line_id']."_".$row['po_detail_id'];
            $bii_trans_line_id = 0;
            
            $source_type_no = $row['trans_no'];
            $source_line_id = $row['id'];

            $total += ($row['standard_cost'] * $row['requested_qty']);

            alt_table_row_color($k);                    

            label_cell($row['biit_id'], 'align=center');
            
            $ids_text = get_trans_view_str(ST_PURCH_REQ, $row['trans_no'], $row['id']."(".$row['trans_no'].")");
//            $pr_allocs_link = get_pr_allocation_ids_string($row['po_detail_id'], $row['pr_po_line_id']);
//            $po_link = get_supplier_trans_view_str(ST_PURCHORDER, $row['po_no'], $row['po_line_id']."(".$row['po_no'].")");

            label_cell($ids_text, 'align=center');
//            label_cell($pr_allocs_link, 'align=center');
//            label_cell($po_link, 'align=center');

            $item_description = get_stock_item_formatted_name($item_code, $row['descr_editable'], $row['stock_master_description']);
            label_cell($item_description);
                    
//            label_cell($row['requested_unit_name']);
            
            $dec = get_qty_dec($item_code);
//                    qty_cell($stock_item->requested_qty/$stock_item->convertion_factor, false, $dec);
            
            
            
            $tran_unit_pr_req_qty = $row['requested_qty'] / $biid_conversion_factor;
            qty_cell($tran_unit_pr_req_qty, false, $dec);
            
//            $conversion_factor = $row['req_unit_conv_factor'];
            
            $tran_unit_pr_issued_qty = $row['issued_qty'] / $biid_conversion_factor;
            qty_cell($tran_unit_pr_issued_qty, false, $dec);
            
            $tran_unit_pr_returned_qty = $row['returned_qty'] / $biid_conversion_factor;
            qty_cell($tran_unit_pr_returned_qty, false, $dec);
              
            
            $tran_unit_pr_balance_qty = $tran_unit_pr_req_qty - ($tran_unit_pr_issued_qty - $tran_unit_pr_returned_qty);
            
            $base_unit_allocated_qty = get_batch_item_issue_source_line_allocated_qty($source_type_id, $source_line_id, $bii_trans_line_id);
            $tran_unit_allocated_qty = $base_unit_allocated_qty / $biid_conversion_factor;
            qty_cell($tran_unit_allocated_qty, false, $dec);
            
            $tran_unit_available_qty = $tran_unit_pr_balance_qty - $tran_unit_allocated_qty;
            qty_cell($tran_unit_available_qty, false, $dec);                
                    
                    

                $current_tran_unit = $row['requested_unit'];
            
                    
                    
            $alternate_unit = get_alternate_unit_info($item_code, $current_tran_unit);
            if($alternate_unit == false){
                $convertion_factor = 1;
            }
            else{
                $convertion_factor = $alternate_unit['convertion_factor'];
            } 
            
            $tran_qty = $row['tran_qty'];
            qty_cell($tran_qty, false, $dec);
            
            $tmp_convesion_factor = 1;
            get_convertion_factor2($item_code, $tran_unit_code, $tmp_convesion_factor);
            $base_unit_qty = $tran_qty * $tmp_convesion_factor;
            

            $total_tran_unit_selected_qty += $tran_qty;
            

            label_cell($row['project_site_description']);
            
            if($row['target_line_id'] > 0){
                $target_link = get_supplier_trans_view_str($row['target_type_id'], $row['target_type_no'], $row['target_line_id']."(".$row['target_type_no'].")");
                label_cell($target_link, "colspan=2 align=center");
            }else{
                label_cell("");
            }
                              
				
	}
        
        

        start_row();
            label_cell("Total (".$tran_unit_name.")", 'colspan=8 align=right'); 
            qty_cell($total_tran_unit_selected_qty, false, 3);
            label_cell(_(""), "colspan=2");
        end_row();

    end_table();
	div_end();
        
        return $total_tran_unit_selected_qty;
        
//        hidden('type_id', $_POST['type_id']); 
//        hidden('line_id', $_POST['line_id']); 
}

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


function display_batch_item_issue_selected_transactions($bii_line_id, $from_date, $to_date)
{
    global $Ajax, $systypes_array;
    
    echo "<td colspan=13>";
    
    $biid_row = get_batch_item_issue_details_record($bii_line_id);
    $item_code = $biid_row['stock_id'];
    $tran_unit_code = $biid_row['tran_unit'];
    $bii_tran_qty = $biid_row['tran_qty'];
    $balance_qty = $bii_tran_qty;
    
    $tran_unit_row = get_item_unit($tran_unit_code);
    $tran_unit_name = $tran_unit_row['name'];
    
    $result = get_batch_item_issue_line_selected_transactions($bii_line_id);
    $n = db_num_rows($result);
    if($n > 0){            

            $biid_conversion_factor = 1;
            get_convertion_factor2($item_code, $tran_unit_code, $biid_conversion_factor);

//            $Ajax->activate('items_table');

        //    display_heading2($systypes_array[$source_type_id]."s");
            display_heading2("Selected Transactions");

//            div_start('items_table');
            start_table(TABLESTYLE, "width=98%");
    

            $th = array("Id", "Type", "Line<br>No.", "PO<br>Line No.", _("Item"), 
                        _("Req.<br>Qty"),  _("Issued<br>Qty."), _("Returned<br>Qty"), 
                        _("Allocated<br>Qty"),  _("Available<br>Qty"),
                        _("Issuing<br>Qty"), "");        

            //if ( count($order->line_items)) $th[] = '';

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

            $id = find_submit('Edit');
        
//          $result = get_batch_item_issue_pr_transactions($bii_line_id);
            while ($row = db_fetch($result)) {

//            $prefix = $row['id']."_".$row['pr_po_line_id']."_".$row['po_detail_id'];
                    $bii_trans_line_id = 0;

                    $source_type_id = $row['source_type_id'];
                    $source_type_no = $row['trans_no'];
                    $source_line_id = $row['id'];

                    $total += ($row['standard_cost'] * $row['requested_qty']);

                    alt_table_row_color($k);                    

                    label_cell($row['biit_id'], 'align=center');

                    label_cell($systypes_array[$row['source_type_id']]);
            
                    if($source_type_id == ST_DAILY_ITEM_ISSUE){
                        $ids_text = "";
                    }else{
                        $ids_text = get_trans_view_str($source_type_id, $row['trans_no'], $row['id']."(".$row['trans_no'].")");
                    }
//                  $pr_allocs_link = get_pr_allocation_ids_string($row['po_detail_id'], $row['pr_po_line_id']);
            
                    if($source_type_id == ST_PURCH_REQ){
                        $po_link = get_supplier_trans_view_str(ST_PURCHORDER, $row['po_no'], $row['po_line_id']."(".$row['po_no'].")");
                    }else{
                        $po_link = "";
                    }

                    label_cell($ids_text, 'align=center');
//                  label_cell($pr_allocs_link, 'align=center');
                    label_cell($po_link, 'align=center');

                    $item_description = get_stock_item_formatted_name($item_code, $row['descr_editable'], $row['stock_master_description'], $row['item_trans_description']);
                    label_cell($item_description);
                    
//            label_cell($row['requested_unit_name']);
            
                    $dec = get_qty_dec($item_code);
//                    qty_cell($stock_item->requested_qty/$stock_item->convertion_factor, false, $dec);
            
            
                    if($source_type_id != ST_DAILY_ITEM_ISSUE){


                            $tran_unit_pr_req_qty = $row['requested_qty'] / $biid_conversion_factor;
                            qty_cell($tran_unit_pr_req_qty, false, $dec);
            
//            $conversion_factor = $row['req_unit_conv_factor'];
            
                            $tran_unit_pr_issued_qty = $row['issued_qty'] / $biid_conversion_factor;
                            qty_cell($tran_unit_pr_issued_qty, false, $dec);

                            $tran_unit_pr_returned_qty = $row['returned_qty'] / $biid_conversion_factor;
                            qty_cell($tran_unit_pr_returned_qty, false, $dec);


                            $tran_unit_pr_balance_qty = $tran_unit_pr_req_qty - ($tran_unit_pr_issued_qty - $tran_unit_pr_returned_qty);

                            if($source_type_id == ST_DAILY_ITEM_ISSUE){
                                $base_unit_allocated_qty = 0;
                            }else{
                                $base_unit_allocated_qty = get_batch_item_issue_source_line_allocated_qty($source_type_id, $source_line_id, $bii_trans_line_id);
                            }
                            $tran_unit_allocated_qty = $base_unit_allocated_qty / $biid_conversion_factor;
                            qty_cell($tran_unit_allocated_qty, false, $dec);

                            $tran_unit_available_qty = $tran_unit_pr_balance_qty - $tran_unit_allocated_qty;
                            qty_cell($tran_unit_available_qty, false, $dec);               

                            $current_tran_unit = $row['requested_unit'];

                            $alternate_unit = get_alternate_unit_info($item_code, $current_tran_unit);
                            if($alternate_unit == false){
                                $convertion_factor = 1;
                            }
                            else{
                                $convertion_factor = $alternate_unit['convertion_factor'];
                            } 
                    }else{
                            if($row['task_id'] > 0){
                                $task_error = "";
                            }else{
                                $task_error = "<font color='red'>Budget not selected</font>";
                            }
                            label_cell($task_error, "colspan=5");
                    }

                    $tran_qty = $row['tran_qty'];
                    qty_cell($tran_qty, false, $dec);

                    $tmp_convesion_factor = 1;
                    get_convertion_factor2($item_code, $tran_unit_code, $tmp_convesion_factor);
                    $base_unit_qty = $tran_qty * $tmp_convesion_factor;
            

                    $total_tran_unit_selected_qty += $tran_qty;


//                    label_cell($row['project_site_description']);

                    if($row['target_line_id'] > 0){
                        switch ($row['target_type_id']) {
                            case ST_PR_ISSUE:
                                $target_link = get_supplier_trans_view_str($row['target_type_id'], $row['target_type_no'], $row['target_line_id']."(".$row['target_type_no'].")");
                                break;
                            case ST_MATERIAL_ISSUE:
                                $target_link = get_inventory_trans_view_str($row['target_type_id'], $row['target_type_no'], $row['target_line_id']."(".$row['target_type_no'].")");
                                break;
                            case ST_DAILY_ITEM_ISSUE:
                                $target_link = get_inventory_trans_view_str($row['target_type_id'], $row['target_type_no'], $row['target_line_id']."(".$row['target_type_no'].")");
                                break;
                            default:
                                $target_link = "";
                                break;
                        }
                        label_cell($target_link, "align=center");
                    }else{
                        label_cell("");
                    }
                    
                    end_row();
            }

        start_row();
            label_cell("Total (".$tran_unit_name.")", 'colspan=10 align=right'); 
            qty_cell($total_tran_unit_selected_qty, false, 3);
            label_cell(_(""), "colspan=2");
        end_row();
        
        $balance_qty = $bii_tran_qty - $total_tran_unit_selected_qty;
        if($balance_qty > 0){
            start_row();
                label_cell("Balance (".$tran_unit_name.")", 'colspan=10 align=right'); 
                qty_cell($balance_qty, false, 3);
                label_cell(_(""), "colspan=2");
            end_row();
        }
        
    }else{
        start_table(TABLESTYLE, "width=98%");
            br(1);
            display_note("No transactions selected");
            br(1); 
    }        

    if($balance_qty > 0){
        start_row();
            $message = "There is a balance quantity of ".$balance_qty." ".$tran_unit_name;
            $message = "<font color='red'>".$message."</font>";
            label_cell($message, 'colspan=13 align=center'); 
        end_row();
    }
    
end_table();
    
//	div_end();
        
        echo "</td>";
        
//        return $total_tran_unit_selected_qty;
        
//        hidden('type_id', $_POST['type_id']); 
//        hidden('line_id', $_POST['line_id']); 
}

function display_batch_item_issue_dii_lines($source_type_id, $bii_line_id, $from_date, $to_date)
{
    global $Ajax, $systypes_array;
        
    $biid = get_batch_item_issue_details_record($bii_line_id);
    $tran_unit_code = $biid['tran_unit'];
    $item_code = $biid['stock_id'];
    $item_description = $biid['item_description'];
    $daily_item_type = $biid['daily_item_type'];
    
    $type = $biid['type'];
    $trans_no = $biid['trans_no'];
    $biih = get_batch_item_issue_header_record_type_trans_no($type, $trans_no);
    
    $tran_unit_row = get_item_unit($tran_unit_code);
    $tran_unit_name = $tran_unit_row['name'];
    
    $biid_conversion_factor = 1;
    get_convertion_factor2($item_code, $tran_unit_code, $biid_conversion_factor);

    $Ajax->activate('items_table');
    
    display_heading($systypes_array[$source_type_id]);
        
    div_start('items_table');
    start_table(TABLESTYLE, "width=80%");
    
    $th = array(_("Item"), _("Issuing<br>Qty"), _("Unit Cost"), _("Budget"), "");     

	//if ( count($order->line_items)) $th[] = '';

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

        $bii_trans_line_id = $target_type_id = $target_type_no = $target_line_id = 0;
            
        $source_type_no = $trans_no;
        $source_line_id = $biid['id'];
        $tran_qty = $task_id = $tran_unit_cost = 0;
        $trans_row = get_batch_item_issue_transaction_record($bii_line_id, $source_type_id, $source_type_no, $source_line_id);
        if(is_array($trans_row)){
            $bii_trans_line_id = $trans_row['id'];
            $tran_qty = $trans_row['tran_qty'];
            $task_id = $trans_row['task_id'];
            $tran_unit_cost = $trans_row['tran_unit_cost'];
        }
                
	$id = find_submit('Edit');
        
        alt_table_row_color($k);   
        
        $item_name = $item_code." - ".$item_description;
        
        label_cell($item_name);

        $prefix = $biid['id']."_";

        $dec = get_qty_dec($item_code);

        set_post_not_set_numeric($prefix.'_tran_qty', $tran_qty);
        set_post_not_set_numeric($prefix.'task_id', $task_id);
//        $_POST[$prefix.'_tran_qty'] = $trans_row['tran_qty'];
//        $_POST[$prefix.'task_id'] = $trans_row['task_id'];

        $suggested_issu_qty = 0;
        $line_ref = '';
        $item_rec = get_stock_item_edit_info($item_code);
        display_quantity_cells(get_post('guid'), $source_type_id, $line_ref, $item_code, 
                                $item_rec['editable'], $prefix.'_', $suggested_issu_qty, $tran_unit_code, 
                                $dec, "items_table");

        if($daily_item_type == NSIT_NON_STOCK_ITEM){
            amount_cells(null, $prefix.'tran_unit_cost', $tran_unit_cost);
        }else{
            $standard_cost = get_standard_cost($item_code);
            $tran_unit_cost = $standard_cost * $biid_conversion_factor;
            amount_cell($tran_unit_cost);
            
            $_POST[$prefix.'tran_unit_cost'] = $tran_unit_cost;
            hidden($prefix.'tran_unit_cost', $_POST[$prefix.'tran_unit_cost']);
        }
        
        tasks_list_cells(null, $prefix.'task_id', $biih['project_id'], null, _(''));
        
        if($bii_trans_line_id > 0){
            delete_button_cell("Delete"."_biit_".$bii_trans_line_id, _("Delete")); 
        }else{
            label_cell("");
        }

        end_row();
        
        $tran_qty = input_num($prefix.'_tran_qty');

        start_row();
            $total_base_unit_pr_allocations = get_batch_item_issue_detail_line_allocated_qty($bii_line_id, ST_PURCH_REQ);
            label_cell($systypes_array[ST_PURCH_REQ]."s allocations (".$tran_unit_name.")", 'colspan=1 align=right'); 
            $total_tran_unit_pr_allocations = $total_base_unit_pr_allocations / $biid_conversion_factor;
            qty_cell($total_tran_unit_pr_allocations, false, 3);
            label_cell(_(""), 'colspan=2');
        end_row();
        start_row();
            $total_base_unit_mr_allocations = get_batch_item_issue_detail_line_allocated_qty($bii_line_id, ST_MATERIAL_REQ);
            label_cell($systypes_array[ST_MATERIAL_REQ]."s allocations (".$tran_unit_name.")", 'colspan=1 align=right'); 
            $total_tran_unit_mr_allocations = $total_base_unit_mr_allocations / $biid_conversion_factor;
            qty_cell($total_tran_unit_mr_allocations, false, 3);
            label_cell(_(""));
        end_row();
        start_row();
            $tran_unit_total_allocations = $total_tran_unit_mr_allocations + $total_tran_unit_pr_allocations + $tran_qty;
            label_cell("Total allocations (".$tran_unit_name.")", 'colspan=1 align=right'); 
            qty_cell($tran_unit_total_allocations, false, 3);
            label_cell(_(""), 'colspan=2');
        end_row();

    end_table();
	div_end();
        
//        hidden('type_id', $_POST['type_id']); 
//        hidden('line_id', $_POST['line_id']); 
}


function auto_allocate()
{
    $result = get_batch_item_issue_details($trans_no, $type);      
    while ($row = db_fetch($result)) {
        
        auto_allocate_item_issue_line();        
    }
}

function auto_allocate_item_issue_line($row)
{
    $required_tran_qty = $row['tran_qty'];
    $remaining_tran_qty = $required_tran_qty;
    $result = get_batch_item_issue_pr_lines($bii_line_id, $from_date, $to_date);
    while ($row = db_fetch($result)) {
        $bii_trans_line_id = 0;
        $source_line_id = $row['id'];
        $source_type_no = $row['trans_no'];
        $trans_row = get_batch_item_issue_transaction_record($bii_line_id, $source_type_id, $source_type_no, $source_line_id);
        if(is_array($trans_row)){
            $bii_trans_line_id = $trans_row['id'];
            $allocated_tran_qty = $trans_row['tran_qty'];
            $remaining_tran_qty -= $allocated_tran_qty;
            continue;
        }else{
            $issuing_tran_qty = $row['requested_qty'] * $biid_conversion_factor;
        }

        $base_unit_allocated_qty = get_batch_item_issue_source_line_allocated_qty($source_type_id, $source_line_id, $bii_trans_line_id);
        $tran_unit_allocated_qty = $base_unit_allocated_qty / $biid_conversion_factor; 
    }
    
}