<?php

$page_security = 'SA_MONTH_REPORTING_ADJUSTMENTS';
$path_to_root = "../";

include($path_to_root . "/includes/session.inc");
include($path_to_root . "/includes/ui.inc");
include($path_to_root . "/gl/includes/ui/month_reporting_adjustments_ui.inc"); 
include($path_to_root . "/gl/includes/db/month_reporting_adjustments_db.inc"); 
include($path_to_root . "/admin/db/list_values_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 = "Month Reporting Adjustments"), false, false, "", $js);

simple_page_mode(false);

if ($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM') {
    if (can_process_month_reporting_adjustment()) {
    	if ($selected_id > 0) {
            $aff_row = update_month_reporting_adjustment($selected_id, $_POST['date'], input_num('amount'), $_POST['comment']);
                if ($aff_row > 0) {
                    display_notification(_("Record #$selected_id has been updated."));
                        $Mode = 'RESET';
                } else {
                    display_error(_("Couldnot be upadeted record #$selected_id."));
                        $Mode = 'Edit';
                }
    	} else {
            $insert_no = add_month_reporting_adjustment($_POST['year_id'], $_POST['month_id'], $_POST['report_adjust_type_id'], $_POST['date'], input_num('amount'), 
                                                        $_POST['comment']);
                if ($insert_no > 0) {
                    display_notification(_("New month record adjustment has been added."));
                        $Mode = 'RESET';
                } else {
                    display_error(_("Couldnot be addes new month record adjustment."));
                }
    	}
    }
}

if ($Mode == 'Delete') {
    if (can_delete_month_reporting_adjustment($selected_id)) {
        $aff_row = delete_month_reporting_adjustment_record_by_id($selected_id);
            if ($aff_row > 0) {
                display_notification(_("Record #$selected_id has been deleted."));
            } else {
                display_error(_("Couldnot be deleted record #$selected_id."));
            }
    }
    
        $Mode = 'RESET';
}

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

    start_form();
    
        $Ajax->activate('mon_rep_ads');
        div_start('mon_rep_ads');

            display_month_reporting_adjustment_filetrs();        br(2);

            display_month_reporting_adjustment_details($_POST['year_id'], $_POST['month_id'], $_POST['report_adjust_type_id']);

            display_month_reporting_adjustment_controls($Mode, $selected_id);        br(2);
        
        div_end();

    end_form();

end_page();

?>