<commit_before>class TimeInvoiceReport
  unloadable
  attr_accessor :report_options
  
  def initialize(options) 
    @report_options = options
  end

  # returns nil if validation completes
  # returns an error string or array of string when validation fails
  def validate
    if @report_options[:start_date_from] > @report_options[:start_date_to] && 
        @report_options[:end_date_from] <= @report_options[:end_date_to] && !report_options[:start_date_to].blank?
      return 'For Start Date :From Date Cannot Be smaller than To Date ' 
    else if  @report_options[:end_date_from] > @report_options[:end_date_to] &&
          @report_options[:start_date_from] <= @report_options[:start_date_to] && !report_options[:end_date_to].blank?
        return 'For End Date :From Cannot Be Smaller than To Date '
      else if @report_options[:start_date_from] > @report_options[:start_date_to] &&
            @report_options[:end_date_from] > @report_options[:end_date_to] &&
            !report_options[:start_date_to].blank? && !report_options[:end_date_to].blank?
            return 'From Dates Cannot Be Smaller than To Dates'
        end
      end
    end
    nil
  end
  
  def generate
    #@time_invoices = TimeInvoice
    @time_invoice_details=TimeInvoiceDetail.includes(:time_invoice)
    #@time_invoice_details=TimeInvoiceDetail.joins(:time_invoice)
 
#Start Date=====================================================================
    
      unless @report_options[:start_date_from].blank?
        @time_invoice_details = @time_invoice_details.where("#{TimeInvoice.table_name}.start_date >= ?", 
          @report_options[:start_date_from])
      end
      unless @report_options[:start_date_to].blank?
        @time_invoice_details = @time_invoice_details.where("#{TimeInvoice.table_name}.start_date <= ?", 
          @report_options[:start_date_to])
      end 
      
       ActiveRecord::Base.logger.debug "#{'*'*80}\nTime invoice Start Time #{@time_invoice_details.count(:all)}\n#{'*'*80}"


#===============================================================================

#End Date=======================================================================    
     

      unless @report_options[:end_date_from].blank?
        @time_invoice_details = @time_invoice_details.where("#{TimeInvoice.table_name}.end_date >= ?", 
          @report_options[:end_date_from])
      end
      unless @report_options[:end_date_to].blank?
        @time_invoice_details = @time_invoice_details.where("#{TimeInvoice.table_name}.end_date <= ?", 
          @report_options[:end_date_to])
      end  
       ActiveRecord::Base.logger.debug "#{'*'*80}\nTime invoice End Time #{@time_invoice_details.count(:all)}\n#{'*'*80}"

    
   
#===============================================================================    

    
#User===========================================================================
    selected_users = @report_options[:selected_users]
    if selected_users.present?
      @time_invoice_details = @time_invoice_details.where(:user_id => selected_users)
    end
     ActiveRecord::Base.logger.debug "#{'-'*80}\nSeelcted Users Without Group #{selected_users}\n#{'*'*80}"

#===============================================================================

    
#Group==========================================================================
selected_groups = @report_options[:groups]
     ActiveRecord::Base.logger.debug "#{'*'*1000}These are selected groups #{selected_groups}"
    unless selected_groups.nil?
      
            selected_group_users = User.active.joins(:groups).
              where("#{User.table_name_prefix}groups_users#{User.table_name_suffix}.id" =>
                selected_groups)
      
      if selected_users.nil?
        selected_users=[]
      end

       ActiveRecord::Base.logger.debug "These are Redmine selected users #{selected_group_users}"

      
      selected_group_users.each do |group_user|
        selected_users << User.find(group_user).id.to_s
      end
      
      selected_users = selected_users.uniq
      
      unless selected_users.nil?
        @time_invoice_details = @time_invoice_details.where(:user_id => selected_users)
         ActiveRecord::Base.logger.debug "#{'+'*80}\nSelcted Users #{selected_users}\n#{'*'*80}"
      end
    end
    
    
    
    
#===============================================================================

#Submitted By User==============================================================  
    unless @report_options[:submitted_by_user].blank?
      @time_invoice_details = @time_invoice_details.
        where("#{TimeInvoice.table_name}.submitted_by_id" =>
          @report_options[:submitted_by_user])
       ActiveRecord::Base.logger.debug "#{'SBU-'*80}\nTime Invoice Submitted by User 
                                {@time_invoice_details.count(:all)}\n#{'*'*80}#"
    end
    
    
#===============================================================================
    
        
#Invoiced Time==================================================================
        
    unless @report_options[:invoiced_time_compared_hours].blank?
          
       ActiveRecord::Base.logger.debug "#{'%'*80}\nInside Time Invoice Report 
                    {@report_options[:invoiced_time_compared_hours]}\n#{'*'*80}#"
          
      invoiced_operator_value=@report_options[:invoiced_operator_value]
          
      if invoiced_operator_value =='<'
        sum_invoiced_time_users = @time_invoice_details.dup
        sum_invoiced_time_users = sum_invoiced_time_users.group(:user_id).
          having('SUM(invoiced_hours) < ?', @report_options[:invoiced_time_compared_hours].to_i)

        @time_invoice_details = @time_invoice_details.
          where(user_id: sum_invoiced_time_users.collect{|it| it.user_id})          
         ActiveRecord::Base.logger.debug "#{'IT'*80}\nInside Less than Invoice Condition "
      end
          
      if invoiced_operator_value =='>'
        sum_invoiced_time_users = @time_invoice_details.dup
        sum_invoiced_time_users = sum_invoiced_time_users.group(:user_id).
          having('SUM(invoiced_hours) > ?', @report_options[:invoiced_time_compared_hours].to_i)
            
        @time_invoice_details = @time_invoice_details.where(user_id: sum_invoiced_time_users.collect{|it| it.user_id})          
         ActiveRecord::Base.logger.debug "#{'IT'*80}\nInside Greater  than Invoice Condition "
              
      end
      
    end
#===============================================================================
     
#Logged Time===================================================================
        
    unless @report_options[:logged_time_compared_hours].blank?          
       ActiveRecord::Base.logger.debug "#{'%'*80}\nInside Time Invoice Report 
                    {@report_options[:logged_time_compared_hours]}\n#{'*'*80}#"          
      logged_operator_value=@report_options[:logged_operator_value]
          
      if logged_operator_value =='<'
        sum_logged_time_users = @time_invoice_details.dup
        sum_logged_time_users = sum_logged_time_users.group(:user_id).
          having('SUM(logged_hours) < ?', @report_options[:logged_time_compared_hours].to_i)
         ActiveRecord::Base.logger.debug "Got logged_time_users: #{sum_logged_time_users.inspect}"
        @time_invoice_details = @time_invoice_details.
          where(user_id: sum_logged_time_users.collect{|it| it.user_id})          
         ActiveRecord::Base.logger.debug "#{'LT'*80}\nInside Less than Logged Condition "
      end
          
      if logged_operator_value =='>'
        sum_logged_time_users = @time_invoice_details.dup
        sum_logged_time_users = sum_logged_time_users.group(:user_id).
          having('SUM(logged_hours) > ?', @report_options[:logged_time_compared_hours].to_i)
            
        @time_invoice_details = @time_invoice_details.where(user_id: sum_logged_time_users.collect{|it| it.user_id})          
         ActiveRecord::Base.logger.debug "#{'LT'*80}\nInside Greater  than Logged Condition "
              
      end
      
    end  
    
 
#===============================================================================

     ActiveRecord::Base.logger.debug "#{'F'*80}\nTime invoice End Time #{@time_invoice_details.count(:all)}\n#{'*'*80}"
    @time_invoice_details
  end
  
   
  
end
<commit_msg>adds date validator for time_invoice_report<commit_after>class TimeInvoiceReport
  include ActiveModel::Validations
  
  unloadable
  attr_accessor :report_options
  validates :start_date_from, :date => true,:allow_nil => true
  validates :start_date_to, :date => true,:allow_nil => true
  validates :end_date_from, :date => true,:allow_nil => true
  validates :end_date_to, :date => true,:allow_nil => true
  
  attr_accessor :start_date_from, :start_date_to,:end_date_from,:end_date_to
  
  def initialize(options) 
    @report_options = options
    self.start_date_from = options[:start_date_from]
    self.start_date_to = options[:start_date_to]
    self.end_date_from = options[:end_date_from]
    self.end_date_to = options[:end_date_to]
  end

  # returns nil if validation completes
  # returns an error string or array of string when validation fails
  def validate
    if @report_options[:start_date_from] > @report_options[:start_date_to] && 
        @report_options[:end_date_from] <= @report_options[:end_date_to] && !report_options[:start_date_to].blank?
      return 'For Start Date :From Date Cannot Be smaller than To Date ' 
    else if  @report_options[:end_date_from] > @report_options[:end_date_to] &&
          @report_options[:start_date_from] <= @report_options[:start_date_to] && !report_options[:end_date_to].blank?
        return 'For End Date :From Cannot Be Smaller than To Date '
      else if @report_options[:start_date_from] > @report_options[:start_date_to] &&
            @report_options[:end_date_from] > @report_options[:end_date_to] &&
            !report_options[:start_date_to].blank? && !report_options[:end_date_to].blank?
            return 'From Dates Cannot Be Smaller than To Dates'
        end
      end
    end
    nil
  end
  
  def generate
    #@time_invoices = TimeInvoice
    @time_invoice_details=TimeInvoiceDetail.includes(:time_invoice)
    #@time_invoice_details=TimeInvoiceDetail.joins(:time_invoice)
 
#Start Date=====================================================================
    
      unless @report_options[:start_date_from].blank?
        @time_invoice_details = @time_invoice_details.where("#{TimeInvoice.table_name}.start_date >= ?", 
          @report_options[:start_date_from])
      end
      unless @report_options[:start_date_to].blank?
        @time_invoice_details = @time_invoice_details.where("#{TimeInvoice.table_name}.start_date <= ?", 
          @report_options[:start_date_to])
      end 
      
       ActiveRecord::Base.logger.debug "#{'*'*80}\nTime invoice Start Time #{@time_invoice_details.count(:all)}\n#{'*'*80}"


#===============================================================================

#End Date=======================================================================    
     

      unless @report_options[:end_date_from].blank?
        @time_invoice_details = @time_invoice_details.where("#{TimeInvoice.table_name}.end_date >= ?", 
          @report_options[:end_date_from])
      end
      unless @report_options[:end_date_to].blank?
        @time_invoice_details = @time_invoice_details.where("#{TimeInvoice.table_name}.end_date <= ?", 
          @report_options[:end_date_to])
      end  
       ActiveRecord::Base.logger.debug "#{'*'*80}\nTime invoice End Time #{@time_invoice_details.count(:all)}\n#{'*'*80}"

    
   
#===============================================================================    

    
#User===========================================================================
    selected_users = @report_options[:selected_users]
    if selected_users.present?
      @time_invoice_details = @time_invoice_details.where(:user_id => selected_users)
    end
     ActiveRecord::Base.logger.debug "#{'-'*80}\nSeelcted Users Without Group #{selected_users}\n#{'*'*80}"

#===============================================================================

    
#Group==========================================================================
selected_groups = @report_options[:groups]
     ActiveRecord::Base.logger.debug "#{'*'*1000}These are selected groups #{selected_groups}"
    unless selected_groups.nil?
      
            selected_group_users = User.active.joins(:groups).
              where("#{User.table_name_prefix}groups_users#{User.table_name_suffix}.id" =>
                selected_groups)
      
      if selected_users.nil?
        selected_users=[]
      end

       ActiveRecord::Base.logger.debug "These are Redmine selected users #{selected_group_users}"

      
      selected_group_users.each do |group_user|
        selected_users << User.find(group_user).id.to_s
      end
      
      selected_users = selected_users.uniq
      
      unless selected_users.nil?
        @time_invoice_details = @time_invoice_details.where(:user_id => selected_users)
         ActiveRecord::Base.logger.debug "#{'+'*80}\nSelcted Users #{selected_users}\n#{'*'*80}"
      end
    end
    
    
    
    
#===============================================================================

#Submitted By User==============================================================  
    unless @report_options[:submitted_by_user].blank?
      @time_invoice_details = @time_invoice_details.
        where("#{TimeInvoice.table_name}.submitted_by_id" =>
          @report_options[:submitted_by_user])
       ActiveRecord::Base.logger.debug "#{'SBU-'*80}\nTime Invoice Submitted by User 
                                {@time_invoice_details.count(:all)}\n#{'*'*80}#"
    end
    
    
#===============================================================================
    
        
#Invoiced Time==================================================================
        
    unless @report_options[:invoiced_time_compared_hours].blank?
          
       ActiveRecord::Base.logger.debug "#{'%'*80}\nInside Time Invoice Report 
                    {@report_options[:invoiced_time_compared_hours]}\n#{'*'*80}#"
          
      invoiced_operator_value=@report_options[:invoiced_operator_value]
          
      if invoiced_operator_value =='<'
        sum_invoiced_time_users = @time_invoice_details.dup
        sum_invoiced_time_users = sum_invoiced_time_users.group(:user_id).
          having('SUM(invoiced_hours) < ?', @report_options[:invoiced_time_compared_hours].to_i)

        @time_invoice_details = @time_invoice_details.
          where(user_id: sum_invoiced_time_users.collect{|it| it.user_id})          
         ActiveRecord::Base.logger.debug "#{'IT'*80}\nInside Less than Invoice Condition "
      end
          
      if invoiced_operator_value =='>'
        sum_invoiced_time_users = @time_invoice_details.dup
        sum_invoiced_time_users = sum_invoiced_time_users.group(:user_id).
          having('SUM(invoiced_hours) > ?', @report_options[:invoiced_time_compared_hours].to_i)
            
        @time_invoice_details = @time_invoice_details.where(user_id: sum_invoiced_time_users.collect{|it| it.user_id})          
         ActiveRecord::Base.logger.debug "#{'IT'*80}\nInside Greater  than Invoice Condition "
              
      end
      
    end
#===============================================================================
     
#Logged Time===================================================================
        
    unless @report_options[:logged_time_compared_hours].blank?          
       ActiveRecord::Base.logger.debug "#{'%'*80}\nInside Time Invoice Report 
                    {@report_options[:logged_time_compared_hours]}\n#{'*'*80}#"          
      logged_operator_value=@report_options[:logged_operator_value]
          
      if logged_operator_value =='<'
        sum_logged_time_users = @time_invoice_details.dup
        sum_logged_time_users = sum_logged_time_users.group(:user_id).
          having('SUM(logged_hours) < ?', @report_options[:logged_time_compared_hours].to_i)
         ActiveRecord::Base.logger.debug "Got logged_time_users: #{sum_logged_time_users.inspect}"
        @time_invoice_details = @time_invoice_details.
          where(user_id: sum_logged_time_users.collect{|it| it.user_id})          
         ActiveRecord::Base.logger.debug "#{'LT'*80}\nInside Less than Logged Condition "
      end
          
      if logged_operator_value =='>'
        sum_logged_time_users = @time_invoice_details.dup
        sum_logged_time_users = sum_logged_time_users.group(:user_id).
          having('SUM(logged_hours) > ?', @report_options[:logged_time_compared_hours].to_i)
            
        @time_invoice_details = @time_invoice_details.where(user_id: sum_logged_time_users.collect{|it| it.user_id})          
         ActiveRecord::Base.logger.debug "#{'LT'*80}\nInside Greater  than Logged Condition "
              
      end
      
    end  
    
 
#===============================================================================

     ActiveRecord::Base.logger.debug "#{'F'*80}\nTime invoice End Time #{@time_invoice_details.count(:all)}\n#{'*'*80}"
    @time_invoice_details
  end
  
   
  
end

{'fraction_non_alphanumeric': 0.2211405529953917, 'fraction_numerical': 0.005069124423963134, 'mean_word_length': 3.7590460526315788, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 39, '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': '2115572', 'n_tokens_mistral': 5191, 'n_tokens_neox': 4956, 'n_words': 691}