summaryrefslogtreecommitdiff
path: root/engines/billing/app/models/customer.rb
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2014-05-16 08:42:36 +0200
committerAzul <azul@leap.se>2014-05-16 08:42:36 +0200
commit8fbbb8717f0578536b97c2dc0883c632f120e976 (patch)
tree17aeb2b48ada703ac916a9a65fbf3c75a5dadb86 /engines/billing/app/models/customer.rb
parent81555ec6244ed76f92e3629880f68104b8705817 (diff)
parenta4f7a410c536d88c91c834cab6ee950c71005ddd (diff)
Merge remote-tracking branch 'origin/develop'
Conflicts: app/assets/javascripts/srp test/nagios/soledad_sync.py test/nagios/webapp_login.py
Diffstat (limited to 'engines/billing/app/models/customer.rb')
-rw-r--r--engines/billing/app/models/customer.rb58
1 files changed, 58 insertions, 0 deletions
diff --git a/engines/billing/app/models/customer.rb b/engines/billing/app/models/customer.rb
new file mode 100644
index 0000000..1acc7a5
--- /dev/null
+++ b/engines/billing/app/models/customer.rb
@@ -0,0 +1,58 @@
+class Customer < CouchRest::Model::Base
+
+ FIELDS = [:first_name, :last_name, :phone, :website, :company, :fax, :addresses, :credit_cards, :custom_fields]
+ attr_accessor *FIELDS
+
+ use_database "customers"
+ belongs_to :user
+ belongs_to :braintree_customer
+
+ # Braintree::Customer - stored on braintrees servers - we only have the id.
+ def braintree_customer
+ @braintree_customer ||= Braintree::Customer.find(braintree_customer_id)
+ end
+
+ validates :user, presence: true
+
+ design do
+ view :by_user_id
+ view :by_braintree_customer_id
+ end
+
+ def has_payment_info?
+ !!braintree_customer_id
+ end
+
+ # from braintree_ruby_examples/rails3_tr_devise and should be tweaked
+ def with_braintree_data!
+ return self unless has_payment_info?
+
+ FIELDS.each do |field|
+ send(:"#{field}=", braintree_customer.send(field))
+ end
+ self
+ end
+
+ def default_credit_card
+ return unless has_payment_info?
+
+ credit_cards.find { |cc| cc.default? }
+ end
+
+ # based on 2nd parameter, either returns the single active subscription (or nil if there isn't one), or an array of all subsciptions
+ def subscriptions(braintree_data=nil, only_pending_active_pastdue=true)
+ self.with_braintree_data!
+ return unless has_payment_info?
+
+ subscriptions = []
+ self.default_credit_card.subscriptions.each do |sub|
+ if only_pending_active_pastdue and ['Pending', 'Active','Past Due'].include? sub.status
+ return sub
+ else
+ subscriptions << sub
+ end
+ end
+ only_pending_active_pastdue ? nil : subscriptions
+ end
+
+end