Permalink
Browse files

Adding credit card limit field so that current balances can be colore…

…d according to their current amount used. Defaults are low (0% or near), medium (30%), high (80%), and critical (100%).
  • Loading branch information...
1 parent 6ba02ee commit a42095f712e7fc160e30bc544a8413263568e2d3 Kieran Pilkington committed with May 16, 2009
@@ -12,6 +12,16 @@ def balance_cell(container, options={})
classes = %w(number)
classes += Array(options[:classes]) if options[:classes]
classes << "negative" if balance < 0
+ classes << "current_balance"
+
+ if container.is_a?(Account) && container.credit_card?
+ percentage_used = container.limit.abs.to_i == 0 ? 100 :
+ ((container.balance.abs.to_f / container.limit.abs.to_f) * 100).to_i
+ classes << if percentage_used >= Account::DEFAULT_LIMIT_VALUES[:critical]: "critical"
+ elsif percentage_used >= Account::DEFAULT_LIMIT_VALUES[:high]: "high"
+ elsif percentage_used >= Account::DEFAULT_LIMIT_VALUES[:medium]: "medium"
+ else "low" end
+ end
content = format_amount(balance)
if real_balance != balance
View
@@ -1,13 +1,22 @@
class Account < ActiveRecord::Base
DEFAULT_BUCKET_NAME = "General"
+ # When should the levels of credit cards be reached (in %)
+ DEFAULT_LIMIT_VALUES = {
+ :critical => 100,
+ :high => 80,
+ :medium => 30,
+ :low => 0
+ }
+
belongs_to :subscription
belongs_to :author, :class_name => "User", :foreign_key => "user_id"
attr_accessor :starting_balance
- attr_accessible :name, :role, :starting_balance
+ attr_accessible :name, :role, :limit, :starting_balance
validates_presence_of :name
+ validates_presence_of :limit, :if => :credit_card?
validates_uniqueness_of :name, :scope => :subscription_id, :case_sensitive => false
has_many :buckets do
@@ -18,7 +18,12 @@
%p
%label
<strong>What kind</strong> of account is this?
- = form.select :role, [["Checking", "checking"], ["Credit card", "credit-card"], ["Other", "other"]]
+ = form.select :role, [["Checking", "checking"], ["Credit card", "credit-card"], ["Other", "other"]], {}, :onchange => "Accounts.showOrHideCreditLimit(this.value);"
+
+ %p{:style => 'display: none;', :id => 'credit_limit_div'}
+ %label
+ What is the <strong>credit limit</strong>:
+ == $#{form.text_field :limit, :class => "number", :size => 8}
- if form.object.nil? || form.object.new_record?
%fieldset
@@ -1,4 +1,7 @@
%span.actions
+ - if account.credit_card?
+ = link_to_function("Adjust Limit", "Accounts.adjustLimit(#{account_path(account).to_json}, #{account.limit.to_json}, #{form_authenticity_token.to_json})")
+ |
- if account.statements.pending.any?
= link_to("Resume reconciling", edit_statement_path(account.statements.pending.first))
- else
@@ -0,0 +1,9 @@
+class AddLimitToAccounts < ActiveRecord::Migration
+ def self.up
+ add_column :accounts, :limit, :integer
+ end
+
+ def self.down
+ remove_column :accounts, :limit
+ end
+end
View
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20090506161959) do
+ActiveRecord::Schema.define(:version => 20090516072907) do
create_table "account_items", :force => true do |t|
t.integer "event_id", :null => false
@@ -31,6 +31,7 @@
t.datetime "created_at"
t.datetime "updated_at"
t.integer "balance", :default => 0, :null => false
+ t.integer "limit"
end
add_index "accounts", ["subscription_id", "name"], :name => "index_accounts_on_subscription_id_and_name", :unique => true
@@ -34,9 +34,18 @@ var Accounts = {
return false;
}
+ if($F('account_role') == 'credit-card' && $F('account_limit').blank()) {
+ $('account_name').activate();
+ alert('Please provide a limit for the account.');
+ return false;
+ }
+
var balance = Money.parse('current_balance', true);
$('account_starting_balance_amount').value = balance;
+ var limit = Money.parse('account_limit', true);
+ $('account_limit').value = limit;
+
return true;
},
@@ -57,5 +66,43 @@ var Accounts = {
parameters:params
});
}
+ },
+
+ adjustLimit: function(url, limit, token) {
+ new_limit = prompt("Enter the new limit for this account:", Money.formatValue(limit));
+ new_limit = Money.parseValue(new_limit);
+ while(new_limit == '') {
+ new_limit = prompt("Cannot have have a blank limit. Please re-enter it:", Money.formatValue(limit));
+ }
+ if(new_limit && new_limit != limit) {
+ params = encodeURIComponent("account[limit]") + "=" + encodeURIComponent(new_limit) +
+ "&authenticity_token=" + encodeURIComponent(token);
+
+ new Ajax.Request(url, {
+ asynchronous:true,
+ evalScripts:true,
+ method:'put',
+ parameters:params,
+ onSuccess: function(request) {
+ window.location.reload();
+ }
+ });
+ }
+ },
+
+ showOrHideCreditLimit: function(value) {
+ if (value == 'credit-card') {
+ Accounts.showCreditLimit();
+ } else {
+ Accounts.hideCreditLimit();
+ }
+ },
+
+ showCreditLimit: function() {
+ $('credit_limit_div').show();
+ },
+
+ hideCreditLimit: function() {
+ $('credit_limit_div').hide();
}
}
@@ -266,6 +266,10 @@ td.number, th.number {
white-space: nowrap;
}
+td.current_balance, th.current_balance {
+ color: #999;
+}
+
span.real_balance {
font-size: 80%;
color: #999;
@@ -280,6 +284,22 @@ span.check {
color: red;
}
+.critical {
+ color: red !important;
+}
+
+.high {
+ color: brown !important;
+}
+
+.medium {
+ color: sandybrown !important;
+}
+
+.low {
+ color: green !important;
+}
+
td.date, th.date {
width: 1px;
padding-left: 0.3em;
@@ -17,6 +17,7 @@ john_mastercard:
name: Mastercard
role: credit-card
balance: -2525
+ limit: 5000
created_at: <%= (60.days.ago + 2.hours).utc.to_s(:db) %>
updated_at: <%= (60.days.ago + 2.hours).utc.to_s(:db) %>
@@ -141,7 +141,8 @@ def new_account(options={})
subscription = options.delete(:subscription) || subscriptions(:john)
options = {:name => "Visa",
- :role => "credit-card"}.merge(options)
+ :role => "credit-card",
+ :limit => 5000}.merge(options)
subscription.accounts.create(options, :author => users(:john))
end

0 comments on commit a42095f

Please sign in to comment.