<commit_before>class EventOrder < ActiveRecord::Base
  belongs_to :event
  belongs_to :user
  has_many :event_order_status_transitions
  has_many :items, class_name: 'EventOrderItem', foreign_key: "order_id"
  has_many :refunds, class_name: 'EventOrderRefund', foreign_key: "order_id"
  has_one :participant     , class_name: 'EventOrderParticipant'    , foreign_key: "order_id"
  has_one :shipping_address, class_name: 'EventOrderShippingAddress', foreign_key: "order_id"
  priceable :price, :paid_amount

  accepts_nested_attributes_for :items, :shipping_address

  validates :event_id, :user_id, presence: true
  validates :quantity, presence: true, numericality: { greater_than: 0 }
  validate :quantity_cannot_be_greater_than_event_quantity, :invoice_should_has_address, on: :create

  before_validation do
    self.quantity = calculate_quantity
    self.price_in_cents = calculate_price_in_cents
  end

  before_create do
    self.status = free? ? 'paid' : 'pending'
    event.decrement! :tickets_quantity, self.quantity if event.tickets_quantity
    self.number = Sequence.get
  end

  after_create do
    OrderMailer.delay.notify_user_created(self)
    OrderMailer.delay.notify_organizer_created(self)
    user.follow event.group
  end

  def free?
    self.price_in_cents.zero?
  end

  def status_name
    I18n.t(self.status, scope: 'activerecord.state_machines.event_order.states')
  end

  state_machine :status, :initial => :pending do
    store_audit_trail

    state :pending, :canceled, :closed
    state :refund_pending, :refunded do
      validates :trade_no, :presence => true
    end

    event :pay do
      transition :pending => :paid, :if => ->(order) { !order.event.finished? }
    end

    event :cancel do # use order.cancel instead of order.cancel! if you don't want to raise error when order has been finished.
      transition  [:pending, :paid] => :canceled, :if => ->(order) { !order.event.finished? }
    end

    event :request_refund do
      transition :paid => :refund_pending, :if => ->(order) { order.event.start_time - Time.now > 7.days }
    end

    event :complete_refund do
      transition :refund_pending => :refunded
    end

    event :close do
      transition :pending => :closed
    end
  end

  # override state_machine generated method to accept trade_no
  def pay(trade_no = nil)
    self.trade_no = trade_no
    super
  end

  def pay_with_bank_transfer?
    self.trade_no.blank? # for now, only bank transfer was supported.
  end

  def require_invoice<commit_msg>request refund order can be cancel.<commit_after>class EventOrder < ActiveRecord::Base
  belongs_to :event
  belongs_to :user
  has_many :event_order_status_transitions
  has_many :items, class_name: 'EventOrderItem', foreign_key: "order_id"
  has_many :refunds, class_name: 'EventOrderRefund', foreign_key: "order_id"
  has_one :participant     , class_name: 'EventOrderParticipant'    , foreign_key: "order_id"
  has_one :shipping_address, class_name: 'EventOrderShippingAddress', foreign_key: "order_id"
  priceable :price, :paid_amount

  accepts_nested_attributes_for :items, :shipping_address

  validates :event_id, :user_id, presence: true
  validates :quantity, presence: true, numericality: { greater_than: 0 }
  validate :quantity_cannot_be_greater_than_event_quantity, :invoice_should_has_address, on: :create

  before_validation do
    self.quantity = calculate_quantity
    self.price_in_cents = calculate_price_in_cents
  end

  before_create do
    self.status = free? ? 'paid' : 'pending'
    event.decrement! :tickets_quantity, self.quantity if event.tickets_quantity
    self.number = Sequence.get
  end

  after_create do
    OrderMailer.delay.notify_user_created(self)
    OrderMailer.delay.notify_organizer_created(self)
    user.follow event.group
  end

  def free?
    self.price_in_cents.zero?
  end

  def status_name
    I18n.t(self.status, scope: 'activerecord.state_machines.event_order.states')
  end

  state_machine :status, :initial => :pending do
    store_audit_trail

    state :pending, :canceled, :closed
    state :refund_pending, :refunded do
      validates :trade_no, :presence => true
    end

    event :pay do
      transition :pending => :paid, :if => ->(order) { !order.event.finished? }
    end

    event :cancel do # use order.cancel instead of order.cancel! if you don't want to raise error when order has been finished.
      transition  [:pending, :paid, :refund_pending] => :canceled, :if => ->(order) { !order.event.finished? }
    end

    event :request_refund do
      transition :paid => :refund_pending, :if => ->(order) { order.event.start_time - Time.now > 7.days }
    end

    event :complete_refund do
      transition :refund_pending => :refunded
    end

    event :close do
      transition :pending => :closed
    end
  end

  # override state_machine generated method to accept trade_no
  def pay(trade_no = nil)
    self.trade_no = trade_no
    super
  end

  def pay_with_bank_transfer?
    self.trade_no.blank? # for now, only bank transfer was supported.
  end

  def require_invoice
{'fraction_non_alphanumeric': 0.09037212049616067, 'fraction_numerical': 0.0015751132112620594, 'mean_word_length': 3.9706457925636007, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '6854594', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1619, 'n_words': 415}