kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
317 lines
8.5 KiB
317 lines
8.5 KiB
11 years ago
|
#-- copyright
|
||
|
# OpenProject Reporting Plugin
|
||
|
#
|
||
|
# Copyright (C) 2010 - 2014 the OpenProject Foundation (OPF)
|
||
|
#
|
||
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License
|
||
|
# version 3.
|
||
|
#
|
||
|
# This program is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
|
# along with this program; if not, write to the Free Software
|
||
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
#++
|
||
|
|
||
15 years ago
|
class CostReportsController < ApplicationController
|
||
7 years ago
|
module QueryPreperation
|
||
|
##
|
||
|
# Make sure to add cost type filter after the
|
||
|
# query filters have been reset by .prepare_query
|
||
|
# from the Report::Controller.
|
||
|
def prepare_query
|
||
|
query = super
|
||
|
|
||
|
set_cost_type if @unit_id.present?
|
||
|
|
||
|
query
|
||
|
end
|
||
|
end
|
||
|
|
||
14 years ago
|
rescue_from Exception do |exception|
|
||
14 years ago
|
session.delete(CostQuery.name.underscore.to_sym)
|
||
13 years ago
|
raise exception
|
||
|
end
|
||
14 years ago
|
|
||
10 years ago
|
rescue_from ActiveRecord::RecordNotFound do |_exception|
|
||
13 years ago
|
render_404
|
||
14 years ago
|
end
|
||
|
|
||
14 years ago
|
Widget::Base.dont_cache!
|
||
14 years ago
|
|
||
7 years ago
|
before_action :check_cache
|
||
|
before_action :load_all
|
||
|
before_action :find_optional_project
|
||
|
before_action :find_optional_user
|
||
7 years ago
|
|
||
14 years ago
|
include Report::Controller
|
||
6 years ago
|
include Concerns::Layout
|
||
7 years ago
|
prepend QueryPreperation
|
||
|
|
||
7 years ago
|
before_action :set_cost_types # has to be set AFTER the Report::Controller filters run
|
||
15 years ago
|
|
||
9 years ago
|
verify method: :delete, only: %w[destroy]
|
||
|
verify method: :post, only: %w[create, update, rename]
|
||
13 years ago
|
|
||
14 years ago
|
helper_method :cost_types
|
||
14 years ago
|
helper_method :cost_type
|
||
14 years ago
|
helper_method :unit_id
|
||
14 years ago
|
helper_method :public_queries
|
||
|
helper_method :private_queries
|
||
|
|
||
14 years ago
|
attr_accessor :cost_types, :unit_id, :cost_type
|
||
14 years ago
|
|
||
|
# Checks if custom fields have been updated, added or removed since we
|
||
|
# last saw them, to rebuild the filters and group bys.
|
||
|
# Called once per request.
|
||
9 years ago
|
def check_cache
|
||
|
CostQuery::Cache.check
|
||
14 years ago
|
end
|
||
|
|
||
14 years ago
|
##
|
||
|
# @Override
|
||
|
# Use respond_to hook, so redmine_export can hook up the excel exporting
|
||
|
def index
|
||
|
super
|
||
|
respond_to do |format|
|
||
|
format.html {
|
||
|
session[report_engine.name.underscore.to_sym].try(:delete, :name)
|
||
6 years ago
|
render action: 'index', layout: layout_non_or_no_menu
|
||
14 years ago
|
}
|
||
|
end unless performed?
|
||
|
end
|
||
|
|
||
8 years ago
|
current_menu_item :index do |controller|
|
||
8 years ago
|
controller.menu_item_to_highlight_on_index
|
||
8 years ago
|
end
|
||
|
|
||
8 years ago
|
def menu_item_to_highlight_on_index
|
||
|
@project ? :cost_reports : :cost_reports_global
|
||
9 years ago
|
end
|
||
|
|
||
14 years ago
|
def drill_down
|
||
10 years ago
|
redirect_to action: :index
|
||
14 years ago
|
end
|
||
|
|
||
14 years ago
|
##
|
||
|
# Determines if the request sets a unit type
|
||
|
def set_unit?
|
||
|
params[:unit]
|
||
|
end
|
||
|
|
||
14 years ago
|
##
|
||
|
# @Override
|
||
|
# We cannot show a progressbar in Redmine, due to Prototype being less than 1.7
|
||
|
def no_progress?
|
||
|
true
|
||
|
end
|
||
|
|
||
15 years ago
|
##
|
||
|
# Set a default query to cut down initial load time
|
||
15 years ago
|
def default_filter_parameters
|
||
7 years ago
|
{
|
||
7 years ago
|
operators: { spent_on: '>d' },
|
||
|
values: { spent_on: [30.days.ago.strftime('%Y-%m-%d')] }
|
||
15 years ago
|
}.tap do |hash|
|
||
|
if @project
|
||
7 years ago
|
set_project_filter(hash, @project.id)
|
||
15 years ago
|
end
|
||
7 years ago
|
|
||
|
if current_user.logged?
|
||
|
set_me_filter(hash)
|
||
|
end
|
||
15 years ago
|
end
|
||
|
end
|
||
15 years ago
|
|
||
7 years ago
|
##
|
||
|
# Get the filter params with an optional project context
|
||
|
def filter_params
|
||
|
filters = super
|
||
|
update_project_context!(filters)
|
||
|
|
||
|
filters
|
||
|
end
|
||
|
|
||
|
##
|
||
|
# Clear the query if the project context changed
|
||
|
def update_project_context!(filters)
|
||
|
|
||
|
# Only in project context
|
||
|
return unless @project
|
||
|
|
||
|
# Only if the project context changed
|
||
|
context = filters[:project_context]
|
||
|
|
||
|
# Context is same, don't set project (allow override)
|
||
|
return if context == @project.id
|
||
|
|
||
|
# Reset context if project missing
|
||
|
if context.nil?
|
||
|
filters[:project_context] = @project.id
|
||
|
return
|
||
|
end
|
||
|
|
||
|
# Update the project context and project_id filter
|
||
|
set_project_filter(filters, @project.id)
|
||
|
end
|
||
|
|
||
|
def set_project_filter(filters, project_id)
|
||
|
filters[:project_context] = project_id
|
||
|
filters[:operators].merge! project_id: '='
|
||
|
filters[:values].merge! project_id: [project_id]
|
||
|
end
|
||
|
|
||
7 years ago
|
def set_me_filter(filters)
|
||
|
filters[:operators].merge! user_id: '='
|
||
|
filters[:values].merge! user_id: [CostQuery::Filter::UserId.me_value]
|
||
|
end
|
||
|
|
||
15 years ago
|
##
|
||
|
# Set a default query to cut down initial load time
|
||
15 years ago
|
def default_group_parameters
|
||
10 years ago
|
{ columns: [:week], rows: [] }.tap do |h|
|
||
15 years ago
|
if @project
|
||
12 years ago
|
h[:rows] << :work_package_id
|
||
15 years ago
|
else
|
||
|
h[:rows] << :project_id
|
||
|
end
|
||
15 years ago
|
end
|
||
15 years ago
|
end
|
||
|
|
||
14 years ago
|
##
|
||
14 years ago
|
# Determine active cost types, the currently selected unit and corresponding cost type
|
||
|
def set_cost_types
|
||
|
set_active_cost_types
|
||
14 years ago
|
set_unit
|
||
|
set_cost_type
|
||
|
end
|
||
|
|
||
14 years ago
|
# Determine the currently active unit from the parameters or session
|
||
|
# sets the @unit_id -> this is used in the index for determining the active unit tab
|
||
14 years ago
|
def set_unit
|
||
13 years ago
|
@unit_id = if set_unit?
|
||
10 years ago
|
params[:unit].to_i
|
||
|
elsif @query.present?
|
||
|
cost_type_filter = @query.filters.detect { |f| f.is_a?(CostQuery::Filter::CostTypeId) }
|
||
13 years ago
|
|
||
10 years ago
|
cost_type_filter.values.first.to_i if cost_type_filter
|
||
13 years ago
|
end
|
||
|
|
||
9 years ago
|
@unit_id = -1 unless @cost_types.include? @unit_id
|
||
14 years ago
|
end
|
||
|
|
||
14 years ago
|
# Determine the active cost type, if it is not labor or money, and add a hidden filter to the query
|
||
|
# sets the @cost_type -> this is used to select the proper units for display
|
||
14 years ago
|
def set_cost_type
|
||
7 years ago
|
return unless @query
|
||
|
|
||
|
@query.filter :cost_type_id, operator: '=', value: @unit_id.to_s, display: false
|
||
|
@cost_type = CostType.find(@unit_id) if @unit_id > 0
|
||
14 years ago
|
end
|
||
|
|
||
14 years ago
|
# set the @cost_types -> this is used to determine which tabs to display
|
||
|
def set_active_cost_types
|
||
14 years ago
|
unless session[:report] && (@cost_types = session[:report][:filters][:values][:cost_type_id].try(:collect, &:to_i))
|
||
9 years ago
|
relevant_cost_types = CostType.select(:id).order('id ASC').select do |t|
|
||
14 years ago
|
t.cost_entries.count > 0
|
||
|
end.collect(&:id)
|
||
|
@cost_types = [-1, 0, *relevant_cost_types]
|
||
14 years ago
|
end
|
||
14 years ago
|
end
|
||
|
|
||
14 years ago
|
def load_all
|
||
|
CostQuery::GroupBy.all
|
||
|
CostQuery::Filter.all
|
||
|
end
|
||
|
|
||
14 years ago
|
# @Override
|
||
|
def determine_engine
|
||
|
@report_engine = CostQuery
|
||
|
@title = "label_#{@report_engine.name.underscore}"
|
||
|
end
|
||
|
|
||
13 years ago
|
# N.B.: Users with save_cost_reports permission implicitly have
|
||
|
# save_private_cost_reports permission as well
|
||
|
#
|
||
14 years ago
|
# @Override
|
||
13 years ago
|
def allowed_to?(action, report, user = User.current)
|
||
|
# admins may do everything
|
||
|
return true if user.admin?
|
||
|
|
||
|
# If this report does belong to a project but not to the current project, we
|
||
|
# should not do anything with it. It fact, this should never happen.
|
||
|
return false if report.project.present? && report.project != @project
|
||
|
|
||
|
# If report does not belong to a project, it is ok to look for the
|
||
|
# permission in any project. Otherwise, the user should have the permission
|
||
|
# in this project.
|
||
|
if report.project.present?
|
||
|
options = {}
|
||
|
else
|
||
10 years ago
|
options = { global: true }
|
||
13 years ago
|
end
|
||
|
|
||
|
case action
|
||
|
when :create
|
||
|
user.allowed_to?(:save_cost_reports, @project, options) or
|
||
|
user.allowed_to?(:save_private_cost_reports, @project, options)
|
||
|
|
||
8 years ago
|
when :save, :destroy, :rename
|
||
13 years ago
|
if report.is_public?
|
||
|
user.allowed_to?(:save_cost_reports, @project, options)
|
||
|
else
|
||
|
user.allowed_to?(:save_cost_reports, @project, options) or
|
||
|
user.allowed_to?(:save_private_cost_reports, @project, options)
|
||
|
end
|
||
|
|
||
|
when :save_as_public
|
||
|
user.allowed_to?(:save_cost_reports, @project, options)
|
||
|
|
||
|
else
|
||
|
false
|
||
|
end
|
||
14 years ago
|
end
|
||
|
|
||
14 years ago
|
def public_queries
|
||
13 years ago
|
if @project
|
||
9 years ago
|
CostQuery.where(['is_public = ? AND (project_id IS NULL OR project_id = ?)', true, @project])
|
||
|
.order('name ASC')
|
||
13 years ago
|
else
|
||
9 years ago
|
CostQuery.where(['is_public = ? AND project_id IS NULL', true])
|
||
|
.order('name ASC')
|
||
13 years ago
|
end
|
||
14 years ago
|
end
|
||
|
|
||
|
def private_queries
|
||
13 years ago
|
if @project
|
||
9 years ago
|
CostQuery.where(['user_id = ? AND is_public = ? AND (project_id IS NULL OR project_id = ?)',
|
||
|
current_user,
|
||
|
false,
|
||
|
@project])
|
||
|
.order('name ASC')
|
||
13 years ago
|
else
|
||
9 years ago
|
CostQuery.where(['user_id = ? AND is_public = ? AND project_id IS NULL', current_user, false])
|
||
|
.order('name ASC')
|
||
13 years ago
|
end
|
||
14 years ago
|
end
|
||
|
|
||
13 years ago
|
def display_report_list
|
||
|
report_type = params[:report_type] || :public
|
||
10 years ago
|
render partial: 'report_list', locals: { report_type: report_type }, layout: !request.xhr?
|
||
13 years ago
|
end
|
||
|
|
||
15 years ago
|
private
|
||
10 years ago
|
|
||
14 years ago
|
def find_optional_user
|
||
|
@current_user = User.current || User.anonymous
|
||
|
end
|
||
13 years ago
|
|
||
|
def default_breadcrumb
|
||
|
l(:cost_reports_title)
|
||
|
end
|
||
15 years ago
|
end
|