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




function display_project_boq_item_details_view($project_id){

    global $Ajax;
    
    $Ajax->activate('price_table');
    
    $boq_groups = array();
//    build_boq_groups_summary_array($boq_groups, $project_id);
    build_boq_groups_summary_array_new($boq_groups, $project_id);
    
    div_start('price_table');

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


    $th = array(_("BOQ Item"), _("Detail Type"), _("Primary Type"), 
                _("Id"), "Item Code", _("Item Description"), _("Unit"),_("Qty"),_("Rate"), _("Amount"));   
    

    table_header($th);

    $cost_total = 0;
    $k = 0; //row colour counter

//    while ($myrow = db_fetch($result))
//    foreach ($boq_groups as $myrow) {
//        $depth = spaces(($myrow["depth"]) * 10);
//    
//        $name = "[".$myrow["code"]."] ".$myrow["description"];
//        $s = get_project_trans_view_str(ST_PROJECT_BOQ_ITEM_GROUP, $myrow['project_id']."|".$myrow["id"], $name);
//
////        label_cell($depth.text_bold($name), 'colspan=5');
//        label_cell($depth.text_bold($name), 'colspan=2');
//            
//    }
        
    display_boq_item_component_details($project_id, $cost_total);

    start_row();
    label_cell(text_bold("No BOQ Item Group"), 'colspan=2');
    end_row();
    
//    $cost_total = 0;
    
    display_no_boq_item_component_details($project_id, $cost_total);
    
    start_row();
    label_cell(text_bold("Grand Total"),'colspan=9 align=right');
        
    amount_cell($cost_total, true);  
    end_row();
    end_table();
    div_end();
}


function display_boq_item_component_details($project_id, &$cost_total)
{
    global $project_boq_item_component_primary_types;
    
//    $project_id = $boq_group["project_id"];
//    $boq_group_id = $boq_group["id"];
    $result = get_project_boq_groups_and_item_component_details($project_id);
    
    
    /*
      vw_project_boq_item_component_details
     
    select  boq . project_id  AS  project_id , boq . id  AS  id , boq . code  AS  code , boq . description  AS  description , boq . group_id  AS  group_id , boqc . detail_type_code  AS  detail_type_code ,2 AS  primary_type , cm . id  AS  detail_id , cm . item_type  AS  item_type ,'' AS  item_id , cm . stock_id  AS  item_code , cm . tran_unit  AS  unit ,'' AS  item_description , cm . tran_qty  AS  item_qty ,0 AS  rate_id , cm . tran_unit_cost  AS  item_tran_unit_cost , cm . comment  AS  item_comment  
from 0_project_boq_items   boq  
	join  0_project_boq_item_components   boqc  
		on boqc . boq_item_id  =  boq . id  
	join  0_project_boq_item_material cm  
		on cm . boq_item_id  =  boqc . boq_item_id  
		and cm . detail_type_code  =  boqc . detail_type_code    
   
 UNION 
   
select  boq . project_id  AS  project_id , boq . id  AS  id , boq . code  AS  code , boq . description  AS  description , boq . group_id  AS  group_id , boqc . detail_type_code  AS  detail_type_code ,1 AS  primary_type , cc . id  AS  detail_id ,0 AS  item_type ,0 AS  item_id ,'' AS  item_code , cc . unit_name  AS  unit , cc . description  AS  item_description , cc . quantity  AS  item_qty ,0 AS  rate_id , cc . rate  AS  item_tran_unit_cost , cc . comment  AS  item_comment  
   from 0_project_boq_items   boq  
		join  0_project_boq_item_components   boqc  
			on boqc . boq_item_id  =  boq . id  
		join  0_project_boq_item_costs   cc 
			on cc . boq_item_id  =  boqc . boq_item_id  
			and cc . detail_type_code  =  boqc . detail_type_code   
     * 
     */  
    $budget = $used = $allocated = 0;
    $k = 0;
    $previous_boq_item_id = $component_count = $boq_item_total = $boq_group_total = 0;
    $previous_boq_group_id = 0;
    $previous_boq_group_name = $previous_boq_item_name = $boq_item_name = $previous_task_name = $task_name = "";
    
    while ($row = db_fetch($result)) {
        
        $depth = spaces(($row["depth"] - 1) * 5);
        
        $boq_group_id = $row["boq_group_id"];
        $boq_group_code = $row["boq_group_code"];
        $boq_group_description = $row["boq_group_description"];
        
        $boq_group_name = "[".$boq_group_code."] ".$boq_group_description;  
        
//        $boq_group_id = $row["boq_group_id"];
        $task_code = $row["task_code"];
        $task_description = $row["task_description"];
        $task_name = "[".$task_code."] ".$task_description;  
        

        
//        $boq_item_id = $row["detail_type_code"];
        
        $detail_type_code = $row["detail_type_code"];
        $detail_type_name = $row["detail_type_name"];
        $primary_type = $row["primary_type_id"];
        $detail_id = $row["detail_id"]; 
        $item_type = $row["item_type_id"]; 
        if($item_type == 0){
            $item_type_name = "Stock Item";
        }elseif($item_type == 1){
            $item_type_name = "Material Group";
        }
        
        $item_id = $row["item_id"]; 
        if($item_id == 0){
            $item_id = '';
        }
        
        $item_code = $row["item_code"]; 
        $item_description = $row["item_description"]; 
        $item_qty = $row["item_qty"]; 
        $unit = $row["unit"]; 
        $item_tran_unit_cost = $row["item_tran_unit_cost"]; 
        $item_comment = $row["item_comment"]; 
        $boq_item_id = $row["boq_item_id"]; 
        
        $boq_item_code = $row["boq_item_code"];  
        $boq_item_description = $row["boq_item_description"];
        
        if(isset($boq_item_code) && strlen($boq_item_code) > 0){
            $boq_item_name = "[".$boq_item_code."] ".$boq_item_description;  
        }else{
            $boq_item_name = "No BOQ Items founds.";
        }        

        
        
//        if($row["code"] == NULL || strlen($row["code"]) == 0){
//            continue;
//        }

        
        if(($boq_item_id > 0 && $previous_boq_item_id == 0) || // first pass
                        ($boq_item_id != $previous_boq_item_id && $previous_boq_item_id > 0)){ // other
            
            if($previous_boq_item_id > 0){
                start_row();
                    label_cell("BOQ Item ".text_bold($previous_boq_item_name)." Total", 'colspan=8 align=right');      
                    label_cell("");
                    label_cell(text_bold(number_format($boq_item_total,2)), 'align=right'); 
                    $previous_boq_item_name = $boq_item_name;
                    $previous_boq_item_id = $boq_item_id;
                    $boq_group_total += $boq_item_total;
                    $boq_item_total = 0;
                end_row();
            }
            
        }
        
        
                
        if(($boq_group_id > 0 && $previous_boq_group_id == 0) || // first pass
                        ($boq_group_id != $previous_boq_group_id && $previous_boq_group_id > 0)){ // other

            if($previous_boq_group_id > 0){
                start_row();
                    label_cell("BOQ Item Group ".text_bold($previous_boq_group_name)." Total", 'colspan=9 align=right');   
//                    label_cell("");
                    label_cell(text_bold(number_format($boq_group_total,2)), 'align=right'); 
                    $previous_boq_group_name = $boq_group_name;
                    $cost_total += $boq_group_total;
                    $boq_group_total = 0;
                end_row();
            }
            
            
        }
        
                
        if(($boq_group_id > 0 && $previous_boq_group_id == 0) || // first pass
                        ($boq_group_id != $previous_boq_group_id && $previous_boq_group_id > 0)){ // other

                start_row();
                    label_cell(text_bold($boq_group_name)." (BOQ Item Group)", 'colspan=10');      
//                    label_cell("");
//                    label_cell(text_bold(number_format($boq_group_total,2)), 'align=right'); 
                    $previous_boq_item_name = $boq_item_name;
                    $cost_total += $boq_item_total;
                    $boq_item_total = 0;
                end_row();
            
            
        }
        
        $component_count++;
        
        alt_table_row_color($k);

              
        
//        label_cell($depth.spaces(5).$boq_item_name);
        label_cell($depth.spaces(5).$task_name);
        
        label_cell($detail_type_name);   
        
        $primary_type_name = "";
        if($primary_type > 0){
            $primary_type_name = $project_boq_item_component_primary_types[$primary_type];
            $editing_primary_type = $primary_type;
        }else{
            $primary_type_name = "Add";
            $editing_primary_type = BOQPT_COST;
        }
            
        $boq_item_detail_link = get_project_trans_view_str(ST_PROJECT_BOQ_ITEM_DETAIL_EDIT, $boq_item_id."&dtype=".$detail_type_code."&ptype=".$editing_primary_type, $primary_type_name);
        label_cell($boq_item_detail_link, "align=center"); 

//        $material = recalculate_boq_item_component_material($myrow['boq_item_id'], $myrow['detail_type_code']);
//        $material_link = get_project_trans_view_str(ST_PROJECT_BOQ_ITEM_DETAIL_EDIT, $row['task_group_id']."&dtype=".$row["detail_type_code"]."&ptype=".BOQPT_MATERIAL, "Materials");
//        label_cell($material_link, "align=right");
        
//        label_cell($primary_type_name, 'align=center color=blue');
        
        if($detail_id > 0){
            if($k==1){
                $backgroud_colour = "LimeGreen";
            }else{
                $backgroud_colour = "LightGreen";
            }
            label_cell($detail_id, 'align=center  style=background-color:'.$backgroud_colour);   
//            label_cell($item_type_name, 'style=background-color:'.$backgroud_colour); 
//            label_cell($item_id, 'align=center style=background-color:'.$backgroud_colour);  
            label_cell($item_code, 'style=background-color:'.$backgroud_colour);   
            label_cell($item_description, 'style=background-color:'.$backgroud_colour); 
            label_cell($unit, 'style=background-color:'.$backgroud_colour);  
            amount_cell($item_qty, false, 'style=background-color:'.$backgroud_colour); 
            amount_cell($item_tran_unit_cost, false, 'style=background-color:'.$backgroud_colour); 
            $line_amount = $item_tran_unit_cost * $item_qty;
            amount_cell($line_amount, false, 'style=background-color:'.$backgroud_colour); 
            $boq_item_total += $line_amount;
        }else{
            label_cell($detail_id, 'align=center ');  
            label_cell("");  
//            label_cell($item_id, 'align=center ');  
//            label_cell($item_code);  
            label_cell($item_description); 
            label_cell($unit);  
            amount_cell($item_qty);  
            amount_cell($item_tran_unit_cost);  
            $line_amount = $item_tran_unit_cost * $item_qty;
            amount_cell($line_amount);  
            $boq_item_total += $line_amount;
        }
//        label_cell($item_comment);  
//        label_cell($boq_item_id, 'align=center '); 
        
        $previous_boq_item_id = $boq_item_id;
        $previous_boq_group_id = $boq_group_id; 
        $previous_boq_group_name = $boq_group_name;
        $previous_task_name = $task_name;

    }
    
    start_row();
        label_cell("BOQ Item ".text_bold($previous_boq_item_name)." Total", 'colspan=9 align=right');      
//        label_cell("");
        label_cell(text_bold(number_format($boq_item_total,2)), 'align=right');  
        $boq_group_total += $boq_item_total;
        $cost_total += $boq_item_total;
        $boq_item_total = 0;
    end_row();
    
    start_row();
        label_cell("BOQ Item Group ".text_bold($previous_boq_group_name)." Total", 'colspan=9 align=right');      
//        label_cell("");
        label_cell(text_bold(number_format($boq_group_total,2)), 'align=right'); 
        $previous_boq_item_name = $boq_item_name;
        $cost_total += $boq_item_total;
        $boq_item_total = 0;
    end_row();
    
//    end_row();
    

}

function display_no_boq_item_component_details($project_id, &$cost_total)
{
        
    global $project_boq_item_component_primary_types;
    
//    $project_id = $boq_group["project_id"];
//    $boq_group_id = $boq_group["id"];
    $result = get_project_no_boq_group_boq_item_component_details($project_id);
        
//    $depth = spaces(($boq_group["depth"]+1) * 5);
    
    $boq_item_total = $used = $allocated = $previous_boq_item_id = $boq_group_total = 0;
    $previous_boq_item_name = "";
    $k = 0;
    while ($row = db_fetch($result)) {
        alt_table_row_color($k);

        $name = "[".$row["boq_item_code"]."] ".$row["boq_item_description"];  
        
        $detail_type_code = $row["detail_type_code"];
        $detail_type_name = $row["detail_type_name"];
        $primary_type = $row["primary_type_id"];
        $detail_id = $row["detail_id"]; 
//        $item_type = $row["item_type"]; 
//        if($item_type == 0){
//            $item_type = '';
//        }
        
//        $item_id = $row["item_id"]; 
//        if($item_id == 0){
//            $item_id = '';
//        }
        
        $item_code = $row["item_code"]; 
        $item_description = $row["item_description"]; 
        $item_qty = $row["item_qty"]; 
        $unit = $row["unit"]; 
        $item_tran_unit_cost = $row["item_tran_unit_cost"]; 
        $item_comment = $row["item_comment"]; 
        
        $boq_item_id = $row["boq_item_id"];
        
        $boq_item_code = $row["boq_item_code"];  
        $boq_item_description = $row["boq_item_description"];
        $boq_item_name = "[".$boq_item_code."] ".$boq_item_description;
        
        if(($boq_item_id > 0 && $previous_boq_item_id == 0) || // first pass
                        ($boq_item_id != $previous_boq_item_id && $previous_boq_item_id > 0)){ // other
            
            if($previous_boq_item_id > 0){
                start_row();
                    label_cell("BOQ Item ".text_bold($previous_boq_item_name)." Total", 'colspan=9 align=right');      
//                    label_cell("");
                    label_cell(text_bold(number_format($boq_item_total,2)), 'align=right'); 
                    $previous_boq_item_name = $boq_item_name;
                    $previous_boq_item_id = $boq_item_id;
                    $boq_group_total += $boq_item_total;
                    $cost_total += $boq_item_total;
                    $boq_item_total = 0;
                end_row();
            }
            
        }
        
              
        
        label_cell($name);
        
        label_cell($detail_type_name);  
        
        $primary_type_name = "";
        if($primary_type > 0){
            $primary_type_name = $project_boq_item_component_primary_types[$primary_type];
            $editing_primary_type = $primary_type;
        }else{
            $primary_type_name = "Add";
            $editing_primary_type = BOQPT_COST;
        }
            
        $boq_item_detail_link = get_project_trans_view_str(ST_PROJECT_BOQ_ITEM_DETAIL_EDIT, $boq_item_id."&dtype=".$detail_type_code."&ptype=".$editing_primary_type, $primary_type_name);
        label_cell($boq_item_detail_link, "align=center"); 
        
        if($detail_id > 0){
            if($k==1){
                $backgroud_colour = "LimeGreen";
            }else{
                $backgroud_colour = "LightGreen";
            }
            label_cell($detail_id, 'align=center  style=background-color:'.$backgroud_colour);   
//            label_cell($item_type_name, 'style=background-color:'.$backgroud_colour); 
//            label_cell($item_id, 'align=center style=background-color:'.$backgroud_colour);  
            label_cell($item_code, 'style=background-color:'.$backgroud_colour);   
            label_cell($item_description, 'style=background-color:'.$backgroud_colour); 
            label_cell($unit, 'style=background-color:'.$backgroud_colour);  
            amount_cell($item_qty, false, 'style=background-color:'.$backgroud_colour); 
            amount_cell($item_tran_unit_cost, false, 'style=background-color:'.$backgroud_colour); 
            $line_amount = $item_tran_unit_cost * $item_qty;
            amount_cell($line_amount, false, 'style=background-color:'.$backgroud_colour); 
        }else{
            label_cell($detail_id, 'align=center ');  
    //        label_cell($item_type);  
    //        label_cell($item_id, 'align=center ');  
            label_cell($item_code);  
            label_cell($item_description);  
            label_cell($unit);  
            amount_cell($item_qty);  
            amount_cell($item_tran_unit_cost);  
            $line_amount = $item_tran_unit_cost * $item_qty;
            amount_cell($line_amount);  
        }
            $boq_item_total += $line_amount;
    //        label_cell($item_comment); 
        
        $previous_boq_item_id = $boq_item_id;
        $previous_boq_item_name = $name;
    }
    
    start_row();
        label_cell("BOQ Item ".text_bold($previous_boq_item_name)." Total", 'colspan=9 align=right');      
//        label_cell("");
        label_cell(text_bold(number_format($boq_item_total,2)), 'align=right'); 
        $boq_group_total += $boq_item_total;  
        $cost_total += $boq_item_total; 
    end_row();
    
    
    start_row();
        label_cell(text_bold("No BOQ Item Group")." Total", 'colspan=9 align=right');                
        label_cell(text_bold(number_format($boq_group_total,2)), 'align=right');  
    end_row();
}


function get_background_colour($value)
{
    $colour = '';
    if($value < 0){
        $colour = "'background-color:rgb(138,141,143)'";
    }
    elseif($value <= 5){
        $colour = "'background-color:rgb(204, 0, 0)'";
    }
    elseif($value <= 10){
        $colour = "'background-color:rgb(255, 80, 80)'";
    }
    elseif($value <= 20){
        $colour = "'background-color:rgb(255, 102, 102)'";
    }
    elseif($value <= 30){
        $colour = "'background-color:rgb(255, 153, 153)'";
    }
    elseif($value <= 40){
        $colour = "'background-color:rgb(255, 204, 204)'";
    }
    elseif($value <= 50){
        $colour = "'background-color:rgb(255, 255, 204)'";
    }
    else{
        
    }
    
    return $colour;
}