<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_STOCK_COUNT';
$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui/ui_input.inc");
include_once($path_to_root . "/includes/ui/ui_lists_custom.inc"); 
include_once($path_to_root . "/includes/ui/ui_view_custom.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"); 

$js = "";
$js .= get_js_open_window(800, 500);
$js .= get_js_date_picker();
 
page("Asset Verification", false, false, "", $js);

$asset_verification_count_no = -1;

if (isset($_GET['ModifyAssetVerificationCountNo']) && $_GET['ModifyAssetVerificationCountNo'] > 0) {
    $asset_verification_count_no = $_POST['ModifyAssetVerificationCountNo'] = $_GET['ModifyAssetVerificationCountNo'];
} elseif (isset($_POST['ModifyAssetVerificationCountNo']) && $_POST['ModifyAssetVerificationCountNo'] > 0) {
    $asset_verification_count_no = $_POST['ModifyAssetVerificationCountNo'];
}

if (isset($_GET['AddedID'])) {
    $trans_no = $_GET['AddedID'];
    $trans_type = ST_ASSET_VERIFICATION;

	display_notification_centered(_("Asset Verification ".$trans_no." has been created."));
        
	display_note(get_asset_trans_view_str($trans_type, $trans_no, _("&View this Asset Verification #$trans_no")), 0, 1);

        submenu_option(_("Enter &Another Asset Verification"),	"/assets/manage/asset_verification_count.php?NewAssetVerification=Yes");
        
	display_footer_exit();
}

if (isset($_GET['UpdatedID'])) {
    $trans_no = $_GET['UpdatedID'];
    $trans_type = ST_ASSET_VERIFICATION;

	display_notification_centered(_("Stock count ".$trans_no." has been updated"));
        
	display_note(get_asset_trans_view_str($trans_type, $trans_no, _("&View this Asset Verification")), 0, 1);

        submenu_option(_("Enter &Another Asset Verification"),	"/assets/manage/asset_verification_count.php?NewAssetVerification=Yes");
        
	display_footer_exit();
}

if (isset($_GET['CountingID'])) {
    $trans_no = $_GET['CountingID'];
    $trans_type = ST_ASSET_VERIFICATION;

	display_notification_centered(_("Counting started for stock count ".$trans_no));
        
	display_note(get_asset_trans_view_str($trans_type, $trans_no, _("&View this Asset Verification")), 0, 1);

	display_footer_exit();
}

if (isset($_POST['AdAssetVerification']) && can_process_asset_verification_count()) {
    $trans_no = create_new_asset_verification_count();    
   	meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
} 

if (isset($_POST['UpdateAssetVerification']) && can_process_asset_verification_count()) {
    $sc_result = edit_asset_verification_count_header($asset_verification_count_no);           
	if ($sc_result) {
            meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$asset_verification_count_no");
        } else {
            display_error("Could not update stock count.");
        }
}

if (isset($_POST['StartCounting']) && can_process_asset_verification_count()) {
    $sc_result = edit_asset_verification_count_header($asset_verification_count_no);           
	if ($sc_result) {
            $n3 = update_asset_verification_count_header_status($asset_verification_count_no, SC_COUNTING, SC_NEW);
                if ($n3 == 1) {
                    meta_forward($_SERVER['PHP_SELF'], "CountingID=$asset_verification_count_no");
                } else {
                    display_error("Another user has updated asset verification count #$asset_verification_count_no.");
                }
        } else {
            display_error("Could not update asset verification count #$asset_verification_count_no.");
        }
}

global $Ajax;

    start_form();

        hidden('ModifyAssetVerificationCountNo', $asset_verification_count_no);
        
        $Ajax->activate('ast_vrf_header');
        div_start("ast_vrf_header");
            display_asset_verification_count_edit_controls($asset_verification_count_no);

            if ($asset_verification_count_no > 0) {
                submit_center_first('UpdateAssetVerification', _("Update Asset Verification"), '', 'default');
                submit_center_last('StartCounting', _("Start Counting"), '', 'default');
            } else {
                submit_center_first('AdAssetVerification', _("Create Asset Verification"), '', 'default'); 
            }
        div_end();

    end_form(3);
    
end_page();

?>