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

$page_security = 'SA_PROJECT_BOQ_ITEMS';

$path_to_root = "../..";

//include_once($path_to_root . "/includes/ui.inc");
//include_once($path_to_root . "/payroll/includes/db/nopay_references_db.inc");
//include_once($path_to_root . "/includes/ui/ui_input.inc");
//include_once($path_to_root . "/projects/includes/db/project_boq_items_db.inc");
//include_once($path_to_root . "/includes/session.inc");
//
//simple_page_mode(true);
//
//if(!isset($selected_id)){
//    global $selected_id, $Mode;
//}
//
////------------------------------------------------------------------------------------------
//
//if (isset($_GET['boq_item_id']) && $_GET['boq_item_id'] > 0) 
//{
//    $_POST['boq_item_id'] = $_GET['boq_item_id'];
//}





function display_est_boq_item_costs($boq_item_id, $detail_type_code)
{
    global $Ajax;
    
    div_start('boq_cost_details');
    
    $result = get_est_boq_item_costs($boq_item_id, $detail_type_code);
//    div_start('bom');
	start_table(TABLESTYLE, "width=80%");
	$th = array(_("ID"), _("Description"), _("Cost"), _("Comment"), '','');
	table_header($th);        
        
	$k = 0;
        $total = 0;
	while ($myrow = db_fetch($result))
	{
            alt_table_row_color($k);

            label_cell($myrow['id'], "align=right");
            label_cell($myrow["description"]);
            label_cell(number_format($myrow["cost"], 2), "align=right");
            label_cell($myrow["comment"]);

            $total += $myrow["cost"];

            edit_button_cell("Edit".$myrow['id'], _("Edit"));         
            delete_button_cell("Delete_BOQ_Item_Cost".$myrow['id'], _("Delete"));
        end_row();

	} //END WHILE LIST LOOP
        
        start_row();
            label_cell("Total :", "colspan=2 align=right");
            amount_decimal_cell($total);
        end_row();


    end_table();
    div_end();
}

//--------------------------------------------------------------------------------------------------

function display_est_boq_cost_edit_controls($boq_item_id, $detail_type_code, $selected_id, &$Mode)
{
    global $Ajax;
    
    div_start('boq_cost_edit_controls');
    start_table(TABLESTYLE2);
    
    if ($selected_id > 0)
	{
            if ($Mode == 'Edit') {
                $myrow = get_est_boq_cost_detail($selected_id);

                $_POST['boq_cost_description'] = $myrow["description"];
                $_POST['boq_cost_cost'] = $myrow["cost"]; // by Tom Moulton
                $_POST['boq_cost_comment'] = $myrow["comment"];
  
            }
            hidden('selected_id', $selected_id);
	}
	
    br(2);
    
   // bom_processes_list_row('Process', 'bom_labour_process_id', $parent);

   text_row("Description :", 'boq_cost_description', null, 50, 200); 
   amount_row("Cost :", 'boq_cost_cost');
//   text_row("Comment :", 'boq_cost_comment', null, 50, 200); 
   textarea_row(_("Comment :"), 'boq_cost_comment', null, 31, 3);
     


    end_table(1);
    div_end();
    
         
    
    if($selected_id > 0){
        
        submit_center_first('UPDATE_BOQ_ITEM_COST', _("Update"), '', 'default');
        submit_center_last('Cancel_Edit', _("Cancel"), '', 'default');
    }
    else{
        submit_center_first('ADD_BOQ_ITEM_COST', _("Add"), '', 'default');
    }
    
//      submit_add_or_update_center($selected_id == -1, '', 'both');
 
    
    }

//--------------------------------------------------------------------------------------------------

function can_commit($selected_id, $boq_item_id, $detail_type_code, $primary_type)
{
    global $project_boq_item_component_primary_types;
    
    $est_boq_item = get_est_project_boq_item_record($boq_item_id);
    $project = get_est_project_record($est_boq_item['est_project_id']);
    if(!($project['approval_status'] == AS_NEW || $project['approval_status'] == AS_WITH_THE_APPROVER || 
            $project['approval_status'] == AS_AWAITING_APPROVAL || $project['approval_status'] == AS_BACK_TO_ORIGINATOR || 
            $project['approval_status'] == AS_REOPENED)){
        display_error("This project status does not allow further updating.");
        return false;
    }
    
    $component = get_est_boq_item_component_record($boq_item_id, $detail_type_code);
    if(isset($component['id']) && $component['id'] > 0){
        if($component['primary_type'] != $primary_type){
            display_error("There are already created ".$project_boq_item_component_primary_types[$component['primary_type']]." entries for this BOQ component. 
                            Please delete those before creating entries for new primary type.");
            set_focus('primary_type');
            return false;
        }
    }
    
    return true;
}

//--------------------------------------------------------------------------------------------------



//--------------------------------------------------------------------------------------------------
 
if(isset($_POST['ADD_BOQ_ITEM_COST']) && $selected_id < 0)
{
    if(can_commit($selected_id, get_post('boq_item_id'), get_post('detail_type_code'), get_post('primary_type'))){
        
        begin_transaction();
        
        $sbp_msg = save_est_boq_item_cost($selected_id, get_post('boq_item_id'), get_post('detail_type_code'), 
                                          get_post('boq_cost_description'), input_num('boq_cost_cost'), get_post('boq_cost_comment'));   
        if($sbp_msg['success'] === true){
            commit_transaction();
            $Mode = '';
            $selected_id = -1;
            
            display_notification("A new Estimation BOQ Item Cost has been added");
            
        }
        else{
            cancel_transaction();
        }
    }
}

if(isset($_POST['UPDATE_BOQ_ITEM_COST']) && $selected_id > 0)
{
    if(can_commit($selected_id, get_post('boq_item_id'), get_post('detail_type_code'), get_post('primary_type'))){
        
        begin_transaction();
        
        $sbp_msg = save_est_boq_item_cost($selected_id, get_post('boq_item_id'), get_post('detail_type_code'), 
                                          get_post('boq_cost_description'), input_num('boq_cost_cost'), get_post('boq_cost_comment'));   
        if($sbp_msg['success'] === true){
            commit_transaction();
            $Mode = '';
            $selected_id = -1;
            
            display_notification("Selected Estimation BOQ Item Cost has been updated");
        }
        else{
            cancel_transaction();
        }
    }
}

if(isset($_POST['Cancel_Edit']))
{
        $Mode = 'RESET';
        $selected_id = -1;
}

//--------------------------------------------------------------------------------------------------


$delete_boq_cost_id  = find_submit("Delete_BOQ_Item_Cost");
if ($delete_boq_cost_id > 0)
{
    begin_transaction();
    $sbp_msg = delete_est_boq_item_cost($delete_boq_cost_id, get_post('boq_item_id'), get_post('detail_type_code'));
    if($sbp_msg['success'] === true){
        commit_transaction();
        $Mode = '';
        $selected_id = -1;

        display_notification(_('Selected Estimation BOQ Item Cost has been deleted'));
    }
    else{
        cancel_transaction();
    }
    
    
	
	$Mode = 'RESET';
}


if ($Mode == 'RESET')
{
    $selected_id = -1;
//    $sav = get_post('show_inactive');
//    unset($_POST);
//    $_POST['show_inactive'] = $sav;
}

//-------------------------------------------------------------------------------------------------

function display_est_project_boq_costs_page(&$selected_id, &$Mode, $boq_item_id, $detail_type_code)
{

    global $Ajax;

    start_form();
    
    $Ajax->Activate('boq_cost_details'); 
    
    display_est_boq_item_costs($boq_item_id, $detail_type_code);


    $Ajax->Activate('boq_cost_edit_controls'); 

    display_est_boq_cost_edit_controls($boq_item_id, $detail_type_code, $selected_id, $Mode); 

    end_form();
}


?>



















