<?php

function display_copy_project_tasks($from_project_id, $to_project_id, $copy_type, $line_count){
    global $Ajax;
    
    $Ajax->activate('price_table');
    
    $boq_groups = array();
    if(!(isset($from_project_id) && $from_project_id > 0)){
        $msg = "Please select a project to copy from.";
        display_error($msg); 
        return;
    } 
    if(!(isset($to_project_id) && $to_project_id > 0)){
        $msg = "Please select a project to copy to";
        display_error($msg); 
        return;
    } 
    
    build_boq_groups_summary_array_new($boq_groups, $from_project_id);
    
    div_start('price_table');

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

            $th = array(_("From Budget Name"),_("From Budget Amount"), _("To Budget Name"), "");  
            table_header($th);

            $budgets_total = $used_total = $allocated_total = $new_budgets_total = 0;

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

                    $name = "[".$myrow["code"]."] ".$myrow["description"]." (".$myrow['id'].")";
                    label_cell($depth.text_bold($name));
                    label_cell("");

                    $to_group_name = "No matching BOQ Item Group found";
                    $to_depth = '';
                    $to_boq_group = get_project_boq_item_group_record_by_code($to_project_id, $myrow["code"]);
                    if(isset($to_boq_group['id']) && $to_boq_group['id'] > 0){
                        $to_group_name = "[".$to_boq_group["code"]."] ".$to_boq_group["description"]." (".$to_boq_group['id'].")";
                        $to_depth = spaces(($to_boq_group["depth"] - 1) * 10);
                        label_cell($to_depth.text_bold($to_group_name));
                    }else{
                        label_cell($to_group_name);
                    }

                    label_cell("");

                    $boq_group_id = $myrow["id"];
                    $depth_value = $myrow["depth"];
                    display_boq_item_group_budgets($boq_group_id, $depth_value, $budgets_total, $from_project_id, $to_project_id, $copy_type, $line_count);
                }

            display_copy_no_boq_item_group_tasks($budgets_total, $from_project_id, $to_project_id, $copy_type, $line_count);

            start_row();
                label_cell(text_bold("Total"));
                amount_cell($budgets_total, true);  
                amount_cell($new_budgets_total); 
            end_row();
        end_table(2);
    div_end();
}

function display_boq_item_group_budgets($boq_group_id, $depth_value, &$budgets_total, $from_project_id, $to_project_id, $copy_type, $line_count)
{
    $result = get_boq_item_group_budget_details_with_target_budget_details($from_project_id, $boq_group_id, $to_project_id);
        
    $depth = spaces(($depth_value) * 10);
    
    $budget = $used = $allocated = 0;
    $k = 0;
    while ($row = db_fetch($result)) {
        alt_table_row_color($k);

        $from_task_name = "[".$row["task_code"]."] ".$row["description"]." (".$row["task_id"].")";
        $check_post_name = "chk_".$row["task_id"]."_".$boq_group_id;
        
        label_cell($depth.$from_task_name);
        
        $budget = $row["budget"]; 
        
        amount_decimal_cell($budget); 
        
        $to_task_id = 0;
        $to_task_code = $to_description = $to_task_name = "";
        if(isset($row["to_task_id"]) && $row["to_task_id"] > 0){
            $to_task_id = $row["to_task_id"];
            $to_task_code = $row["to_task_code"];
            $to_description = $row["to_description"];
            $to_task_name = "[".$to_task_code."] - ".$to_description." (".$to_task_id.")";
            
            $to_task = get_task_record($to_task_id);
            $to_task_group = get_task_group_record_by_id($to_task["task_group_id"]);
            $to_boq_group_id = $to_task_group["boq_group_id"];
            
            $to_task_name = "BOQ Group Id: ".$to_boq_group_id."<br>".$to_task_name;
        }
        
        if ($to_task_id > 0 && $row['to_boq_item_componet_id'] > 0) {
            label_cell($to_task_name);
            label_cell("Available", 'align=center');    
        } elseif ($to_task_id > 0 && ! ($row['to_boq_item_componet_id'] > 0)) {
            $to_task_name .= " (For the this budget related project BOQ item component is not found.)";
            label_cell($to_task_name, 'style=color:red');
            check_cells("", $check_post_name, null, false, false, 'align=center');  
        } else {   
            label_cell($to_task_name);
            if($copy_type == AUST_ADD_ONLY || $copy_type == AUST_ADD_OR_UPDATE){
                $_POST[$check_post_name] = 1;
            }
            check_cells("", $check_post_name, null, false, false, 'align=center');  
        } 
        
        $budgets_total += $budget; 
    }

    end_row();
}

function display_copy_no_boq_item_group_tasks(&$budgets_total, $from_project_id, $to_project_id, $copy_type, $line_count)
{
    label_cell(text_bold("Budgets with no BOQ Item Group"));
    display_boq_item_group_budgets(0, 0, $budgets_total, $from_project_id, $to_project_id, $copy_type, $line_count);
}

function handle_copy_project_budget_details_to_another_project($from_project_id, $to_project_id) { 
    $boq_groups = array(); 
    
    if(!(isset($from_project_id) && $from_project_id > 0)){
        return array("success"=>false, "message"=>"Please select a project to copy from");
    } 
    
    if(!(isset($to_project_id) && $to_project_id > 0)){
        return array("success"=>false, "message"=>"Please select a project to copy to");
    } 
    
    if ($from_project_id == $to_project_id) {
        return array("success" => false, "message" => "From project and to project cannot be the same project.");
    } 
    
    build_boq_groups_summary_array_new($boq_groups, $from_project_id);

        $selected_recs = 0;
        $to_project = get_project_record($to_project_id);
        foreach ($boq_groups as $myrow) {
            $from_boq_group_id = $myrow["id"];
            $from_boq_group_code = $myrow["code"];
            
            $msg = copy_project_boq_item_group_budget_details_to_another_project($from_boq_group_id, $from_boq_group_code, $from_project_id, $to_project);
                if ($msg["success"] !== true) {
                    return $msg;
                }
                
                $selected_recs += $msg["selected_recs"];
        }

        $msg = copy_project_boq_item_group_budget_details_to_another_project(0, "", $from_project_id, $to_project);
            if ($msg["success"] !== true) {
                return $msg;
            }
            
            $selected_recs += $msg["selected_recs"];
            
        if (! ($selected_recs > 0)) {
            return array("success" => false, "message" => "Please select at least one budget before copying.");
        }

        return array("success"=>true, "message" => "Selected budgets have been copied.");
}

function copy_project_boq_item_group_budget_details_to_another_project($from_boq_group_id, $from_boq_group_code, $from_project_id, $to_project) {
    $to_project_id = $to_project["project_id"];
    $selected_recs = 0;
    
    $result = get_boq_item_group_budget_details_with_target_budget_details($from_project_id, $from_boq_group_id, $to_project_id);
        while ($row = db_fetch($result)) {
            $check_post_name = "chk_".$row["task_id"]."_".$from_boq_group_id;
                if (! (isset($_POST[$check_post_name]) && $_POST[$check_post_name] == true)) {
                    continue;
                }
                
            $from_boq_group_id = $row['from_boq_group_id']; $from_budget_group_code = $row['from_budget_group_code']; $from_boq_group_type_id = $row['from_boq_group_type_id'];
            $from_budget_group_description = $row['from_budget_group_description']; $from_budget_group_long_description = $row['from_budget_group_long_description'];
            $from_task_id = $row['task_id']; $start_date_ = sql2date($row['start_date']); $end_date_ = sql2date($row['end_date']); $from_task_description = $row['description']; 
            $to_task_id = $row['to_task_id']; $to_boq_item_id = $to_task_group_id = $row['to_task_group_id']; $to_detail_type_code = $row['to_detail_type_code'];
            $to_boq_item_componet_id = $row['to_boq_item_componet_id']; $selected_recs++;
            
            $error_line = "$from_task_description ($from_task_id)";
            begin_transaction();
            
            // check to budget exsist
            if ($to_task_id > 0) {
                // check to BOQ component exsist => budget id = boq item component id
                if ($to_boq_item_componet_id > 0) {
                    commit_transaction();
                    continue;
                } else {
                    $com_ins_id = add_project_boq_item_component($to_task_id, $to_boq_item_id, $to_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();
                    continue;
                }
            } else {
                // check to project task group exsist 
                $tsk_grp_row = get_task_group_record_by_code($to_project_id, $from_budget_group_code);    
                    if (isset($tsk_grp_row['id']) && $tsk_grp_row['id'] > 0) {
                        $tsk_grp_id = $tsk_grp_row['id'];
                        
                        // if to project BOQ item is not exsist then add
                        $boq_itm_row = get_project_boq_item_record_by_code($to_project_id, $from_budget_group_code);    
                            if (! (isset($boq_itm_row['id']) && $boq_itm_row['id'] > 0)) {
                                $pro_boq_ins_id = add_project_boq_item_with_id($tsk_grp_id, $to_project_id, $tsk_grp_row['type_id'], 0, $tsk_grp_row['code'], 
                                                                               $tsk_grp_row['description'], 0, '', 0, 0, '', 0, 0, $tsk_grp_row['boq_group_id'], 
                                                                               $tsk_grp_row['start_date'], $tsk_grp_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.");
                                    }                                           
                            } else {
                                $pro_boq_ins_id = $boq_itm_row['id'];
                            }
                    } else {
                        // check to project BOQ item exsist without the project task group then it is impossible => error
                        $boq_itm_row = get_project_boq_item_record_by_code($to_project_id, $from_budget_group_code);    
                            if (isset($boq_itm_row['id']) && $boq_itm_row['id'] > 0) {
                                cancel_transaction();
                                return array('success' => false, 'message' => "Budget $error_line; Has a record in project BOQ item without the project budget group.");
                            }
                          
                        $to_boq_group_id = 0;    
                        if (strlen($from_boq_group_code) > 0) {    
                            // check to BOQ item group is not exsist then error   
                            $to_boq_grp_row = get_project_boq_item_group_record_by_code($to_project_id, $from_boq_group_code);
                                if (! (isset($to_boq_grp_row['id']) && $to_boq_grp_row['id'] > 0)) {
                                    cancel_transaction();
                                    return array('success' => false, 'message' => "Budget $error_line; Please create project BOQ item group for the 'To project'.");
                                }

                                $to_boq_group_id = $to_boq_grp_row['id'];
                        }
                            
                        $tsk_grp_id = add_task_group_details($to_project_id, $from_budget_group_code, $from_budget_group_description, $from_boq_group_type_id, 
                                                             $from_budget_group_long_description, $start_date_, $end_date_, $to_boq_group_id);    
                            if (! ($tsk_grp_id > 0)) {
                                cancel_transaction();
                                return array('success' => false, 'message' => "Budget $error_line; Could not add project task group.");
                            }
                            
                        $pro_boq_ins_id = add_project_boq_item_with_id($tsk_grp_id, $to_project_id, $from_boq_group_type_id, 0, $from_budget_group_code, 
                                                                       $from_budget_group_description, 0, '', 0, 0, $from_budget_group_long_description, 0, 0, 
                                                                       $to_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.");
                            }    
                    }
                    
                commit_transaction();
                    
                begin_transaction();
                    $tsk_ins_id = add_task($to_project_id, $row['task_code'], $row['description'], $row['type_id'], $row['budget'], $tsk_grp_id, $row['detail_type_code']);
                        if (! ($tsk_ins_id > 0)) {
                            cancel_transaction();
                            return array('success' => false, 'message' => "Could not add project budget from budget $from_task_description.");
                        }

                    $com_ins_id = add_project_boq_item_component($tsk_ins_id, $pro_boq_ins_id, $row['detail_type_code'], 0, 0, 0);
                        if (! ($com_ins_id > 0)) {
                            cancel_transaction();
                            return array('success' => false, 'message' => "Could not add BOQ item component from budget $from_task_description.");
                        }
                commit_transaction();
            }
        }
    
        return array("success" => true, "message" => "", "selected_recs" => $selected_recs);
}

?>