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

include($path_to_root . "/includes/session.inc");

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/inventory/includes/db/stock_items_adjustment_inquiry_db.inc");


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


page(_($help_context = "View Inventory Adjustment"), true, false, "", $js);


if (isset($_GET["trans_no"]))
{
	$trans_no = $_GET["trans_no"];
}

display_heading($systypes_array[ST_INVENTORY_ISSUE] . " #$trans_no");

br(1);
$adjustment_items = view_stock_adjustment_items(ST_INVENTORY_ISSUE, $trans_no);
$total_cost = $k = 0;
$header_shown = false;
while ($adjustment = db_fetch($adjustment_items))
{

	if (!$header_shown)
	{
		$adjustment_type = get_movement_type($adjustment['person_id']) ;

		start_outer_table(TABLESTYLE2, "width=60%");
    
                    table_section(1, '50%');
                            label_row(_("Location:"), $adjustment['location_name'], "class='tableheader2'");
                            label_row(_("Date:"), sql2date($adjustment['ai_date']), "class='tableheader2'");
                            label_row(_("Type:"), $adjustment['movement_type'], "class='tableheader2'");
                            label_row(_("Project:"), $adjustment['project_name'], "class='tableheader2'");
                            label_row(_("Sample Items:"), $adjustment['sample_items'], "class='tableheader2'");

                    table_section(2, '50%');
                        global $inventory_movement_type_linked_fields;
                        label_row(_("Movement Type:"), $adjustment['movement_type'], "class='tableheader2'");

                        if ($adjustment['linked_field_type'] != IMLF_NOT_LINKED) {
                            $link_field = get_movement_type_linked_field_values($adjustment['linked_field_type'], $adjustment['linked_field_id']);
                            label_row(_($inventory_movement_type_linked_fields[$adjustment['linked_field_type']].":"), $link_field['value'], "class='tableheader2'");
                        }
                        
                        label_row(_("Reference:"), $adjustment['reference'], "class='tableheader2'");
                        $s = get_inventory_trans_view_str(ST_INVENTORY_ISSUE_REQUEST, $adjustment['sar_id']>0?$adjustment['sar_id']:null);
                        label_row(_("Request:"), $s, "class='tableheader2'");
                        end_row();

                end_outer_table(2);
                
                $comment = $adjustment['comments'];
                    if (strlen($comment) > 0) {
                        start_table(TABLESTYLE);
                            label_row(_("Comment: "), nl2br($comment), "class='tableheader2'"); 
                        end_table(2, false);
                    }
                        
		$header_shown = true;

		echo "<br>";
		start_table(TABLESTYLE, "width=90%");

    	
    	$th = array(_("Item"), _("Description"), _("Job"), _("Units"), _("Quantity"), _("Unit Cost"), _("Line Cost"), _("Budget"));
        
        table_header($th);
        
	}

    alt_table_row_color($k);

    label_cell($adjustment['stock_id']);
    label_cell($adjustment['description']);
    label_cell($adjustment['job']);
    label_cell($adjustment['tran_unit']);
    
    qty_cell($adjustment['tran_qty'], false, get_qty_dec($adjustment['stock_id']));   
    $tran_unit_cost = $adjustment['standard_cost'] * $adjustment['convertion_factor'];
    $line_cost = $tran_unit_cost * $adjustment['tran_qty'];
    
    if ($_SESSION["wa_current_user"]->can_access("SA_VIEW_ITEM_COST")){
        label_cell(number_format($tran_unit_cost, 2), "align=right");        
        label_cell(number_format($line_cost, 2), "align=right");  
    }
    else{
        label_cell("");
        label_cell("");
    }

    label_cell($adjustment['task_description']);
    

    end_row();
    
    $total_cost += $line_cost;
    
}

start_row();
label_cell("Total", "align=right colspan=5");
label_cell("<b>".number_format($total_cost, 2)."</b>", "align=right");
//label_cell("", "colspan=2");
end_row();

end_table(1);

is_voided_display(ST_INVADJUST, $trans_no, _("This adjustment has been voided."));

end_page(true, false, false, ST_INVADJUST, $trans_no);
?>