<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_LISTS';
$path_to_root = "..";
include_once($path_to_root . "/includes/session.inc");

page(_($help_context = "Lists"));

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
//include_once($path_to_root . "/includes/manufacturing.inc");
include_once($path_to_root . "/includes/data_checks.inc");

//below file should have been included by the system already. But it is not. So included manually. Once the issues found remove this inclusion
include_once($path_to_root . "/admin/db/list_values_db.inc");
//check_db_has_purchasable_items(_("There are no inventory items defined in the system."));

simple_page_mode(true);
//--------------------------------------------------------------------------------------------------


function display_lists()
{
    $result = get_all_lists();

    br();

    div_start('code_table');
	start_table(TABLESTYLE, "width=60%");

	$th = array( _("Description"),_("Code"), _("Type"), 
		    _("Group List"), _("Active"), "", "");

        table_header($th);

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

        while ($myrow = db_fetch($result))
        {
	    alt_table_row_color($k);            
           
            label_cell($myrow["list_name"]);
            label_cell($myrow["list_code"]);            
            label_cell($myrow["list_type"]);            
            label_cell($myrow["group_name"]);               
                      
            if ($myrow["inactive"] == 0) 
                    $display_status = "Yes";
            else 
                    $display_status = "No";
                
            label_cell($display_status);          
           
                                  
	    edit_button_cell("Edit".$myrow['list_id'], _("Edit"));
	    edit_button_cell("Delete".$myrow['list_id'], _("Delete"));
            end_row();

            $j++;
            If ($j == 12)
            {
            	$j = 1;
        		table_header($th);
            } //end of page full new headings
        } //end of while loop

	end_table();
    div_end();
}

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
{

   	$input_error = 0;
    
    if (strstr($_POST['list_code'], " ") || strstr($_POST['list_code'],"'") || 
        strstr($_POST['list_code'], "+") || strstr($_POST['list_code'], "\"") || 
        strstr($_POST['list_code'], "&") || strstr($_POST['list_code'], "\t")||
        strstr($_POST['list_code'], "%"))    
    {
        $input_error = 1;
        display_error( _('The code cannot contain any of the following characters -  & + % OR a space OR quotes'));
        set_focus('list_code');

    }
    
   	if ($_POST['list_code'] == "" || !isset($_POST['list_code']))
   	{
      	$input_error = 1;
      	display_error( _("There is no list selected."));
		set_focus('list_code');
   	}

   	elseif ($_POST['list_name'] == '')
   	{
      	$input_error = 1;
      	display_error( _("Value name cannot be empty."));
		set_focus('list_name');
   	}
    
    if ($Mode == 'ADD_ITEM'){
        $existing_list = get_list($_POST['list_code']);
        if(isset($existing_list['list_code'])){
            $input_error = 1;
            display_error( _("Already there is list with this code."));
            set_focus('list_code');
        }
    }

	
	if ($input_error == 0)
	{
     	if ($Mode == 'ADD_ITEM') 
       	{
			add_list($_POST['list_type'], $_POST['list_code'],
                        $_POST['list_name'], $_POST['inactive'], $_POST['group_list_code'],
                        $_POST['text1_active'], $_POST['text1_name'], input_num('text1_length'),
                        $_POST['text2_active'], $_POST['text2_name'], input_num('text2_length'),
                        $_POST['amount1_active'], $_POST['amount1_name'], input_num('amount1_decimals'),
                        $_POST['date1_active'], $_POST['date1_name']); 				

    		display_notification(_("New list has been added."));
       	} else
       	{
			update_list($selected_id, $_POST['list_type'], 
                        $_POST['list_name'], $_POST['inactive'], $_POST['group_list_code'],
                        $_POST['text1_active'], $_POST['text1_name'], input_num('text1_length'),
                        $_POST['text2_active'], $_POST['text2_name'], input_num('text2_length'),
                        $_POST['amount1_active'], $_POST['amount1_name'], input_num('amount1_decimals'),
                        $_POST['date1_active'], $_POST['date1_name']); 

    	  	display_notification(_("Selected List has been updated."));
       	}
		$Mode = 'RESET';
	}
}
//------------------------------------------------------------------------------------------------

function can_delete($selected_id)
{
    $list = get_list_by_id($selected_id);
    $result = get_all_list_values($list['list_code']);
    if(db_num_rows($result) > 0){
        display_error("There are values created for this list.");
        return false;
    }
    
    return true;
}
//--------------------------------------------------------------------------------------------------

if ($Mode == 'Delete')
{
   if(can_delete($selected_id)){
    delete_list($selected_id);
	
	display_notification(_("Selected List has been sucessfully deleted."));
	$Mode = 'RESET';
    }
}

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

if (list_updated('stock_id')) 
	$Ajax->activate('_page_body');

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

start_form();

//if (!isset($_POST['list_id']))
//	$_POST['list_id'] = get_global_list_code();

//echo "<center>" . _("Select List Code:"). "&nbsp;";
//echo "<center>" ;
//Chaitanya : Manufcatured item visible
//echo lists_list('list_code', '', $_POST['list_code'], false, true);
//echo stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true);

echo "<hr></center>";


display_lists();


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

if ($selected_id > 0) {
	if ($Mode =='Edit')
	{
		$myrow = get_list_by_id($selected_id);
		$_POST['list_type'] = $myrow["list_type"];
		$_POST['list_code'] = $myrow["list_code"];
		$_POST['list_name'] = $myrow["list_name"];
		$_POST['inactive'] = $myrow["inactive"];
        $_POST['group_list_code'] = $myrow["group_list_code"];
        
        $_POST['text1_active'] = $myrow["text1_active"];
		$_POST['text1_name'] = $myrow["text1_name"];
		$_POST['text1_length'] = $myrow["text1_length"];
        
        $_POST['text2_active'] = $myrow["text2_active"];
		$_POST['text2_name'] = $myrow["text2_name"];
		$_POST['text2_length'] = $myrow["text2_length"];
        
        $_POST['amount1_active'] = $myrow["amount1_active"];
		$_POST['amount1_name'] = $myrow["amount1_name"];
		$_POST['amount1_decimals'] = $myrow["amount1_decimals"];
        
        $_POST['date1_active'] = $myrow["date1_active"];
		$_POST['date1_name'] = $myrow["date1_name"];
                
	}
	hidden('selected_id', $selected_id);
} 
else {
	$_POST['list_type'] = '';
	$_POST['list_code'] = '';
    $_POST['list_name'] = '';
	$_POST['inactive'] = 0;
    $_POST['list_value_group_id'] = '';
}
if(!isset($_POST['inactive'])) 
    $_POST['inactive'] = 0;

echo "<br>";
start_table(TABLESTYLE2);

hidden('list_id', $selected_id);


if($selected_id>0) {
    label_row(_("Code:"), $_POST['list_code'], null, 50, 50);   
    hidden('list_code', $_POST['list_code']);
} else {
    text_row(_("Code:"), 'list_code', null, 50, 50);
}

text_row(_("Description:"), 'list_name', null, 50, 100);
text_row(_("Type:"), 'list_type', null, 20, 20);
yesno_list_row(_("Status:"), 'inactive', $_POST['inactive'], 'Inactive', 'Active');
lists_list_row("Group List:", 'group_list_code', null, null, 'No Grouping', false);

label_row("", spaces(1));
yesno_list_row(_("Text1 Active :"), 'text1_active', null, 'Yes', 'No');
text_row(_("Text1 Name:"), 'text1_name', null, 50, 50);
small_qty_row("Text1 Length :", 'text1_length', null, null, null, 0);

label_row("", spaces(1));
yesno_list_row(_("Text2 Active :"), 'text2_active', null, 'Yes', 'No');
text_row(_("Text2 Name:"), 'text2_name', null, 50, 50);
small_qty_row("Text2 Length :", 'text2_length', null, null, null, 0);

label_row("", spaces(1));
yesno_list_row(_("Amount1 Active :"), 'amount1_active', null, 'Yes', 'No');
text_row(_("Amount1 Name:"), 'amount1_name', null, 50, 50);
small_qty_row("Amount1 Decimals :", 'amount1_decimals', null, null, null, 0);

label_row("", spaces(1));
yesno_list_row(_("Date1 Active :"), 'date1_active', null, 'Yes', 'No');
text_row(_("Date1 Name:"), 'date1_name', null, 50, 50);

end_table(1);
submit_add_or_update_center($selected_id == -1, '', 'both');

end_form();
end_page();

?>
