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

$js = "";
$js .= get_js_open_window(800, 500);

page(_($help_context = "View Asset Verification"), true, false, "", $js);

simple_page_mode();

if (isset($_GET["trans_no"])) {
   $trans_no = $_GET["trans_no"];
} elseif (isset($_POST["trans_no"])) {
   $trans_no = $_POST["trans_no"];
} else {
    $trans_no = -1;
}

function view_asset_verification_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["asset_verification_count_no"], 'align=center');
                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(nl2br($sc_header["comments"]), "colspan=5");
                end_row();
            }

	end_table(1);
}

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

    $sc_items = get_view_asset_verification_count_details($trans_no, $sheet_no);
        if (! (db_num_rows($sc_items) > 0)) {
            display_note('No any asset verification count item entered.', 2, 2, "style=color:red");
            return;
        }
        
    display_heading2("Asset Verification Items");    br();
        
    start_table(TABLESTYLE, "width=90%");

        $th = array(_("Asset"), _("Description"), _("Good Qty"), _("Damaged Qty"), _("System Qty"), _("Qty Diff"),  _("Current Value"), 
                    _("Value Diff"), _("Line Status"));
        table_header($th);

            $dec = 2;
            $k = $total_adj_value = 0;

            while ($myrow = db_fetch($sc_items)) {
                alt_table_row_color($k);
                    label_cell($myrow["asset_code"].' ('.$myrow["asset_id"].')', 'align=center');
                    label_cell($myrow["description"]);

                    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");
                    $adj_qty = $myrow["good_qty"] + $myrow["damaged_qty"] - $myrow["system_qty"];
                    label_cell(number_format($adj_qty, $dec), "align=right");
                    label_cell(number_format($myrow["current_value"], $dec), "align=right");

                    $adj_value = $adj_qty * $myrow["current_value"];
                    label_cell(number_format($adj_value, 2), "align=right");

                    label_cell($stock_counting_statuses[$myrow['status_id']],"align=center");
                end_row();
                
                $total_adj_value += $adj_value;
            }
                alt_table_row_color($k);
                    label_cell(text_bold('Total'), "align=right colspan=7");
                    label_cell(text_bold(number_format($total_adj_value, $dec)), "align=right");
                    label_cell('');
                end_row();

    end_table(2);
}

$sc_header = get_asset_verification_count_view_header($trans_no);
    if (! is_array($sc_header)) {
        display_note(_("The asset verification 3$trans_no is not valid."));
        exit;
    }
    
    start_form();
    
        hidden('trans_no', $trans_no);

        display_heading($systypes_array[ST_ASSET_VERIFICATION] . " #$trans_no");    br();
        view_asset_verification_count_header($sc_header);     br();

        start_table(TABLESTYLE_NOBORDER);
            asset_verification_count_sheets_list_row("Sheet No:", 'sheet_no', $trans_no, null, false, true);
        end_table(2);

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

    end_form();

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

?>