class Admin::RulingTimersController < OrbitAdminController include Admin::RulingTimersHelper before_action ->(module_app = @app_title) { set_variables module_app } before_action :set_weekdays def initialize super @app_title = "ruling_timer" end def index user = current_user @ruling_timer_temp = RulingTimerTemp.where(:user=>user).first @ruling_timer_history = [] all_count = 0 per = 10 page = params[:page].to_i if @ruling_timer_temp @ruling_timer_history = RulingTimerHistory.where(:user=>user).desc(:created_at) all_count += (1 + @ruling_timer_history.count) if page <= 1 @ruling_timer_history = @ruling_timer_history.page(0).per(per - 1) else @ruling_timer_history = @ruling_timer_history.limit(per).skip(per * page - 1) @ruling_timer_temp = nil end end @ruling_pager = RulingPager.new(:count=>all_count,:page=>page,:per=>per) end def timer_management @timer_temps = RulingTimerTemp.all.page(params[:page]).per(15) @active_users = @timer_temps.map{|t| t.user} end def task_management @tasks = RulingTimerTask.all.page(params[:page]).per(10) end def work_history user = User.find(params[:id]) rescue nil @ruling_timer_temp = RulingTimerTemp.where(:user=>user).first @ruling_timer_history = [] all_count = 0 per = 10 page = params[:page].to_i if @ruling_timer_temp @ruling_timer_history = RulingTimerHistory.where(:user=>user).desc(:created_at) all_count += (1 + @ruling_timer_history.count) if page <= 1 @ruling_timer_history = @ruling_timer_history.page(0).per(per - 1) else @ruling_timer_history = @ruling_timer_history.limit(per).skip(per * page - 1) @ruling_timer_temp = nil end end @ruling_pager = RulingPager.new(:count=>all_count,:page=>page,:per=>per) @user_name = user.name render "index" end def edit_temp_timer @timer = RulingTimerTemp.find(params[:id]) rescue nil if @timer.nil? redirect_to admin_ruling_timers_path and return end render "edit_timer" end def edit_timer @timer = RulingTimerHistory.find(params[:id]) rescue nil if @timer.nil? redirect_to admin_ruling_timers_path and return end end def update_timer @timer = nil if params[:type] == "temp" @timer = RulingTimerTemp.find(params[:id]) else @timer = RulingTimerHistory.find(params[:id]) end @timer_params = timer_params date = @timer.date.split(" ")[0] @timer_params[:work_times] = @timer_params[:work_times].to_a.map do |t| if t.blank? nil else DateTime.parse("#{date} #{t}#{@timer.time_offset}").utc end end.compact @timer.update_attributes(@timer_params) @timer.fix_work_times redirect_to params[:referer_url] end def add_task @task = RulingTimerTask.new @users = User.all.where(:user_name.ne=>"rulingcom") end def edit_task @task = RulingTimerTask.find(params[:id]) @users = User.all.where(:user_name.ne=>"rulingcom") end def create_task @task = RulingTimerTask.create(task_params) redirect_to params[:referer_url] end def update_task @task = RulingTimerTask.find(params[:id]) @task.update_attributes(task_params) redirect_to params[:referer_url] end def delete_task if params[:confirm_delete] RulingTimerTask.where(:id=>params[:id]).destroy render :json => {:success=>true} end end def view_task @task = RulingTimerTask.find(params[:id]) end def delete_history if params[:confirm_delete] if params[:type] == 'temp' RulingTimerTemp.where(:id=>params[:id]).destroy else RulingTimerHistory.where(:id=>params[:id]).destroy end render :json => {:success=>true} end end def add_history @user = nil if params[:id] == "current_user" params[:id] = current_user.id.to_s @user = current_user else @user = User.find(params[:id]) end @history = RulingTimerHistory.new(:user_id=>params[:id]) end def create_history @history_params = history_params user = User.find(@history_params[:user_id]) @old_historys = RulingTimerHistory.where(:date=>@history_params[:date],:user=>user).to_a + RulingTimerTemp.where(:date=>@history_params[:date],:user=>user).to_a if @old_historys.count == 0 if RulingTimerTemp.where(:user=>user).count == 0 @history = RulingTimerTemp.create(@history_params) else @history = RulingTimerHistory.create(@history_params) end @history.recalc_all @history.fix_work_times else @old_history = @old_historys.first @old_history.merge_work_times(@history_params[:work_times]) end redirect_to params[:referer_url] end def history_params history_params = params.require(:ruling_timer_history).permit! date = history_params[:date] time_offset = history_params[:time_offset] history_params[:work_times] =[] if history_params[:work_times].nil? first_time = history_params[:work_times].first first_time = "00:00:00" if first_time.blank? history_params[:work_times] = history_params[:work_times].map do |t| if t.blank? nil else DateTime.parse("#{date} #{t}#{time_offset}").utc end end.compact history_params[:date] = DateTime.parse(date).strftime("%Y/%m/%d %w") history_params[:created_at] = DateTime.parse(date + " " + first_time + time_offset).utc history_params end def task_params task_params = params.require(:ruling_timer_task).permit! task_params[:user_ids] = [] if task_params[:user_ids].nil? task_params end def timer_params if params[:type] == "temp" params.require(:ruling_timer_temp).permit! else params.require(:ruling_timer_history).permit! end end def set_weekdays @weekdays = ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"] @weekdays.map!{|d| I18n.t("ruling_timer.week_days.#{d}")} end class RulingPager attr_accessor :count,:page,:per def initialize(attrs) if attrs.class == Hash attrs.each do |k,v| self.send("#{k}=",v) end end end def current_page self.page.to_i end def limit_value self.per end def total_pages (self.count / self.per).ceil end end end