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


function build_work_done_boq_groups_array(&$items, $project_id)
{
    $loop_count = 1;
    
    $boq_groups_top_q = get_top_level_boq_groups($project_id);    
    
    while ($boq_groups_top = db_fetch($boq_groups_top_q))
    {
        $line = array();
        $line['id'] = $boq_groups_top['id']; 
        $line['code'] = $boq_groups_top['code']; 
        $line['description'] = $boq_groups_top['description']; 

        $line['parent_id'] = $boq_groups_top['parent_id'];  
        
        $line['depth'] = $loop_count;
        
        $child_groups_array = add_child_boq_groups_to_array($boq_groups_top['id'], $loop_count);
        if($child_groups_array){    
            $line['children'] = $child_groups_array;
        }
        else{
            $line['children'] = array();
        }       

        $items[] = $line;        
    }
}


function add_child_boq_groups_to_array($parent_id, &$loop_count)
{
    $loop_count++;
    if($loop_count > 20){
        //Stop infinite recursive loop
        return null;
    }
    $boq_groups_array = array();

    $child_boq_groups_q = get_child_boq_groups($parent_id);    
    
    while ($boq_group = db_fetch($child_boq_groups_q))
    {
        $line = array();
        $line['id'] = $boq_group['id']; 
        $line['code'] = $boq_group['code']; 
        $line['description'] = $boq_group['description']; 

        $line['parent_id'] = $boq_group['parent_id']; 

        $line['depth'] = $loop_count;
        
        $child_groups_array = add_child_boq_groups_to_array($boq_group['id'], $loop_count);
        if($child_groups_array){    
            $line['children'] = $child_groups_array;
        }
        else{
            $line['children'] = array();
        }       

        $boq_groups_array[] = $line;
    }
    
    $loop_count--;

    return $boq_groups_array;

}


function display_work_done_boq_item_group($project_id, $boq_groups, $path_to_root)
{


    alt_table_row_color($k);
//    label_cell($boq_groups["display_order"]);

    $name = spaces(($boq_groups["depth"]-1) * 10)."[".$boq_groups["code"]."] ".$boq_groups["description"];
    label_cell($name);
//    amount_decimal_cell($boq_groups["child_groups_total"]); 
//    amount_decimal_cell($boq_groups["child_groups_taxes_total"]); 
//    amount_decimal_cell($boq_groups["boq_items_total"]); 
//    amount_decimal_cell($boq_groups["boq_item_taxes_total"]); 

//    $entry_date = sql2date($_POST['tran_date']);
    //$url = "<a href='$path_to_root/projects/work_done_entry_details.php?ProjectId=".$project_id."&BOQGroupId=".$boq_groups["id"]."'>" . "Details" . "</a>";
    $s = get_project_trans_view_str(ST_PROJECT_BOQ_GROUP_WORK_DONE, $project_id."|".$boq_groups['id'], "Details");
    label_cell($s);
//    button_cell("Edit".$boq_groups['id'], "Details");
//    label_cell($url);
//    button_cell("Edit".$boq_groups['id'], "Details");
    end_row();
    
    foreach ($boq_groups['children'] as $myrow) {
        display_work_done_boq_item_group($project_id, $myrow, $path_to_root);
    }
}


function display_work_done_boq_groups($path_to_root, $project_id)
{
    global $Ajax;
    
    $boq_groups = array();
    
    $Ajax->activate('iou_details');

    build_work_done_boq_groups_array($boq_groups, $project_id);
    
    $result = get_project_boq_item_groups($project_id);
    div_start('iou_details');

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

    $th = array(_("Group"), "");
    table_header($th);

    $k=0; //row colour counter

    foreach ($boq_groups as $myrow) {
            display_work_done_boq_item_group($project_id, $myrow, $path_to_root);
    }
    
    $name = "BOQ Items with no Group";
    label_cell($name);
//    amount_decimal_cell($boq_groups["child_groups_total"]); 
//    amount_decimal_cell($boq_groups["child_groups_taxes_total"]); 
//    amount_decimal_cell($boq_groups["boq_items_total"]); 
//    amount_decimal_cell($boq_groups["boq_item_taxes_total"]); 

//    $entry_date = sql2date($_POST['tran_date']);
    $s = get_project_trans_view_str(ST_PROJECT_BOQ_GROUP_WORK_DONE, $project_id."|0", "Details");
    label_cell($s);

    end_table(1);
    div_end();
}



function delete_work_done_entry_details($project_id, $boq_group_id, $site_id)
{   
    $delete_count = 0;
    
    $batch_q = get_group_boq_items($boq_group_id);

    while ($boq_item = db_fetch($batch_q))
    {
        if(isset($_POST[$boq_item["id"]."_check_item"]) && $_POST[$boq_item["id"]."_check_item"] == 1){  
                    
            $boq_item_id = $_POST[$boq_item["id"]."_"."boq_item_id"];
            $tran_date =  $_POST['entry_date'];
                
            delete_work_done_details($boq_item_id, $site_id, $tran_date);     
            
            unset($_POST[$boq_item["id"]."_check_item"]);
            $delete_count++;
        }
        else{
            continue;
        }
    }    
    
    return $delete_count;
      
}


function display_work_done_entry_details($project_id, $boq_group_id, $entry_date, $project_site_id)
{
    global $Ajax; 
    $Ajax->activate('work_done_details');
    
    if(!($boq_group_id >= 0)){
        $boq_group_id = 0;
    }
    
    if(!($project_site_id >= 0)){
        $project_site_id = 0;
    }
    
    div_start('work_done_details');
//  display_heading('Expenses');
    
    $entry_date = date2sql($_POST['entry_date']);
    
    start_table(TABLESTYLE, "width=90%");
//    $result_a = get_group_boq_items($boq_group_id);
    $result_a = get_project_group_boq_items($project_id, $boq_group_id);
    
    
    $th = array(_("ID"),_("Code"), _("Description"), "Billing Type",
                _("Completed Quantity"), _("Unit"), "Last Entry Date",  _("Quantity"),_("Comments"), "");
   
    table_header($th);

    $j = 1;
    $k=0; //row colour counter

    while ($boq_item = db_fetch($result_a))
    {
        alt_table_row_color($k);
        
//        label_cell($boq_item["id"], "align=right");
//        label_cells(get_petty_cash_trans_view_str(ST_pro, $iou_line["id"]));
//        label_cell(get_project_trans_view_str(ST_PROJECT_BOQ_ITEM, $boq_item["id"]), "align=center");
        label_cell($boq_item["id"], "align=center");
        label_cell($boq_item["code"]);
        label_cell($boq_item["description"]);

        if($boq_item["billing_type"] == 1){
            label_cell("Lump Sum");
        }else{
            label_cell("Measure and Pay");
        }
        
        $boq_item_site_completed_qty = 0;
        $boq_item_site_rec = get_boq_item_site_record($boq_item["id"], $project_site_id);
        if(isset($boq_item_site_rec['completed_qty']) &&  $boq_item_site_rec['completed_qty'] > 0){
            $boq_item_site_completed_qty =  $boq_item_site_rec['completed_qty'];
        }
        

//        label_cell(get_project_trans_view_str(ST_PROJECT_WORK_DONE, $boq_item_site_completed_qty), "align=right");
        if($boq_item_site_completed_qty>0){
            $s = get_project_trans_view_str(ST_PROJECT_BOQ_ITEM_WORK_DONE, $boq_item["id"]."|".$project_site_id, $boq_item_site_completed_qty);
        }
        else{
            $s = "";
        }
        label_cell($s, "align=right");
        
        label_cell($boq_item["unit"]);        
        hidden($boq_item["id"]."_unit", $boq_item["unit"]);
     
        
        if($boq_item["last_entry_date"] == "0000-00-00"){
            label_cell("");
        }else{
            label_cell(sql2date($boq_item['last_entry_date']), "align=center");
        }
       
//        mmddyyyy=sql2date(yyyymmdd);
        $work_done_detail = get_work_done_detail($boq_item["id"], $project_site_id, $entry_date);
        if($work_done_detail){
            $_POST[$boq_item["id"]."_tran_date"] = sql2date($work_done_detail['tran_date']);
            $_POST[$boq_item["id"]."_quantity"] = $work_done_detail["quantity"];
            $_POST[$boq_item["id"]."_comment"] = $work_done_detail["comment"];
        }
        else{
            $_POST[$boq_item["id"]."_tran_date"] = sql2date($entry_date);
            $_POST[$boq_item["id"]."_quantity"] = 0.00;
            $_POST[$boq_item["id"]."_comment"] = "";
        }
        
//      $_POST[$boq_item["id"]."_tran_date"] = sql2date($_POST[$boq_item["id"]."_tran_date"]);
//        date_cells(null, $boq_item["id"]."_tran_date");
        amount_cells(null,$boq_item["id"]."_quantity");
        
        text_cells(null, $boq_item["id"]."_comment");
            
        hidden($boq_item["id"]."_"."boq_item_id", $boq_item["id"]);

        check_cells(null, $boq_item["id"]."_check_item");
    }
    
    hidden('ProjectId', $project_id);
    hidden('BOQGroupId', $boq_group_id);
    
    end_table(1);
    div_end();
}


function save_work_done_entry_details($project_id, $boq_group_id, $site_id)
{   
//    $batch_q = get_group_boq_items($boq_group_id);
    $batch_q = get_project_group_boq_items($project_id, $boq_group_id);

    while ($boq_item = db_fetch($batch_q))
    {
        if(isset($_POST[$boq_item["id"]."_check_item"]) && $_POST[$boq_item["id"]."_check_item"] == 1){  
                
                $r = can_process($boq_item["id"], $site_id);            
                if($r["success"] !== true){
                    return $r;
                }
                    
                $boq_item_id = $_POST[$boq_item["id"]."_"."boq_item_id"];
                
                
                $tran_date =  $_POST['entry_date'];
                $quantity = input_num($boq_item["id"].'_'.'quantity');
                $unit =  $_POST[$boq_item["id"].'_'.'unit']; 
                $comment =  $_POST[$boq_item["id"].'_'.'comment']; 
                
            $n = write_work_done_entry_detail($boq_item_id, $site_id, $tran_date, $quantity, $unit, $comment);     
            
            unset($_POST[$boq_item["id"]."_check_item"]);
        }
        else{
            continue;
        }
    }    
        return $r;
}

function can_process($boq_item_id, $site_id)
{
    $tran_date = $_POST['entry_date'];   
    
    $boq_item = get_project_boq_item_details($boq_item_id);
//    $last_entry_date = sql2date($boq_item['last_entry_date']);     
    $description = $boq_item['description'];
    $code = $boq_item['code'];
    
    if(!(check_num2(input_num($boq_item_id."_quantity"), 0.001))){   
        $message = "[".$code."] ".$description." - quantity entered should be positive and valid amount."; 
        $set_focus = $boq_item["id"]."_quantity";
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);                    
    }
        
    $site_wd = get_last_site_work_done_detail_record($boq_item_id, $site_id);
    if($site_wd){
        $last_entry_date = sql2date($site_wd['tran_date']);
    }
    else{
        $last_entry_date = "";
    }
    
    if(date1_greater_date2($last_entry_date, $tran_date)){   
        $message = "[".$code."]".$description." - There is an already entered workdone detail for ".$last_entry_date."."; 
        $set_focus = $boq_item["id"]."_quantity";
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);                    
    }     

    return array("success" => true, "message" => "" );     
}

?>