<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PROJECT_FLOATS';
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 . "/petty_cash/includes/db/project_floats_db.inc");
include_once($path_to_root . "/includes/ui/ui_lists_group_companies.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/includes/common_functions.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 = "Project Floats"));
//------------------------------------------------------------------------------------------
simple_page_mode(true);

//if (isset($_GET['ModifyMRNumber']))
//{
//	  $_POST['ModifyMRNumber'] = $_GET['ModifyMRNumber'];
//}

function can_commit($selected_id)
{	
    if(!($_POST['project_id'] > 0))
    {
        display_error( _("Please select a project."));
        set_focus('project_id');
        return false;
    }

    if(!(isset($_POST['employee_code']) && strlen($_POST['employee_code']) > 1))
    {
        display_error( _("Please select an employee."));
        set_focus('employee_code');
        return false;
    }

    if (!check_num('amount',0))
    {
        display_error( _("Amount entered is not numeric or negative."));
        set_focus('amount');
        return false;
    }

    if(isset($_POST['Add'])){
        if (check_project_float_exists($_POST['project_id']))
        {
            display_error( _("There is an existing Project Float the selected project."));
            set_focus('project_id');
            return false;
        }
    }
    
    return true;    
}




function clear_data()
{
    unset($_POST['id']);
    unset($_POST['name']);
    unset($_POST['amount']);
    unset($_POST['account_code']);
}


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

if(isset($_POST['Add'])){
    
    if(can_commit(-1)){    

        add_project_float($_POST['project_id'], $_POST['group_company_code'], 
                            $_POST['employee_code'], input_num('amount'), 
                            $_POST['comment'], $_POST['inactive']);
                
        $Mode = 'RESET';
        display_notification("New project float has been added");
        clear_data();
    }
}    
 
if(isset($_POST['Update'])){
    if(can_commit($selected_id)){ 
        update_project_float($selected_id,$_POST['group_company_code'], 
                            $_POST['employee_code'], input_num('amount'), 
                            $_POST['comment'], $_POST['inactive']);
        $Mode = 'RESET';
        display_notification("Project float has been updated");
        clear_data();
    }
 }


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

//if (isset($_POST['Close_VR'])){
//    close_vr($selected_id);
//} 

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

//if ($Mode == 'Delete')
//{
//        $recs = delete_project_float($selected_id);
//        
//        if($recs == 1){
//                display_notification(_("Petty Cash Float has been deleted."));
//        }
//        else{
//                display_error(_("Vehicle Request could not be deleted."));
//        }
//	$Mode = 'RESET';
//}

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

//if (isset($_POST['Update']) )
//{
//	$selected_id = get_post('selected_id');
//}

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

//if (!@$_GET['popup'])	
    
$result = get_project_floats();   

start_form();
start_table(TABLESTYLE, "width=80%");
$th = array(_('Project'),_('Company'), "Owner", "Amount", "Balance", "Comment", "Inactive", "");

table_header($th);
$k = 0; //row colour counter

while ($myrow = db_fetch($result))
{
    alt_table_row_color($k);
    
    label_cell($myrow["project_description"]);
    label_cell($myrow["group_company_name"]);
    $en = get_group_company_employee_formatted_long_name($myrow["company_code"], $myrow["employee_code"]);
    label_cell($en);
    amount_decimal_cell($myrow["amount"]);
    
    $balance = get_project_float_balance($myrow["project_id"]);
    amount_decimal_cell($balance);
    label_cell($myrow["comment"]);
    label_cell(get_yes_no_name($myrow["inactive"]));
    
    edit_button_cell("Edit".$myrow["id"], _("Edit"));    
//    delete_button_cell("Delete".$myrow["id"], _("Delete"));
    
    end_row();
}
//inactive_control_row($th);
end_table(1);
//----------------------------------------------------------------------------------------

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

    

if ($Mode =='Edit')   
{
    
}

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

start_table(TABLESTYLE2);

    $myrow = get_project_float($selected_id);
    
    if(isset($myrow['id']) && $myrow['id'] > 0){
        $_POST['id'] = $myrow["id"];
        $_POST['project_id'] = $myrow["project_id"];
        $_POST['project_description'] = $myrow['project_description'];
        
        if(!(isset($_POST['group_company_code']) && strlen($_POST['group_company_code']) > 0)){
            $_POST['group_company_code'] = $myrow["company_code"];
        }
        $_POST['employee_code'] = $myrow["employee_code"];
        $_POST['amount'] = $myrow["amount"];
        $_POST['comment'] = $myrow["comment"];
        $_POST['inactive'] = $myrow["inactive"];
        
        hidden('selected_id', $_POST['id'] );
        
        label_row("Project :", $_POST['project_description']);
        hidden('project_id', $_POST['project_id'] );
        
    }
    else{
        hidden('selected_id', -1);
        project_list_row("Project :", 'project_id', null, "Select a project", true);
    }
    
    group_companies_list_row("Company:", 'group_company_code', null, false, true);
    if(list_updated('group_company_code')){
        $_POST['employee_code'] = 0;
    }
    group_company_employees_simple_list_row("Employee :", 'employee_code', $_POST['group_company_code'], null, "Select an employee");
    amount_row('Amount:', 'amount', null, null, null, 2);
    textarea_row("Comment:", 'comment', null, 35, 3);
    yesno_list_row("Inactive :", 'inactive');

end_table(1);


if($selected_id > 0){
    submit_center_first('Update', _("Update"), '', 'default');
    submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');

}
else{
        
    submit_center_first('Add', _("Add"), '', 'default');
    submit_center_last('cancel', _("Cancel"), _("Cancel"), 'cancel');
}


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

?>
