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

  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
  end

  after_create do
    OrderMailer.delay.notify_user_created(self)
    OrderMailer.delay.notify_organizer_created(self)
  end

  def free?
    self.price_in_cents.zero?
  end

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

    state :pending
    state :paid, :request_refund, :complete_refund do
      validates :trade_no, :presence => true
    end

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

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

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

    event :complete_refund do
      transition :request_refund => :complete_refund
    end
  end

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

  #def pending?
  #  self.status.to_sym == :pending
  #end
  #
  #def paid?
  #  self.status.to_sym == :paid
  #end
  #
  #def canceled?
  #  self.status.to_sym == :canceled
  #end
  #
  #def request_refund?
  #  self.status.to_sym == :request_refund
  #end
  #
  #def complete_refund?
  #  self.status.to_sym == :complete_refund
  #end
  #
  #def can_pay?
  #  pending? && !event.finished?
  #end
  #
  #def can_cancel?
  #  pending? && !event.finished?
  #end
  #
  #def can_request_refund?
  #  # TODO: can not refund if attended
  #  paid? && (event.start_time - Time.now > 7.days)
  #end
  #
  #def can_complete_refund?
  #  request_refund?
  #end
  #
  #def pay!(trade_no)
  #  return false unless can_pay?
  #
  #  self.update_attributes status: 'paid', trade_no: trade_no
  #  OrderMailer.delay.notify_user_paid(self)
  #  OrderMailer.delay.notify_organizer_paid(self)
  #end
  #
  #def cancel!
  #  return false unless can_cancel?
  #
  #  self.update_attributes status: 'canceled'
  #  event.increment! :tickets_quantity, self.quantity if event.tickets_quantity
  #end
  #
  #def request_refund!
  #  return false unless can_request_refund?
  #
  #  self.update_attributes status: 'request_refund'
  #  event.increment! :tickets_quantity, self.quantity if event.tickets_quantity
  #end
  #
  #def complete_refund!
  #  return false unless can_complete_refund?
  #
  #  self.update_attributes status: 'complete_refund'
  #end

  def require_invoice
    items.map(&:require_invoice).any?
  end

  class<< self
    def build_order(user, event, params)
      items_attributes = EventOrderItem.filter_attributes(
          event,
          params[:items_attributes]
      )
      order_params = {
          user: user,
          status: :pending,
          items_attributes: items_attributes
      }
      order_params[:shipping_address_attributes] = params[:shipping_address_attributes] if params[:shipping_address_attributes]
      event.orders.build order_params
    end

    # TODO: validate, event and its inventory, #457, #467
    def place_order(user, event, params)
      build_order(user, event, params).tap do |order|
        order.save!
      end
    end
  end

  private<commit_msg>clean up<commit_after>class EventOrder < ActiveRecord::Base
  belongs_to :event
  belongs_to :user
  has_many :items, class_name: 'EventOrderItem', foreign_key: "order_id"
  has_one :shipping_address, class_name: 'EventOrderShippingAddress', foreign_key: "order_id"
  priceable :price
  has_many :event_order_status_transitions

  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
  end

  after_create do
    OrderMailer.delay.notify_user_created(self)
    OrderMailer.delay.notify_organizer_created(self)
  end

  def free?
    self.price_in_cents.zero?
  end

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

    state :pending
    state :paid, :request_refund, :complete_refund do
      validates :trade_no, :presence => true
    end

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

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

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

    event :complete_refund do
      transition :request_refund => :complete_refund
    end
  end

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

  def require_invoice
    items.map(&:require_invoice).any?
  end

  class<< self
    def build_order(user, event, params)
      items_attributes = EventOrderItem.filter_attributes(
          event,
          params[:items_attributes]
      )
      order_params = {
          user: user,
          status: :pending,
          items_attributes: items_attributes
      }
      order_params[:shipping_address_attributes] = params[:shipping_address_attributes] if params[:shipping_address_attributes]
      event.orders.build order_params
    end

    # TODO: validate, event and its inventory, #457, #467
    def place_order(user, event, params)
      build_order(user, event, params).tap do |order|
        order.save!
      end
    end
  end

  private
{'fraction_non_alphanumeric': 0.0947146514079205, 'fraction_numerical': 0.0025598554434573106, 'mean_word_length': 3.4697173620457606, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 30, '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': '5771010', 'n_tokens_mistral': 2255, 'n_tokens_neox': 2174, 'n_words': 490}