<?php
/**********************************************************************
***********************************************************************/
//$page_security = 'SA_PROJECT_BOQ_ITEMS';

$path_to_root = "../..";


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



function clear_boq_item_labour_data()
{
    unset($_POST['serial']);
    unset($_POST['description']);
    unset($_POST['long_description']);  
    unset($_POST['labour_type_id']);
    unset($_POST['labour_category_id']);
    unset($_POST['cost']);
    unset($_POST['current_value']);
    unset($_POST['make']);
    unset($_POST['model']);
    unset($_POST['po_number']);
    unset($_POST['useful_life']);
    unset($_POST['extended_life']);         
    unset($_POST['warranty_from']);
    unset($_POST['warranty_to']);
}

function can_commit_boq_item_component_labour($selected_id, $boq_item_id, $detail_type_code, $primary_type)
{       
    global $project_boq_item_component_primary_types;
    
    if (!($_POST['boq_labour_rate_category_id'] > 0))
    {
        display_error( _("Labour rate category can not be empty."));
        set_focus('boq_labour_rate_category_id');
        return false;
    } 
    
//    $component = get_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;
//        }
//    }
            
    if(!(check_num('boq_labour_rate',0))){
            display_error("Daily rate has to be numeric and can not be negative.");
            set_focus('boq_cost_cost');
            return false;
    }
    
    if(!(check_num('boq_labour_days',0.00001))){
            display_error("No of days has to be numeric and positive.");
            set_focus('boq_cost_cost');
            return false;
    }
    
    return true;    
}


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

function display_boq_item_labour($boq_item_id, $detail_type_code)
{
    $result = get_boq_item_labour($boq_item_id, $detail_type_code);
     
    div_start('boq_labour_details');
    
	start_table(TABLESTYLE, "width=80%");
	$th = array(_("ID"), _("Rate Category"), _("Current Rate"), 
                    _("Applied Rate"),_("Days"),"Cost", _("Comment"),'','');
	table_header($th);         
        
	$k = 0;
        $total_cost = 0;
	while ($myrow = db_fetch($result))
	{
        alt_table_row_color($k);

        label_cell($myrow['id'], "align=right");
        label_cell($myrow["rate_category_description"]);    
        label_cell(number_format($myrow['current_rate'], 2), "align=right"); 
        label_cell(number_format($myrow['rate'], 2), "align=right"); 
        label_cell(number_format($myrow['days'], 1), "align=right"); 
                 
        
        $cost = $myrow['rate'] * $myrow['days'];
        label_cell(number_format($cost, 2), "align=right");  
        label_cell($myrow["comment"]); 

        $total_cost += $cost; 

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

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

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

function display_boq_labour_edit_controls($boq_item_id, $detail_type_code, $selected_id, $Mode)
{  
    div_start('boq_labour_edit_controls');
    start_table(TABLESTYLE2);
    
    if ($selected_id > 0)
	{
        $myrow = get_boq_item_labour_record($selected_id);

        $_POST['boq_labour_rate_category_id'] = $myrow["rate_category_id"];  
        //$_POST['description'] = $myrow["description"];
        $_POST['boq_labour_rate'] = $myrow["rate"];
        $_POST['boq_labour_days'] = $myrow["days"];
        $_POST['boq_labour_comment'] = $myrow["comment"];                         

        hidden('selected_id', $selected_id);
    }

  
    rate_category_list_row("Rate Category :", 'boq_labour_rate_category_id', null, 'Select a labour rate');     
    amount_row(_("Daily Rate :"), 'boq_labour_rate', null, null, null, 2);                       
    amount_row(_("No Of Days :"), 'boq_labour_days', null, null, null, 1);    
    textarea_row("Comment :", "boq_labour_comment", null, 35, 2);   
                      
    end_table(1);
    div_end();

    if($selected_id > 0){        
        submit_center_first('UPDATE_BOQ_ITEM_LABOUR', _("Update"), '', 'default');
        submit_center_last('Cancel_Edit', _("Cancel"), '', 'default');
    }
    else{
        submit_center_first('ADD_BOQ_ITEM_LABOUR', _("Add"), '', 'default');
    }
}    


if(isset($_POST['ADD_BOQ_ITEM_LABOUR']) && $selected_id < 0)
{    
    if(can_commit_boq_item_component_labour($selected_id, get_post('boq_item_id'), get_post('detail_type_code'), get_post('primary_type'))){
        
        begin_transaction();
        
        $sbp_msg = add_boq_item_labour(get_post('boq_item_id'), get_post('detail_type_code'), 
                            get_post('boq_labour_rate_category_id'), input_num('boq_labour_rate'), 
                            input_num('boq_labour_days'), get_post('boq_labour_comment')); 
        
        if($sbp_msg['success'] === true){
            commit_transaction();
            $Mode = 'RESET';
            $selected_id = -1;
            
            display_notification("A new BOQ Item Labour detail has been added");
        }
        else{
            cancel_transaction();
        }           
    }               
}


if(isset($_POST['UPDATE_BOQ_ITEM_LABOUR']) && $selected_id > 0)
{    
    if(can_commit_boq_item_component_labour($selected_id, get_post('boq_item_id'), get_post('detail_type_code'), get_post('primary_type'))){       

        begin_transaction();
        
        $sbp_msg = update_boq_item_labour($selected_id, get_post('boq_item_id'), get_post('detail_type_code'),
                                get_post('boq_labour_rate_category_id'), input_num('boq_labour_rate'), 
                                input_num('boq_labour_days'), get_post('boq_labour_comment')); 
        
        if($sbp_msg['success'] === true){
            commit_transaction();
            $Mode = 'RESET';
            $selected_id = -1;
            
            display_notification("Selected BOQ Item Labour detail has been updated");
        }
        else{
            cancel_transaction();
        }                     
    }
}

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

$delete_boq_labour_id  = find_submit("Delete_BOQ_Item_Labour");
if ($delete_boq_labour_id > 0)
{
    begin_transaction();
    
    $sbp_msg = delete_boq_item_labour($delete_boq_labour_id, get_post('boq_item_id'), get_post('detail_type_code'));
    if($sbp_msg['success'] === true){
        commit_transaction();
        $Mode = 'RESET';
        $selected_id = -1;

        display_notification(_('Selected BOQ Item Labour detail has been deleted'));
    }
    else{
        cancel_transaction();
    }
	
	
}

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


if (get_post('cancel')) {
	
    clear_boq_item_labour_data();
    set_focus('serial');
    $Ajax->activate('_page_body');
}


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

    start_form();
    
    display_boq_item_labour($boq_item_id, $detail_type_code);

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


    end_form();

}


?>

