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

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

if (!@$_GET['popup'])
        page(_($help_context = "Inventory Item Sales prices"),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['est_project_id']))
{
	$_POST['est_project_id'] = $_GET['est_project_id'];
}


//---------------------------------------------------------------------------------------------------
if (!@$_GET['popup'])
	start_form();

	br(2);
//set_global_stock_item($_POST['est_project_id']);
function clear_task_data()
{
	unset($_POST['long_description']);
	unset($_POST['description']);
	unset($_POST['start_date']);
	unset($_POST['end_date']);
	unset($_POST['budget']);
	
}
//----------------------------------------------------------------------------------------------------
function can_process()
{
   if (is_date($_POST['date']) == FALSE)
    {  
        $input_error = 1;
        display_error( _("The entered date is invalid."));
        set_focus('date');
        return false;
    }
    if (check_num('percentage',0) == FALSE){

        $input_error = 1;
        display_error(_("Percentage must be numeric and positive."));
        set_focus('percentage');
        return false;
    } 
    if (check_num('amount',0) == FALSE)
    {
        $input_error = 1;
        display_error(_("Amount must be numeric and positive."));
        set_focus('amount');
        return false;
    } 
//    $inv = $_POST['invoice_no'];
//    if (is_numeric($inv) == FALSE )
//    {
//        $input_error = 1;
//        display_error(_("Invoice number must be numeric."));
//        set_focus('invoice_no');
//        return false;
//    } 
//    if ($_POST['amount']<0)
//    {
//        $input_error = 1;
//        display_error(_("Amount can not be negative."));
//        set_focus('amount');
//        return false;
//    }
    return true;
}

if ($Mode=='ADD_ITEM' && can_process()) 
{
    add_est_projects_billing_schedule($est_project_id, $_POST['date'],
            input_num('amount'), input_num('percentage'),
            $_POST['comment']);
    clear_task_data();
    $msg = _("The new billing schedule has been added.");
            
    display_notification($msg);
    $Mode = 'RESET';
	
}

if ($Mode=='UPDATE_ITEM' && can_process()) 
{

        update_est_projects_billing_schedule($selected_id, $_POST['date'],
                            input_num('amount'), input_num('percentage'),
                            $_POST['comment']);
        clear_task_data();
        $msg = _("This billing schedule has been updated.");
        
        display_notification($msg);
        $Mode = 'RESET';
}

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

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

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

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

$tasks_list = get_est_schedules($_POST['est_project_id']);

div_start('price_table');

start_table(TABLESTYLE, "width=40%");

$th = array(_("Date"), _("Percentage %"), _("Amount"), _("Comment"), _("Invoice No"), "", "");

table_header($th);

$k = 0; //row colour counter

$calculated = false;

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

    label_cell(sql2date($myrow["date"]), 'align=center');
    amount_cell($myrow["percentage"]);
    amount_cell($myrow["amount"]); 
    label_cell($myrow["comment"]);
//    $invoice_no = '';
//    if ($myrow['invoice_no'] > 0) 
//    {
//        $invoice_no = $myrow['invoice_no'];
//        
//    }
//    label_cell(get_trans_view_str(ST_SALESINVOICE, $invoice_no), 'align=center');
    
    
    edit_button_cell("Edit".$myrow['schedule_id'], _("Edit"));
    delete_button_cell("Delete".$myrow['schedule_id'], _("Delete"));
    end_row();
}

end_table();

div_end();
//------------------------------------------------------------------------------------------------

echo "<br>";

if ($Mode == 'Edit')
{
	$myrow = get_est_schedule_details($selected_id);
        
        $_POST['date'] = sql2date($myrow["date"]);
        $_POST['amount'] = $myrow["amount"];
        $_POST['percentage'] = $myrow["percentage"];
        $_POST['comment'] = $myrow["comment"];
        //$_POST['invoice_no'] = $myrow["invoice_no"];
}
else{
        $_POST['amount'] = 0.00;
        $_POST['percentage'] = 0.00;
        $_POST['comment'] = '';
}

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);

    date_row(_("Date:"), 'date',null,null,30);
    //date_row(_("End Date:"), 'end_date',null,null,30);
    amount_row(_("Percentage:"), 'percentage');
    amount_row(_("Amount:"), 'amount');
    textarea_row(_("Comment:"), 'comment', null, 31, 3);
    //ref_row(_("Invoice No:"), 'invoice_no', null, null, null, 0);
    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);
}	
?>




























