<?php

function display_stock_asset_transfer_header($GRN_No, $type, $status=true) {
    global $Refs;
    
        $grn_header = get_grn_and_po_headers_details_by_grn_no($GRN_No);
            $user_id = $_SESSION['wa_current_user']->user;
            $cap = can_access_project($grn_header['project_id'], $user_id);
                if ($cap['success'] !== true) {
                    display_error($cap["message"]);
                    set_focus('project_id');
                    display_footer_exit();
                    exit;
                } 

        div_start('nsit_header');
  
            start_outer_table(TABLESTYLE2, "width=90%"); // outer table
                start_table(TABLESTYLE2);  
                        table_section(1, "33%");
                            label_row("GRN #:", get_trans_view_str(ST_SUPPRECEIVE, $grn_header["id"]), "class='tableheader2'");  
                            label_row("Supplier:", $grn_header["supp_name"], "class='tableheader2'");  
                            label_row("Reference:", $grn_header["reference"], "class='tableheader2'");  
                            label_row("Purchase Order #:", get_trans_view_str(ST_PURCHORDER, $grn_header["purch_order_no"]), "class='tableheader2'");  

                        table_section(2, "33%");
                            label_row("Delivery Date:", sql2date($grn_header['delivery_date']), "class='tableheader2'");   
                            label_row("Project:", $grn_header['project_description'], "class='tableheader2'");   
                            label_row("Deliver Location:", $grn_header['location_name'], "class='tableheader2'");   
                            
                        table_section(3, "33%");
                            label_row("Delivery Address:", $grn_header['delivery_address'], "class='tableheader2'");   
                            label_row("Comments:", nl2br($grn_header['comments']), "class='tableheader2'");   
                end_table();
            end_outer_table(2); // outer table
            
            
            $_POST['reference'] = $Refs->get_next($type);            
            $_POST['approval_status'] = 0;
            
            start_outer_table(TABLESTYLE2, "width=50%"); // outer table
                table_section(1, "40%");
                    text_row("Reference:", 'reference', $Refs->get_next($type), null, null);   
                    date_row("Date:", 'tran_date');

                table_section(3, "40%");
                    textarea_row("Comment:", 'comments', null, 35, 2); 
            end_outer_table(3); // outer table
    
        div_end();
}

function display_stock_asset_transfer_details($GRN_No, $status=true) { 
    global $Ajax;
    
    display_heading("Details");    br();
    
    $Ajax->activate('dir_details');
    div_start('dir_details');
    
        start_table(TABLESTYLE, "width=75%");

            labelheader_cell('GRN Item', 'colspan=2');
            labelheader_cell('Asset', 'colspan=2');
            labelheader_cell('');
            $th = array(_('Stock Field'), _('Stock Value'), _('Asset Field'), _('Asset Value'), _(''));  
            table_header($th);
            
            $grn_header = get_grn_and_po_headers_details_by_grn_no($GRN_No);
                $grn_location_name = $grn_header['location_name'];
            
            $first_row = true;
            $k = 0;
            $result = get_grn_and_po_lines_details_by_grn_no($GRN_No);      
                while ($row = db_fetch($result)) {
                    
                    $id = $row['grn_line_no'];
                    
                    $task_name = $outstanding = $pr_line_text = '';
                    $po_line_no = $row['po_detail_item'];
                    $pr_row = get_pr_line_details_for_po_line($po_line_no);
                        if (is_array($pr_row)) {
                            $task_name = $pr_row['task_name'];
                            $outstanding = $pr_row['quantity'] - ($pr_row['pr_issued_qty'] - $pr_row['pr_returned_qty']);
                            $pr_line_text = '('.$pr_row['pr_no'].') '.$pr_row['pr_line_no'];
                            $pr_line_text = get_supplier_trans_view_str(ST_PURCH_REQ, $pr_row['pr_no'], $pr_line_text);
                        }

                    if (! $first_row) {
                        vertical_space("height=18px colspan=5 style='background-color:#97c4c4'");
                    }

                    alt_table_row_color($k); 
                        label_cell(_(text_bold('GRN Line #:')));
                        label_cell($id);
                        label_cell('', 'colspan=2');  
                        check_cells('', "checked_$id", null, false, false, 'align=center'); 
                    end_row();
                    
                    alt_table_row_color($k); 
                        label_cell(_(text_bold('PR #:')));
                        label_cell($pr_line_text);
                        label_cell('', 'colspan=3');  
                    end_row();

                    alt_table_row_color($k);
                        label_cell(_(text_bold('Project:')));
                        label_cell($row['po_project_name']);
                        label_cell(_(text_bold('Charging Project:')));
                        label_cell(project_list("project_id_$id", null, '', true));
                        label_cell('');
                    end_row();

                    alt_table_row_color($k);
                        label_cell(_(text_bold('Budget:')));
                        label_cell($task_name);
                        label_cell(_(text_bold('Charging Budget:')));
                        label_cell(tasks_list("task_id_$id", $_POST["project_id_$id"], null, _('')));
                        label_cell('');
                    end_row();

                    alt_table_row_color($k);
                        label_cell(_(text_bold('Item:')));
                        $stock_item_name = get_stock_item_formatted_name($row['item_code'], $row['editable'], $row['sm_item_description'], 
                                                                         $row['grn_item_description']);
                        label_cell($stock_item_name);
                        label_cell(_(text_bold('Asset:')));
                        label_cell(asset_list("asset_id_$id", null, false, true));
                        label_cell('');
                    end_row();

                    alt_table_row_color($k);
                        label_cell(_(text_bold('GRN Unit:')));
                        label_cell($row["tran_unit_name"]);
                        label_cell('', 'colspan=3');  
                    end_row();

                    alt_table_row_color($k);
                        label_cell(_(text_bold('GRN Qty:')));
                        $grn_tran_qty = $row["tran_qty"];
                        label_cell(number_format($grn_tran_qty, 2));
                        label_cell('', 'colspan=3');  
                    end_row();

                    alt_table_row_color($k);
                        $po_order_unit_price = $row["po_order_unit_price"];
                        $po_order_unit_price_discount = $row["po_order_unit_price_discount"];
                        $after_discount = ($po_order_unit_price - $po_order_unit_price_discount);

                        label_cell(_(text_bold('GRN Price (After Discount):')));
                        label_cell(number_format($after_discount, 2));
                        
                        $asset_value = $line_total = $multiple_items = 0;
                        $asset_row = get_asset($_POST["asset_id_$id"]);
                            if (is_array($asset_row)) {
                                $asset_value = $asset_row['current_value'];
                                $multiple_items = $asset_row['multiple_items'];
                            }
                        label_cell(_(text_bold('Asset Value:')));
                        label_cell(number_format($asset_value, 2));
                        label_cell('');
                    end_row();

                    alt_table_row_color($k);
                        label_cell(_(text_bold('Issuing Item Qty:')));
                        small_amount_cells(_(''), "transferring_stock_item_quantity_$id", null, 'align=left');
                        label_cell(_(text_bold('Receiving Asset Qty:')));
                        
                        if($multiple_items == 1){
                            small_amount_cells(_(''), "tran_asset_quantity_$id", null, 'align=left');
                        }else{
                            label_cell(number_format(1, 2));
                            $_POST["tran_asset_quantity_$id"] = 1;
                            hidden("tran_asset_quantity_$id", $_POST["tran_asset_quantity_$id"]);
                        }
                        label_cell('');
                    end_row();

                    alt_table_row_color($k);
                        label_cell(_(text_bold('GRN Line Total:')));
                        $grn_line_total = ($grn_tran_qty * $after_discount);
                        label_cell(number_format($grn_line_total, 2));
                        label_cell('', 'colspan=3'); 
                    end_row();

                    alt_table_row_color($k);
                        label_cell(_(text_bold('GRN Location:')));
                        label_cell($grn_location_name);
                        label_cell(_(text_bold('Asset Location:')));
                        label_cell(asset_locations_list("asset_location_$id", null, '', false));
                        label_cell('');
                    end_row();

                    alt_table_row_color($k);
                        label_cell(_(text_bold('PR Outstanding Qty:')));
                        if (is_numeric($outstanding)) {
                            label_cell(number_format($outstanding, 2));
                        } else {
                            label_cell('');
                        }
                        label_cell(_(text_bold('Qty Transferred:')));
                        $transferred_total_qty = get_stock_asset_transfer_grn_line_transferred_total_qty($id);
                        label_cell(number_format($transferred_total_qty, 2));
                        label_cell('');
                    end_row();
                        
                    $first_row = false;
                }

        end_table(3);
    
    div_end();
}

function can_process_stock_asset_transfer_header($GRN_No, $type) {
    global $Refs;
    
    $header = get_grn_batch($GRN_No);
        if (! is_array($header)) {
            $message = ("Wrong GRN No $GRN_No.");
            $set_focus = '';
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);      
        }
    
    if (! $Refs->is_valid($_POST['reference'])) {
        $message = ("Please enter a reference.");
        $set_focus = 'reference';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);      
    }    
 
    $ref_msg = reference_exists($_POST['reference'], $type, 0);
        if ($ref_msg) {
            $message = $ref_msg;
            $set_focus = 'reference';
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);       
        }
    
    if (! is_date($_POST['tran_date'])) {
        $message = "Date entered is not a valid date.";
        $set_focus = 'tran_date';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);        
    }
    
    if (!is_date_in_fiscalyear($_POST['tran_date'])) {
        $message = (_("The entered invoice date is not in fiscal year."));
        $set_focus = ('tran_date');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
        
    $msg = is_accounting_period_open($_POST['tran_date']);
    if ($msg['success'] !== true)
    {
        $message = ($msg['message']);
        $set_focus = ('tran_date');
        return array("success"=>false, "set_focus"=>$set_focus, "message"=>$message);
    }
    
    
        return array("success" => true, "message" => "");    
}

function can_process_stock_asset_transfer_lines($GRN_No) {
    $selected_lines_count = 0;
        $result = get_grn_and_po_lines_details_by_grn_no($GRN_No);        
            while ($row = db_fetch($result)) {
                $id = $row['grn_line_no'];
                
                if (isset($_POST["checked_$id"]) && $_POST["checked_$id"] == 1) {
                    
                    if (! check_num("transferring_stock_item_quantity_$id", 0.00001)) {
                        $message = _("GRN Line #$id : transferring stock item quantity should be greater than zero.");
                        $set_focus = "transferring_stock_item_quantity_$id";
                        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
                    }

                    if ($_POST["project_id_$id"] == 0) {
                        $message = _("GRN Line #$id : Please select a charging project.");
                        $set_focus = "project_id_$id";
                        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
                    }
                    
                    if ($_POST["task_id_$id"] == 0) {
                        $message = _("GRN Line #$id : please select a budget.");
                        $set_focus = "task_id_$id";
                        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
                    }
                    
                    if (! isset($_POST["asset_id_$id"]) || ! ($_POST["asset_id_$id"] > 0)) {
                        $message = _("GRN Line #$id : Please select an asset.");
                        $set_focus = "asset_id_$id";
                        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
                    }

                    if (! check_num("tran_asset_quantity_$id", 0.00001)) {
                        $message = _("GRN Line #$id : transferring asset quantity should be greater than zero.");
                        $set_focus = "tran_asset_quantity_$id";
                        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
                    }
                    
                    $asset = get_asset($_POST["asset_id_$id"]);
                    if($asset['multiple_items'] == 0){
                        $asset_total_qty = get_asset_total_qty($asset['asset_id']);
                        if ($asset_total_qty > 0) {
                            $message = "Selected asset does not allow multiple items. Already there are $asset_total_qty items under selected asset.";
                            $set_focus = "asset_id_$id";
                            return array("success" => false, "focus_to" => $set_focus, "message" => $message);
                        }
                    }
                    
                    if (! (strlen($_POST["asset_location_$id"]) > 0)) {
                        $message = _("GRN Line #$id : Please select an asset location.");
                        $set_focus = "asset_location_$id";
                        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
                    }
    
                    $selected_lines_count++;
                }
            }
            
            if (! ($selected_lines_count > 0)) {
                $message = "You should select at least one line before saving.";
                $set_focus = "";
                return array("success" => false, "focus_to" => $set_focus, "message" => $message);  
            }

            return array("success" => true, "focus_to" => '', "message" => "");  
}

function save_stock_asset_transfer_header($GRN_No, $type) {
    global $Refs;
        $tran_date = date2sql($_POST['tran_date']);
     
        $inser_id = insert_stock_asset_transfer_header($GRN_No, $type, $_POST['reference'], $tran_date, $_POST['comments']);
            if (! ($inser_id > 0)) {
                return 0;
            }
    
        $Refs->save($type, $inser_id, $_POST['reference']);

        return $inser_id;
}

function save_stock_asset_transfer_lines($trans_no, $type, $GRN_No) {    
    global $systypes_array;
        
        $result = get_grn_and_po_lines_details_by_grn_no($GRN_No, $trans_no);      
            while ($row = db_fetch($result)) {
                $id = $row['id'];
                
                if (isset($_POST["checked_$id"]) && $_POST["checked_$id"] == 1) {
                    
                    $asset_id = $_POST["asset_id_$id"];
                    $project_id = $_POST["project_id_$id"];
                    $task_id = $_POST["task_id_$id"];
                    $asset_location = $_POST["asset_location_$id"];
                    $tran_asset_qty = input_num("tran_asset_quantity_$id");
                    
                    $stock_id = $row['item_code'];
                    
                    $transferring_stock_qty = input_num("transferring_stock_item_quantity_$id");
                    $grn_convertion_factor = $row['convertion_factor']; 
                    $grn_unit_cost = $row['unit_cost']; 
                    
                    $stock_item_base_unit_qty = $transferring_stock_qty * $grn_convertion_factor;
                    $stock_item_base_unit_cost = $grn_unit_cost;
                    
                    $total_value_of_stock_item = $transferring_stock_qty * $grn_convertion_factor * $grn_unit_cost;
                    $asset_value = $total_value_of_stock_item / $tran_asset_qty;
                    
                    $unit = $row['tran_unit']; // grm item unit
                    
                    
                    
                    $po_order_unit_price = $row["po_order_unit_price"];
                    $po_order_unit_price_discount = $row["po_order_unit_price_discount"];
                    $after_discount = ($po_order_unit_price - $po_order_unit_price_discount);
                    
                    $asset_row = get_asset($asset_id);
                    $asset_current_value = $asset_row['current_value'];
                    $existing_asset_total = get_asset_total_qty($asset_id);
                    
                    $existing_total_asset_value = $asset_current_value * $existing_asset_total;
                    
                    $new_asset_total_value = $existing_total_asset_value + ($after_discount + $transferring_stock_qty);
                    
                    $asset_new_current_value = $new_asset_total_value / ($existing_asset_total + $tran_asset_qty);
                    
                    if (! ($asset_new_current_value >= 0)) {
                        $message = _("GRN Line #$id : Asset #$asset_id calculated asset current value is nagative.");
                        return array("success" => false, 'trans_no' => $trans_no, "message" => $message);
                    }
                    
                    $pr_task_id = 0;
                    $po_line_no = $row['po_line_no'];
                    $pr_row = get_pr_line_details_for_po_line($po_line_no);
                        if (is_array($pr_row)) {
                            $pr_task_id = $pr_row['pr_task_id'];
                        }
                    
                    $mb_flag = $row['mb_flag'];
//                        if($mb_flag == "B" || $mb_flag == "M"){
//                            $standard_cost = $row['standard_cost'];
//                        }else{
//                            $standard_cost = $stock_item_base_unit_cost;
//                        }
                    
                    $standard_cost = $stock_item_base_unit_cost; // Stock value has to be adjusted by the Purchased Cost (GRN unit cost)

                    $asat_msg = save_stock_asset_transfer_detail($type, $asset_id, $tran_asset_qty, $stock_id, $unit, $transferring_stock_qty, 
                                            $stock_item_base_unit_qty, $stock_item_base_unit_cost, $row['project_id'], $pr_task_id, 
                                            $asset_value, $row['purch_order_no'], $_POST['reference'], $_POST['tran_date'], $_POST['comments'],
                                            0, $grn_convertion_factor, $row['loc_code'], $asset_location, $project_id, $task_id, 0, 0, $trans_no, $id,
                                            $standard_cost, $mb_flag, $asset_new_current_value);
                    
                        if ($asat_msg['success'] === false) {
                            return $asat_msg;
                        } 
                }
            }

                $ln_ms = $systypes_array[$type]." lines have been saved.";
                
                return array("success" => true, 'trans_no' => $trans_no, "message" => $ln_ms);
}

function save_stock_asset_transfer($GRN_No, $type) {
    global $systypes_array;
    
    $hd_msg = can_process_stock_asset_transfer_header($GRN_No, $type);   
        if ($hd_msg["success"] !== true) {          
            if (isset($hd_msg["focus_to"])) {
                set_focus($hd_msg["focus_to"]);            
            }
            
            return array("success" => false, "message" => $hd_msg["message"]);
        }
    
    $ln_msg = can_process_stock_asset_transfer_lines($GRN_No);
        if ($ln_msg['success'] !== true) {
            if (isset($ln_msg["focus_to"])) {
                set_focus($ln_msg["focus_to"]);            
            }
            
            return array("success" => false, "message" => $ln_msg['message']);        
        }
       
        begin_transaction();
        
            $trans_no = save_stock_asset_transfer_header($GRN_No, $type);
                if (! ($trans_no > 0)) {
                    cancel_transaction();
                    return array("success" => false, "message" => "Could not be saved ".$systypes_array[$type]." header.");
                }
        
            $line_msg = save_stock_asset_transfer_lines($trans_no, $type, $GRN_No);
                if ($line_msg['success'] !== true) {
                    cancel_transaction();
                    return array("success" => false, 'trans_no' => $trans_no, "message" => $line_msg['message']);
                }
                
            $bgt_msg = update_stock_asset_transfer_budgets($trans_no, $type);
                if ($bgt_msg['success'] !== true) {
                    cancel_transaction();
                    return array("success" => false, 'trans_no' => $trans_no, "message" => $bgt_msg['message']);
                }
                
        commit_transaction();

        meta_forward($_SERVER['PHP_SELF'], "SavedID=$trans_no"); 
}

function update_stock_asset_transfer_budgets($trans_no, $type) {
    $result = get_stock_asset_transfer_records($trans_no);    
        while ($row = db_fetch($result)) {
            $qty = abs($row['stock_item_quantity']);

            $b['row_id'] = $row['id'];
            $b['task_id'] = $row['asset_task_id'];
            $b['requested_qty'] = $qty;
            $b['high_value_item'] = 0;

            $standard_cost = $row['stock_item_cost'];
            $b['reference_cost'] = $standard_cost; 
            $b['standard_cost'] = $standard_cost;
            $b['charging_cost'] = $standard_cost;
            $b['total_issued_qty'] = $qty;
            $b['qty'] = $qty;

                $dtr_details_array[] = $b;
        }

            $mi_tasks_summary = calculate_task_remaining_totals($dtr_details_array);
            $created_by = $_SESSION['wa_current_user']->user;
    
            return charge_cost_to_tasks($type, $trans_no, $mi_tasks_summary, $created_by);
}

function view_grn_stock_asset_transfer_header($GRN_No) {
    $grn_header = get_grn_and_po_headers_details_by_grn_no($GRN_No);
  
        start_outer_table(TABLESTYLE2, "width=90%"); // outer table
            start_table(TABLESTYLE2);  
                    table_section(1, "33%");
                        label_row("GRN #:", get_trans_view_str(ST_SUPPRECEIVE, $grn_header["id"]), "class='tableheader2'");  
                        label_row("Supplier:", $grn_header["supp_name"], "class='tableheader2'");  
                        label_row("Reference:", $grn_header["reference"], "class='tableheader2'");  
                        label_row("Purchase Order #:", get_trans_view_str(ST_PURCHORDER, $grn_header["purch_order_no"]), "class='tableheader2'");  

                    table_section(2, "33%");
                        label_row("Delivery Date:", sql2date($grn_header['delivery_date']), "class='tableheader2'");   
                        label_row("Project:", $grn_header['project_description'], "class='tableheader2'");   
                        label_row("Deliver Location:", $grn_header['location_name'], "class='tableheader2'");   

                    table_section(3, "33%");
                        label_row("Delivery Address:", $grn_header['delivery_address'], "class='tableheader2'");   
                        label_row("Comments:", nl2br($grn_header['comments']), "class='tableheader2'");   
            end_table();
        end_outer_table(2); // outer table
}

function view_stock_asset_transfer_header($trans_no) {
    $row = get_stock_asset_transfer_record($trans_no);
    
    view_grn_stock_asset_transfer_header($row['grn_batch_no']);
    
        start_outer_table(TABLESTYLE2, "width=50%");

            table_section(1, "50%");
                label_row(_("GRN No:"), get_trans_view_str(ST_SUPPRECEIVE, $row["grn_batch_no"]), "class='tableheader2'");
                label_row(_("Reference:"), $row['reference'], "class='tableheader2'");
                label_row(_("Trans Date:"), sql2date($row['tran_date']), "class='tableheader2'");

            table_section(2, "50%");
                label_row(_("Comment:"), nl2br($row['comment']), "class='tableheader2'");

        end_outer_table(2);
}

function view_stock_asset_transfer_details($trans_no) {
    start_table(TABLESTYLE, "width=95%");

        labelheader_cell('GRN Stock Item Line', 'colspan=2');
        labelheader_cell('Asset Line', 'colspan=2');
        $th = array(_('Stock Field'), _('Stock Value'), _('Asset Field'), _('Asset Value'));  
        table_header($th);

        $first_row = true;
        $result = get_grn_and_stock_asset_transfer_lines_details_by_stock_asset_trans($trans_no);     
            while ($row = db_fetch($result)) {
                $id = $row['grn_line_no'];
                
                $task_name = $outstanding = $pr_line_text = '' ;
                $po_line_no = $row['po_detail_item'];
                $pr_row = get_pr_line_details_for_po_line($po_line_no);
                    if (is_array($pr_row)) {
                        $task_name = $pr_row['task_name'];
                        $outstanding = $pr_row['quantity'] - ($pr_row['pr_issued_qty'] - $pr_row['pr_returned_qty']);
                        $pr_line_text = '('.$pr_row['pr_no'].') '.$pr_row['pr_line_no'];
                        $pr_line_text = get_supplier_trans_view_str(ST_PURCH_REQ, $pr_row['pr_no'], $pr_line_text);
                    }

                if (! $first_row) {
                    vertical_space("height=18px colspan=4 style='background-color:#97c4c4'");
                }

                start_row();
                    label_cell(_(text_bold('GRN Line #:')));
                    label_cell($id);
                    label_cell('', 'colspan=2');  
                end_row();
                
                start_row();
                    label_cell(_(text_bold('PR #:')));
                    label_cell($pr_line_text);
                    label_cell('', 'colspan=2');  
                end_row();

                start_row();
                    label_cell(_(text_bold('Project:')));
                    label_cell($row['stock_project_description']);
                    label_cell(_(text_bold('Charging Project:')));
                    label_cell($row['asset_project_description']);
                end_row();

                start_row();
                    label_cell(_(text_bold('Budget:')));
                    label_cell($task_name);
                    label_cell(_(text_bold('Charging Budget:')));
                    label_cell($row['asset_task_description']);
                end_row();

                start_row();
                    label_cell(_(text_bold('Item:'))); 
                    $stock_item_name = get_stock_item_formatted_name($row['stock_id'], $row['editable'], $row['sm_item_description'], 
                                                                     $row['grn_item_description']);
                    label_cell($stock_item_name);
                    label_cell(_(text_bold('Asset:')));
                    $asset_name = get_asset_formatted_name($row['asset_id']);
                    label_cell($asset_name);
                end_row();

                start_row();
                    label_cell(_(text_bold('GRN Unit:')));
                    label_cell($row["tran_unit_name"]);
                    label_cell('', 'colspan=2');  
                end_row();

                start_row();
                    label_cell(_(text_bold('GRN Qty:')));
                    $grn_tran_qty = $row["grn_tran_qty"];
                    label_cell(number_format($grn_tran_qty, 2));
                    label_cell('', 'colspan=2');  
                end_row();

                start_row();
                    $po_order_unit_price = $row["po_order_unit_price"];
                    $po_order_unit_price_discount = $row["po_order_unit_price_discount"];
                    $after_discount = ($po_order_unit_price - $po_order_unit_price_discount);

                    label_cell(_(text_bold('GRN Price (After Discount):')));
                    label_cell(number_format($after_discount, 2));
                    label_cell(_(text_bold('Asset Current Value:')));
                    $asset_value = $row['value'];
                    label_cell(number_format($asset_value, 2));
                end_row();
                
                start_row();
                    label_cell(_(text_bold('Issuing Item Qty:')));
                    $tran_qty = $row["tran_qty"];
                    label_cell(number_format($tran_qty, 2));
                    label_cell(_(text_bold('Receiving Asset Qty:')));
                    $asset_quantity = $row["asset_quantity"];
                    label_cell(number_format($asset_quantity, 2));
                end_row();

                start_row();
                    label_cell(_(text_bold('GRN Line Total:')));
                    $grn_line_total = ($grn_tran_qty * $after_discount);
                    label_cell(number_format($grn_line_total, 2));
                    label_cell(_(text_bold('Asset Line Total:')));
                    $asset_line_total = ($asset_quantity * $asset_value);
                    label_cell(number_format($asset_line_total, 2));
                end_row();

                start_row();
                    label_cell(_(text_bold('GRN Location:')));
                    label_cell($row['stock_location_name']);
                    label_cell(_(text_bold('Asset Location:')));
                    label_cell($row['asset_location_name']);
                end_row();

                start_row();
                    label_cell(_(text_bold('PR Outstanding Qty:')));
                        if (is_numeric($outstanding)) {
                            label_cell(number_format($outstanding, 2));
                        } else {
                            label_cell('');
                        }
                    label_cell(_(text_bold('Qty Transferred:')));
                    $transferred_total_qty = get_stock_asset_transfer_grn_line_transferred_total_qty($id);
                    label_cell(number_format($transferred_total_qty, 2));
                end_row();

                $first_row = false;
            }

    end_table(3);
}

?>