<commit_before>class BillsController < ApplicationController
  
  # GET /bills/unpaid_fees
  def unpaid_fees
    @user = User.get_logged_in session
    unpaid_teams = @user.teams.where(:bill_id => nil, :paid_by_club => false)

    @teams_by_event = Hash.new{|h, k| h[k] = []}
    for team in unpaid_teams do
      event = team.team_pool.event
      if EventSetting.get_b('startfees_enable',event)
        @teams_by_event[event] << team
      end
    end
    
    respond_to do |format|
      format.html # unpaid_fees.html.erb
    end
  end
  
  
  # GET /bills
  # GET /bills.xml
  def index
    @user = User.get_logged_in session
    allBills = @user.bills
    @bills = allBills.where(:id => Team.where(:bill_id => allBills.map{|b| b.id}, :paid_by_club => false).map {|t| t.bill_id}).order('created_at DESC')
    @userHasUnbilledFees = (@user.teams.where(:bill_id => nil, :paid_by_club => false).size > 0)
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @bills }
    end
  end

  # GET /bills/1
  # GET /bills/1.xml
  def show
    @user = User.get_logged_in session
    @bill = @user.bills.find(params[:id])
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @bill }
    end
  end

  # GET /bills/new
  # GET /bills/new.xml
  def new
    raise "Missing parameter" unless params[:event]
    @user = User.get_logged_in session
    @event = Event.find(params[:event])
    raise "Invalid event!" unless @event
    @teams = @user.teams.where(:team_pool_id => TeamPool.where(:event_id => @event.id).map{|tp| tp.id}, :bill_id => nil, :paid_by_club => false)
    @teams_hash = generate_teams_hash(@teams)
    @total_amount = @teams.inject(0) { |sum, t| sum + t.start_fee }
    @bill = Bill.new
    @userAddressKnown = false
    if @user.has_full_address
      @userAddressKnown = true
    end
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @bill }
    end
  end

  # POST /bills
  # POST /bills.xml
  def create
    
    #check user
    @user = User.get_logged_in session
    raise "Full address is required to create bills" unless @user.has_full_address
    
    #check teams
    teamIds = params[:bill][:team_ids]
    teamIdsHash = params[:bill][:team_ids_hash]
    raise "Missing teams for bill" unless teamIds != nil and !teamIds.empty?
    teams = Team.where(:id => teamIds)
    raise "Invalid team ids" unless !teams.empty? and teamIds.size == teams.size and teamIdsHash and check_team_hash(teams, teamIdsHash)
    eventId = teams[0].team_pool.event.id
    teams.map do |t|
      raise "Invalid collection of teams" if t.user_id != @user.id or t.bill_id != nil or t.team_pool.event.id != eventId
    end
    
    @bill = Bill.new
    @bill.exchange_rate = 1.0
    @bill.is_paper_bill = false
    @bill.is_paper_bill_sent = false
    @bill.is_paid = false
    @bill.reference_number = generate_reference_number(@user)
    
    @bill.user = @user
    @bill.first_name = @user.first_name
    @bill.last_name = @user.last_name
    @bill.address_line_1 = @user.address_line_1
    @bill.address_line_2 = @user.address_line_2
    @bill.city = @user.city
    @bill.zipcode = @user.zipcode
    @bill.province = @user.province
    @bill.country = @user.country
    
    error = false
    if @bill.save
      teams.map do |t| 
        t.bill = @bill
        t.save
      end
    else
      error = true
    end
    
    respond_to do |format|
      if !error
        format.html { redirect_to(@bill, :notice => noticeInfo(t('.billcreationsuccess'))) }
        format.xml  { render :xml => @bill, :status => :created, :location => @bill }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @bill.errors, :status => :unprocessable_entity }
      end
    end
  end

  private
  #Check if current user has the right to access the bill
  def check_auth(user, bill)
    raise "Permission denied" unless bill.user_id == user.id
  end
  
  def generate_reference_number(user)
    randString = ""
    19.times{randString  << (48 + rand(10)).chr}
    ("%07d" % user.id)[-7..-1]+randString
  end
   
  def secretString
    BILL_TEAM_ID_LIST_SECRET
  end
  
  def generate_teams_hash(teams)
    concatString = secretString
    teams.each do |t|
      concatString += "#"+t.created_at.to_s()+t.id.to_s()
    end
    Digest::SHA2.hexdigest(concatString)
  end
    
  def check_team_hash(teams, hash)
    h = generate_teams_hash(teams)
    h == hash
  end
  
end<commit_msg>honor event setting startfees_enable when displaying unbilled teams<commit_after>class BillsController < ApplicationController
  
  # GET /bills/unpaid_fees
  def unpaid_fees
    @user = User.get_logged_in session
    unpaid_teams = getUnbilledTeams(@user)

    @teams_by_event = Hash.new{|h, k| h[k] = []}
    for team in unpaid_teams do
      @teams_by_event[team.team_pool.event] << team
    end
    
    respond_to do |format|
      format.html # unpaid_fees.html.erb
    end
  end
  
  
  # GET /bills
  # GET /bills.xml
  def index
    @user = User.get_logged_in session
    allBills = @user.bills
    @bills = allBills.where(:id => Team.where(:bill_id => allBills.map{|b| b.id}, :paid_by_club => false).map {|t| t.bill_id}).order('created_at DESC')
    @userHasUnbilledFees = getUnbilledTeams(@user).size > 0
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @bills }
    end
  end

  # GET /bills/1
  # GET /bills/1.xml
  def show
    @user = User.get_logged_in session
    @bill = @user.bills.find(params[:id])
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @bill }
    end
  end

  # GET /bills/new
  # GET /bills/new.xml
  def new
    raise "Missing parameter" unless params[:event]
    @user = User.get_logged_in session
    @event = Event.find(params[:event])
    raise "Invalid event!" unless @event
    @teams = @user.teams.where(:team_pool_id => TeamPool.where(:event_id => @event.id).map{|tp| tp.id}, :bill_id => nil, :paid_by_club => false)
    @teams_hash = generate_teams_hash(@teams)
    @total_amount = @teams.inject(0) { |sum, t| sum + t.start_fee }
    @bill = Bill.new
    @userAddressKnown = false
    if @user.has_full_address
      @userAddressKnown = true
    end
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @bill }
    end
  end

  # POST /bills
  # POST /bills.xml
  def create
    
    #check user
    @user = User.get_logged_in session
    raise "Full address is required to create bills" unless @user.has_full_address
    
    #check teams
    teamIds = params[:bill][:team_ids]
    teamIdsHash = params[:bill][:team_ids_hash]
    raise "Missing teams for bill" unless teamIds != nil and !teamIds.empty?
    teams = Team.where(:id => teamIds)
    raise "Invalid team ids" unless !teams.empty? and teamIds.size == teams.size and teamIdsHash and check_team_hash(teams, teamIdsHash)
    eventId = teams[0].team_pool.event.id
    teams.map do |t|
      raise "Invalid collection of teams" if t.user_id != @user.id or t.bill_id != nil or t.team_pool.event.id != eventId
    end
    
    @bill = Bill.new
    @bill.exchange_rate = 1.0
    @bill.is_paper_bill = false
    @bill.is_paper_bill_sent = false
    @bill.is_paid = false
    @bill.reference_number = generate_reference_number(@user)
    
    @bill.user = @user
    @bill.first_name = @user.first_name
    @bill.last_name = @user.last_name
    @bill.address_line_1 = @user.address_line_1
    @bill.address_line_2 = @user.address_line_2
    @bill.city = @user.city
    @bill.zipcode = @user.zipcode
    @bill.province = @user.province
    @bill.country = @user.country
    
    error = false
    if @bill.save
      teams.map do |t| 
        t.bill = @bill
        t.save
      end
    else
      error = true
    end
    
    respond_to do |format|
      if !error
        format.html { redirect_to(@bill, :notice => noticeInfo(t('.billcreationsuccess'))) }
        format.xml  { render :xml => @bill, :status => :created, :location => @bill }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @bill.errors, :status => :unprocessable_entity }
      end
    end
  end

  private
  #Check if current user has the right to access the bill
  def check_auth(user, bill)
    raise "Permission denied" unless bill.user_id == user.id
  end
  
  def generate_reference_number(user)
    randString = ""
    19.times{randString  << (48 + rand(10)).chr}
    ("%07d" % user.id)[-7..-1]+randString
  end
   
  def secretString
    BILL_TEAM_ID_LIST_SECRET
  end
  
  def generate_teams_hash(teams)
    concatString = secretString
    teams.each do |t|
      concatString += "#"+t.created_at.to_s()+t.id.to_s()
    end
    Digest::SHA2.hexdigest(concatString)
  end
    
  def check_team_hash(teams, hash)
    h = generate_teams_hash(teams)
    h == hash
  end
  
  def getUnbilledTeams(user)
    unbilledTeams = []
    user.teams.where(:bill_id => nil, :paid_by_club => false).map{|t| unbilledTeams << t if EventSetting.get_b("startfees_enable",t.team_pool.event)}
    return unbilledTeams
  end
  
end
{'fraction_non_alphanumeric': 0.12027789839339992, 'fraction_numerical': 0.004776378636561007, 'mean_word_length': 2.77427283900041, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 28}, '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': '5431408', 'n_tokens_mistral': 3410, 'n_tokens_neox': 3285, 'n_words': 735}