<?php
/**********************************************************************
	Copyright (C) Evolve Techniologies
	Author : Chathuri Gunawardana
	Email : chathuri@evolve-sl.com
	Company : Evolve Technologies(pvt)Ltd
***********************************************************************/
$page_security = 'SA_SALESGROUP';
$path_to_root = "../..";
include($path_to_root . "/includes/session.inc");

page(_($help_context = "Course Registration"));

include($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/sales/includes/db/course_registration_db.inc");

simple_page_mode(true);

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

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
{
	//initialise no input errors assumed initially before we test
	$input_error = 0;

	if (strlen($_POST['courseName']) == 0)
	{
		$input_error = 1;
		display_error(_("The course name cannot be empty."));
		set_focus('courseName');
	}
	/*if (!check_num('courseRegFee', 0)) {
		$input_error = 1;
		display_error( _("Course reg. Fee must be numeric and not less than 0."));
		set_focus('courseRegFee');
	}*/
	if ($input_error != 1)
	{
                if ($selected_id != -1) 
                {
                        /*selected_id could also exist if submit had not been clicked this code would not run in this case cos submit is false of course  see the delete code below*/
                                //update_salesman($selected_id, $_POST['salesman_name'], $_POST['salesman_phone'], $_POST['salesman_fax'],$_POST['salesman_email'], input_num('provision'), input_num('break_pt'), input_num('provision2'));
                                update_courses($selected_id, $_POST['courseName'], $_POST['courseDuration']/*,input_num('courseRegFee')*/);
                }
                else
                {
                        /*Selected group is null cos no item selected on first time round so must be adding a record must be submitting new entries in the new Sales-person form */
                                //add_salesman($_POST['salesman_name'], $_POST['salesman_phone'], $_POST['salesman_fax'],$_POST['salesman_email'], input_num('provision'), input_num('break_pt'), input_num('provision2'));
                                add_courses($_POST['courseName'], $_POST['courseDuration']/*,input_num('courseRegFee')*/);
                }

        if ($selected_id != -1) 
			display_notification(_('Selected course has been updated'));
		else
			display_notification(_('New course has been added'));
			
		$Mode = 'RESET';
	}
}
if ($Mode == 'Delete')
{
	//the link to delete a selected record was clicked instead of the submit button

	// PREVENT DELETES IF DEPENDENT RECORDS IN 'cust_branch'

	$cat_id = get_mainCat_cat_id($selected_id);
	
	//if (key_in_foreign_table($selected_id, 'cust_branch', 'course_id'))
	if (key_in_foreign_table($cat_id, 'stock_category', 'main_category_id'))
	{
		display_error(_("Cannot delete because batch/es are created for this course."));
	}
	else
	{
		delete_courses($selected_id);
		display_notification(_('Selected course has been deleted'));
	}
	$Mode = 'RESET';
	
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
	$sav = get_post('show_inactive');
	unset($_POST);
	$_POST['show_inactive'] = $sav;
	if ($sav) $_POST['show_inactive'] = 1;
	
	/***********BOF Chat Added******************/
	$_POST['courseName'] = -1;
	//$_POST['courseShortName'] = '';
	$_POST['courseDuration'] = -1;
	//$_POST['courseRegFee'] = 0.00;
	/***********EOF Chat Added******************/
}
//------------------------------------------------------------------------------------------------

//$result = get_salesmen(check_value('show_inactive'));
$result = get_all_courses(check_value('show_inactive'));

start_form();
start_table(TABLESTYLE, "width=60%");
//$th = array(_("Name"), _("Phone"), _("Fax"), _("Email"), _("Provision"), _("Break Pt."), _("Provision")." 2", "", "");

//$th = array (_('Course Full Name'), _('Course Short Name'), _('Total Duration (Years)'),_('Total Course Fee'), '','');
$th = array (_('Course Full Name'), _('Total Duration (Years)'), '','');

inactive_control_column($th);
table_header($th);

$k = 0;

while ($myrow = db_fetch($result))
{
	alt_table_row_color($k);
	
	label_cell($myrow["course_full_name"]);
	//label_cell($myrow["course_short_name"]);
	label_cell($myrow["total_duration"]);
	//amount_cell($myrow["total_course_fee"]);
	inactive_control_cell($myrow["id"], $myrow["inactive"], 'courses', 'id');
	
 	//edit_button_cell("Edit".$myrow["id"], _("Edit"));
 	//delete_button_cell("Delete".$myrow["id"], _("Delete"));
  	end_row();
	
} //END WHILE LIST LOOP

inactive_control_row($th);
end_table();
echo '<br>';

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

if ($selected_id != -1) 
{
 	if ($Mode == 'Edit') {
		//editing an existing courses
		
		$myrow2 = get_courses($selected_id);
		
		$_POST['courseName']  = $myrow2["course_full_name"];
		//$_POST['courseShortName']  = $myrow2["course_short_name"];
		$_POST['courseDuration']  = $myrow2["total_duration"];
		//$_POST['courseRegFee']  = number_format2($myrow2["total_course_fee"],2);
	}
	hidden('selected_id', $selected_id);
	
	
} elseif ($Mode != 'ADD_ITEM') {
	
		//$_POST['courseRegFee']  = number_format2(1,2);
		$_POST['courseName'] = '';
		$_POST['courseShortName'] = '';
		$_POST['courseDuration'] = -1;
}

start_table(TABLESTYLE2);

text_row_ex(_("Course Full Name").':', 'courseName', null);
//text_row_ex(_("Course Short Name").':', 'courseShortName', null);
totDuration_list_row(_("Course Duration:"), 'courseDuration', null);
//amount_row(_("Total Course Fee:"), 'courseRegFee', null);

end_table(1);

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

end_form();

end_page();

?>
