<?php

function display_copy_project_boq_items_from_tasks($project_id) {
    global $Ajax;
    $Ajax->activate('copy_project_boq_items_from_tasks');
    
    if (! (isset($project_id) && $project_id > 0)) {
        $msg = "Please select a project.";
        display_error($msg); 
        return;
    } 
    
    $boq_groups = array();
    build_boq_groups_summary_array_new($boq_groups, $project_id);
    
    div_start('copy_project_boq_items_from_tasks');
        start_table(TABLESTYLE, "width=80%");

            $th = array(_("Budget Name"), _("Project BOQ Item Component"));  
            table_header($th);

                foreach ($boq_groups as $myrow) {
                    $depth = spaces(($myrow["depth"] - 1) * 10);

                    $name = "[".$myrow["code"]."] ".$myrow["description"]." (".$myrow['id'].")";
                    
                    start_row();
                        label_cell($depth.text_bold($name), 'colspan=2');
                    end_row();
                    
                    $boq_group_id = $myrow["id"];
                    $depth_value = $myrow["depth"];
                    display_boq_item_group_budgets_with_components($boq_group_id, $depth_value, $project_id);
                }

            display_copy_no_boq_item_group_tasks_with_components($project_id);

        end_table(2);
    div_end();
}

function display_boq_item_group_budgets_with_components($boq_group_id, $depth_value, $project_id) {
    $result = get_boq_item_group_budget_details_with_boq_item_component_details($project_id, $boq_group_id);
        
    $depth = spaces(($depth_value) * 10);
    
        $k = 0;
        while ($row = db_fetch($result)) {
            alt_table_row_color($k);

                $task_name = "[".$row["task_code"]."] ".$row["description"]." (".$row["task_id"].")";

                label_cell($depth.$task_name);

                if ($row["boq_item_id"] > 0 && $row['boq_item_componet_id'] > 0) {
                    if ($row["boq_item_id"] == $row['cmp_boq_item_id']) {
                        label_cell('BOQ item component available');
                    } else {
                        label_cell('BOQ item component is linked with wrong BOQ item', 'style=color:red');
                    }
                } elseif ($row["boq_item_id"] > 0) {
                    label_cell('BOQ item is availabble without BOQ item component', 'style=color:red');
                } elseif ($row["boq_item_componet_id"] > 0) {
                    label_cell('BOQ item compenent is availabble without BOQ item', 'style=color:red');
                } else {   
                    label_cell('No matching BOQ item component found');
                } 

            end_row();
        }
}

function display_copy_no_boq_item_group_tasks_with_components($project_id) {
    label_cell(text_bold("Budgets with no BOQ Item Group"));
    display_boq_item_group_budgets_with_components(0, 0, $project_id);
}

function handle_copy_project_boq_items_from_tasks($project_id) { 
    if (! (isset($project_id) && $project_id > 0)){
        return array("success" => false, "message" =>" Please select a project");
    } 
    
    $boq_groups = array(); 
    build_boq_groups_summary_array_new($boq_groups, $project_id);

        $saved_recs = 0;
        foreach ($boq_groups as $myrow) {
            $boq_group_id = $myrow["id"];
            
            $msg = copy_project_boq_item_from_tasks($boq_group_id, $project_id);
                if ($msg["success"] !== true) {
                    return $msg;
                }
                
                $saved_recs += $msg["saved_recs"];
        }

        $msg = copy_project_boq_item_from_tasks(0, $project_id);
            if ($msg["success"] !== true) {
                return $msg;
            }
            
            $saved_recs += $msg["saved_recs"];
            
        if (! ($saved_recs > 0)) {
            return array("success" => false, "message" => "All project BOQ items and budgets are identical.");
        }

        return array("success"=>true, "message" => "Project BOQ items have been copied.");
}

function copy_project_boq_item_from_tasks($boq_group_id, $project_id) {
    $saved_recs = 0;
    $result = get_boq_item_group_budget_details_with_boq_item_component_details($project_id, $boq_group_id);
        while ($row = db_fetch($result)) {
            $boq_item_id = $row['boq_item_id']; $cmp_boq_item_id = $row['cmp_boq_item_id']; $boq_item_componet_id = $row['boq_item_componet_id']; $task_id = $row['task_id']; 
            
            if ($boq_item_componet_id > 0 && $boq_item_id > 0 && $boq_item_id == $cmp_boq_item_id) {
                continue;
            }
            
            $error_line = $row['description']." ($task_id)";
            
            begin_transaction();
            
            // check boq item exsist
            if ($boq_item_id > 0) {
                // check to boq item component exsist => budget id = boq item component id
                if ($boq_item_componet_id > 0) {
                    //  if project boq item component's boq_item_id is mismatched
                    if ($boq_item_id != $cmp_boq_item_id) {
                        $com_af_rw = update_project_boq_item_component_boq_item_id($boq_item_componet_id, $boq_item_id);
                            if ($com_af_rw != 1) {
                                cancel_transaction();
                                return array('success' => false, 'message' => "Budget $error_line; Could not update project BOQ item component.");
                            }
                    }
                } else {
                    // add project boq item component 
                    $com_ins_id = add_project_boq_item_component($task_id, $boq_item_id, $row['detail_type_code'], 0, 0, 0);
                        if (! ($com_ins_id > 0)) {
                            cancel_transaction();
                            return array('success' => false, 'message' => "Budget $error_line; Could not add project BOQ item component.");
                        }
                }
            } else {
                // check project boq item record is already created by going through this loop
                $nw_boq_itm_row = get_project_boq_item_record($row['task_group_id']);
                    if (isset($nw_boq_itm_row['id']) && $nw_boq_itm_row['id'] > 0) {
                        $pro_boq_ins_id = $nw_boq_itm_row['id'];
                    } else {
                        // add project boq item  
                        $pro_boq_ins_id = add_project_boq_item_with_id($row['task_group_id'], $project_id, $row['type_id'], 0, $row['code'], $row['description'], 
                                                                       0, '', 0, 0, '', 0, 0, $row['boq_group_id'], $row['start_date'], $row['end_date']);
                            if (! ($pro_boq_ins_id > 0)) {
                                cancel_transaction();
                                return array('success' => false, 'message' => "Budget $error_line; Could not add project BOQ item.");
                            } 
                    }
                    
                // check to boq item component exsist => budget id = boq item component id
                if ($boq_item_componet_id > 0) {
                    //  if old project boq item component's boq_item_id is mismatched with newly inserted one then update
                    if ($pro_boq_ins_id != $cmp_boq_item_id) {
                        $com_af_rw = update_project_boq_item_component_boq_item_id($boq_item_componet_id, $pro_boq_ins_id);
                            if ($com_af_rw != 1) {
                                cancel_transaction();
                                return array('success' => false, 'message' => "Budget $error_line; Could not update project BOQ item component.");
                            }
                    }
                } else {
                    // add project boq item component 
                    $com_ins_id = add_project_boq_item_component($task_id, $pro_boq_ins_id, $row['detail_type_code'], 0, 0, 0);
                        if (! ($com_ins_id > 0)) {
                            cancel_transaction();
                            return array('success' => false, 'message' => "Budget $error_line; Could not add project BOQ item component.");
                        }
                }
            }
                
            commit_transaction();    
            
            $saved_recs++;
        }
    
        return array("success" => true, "message" => "", "saved_recs" => $saved_recs);
}

?>