<?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_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"); 
include_once($path_to_root . "/assets/includes/db/asset_transfer_db.inc"); 

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

if($_GET['trans_no'] != ""){
    $trans_no = $_GET['trans_no'];
}
if($_GET['type'] != ""){
    $trans_type = $_GET['type'];
}

global $systypes_array, $approval_statuses;

page(_($help_context = $systypes_array[$trans_type]), true, false, "", $js);


$row = view_asset_transfer_header_by_type_trans_no($trans_type, $trans_no);
    if (! is_array($row)) {
        display_heading("Asset Transfer #$trans_no or type is not valid.");
        
        display_footer_exit();
    }
    
    $trans_type = $row['type'];
    $asset_transfer_id = $row['id'];

$woid = 0;

display_heading("$systypes_array[$trans_type] #$trans_no");

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

        start_row();
            label_cells("Reference", $row['reference'], "class='tableheader2'");
        end_row();
        if ($trans_type != ST_ASSET_TRANSFER) {
            start_row();
                if ($trans_type == ST_ASSET_REQUISITION_TRANSFER) {
                    $req_link = get_asset_trans_view_str(ST_ASSET_TRANSFER_REQUEST, $row['atr_id']);
                    label_cells($systypes_array[ST_ASSET_TRANSFER_REQUEST], $req_link, "class='tableheader2'");
                } else {
                    $issue_h = get_asset_transfer_header_record($row['atr_id']);
                    $issue_link = get_asset_trans_view_str(ST_ASSET_REQUISITION_TRANSFER, $issue_h['trans_no']);
                    label_cells($systypes_array[ST_ASSET_REQUISITION_TRANSFER], $issue_link, "class='tableheader2'");
                }
            end_row();
        }
        start_row();
            label_cells(_("From Project"), $row['from_project_description'], "class='tableheader2'");
        end_row();
        start_row();
            label_cells(_("To Project"), $row['to_project_description'], "class='tableheader2'");
        end_row();
        start_row();
            label_cells(_("From Location"), $row['from_location_name'], "class='tableheader2'");
        end_row();
        start_row();
            label_cells(_("To Location"), $row['to_location_name'], "class='tableheader2'");
        end_row();
        start_row();
            label_cells(_("Company"), get_group_company_name($row['company_code']), "class='tableheader2'");
        end_row();
        start_row();
            $employee_name = get_group_company_employee($row['company_code'], $row['employee_code']);
            label_cells(_("Employee"), $employee_name['employee_code'].' - '.$employee_name['initials'].' '.$employee_name['last_name'], "class='tableheader2'");
        end_row();
        start_row();
            label_cells(_("Date"), sql2date($row['tran_date']), "class='tableheader2'");
        end_row();
        start_row();
            label_cells(_("Comment"), $row['comment'], "class='tableheader2'");
        end_row();
        start_row();
            label_cells(_("Total"), number_format($row['total'], 2), "class='tableheader2'");
        end_row();
        if ($trans_type != ST_ASSET_TRANSFER_RECEIPT) {
            start_row();
                label_cells(_("Approval Status"), $approval_statuses[$row['approval_status']], "class='tableheader2'");
            end_row();
        }

    end_table(2);

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

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

        display_heading($systypes_array[$trans_type]." Details");
        
        if ($trans_type == ST_ASSET_TRANSFER) {
            $th = array(_("Line ID"), _('Asset'), _("Qty"), _("Asset Value"), _('Line Total'));  
        } elseif ($trans_type == ST_ASSET_TRANSFER_RECEIPT) {
            $th = array(_("Line ID"), _("Req. Line ID"), _('Asset'), _("Qty"), _("Asset Value"), _('Line Total'));  
        } else {
            $th = array(_("Line ID"), _("Req. Line ID"), _('Asset'), _("Transferred Qty"), _("Received Qty"), _("Asset Value"), _('Line Total'));  
        }

        $asset_requisition_transfer_ids_array = array($asset_transfer_id);

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

        $line_total = 0;
        
            if ($trans_type == ST_ASSET_TRANSFER) {
                $result = view_asset_transfer_details($asset_transfer_id);
                $cmnt_colspan = 6;  $tot_colspan = 4;
            } elseif ($trans_type == ST_ASSET_TRANSFER_RECEIPT) {
                $result = view_asset_transfer_details($asset_transfer_id);
                $cmnt_colspan = 7;  $tot_colspan = 5;
            } else {
                $result = view_asset_requisition_transfer_details_with_AT_RCPT_counts($asset_transfer_id);
                $cmnt_colspan = 8;  $tot_colspan = 6;
            }
                while($row = db_fetch($result)){

                    alt_table_row_color($k);

                        label_cell($row['id'], "align=center");
                        if ($trans_type != ST_ASSET_TRANSFER) {
                            label_cell($row['atr_line_id'], "align=center");
                        }
                        label_cell($row['asset_code']."-".$row['asset_description']."-"."(".$row['asset_id'].")");
                        label_cell(number_format($row['quantity'], 2), "align=right");
                        
                        if ($trans_type == ST_ASSET_REQUISITION_TRANSFER) {
                            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=$cmnt_colspan");
                            end_row();
                        }
                        
                    end_row();
                    
                    $line_total += $line_cost;
                    $k++;
                    
                }
        
        start_row();
            label_cell("Total", "colspan=$tot_colspan align=right");
            amount_cell($line_total, true);    
        end_row();

    end_table(1);
    
    
    if ($trans_type == ST_ASSET_TRANSFER_RECEIPT || $trans_type == ST_ASSET_TRANSFER) {
        end_page(true, false, false, ST_ASSET_TRANSFER, $asset_transfer_id);
        
        return;
    }
    
    //-----------------------------------------------------------------------------------------------
    
    start_table(TABLESTYLE2, "width=40%");

        display_heading("Asset Transfer Receipts");

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

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

            $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);
    

end_page(true, false, false, ST_ASSET_TRANSFER, $asset_transfer_id);

?>