/app/models/lead.rb

https://github.com/jdhollis/renewzle · Ruby · 141 lines · 117 code · 24 blank · 0 comment · 1 complexity · da70ab408d3bb81097e9a26a01a58d9a MD5 · raw file

  1. class Lead < ActiveRecord::Base
  2. acts_as_soft_deletable
  3. has_one :profile
  4. has_one :solution, :dependent => :destroy
  5. belongs_to :quote
  6. belongs_to :partner
  7. named_scope :recent, :order => 'created_at DESC', :limit => 20
  8. composed_of :selling_price, :class_name => 'Money', :mapping => [ %w(selling_price_cents cents), %w(selling_price_currency currency) ]
  9. validates_presence_of :quote_id
  10. delegate :company, :to => :partner
  11. delegate :city, :to => :profile
  12. delegate :customer, :to => :profile
  13. delegate :nameplate_rating, :to => :quote
  14. delegate :cec_rating, :to => :quote
  15. delegate :postal_code, :to => :quote
  16. delegate :total_price, :to => :quote
  17. attr_accessor :billing_name, :billing_email, :billing_phone_number, :billing_street_address, :billing_city, :billing_state, :billing_postal_code, :billing_country
  18. attr_accessor :card_first_name, :card_last_name, :card_type, :card_number, :card_expiration_month, :card_expiration_year, :card_verification
  19. attr_protected :confirmation_number, :authorization, :ip_address, :purchased
  20. before_destroy do |record|
  21. record.profile.mark_as_second_chance! unless record.profile.second_chance?
  22. end
  23. def self.find_purchased_by_partner(lead_id, partner_id)
  24. unless lead = Lead.find_by_id_and_purchased_and_partner_id(lead_id, true, partner_id)
  25. raise ActiveRecord::RecordNotFound
  26. end
  27. return lead
  28. end
  29. def purchased?
  30. purchased
  31. end
  32. def purchase_price
  33. price = quote.lead_purchase_price
  34. global_discounts.each { |discount| price = discount.apply_to(price, self) }
  35. partner.lead_price_given(price, self)
  36. end
  37. def companys_first_lead?
  38. !company.has_purchased_leads?
  39. end
  40. def set_billing_information_from(user)
  41. self.billing_name = user.full_name
  42. self.billing_email = user.email
  43. self.billing_phone_number = user.phone_number
  44. self.billing_street_address = user.street_address
  45. self.billing_city = user.city
  46. self.billing_state = user.state
  47. self.billing_postal_code = user.postal_code
  48. self.billing_country = 'United States'
  49. end
  50. def update_profile_address_with(params)
  51. profile.attributes = params
  52. transaction do
  53. profile.save!
  54. save!
  55. end
  56. end
  57. def purchase!
  58. credit_card = ActiveMerchant::Billing::CreditCard.new(
  59. :first_name => card_first_name,
  60. :last_name => card_last_name,
  61. :type => card_type,
  62. :number => card_number,
  63. :verification_value => card_verification,
  64. :month => card_expiration_month,
  65. :year => card_expiration_year
  66. )
  67. unless credit_card.valid?
  68. credit_card.errors.each do |key, message|
  69. case key
  70. when :verification_value
  71. errors.add(:card_verification, message)
  72. when :month
  73. errors.add(:card_expiration_month, message)
  74. when :year
  75. errors.add(:card_expiration_year, message)
  76. else
  77. errors.add("card_#{key}".to_sym, message)
  78. end
  79. end
  80. raise Renewzle::CreditCardInvalid, 'Credit card invalid'
  81. end
  82. options = {
  83. :name => billing_name,
  84. :email => billing_email,
  85. :phone => billing_phone_number,
  86. :ip_address => ip_address,
  87. :card_code => card_verification,
  88. :billing_address => {
  89. :name => billing_name,
  90. :address1 => billing_street_address,
  91. :city => billing_city,
  92. :state => billing_state,
  93. :zip => billing_postal_code,
  94. :country => billing_country,
  95. :phone => billing_phone_number
  96. }
  97. }
  98. amount = purchase_price.to_money # discounts aren't calculated using Money (for some reason)
  99. response = $gateway.authorize(amount, credit_card, options)
  100. raise Renewzle::PaymentAuthorizationFailed, response.message unless response.success?
  101. $gateway.capture(amount, response.authorization)
  102. self.authorization = '1234' #response.authorization
  103. self.purchased = true
  104. self.selling_price = amount
  105. self.confirmation_number = Time.now.strftime("%Y%m%d") + "-" + Digest::MD5.hexdigest(authorization).gsub(/[a-z]/, '')[0..8]
  106. save!
  107. LeadNotifier.deliver_purchase_confirmation_to_partner_for(self)
  108. end
  109. private
  110. attr_accessor :cached_global_discounts
  111. def global_discounts
  112. if cached_global_discounts.blank?
  113. self.cached_global_discounts = Discount.global
  114. end
  115. cached_global_discounts
  116. end
  117. end