<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_STOCK_COUNT_VIEW';
$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_count_db.inc");
include_once($path_to_root . "/inventory/includes/db/stock_count_inquiry_db.inc");

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

page(_($help_context = "View Stock Count"), true, false, "", $js);
simple_page_mode();

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

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

$parameter = $_SERVER['QUERY_STRING'];

function view_stock_count_header($sc_header)
{
    global $stock_counting_statuses;
    
   	start_table(TABLESTYLE, "width=80%");

	$th = array(_("#"), _("Reference"), _("Location"), 
                    _("Date"), _("Status"));

	table_header($th);
	start_row();
        label_cell($sc_header["stock_count_no"]);
        label_cell($sc_header["reference"],"align=center");
        label_cell($sc_header["loc_code"]." - ".$sc_header["location_name"],"align=center");
        label_cell(sql2date($sc_header["tran_date"]),"align=center");
        label_cell($stock_counting_statuses[$sc_header['status_id']],"align=center");
    end_row();
        
    if(strlen($sc_header["comments"] > 0)){
        start_row();
            label_cell($sc_header["comments"], "colspan=5");
        end_row();
    }

	//comments_display_row(ST_MATERIAL_ISSUE, $mrt_no);

	end_table();

}

function display_stock_count_details($trans_no, $header_status_id, $sheet_no)
{
    global $stock_counting_statuses;

    $sc_items = view_stock_count_details($trans_no, $sheet_no);
    $k = $count = 0;

    if($header_status_id < SC_COMPLETING){
            $th = array(_("Item"), _("Description"), _("Units"),
                            _("Good Qty"), _("Damaged Qty"), _("Line Status"));
    }
    else{
            $th = array(_("Item"), _("Description"), _("Unit"),
                            _("Good Qty"), _("Damaged Qty"), _("System Qty"), 
                            _("Qty Diff"),  _("Unit Cost"), _("Value Diff"),
                            _("Line Status"));
    }

    $th = array(_("Stock_ID"),_("Description"), 
        
                nl2br("Tran \r\n Unit"), 
                nl2br("Tran \r\n Unit \r\n Good \r\n Qty"), 
                nl2br("Tran \r\n Unit \r\n Damaged \r\n Qty"),
        
                nl2br("Base \r\n Unit"), 
                nl2br("Conv. \r\n Factor"), 
        
                nl2br("Base \r\n Unit \r\n Good \r\n Qty"), 
                nl2br("Base \r\n Unit \r\n Damaged \r\n Qty"), 
                nl2br("Base \r\n Unit \r\n System \r\n Qty"),
        
                nl2br("Base \r\n Unit \r\n Cost"),
                nl2br("Base \r\n Unit \r\n Adj \r\n Qty"),
                nl2br("Base \r\n Unit \r\n Adj \r\n Value"),
                nl2br("Tran \r\n Unit \r\n Adj \r\n Qty"),
                nl2br("Line \r\n Status")
        );
    
    
    start_table(TABLESTYLE, "width=90%");

    table_header($th);

    $dec = 2;
    $total_adj_value = 0;
    
    while ($myrow = db_fetch($sc_items))
    {
        alt_table_row_color($k);

        //label_cell($sc_row['stock_id']);
        view_stock_status_cell($myrow['stock_id']);
        label_cell($myrow["description"]);
        
        $convertion_factor = $myrow['conversion_factor'];
        label_cell($myrow["tran_unit_name"]);
        label_cell(number_format($myrow["tran_unit_good_qty"], $dec), "align=right");
        label_cell(number_format($myrow["tran_unit_damaged_qty"], $dec), "align=right");
        
        label_cell($myrow["base_unit_name"]);
        label_cell($convertion_factor, "align=right");
        label_cell(number_format($myrow["good_qty"], $dec), "align=right");
        label_cell(number_format($myrow["damaged_qty"], $dec), "align=right");
        label_cell(number_format($myrow["system_qty"], $dec), "align=right");
        
        label_cell(number_format($myrow["standard_cost"], 2), "align=right");
        $base_unit_adj_qty = $myrow["good_qty"] + $myrow["damaged_qty"] - $myrow["system_qty"];
        label_cell(number_format($base_unit_adj_qty, $dec), "align=right");
        $base_unit_adj_value = $base_unit_adj_qty * $myrow["standard_cost"];
        label_cell(number_format($base_unit_adj_value, 2), "align=right");
        
        label_cell(number_format($base_unit_adj_qty/$convertion_factor, $dec), "align=right");
        
        
        label_cell($stock_counting_statuses[$myrow['status_id']],"align=center");
        end_row();
        
        $total_adj_value += $base_unit_adj_value;
        $count++;
    }
    
    start_row();
    label_cell("No of records: ".$count, "colspan=12");
    amount_cell($total_adj_value);
    label_cell("", "colspan=2");
    end_row();

    end_table(1);
}

$sc_header = get_stock_count_view_header($trans_no);
if (!$sc_header)
{
    display_note(_("The stock count sent is not valid."));
    exit;
}
    
start_form();

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

view_stock_count_header($sc_header);
br();

hidden('stc_no', $trans_no);


display_heading2("Stock Count Items");

stock_count_sheets_list_row("Sheet No:", 'sheet_no', $trans_no, null, false, true);

display_stock_count_details($trans_no, $sc_header['status_id'], $_POST['sheet_no']);

end_form();

end_page(true, false, false, ST_STOCK_COUNT, $trans_no);

?>