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

$page_security = 'SA_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 . "/projects/includes/db/project_boq_item_groups_db.inc");
include_once($path_to_root . "/projects/includes/db/tasks_db.inc");

include_once($path_to_root . "/projects/includes/ui/project_boq_item_groups_ui.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 = "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 build_boq_group_array(&$boq_item_group, $selected_id)
{
        $boq_item_group['id'] = $selected_id;
        $boq_item_group['project_id'] = $_POST['project_id'];
        $boq_item_group['code'] = $_POST['code'];
        $boq_item_group['description'] = $_POST['description'];
        $boq_item_group['display_order'] = $_POST['display_order'];
        $boq_item_group['parent_id'] = $_POST['parent_id'];  
        
        $parent = get_project_boq_item_group_record($boq_item_group['parent_id']);
        if(isset($parent['depth']) && $parent['depth'] > 0){
            $boq_item_group['depth'] = $parent['depth'] + 1;
        }
        else{
            $boq_item_group['depth'] = 0;
        }
        
}


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


if ($Mode=='ADD_ITEM') 
{
    $boq_item_group = array();
    build_boq_group_array($boq_item_group, $selected_id);
    
    $cpboqg = can_process_project_boq_item_group($boq_item_group);

    if($cpboqg['success'] !== true){
        display_error($cpboqg['message']);
    }
    else{ 
        $parent_id = 0;
        if($boq_item_group['parent_id'] > 0){
            $parent_id = $boq_item_group['parent_id'];
        }
        add_project_boq_item_groups($boq_item_group['project_id'], $boq_item_group['code'],
                                        $boq_item_group['description'], $boq_item_group['display_order'], $parent_id,
                                        $boq_item_group['depth']); 
                                 
        recalculate_boq_item_group_positions($boq_item_group['project_id']);
        
        clear_task_data();
        $msg = _("A new project BOQ item group has been added.");            
        display_notification($msg);
        $Mode = 'RESET';
    }
    	
}

if ($Mode=='UPDATE_ITEM') 
{
    $boq_item_group = array();
    build_boq_group_array($boq_item_group, $selected_id);
    
    $cpboqg = can_process_project_boq_item_group($boq_item_group);

    if($cpboqg['success'] !== true){
        display_error($cpboqg['message']);
    }
    else{

        $parent_id = 0;
        if($boq_item_group['parent_id'] > 0){
            $parent_id = $boq_item_group['parent_id'];
        }
        
        update_project_boq_item_groups($boq_item_group['id'], $boq_item_group['code'],
                                       $boq_item_group['description'], $boq_item_group['display_order'], $parent_id,
                                        $boq_item_group['depth']);                                    
                                     
        recalculate_boq_item_group_positions($boq_item_group['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_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();
project_list_cells(_("Project:"), 'project_id', null,
  _('New project'), true, check_value('show_inactive'));


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

br();

display_boq_item_groups(get_post('project_id'), 1);

br();

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

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

echo "<br>";

if ($Mode == 'Edit')
{
	$myrow = get_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);
        project_boq_item_groups_list_row("Parent:", 'parent_id', $_POST['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);
}


?>
