<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PROJECT_BOQ_ITEMS';
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_items_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_costs_db.inc.");
include_once($path_to_root . "/projects/includes/db/project_boq_item_material_db.inc.");
include_once($path_to_root . "/projects/includes/db/project_boq_item_labour_db.inc.");
include_once($path_to_root . "/projects/includes/db/project_boq_item_assets_db.inc.");
include_once($path_to_root . "/projects/includes/db/project_boq_item_transport_db.inc.");

include_once($path_to_root . "/projects/includes/db/project_boq_item_detail_edit_db.inc");
include_once($path_to_root . "/projects/includes/ui/project_boq_items_ui.inc");

include_once($path_to_root . "/admin/db/list_values_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 = "BOQ Items"), 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']);




if ($Mode=='ADD_ITEM') 
{
    $boq_item = array();
    build_boq_item_array($boq_item, $selected_id);
    $cpboqi_msg = can_process_boq_item($boq_item);
    if($cpboqi_msg["success"] !== true){
        display_error($cpboqi_msg['message']);
    }
    else{        
        if($boq_item['group_id'] < 0){
            $group_id = 0;
        }
        else{
            $group_id = $boq_item['group_id'];
        }

        $new_boq_iem_id = add_project_boq_item($boq_item['project_id'], $boq_item['type_id'], $boq_item['display_order'], $boq_item['code'],
                                $boq_item['description'], $boq_item['billing_type'], $boq_item['unit'], $boq_item['rate'], 
                                $boq_item['max_quantity'], $boq_item['long_description'], $boq_item['print_long_description'], 
                                $boq_item['tax_group_id'], $group_id);

        hidden('selected_id', $new_boq_iem_id);
        $selected_id = $new_boq_iem_id;
        $Mode = "Edit";

        clear_task_data();
        $msg = _("The new project BOQ item has been added.");            
        display_notification($msg);
    //        $Mode = 'RESET';
        
    }
	
}
if ($Mode=='UPDATE_ITEM') 
{
    $boq_item = array();
    build_boq_item_array($boq_item, $selected_id);
    $cpboqi_msg = can_process_boq_item($boq_item);
    if($cpboqi_msg["success"] !== true){
        display_error($cpboqi_msg['message']);
    }
    else{        
        if($boq_item['group_id'] < 0){
            $group_id = 0;
        }
        else{
            $group_id = $boq_item['group_id'];
        }
        
        update_project_boq_item($selected_id, $boq_item['type_id'], $boq_item['display_order'], $boq_item['code'],
                                    $boq_item['description'], $boq_item['billing_type'], $boq_item['unit'], $boq_item['rate'], 
                                    $boq_item['max_quantity'], $boq_item['long_description'], $boq_item['print_long_description'], 
                                    $boq_item['tax_group_id'], $group_id);
        clear_task_data();
        $msg = _("This project BOQ item has been updated.");
        
        display_notification($msg);
        $_post['boq_item_id'] = -1;
        $selected_id = -1;
        $Mode = 'RESET';
    }
}
//------------------------------------------------------------------------------------------------------

$delete_boq_item_component_id  = find_submit("Delete_BOQ_Item_Component_Costs");
if ($delete_boq_item_component_id > 0)
{
    
    
    include_once($path_to_root . "/projects/includes/db/project_boq_item_costs_db.inc");
    include_once($path_to_root . "/projects/includes/db/project_boq_item_material_db.inc");
    include_once($path_to_root . "/projects/includes/db/project_boq_item_labour_db.inc");
    include_once($path_to_root . "/projects/includes/db/project_boq_item_assets_db.inc");
    include_once($path_to_root . "/projects/includes/db/project_boq_item_transport_db.inc");
    
    begin_transaction();
    
    $sbp_msg = delete_boq_item_component(get_post('boq_item_id'), $delete_boq_item_component_id);
    if($sbp_msg['success'] === true){
        commit_transaction();
        $Mode = 'RESET';
        $selected_id = -1;

        display_notification(_('Selected BOQ Item Labour detail has been deleted'));
    }
    else{
        cancel_transaction();
    }
	
	
}

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

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

if ($Mode == 'RESET')
{
    unset($_POST['boq_item_id']);
	$selected_id = -1;
}

if(isset($_POST['Cancel']))
{
    $Mode = 'RESET';
	$selected_id = -1;
}


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

global  $Ajax;

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

	br();
$Ajax->activate("filtering_group_id");
$Ajax->activate("boq_item_id");

start_table(TABLESTYLE_NOBORDER);
start_row();
project_list_cells(_("Project:"), 'project_id', null,
  _('Select a project'), true, check_value('show_inactive'));

$project_id = get_post('project_id');
//	check_cells(_("Show inactive:"), 'show_inactive', null, true);
end_row();
start_row();
project_boq_item_groups_list_cells("BOQ Item Group:", 'filtering_group_id', $project_id, null, 'No BOQ Item Group', true);
end_row();

if(get_post('filtering_group_id') > 0){
    $filetering_group_id = get_post('filtering_group_id');
}
else{
    $filetering_group_id = 0;
}

start_row();
group_boq_items_list_cells("BOQ Item:", 'boq_item_id', $project_id, $filetering_group_id, null, 'New BOQ Item', true);

end_row();

end_table();




	
br(2);

//display_boq_items($_POST['project_id'], $_POST['filtering_group_id']);
//------------------------------------------------------------------------------------------------

if(get_post('boq_item_id') > 0){
    $selected_id = get_post('boq_item_id');
    $Mode = 'Edit';
}
else{
    $selected_id = -1;
}

$Ajax->Activate('boq_item_edit_controls');

div_start('boq_item_edit_controls');
display_boq_item_edit_controls($Mode, $selected_id);
div_end();

$Ajax->Activate('boq_item_components');

div_start('boq_item_components');

if($selected_id > 0){
    display_boq_item_components($selected_id);
}

div_end();

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


?>
