<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_EST_COMPETITORS';
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_project_competitors_db.inc");
include_once($path_to_root . "/includes/ui/ui_lists_estimation.inc");
include_once($path_to_root . "/admin/db/list_values_db.inc");
include_once($path_to_root . "/includes/ui/ui_lists_group_companies.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");




$js = '';

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

if (!@$_GET['popup'])
        page(_($help_context = "Project Competitors"),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['project_id']))
{
	$_POST['project_id'] = $_GET['project_id'];
}

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

//set_global_stock_item($_POST['project_id']);
function clear_competitor_data()
{
    unset($_POST['competitor']);
    unset($_POST['competitor_type']);
    unset($_POST['value']);
    unset($_POST['bid_winner']);
    unset($_POST['comment']);	
}

function can_process($Mode)
{
    if($_POST['competitor_type'] == true){
            
    }
    else{
        if(empty($_POST['competitor'])){
            display_error("Please enter a competitor.");
            set_focus('competitor');
            return false;
        }
    }
    
    if(!(isset($_POST['est_project_id']) && $_POST['est_project_id'] > 0 )){
        display_error("Please select a project.");
        set_focus('est_project_id');
        return false;
    }
    

    if (!check_num('value',0))
    {
        display_error(_("Value should be zero or positive."));
        set_focus('value');
        return false;
    } 

    if($_POST['bid_winner'] == 1){        
        if(!isset($_POST['id'])){
            $_POST['id'] = 0;
        }
        $existing_bid_winner = get_existing_bid_winner($_POST['est_project_id'],$_POST['id']);
        if ($existing_bid_winner > 0 )
        {
            display_error(_("There is a Bid Winner for this project already."));
            set_focus('bid_winner');
            return false;
        } 
    }
    
    return true;
    
}

function display_competitors($est_project_id)
{
    div_start('price_table');

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

    $th = array(_("ID"), "Type", "Competitor / Group Company", "Value", "Bid Winner", "Comments", " ");

    table_header($th);

    $k = 0; //row colour counter

    $site_list = get_competitors($est_project_id);
    
    while ($myrow = db_fetch($site_list))
    {
        alt_table_row_color($k);

        label_cell($myrow["id"],'align=center');
        
        if($myrow["competitor_type"] == true){
            $competitor_type_name = "Group Company";            
        }
        else{
            $competitor_type_name = "Competitor";
        }
        label_cell($competitor_type_name);
        
        if($myrow["competitor_type"] == true){
            label_cell(get_group_company_name($myrow["company_code"]));            
        }
        else{
            label_cell($myrow["competitor"]);
        }
        
        
        
        amount_decimal_cell($myrow["value"]);
        label_cell(get_yes_no_name($myrow["bid_winner"]),'align=center');
        label_cell($myrow["comment"]);

        edit_button_cell("Edit".$myrow['id'], _("Edit"));
        end_row();
    }

    end_table();

    div_end();

}

function display_competitor_edit_controls($Mode, $selected_id)
{
    if ($Mode == 'Edit')
    {
            $myrow = get_competitor_details($selected_id);

            $_POST['competitor_type'] = $myrow["competitor_type"];
            $_POST['company_code'] = $myrow["company_code"];
            $_POST['competitor'] = $myrow["competitor"];
            $_POST['competitor_type'] = $myrow["competitor_type"];
            $_POST['value'] = $myrow["value"];
            $_POST['bid_winner'] = $myrow["bid_winner"];
            $_POST['comment'] = $myrow["comment"];
    }

    hidden('selected_id', $selected_id);

    div_start('price_details');

    start_table(TABLESTYLE2);
    yesno_list_row("Type", 'competitor_type', null, "Group Company", 'Competitor', true);
    if($_POST['competitor_type'] == true){
        group_companies_list_row("Group Company", 'company_code');
        hidden('competitor', "");
    }
    else{
        text_row(_("Competitor:"), 'competitor', null, 50, 50);
        hidden('company_code', "");
    }
    
    amount_row(_("Value:"), 'value');
    yesno_list_row("Bid Winner", 'bid_winner');
    textarea_row(_("Comment:"), 'comment', null, 42, 3);
    
    end_table(1);

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

    div_end();

}

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

//$est_project_id = get_post('est_project_id');

if ($Mode=='ADD_ITEM') 
{             
    if(can_process($Mode)){
        add_competitor($_POST['est_project_id'], $_POST['competitor'], $_POST['competitor_type'], 
                        $_POST["company_code"], input_num('value'),$_POST['bid_winner'],$_POST['comment']);       
             
        clear_competitor_data();
        $msg = _("The new competitor has been added.");

        display_notification($msg);
        $Mode = 'RESET';  
    }
}

if ($Mode=='UPDATE_ITEM') 
{
    if(can_process($Mode)){
        update_competitor($selected_id, $_POST['competitor'], $_POST['competitor_type'], 
                            $_POST["company_code"], input_num('value'),$_POST['bid_winner'], $_POST['comment']); 
        
        clear_competitor_data();
        $msg = _("Competitor details has been updated.");
        
        display_notification($msg);
        $Mode = 'RESET';
    }
}

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

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

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

//--------------------------------------------------------------------------------------------------------
//$tasks = get_task($_POST['project_id']);

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

	br(2);
    

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

echo "<br>";

start_table(TABLESTYLE_NOBORDER);
start_row();
est_project_list_cells(_("Estimated Project:"), 'est_project_id', null, _('Select an Estimated Project'), true);

$est_project_id = $_POST['est_project_id'];
end_row();
end_table();

br();

if(list_updated('est_project_id')){
    $selected_id = -1;
}

display_competitors($est_project_id);

br(1);

display_competitor_edit_controls($Mode, $selected_id);



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

