<?php
$page_security = 'SA_PROJECT_BOQ_ITEMS';
$path_to_root = "../..";
include_once($path_to_root . "/includes/db_pager.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui/ui_input.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_detail_edit_db.inc");


include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.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 . "/inventory/includes/db/material_groups_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_labour_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_transport_db.inc");

include_once($path_to_root . "/projects/includes/db/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();

simple_page_mode(true);

include_once($path_to_root . "/projects/includes/project_boq_item_costs.inc");
include_once($path_to_root . "/projects/includes/project_boq_item_material.inc");
include_once($path_to_root . "/projects/includes/project_boq_item_assets.inc");
include_once($path_to_root . "/projects/includes/project_boq_item_labour.inc");
include_once($path_to_root . "/projects/includes/project_boq_item_transport.inc");


page(_($help_context = "Project BOQ Item Components"), true, false, false, $js);



global $selected_id, $Ajax;

$boq_item_id = 0;
$detail_type_code = '';
$primary_type = 0;

if (isset($_GET["trans_no"]))
{
	$_POST["trans_no"] = $_GET["trans_no"];
}
if (isset($_POST["trans_no"]))
{
	$boq_item_id = $_POST["trans_no"];
}


if (isset($_GET["dtype"]))
{
	$_POST["dtype"] = $_GET["dtype"];
}
if (isset($_POST["dtype"]))
{
	$detail_type_code = $_POST["dtype"];
}

if (isset($_GET["ptype"]))
{
	$_POST["primary_type"] = $_GET["ptype"];
}
if (isset($_POST["primary_type"]))
{
	$primary_type = $_POST["primary_type"];
}


//$sep_pos = strpos($trans_no, "|");
//$boq_item_id = substr($trans_no, 0, $sep_pos);
//$detail_type_code = substr($trans_no, $sep_pos+1);
//
//if(!$detail_type_code){
//    $detail_type_code = '';
//}


//if(isset($_POST["Submit"])){
//    //Process button is clicked, do validation
//    $val1 = true;
//    //validate to inputted qty in parent window
//    $sum = 0;
//    $lot_qty_array = array();
//    foreach($_POST["lot_ids"] as $s){
//        $name = implode("#",array($_POST['line_ref'],$_POST['stock_id'],"lot",$s,"tran_qty"));
//        
//        $tran_qty= 0;
//        if($_POST[$name]  > 0){
//            $tran_qty = $_POST[$name];
//        }
//        $info = array("line_ref" => $_POST['line_ref'], 
//                        "stock_id" => $_POST['stock_id'], 
//                        "lot_id" => $s, 
//                        "lot_reference" => $_POST["lot_references"][$s], 
//                        "tran_qty" => $tran_qty, 
//                        "row_state" => $_POST["row_states"][$s]);
//        
//        $lot_qty_array[$name] = $info;
//        $sum += $_POST[$name];
//    }
//    if($sum != $_POST["parent_item_qty"]){
//        display_error("Tthe total number of item defined in lot is different than in the main form.");
//        $val1 = false;
//    }
//    
//    //validate to database
////    $val2 = qty_lot_validation($_POST['lot_id'],$lot_qty_array);
////    if(!$val2){
////        display_error("The lot data inputtedd are different from the database");
////    }
//    
//    //validation success
//    if($val1){
//        $stock_item_line_ref = $_POST['line_ref']."_".$_POST['stock_id'];
//        $lot_info_name = "lot_info#".$_POST['parent_guid'];
//        $_SESSION[$lot_info_name][$stock_item_line_ref]=$lot_qty_array;
//        process_lot_to_parent(null,$_POST['parent']);
//    }
//    
//}

//if (isset($_GET['stock_id'])){
//    $stock_id = $_GET['stock_id'];
//}else{
//    $stock_id = $_POST['stock_id'];
//}
//if (isset($_GET['parent'])){
//    $parent = $_GET['parent'];
//}else{
//    $parent = $_POST['parent'];
//}
//
//$parent_unit = "";
//if(isset($_GET['unit_of_parent'])){
//    $parent_unit = $_GET['unit_of_parent'];
//}else{
//    $parent_unit = $_POST['unit_of_parent'];
//}
//
//$guid_ref = "";
//if(isset($_GET['value_ref'])){
//    $guid_ref = $_GET['value_ref'];
//}else{
//    $guid_ref = $_POST['value_ref'];
//}
//
//$line_ref = "";
//if(isset($_GET['line_ref'])){
//    $line_ref = $_GET['line_ref'];
//}else{
//    $line_ref = $_POST['line_ref'];
//}

$Ajax->Activate('detail_type_code'); 
$Ajax->Activate('primary_type');
//    $Ajax->Activate('boq_material_details');
//    $Ajax->Activate('boq_material_edit_controls');
//    $Ajax->Activate('page_body'); 


start_form();

//hidden("parent",$parent);
//hidden("parent_guid");
//hidden("stock_id",$stock_id);
//hidden("parent_item_qty");
//hidden("parent_unit",$parent_unit);
//hidden("unit_of_parent",$parent_unit);
//hidden("value_ref",$guid_ref);
//hidden("line_ref",$line_ref);

hidden('trans_no', $boq_item_id);
hidden('dtype', $detail_type_code);
hidden('ptype', $primary_type);

$boq_item = get_project_boq_item_record($boq_item_id);
$project = get_project_record($boq_item['project_id']);
hidden('boq_item_id',$boq_item_id);

if(strlen($detail_type_code) > 0){
    $component = get_boq_item_component_record($boq_item_id, $detail_type_code);
    if(!(isset($_POST['detail_type_code']) && strlen($_POST['detail_type_code']) > 0)){
        if(!(isset($component['detail_type_code']) && strlen($component['detail_type_code']) > 0)){
            $_POST['detail_type_code'] = $detail_type_code;
        }
        else{
            $_POST['detail_type_code'] = $component['detail_type_code'] ;
        }
    }
//    if(!(isset($_POST['primary_type']) && strlen($_POST['detail_type_code']) > 0)){
//        $_POST['primary_type'] = $component['primary_type'] ;
//    }
}
else{
//    $_POST['detail_type_code'] = '';
//    
//    if(!(isset($_POST['primary_type']) && strlen($_POST['detail_type_code']) > 0)){
//    $_POST['primary_type'] = '';
//    }
}

start_table(TABLESTYLE, "width=40%");
//$th = array(_("Lot Reference"), _("Available Qty"),_("Qty"));
//table_header($th);

label_row("Project", $project['description']);
label_row("BOQ Item", $boq_item['description']);

value_list_code_row("Detail Type", "detail_type_code", 'PROJECT_TASK_DETAIL_TYPES', null, false, true);
project_boq_item_primary_types_list_row("Primary Type", "primary_type", null, '', true);
        
end_table(1);

br();
        
$Ajax->Activate('test'); 
div_start('test');
switch ($primary_type) {
    case BOQPT_COST:
//        display_note('Costs for '.get_post('detail_type_code'));
        display_project_boq_costs_page($selected_id, $Mode, $boq_item_id, get_post('detail_type_code'));
        break;
    
    case BOQPT_MATERIAL:
//        display_note('Materials for '.get_post('detail_type_code'));
        display_project_boq_material_page($selected_id, $Mode, $boq_item_id, get_post('detail_type_code'));
        break;
    
    case BOQPT_LABOUR:
//        display_note('Labour for '.get_post('detail_type_code'));
        display_project_boq_labour_page($selected_id, $Mode, $boq_item_id, get_post('detail_type_code'));
        break;
    
    case BOQPT_ASSET:
//        display_note('Assets for '.get_post('detail_type_code'));
        display_project_boq_assets_page($selected_id, $Mode, $boq_item_id, get_post('detail_type_code'));
        break;
    
    case BOQPT_TRANSPORT:
//        display_note('Transport for '.get_post('detail_type_code'));
        display_project_boq_transport_page($selected_id, $Mode, $boq_item_id, get_post('detail_type_code'));
        break;

    default:
        break;
}
div_end();
//inactive_control_row($th);

br();

echo "<center>";
//submit("Submit", "submit");
echo "<input type='button' name='cancel' class='inputsubmit' onclick='goBack()' value='Close'/>";
echo "</center>";
end_form();

//js_get_data_in_parent("guid","parent_guid");
//js_get_data_in_parent($parent,"parent_item_qty");
//js_get_data_in_parent($parent_unit,"parent_unit");
//show_quantity("parent_item_qty");
end_page();
