2015-04-03 20:34:12 +00:00
|
|
|
module JamRuby
|
|
|
|
|
class SaleLineItem < ActiveRecord::Base
|
|
|
|
|
|
|
|
|
|
JAMBLASTER = 'JamBlaster'
|
|
|
|
|
JAMCLOUD = 'JamCloud'
|
|
|
|
|
JAMTRACK = 'JamTrack'
|
2015-11-29 19:58:10 +00:00
|
|
|
GIFTCARD = 'GiftCardType'
|
2015-04-03 20:34:12 +00:00
|
|
|
|
2015-04-12 18:45:26 +00:00
|
|
|
belongs_to :sale, class_name: 'JamRuby::Sale'
|
|
|
|
|
belongs_to :jam_track, class_name: 'JamRuby::JamTrack'
|
|
|
|
|
belongs_to :jam_track_right, class_name: 'JamRuby::JamTrackRight'
|
2015-11-29 19:58:10 +00:00
|
|
|
belongs_to :gift_card, class_name: 'JamRuby::GiftCard'
|
2015-05-28 13:20:14 +00:00
|
|
|
belongs_to :affiliate_referral, class_name: 'JamRuby::AffiliatePartner', foreign_key: :affiliate_referral_id
|
|
|
|
|
has_many :recurly_transactions, class_name: 'JamRuby::RecurlyTransactionWebHook', inverse_of: :sale_line_item, foreign_key: 'subscription_id', primary_key: 'recurly_subscription_uuid'
|
2015-04-12 18:45:26 +00:00
|
|
|
|
2015-11-29 19:58:10 +00:00
|
|
|
validates :product_type, inclusion: {in: [JAMBLASTER, JAMCLOUD, JAMTRACK, GIFTCARD]}
|
2015-04-03 20:34:12 +00:00
|
|
|
validates :unit_price, numericality: {only_integer: false}
|
|
|
|
|
validates :quantity, numericality: {only_integer: true}
|
|
|
|
|
validates :free, numericality: {only_integer: true}
|
|
|
|
|
validates :sales_tax, numericality: {only_integer: false}, allow_nil: true
|
|
|
|
|
validates :shipping_handling, numericality: {only_integer: false}
|
2015-05-28 13:20:14 +00:00
|
|
|
validates :affiliate_referral_fee_in_cents, numericality: {only_integer: false}, allow_nil: true
|
2015-04-03 20:34:12 +00:00
|
|
|
validates :recurly_plan_code, presence:true
|
|
|
|
|
validates :sale, presence:true
|
|
|
|
|
|
2015-11-29 19:58:10 +00:00
|
|
|
def is_jam_track?
|
|
|
|
|
product_type == JAMTRACK
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def is_gift_card?
|
|
|
|
|
product_type == GIFTCARD
|
|
|
|
|
end
|
|
|
|
|
|
2015-04-10 20:19:08 +00:00
|
|
|
def product
|
2015-04-12 18:45:26 +00:00
|
|
|
if product_type == JAMTRACK
|
|
|
|
|
JamTrack.find_by_id(product_id)
|
2015-11-29 19:58:10 +00:00
|
|
|
elsif product_type == GIFTCARD
|
|
|
|
|
GiftCardType.find_by_id(product_id)
|
2015-04-12 18:45:26 +00:00
|
|
|
else
|
|
|
|
|
raise 'unsupported product type'
|
|
|
|
|
end
|
2015-04-10 20:19:08 +00:00
|
|
|
end
|
|
|
|
|
|
2015-04-12 18:45:26 +00:00
|
|
|
def product_info
|
|
|
|
|
item = product
|
|
|
|
|
{ name: product.name } if item
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def state
|
|
|
|
|
voided = false
|
|
|
|
|
refunded = false
|
|
|
|
|
failed = false
|
|
|
|
|
succeeded = false
|
|
|
|
|
|
|
|
|
|
recurly_transactions.each do |transaction|
|
|
|
|
|
if transaction.transaction_type == RecurlyTransactionWebHook::VOID
|
|
|
|
|
voided = true
|
|
|
|
|
elsif transaction.transaction_type == RecurlyTransactionWebHook::REFUND
|
|
|
|
|
refunded = true
|
|
|
|
|
elsif transaction.transaction_type == RecurlyTransactionWebHook::FAILED_PAYMENT
|
|
|
|
|
failed = true
|
|
|
|
|
elsif transaction.transaction_type == RecurlyTransactionWebHook::SUCCESSFUL_PAYMENT
|
|
|
|
|
succeeded = true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
void: voided,
|
|
|
|
|
refund: refunded,
|
|
|
|
|
fail: failed,
|
|
|
|
|
success: succeeded
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
2015-04-10 20:19:08 +00:00
|
|
|
def self.create_from_shopping_cart(sale, shopping_cart, recurly_subscription_uuid, recurly_adjustment_uuid, recurly_adjustment_credit_uuid)
|
2015-04-03 20:34:12 +00:00
|
|
|
product_info = shopping_cart.product_info
|
|
|
|
|
|
2015-04-10 20:19:08 +00:00
|
|
|
sale.order_total = sale.order_total + product_info[:real_price]
|
2015-04-03 20:34:12 +00:00
|
|
|
|
|
|
|
|
sale_line_item = SaleLineItem.new
|
|
|
|
|
sale_line_item.product_type = shopping_cart.cart_type
|
|
|
|
|
sale_line_item.unit_price = product_info[:price]
|
|
|
|
|
sale_line_item.quantity = product_info[:quantity]
|
|
|
|
|
sale_line_item.free = product_info[:marked_for_redeem]
|
|
|
|
|
sale_line_item.sales_tax = nil
|
|
|
|
|
sale_line_item.shipping_handling = 0
|
|
|
|
|
sale_line_item.recurly_plan_code = product_info[:plan_code]
|
|
|
|
|
sale_line_item.product_id = shopping_cart.cart_id
|
|
|
|
|
sale_line_item.recurly_subscription_uuid = recurly_subscription_uuid
|
2015-04-10 20:19:08 +00:00
|
|
|
sale_line_item.recurly_adjustment_uuid = recurly_adjustment_uuid
|
|
|
|
|
sale_line_item.recurly_adjustment_credit_uuid = recurly_adjustment_credit_uuid
|
2015-05-28 13:20:14 +00:00
|
|
|
|
|
|
|
|
# determine if we need to associate this sale with a partner
|
|
|
|
|
user = shopping_cart.user
|
|
|
|
|
referral_info = user.should_attribute_sale?(shopping_cart)
|
|
|
|
|
|
|
|
|
|
if referral_info
|
|
|
|
|
sale_line_item.affiliate_referral = user.affiliate_referral
|
|
|
|
|
sale_line_item.affiliate_referral_fee_in_cents = referral_info[:fee_in_cents]
|
|
|
|
|
end
|
|
|
|
|
|
2015-04-10 20:19:08 +00:00
|
|
|
sale.sale_line_items << sale_line_item
|
2015-04-03 20:34:12 +00:00
|
|
|
sale_line_item.save
|
|
|
|
|
sale_line_item
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|