<commit_before>class PayformsController < ApplicationController
  before_filter :require_department_admin,  :except => [:index, :show, :go, :prune, :submit, :unsubmit]

  def index
    # raise params.to_yaml
    @start_date = interpret_start
    @end_date = interpret_end
    @payforms = narrow_down(current_user.is_admin_of?(current_department) ?
                            current_department.payforms :
                            current_department.payforms && current_user.payforms)
    @payforms = @payforms.select{|payform| payform.date >= @start_date && payform.date <= @end_date}
    @payforms = @payforms.sort_by{|payform| [payform.user.reverse_name, Date.today - payform.date]}
  end

  def show
    @payform = Payform.find(params[:id])
    flash[:error] = "Payform does not exist." unless @payform
    flash[:error] = "The payform (from #{@payform.department.name}) is not in this department (#{current_department.name})." unless @payform.department == current_department
    flash[:warning] = "This payform is for a current/future time period" if @payform.date > Date.today
    return unless user_is_owner_or_admin_of(@payform, @payform.department)
    if flash[:error]
      redirect_to payforms_path
    else
      respond_to do |show|
        show.html #show.html.erb
        show.pdf  #show.pdf.prawn
        show.csv do
          csv_string = FasterCSV.generate do |csv|
            csv << ["First Name", "Last Name", "Employee ID", "Payrate", "Start Date", "End Date", "Total Hours"]
            csv << [@payform.user.first_name, @payform.user.last_name, @payform.user.employee_id, @payform.payrate, @payform.start_date, @payform.date, @payform.hours]
          end
          send_data csv_string, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment; filename=users.csv"
        end
      end
    end
  end

  def go
    date = params[:date] ? params[:date].to_date : Date.today
    redirect_to Payform.build(current_department, current_user, date)
  end

  def prune
    @payforms = current_department.payforms
    @payforms &= current_user.payforms unless current_user.is_admin_of?(current_department)
    @payforms.select{|p| p.payform_items.empty? }.map{|p| p.destroy }
    flash[:notice] = "Successfully pruned empty payforms."
    redirect_to payforms_path
  end

  def submit
    @payform = Payform.find(params[:id])
    return unless user_is_owner_or_admin_of(@payform, @payform.department)
    @payform.submitted = Time.now
    if @payform.save && @payform.hours > current_department.department_config.payform_time_limit
      flash[:notice] = "Successfully submitted payform, however, you have submitted more than the allowed #{current_department.department_config.payform_time_limit} hours this week."
    elsif @payform.save &&  @payform.hours <= current_department.department_config.payform_time_limit
      flash[:notice] = "Successfully submitted payform."
    end
    respond_to do |format|
      format.html {redirect_to @payform }
      format.js
    end
  end

  def unsubmit
    @payform = Payform.find(params[:id])
    return unless user_is_owner_or_admin_of(@payform, @payform.department)
    @payform.submitted = nil
    if @payform.save
      flash[:notice] = "Successfully unsubmitted payform."<commit_msg>added warning that you are viewing a current week's payform, and when submitting one<commit_after>class PayformsController < ApplicationController
  before_filter :require_department_admin,  :except => [:index, :show, :go, :prune, :submit, :unsubmit]

  def index
    # raise params.to_yaml
    @start_date = interpret_start
    @end_date = interpret_end
    @payforms = narrow_down(current_user.is_admin_of?(current_department) ?
                            current_department.payforms :
                            current_department.payforms && current_user.payforms)
    @payforms = @payforms.select{|payform| payform.date >= @start_date && payform.date <= @end_date}
    @payforms = @payforms.sort_by{|payform| [payform.user.reverse_name, Date.today - payform.date]}
  end

  def show
    @payform = Payform.find(params[:id])
    flash[:error] = "Payform does not exist." unless @payform
    flash[:error] = "The payform (from #{@payform.department.name}) is not in this department (#{current_department.name})." unless @payform.department == current_department
    flash[:warning] = "This payform is for a current/future time period" if @payform.date > Date.today
    return unless user_is_owner_or_admin_of(@payform, @payform.department)
    if flash[:error]
      redirect_to payforms_path
    else
      respond_to do |show|
        show.html #show.html.erb
        show.pdf  #show.pdf.prawn
        show.csv do
          csv_string = FasterCSV.generate do |csv|
            csv << ["First Name", "Last Name", "Employee ID", "Payrate", "Start Date", "End Date", "Total Hours"]
            csv << [@payform.user.first_name, @payform.user.last_name, @payform.user.employee_id, @payform.payrate, @payform.start_date, @payform.date, @payform.hours]
          end
          send_data csv_string, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment; filename=users.csv"
        end
      end
    end
  end

  def go
    date = params[:date] ? params[:date].to_date : Date.today
    redirect_to Payform.build(current_department, current_user, date)
  end

  def prune
    @payforms = current_department.payforms
    @payforms &= current_user.payforms unless current_user.is_admin_of?(current_department)
    @payforms.select{|p| p.payform_items.empty? }.map{|p| p.destroy }
    flash[:notice] = "Successfully pruned empty payforms."
    redirect_to payforms_path
  end

  def submit
    @payform = Payform.find(params[:id])
    return unless user_is_owner_or_admin_of(@payform, @payform.department)
    @payform.submitted = Time.now
 
    if @payform.date > Date.today
      flash[:warning] = "You have submitted a payform that ends on a future date. Please unsubmit this payform if this was unintentional"
    end
 
    if @payform.save && @payform.hours > current_department.department_config.payform_time_limit
      flash[:notice] = "Successfully submitted payform, however, you have submitted more than the allowed #{current_department.department_config.payform_time_limit} hours this week."
    elsif @payform.save &&  @payform.hours <= current_department.department_config.payform_time_limit
      flash[:notice] = "Successfully submitted payform."
    end
    respond_to do |format|
      format.html {redirect_to @payform }
      format.js
    end
  end

  def unsubmit
    @payform = Payform.find(params[:id])
    return unless user_is_owner_or_admin_of(@payform, @payform.department)
    @payform.submitted = nil
    if @payform.save
      flash[:notice] = "Successfully unsubmitted payform."
{'fraction_non_alphanumeric': 0.10922152177100132, 'fraction_numerical': 0.0014660606949127694, 'mean_word_length': 3.6188219363574814, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '806930', 'n_tokens_mistral': 2231, 'n_tokens_neox': 2130, 'n_words': 490}