<?php
/**********************************************************************
    Copyright (C) FrontAccounting, LLC.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/
$page_security = 'SA_EMPL';
$path_to_root = '../../..';
include_once($path_to_root . "/includes/session.inc");
add_access_extensions();

$js = "";
if (user_use_date_picker())
	$js .= get_js_date_picker();

include_once($path_to_root . '/includes/ui.inc');
include_once($path_to_root . '/includes/date_functions.inc');
include_once($path_to_root . '/modules/FrontHrm/includes/frontHrm_db.inc');
include_once($path_to_root . '/modules/FrontHrm/includes/db/payroll_db.inc');
include_once($path_to_root . '/modules/FrontHrm/includes/frontHrm_ui.inc');
include_once($path_to_root . '/modules/church/includes/ui/member_ui.inc');

page(_($help_context = "Dimension Entry"), false, false, "", $js);

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

if(isset($_POST['selected_id']))
{
	$selected_id = $_POST['selected_id'];
}
else
	$selected_id = -1;
//---------------------------------------------------------------------------------------

if (isset($_GET['AddedID'])) 
{
	$id = $_GET['AddedID'];

	display_notification_centered(_("Payroll has been entered."));

	safe_exit();
}

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

if (isset($_GET['UpdatedID'])) 
{
	$id = $_GET['UpdatedID'];

	display_notification_centered(_("The dimension has been updated."));
	safe_exit();
}

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

function safe_exit()
{
	global $path_to_root;

	hyperlink_no_params("", _("Enter a &new Payroll"));
	echo "<br>";
	hyperlink_no_params($path_to_root . "/dimensions/inquiry/search_dimensions.php", _("&Select an existing Payroll"));

	display_footer_exit();
}

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

function can_process()
{
	global $selected_id, $Refs;

	if (payroll_name_exist($_POST['name'])) 
	{
		display_error( _("The payroll name already exists."));
		set_focus('name');
		return false;
	}
        
        if (!is_date($_POST['fromdate']))
	{
		display_error( _("The Start date entered is in an invalid format."));
		set_focus('fromdate');
		return false;
	}
        
        if (!is_date($_POST['todate']))
	{
		display_error( _("The End date entered is in an invalid format."));
		set_focus('todate');
		return false;
	}

	if (!is_date($_POST['date']))
	{
		display_error( _("The date entered is in an invalid format."));
		set_focus('date');
		return false;
	}

	if (empty($_POST['position']))
	{
		display_error( _("Please select Position."));
		set_focus('position');
		return false;
	}
        
        if (payroll_exist($_POST['month'],$_POST['fromdate'],$_POST['todate'],$_POST['position']))
	{
		display_error( _("Payroll for selected month, dates and position alresdy exists."));
		return false;
	}

	return true;
}

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

if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM'])) 
{
		
	if (can_process()) 
	{

		if ($selected_id == -1) 
		{
			$id = add_payroll($_POST['name'], $_POST['date'], $_POST['fromdate'], $_POST['todate'], $_POST['position'], $_POST['month']);
			meta_forward($_SERVER['PHP_SELF'], "AddedID=$id");
		} 
		else 
		{
                    update_payroll($selected_id, $_POST['name'], $_POST['date'], $_POST['fromdate'], $_POST['todate'], $_POST['position'], $_POST['month']);
			meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$selected_id");
		}
	}
}


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

start_form();

start_table(TABLESTYLE2);

if ($selected_id != -1)
{
	$myrow = get_payroll($selected_id);

	if (strlen($myrow[0]) == 0) 
	{
		display_error(_("The payroll sent is not valid."));
		display_footer_exit();
	}

	$_POST['name'] = $myrow["name"];
	$_POST['date'] = sql2date($myrow["date"]);
	$_POST['fromdate'] = sql2date($myrow["fromdate"]);
	$_POST['todate'] = sql2date($myrow["todate"]);
	$_POST['month'] = ($myrow["month"]);
	$_POST['position'] = ($myrow["position"]);	

	label_row(_("Name:"), $_POST['name']);
	hidden('selected_id', $selected_id);
} 
else 
{
	text_row_ex(_("Name") . ":", 'name', 50, 75);
}
month_list_row(_("Select Month"),"month", date('m'), _("Select Month"), FALSE);
date_row(_("Start Date") . ":", 'fromdate');

date_row(_("End Date") . ":", 'todate');

position_list_row("Select Position:", "position", null, _("Select Position"));

date_row(_("Payroll Date") . ":", 'date');

end_table(1);


if ($selected_id != -1) 
{
	echo "<br>";
	submit_center_first('UPDATE_ITEM', _("Update"), _('Save changes'), 'default');
}
else
{
	submit_center('ADD_ITEM', _("Add"), true, '', 'default');
}
end_form();

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

end_page();

