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

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/payroll/includes/payroll_db.inc");

if (!@$_GET['popup'])
	page(_($help_context = "Promotion Benefit Items"));

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

simple_page_mode(true);
//---------------------------------------------------------------------------------------------------
$input_error = 0;

if (isset($_GET['employee_code']))
{
	$_POST['employee_code'] = $_GET['employee_code'];
}
//---------------------------------------------------------------------------------------------------
if (!@$_GET['popup'])
	start_form();

if (!isset($_POST['employee_code']))
	//$_POST['employee_code'] = get_global_employee_code();

if (!@$_GET['popup'])
{
	/*echo "<center>" . _("Item:"). "&nbsp;";
	echo sales_items_list('employee_code', $_POST['employee_code'], false, true, '', array('editable' => false));
	echo "<hr></center>";
         * 
         */
}
else
	br(2);
//set_global_employee_code($_POST['employee_code']);

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

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

	$input_error = 0;
    if(!is_date($_POST['work_start_date']))
    {
        $input_error = 1;
        display_error( _('Work start date is not a valid date'));
        set_focus('work_start_date');
    }
    if(!is_date($_POST['contract_start_date']))
    {
        $input_error = 1;
        display_error( _('Contract start date is not a valid date'));
        set_focus('contract_start_date');
    }
    if(!is_date($_POST['contract_end_date']))
    {
        $input_error = 1;
        display_error( _('Contract end date is not a valid date'));
        set_focus('contract_end_date');
    }
    if(date1_greater_date2($_POST['contract_start_date'], $_POST['contract_end_date'])){
        $input_error = 1;
        display_error( _('Contract end date can not be earlier than contract start date'));
        set_focus('contract_end_date');
    }
    if(date1_greater_date2($_POST['work_start_date'], $_POST['contract_end_date'])){
        $input_error = 1;
        display_error( _('Work start date can not be earlier than contract end date'));
        set_focus('work_start_date');
        //return;
    }
    

	if ($input_error == 0)
	{

                if ($selected_id != -1) 
		{
			//editing an existing price
			update_employment_detail($selected_id, $_POST['designation'],
                                $_POST['employment_type_id'],$_POST['job_category_id'],
                                date2sql($_POST['work_start_date']),date2sql($_POST['contract_start_date']),
                                date2sql($_POST['contract_end_date']));

			$msg = _("Employment detail has been updated.");
		}
		else
		{
            add_employment_detail($_POST['employee_code'], $_POST['designation'],
                                $_POST['employment_type_id'],$_POST['job_category_id'],
                                date2sql($_POST['work_start_date']),date2sql($_POST['contract_start_date']),
                                date2sql($_POST['contract_end_date']));

                        $msg = _("Employment detail has been added.");
		}
		display_notification($msg);
		$Mode = 'RESET';
	}

}

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

if ($Mode == 'Delete')
{
	//the link to delete a selected record was clicked
	delete_employment_detail($selected_id);
	display_notification(_("The selected employment detail has been deleted."));
	$Mode = 'RESET';
}

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

if (list_updated('stock_id')) {
	$Ajax->activate('price_table');
	$Ajax->activate('price_details');
}
if (list_updated('stock_id') || isset($_POST['_curr_abrev_update']) || isset($_POST['_sales_type_id_update'])) {
	// after change of stock, currency or salestype selector
	// display default calculated price for new settings. 
	// If we have this price already in db it is overwritten later.
	unset($_POST['price']);
	$Ajax->activate('price_details');
}

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

$employment_details_list = get_employment_details($_POST['employee_code']);

div_start('price_table');
start_table(TABLESTYLE, "width=90%");

$th = array(
    _("Designation"), _("Employment Type"), _("Job Category"),  _("Work Start Date"), _("Contract Start date"),  _("Contract End date"), "", "");
table_header($th);
$k = 0; //row colour counter
$calculated = false;


while ($myrow = db_fetch($employment_details_list))
{

	alt_table_row_color($k);

    	label_cell($myrow["designation"],"align=center");
        label_cell($myrow["employment_type_name"]);
        label_cell($myrow["job_category"],"align=center");
        label_cell(sql2date($myrow["work_start_date"]),"align=center");
        label_cell(sql2date($myrow["contract_start_date"]),"align=center");
        
        if($myrow["contract_end_date"] == "3000-01-01"){
            label_cell("");
        }
        else{
            label_cell(sql2date($myrow["contract_end_date"]),"align=center");
        }
 	edit_button_cell("Edit".$myrow['id'], _("Edit"));
 	delete_button_cell("Delete".$myrow['id'], _("Delete"));
        
        end_row();

}

end_table();

/*if (db_num_rows($prices_list) == 0)
{
	if (get_company_pref('add_pct') != -1)
		$calculated = true;
	display_note(_("There are no prices set up for this part."), 1);
}
 * 
 */
div_end();
//------------------------------------------------------------------------------------------------

echo "<br>";

if ($Mode == 'Edit')
{

        $job_detail = get_employment_detail($selected_id);
        $job_detail_row  = db_fetch($job_detail);
//        var_dump($promotion_benefit_item_row);
//        echo $selected_id;
	$_POST['designation'] = $job_detail_row["designation"];
        $_POST['employment_type_id'] = $job_detail_row["employment_type_id"];
        $_POST['job_category_id'] = $job_detail_row["job_category_id"];
        $_POST['work_start_date'] = sql2date($job_detail_row["work_start_date"]);
        $_POST['contract_start_date'] = sql2date($job_detail_row["contract_start_date"]);
        $_POST['contract_end_date'] = sql2date($job_detail_row["contract_end_date"]);

}
else{
    $_POST['contract_end_date'] = sql2date(INFINITE_END_DATE);
}

hidden('selected_id', $selected_id);
if (@$_GET['popup'])
{
	hidden('_tabs_sel', get_post('_tabs_sel'));
	hidden('popup', @$_GET['popup']);
}
div_start('price_details');
start_table(TABLESTYLE2);

//value_list_row(_("Relationship:"), 'family_member_relationship_id', 'F_MEMBERS');

text_row(_("Designation:"), 'designation', null, 50, 500);

if(isset($_POST['employment_type_id'])){
    value_list_row(_("Employment Type:"), 'employment_type_id', "EMPLOYMENT_TYPES", $_POST['employment_type_id']);
}
else {
    value_list_row(_("Employment Type:"), 'employment_type_id', "EMPLOYMENT_TYPES");
}

if(isset($_POST['job_category_id'])){
    value_list_row(_("job Category:"), 'job_category_id', "JOB_CATEGORY", $_POST['job_category_id']);
}
else {
    value_list_row(_("Job Category:"), 'job_category_id', "JOB_CATEGORY");
}

date_row(_('Work Start Date:'), 'work_start_date');
date_row(_('Contract Start Date:'), 'contract_start_date');
date_row(_('Contract End Date:'), 'contract_end_date');

end_table(1);

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

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