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

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

include_once($path_to_root . "/includes/ui/items_cart.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui/ui_controls.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/inventory/includes/stock_count_ui.inc");
include_once($path_to_root . "/inventory/includes/db/stock_count_db.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");


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

 
page("Stock Count", false, false, "", $js);

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

check_db_has_costable_items(_("There are no inventory items defined in the system which can be adjusted (Purchased or Manufactured)."));

check_db_has_movement_types(_("There are no inventory movement types defined in the system. Please define at least one inventory adjustment type."));

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

$stock_count_no = -1;

if (isset($_GET['ModifyStockCountNo']))
{
    $_POST['ModifyStockCountNo'] = $_GET['ModifyStockCountNo'];
}
if (isset($_POST['ModifyStockCountNo']) && $_POST['ModifyStockCountNo'] > 0)
{
    $stock_count_no = $_POST['ModifyStockCountNo'];
}


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

	display_notification_centered(_("Stock count ".$trans_no." has been created"));
        
	display_note(get_trans_view_str($trans_type, $trans_no, _("&View this Stock Count")), 0, 1);

	display_note(print_document_link($trans_no, _("&Print this Stock Count"), true, $trans_type), 0, 1);

	//hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter &Another Stock Count"));
    submenu_option(_("Enter &Another Stock Count"),	"/inventory/stock_count.php?NewStockCount=Yes");
        
	display_footer_exit();
}

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

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

	display_note(print_document_link($trans_no, _("&Print this Stock Count"), true, $trans_type), 0, 1);

	//hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter &Another Stock Count"));
    submenu_option(_("Enter &Another Stock Count"),	"/inventory/stock_count.php?NewStockCount=Yes");
        
	display_footer_exit();
}

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

	display_notification_centered(_("Counting started for stock count ".$trans_no));
        
	display_note(get_trans_view_str($trans_type, $trans_no, _("&View this Stock Count")), 0, 1);

	display_note(print_document_link($trans_no, _("&Print this Stock Count"), true, $trans_type), 0, 1);

	//hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter &Another Stock Count"));
//    submenu_option(_("Enter &Another Stock Count"),	"/inventory/stock_count.php?NewStockCount=Yes");
        
	display_footer_exit();
}

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

function line_start_focus() {
  global $Ajax;

  $Ajax->activate('items_table');
  set_focus('_stock_id_edit');
}

function can_process()
{
    if (($_POST['reference'] == "")){
		display_error(_("Reference can not be empty"));
		set_focus('reference');
		return false;
	}

    if(!($_POST['project_id'] > 0)){
        display_error("Please select a Project.");
        set_focus('project_id');
        return false;
    }

    if(!($_POST['task_id'] > 0)){
        display_error("Please select a budget.");
        set_focus('task_id');
        return false;
    }
    
    if(!(strlen($_POST['location']) > 0)){
        display_error("Please select a location.");
        set_focus('location');
        return false;
    }

if(!(is_date($_POST['tran_date']))){
        display_error("Entered date is not valid.");
        set_focus('tran_date');
        return false;
    }     

	return true;
}

function display_stock_count_edit_controls($stock_count_no)
{
    if($stock_count_no > 0){
        $myrow = get_stock_count_header($stock_count_no);

        if(!(isset($_POST['project_id']) && $_POST['project_id'] > 0)){
            $_POST['project_id'] = $myrow['project_id'];
        }

        if(!(isset($_POST['task_id']) && $_POST['task_id'] > 0)){
            $_POST['task_id'] = $myrow['task_id'];
        }

        if(!(isset($_POST['reference']))){
            $_POST['reference'] = $myrow['reference'];
        }

        if(!(isset($_POST['location']))){
            $_POST['location'] = $myrow['loc_code'];
        }

        if(!(isset($_POST['tran_date']))){
            $_POST['tran_date'] = sql2date($myrow['tran_date']);
        }
        
        if(!(isset($_POST['comments']))){
            $_POST['comments'] = $myrow['comments'];
        }
    }
    
    start_table(TABLESTYLE2); 

//    ref_row(_("Reference:"), 'reference', '');
    text_row(_("Reference:"), 'reference', null, 30, 100);
    
    project_list_row(_("Project:"), 'project_id', null, _('Select project'), true);
    hidden('ModifyStockCountNo', $stock_count_no);

    tasks_list_row(_("Budget:"), 'task_id', $_POST['project_id'], null, _('Select a task'));

    locations_list_row(_("Location:"), 'location', null, '', false);

    date_row(_("Date:"), 'tran_date');

    stock_count_status_list_cells(_("Status:"), 'sid', null, false);
    
    textarea_row("Comments", "comments", '', 35, 3);

    end_table(1);
}

function create_new_stock_count()
{
    $trans_no = add_stock_count_header( $_POST['location'], $_POST['reference'], 
                                            SC_NEW , $_POST['tran_date'],  $_POST['comments'],
                                            $_POST['project_id'],$_POST['task_id'] );
    
    return $trans_no;
}

function edit_stock_count_header($stock_count_no)
{
    $n = update_stock_count_header($stock_count_no, $_POST['location'], $_POST['reference'], 
                                                SC_NEW, $_POST['tran_date'],  $_POST['comments'],
                                                $_POST['project_id'],$_POST['task_id'] );
    
    if($n == 1){
        return true;
    }
    else{
        return false;
    }
}
//------------------------------------------------------------------------------------------

if (isset($_POST['AdStockCount']) && can_process()){

    $trans_no = create_new_stock_count();    
	
   	meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
   
} 

if (isset($_POST['UpdateStockCount']) && can_process()){
           
    $sc_result = edit_stock_count_header($stock_count_no);           
	if($sc_result){
        meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$stock_count_no");
    }
    else{
        display_error("Could not update stock count.");
    }
    
}

if (isset($_POST['StartCounting']) && can_process()){
           
    $sc_result = edit_stock_count_header($stock_count_no);           
	if($sc_result){
        $n3 = update_stock_count_header_status($stock_count_no, SC_COUNTING, SC_NEW);
        if($n3 == 1){
            meta_forward($_SERVER['PHP_SELF'], "CountingID=$stock_count_no");
        }
        else{
            display_error("Another user has updated stock count $stock_count_no.");
        }
    }
    else{
        display_error("Could not update stock count.");
    }
    
}


//-----------------------------------------------------------------------------------------------
    
global $Ajax;

start_form();

$Ajax->activate('sc_header');

div_start("sc_header");
display_stock_count_edit_controls($stock_count_no);

if($stock_count_no > 0){
    submit_center_first('UpdateStockCount', _("Update Stock Count"), '', 'default');
    submit_center_last('StartCounting', _("Start Counting"), '', 'default');
}else{
    submit_center_first('AdStockCount', _("Create Stock Count"), '', 'default'); 
}

div_end();
 
end_form();
end_page();




?>


