<commit_before>module Effective
  class OrderItem < ActiveRecord::Base
    self.table_name = EffectiveOrders.order_items_table_name.to_s

    belongs_to :order
    belongs_to :purchasable, :polymorphic => true
    belongs_to :seller, :class_name => 'User'

    delegate :purchased_download_url, :to => :purchasable
    delegate :purchased?, :declined?, :to => :order

    structure do
      title                 :string
      quantity              :integer
      price                 :integer, default: 0
      tax_exempt            :boolean

      timestamps
    end

    validates :purchasable, associated: true, presence: true
    accepts_nested_attributes_for :purchasable, allow_destroy: false, reject_if: :all_blank, update_only: true

    validates :title, presence: true
    validates :quantity, presence: true, numericality: { greater_than: 0 }
    validates :price, numericality: true
    validates :tax_exempt, inclusion: { in: [true, false] }

    validates :seller_id, presence: true, if: Proc.new { |order_item| EffectiveOrders.stripe_connect_enabled }

    scope :sold, -> { joins(:order).where(:orders => {:purchase_state => EffectiveOrders::PURCHASED}) }
    scope :sold_by, lambda { |user| sold().where(:seller_id => user.try(:id)) }

    def to_s
      (quantity || 0) > 1 ? "#{quantity}x #{title}" : title
    end

    def subtotal
      price * quantity
    end

    def total
      return subtotal if tax_exempt?
      raise 'parent Effective::Order must have a tax_rate to compute order item total' unless order.try(:tax_rate).present?

      (subtotal + (subtotal * order.tax_rate / 100.0)).round(0).to_i
    end

    def price=(value)
      if value.kind_of?(Integer)
        super
      elsif value.kind_of?(String) && !value.include?('.') # Looks like an integer
        super
      else # Could be Float, BigDecimal, or String like 9.99
        ActiveSupport::Deprecation.warn('order_item.price= was passed a non-integer. Expecting an Integer representing the number of cents.  Continuing with (price * 100.0).round(0).to_i conversion') unless EffectiveOrders.silence_deprecation_warnings
        super((value.to_f * 100.0).to_i)
      end
    end

    # This is going to return an Effective::Customer object that matches the purchasable.user
    # And is the Customer representing who is selling the product
    # This is really only used for StripeConnect
    def seller
      @seller ||= Effective::Customer.for_user(purchasable.try(:seller))<commit_msg>Add tax method back to order_item based on order.tax_rate<commit_after>module Effective
  class OrderItem < ActiveRecord::Base
    self.table_name = EffectiveOrders.order_items_table_name.to_s

    belongs_to :order
    belongs_to :purchasable, :polymorphic => true
    belongs_to :seller, :class_name => 'User'

    delegate :purchased_download_url, :to => :purchasable
    delegate :purchased?, :declined?, :to => :order

    structure do
      title                 :string
      quantity              :integer
      price                 :integer, default: 0
      tax_exempt            :boolean

      timestamps
    end

    validates :purchasable, associated: true, presence: true
    accepts_nested_attributes_for :purchasable, allow_destroy: false, reject_if: :all_blank, update_only: true

    validates :title, presence: true
    validates :quantity, presence: true, numericality: { greater_than: 0 }
    validates :price, numericality: true
    validates :tax_exempt, inclusion: { in: [true, false] }

    validates :seller_id, presence: true, if: Proc.new { |order_item| EffectiveOrders.stripe_connect_enabled }

    scope :sold, -> { joins(:order).where(:orders => {:purchase_state => EffectiveOrders::PURCHASED}) }
    scope :sold_by, lambda { |user| sold().where(:seller_id => user.try(:id)) }

    def to_s
      (quantity || 0) > 1 ? "#{quantity}x #{title}" : title
    end

    def subtotal
      price * quantity
    end

    def tax
      return 0 if tax_exempt?
      raise 'parent Effective::Order must have a tax_rate to compute order item tax' unless order.try(:tax_rate).present?
      (subtotal * order.tax_rate / 100.0).round(0).to_i
    end

    def total
      return subtotal if tax_exempt?
      raise 'parent Effective::Order must have a tax_rate to compute order item total' unless order.try(:tax_rate).present?
      subtotal + tax
    end

    def price=(value)
      if value.kind_of?(Integer)
        super
      elsif value.kind_of?(String) && !value.include?('.') # Looks like an integer
        super
      else # Could be Float, BigDecimal, or String like 9.99
        ActiveSupport::Deprecation.warn('order_item.price= was passed a non-integer. Expecting an Integer representing the number of cents.  Continuing with (price * 100.0).round(0).to_i conversion') unless EffectiveOrders.silence_deprecation_warnings
        super((value.to_f * 100.0).to_i)
      end
    end

    # This is going to return an Effective::Customer object that matches the purchasable.user
    # And is the Customer representing who is selling the product
    # This is really only used for StripeConnect
    def seller
      @seller ||= Effective::Customer.for_user(purchasable.try(:seller))
{'fraction_non_alphanumeric': 0.09657560600230858, 'fraction_numerical': 0.008272412466333205, 'mean_word_length': 3.1592, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4606265', 'n_tokens_mistral': 1617, 'n_tokens_neox': 1534, 'n_words': 453}