<?php

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

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

            $th = array(_("From BOQ Item"), _("Type"), _("Detail Type"), _('Id'), _('Item'), _('Unit'), _('Qty'), _('Amount'), _('Cost'), _('Availability'), _(''));  
            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=11');
                    end_row();

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

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

        end_table(2);
    div_end();
}

function display_project_boq_item_components($boq_group_id, $depth_value, $from_project_id, $to_project_id, $copy_type) {
    $depth = spaces(($depth_value) * 10);
    $k = $previous_task_id = 0;
    $type = ST_PROJECT_BOQ_ITEM_DETAIL_EDIT;
        
    $result = get_project_boq_item_group_budget_details($from_project_id, $boq_group_id);
        while ($row = db_fetch($result)) {
            
            $task_id = $row["task_id"]; // equal to boq item component id
            $task_code = $row["task_code"]; 
            $from_task_group_id = $row["from_task_group_id"]; 
            
            alt_table_row_color($k);

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

                label_cell($depth.$from_task_name);
                
                $error_msg = null;
                $from_task_row = get_project_boq_item_record($from_task_group_id);
                $to_boq_item_row = get_project_boq_item_record_by_code($to_project_id, $from_task_row['code']);
                    if (! (isset($to_boq_item_row['id']) && $to_boq_item_row['id'] > 0)) {
                        $error_msg = "Could not find to project BOQ item";
                    } else {
                        $to_cmp_row = get_boq_item_component_record($to_boq_item_row['id'], $row['detail_type_code']);
                            if (! (isset($to_cmp_row['id']) && $to_cmp_row['id'] > 0)) {
                                $error_msg = "Could not find to project BOQ item component";
                            }
                    }

                $result_2 = get_project_boq_item_component_items($task_id);
                    if (db_num_rows($result_2) > 0) {
                        while ($row_2 = db_fetch($result_2)) {
                            $id = $row_2['id'];
                            $detail_type_code = $row_2['detail_type_code']; $item_type = $row_2['item_type']; $stock_id = $row_2['stock_id']; 
                            $item_or_cost = $row_2['item_or_cost']; $tran_unit = $row_2['tran_unit']; $tran_qty = $row_2['tran_qty']; 
                            $tran_unit_cost = $row_2['tran_unit_cost'];
                            $check_post_name = "chk_".$task_id."_".$boq_group_id."_".$id;

                            if ($previous_task_id != 0 && $task_id == $previous_task_id) {
                                alt_table_row_color($k);
                                if ($task_id == $previous_task_id) {
                                    label_cell('');
                                }
                            }

                                label_cell($item_or_cost, 'align=center');  
                                label_cell($row_2['details_type']);
                                
                                if ($item_or_cost == 'Item') {
                                    $link = get_project_trans_view_str($type, $from_task_group_id."&dtype=$detail_type_code&ptype=".BOQPT_MATERIAL, $id);
                                } elseif ($item_or_cost == 'Cost') {
                                    $link = get_project_trans_view_str($type, $from_task_group_id."&dtype=$detail_type_code&ptype=".BOQPT_COST, $id);
                                }
                                
                                label_cell($link, 'align=center');  
                                label_cell($stock_id.' '.$row_2['item_description']);  
                                label_cell($row_2['unit_name'], 'align=center');  
                                amount_cell($tran_qty);  
                                amount_cell($tran_unit_cost);  
                                amount_cell($row_2['cost']);  
                                
                                $item_code = strlen($stock_id) > 0 ? $stock_id : $row_2['item_description'];
                                
                                if ($error_msg === null) {
                                    $itm_row = get_boq_item_component_item_by_filters($to_project_id, $task_code, $detail_type_code, $item_type, $item_code, $tran_unit, 
                                                                                      $tran_qty, $tran_unit_cost, $item_or_cost);
                                        if (isset($itm_row['id']) && $itm_row['id'] > 0) {
                                            label_cell("Available", 'align=center'); 
                                            label_cell('');
                                        } else {
                                            label_cell("Not available", 'align=center'); 
                                            if ($copy_type == AUST_ADD_ONLY) {
                                                $_POST[$check_post_name] = 1;
                                            }
                                            check_cells("", $check_post_name, null, false, false, 'align=center');  
                                        }   
                                } else {
                                    label_cell($error_msg, 'style=color:red'); label_cell(''); 
                                }

                            end_row();
                            
                            $previous_task_id = $task_id;
                        }
                    } else {
                        if ($error_msg === null) {
                            for ($i=0; $i<10; $i++) {
                                label_cell('');
                            }
                        } else {
                            label_cell($error_msg, 'colspan=9 align=right style=color:red'); label_cell('');
                        }

                        end_row();
                    }

                $previous_task_id = $task_id;
        }
}

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

function handle_copy_project_boq_item_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;
        foreach ($boq_groups as $myrow) {
            $from_boq_group_id = $myrow["id"];
            
            $msg = copy_project_boq_item_details_to_another_project($from_boq_group_id, $from_project_id, $to_project_id);
                if ($msg["success"] !== true) {
                    return $msg;
                }
                
                $selected_recs += $msg["selected_recs"];
        }

        $msg = copy_project_boq_item_details_to_another_project(0, $from_project_id, $to_project_id);
            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 item record before copying.");
        }

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

function copy_project_boq_item_details_to_another_project($from_boq_group_id, $from_project_id, $to_project_id) {
    $selected_recs = 0;
    
    $result = get_project_boq_item_group_budget_details($from_project_id, $from_boq_group_id);
        while ($row = db_fetch($result)) {
            $task_id = $row["task_id"]; // equal to boq item component id
            $task_code = $row["task_code"]; 
            
            $result_2 = get_project_boq_item_component_items($task_id);
                while ($row_2 = db_fetch($result_2)) {
                    $id = $row_2['id'];
                    $check_post_name = "chk_".$task_id."_".$from_boq_group_id."_".$id;
            
                    if (! (isset($_POST[$check_post_name]) && $_POST[$check_post_name] == true)) {
                        continue;
                    }
                    
                    $item_or_cost = $row_2['item_or_cost'];
                    $error_line = "[$task_code] ".$row["description"]." ($task_id): $item_or_cost id #$id";
                    
                    // check to boq item is available if not error
                    $from_task_row = get_project_boq_item_record($row['from_task_group_id']);
                    $to_boq_item_row = get_project_boq_item_record_by_code($to_project_id, $from_task_row['code']);
                        if (! (isset($to_boq_item_row['id']) && $to_boq_item_row['id'] > 0)) {
                            return array('success' => false, 'message' => "From BOQ item $error_line. Could not find to project BOQ item.");
                        } else {
                            // check to boq item componet is available if not error
                            $to_cmp_row = get_boq_item_component_record($to_boq_item_row['id'], $row['detail_type_code']);
                                if (! (isset($to_cmp_row['id']) && $to_cmp_row['id'] > 0)) {
                                    return array('success' => false, 'message' => "From BOQ item $error_line. Could not find to project BOQ item component.");
                                }
                        }
                    
                    $detail_type_code = $row_2['detail_type_code']; $stock_id = $row_2['stock_id']; $to_boq_item_id = $to_boq_item_row['id'];
                    $tran_unit = $row_2['tran_unit']; $tran_qty = $row_2['tran_qty']; $tran_unit_cost = $row_2['tran_unit_cost']; $item_type = $row_2['item_type'];
                    $item_code = strlen($stock_id) > 0 ? $stock_id : $row_2['item_description'];
                    
                    // check boq componet item is already in the table then return
                    $itm_row = get_boq_item_component_item_by_filters($to_project_id, $task_code, $detail_type_code, $item_type, $item_code, $tran_unit, $tran_qty, 
                                                                      $tran_unit_cost, $item_or_cost);
                        if (isset($itm_row['id']) && $itm_row['id'] > 0) {
                            cancel_transaction();
                            return array('success' => false, 'message' => "From BOQ item $error_line. Item is already in the database.");
                        }
                
                    begin_transaction();
                    
                        if ($item_or_cost == 'Item') {
                            $msg = add_boq_item_material($to_boq_item_id, $detail_type_code, $stock_id, $tran_unit, $tran_unit_cost, $tran_qty, '', $item_type);
                                if ($msg['success'] === false) {
                                    cancel_transaction();
                                    return array('success' => false, 'message' => "From BOQ item $error_line. Could not add boq item material.");
                                }
                        } elseif ($item_or_cost == 'Cost') {
                            $msg = add_boq_item_cost($to_boq_item_id, $detail_type_code, $row_2['item_description'], $tran_unit, $tran_unit_cost, $tran_qty, $row_2['cost'], 
                                                     '');
                                if ($msg['success'] === false) {
                                    cancel_transaction();
                                    return array('success' => false, 'message' => "From BOQ item $error_line. Could not add boq item cost.");
                                }
                        } else {
                            cancel_transaction();
                            return array('success' => false, 'message' => "From BOQ item $error_line. Boq item component item shuold be either an item or a cost.");
                        }
                    
                    commit_transaction();
                    
                    $selected_recs++;
                }
        }
    
        return array("success" => true, "message" => "", "selected_recs" => $selected_recs);
}

?>