<?php

function display_month_reporting_adjustment_filetrs() {
    start_table(TABLESTYLE);
        $years_array = get_years_array();
            custom_values_list_row('Year:', 'year_id', $years_array, null, 'Select an year', true);
        $months_array = get_months_array();
            custom_values_list_row('Month:', 'month_id', $months_array, null, 'Select a month', true);
        value_list_row('Report Adjust Type:', 'report_adjust_type_id', 'MONTH_REP_ADJ_TYPES', null, 'Select a reporting adjustment type', true);
    end_table(1);
    
    start_row();
        submit_cells('Refresh', _("Refresh"), '', _('Refresh'), false, ICON_SUBMIT);
    end_row();
}
        
function get_years_array() {
    $years_array = array();
            
        $result = get_all_list_values('YEAR');
            while ($row = db_fetch($result)) {
                $years_array[$row['value_name']] = $row['value_name'];
            }
            
            krsort($years_array);
            
        return $years_array;
}

function get_months_array() {
    $months_array = array();
            
        $result = get_all_list_values('MONTHS');
            while ($row = db_fetch($result)) {
                $months_array[$row['value_code']] = $row['value_name'];
            }
            
            ksort($months_array);
            
        return $months_array;
}

function display_month_reporting_adjustment_details($year_id, $month_id, $report_adjust_type_id) {
    start_table(TABLESTYLE, "width=75%");

        $th = array(_("Id"), _("Date"), _("Amount"), _("Comment"), _(""), _(""));
            table_header($th);

                $k = 0;
                $result = get_month_reporting_adjustments_details($year_id, $month_id, $report_adjust_type_id);
                    while ($row = db_fetch($result)) {
                        alt_table_row_color($k);
                            label_cell($row["id"], 'align=center');
                            label_cell(sql2date($row["date"]), 'align=center');
                            label_cell(number_format($row["amount"], 2), 'align=right');
                            label_cell(nl2br($row["comment"]));
                            
                            edit_button_cell("Edit".$row["id"], _("Edit"));
                            delete_button_cell("Delete".$row["id"], _("Delete"));
                        end_row();
                    }
                    
    end_table(3);
}

function display_month_reporting_adjustment_controls(&$Mode, $selected_id) {
    if ($selected_id > 0 && $Mode != 'Delete') {
        $myrow = get_month_reporting_adjustment_record_by_id($selected_id);
            $_POST['date'] = sql2date($myrow["date"]);
            $_POST['amount'] = $myrow["amount"];
            $_POST['comment'] = $myrow["comment"];

            hidden('selected_id', $selected_id);
            
            $Mode = 'Edit';
    }
    
        start_table(TABLESTYLE2);
            if ($selected_id > 0 && $Mode == 'Edit') {
                label_row('Id:', $selected_id);
            }

            date_row('Date:', 'date');
            amount_row('Amount:', 'amount', null, null, null, 2);
            textarea_row(_("Comment:"), 'comment', null, 50, 3);

        end_table(1);

        if ($Mode == 'RESET') {
            submit_add_or_update_center(true, '', 'both');
        } else {
            submit_add_or_update_center($selected_id == "", '', 'both');
        }
}

function can_process_month_reporting_adjustment() {
    if (! ($_POST['year_id'] > 0)) {
        display_error( _("Please select an year."));
            set_focus('year_id');
                return false;
    }
    
    if (! ($_POST['month_id'] > 0)) {
        display_error( _("Please select a month."));
            set_focus('month_id');
                return false;
    }
        
    if (! ($_POST['report_adjust_type_id'] > 0)) {
        display_error( _("Please select a report adjust type."));
            set_focus('report_adjust_type_id');
                return false;
    }
    
    if (! is_date($_POST['date'])) {
        display_error( _("Entered date is not a valid date."));
            set_focus('date');
                return false;
    }
    
        $month_start = $_POST['year_id'].'-'.$_POST['month_id'].'-01';
        $month_end = date("Y-m-t", strtotime($month_start));
        $date = date2sql($_POST['date']);
        
            if (! ($date >= $month_start && $date <= $month_end)) {
                display_error( _("Date should be within the selected month."));
                    set_focus('date');
                        return false;
            }
        
    if (! is_numeric(input_num('amount'))) {
        display_error( _("Entered amount should be numeric."));
            set_focus('amount');
                return false;
    }
        
	return true;
}

function can_delete_month_reporting_adjustment($selected_id) {

	return true;
}

function clear_month_reporting_adjustment_controls() {
    unset($_POST['date']);
    unset($_POST['amount']);
    unset($_POST['comment']); 
}

?>