<commit_before># == Schema Information
#
# Table name: expense_items
#
#  id                     :integer          not null, primary key
#  name                   :string(255)
#  description            :string(255)
#  cost                   :decimal(, )
#  export_name            :string(255)
#  position               :integer
#  created_at             :datetime
#  updated_at             :datetime
#  expense_group_id       :integer
#  has_details            :boolean
#  details_label          :string(255)
#  maximum_available      :integer
#  tax_percentage         :decimal(5, 3)    default(0.0)
#  has_custom_cost        :boolean          default(FALSE)
#  maximum_per_registrant :integer          default(0)
#
# Indexes
#
#  index_expense_items_expense_group_id  (expense_group_id)
#

class ExpenseItem < ActiveRecord::Base
  default_scope { order('expense_group_id ASC, position ASC') }

  validates :name, :description, :position, :cost, :expense_group, :tax_percentage, :presence => true
  validates :has_details, :inclusion => { :in => [true, false] } # because it's a boolean
  validates :has_custom_cost, :inclusion => { :in => [true, false] } # because it's a boolean
  validates :tax_percentage, :numericality => {:greater_than_or_equal_to => 0}

  has_many :payment_details, dependent: :restrict_with_error
  has_many :registrant_expense_items, :inverse_of => :expense_item, dependent: :restrict_with_error
  has_many :coupon_code_expense_items, dependent: :destroy

  translates :name, :description, :details_label
  accepts_nested_attributes_for :translations

  belongs_to :expense_group, :inverse_of => :expense_items
  validates :expense_group_id, :uniqueness => true, :if => "(expense_group.try(:competitor_required) == true) or (expense_group.try(:noncompetitor_required) == true)"

  before_destroy :check_for_payment_details

  after_create :create_reg_items

  after_initialize :init

  def init
    self.has_details = false if self.has_details.nil?
    self.tax_percentage = 0 if self.tax_percentage.nil?
    self.has_custom_cost = false if self.has_custom_cost.nil?
  end

  # items paid for
  def paid_items
    payment_details.paid.where(refunded: false)
  end

  def num_paid_with_coupon
    paid_items.with_coupon.count
  end

  def num_paid_without_coupon
    num_paid - num_paid_with_coupon
  end

  def num_paid
    paid_items.count
  end

  # how much have we received for the paid items
  def total_amount_paid
    paid_items.map(&:cost).inject(:+).to_f
  end

  def num_free
    free_items.count
  end

  def unpaid_items
    registrant_expense_items.joins(:registrant).where(:registrants => {:deleted => false})
  end

  def num_unpaid
    unpaid_items.count
  end

  def create_reg_items
    if self.expense_group.competitor_required
      Registrant.competitor.each do |reg|
        reg.build_registration_item(self)
        reg.save
      end
    end
    if self.expense_group.noncompetitor_required<commit_msg>fix num_free<commit_after># == Schema Information
#
# Table name: expense_items
#
#  id                     :integer          not null, primary key
#  name                   :string(255)
#  description            :string(255)
#  cost                   :decimal(, )
#  export_name            :string(255)
#  position               :integer
#  created_at             :datetime
#  updated_at             :datetime
#  expense_group_id       :integer
#  has_details            :boolean
#  details_label          :string(255)
#  maximum_available      :integer
#  tax_percentage         :decimal(5, 3)    default(0.0)
#  has_custom_cost        :boolean          default(FALSE)
#  maximum_per_registrant :integer          default(0)
#
# Indexes
#
#  index_expense_items_expense_group_id  (expense_group_id)
#

class ExpenseItem < ActiveRecord::Base
  default_scope { order('expense_group_id ASC, position ASC') }

  validates :name, :description, :position, :cost, :expense_group, :tax_percentage, :presence => true
  validates :has_details, :inclusion => { :in => [true, false] } # because it's a boolean
  validates :has_custom_cost, :inclusion => { :in => [true, false] } # because it's a boolean
  validates :tax_percentage, :numericality => {:greater_than_or_equal_to => 0}

  has_many :payment_details, dependent: :restrict_with_error
  has_many :registrant_expense_items, :inverse_of => :expense_item, dependent: :restrict_with_error
  has_many :coupon_code_expense_items, dependent: :destroy

  translates :name, :description, :details_label
  accepts_nested_attributes_for :translations

  belongs_to :expense_group, :inverse_of => :expense_items
  validates :expense_group_id, :uniqueness => true, :if => "(expense_group.try(:competitor_required) == true) or (expense_group.try(:noncompetitor_required) == true)"

  before_destroy :check_for_payment_details

  after_create :create_reg_items

  after_initialize :init

  def init
    self.has_details = false if self.has_details.nil?
    self.tax_percentage = 0 if self.tax_percentage.nil?
    self.has_custom_cost = false if self.has_custom_cost.nil?
  end

  # items paid for
  def paid_items
    payment_details.paid.where(refunded: false)
  end

  def num_paid_with_coupon
    paid_items.with_coupon.count
  end

  def num_paid_without_coupon
    num_paid - num_paid_with_coupon
  end

  def num_paid
    paid_items.count
  end

  # how much have we received for the paid items
  def total_amount_paid
    paid_items.map(&:cost).inject(:+).to_f
  end

  def free_items
    payment_details.free
  end

  def num_free
    free_items.count
  end

  def unpaid_items
    registrant_expense_items.joins(:registrant).where(:registrants => {:deleted => false})
  end

  def num_unpaid
    unpaid_items.count
  end

  def create_reg_items
    if self.expense_group.competitor_required
      Registrant.competitor.each do |reg|
        reg.build_registration_item(self)
        reg.save
      end
    end
    if self.expense_group.noncompetitor_required
{'fraction_non_alphanumeric': 0.08716829022505879, 'fraction_numerical': 0.006382264024185421, 'mean_word_length': 3.3594436310395315, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '2196863', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1951, 'n_words': 414}