<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PROJECT_WORK_DONE';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$path_to_root = "..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");


include_once($path_to_root . "/includes/approval_functions.inc");
include_once($path_to_root . "/includes/project_cost_functions.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_groups_db.inc");
//include_once($path_to_root . "/includes/references.inc");
include_once($path_to_root . "/projects/includes/ui/work_done_entry_ui.inc");


include_once($path_to_root . "/projects/includes/db/project_boq_items_db.inc");
include_once($path_to_root . "/projects/includes/db/work_done_entry_details_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_sites_db.inc");

$js = '';

if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}

page(_($help_context = "Work Done Entry"), false, false, "", $js);

//----------------------------------------------------------------------------------------
simple_page_mode(true);


function display_work_done_filtering_controls()
{
    global $approval_statuses;

    $dec2 = 6;

    br();

//    hidden('selected_id', $selected_id);

    start_table(TABLESTYLE, "width=30%");
   
    project_list_row(_("Project:"), 'project_id', null, _('Select a project'), true);
    $project = get_project($_POST['project_id']);
    $department_name = $project['department_name'];
    $project_manger_name = $project['project_manager_name'];
    $customer_name = $project['customer_name'];

    if($_POST['project_id'] > 0) {
        label_row(_("Department:"), $department_name); 
        label_row(_('Customer:'), $customer_name);
        label_row("Project Manager:", $project_manger_name);
    }
    end_table();

    br();
    
    if($_POST['project_id'] > 0) {
        start_table(TABLESTYLE, "width=30%");
        project_boq_item_groups_list_row("BOQ Item Group :", "boq_group_id", $_POST['project_id'], null, "Select a BOQ Item Group", true);
        date_row("Entry date :", "entry_date", null, null, 0, 0, 0, null, true);
        project_sites_list_row("Job :", "project_site_id", $_POST['project_id'], null, "Select a job" , true);
        end_table();
    }
    
    

     //text_row("Reference:", 'reference', null, null, null);
    // date_row("Date:", "tran_date", $_POST['tran_date']);
       
//    hidden("ModifyMRNumber", $iou_id);
//    hidden('row_state', $_POST['row_state']);

    
}
//--------------------------------------------------------------------------------------------------

if(isset($_POST['Update_workdone_entry_details'])){
    if($_POST['project_id'] > 0){
        if(!($_POST['boq_group_id'] > 0)){
            $boq_group_id = 0;
        }
        else{
            $boq_group_id = $_POST['boq_group_id'];
        }
        
        $site_id = $_POST['project_site_id'];
        if(!($site_id > 0)){
            $site_id = 0;
        }
                
        $wde = save_work_done_entry_details($_POST['project_id'], $boq_group_id, $site_id);
        if($wde["success"] !== true){
            if(isset($wde["message"])){            
                 display_error($wde["message"]);                
            }      
        }   
    }
}

if(isset($_POST['Delete_workdone_entry_details'])){
    
    if(!($_POST['boq_group_id'] > 0)){
        $boq_group_id = 0;
    }
    else{
        $boq_group_id = $_POST['boq_group_id'];
    }

    $site_id = $_POST['project_site_id'];
    if(!($site_id > 0)){
        $site_id = 0;
    }
    
    $n = delete_work_done_entry_details($_POST['project_id'], $boq_group_id, $site_id);
    if($n == 0){
       display_error($wde["No items selected"]);    
    }
    else{
        display_note("All previous workdone entries up to ".$_POST["entry_date"]." of the selected items deleted.");     
    }    
}


if ($Mode == 'RESET')
{
	$selected_id = -1;
}

if (isset($_POST['_selected_id_update']) )
{
	$selected_id = $_POST['selected_id'];
	//$Ajax->activate('_page_body');
}

//if (list_updated('employee_code')) 
	$Ajax->activate('_page_body');
//--------------------------------------------------------------------------------------------------

if (!@$_GET['popup']){
	start_form();
}

if(!isset($_POST['amount'])){
    $_POST['amount'] = 0;
}
if(!isset($_POST['tran_date'])){
    $_POST['tran_date'] = '';
}
if(!isset($_POST['comments'])){
    $_POST['comments'] = '';
}
    
display_work_done_filtering_controls();

br();

if(isset($_POST['project_id']) && $_POST['project_id'] > 0){
    echo(get_project_trans_view_str(ST_PROJECT_WORK_DONE, $_POST['project_id'], "View Project Workdone"));
    br(2);
}


//display_work_done_boq_groups($path_to_root, $_POST['project_id']);

if($_POST['project_id'] > 0){
    display_work_done_entry_details($_POST['project_id'], $_POST['boq_group_id'], $_POST['entry_date'], $_POST['project_site_id']);

    if(isset($_POST['project_id'])){
        submit_center_first('Update_workdone_entry_details', _("Update"), '', 'default');
        submit('Delete_workdone_entry_details', 'Delete', true, '', 'default');
    }
}

if (!@$_GET['popup'])
{
	end_form();
	end_page(@$_GET['popup'], false, false);
}

?>
