class Admin::RulingTimersController < OrbitMemberController include Admin::RulingTimersHelper before_action :check_permissions, :set_weekdays, :set_setting before_action :set_projects , :only => [:project_management,:add_task,:edit_task] before_action :set_tags, :only => [:index, :task_management, :project_management, :add_task,:edit_task] def check_permissions @is_manager = false OrbitHelper.set_params(params,current_user) access_level = OrbitHelper.user_access_level? if (access_level.nil? || access_level == "user") @setting = RulingTimerSetting.first unless @setting.authorize?(current_user) render_401 end else @is_manager = true end end def set_type_params all_types = ["all_tasks","my_tasks","my_assisted_tasks","my_observed_tasks"] type = params[:type] if !all_types.include?(type) if @is_manager type = all_types[0] else type = all_types[1] end end case type when "all_tasks" @tasks = @tasks when "my_tasks" @tasks = @tasks.where(:owner=>current_user) when "my_assisted_tasks" @tasks = @tasks.where(:helper_ids=>current_user.id.to_s) when "my_observed_tasks" @tasks = @tasks.where(:observer_ids=>current_user.id.to_s) end end def initialize super @app_title = "ruling_timer" end def index user = current_user get_user_timer_data(user) 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.desc(:created_at).page(params[:page]).per(10) set_type_params end def project_management end def work_history user = User.find(params[:id]) rescue nil get_user_timer_data(user,true) render "index" end def set_projects @categories = current_user.approved_categories.select{|c| c.module_app_id == @module_app.id} rescue [] @task = nil @private_project_id = nil if params[:action] == "edit_task" @task = RulingTimerTask.find(params[:id]) @private_project_id = RulingTimerProject.where(:is_private=>true,:creator=>@task.creator).pluck(:id).first #私人專案 else @private_project_id = RulingTimerProject.where(:is_private=>true,:creator=>current_user).pluck(:id).first #私人專案 end @projects = RulingTimerProject.any_of({:is_hidden=>false,:category_id.in=>@categories.collect(&:id)},{:id=>@private_project_id}).desc(:created_at) if params[:action] == "project_management" @projects = @projects.page(params[:page]).per(10) else @projects = @projects.to_a if @private_project_id.nil? @projects = [RulingTimerProject.new] + @projects end end end def get_user_timer_data(user, display_name = false) time_now = DateTime.now.utc.new_offset("+8") now_year_month = time_now.strftime("%Y/%m") @start_year_month = params[:start_year_month].present? ? DateTime.parse(params[:start_year_month]).utc : time_now @end_year_month = params[:end_year_month].present? ? DateTime.parse(params[:end_year_month]).utc : time_now @all_year_months = [] tmp = @start_year_month.clone while tmp <= @end_year_month do @all_year_months << tmp.strftime("%Y/%m") tmp += 1.month end @all_year_months_data = @all_year_months.map do |ym| tmp = RulingTimerTemp.where(:user=>user,:date=>/#{ym.sub("/","\\/")}/).first all_infos = [] if tmp all_infos << {:date=>tmp.date.split(" ")[0],:seconds=>tmp.calc("work_times"),:work_time_str=>tmp.transform_second_to_time(tmp.all_work_times_seconds)} end all_infos += RulingTimerHistory.where(:user=>user,:date=>/#{ym.sub("/","\\/")}/).desc(:created_at).pluck(:date,:all_work_times_seconds,:work_time_str).map{|d,t,str| {:date=> d.split(" ")[0],:seconds=>t,:work_time_str=>str}} [ym, all_infos.inject(0){|sum,h| sum + h[:seconds] } , all_infos] end @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 if display_name 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 @no_comment_save_btn = true @task = RulingTimerTask.new(:owner=>current_user,:ruling_timer_project_id=>@private_project_id) if params[:project_id] @task.ruling_timer_project_id = params[:project_id] end @project = @task.ruling_timer_project @users = User.where(:member_profile_id.in=>MemberProfile.where(:role_ids.in=>@setting.role_ids).pluck(:id)) end def edit_task @no_comment_save_btn = true @users = User.where(:member_profile_id.in=>MemberProfile.where(:role_ids.in=>@setting.role_ids).pluck(:id)) @project = nil if params[:project_id] @project = RulingTimerProject.find(params[:project_id]) else @project = @task.ruling_timer_project end end def create_task @task = RulingTimerTask.new(task_params) @task.creator = current_user @task.update_user_id = current_user.id @task.save redirect_to params[:referer_url] end def update_task @task = RulingTimerTask.find(params[:id]) @task.update_user_id = current_user.id @task.update_attributes(task_params) @task.save 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 = nil if params[:type] == "sub_task" @task = RulingTimerSubTask.find(params[:id]).ruling_timer_task else @task = RulingTimerTask.find(params[:id]) end 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 add_project @project = RulingTimerProject.new(:project_manager=>current_user) @categories = current_user.approved_categories.select{|c| c.module_app_id == @module_app.id} rescue [] @users = User.where(:member_profile_id.in=>MemberProfile.where(:role_ids.in=>@setting.role_ids).pluck(:id)) end def edit_project @project = RulingTimerProject.find(params[:id]) @categories = current_user.approved_categories.select{|c| c.module_app_id == @module_app.id} rescue [] @users = User.where(:member_profile_id.in=>MemberProfile.where(:role_ids.in=>@setting.role_ids).pluck(:id)) end def create_project @project = RulingTimerProject.new(project_params.merge({:creator=>current_user})) @project.save redirect_to params[:referer_url] end def update_project @project = RulingTimerProject.find(params[:id]) @project.update_attributes(project_params) redirect_to params[:referer_url] end def delete_project if params[:confirm_delete] RulingTimerProject.where(:id=>params[:id]).destroy render :json => {:success=>true} end end def view_project @project = RulingTimerProject.find(params[:id]) @tasks = @project.ruling_timer_tasks.desc(:created_at).page(params[:page]).per(10) set_type_params render "task_management" 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 section_management @project = RulingTimerProject.find(params[:id]) @sections = @project.ruling_timer_sections.page(params[:page]).per(10) if request.xhr? render :partial => "sections_content" end end def add_section @section = RulingTimerSection.new(:ruling_timer_project_id=>params[:id]) render :layout => false end def edit_section @section = RulingTimerSection.find(params[:id]) render :layout => false end def update_section @section = RulingTimerSection.find(params[:id]) @section.update_attributes(section_params) render :json => {:success => true} end def create_section @section = RulingTimerSection.create(section_params) render :json => {:success => true} end def delete_section if params[:confirm_delete] RulingTimerSection.where(:id=>params[:id]).destroy render :json => {:success=>true} end end def save_comment if params["task_id"] @comment = nil @task = RulingTimerTask.find(params["task_id"]) i = 0 if params["id"] @comment = RulingTimerComment.where(:id=>params["id"]).first end if @comment.nil? @comment = RulingTimerComment.new(:ruling_timer_task_id=>params["task_id"],:uploader_id=>current_user.id) i = @task.ruling_timer_comments.count else i = @task.ruling_timer_comments.find_index{|c| c.id.to_s == params["id"]} end @comment.content = params["content"] @comment.save html = nil f = RulingFormHelper.new f.fields_for :task,@task do |f| f.fields_for :ruling_timer_comments, @comment do |f| html = render_to_string(:partial=>"form_comment", :object => @comment ,:locals=>{:f=>f,:i=>i}) end end render :json => {:success=>true,:id=>@comment.id.to_s, :html => html} end end def delete_comment if params["id"] RulingTimerComment.where(:id=>params["id"]).destroy end puts params render :json => {:success=>true} end def section_params section_params = params.require(:ruling_timer_section).permit! 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! end def project_params project_params = params.require(:ruling_timer_project).permit! project_params[:all_user_ids] = [] if project_params[:all_user_ids].nil? project_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 def settings @roles = Role.all end def update_setting @setting.update_attributes(setting_params) redirect_to settings_admin_ruling_timers_path and return end def set_setting @module_app = ModuleApp.where(:key=>@app_title).first if @module_app.nil? @module_app = OrbitMemberModule.where(:key=>@app_title).first end @setting = RulingTimerSetting.first if @setting.nil? @setting = RulingTimerSetting.create end end def set_tags @tags = @module_app.tags end def setting_params params.require(:ruling_timer_setting).permit! 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 class RulingFormHelper include ActionView::Helpers::FormHelper include ActionView::Context end end