<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_TASK_BUDGETS';	
$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 . "/inventory/includes/inventory_db.inc");
include_once($path_to_root . "/projects/includes/db/tasks_db.inc");
include_once($path_to_root . "/projects/includes/db/task_budgets_db.inc");
$js = '';

if (user_use_date_picker()) {
        $js .= get_js_date_picker();
}

if (!@$_GET['popup'])
        page(_($help_context = "Task Budgets"),false,false,"",$js);

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

check_db_has_projects(_("There are no projects defined in the system."));

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

//if (isset($_GET['stock_id']))
//{
//	$_POST['stock_id'] = $_GET['stock_id'];
//}
//if (isset($_GET['Item']))
//{
//	$_POST['stock_id'] = $_GET['Item'];
//}
//
//if (!isset($_POST['curr_abrev']))
//{
//	$_POST['curr_abrev'] = get_company_currency();
//}

//---------------------------------------------------------------------------------------------------
if (!@$_GET['popup'])
	start_form();
//
//if (!isset($_POST['stock_id']))
//	$_POST['stock_id'] = get_global_stock_item();


project_list_row(_("Project:"), 'project_id', null, _('Select a project'), true);
br(2);
$project_id = $_POST['project_id'];
tasks_list_row(_("Task:"), 'task_id', $project_id, null, _('Select a task'), true);
br(2);

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

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

    $input_error = 0;
    $result = get_task_details($_POST['task_id']);
    $task_start_date = sql2date($result['start_date']);
    $task_end_date = sql2date($result['end_date']);
    
    $task_budget = $result['budget'];
    $task_budgets = get_task_budget($_POST['task_id']);
    $budget = 0;
//    while ($myrow = db_fetch($task_budgets))
//    {  
//        if(date1_greater_date2(sql2date($myrow['end_date']), $_POST['start_date'])){
//            
//            display_notification("There is a budget record overlaping with this record");
//            set_focus(start_date);
//            return;
//        }
//        $budget = $budget + $myrow['budget'];
//    } 
    $overlap = get_overlap_task_budgets($_POST['task_id'], $_POST['start_date'], $_POST['end_date'], $selected_id);
    
    if($overlap != null){
        display_notification("There is a budget record overlaping with this record");
        set_focus('start_date');
        return;
    }
    
    if ($_POST['project_id'] == -1)
    {
        $input_error = 1;
        display_error( _("Select a project."));
        set_focus('project_id');
          
    }
    if ($_POST['task_id'] == -1)
    {
        $input_error = 1;
        display_error( _("Select a task."));
        set_focus('task_id');
          
    }
    
    if (!is_date($_POST['start_date']))
    {
        $input_error = 1;
        display_error( _("The start date entered is invalid."));
        set_focus('start_date');
          
    }
    if (date1_greater_date2($task_start_date, $_POST['start_date']))
    {
        $input_error = 1;
        display_error( _("Task budget start date can not be ealier than task start date"));
        set_focus('start_date');

    }
    if (!is_date($_POST['end_date']))
    {
        $input_error = 1;
        display_error( _("The end date entered is invalid."));
        set_focus('end_date');

    }
    if (date1_greater_date2($_POST['end_date'], $task_end_date))
    {
        $input_error = 1;
        display_error( _("Task budget end date can not be later than task end date"));
        set_focus('end_date');
            
    }
    if (date1_greater_date2($_POST['start_date'], $_POST['end_date']))
    {
        $input_error = 1;
        display_error( _("Budget task end date can not be greater than budget task start date"));
        set_focus('end_date');
            
    }
    if (!check_num('budget', 0))
    {
        $input_error = 1;
        display_error( _("The budget entered must be numeric and can not be negative."));
        set_focus('budget');
            
    }
    if(($_POST['budget']+$budget)> $task_budget)
    {
        $input_error = 1;
        display_error( _("With the budget entered, the total budget of this task budgets will exceed the task budget."));
        set_focus('budget');
    }

	if ($input_error != 1)
	{

            if ($selected_id != -1) 
            {
                //editing an existing price
                update_budget_task($selected_id, $_POST['start_date'],
                        $_POST['end_date'], input_num('budget'));

                $msg = _("This task budget has been updated.");
            }
            else
            {

                add_budget_task($_POST['task_id'], $_POST['start_date'],
                        $_POST['end_date'], input_num('budget'));

                $msg = _("The new task budget has been added.");
            }
            
            display_notification($msg);
            $Mode = 'RESET';
	}

}

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

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

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

//if (list_updated('stock_id')) {
//	$Ajax->activate('pos_price_table');
//	$Ajax->activate('pos_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['pos_price']);
//	$Ajax->activate('pos_price_details');
//}

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

$task_budget_list = get_task_budget($_POST['task_id']);

div_start('pos_price_table');
start_table(TABLESTYLE, "width=40%");

$th = array(_("Start date"),_("End date"),_("Budget"), _("Budget Used"),_("Budget Allocated"), "", "");
table_header($th);
$k = 0; //row colour counter
$calculated = false;

while ($myrow = db_fetch($task_budget_list))
{
    alt_table_row_color($k);

    label_cell(sql2date($myrow["start_date"]));
    label_cell(sql2date($myrow["end_date"]));
    amount_cell($myrow["budget"]);
    amount_cell($myrow["budget_used"]);
    amount_cell($myrow["budget_allocated"]);
    edit_button_cell("Edit".$myrow['id'], _("Edit"));
    delete_button_cell("Delete".$myrow['id'], _("Delete"));
    end_row();
}

end_table();
br();
if (db_num_rows($task_budget_list) == 0)
{
	if (get_company_pref('add_pct') != -1)
		$calculated = true;
	display_note(_("There are no task budget set up for this part."), 1);
        br(1);
}
div_end();
//------------------------------------------------------------------------------------------------
   
        
if ($Mode == 'Edit')
{
	$myrow = get_task_budget_details($selected_id);
	$_POST['task_id'] = $myrow["task_id"];
	$_POST['budget'] = price_format($myrow["budget"]);
        $_POST['start_date'] = sql2date($myrow["start_date"]);
	$_POST['end_date'] = sql2date($myrow["end_date"]);
}

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

date_row(_('Start date:'), 'start_date');
date_row(_('End date:'), 'end_date');
amount_row(_('Budget:'), 'budget');



end_table(1);
//if ($calculated)
//	display_note(_("The price is calculated."), 0, 1);

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

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