service_packs_controller.rb 3.76 KB
Newer Older
1
class ServicePacksController < ApplicationController
Khang Le's avatar
Khang Le committed
2
3
4
5
6
7
8
9
10
11
12
13
  # only allow admin
  before_action :require_admin

  # Specifying Layouts for Controllers, looking at OPENPROJECT_ROOT/app/views/layouts/admin
  layout 'admin'

  def index
    @service_packs = ServicePack.all
  end

  def new
    @service_pack = ServicePack.new
14
15
16
    # TimeEntryActivity.shared.count.times {@service_pack.mapping_rates.build}
    @sh = TimeEntryActivity.shared
    @c = TimeEntryActivity.shared.count
Khang Le's avatar
Khang Le committed
17
18
19
20
  end

  def show
    @service_pack = ServicePack.find(params[:id])
21
22
    # controller chooses not to get the thresholds.
    # assume the service pack exists.
23
    # TODO: make a separate action JSON only.
24
    respond_to do |format|
25
26
27
28
      format.json {
        render plain: ServicePackPresenter.new(@service_pack).json_export(:rate)
      }
      format.html {
29
        @rates = @service_pack.mapping_rates
30
31
        @assignments = @service_pack.assigns.where(assigned: true).all
      }
32
    end
Khang Le's avatar
Khang Le committed
33
34
35
  end

  def create
36
37
38
39
40
41
42
    # @service_pack = ServicePack.new(service_pack_params)
    # if @service_pack.save
    #   redirect_to @service_pack
    # else
    #   render 'new'
    # end
    mapping_rate_attribute = params['service_pack']['mapping_rates_attributes']
43
    # binding.pry
44
45
    activity_id = []
    mapping_rate_attribute.each {|_index, hash_value| activity_id.push(hash_value['activity_id'])}
46

47
    if activity_id.uniq.length == activity_id.length
48
      @service_pack = ServicePack.new(service_pack_params)
49
50
51
52
53
      # render plain: 'not duplicated'
      if @service_pack.save
        flash[:notice] = 'Service Pack creation successful.'
        redirect_to action: :show, id: @service_pack.id and return
      else
Tam Le's avatar
Tam Le committed
54
        flash.now[:error] = 'Service Pack creation failed.'
55
56
57
        @service_pack = ServicePack.new
        render 'new'
      end
Khang Le's avatar
Khang Le committed
58
    else
59
      # render plain: 'duplicated'
Tam Le's avatar
Tam Le committed
60
      flash.now[:error] = 'Only one rate can be defined to one activity.'
61
62
      @service_pack = ServicePack.new
      render 'new'
Khang Le's avatar
Khang Le committed
63
64
65
    end
  end

Kiet's avatar
Kiet committed
66
67
68
69
70
  def edit
    @sp = ServicePack.find(params[:id])
    @activity = @sp.time_entry_activities.build
  end

Kiet's avatar
Kiet committed
71
72
73
74
75
76
77
  def destroy
    @sp = ServicePack.find(params[:id])
    @sp.destroy

    redirect_to service_packs_path
  end

Tam Le's avatar
Tam Le committed
78
  def statistics
79
80
81
82
83
84
    start_day = params[:start_period]&.to_date # ruby >= 2.3.0
    end_day = params[:end_period]&.to_date
    if start_day.nil? ^ end_day.nil?
      render json: { error: 'GET OUT!'}, status: 400 and return
    end
    # binding.pry
85
    # never say something = NULL in SQL.
Tam Le's avatar
Tam Le committed
86
87
    get_parent_id = <<-SQL
      SELECT id, name,
88
      CASE WHEN parent_id IS NULL THEN id ELSE parent_id END AS pid
Tam Le's avatar
Tam Le committed
89
      FROM #{TimeEntryActivity.table_name}
90
      WHERE type = 'TimeEntryActivity'
Tam Le's avatar
Tam Le committed
91
92
      SQL
    body_query = <<-SQL
93
      SELECT t3.pid AS act_id, t3.name AS name, sum(t1.units) AS consumed
Tam Le's avatar
Tam Le committed
94
95
96
97
      FROM #{ServicePackEntry.table_name} t1
      INNER JOIN #{TimeEntry.table_name} t2
      ON t1.time_entry_id = t2.id
      INNER JOIN (#{get_parent_id}) t3
98
      ON t2.activity_id = t3.pid
Tam Le's avatar
Tam Le committed
99
100
101
102
103
      SQL
    group_clause = <<-SQL
      GROUP BY t3.pid, t3.name
      ORDER BY consumed
      SQL
Tam Le's avatar
Tam Le committed
104
    where_clause = "WHERE t1.service_pack_id = ?"
105
    where_clause << (start_day.nil? ? '' : ' AND t1.created_at BETWEEN ? AND ?')
Tam Le's avatar
Tam Le committed
106
    query = body_query + where_clause + group_clause
107
    binding.pry
108
109
    par = start_day.nil? ? [query, params[:service_pack_id]] : [query, params[:service_pack_id], start_day, end_day]
    sql = ActiveRecord::Base.send(:sanitize_sql_array, par)
110
    render json: ActiveRecord::Base.connection.exec_query(sql).to_hash, status: 200
Tam Le's avatar
Tam Le committed
111
112
  end

Khang Le's avatar
Khang Le committed
113
114
115
  private

  def service_pack_params
116
    params.require(:service_pack).permit(:name, :total_units, :started_date, :expired_date, :threshold1, :threshold2, mapping_rates_attributes: [:id, :activity_id, :service_pack_id, :units_per_hour, :_destroy])
Khang Le's avatar
Khang Le committed
117
  end
118

119
end