<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_STOCK_COUNT';
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/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/inventory/includes/db/stock_counts_db.inc");

$js = '';

if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}


if (!@$_GET['popup'])
	page(_($help_context = "Attendance Entry"));

//----------------------------------------------------------------------------------------
simple_page_mode(true);
if (isset($_GET['employee_code']))
{
	$_POST['employee_code'] = $_GET['employee_code'];
}

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

if (isset($_POST['additem']))
{    
    $input_error = 0;
    
    $myrow = get_stock_count_item($_POST['stock_count_no'] , $_POST['stock_id'] );
    if($myrow)
    {
        $input_error = 1;
        display_error( _("This item is already in the stock count."));
        set_focus('from_date');            
    }
        //return;
    if($input_error != 0)
        return;
    
    begin_transaction();

    if($_POST['stock_count_no']>0){
        $stock_count_id = $_POST['stock_count_no'];
    }
    else{
        $stock_count_id = add_stock_count_header($_POST['location_code'], $_POST['sc_description']);
    }
    
    add_stock_count_item($stock_count_id,$_POST['stock_id']);
    
    commit_transaction();

    if($stock_count_id > 0)
        display_notification(_("stock count has been added."));
    else {
        display_notification(_("stock count could not be added"));
    }
    
    $Ajax->activate('_page_body');
}

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


function clear_data()
{
	unset($_POST['long_description']);
}



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


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


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


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

if ($Mode == 'RESET')
{
	$selected_id = -1;
}

//if (list_updated('employee_code')) 
//	$Ajax->activate('Entitlements');
//--------------------------------------------------------------------------------------------------


start_form();

    stock_counts_list_cells(_("Select an item:"), 'stock_count_no', null,
	  _('New Stock Count'), true, check_value('show_inactive'));

br(2);

$Ajax->activate('attendance');


$result = get_stock_count_details($_POST['stock_count_no']);

div_start('attendance');

start_table(TABLESTYLE, "width=25%");

if(isset($_POST['stock_count_no']))
{
    if($_POST['stock_count_no']>0){
        $sc_row = get_stock_count_header($_POST['stock_count_no']);
        label_row("Location:",$sc_row['location_name']);
        
        $_POST['sc_description'] = $sc_row["description"];
        text_row("Description:", 'sc_description', $_POST['sc_description'], 20, 200);
    }
    else{
        locations_list_row ("Select Location:", "location_code");
        text_row("Description:", 'sc_description', "", 20, 200);
    }
}

end_table();

br(1);

if (db_num_rows($result) == 0)
{
    display_note(_("There are no items added in this stock count"));
}
else
{
    start_table(TABLESTYLE, "width=65%");

            $th = array(_("Id"),_("Itemcode"),_("Item Description"), _("System Qty"), 
                        _("Counted Qty"),_("Status") ,"");

    table_header($th);
    $last_header_date ="";

    $k = $j = 0; //row colour counter

    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);
        
        $_POST['id'] = $myrow["id"];
        $_POST['stock_id'] = $myrow["stock_id"];
        $_POST['description'] = $myrow["description"];
        
        $_POST['system_qty'] = $myrow["system_qty"];
        $_POST['counted_qty'] = $myrow["counted_qty"];
        $_POST['status'] = $myrow["status"];

        //label_cell($_POST['id']);
        label_cell($_POST['id']);
        label_cell($_POST['stock_id']);
        label_cell($_POST['description']);
        
        label_cell($_POST['system_qty']);
        label_cell($_POST['counted_qty']);
        label_cell($_POST['status']);
        

            //edit_button_cell("Edit".$myrow['id'], _("Edit"));
            edit_button_cell("Delete".$myrow['id'], _("Delete"));           
     

        end_row();
        
     //end of while loop
    }

    end_table();
}
div_end();

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


br();

hidden('selected_id', $selected_id);



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

start_table(TABLESTYLE2);

stock_costable_items_list_row(_("Itemcode:"),'stock_id', null, false, false);

end_table(1);
div_end();

submit_center('additem', _("Add item"), true, '', 'default');

br(2);

start_table(TABLESTYLE2);

stock_categories_list_row(_("Item Cetegory:"),'stock_category', null, false, false);

end_table(1);

submit_center('add_category_items', _("Add All Items of This Category"), true, '', 'default');

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