<?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");
include_once($path_to_root . "/payroll/includes/db/salary_commitments_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 ($_POST['branch_id'] == '')
	{
		$input_error = 1;
		display_error(_("Select a bank branch."));
		set_focus('branch_id');
              
	}
        if (strlen($_POST['account_no']) == 0)
	{
		$input_error = 1;
		display_error(_("Account No can not be empty."));
		set_focus('account_no');
              
	}
        if(!is_date($_POST['start_date']))
        {
                $input_error = 1;
                display_error( _('Start date is not a valid date'));
                set_focus('start_date');
        }
        if(!is_date($_POST['end_date']))
        {
                $input_error = 1;
                display_error( _('End date is not a valid date'));
                set_focus('end_date');
        }
        if (date1_greater_date2($_POST['start_date'], $_POST['end_date']))
	{
		$input_error = 1;
		display_error( _("End date can not be later than Start date"));
		set_focus('end_date');
	}

	if ($input_error != 1)
	{

                if ($selected_id != -1) 
		{
			//editing an existing price
			update_salary_commitments($selected_id, $_POST['bank_id'],
                                $_POST['branch_id'],$_POST['account_no'],$_POST['commitment'],
                                date2sql($_POST['start_date']),date2sql($_POST['end_date']));

			$msg = _("Salary Commitment has been updated.");
		}
		else
		{
                        add_salary_commitments($_POST['employee_code'], $_POST['bank_id'],
                                $_POST['branch_id'],$_POST['account_no'],$_POST['commitment'],
                                date2sql($_POST['start_date']), date2sql($_POST['end_date']));

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

}

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

if ($Mode == 'Delete')
{
	//the link to delete a selected record was clicked
	delete_salary_commitments($selected_id);
	display_notification(_("The selected family member 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');
}

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

$salary_commitment_list = get_salary_commitments($_POST['employee_code']);

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

$th = array(
    _("Bank Name"), _("Branch Name"), _("Account No"), _("Commitment"),  _("Start Date"), _("End date"), "", "");
table_header($th);
$k = 0; //row colour counter
$calculated = false;


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

	alt_table_row_color($k);

    	label_cell($myrow["bank_name"]);
        label_cell($myrow["bank_branch_name"]);
        label_cell($myrow["account_no"]);
        label_cell($myrow["commitment"]);
        label_cell(sql2date($myrow["start_date"]),"align=center");
        if($myrow["end_date"] == "3000-01-01"){
            label_cell("");
        }
        else{
            label_cell(sql2date($myrow["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')
{

        $salary_commitment = get_salary_commitment($selected_id);
        $salary_commitment_row  = db_fetch($salary_commitment);
//        var_dump($promotion_benefit_item_row);
//        echo $selected_id;
	$_POST['bank_id'] = $salary_commitment_row["bank_id"];
        $_POST['branch_id'] = $salary_commitment_row["branch_id"];
        $_POST['account_no'] = $salary_commitment_row["account_no"];
        $_POST['commitment'] = $salary_commitment_row["commitment"];
        $_POST['start_date'] = sql2date($salary_commitment_row["start_date"]);
        $_POST['end_date'] = sql2date($salary_commitment_row["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');

if(isset($_POST['bank_id']))
    value_list_row(_("Bank Name:"), 'bank_id', "BANKS", $_POST['bank_id'], false, true);
else {
    value_list_row(_("Bank Name:"), 'bank_id', "BANKS", null, false, true);
}

bank_branch_list_row(_("Bank Branch Name:"), 'branch_id', $_POST['bank_id']);

text_row(_("Account No:"), 'account_no', null, 30, 30);

text_row(_("Commitment:"), 'commitment', null, 30, 30);

date_row(_('Start Date:'), 'start_date');

if ($Mode != 'Edit') 
{  
    $_POST['end_date'] = sql2date(INFINITE_END_DATE);
} 
date_row(_('End Date:'), '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);
}	
?>
