From 2dc1deb74a50c19065657016bc483bcdde198068 Mon Sep 17 00:00:00 2001 From: Frank Duncan Date: Wed, 24 Feb 2021 09:56:51 -0600 Subject: [PATCH] Update people controller to have search actually work And new fields for the CSV download Issue #301: Built-in report pages, to replace dependency on external query dashboards. --- .../scheduler/people_controller.rb | 42 +++++++++++++++---- app/helpers/scheduler/people_helper.rb | 4 -- .../scheduler/people/_index_table.html.haml | 13 +----- app/views/scheduler/people/index.csv.erb | 12 +----- app/views/scheduler/people/index.html.haml | 9 ++-- 5 files changed, 44 insertions(+), 36 deletions(-) diff --git a/app/controllers/scheduler/people_controller.rb b/app/controllers/scheduler/people_controller.rb index ec873433..c85ad210 100644 --- a/app/controllers/scheduler/people_controller.rb +++ b/app/controllers/scheduler/people_controller.rb @@ -9,16 +9,30 @@ class Scheduler::PeopleController < Scheduler::BaseController load_and_authorize_resource class: Roster::Person has_scope :name_contains + has_scope :name_gteq has_scope :in_shift_territory, as: :shift_territory, default: Proc.new {|controller| controller.current_user.primary_shift_territory_id} - #has_scope :in_shift_territory, as: :shift_territory do |controller, scope, val| - # positions = Scheduler::Shift.where(shift_territory_id: val).map{|sh| sh.positions}.flatten - # scope.in_shift_territory(val) - #end # , default: Proc.new {|controller| controller.current_region.positions.where(name: ['DAT Team Lead', 'DAT Technician', 'DAT Trainee', 'DAT Dispatcher']).map(&:id)} has_scope :with_position, type: :array, default: [] - has_scope :last_shift do |controller, scope, val| - scope.where(Scheduler::ShiftAssignment.where(person_id: {roster_people: :id}).where('date > ?', Date.current-val.to_i).exists.not) + has_scope :date_after, :allow_blank => true, :default => FiscalYear.current.start_date.to_s do |controller, scope, val| + if not controller.params[:date_before].blank? + scope.where(Scheduler::ShiftAssignment.where("person_id = roster_people.id").where('date > ?', val).where('date < ?', controller.params[:date_before]).exists) + elsif not val.blank? + scope.where(Scheduler::ShiftAssignment.where("person_id = roster_people.id").where('date > ?', val).exists) + else + scope.where(Scheduler::ShiftAssignment.where("person_id = roster_people.id").exists) + end + end + has_scope :date_before do |controller, scope, val| + # This is a hack, I guess, but I don't know how to do it otherwise + # If there date_after is present, we have to let that scope handle everything + if not controller.params[:date_after].blank? + scope + elsif not controller.params[:date_before].blank? + scope.where(Scheduler::ShiftAssignment.where("person_id = roster_people.id").where('date < ?', val).exists) + else + scope + end end def collection @@ -32,7 +46,16 @@ def prev_shift person helper_method :num_shifts def num_shifts person - person.shift_assignments.where("date < ?", Date.current).count + assignments = person.shift_assignments + if params[:date_after].nil? + assignments = assignments.where("date > ?", FiscalYear.current.start_date.to_s) + elsif not params[:date_after].blank? + assignments = assignments.where("date > ?", params[:date_after]) + end + if not params[:date_before].blank? + assignments = assignments.where("date < ?", params[:date_before]) + end + assignments.count end helper_method :next_shift @@ -40,6 +63,11 @@ def next_shift person person.shift_assignments.where("date >= ?", Date.current).minimum('date') end + def default_search_params + {date_gteq: FiscalYear.current.start_date} + t + end + helper_method :date_ranges def date_ranges [ ["Now", -2], diff --git a/app/helpers/scheduler/people_helper.rb b/app/helpers/scheduler/people_helper.rb index c220b28d..001d64f4 100644 --- a/app/helpers/scheduler/people_helper.rb +++ b/app/helpers/scheduler/people_helper.rb @@ -4,8 +4,4 @@ def flex_schedules ids = collection.to_a.map(&:id) @flex_schedules ||= Scheduler::FlexSchedule.where(id: ids).group_by(&:id) end - - def flex_schedule_for person_id - flex_schedules[person_id].try :first - end end \ No newline at end of file diff --git a/app/views/scheduler/people/_index_table.html.haml b/app/views/scheduler/people/_index_table.html.haml index 09789ad8..430c0179 100644 --- a/app/views/scheduler/people/_index_table.html.haml +++ b/app/views/scheduler/people/_index_table.html.haml @@ -9,8 +9,6 @@ %th Num Shifts %th Prev Shift %th Next Shift - %th Last Login - %th Flex Schedule %th=t '.actions', :default => t("helpers.actions") %tbody - collection.each do |person| @@ -30,21 +28,12 @@ %td -if d = next_shift(person) =d.to_s :dow_short - %td - -if d = person.last_login - =d.to_s :dow_short - %br - (#{(Date.current-d.to_date).to_i} days ago) - %td - = flex_schedule_for(person.id).try(:num_shifts) %td.small -if flex = can?( :update, Scheduler::FlexSchedule.new(person: person)) = link_to "Flex Schedule", scheduler_flex_schedule_path(person), :class => 'btn btn-xs btn-default' -if reminders = can?( :update, Scheduler::NotificationSetting.new(person: person)) = link_to "Reminders", scheduler_notification_setting_path(person), :class => 'btn btn-xs btn-default' - - if flex or reminders - %br = link_to "Shifts", scheduler_person_shift_assignments_path(person), :class => 'btn btn-xs btn-default' - = link_to "Email", person.vc_email_url, :class => 'btn btn-xs btn-default' + = link_to "Email", "mailto:#{person.email}", :class => 'btn btn-xs btn-default' .text-center= paginate collection, :theme => 'twitter-bootstrap-3', remote: true \ No newline at end of file diff --git a/app/views/scheduler/people/index.csv.erb b/app/views/scheduler/people/index.csv.erb index a7b93601..b4e1a265 100644 --- a/app/views/scheduler/people/index.csv.erb +++ b/app/views/scheduler/people/index.csv.erb @@ -1,4 +1,4 @@ -<%- headers = ['Name', 'Positions', 'Num Shifts', 'Prev Shift', 'Next Shift', 'Last Login', 'Flex Schedule'] -%> +<%- headers = ['Name', 'Positions', 'Num Shifts', 'Prev Shift', 'Next Shift'] -%> <%= CSV.generate_line headers -%> <%- collection.each do |person| d = prev_shift(person) @@ -13,20 +13,12 @@ else next_shift = "" end - d = person.last_login - if d - last_login = d.to_s :dow_short - else - last_login = "" - end positions = person.positions.select{|p|!p.hidden}.map(&:name).join ", " -%> <%= CSV.generate_line([person.full_name, positions, pluralize(num_shifts(person), 'shift'), prev_shift, - next_shift, - last_login, - flex_schedule_for(person.id).try(:num_shifts) + next_shift ]).html_safe -%> <%- end -%> diff --git a/app/views/scheduler/people/index.html.haml b/app/views/scheduler/people/index.html.haml index 59c8d2bd..6dabcbc9 100644 --- a/app/views/scheduler/people/index.html.haml +++ b/app/views/scheduler/people/index.html.haml @@ -9,6 +9,12 @@ %tr %th Shift Territory %td=select_tag "shift_territory", options_from_collection_for_select(current_region.shift_territories.enabled, :id, :name, current_scopes[:shift_territory]), multiple: false, id: 'choose-shift_territory', prompt: "All", class: "form-control" + %tr + %th Shifts After + %td=date_field_tag "date_after", (current_scopes[:date_after] or FiscalYear.current.start_date.to_s), class: 'form-control', placeholder: 'After YYYY-MM-DD' + %tr + %th Shifts Before + %td=date_field_tag "date_before", current_scopes[:date_before], class: 'form-control', placeholder: 'Before YYYY-MM-DD' %tr %th Position %td @@ -26,9 +32,6 @@ - checked = current_scopes[:with_position] && current_scopes[:with_position].include?(second.id.to_s) =check_box_tag "with_position[]", second.id, checked = second.name - %tr - %th Last Shift Taken - %td=select_tag "last_shift", options_for_select(date_ranges, current_scopes[:last_shift].try(:to_i)), prompt: "Any Time", class: "form-control" %tr %th Name %td=text_field_tag :name_contains, current_scopes[:name_contains], placeholder: "Search Name", class: "form-control"