<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_SUPPTRANSVIEW';
$path_to_root = "../..";
include($path_to_root . "/purchasing/includes/po_class.inc");
include($path_to_root . "/includes/session.inc");
include($path_to_root . "/purchasing/includes/ui/purchasing_ui.inc");
include($path_to_root . "/purchasing/includes/db/purchase_requisitions_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");
include_once($path_to_root . "/imports/includes/db/import_purchase_orders_db.inc");
include_once($path_to_root . "/imports/includes/db/import_purch_order_costing_detail_db.inc");
include_once($path_to_root . "/imports/includes/db/import_payment_terms_db.inc");
 
        

$js = "";
if($use_popup_windows)
    $js .= get_js_open_window(900, 500);

page(_($help_context = "View Import Purchase Order #".$_GET['trans_no']), true, false, "", $js);

if(!isset($_GET['trans_no'])){
    die ("<br>" . _("This page must be called with a purchase order number to review."));
}

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

function view_import_purchase_order_header($order_no){
    
    global $approval_statuses;
    display_heading(_("Import Purchase Order #$order_no"));
    br();
    $local_po_header = get_local_purchase_order_header($order_no);
    $import_po_header = get_import_purchase_order_header($order_no);
    
        start_outer_table(TABLESTYLE2, "width=88%"); // outer table
            start_table(TABLESTYLE2);  
                table_section(1);
                
                    label_row("Supplier:", $local_po_header['supp_name'], "class='tableheader2'");
                    label_row("Date:", sql2date($local_po_header['ord_date']), "class='tableheader2'");
                    supplier_credit_row($local_po_header['supplier_id'], null, "class='tableheader2'");
                    label_row(_("Supplier Currency:"), $local_po_header['curr_code'], "class='tableheader2'");
                    if(isset($local_po_header['curr_code']) && strlen($local_po_header['curr_code']) > 0){
                        $curr_rate = get_date_exchange_rate($local_po_header['curr_code'], sql2date($local_po_header['ord_date']));
                        label_row(_("Currency Rate:"), number_format($curr_rate,2), "class='tableheader2'");
                    }else{
                        hidden('curr_rate', 0);
                    }
                    label_row(_("Reference:"), $local_po_header['reference'], "class='tableheader2'");
              
                table_section(2);
                    label_row(_("Supplier's Reference:"), $local_po_header['requisition_no'], "class='tableheader2'");
                    label_row(_("Receive Into:"), $local_po_header['location_name'], "class='tableheader2'");
                    $terms_indicator_name = get_import_payment_term($local_po_header['terms_indicator']);
                    label_row(_("Payment Terms:"), $terms_indicator_name['description'], "class='tableheader2'");
                    label_row(_("Payment Term Details:"), $import_po_header['payment_term_details'], "class='tableheader2'");
                    label_row(_("Project:"), $local_po_header['project_description'], "class='tableheader2'");
                    

                table_section(3);
                    label_row(get_project_trans_view_str(ST_APPROVAL_LOG, $order_no."&source_type_id=".ST_PURCHORDER, "Approval Status"), 
                              $approval_statuses[$local_po_header['approval_status']], "class='tableheader2'");
                    label_row(_("Deliver to:"), $local_po_header['saved_delivery_address'], "class='tableheader2'");
                    $ipo_details_rec_count = import_purch_order_costing_details_has_db_records($order_no);
                    if($ipo_details_rec_count > 0){
                        label_row('Landed Costs:', get_trans_view_str(ST_IMPORT_PO_COST_DETAILS, $order_no, "View Import PO Costing Details"), "class='tableheader2'");
                    }
                    else{
                        label_row('Landed Costs:', "No Import PO Costing Details", "class='tableheader2'");
                    }

            end_table();
        end_outer_table(1);
}

function view_import_purchase_order_details($order_no){
    global $Ajax;   
    display_heading(_("Import Order Items"));
        $result = get_purchase_order_details($order_no);     
         
        start_table(TABLESTYLE, "width=98%");
        $th = array(_('PO Item#'), _('Order#'), _('Item Code'), _("Item Description"), _("Quantity"), _('Received'), 
                    _('Unit'), _('Delivery Date'), _('Site'), _("Price"), _('Discount'), _("Line Total"));              

        table_header($th);
        $k = 0; 

        $total_po = 0;
        $line_total = 0;
            while ($myrow = db_fetch($result)){
                $line_no = $myrow["po_detail_item"];
                alt_table_row_color($k);

                    label_cell($myrow['po_detail_item'], 'align=center');
                    label_cell($myrow['order_no'], 'align=center');
                    label_cell($myrow['item_code'], 'align=center');
                    label_cell($myrow['item_description']);
                    qty_cell($myrow['order_unit_qty'], false, get_qty_dec($myrow['item_code']));
                    qty_cell($myrow['quantity_received'], false, get_qty_dec($myrow['item_code']));
                    if($myrow['order_unit'] == ""){
                        label_cell($myrow['units'], 'align=center');
                    }else{
                        label_cell($myrow['order_unit'], 'align=center');
                    }
                    label_cell(sql2date($myrow['delivery_date']), 'align=center');
                    if($myrow['project_site_name'] != null){
                        label_cell($myrow['project_site_name'], 'align=center');
                    }else{
                        label_cell('', 'align=center');
                    }
                    amount_decimal_cell($myrow['order_unit_price']);
                    amount_decimal_cell($myrow['order_unit_price_discount']);
                    $discounted_minimum_price = $myrow['minimum_price'] * (1-$myrow['minimum_price_discount']/100);
                    amount_cell($myrow['line_total']);
                        $line_total += $myrow['line_total'];
                        hidden('line_total', $line_total);
                end_row();
            }

            $ipo_header_details = get_import_purchase_order_header($order_no);

            start_row();
                if(strlen($ipo_header_details['sub_total_name']) > 0){
                    label_cell($ipo_header_details['sub_total_name'].":", "align=right style='font-weight:bold; color:black;' colspan=11");
                }else{
                    label_cell("Sub Total:", "align=right style='font-weight:bold; color:black;' colspan=11");
                }
                label_cell(number_format($line_total, 2), "align=right style='font-weight:bold; color:black;'");
            end_row();
            start_row();
                if(strlen($ipo_header_details['freight_name']) > 0){
                    label_cell($ipo_header_details['freight_name'].":", "align=right style='font-weight:bold; color:black;' colspan=11");
                }else{
                    label_cell("Freight:", "align=right style='font-weight:bold; color:black;' colspan=11");
                }
                label_cell(number_format($ipo_header_details['freight_value'], 2), "align=right style='font-weight:bold; color:black;'");
            end_row();
            start_row();
                if(strlen($ipo_header_details['insurance_name']) > 0){
                    label_cell($ipo_header_details['insurance_name'].":", "align=right style='font-weight:bold; color:black;' colspan=11");
                }else{
                    label_cell("Insurance:", "align=right style='font-weight:bold; color:black;' colspan=11");
                }
                label_cell(number_format($ipo_header_details['insurance_value'], 2), "align=right style='font-weight:bold; color:black;'");
            end_row();
            start_row();
                if(strlen($ipo_header_details['total_name']) > 0){
                    label_cell($ipo_header_details['total_name'].":", "align=right style='font-weight:bold; color:black;' colspan=11");
                }else{
                    label_cell("Total Display Name:", "align=right style='font-weight:bold; color:black;' colspan=11");
                }
                $total = $line_total + $ipo_header_details['freight_value'] + $ipo_header_details['insurance_value'];
                label_cell(number_format($total, 2), "align=right style='font-weight:bold; color:black;'");
            end_row();
        end_table();
}

view_import_purchase_order_header($_GET['trans_no']);
view_import_purchase_order_details($_GET['trans_no']);

end_page(true, false, false, ST_PURCHORDER, $_GET['trans_no']);

?>