<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_ENTER_COUNTED_DATA';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$path_to_root = "..";

include_once($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 . "/includes/data_checks.inc");
include_once($path_to_root . "/inventory/includes/inventory_db.inc");
include_once($path_to_root . "/inventory/includes/db/stock_count_db.inc");
include_once($path_to_root . "/inventory/includes/stock_count_ui.inc");
include_once($path_to_root . "/inventory/includes/enter_counted_data_ui.inc");
include_once($path_to_root . "/inventory/includes/db/complete_stock_count_db.inc");



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


check_db_has_costable_items(_("There are no inventory items defined in the system (Purchased or manufactured items)."));

//------------------------------------------------------------------------------------


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

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

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

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

page("Enter Conted Data", false, false, "", $js);

simple_page_mode(true);

//------------------------------------------------------------------------------------
if (!@$_GET['popup'])
	start_form();

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




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

if (isset($_POST['CancelItemChanges'])){

    $selected_id = -1;

}

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

if (isset($_POST['EndCounting'])){
    
    if(can_end_count($stock_count_id)){
        begin_transaction();
        $n2 = update_stock_count_header_status($stock_count_id, SC_COUNTED, SC_COUNTING);
        if($n2 == 1){
            update_stock_count_details_all_status($stock_count_id, SC_COUNTED, SC_COUNTING);
            $csc_msg = complete_stock_count($stock_count_id);
            if($csc_msg['success'] === true){
                display_notification($csc_msg['message']);
                commit_transaction();
            }
            else{
                cancel_transaction();
                display_notification("Counting ended for Stock count $stock_count_id.");
            }
        }
        else{
            cancel_transaction();
            display_error("Another user has updated Stock count $stock_count_id.");
        }
    }
    
}

display_stock_count_header($stock_count_id);

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

    start_table(TABLESTYLE_NOBORDER); 
    stock_count_sheets_list_row("Sheet No :", 'sheet_no', $stock_count_id, null, false, true);
    end_table(1);
    
display_stock_count_details($stock_count_id, $selected_id);
div_end();
//display_stock_count_details_edit_controls($stock_count_id, $selected_id);


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


if (!@$_GET['popup'])
{
	end_form();
	end_page(@$_GET['popup'], false, false);
}
?>

