<?php

function execute_lot_selection_parent_function($func_type, $type, $trans_no=0, $line_id=0, $param_1=null) {
    switch ($type) {
        case ST_JOB_MATERIAL_ISSUE : 
            if ($func_type == 'DISPLAY_LINE_HEADER') {
                return array(_("Line #"), _("Item Type"), _("Issue Tran Unit"), _("Issue Tran Qty"), _('Base Unit'), _('Base Qty'), _('Site'), 
                             _('Budget'), _('Line Comment'));  
            } elseif ($func_type == 'GET_LINE_DETAILS') {
                return lot_get_job_material_issue_details_line_by_type_trans_no_line_no($type, $trans_no, $line_id);  
            } elseif ($func_type == 'GET_HEADER') {
                return lot_get_job_material_issue_header_record_type_trans_no($type, $trans_no);
            } elseif ($func_type == 'UPDATE_ROW_STATE') {
                return lot_update_job_material_issue_row_state($type, $trans_no, $param_1);
            } elseif ($func_type == 'GET_LINE') {
                return lot_get_job_material_issue_detail_record_type_trans_no_line_no($type, $trans_no, $line_id);
            } elseif ($func_type == 'GET_PARENT_LINE') {
                return lot_get_job_material_requisition_details_record($param_1);
            } elseif ($func_type == 'TRANS_TYPE') {
                return ST_JOB_MATERIAL_REQ;
            }
            
            break;
            
        case ST_BUDGET_TO_BUDGET_TRANSFER : 
            if ($func_type == 'DISPLAY_LINE_HEADER') {
                return array(_("Line #"), _("Item Type"), _("From Job"), _("From Task"), _("Transfer Tran Unit"), _("Transfer Tran Qty"), _('Base Unit'), 
                             _('Base Qty'), _('Site'), _('To Budget'), _('Line Comment'));  
            } elseif ($func_type == 'GET_LINE_DETAILS') {
                return lot_get_budget_to_budget_transfer_details_line_by_type_trans_no_line_no($type, $trans_no, $line_id);  
            } elseif ($func_type == 'GET_HEADER') {
                return lot_get_budget_to_budget_transfer_header_record_type_trans_no($type, $trans_no);
            } elseif ($func_type == 'UPDATE_ROW_STATE') {
                return lot_update_budget_to_budget_transfer_row_state($type, $trans_no, $param_1);
            } elseif ($func_type == 'GET_LINE') {
                return lot_get_budget_to_budget_transfer_detail_record_type_trans_no_line_no($type, $trans_no, $line_id);
            } elseif ($func_type == 'GET_PARENT_LINE') {
                return lot_get_budget_to_budget_transfer_details_record($line_id);
            } elseif ($func_type == 'TRANS_TYPE') {
                return ST_JOB_MATERIAL_REQ;
            }
            
            break;
            
        default :
    }
}

function display_line_details($type, $trans_no, $line_id) { 
    global $systypes_array, $daily_item_types_array; 
    
    $line_row = execute_lot_selection_parent_function('GET_LINE', $type, $trans_no, $line_id);
        if (! is_array($line_row)) {
            display_error("Could not find ".$systypes_array[$type]." Trans #$trans_no and Detail Line #$line_id.");
            display_footer_exit();
        }
        
        display_heading($systypes_array[$type]." Trans #$trans_no and Line #$line_id");
        
        start_table(TABLESTYLE, "width=95%");

            $th = execute_lot_selection_parent_function('DISPLAY_LINE_HEADER', $type);  
            table_header($th);

                $k = 0; //row colour counter
                $row = execute_lot_selection_parent_function('GET_LINE_DETAILS', $type, $trans_no, $line_id);      
                    alt_table_row_color($k);

                        label_cell($row['trans_id'], 'align=center');
                        label_cell($daily_item_types_array[$row['item_type']]);
                        if ($type == ST_BUDGET_TO_BUDGET_TRANSFER) {
                            $from_jo_id = $row["from_jo_id"];
                            label_cell(get_service_trans_view_str(ST_JOB_ORDER, $from_jo_id), 'align=center');  
                            label_cell($row["from_task_description"]);  
                        }
                        label_cell($row["tran_unit_name"], 'align=center');
                        $dec = 4; // get_qty_dec($row["stock_id"]);
                        qty_cell($row["tran_qty"], false, $dec);
                        label_cell($row["base_unit_name"], 'align=center');
                        qty_cell($row["qty"], false, $dec);
                        label_cell($row["site_description"]);          
                        label_cell($row["task_description"]);          
                        label_cell(nl2br($row["line_comment"])); 

                    end_row();        

        end_table(2);
}

function display_issue_lot_selections($type, $trans_no, $line_id) {
    $line = execute_lot_selection_parent_function('GET_LINE', $type, $trans_no, $line_id);
    
        if ($type == ST_BUDGET_TO_BUDGET_TRANSFER) {
            view_budget_to_budget_transfer_lots_selection($type, $trans_no, $line_id, $line, true);
        } else {
            view_lots_selection($type, $trans_no, $line_id, $line, true);
        }
}

function view_budget_to_budget_transfer_lots_selection($type, $trans_no, $ref_line_id, $line, $show_total=false) {
    
    $from_jo_id = $line['from_jo_id'];
    $to_jo_id = $line['to_jo_id'];
    $stock_id = $line['stock_id'];
    $to_loc_code = $line['loc_code'];
    $display_conversion_factor = $line['conversion_factor'];
    $display_unit = $line['tran_unit'];
    
    display_heading("From Job #$from_jo_id Lots Selection Details of ".get_stock_item_formatted_name($stock_id)." item at ".
                    get_location_name($to_loc_code)." (unit in '".get_item_base_unit_name($stock_id)."')");

        start_table(TABLESTYLE, "width=99%");

            $th = array(_("Id"), _("Lot Name"), _("Location"), _("DP.(Recp+Retn) Lot Qty"), _("JM.(Issu+Retn) Lot Qty"), _("B2B.Trn. Lot Qty"), 
                        _("Alloc. (JM.Issu+B2B.Trnf) Lot Qty"), _("Remain Base Qty"), _("Tran Unit"), _("Remain Tran Qty"), _("Tran Qty"), _(""));  
            table_header($th);

            $dec = 4; //get_qty_dec($stock_id);
            $k = 0; //row colour counter

            $result = lot_get_budget_to_budget_transfer_lots($from_jo_id, $to_jo_id, $type, $trans_no, $ref_line_id, $stock_id);   
                while ($row = db_fetch($result)) {
                    $lot_id = $row['lot_id'];
                    $tran_id = $row['tran_id'];

                    alt_table_row_color($k);
                        label_cell($tran_id, 'align=center');
                        label_cell($row['lot_reference']);
                        label_cell($row['location_name']);
                        
                        $dp_rcp_rtn_qty = $row['dp_rcp_rtn_qty']; 
//                        lot_get_dp_receipt_return_lot_qty($stock_id, $from_jo_id, $to_jo_id, $lot_id);
                        qty_cell($dp_rcp_rtn_qty, false, $dec);
                        
                        $jm_iss_rtn_qty_ = lot_get_jm_issue_return_lot_qty($stock_id, $from_jo_id, $to_jo_id, $lot_id);
                        $jm_iss_rtn_qty = abs($jm_iss_rtn_qty_);
                        qty_cell($jm_iss_rtn_qty, false, $dec);
                        
                        $total_b2b_qty_ = lot_get_total_from_job_b_to_b_transfer_lot_qty($stock_id, $from_jo_id, $lot_id);
                        $total_b2b_qty = abs($total_b2b_qty_);
                        qty_cell($total_b2b_qty, false, $dec);
                        
                        $total_jm_iss_b2b_qty_ = lot_get_total_from_job_jm_issue_b_to_b_transfer_lot_qty($stock_id, $from_jo_id, $lot_id);
                        $total_jm_iss_b2b_qty = abs($total_jm_iss_b2b_qty_);
                        qty_cell($total_jm_iss_b2b_qty, false, $dec);
                        
                        $remain_base_qty = $dp_rcp_rtn_qty - $jm_iss_rtn_qty - $total_b2b_qty - $total_jm_iss_b2b_qty;
                        qty_cell($remain_base_qty, false, $dec);

                        if (! isset($_POST["tran_unit_$lot_id"])) {
                            if ($tran_id > 0) {
                                $_POST["tran_unit_$lot_id"] = $row["tran_unit"];
                            } else {
                                $_POST["tran_unit_$lot_id"] = $display_unit;
                            }
                        }

                        units_list_cells('', "tran_unit_$lot_id", $row['stock_id'], null, '', true);

                        $tran_unit = $_POST["tran_unit_$lot_id"];
                        $convesion_factor = 1;
                        get_convertion_factor2($stock_id, $tran_unit, $convesion_factor);
                        
                        $remain_tran_qty = $remain_base_qty / $convesion_factor;
                        qty_cell($remain_tran_qty, false, $dec);

                        if (! isset($_POST["tran_qty_$lot_id"]) && $tran_id > 0) {
                            $_POST["tran_qty_$lot_id"] = abs($row["tran_qty"]);
                        }
                        amount_cells("", "tran_qty_$lot_id", null, null, null, 4);
                        if (! isset($_POST["checked_$lot_id"]) && $tran_id > 0) {
                            $_POST["checked_$lot_id"] = 1;
                        } 
                        check_cells(null, "checked_$lot_id", null, false, false, 'align=center');
                    end_row();        
                }

                if ($show_total === true) {
                    $base_qty_total = get_lot_allocations_total_qty($type, $trans_no, $ref_line_id, $stock_id, $to_loc_code);
                    $tran_qty_total = abs($base_qty_total)/ $display_conversion_factor;
                    start_row();
                        label_cell(text_bold("Total '". get_unit_descr($display_unit)."'"), 'align=right colspan=8');
                        qty_cell($tran_qty_total, true, 4);
                        label_cell('');
                    end_row();
                }

        end_table(2);
}

function view_lots_selection($type, $trans_no, $ref_line_id, $line, $show_total=false) {
    
    $stock_id = $line['stock_id'];
    $loc_code = $line['loc_code'];
    $display_conversion_factor = $line['conversion_factor'];
    $display_unit = $line['tran_unit'];
    
    display_heading('Lot Selection Details of "'.get_stock_item_formatted_name($stock_id).'" item at '.get_location_name($loc_code)
                    ." (unit in '".get_item_base_unit_name($stock_id)."')");

        start_table(TABLESTYLE, "width=60%");

            $th = array(_("Id"), _("Lot Name"), _("Location"), _("Approved Lot Remain Qty"), _("Allocated Lot Remain Qty"), _("All Lot Remain Base Qty"), 
                        _("Tran Unit"), _("All Lot Remain Tran Qty"), _("Tran Qty"), _(""));  
            table_header($th);

            $dec = get_qty_dec($stock_id);
            $k = 0; //row colour counter

            $result = get_lots_selection_edit_details($type, $trans_no, $ref_line_id, $stock_id, $loc_code);   
                while ($row = db_fetch($result)) {
                    $lot_id = $row['lot_id'];
                    $tran_id = $row['tran_id'];

                    alt_table_row_color($k);
                        label_cell($tran_id, 'align=center');
                        label_cell($row['lot_reference']);
                        label_cell($row['location_name']);

                        qty_cell($row['used'], false, $dec);
                        qty_cell($row['allocated'], false, $dec);
                        $total_remain_base_qty = $row['used'] + $row['allocated'];
                        qty_cell($total_remain_base_qty, false, $dec);

                        if (! isset($_POST["tran_unit_$lot_id"])) {
                            if ($tran_id > 0) {
                                $_POST["tran_unit_$lot_id"] = $row["tran_unit"];
                            } else {
                                $_POST["tran_unit_$lot_id"] = $display_unit;
                            }
                        }

                        units_list_cells('', "tran_unit_$lot_id", $row['stock_id'], null, '', true);

                        $tran_unit = $_POST["tran_unit_$lot_id"];
                        $convesion_factor = 1;
                        get_convertion_factor2($stock_id, $tran_unit, $convesion_factor);
                        $total_remain_tran_qty = $total_remain_base_qty / $convesion_factor;
                        qty_cell($total_remain_tran_qty, false, $dec);

                        if (! isset($_POST["tran_qty_$lot_id"]) && $tran_id > 0) {
                            $_POST["tran_qty_$lot_id"] = abs($row["tran_qty"]);
                        }
                        amount_cells("", "tran_qty_$lot_id", null, null, null, 4);
                        if (! isset($_POST["checked_$lot_id"]) && $tran_id > 0) {
                            $_POST["checked_$lot_id"] = 1;
                        } 
                        check_cells(null, "checked_$lot_id", null, false, false, 'align=center');
                    end_row();        
                }

                if ($show_total === true) {
                    $base_qty_total = get_lot_allocations_total_qty($type, $trans_no, $ref_line_id, $stock_id, $loc_code);
                    $tran_qty_total = abs($base_qty_total)/ $display_conversion_factor;
                    start_row();
                        label_cell(text_bold("Total '". get_unit_descr($display_unit)."'"), 'align=right colspan=8');
                        qty_cell($tran_qty_total, true, 3);
                        label_cell('');
                    end_row();
                }

        end_table(2);
}

function can_save_lot_selection_lines($type, $trans_no, $line_id) {
    global $systypes_array;
    
    $hd_row = execute_lot_selection_parent_function('GET_HEADER', $type, $trans_no);
        if (is_array($hd_row)) {
            if (in_array($hd_row['approval_status'], array(AS_APPROVED, AS_COMPLETED))) {
                return array("success" => false, "message" => $systypes_array[$type]." Trans #$trans_no has processd. Cannot update further.");
            }
        } else {
            return array("success" => false, "message" => $systypes_array[$type]." Trans #$trans_no deleted by another user.");
        }
    
    $line_row = execute_lot_selection_parent_function('GET_LINE', $type, $trans_no, $line_id);
        if (is_array($line_row)) {
            $stock_id = $line_row['stock_id'];
            
            $stock_row = get_stock_item_edit_info($stock_id);
            if ($stock_row['stock_control_method'] != SKCM_LOTS) {
                return array("success" => false, "message" => $line_row['stock_id']." Stock Item is not a Lot item.");
            }
        } else {
            return array("success" => false, "message" => $systypes_array[$type]." Detail Line #$line_id is deleted by another user.");
        }
        
    $req_line = execute_lot_selection_parent_function('GET_PARENT_LINE', $type, 0, $line_id, $line_row['source_line_id']);
        
    $data_array = array();
        
        $data_array['source_type'] = $req_line['type'];
        $data_array['source_trans_no'] = $req_line['trans_no'];
        $data_array['source_line_id'] = $line_row['source_line_id'];
        $data_array['stock_id'] = $stock_id;
        $data_array['loc_code'] = $line_row['loc_code'];
        $data_array['conversion_factor'] = $line_row['conversion_factor'];
        $data_array['tran_qty'] = $line_row['tran_qty'];
        $data_array['tran_unit'] = $line_row['tran_unit'];
        $data_array['tran_date'] = $line_row['tran_date'];
        if ($type == ST_BUDGET_TO_BUDGET_TRANSFER) {
            $data_array['from_jo_id'] = $line_row['from_jo_id'];
            $data_array['to_jo_id'] = $line_row['to_jo_id'];
        }
        
        return array("success" => true, "message" => "", "row_state" => $hd_row['row_state'], "data_array" => $data_array);
}

function can_save_lots_selection($type, $trans_no, $line_id, $data_array=array()) {
    
    $stock_id = $data_array['stock_id'];
    $loc_code = $data_array['loc_code'];
    $parent_conversion_factor = $data_array['conversion_factor'];
    $parent_tran_qty_ = $data_array['tran_qty'];
    $parent_tran_unit = $data_array['tran_unit'];
    
    $dec = 4; //get_qty_dec($stock_id);
    $no_selected_recs = $has_old_recs = $total_lots_base_qty = 0;
    
    if ($type == ST_BUDGET_TO_BUDGET_TRANSFER) {
        $from_jo_id = $data_array['from_jo_id']; 
        $to_jo_id = $data_array['to_jo_id'];
        $result = lot_get_budget_to_budget_transfer_lots($from_jo_id, $to_jo_id, $type, $trans_no, $line_id, $stock_id);
    } else {
        $result = get_lots_selection_edit_details($type, $trans_no, $line_id, $stock_id, $loc_code);
    }
    
        while ($row = db_fetch($result)) {
            $lot_id = $row['lot_id'];
            $lot_name = $row['lot_reference'];
            $tran_id = $row['tran_id'];
            
            if (isset($_POST["checked_$lot_id"]) && $_POST["checked_$lot_id"] == 1) {
                $tran_unit = $_POST["tran_unit_$lot_id"];
                $tran_qty = (float) input_num("tran_qty_$lot_id");
                
                if ($row['stock_control_method'] != SKCM_LOTS) {
                    $item_name = get_stock_item_formatted_name($stock_id);
                    return array("success" => false, "focus_to" => "", "message" => "$item_name item is not a Lot Item");  
                } 
                
                if ($tran_unit == -1) {
                    return array("success" => false, "focus_to" => "tran_unit_$lot_id", "message" => "$lot_name Please select a tran unit.");  
                }

                if (! ($tran_qty > 0)) {
                    return array("success" => false, "focus_to" => "tran_qty_$lot_id", "message" => "$lot_name tran Qty should be greater than zero.");  
                }
                
                    $convesion_factor = 1;
                    get_convertion_factor2($stock_id, $tran_unit, $convesion_factor);
                    
                    if ($type == ST_BUDGET_TO_BUDGET_TRANSFER) {
                        $remain_lot_base_qty = get_budget_to_budget_transfer_remain_qty($stock_id, $from_jo_id, $to_jo_id, $lot_id);
                    } else {
                        $remain_lot_base_qty = get_location_lot_remain_qty_on_lot_moves_allocations($lot_id, $loc_code, $tran_id);
                    }

                    $total_remain_lot_tran_qty = round($remain_lot_base_qty / $convesion_factor, $dec);

                    if ($tran_qty > $total_remain_lot_tran_qty) {
                        $uname = get_unit_descr($tran_unit);
                        return array("success" => false, "focus_to" => "tran_qty_$lot_id", 
                                     "message" => "$lot_name Tran Qty is exceeded lot Remain Tran Qty $total_remain_lot_tran_qty in Unit '$uname'");  
                    }
                    
                    $total_lots_base_qty += $tran_qty * $convesion_factor;
    
                $no_selected_recs++;
            }
            
            if ($tran_id > 0) {
                $has_old_recs = 1;
            }
        }
        
        if (! ($no_selected_recs > 0) && ! ($has_old_recs > 0)) {
            return array("success" => false, "message" => "Please select at least one record before saving.");  
        }
        
        $parent_tran_qty = round($parent_tran_qty_, $dec);
        $total_lots_tran_qty = round($total_lots_base_qty / $parent_conversion_factor, $dec);

        if ($total_lots_tran_qty > $parent_tran_qty) {
            $uname = get_unit_descr($parent_tran_unit);
            $diff = round($total_lots_tran_qty - $parent_tran_qty, $dec);
            return array("success" => false, "message" => "Entered Total Tran Lots Qty '$total_lots_tran_qty' is exceeded by $diff. Allowable "
                                                        . "Line Qty of $parent_tran_qty in Unit of '$uname'");  
        }
        
        return array("success" => true, "message" => "");  
}

function get_budget_to_budget_transfer_remain_qty($stock_id, $from_jo_id, $to_jo_id, $lot_id) {
    $dp_rcp_rtn_qty = lot_get_dp_receipt_return_lot_qty($stock_id, $from_jo_id, $to_jo_id, $lot_id);

    $jm_iss_rtn_qty_ = lot_get_jm_issue_return_lot_qty($stock_id, $from_jo_id, $to_jo_id, $lot_id);
    $jm_iss_rtn_qty = abs($jm_iss_rtn_qty_);

    // from lot moves
    $total_b2b_qty_ = lot_get_total_from_job_b_to_b_transfer_lot_qty($stock_id, $from_jo_id, $lot_id);
    $total_b2b_qty = abs($total_b2b_qty_);

    // from lot alloc
    $total_jm_iss_b2b_qty_ = lot_get_total_from_job_jm_issue_b_to_b_transfer_lot_qty($stock_id, $from_jo_id, $lot_id, 0);
    $total_jm_iss_b2b_qty = abs($total_jm_iss_b2b_qty_);

    $remain_lot_qty = $dp_rcp_rtn_qty - $jm_iss_rtn_qty - $total_b2b_qty - $total_jm_iss_b2b_qty;

        return $remain_lot_qty;
}

function save_lot_selection_lines($type, $trans_no, $line_id, $data_array=array()) {    
    global $systypes_array;
    
    $stock_id = $data_array['stock_id'];
    $loc_code = $data_array['loc_code'];
    $tran_date = $data_array['tran_date'];
    $source_type = $data_array['source_type'];
    $source_trans_no = $data_array['source_trans_no'];
    $source_line_id = $data_array['source_line_id'];
    $lot_alloc_tp_name = $systypes_array[ST_LOT_ALLOCATION];
    
    if ($type == ST_BUDGET_TO_BUDGET_TRANSFER) {
        $from_jo_id = $data_array['from_jo_id']; 
        $to_jo_id = $data_array['to_jo_id'];
        $result = lot_get_budget_to_budget_transfer_lots($from_jo_id, $to_jo_id, $type, $trans_no, $line_id, $stock_id);
    } else {
        $result = get_lots_selection_edit_details($type, $trans_no, $line_id, $stock_id, $loc_code);
    }
    
        $line_count = get_lot_allocation_lines_count($type, $trans_no, $line_id);
            if ($line_count > 0) {
                $del_af_rows = delete_lot_allocation_lines($type, $trans_no, $line_id);
                    if ($line_count != $del_af_rows) {
                        return array('success' => false, 'message' => "Could not delete $lot_alloc_tp_name Detail lines.");
                    }
            }
            
            while ($row = db_fetch($result)) {
                $lot_id = $row['lot_id'];

                if (isset($_POST["checked_$lot_id"]) && $_POST["checked_$lot_id"] == 1) {
                    $tran_unit = $_POST["tran_unit_$lot_id"];
                    $tran_qty = input_num("tran_qty_$lot_id");

                    $convesion_factor = 1;
                    get_convertion_factor2($stock_id, $tran_unit, $convesion_factor);

                    $base_qty = $tran_qty * $convesion_factor;
                    
                    if ($type != ST_BUDGET_TO_BUDGET_TRANSFER) {
                        $tran_qty *= -1;
                        $base_qty *= -1;
                    }
                
                    $ln_id = insert_lot_allocation_line($type, $trans_no, $lot_id, $stock_id, $loc_code, $tran_unit, $tran_qty, $convesion_factor, 
                                                        $base_qty, $tran_date, $line_id, $source_type, $source_trans_no, $source_line_id);

                        if (! ($ln_id > 0)) {
                            return array("success" => false, "message" => "Could not save Lot #$lot_id $lot_alloc_tp_name details.");
                        } 
                }
                
                $lot_bal = calculate_and_update_lot_balance_used_and_allocated_qty($lot_id, $loc_code);
                    if ($lot_bal['success'] === false) {
                        return $lot_bal;
                    }
            }   
        
        return array("success" => true, "message" => "");
}

function save_issue_lot_selections($type, $trans_no, $line_id) {
    global $systypes_array;
    
    $msg = can_save_lot_selection_lines($type, $trans_no, $line_id);
        if ($msg['success'] !== true) {
            return $msg;       
        }
        
        $data_array = $msg['data_array'];       
        
    $cn_sv_sk = can_save_lots_selection($type, $trans_no, $line_id, $data_array);
        if ($cn_sv_sk['success'] === false) {
            return $cn_sv_sk;
        } 
        
        $row_state = $msg['row_state'];
        
        begin_transaction();

            $line_msg = save_lot_selection_lines($type, $trans_no, $line_id, $data_array);
                if ($line_msg['success'] !== true) {
                    cancel_transaction();
                    return array("success" => false, "message" => $line_msg['message']);
                }
                
            $rw_stt = execute_lot_selection_parent_function('UPDATE_ROW_STATE', $type, $trans_no, 0, $row_state);
                if ($rw_stt != 1) {
                    cancel_transaction();
                    return array("success" => false, "message" => "Could not update ".$systypes_array[$type]." Trans #$trans_no row state.");
                }
                
        commit_transaction();
        
        return array("success" => true, "message" => $systypes_array[ST_JOB_LOT_SELECTION]." Issue details have been saved.");
}

function clear_issue_lot_selection_details($type, $trans_no, $line_id) {
    $line = execute_lot_selection_parent_function('GET_LINE', $type, $trans_no, $line_id);
        $stock_id = $line['stock_id'];
        $loc_code = $line['loc_code'];
        
        if ($type == ST_BUDGET_TO_BUDGET_TRANSFER) {
            $from_jo_id = $line['from_jo_id']; 
            $to_jo_id = $line['to_jo_id'];
            $result = lot_get_budget_to_budget_transfer_lots($from_jo_id, $to_jo_id, $type, $trans_no, $line_id, $stock_id);
        } else {
            $result = get_lots_selection_edit_details($type, $trans_no, $line_id, $stock_id, $loc_code);
        }
            
            while ($row = db_fetch($result)) {
                if (! ($row['tran_id'])) {
                    $lot_id = $row['lot_id'];
                    
                    unset($_POST["tran_unit_$lot_id"]);
                    unset($_POST["tran_qty_$lot_id"]);
                }
            }
}

?>