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

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/assets/includes/db/assets_db.inc"); 
include_once($path_to_root . "/assets/includes/ui/asset_verification_count_ui.inc"); 
include_once($path_to_root . "/assets/includes/db/asset_verification_count_db.inc"); 
include_once($path_to_root . "/assets/includes/ui/enter_asset_verification_counted_data_ui.inc");
include_once($path_to_root . "/assets/includes/db/complete_asset_verification_count_db.inc"); 

$js = "";
$js .= get_js_open_window(1000, 500);
$js .= get_js_date_picker();


if (isset($_POST['UpdateData'])){
    $asset_verification_count_no = $_SESSION['asset_verification_count_no'];
} 

if (isset($_GET['asset_verification_count_no'])) {
    $_POST['asset_verification_count_no'] = $_GET['asset_verification_count_no'];
}

if (isset($_POST['asset_verification_count_no'])) {
    $asset_verification_count_no = $_POST['asset_verification_count_no'];
}

if (list_updated('stock_id')) {
    $Ajax->activate('show_heading');
    $Ajax->activate('reorders');
}

page("Enter Asset Counted Data", false, false, "", $js);

simple_page_mode(true);

start_form();

if (!isset($_POST['stock_id'])) {
    $_POST['stock_id'] = get_global_stock_item();
}

if (isset($_POST['AddItem']) || isset($_POST['UpdateItem'])) {
    save_asset_verification_count_detail($selected_id, $asset_verification_count_no);
}

if (isset($_POST['CancelItemChanges'])){
    $selected_id = -1;
}

$delete_id = find_submit("Delete");
if ($delete_id > 0) {
    delete_asset_verification_count_detail_item($delete_id, $asset_verification_count_no);
    $selected_id = -1;
}

if (isset($_POST['EndCounting'])) {
    if (can_asset_verification_end_count($asset_verification_count_no)) {
        
        begin_transaction();
            $n2 = update_asset_verification_count_header_status($asset_verification_count_no, SC_COUNTED, SC_COUNTING);
                if ($n2 == 1) {
                    update_asset_verification_count_details_all_status($asset_verification_count_no, SC_COUNTED, SC_COUNTING);
                    
                    $csc_msg = complete_asset_verification_count($asset_verification_count_no);
                        if ($csc_msg['success'] === true) {
                            commit_transaction();
                            display_notification($csc_msg['message']);
                        } else {
                            cancel_transaction();
                            display_error($csc_msg['message']);
                        }
                } else {
                    cancel_transaction();
                    display_error("Another user has updated asset verification count $asset_verification_count_no.");
                }
    }
}

    global $systypes_array;
    display_heading($systypes_array[ST_ASSET_VERIFICATION]." #$asset_verification_count_no");    br();

    display_asset_verification_count_header($asset_verification_count_no);

    $Ajax->activate('sc_details');
    div_start('sc_details');

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

        display_asset_verification_count_details($asset_verification_count_no, $selected_id);
    div_end();

    submit_center('EndCounting', _("End Counting"), true, false, 'default');

end_form(3);
end_page(@$_GET['popup'], false, false);

?>