<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_EST_PROJECT_BOQ_ITEM_GROUPS';
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_boq_item_groups_db.inc");
include_once($path_to_root . "/estimation/includes/db/est_tasks_db.inc");

include_once($path_to_root . "/estimation/includes/ui/est_project_boq_item_groups_ui.inc");
include_once($path_to_root . "/includes/ui/ui_lists_estimation.inc");

include_once($path_to_root . "/estimation/includes/db/est_project_boq_items_db.inc");
include_once($path_to_root . "/estimation/includes/db/est_projects_db.inc");

$js = '';

if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

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

page(_($help_context = "Estimation BOQ Item Groups"), false, false, "", $js);


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

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

simple_page_mode(true);
//---------------------------------------------------------------------------------------------------

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

//set_global_stock_item($_POST['project_id']);
function clear_task_data()
{
	unset($_POST['code']);
	unset($_POST['description']);
	unset($_POST['display_order']);		
}
//----------------------------------------------------------------------------------------------------

function can_process($selected_id = -1)
{    
    if(!(get_post('est_project_id') > 0 ))
    {
        display_error( _("Please Select an Estimation Project."));
        set_focus('code');
        return false;  
    }
        

    $project = get_est_project_record(get_post('est_project_id'));
    if(!($project['approval_status'] == AS_NEW || $project['approval_status'] == AS_WITH_THE_APPROVER || 
            $project['approval_status'] == AS_AWAITING_APPROVAL || $project['approval_status'] == AS_BACK_TO_ORIGINATOR || 
            $project['approval_status'] == AS_REOPENED)){
        display_error("This project status does not allow further updating.");
        return false;
    }
    
        
    if(get_post('code') == '')
    {
        display_error( _("Enter a code."));
        set_focus('code');
        return false;  
    }     
    
    if (get_post('description') == '')
    {
        display_error( _("Enter description."));
        set_focus('description');
        return false;  
    }     
    
    $count = get_existing_code_from_est_project_boq_item_groups(get_post('est_project_id'), get_post('code'), $selected_id);
    if($count > 0)
    {
       display_error("Entered code already exists.");
       set_focus('code');
       return;        
    }
    
    if($selected_id > 0 && $_POST['parent_id'] ==  $selected_id)
    {
       display_error("Can't use the same group as its parent.");
       set_focus('parent_id');
       return;        
    }    
    return true; 
}



if(isset($_POST['Recalculate_BOQ_Item_Groups'])){
    
    recalculate_est_boq_item_groups(get_post('est_project_id'));
    display_note("Estimation BOQ Item Groups have been recalculated.");    
}


if ($Mode=='ADD_ITEM' && can_process($selected_id)) 
{

        if(get_post('parent_id') < 0){
            $parent_id = 0;
        }
        else{
            $parent_id = get_post('parent_id');
        }        
       
        $parent_id = 0;
        if(get_post('parent_id') > 0){
            $parent_id = get_post('parent_id');
        }
        add_est_project_boq_item_groups(get_post('est_project_id'), get_post('code'),
                                        get_post('description'), get_post('display_order'), $parent_id); 
                                 
        recalculate_est_boq_item_group_positions(get_post('est_project_id'));
        
        clear_task_data();
        $msg = _("A new Estimation project BOQ item group has been added.");            
        display_notification($msg);
        $Mode = 'RESET';
    	
}

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

        $parent_id = 0;
        if(get_post('parent_id') > 0){
            $parent_id = get_post('parent_id');
        }
        
        $code  = get_post('code');
        update_est_project_boq_item_groups($selected_id, $code,
                                       get_post('description'), get_post('display_order'), $parent_id);                                     
                                     
        recalculate_est_boq_item_group_positions(get_post('est_project_id'));
        
        clear_task_data();
        $msg = _("Selected project BOQ item group has been updated.");        
        display_notification($msg);
        $Mode = 'RESET';
    
}
//------------------------------------------------------------------------------------------------------

if ($Mode == 'Delete'){
	//the link to delete a selected record was clicked
	delete_est_project_boq_item_groups($selected_id);
	display_notification(_("The selected project BOQ item group has been deleted."));
	$Mode = 'RESET';
}

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

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

	br();
    
start_table(TABLESTYLE_NOBORDER);
start_row();

est_project_list_cells(_("Select an Estimation:"), 'est_project_id', null, _('New Estimation project'), true, check_value('show_inactive'));

//	check_cells(_("Show inactive:"), 'show_inactive', null, true);
end_row();
end_table();

br();

$est_project_id = get_post('est_project_id');
//build_est_boq_groups_summary_array(1,$est_project_id);

display_est_boq_item_groups($est_project_id, 1);

br();

submit_center_first('Recalculate_BOQ_Item_Groups', _("Recalculate Estimation BOQ Item Group Summaries"), '', 'default');

br();
//------------------------------------------------------------------------------------------------

echo "<br>";

if ($Mode == 'Edit')
{
	$myrow = get_est_project_boq_item_group_details($selected_id);
        
        $_POST['id'] = $myrow["id"];
        $_POST['code'] = $myrow["code"];
        $_POST['description'] = $myrow["item_description"];       
        $_POST['display_order'] = $myrow["display_order"];  
        $_POST['parent_id'] = $myrow["parent_id"];  
}

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);  
      
        text_row(_("Code:"), 'code', null, 50, 60);
        textarea_row(_("Description:"), 'description', null, 35, 3);
        text_row(_("Display Order:"), 'display_order', null, 20, 20);
        est_project_boq_item_groups_list_row("Parent:", 'parent_id', $_POST['est_project_id'], null, 'Select a Parent');    
   
    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);
}


?>

