<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_ASSETTRANSVIEW';
$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/assets/includes/db/asset_transfer_requests_inquiry_db.inc");
include_once($path_to_root . "/includes/common_functions.inc");     
include_once($path_to_root . "/includes/db/group_companies_db.inc");   
include_once($path_to_root . "/admin/db/users_db.inc"); 

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
page(_($help_context = "View Asset Transfer Request"), true, false, "", $js);

//-------------------------------------------------------------------------------------------------
$woid = 0;
if($_GET['trans_no'] != ""){
    $asset_transfer_request_id = $_GET['trans_no'];
}

display_heading("Asset Transfer Request No: " . $asset_transfer_request_id);

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

        $myrow = view_asset_transfer_request($asset_transfer_request_id);

        start_row();
            label_cells("Reference", $myrow['reference'], "class='tableheader2'");
        end_row();
        start_row();
            label_cells(_("From Project"), $myrow['from_project_description'], "class='tableheader2'");
        end_row();
        start_row();
            label_cells(_("To Project"), $myrow['to_project_description'], "class='tableheader2'");
        end_row();
        start_row();
            label_cells(_("From Location"), $myrow['from_location_name'], "class='tableheader2'");
        end_row();
        start_row();
            label_cells(_("To Location"), $myrow['to_location_name'], "class='tableheader2'");
        end_row();
        start_row();
            label_cells(_("Date"), sql2date($myrow['tran_date']), "class='tableheader2'");
        end_row();
        start_row();
            label_cells(_("Comment"), $myrow['comment'], "class='tableheader2'");
        end_row();
        start_row();
            label_cells(_("Total"), number_format($myrow['total'], 2), "class='tableheader2'");
        end_row();
//        start_row();
//            $created_by = get_user($myrow['created_by']);
//            label_cells(_("Created BY"), $created_by['real_name'], "class='tableheader2'");
//        end_row();
//        start_row();
//            label_cells(_("Created On"), sql2date($myrow['created_on']), "class='tableheader2'");
//        end_row();
//        start_row();
//            $updated_by = get_user($myrow['updated_by']);
//            label_cells(_("Updated BY"), $updated_by['real_name'], "class='tableheader2'");
//        end_row();
//        start_row();
//            label_cells(_("Updated On"), sql2date($myrow['updated_on']), "class='tableheader2'");
//        end_row();
//        start_row();
//            $approved_by = get_user($myrow['approved_by']);
//            label_cells(_("Approved BY"), $approved_by['real_name'], "class='tableheader2'");
//        end_row();
//        start_row();
//            label_cells(_("Approved On"), sql2date($myrow['approved_on']), "class='tableheader2'");
//        end_row();

        $s = get_project_trans_view_str(ST_APPROVAL_LOG, $myrow['id']."&source_type_id=".ST_ASSET_TRANSFER_REQUEST, "Approval Status");
             label_cells($s,$approval_statuses[$myrow['approval_status']], "class='tableheader2'");  

    end_table(2);

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

    start_table(TABLESTYLE2, "width=98%");

        display_heading("Asset Transfer Request Details");

        $th = array(_("ID"),_("Asset"), _('From Site'),_('To Site'), _("Requested Qty"), _("Transferred Qty"), _("Intransit Qty"), _("Received Qty"),
                    _("Asset Value"),_('Line Total'));  

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

        $line_total =0;
            $result = view_asset_transfer_request_details_with_ART_and_AT_RCPT($asset_transfer_request_id);
                while($row = db_fetch($result)){

                    alt_table_row_color($k);

                        label_cell($row['id'], "align=center");
                        label_cell($row['asset_code']."-".$row['asset_description']."-"."(".$row['asset_id'].")");
                        label_cell($row['from_site_description']);
                        label_cell($row['to_site_description']);
                        label_cell(number_format($row['quantity'], 2), "align=right");
                        label_cell(number_format($row['transferred_qty'], 2), "align=right");
                        
                        $at_receipt_total = get_asset_transfer_receipts_total_by_AT_REQ_line_id($row['id']);
                        $intransit_qty = $row['transferred_qty'] - $at_receipt_total;
                            label_cell(number_format($intransit_qty, 2), "align=right");
                        
                        label_cell(number_format($row['received_qty'], 2), "align=right");
                        label_cell(number_format($row['asset_value'], 2), "align=right");
                        $line_cost = $row["asset_value"] * $row["quantity"];
                        label_cell(number_format($line_cost, 2), "align=right");

                        if(strlen($row['line_comment']) > 0){
                            start_row();
                                $k +=1;
                                label_cell('');
                                label_cell('<b>Comment :</b> '.$row['line_comment'], 'colspan=9');
                            end_row();
                        }
                        
                    end_row();
                        
                        $line_total += $line_cost;
                        $k++;
                }
        
        start_row();
            label_cell("Total", "colspan=9 align=right");
            amount_cell($line_total, true);    
        end_row();

    end_table(2);
    
//-----------------------------------------------------------------------------------------------
    
    start_table(TABLESTYLE2, "width=30%");

        display_heading("Asset Transfer Issues");
        
            $asset_requisition_transfer_ids_array = array();

            $th = array(_("Trans #"), _("Reference"), _('Date'));  

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

            $result = view_asset_requistion_transfers_belong_to_asset_transfer_request($asset_transfer_request_id, ST_ASSET_REQUISITION_TRANSFER);
                while($row = db_fetch($result)){

                    alt_table_row_color($k);

//                        label_cell($row['id'], 'align=center');
                        label_cell(get_asset_trans_view_str(ST_ASSET_REQUISITION_TRANSFER, $row['trans_no']), "align=center");
                        label_cell($row['reference']);
                        label_cell(sql2date($row['tran_date']), "align=center");
                        
                    end_row();
                    
                    $asset_requisition_transfer_ids_array[] = $row['id'];
                }

    end_table(2);
    
    
    
//-----------------------------------------------------------------------------------------------
    
    start_table(TABLESTYLE2, "width=30%");

        display_heading("Asset Transfer Receipts");

            $th = array(_("Trans #"), _("Reference"), _('Date'));  

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

            if (count($asset_requisition_transfer_ids_array) > 0) {
                $result = view_asset_transfer_receipts_belong_to_asset_requisition_transfer($asset_requisition_transfer_ids_array);
                    while($row = db_fetch($result)){

                        alt_table_row_color($k);

    //                        label_cell($row['id'], 'align=center');
                            label_cell(get_asset_trans_view_str(ST_ASSET_TRANSFER_RECEIPT, $row['trans_no']), "align=center");
                            label_cell($row['reference']);
                            label_cell(sql2date($row['tran_date']), "align=center");

                        end_row();
                    }
            }

    end_table(1);
    
    br(2);

end_page(true, false, false, ST_ASSET_TRANSFER_REQUEST, $asset_transfer_request_id);

?>