Permalink
Browse files

caching enabled on loans

  • Loading branch information...
1 parent 51120fe commit 8994fe0af1198878e652c8324e9f857a5b82648a Siddharth committed Jul 20, 2011
@@ -64,7 +64,9 @@ def update(id, client)
@client = Client.get(id)
raise NotFound unless @client
disallow_updation_of_verified_clients
- if @client.update_attributes(client)
+ debugger
+ @client.update_attributes(client)
+ if @client.errors.blank?
if params[:tags]
@client.tags = params[:tags].keys.map{|k| k.to_sym}
else
View
@@ -11,6 +11,7 @@ class Client
before :valid?, :add_created_by_staff_member
after :save, :check_client_deceased
after :save, :levy_fees
+ after :save, :update_loan_cache
property :id, Serial
property :reference, String, :length => 100, :nullable => false, :index => true
@@ -127,6 +128,10 @@ class Client
validates_with_method :date_joined, :method => :dates_make_sense
validates_with_method :inactive_reason, :method => :cannot_have_inactive_reason_if_active
+ def update_loan_cache
+ loans.each{|l| l.update_loan_cache(true); l.save}
+ end
+
def self.from_csv(row, headers)
if center_attr = row[headers[:center]].strip
if center = Center.first(:name => center_attr)
@@ -14,19 +14,19 @@ def self.get_object_state(obj, type)
def self.log(obj)
f = File.open("log/#{obj.class}.log","a")
+ debugger
begin
if obj
attributes = obj.attributes
if @ributes
- diff = @ributes.diff(attributes)
+ diff = @ributes.diff(attributes).reject{|x| x.to_s.match(/^c_/)} # reject the caching properties, defined by c_xxxx
diff = diff.map{|k|
{k => [@ributes[k],attributes[k]]} if k != :updated_at and not (@ributes[k].nil? and attributes[k].class==String and attributes[k].blank?)
}
diff=diff.compact
else
diff = [attributes.select{|k, v| v and not v.blank? and not v==0}.to_hash]
end
-
if diff.length>0 and diff.find{|x| x.keys.include?(:discriminator)}
index = diff.index(diff.find{|x| x.keys.include?(:discriminator)})
diff[index][:discriminator] = diff[index][:discriminator].map{|x| x.to_s if x}
View
@@ -12,6 +12,7 @@ class Loan
after :save, :update_history_caller # also seems to do updates
after :create, :levy_fees_new
after :save, :levy_fees
+ before :save, :update_loan_cache
after :create, :update_cycle_number
before :destroy, :verified_cannot_be_deleted
# after :destroy, :update_history
@@ -170,6 +171,30 @@ class Loan
validates_with_method :clients, :method => :check_client_sincerity
validates_with_method :insurance_policy, :method => :check_insurance_policy
+
+ def update_loan_cache(force = false)
+ t = Time.now
+ self.c_center_id = self.client.center.id if force
+ self.c_branch_id = self.client.center.branch.id if force
+ self.c_scheduled_maturity_date = scheduled_maturity_date
+ # avoid SQL calls
+ first_payment = payments.select{|p| [:prinicpal, :interest].include?(p.type)}.sort_by{|p| p.received_on}[0]
+ self.c_actual_first_payment_date = first_payment.received_on if first_payment
+ st = self.get_status
+ self.c_last_status = st
+ self.c_principal_received = payments.select{|p| p.type == :principal}.reduce(0){|s,p| s + p.amount}
+ self.c_interest_received = payments.select{|p| p.type == :principal}.reduce(0){|s,p| s + p.amount}
+ last_payment = payments.select{|p| [:prinicpal, :interest].include?(p.type)}.sort_by{|p| p.received_on}.reverse[0]
+ self.c_last_payment_received_on = last_payment.received_on if last_payment
+ self.c_maturity_date = c_last_payment_received_on if (STATUSES.index(st) > 5 and last_payment)
+ self.c_last_payment_id = last_payment.id if last_payment
+ puts Time.now - t
+ true
+ end
+
+
+
+
def self.display_name
"Loan"
end
@@ -476,6 +501,7 @@ def make_payments(payments, context = :default, defer_update = false)
self.reload if payments.map{|p| p.received_on}.map{|d| installment_dates.include?(d)}.include?(false)
update_history(true) # update the history if we saved a payment
end
+ update_loan_cache
if payments.length > 0
return [true, payments.find{|p| p.type==:principal}, payments.find{|p| p.type==:interest}, payments.find_all{|p| p.type==:fees}]
else
@@ -490,6 +516,7 @@ def delete_payment(payment, user)
payment.deleted_by = user
if payment.destroy
update_history
+ update_loan_cache
clear_cache
return [true, payment]
end
@@ -39,4 +39,6 @@
%button{:type => "submit", :class => "add"}
= submit_text
  
+%b or
+  
= link_to 'cancel', cancel_url
@@ -41,7 +41,7 @@
%tr
%td At center
%td
- = link_to @center.name, resource(@branch, @center)
+ = link_to @client.center.name, resource(@client.center)
== (of branch #{link_to @branch.name, resource(@branch)})
%br/
%span.greytext
@@ -1,70 +1,98 @@
-= form_for(:loan, :class => "_remote_", :action => url(:controller => :loans, :action => :update_utilization, :id => @loan.id), :method => "POST") do
- %div.box
- %h3 Loan Utilization
- = select :loan_utilization_id, :collection => LoanUtilization.all.map{|lu| [lu.id, lu.name]}, :include_blank => true
- = submit "Save"
-
-- if @loan.status == :outstanding
+%div{:style => "float: left; width: 55%"}
.box
- %hr
- %h3 Prepay this loan
- %p
- = link_to("Calculate prepayment amount", url(:prepay_loan, @loan.id), :class => "_remote_")
+ %fieldset
+ %legend Loan Utilization
+ = form_for(:loan, :class => "_remote_", :action => url(:controller => :loans, :action => :update_utilization, :id => @loan.id), :method => "POST") do
+ %h3 Update Loan Utilization
+ = select :loan_utilization_id, :collection => LoanUtilization.all.map{|lu| [lu.id, lu.name]}, :include_blank => true
+ = submit "Save"
+
+
+ - if @loan.status == :outstanding
+ .box
+ %fieldset
+ %legend Pay
+ = link_to("Prepay This Loan", url(:prepay_loan, @loan.id), :class => "_remote_")
+
+ - if [:admin, :mis_manager].include?(session.user.role)
+ .box
+ %fieldset
+ %legend Levy Penalty/Fee
+ = partial "applicable_fees/form", :layout => false, :object => @loan
+
+ - if session.user.role==:admin
+ .box
+ %fieldset
+ %legend Write Off Loan
+ = form_for(@loan, :action => url(:action => :write_off, :id => @loan.id), :method => "POST", :class => "_disable_button_") do
+ %table.tall.shaded
+ %tr
+ %th
+ Write off Date :
+ %td
+ = date_select_for @loan, :written_off_on
+ %tr
+ %th
+ Select Staff Member :
+ %td
+ - center = @client.center
+ - branch = center.branch
+ = select :written_off_by_staff_id, :id => "written_off_by_staff_id", :collection => [branch.manager, center.manager].uniq.map{|m| [m.id, m.name]}, :prompt => "select staff member"
+ %tr
+ %td{:colspan => "2"}
+ %span.greytext
+ Pick the staff member who is writing off the loan
+ = submit "Write off"
+ - elsif staff_member = session.user.staff_member
+ = form_for(@loan, :action => url(:action => :suggest_write_off, :id => @loan.id), :method => "POST", :class => "_disable_button_") do
+ %table
+ %tr
+ %td
+ %b Suggest to Write Off this Loan
+ %tr
+ %td
+ suggested write off Date :
+ %td
+ = date_select_for @loan, :suggested_written_off_on
+ %tr
+ %td
+ Suggested by staff member :
+ %td
+ = staff_member.name
+ %td
+ %tr
+ %td
+ = submit "Send suggestion"
-- if [:admin, :mis_manager].include?(session.user.role)
- %hr
- .box
- = partial "applicable_fees/form", :layout => false, :object => @loan
- %hr
+%div{:style => "float: left; width: 40%"}
.box
- %h3 Applicable fees
- - if @loan.applicable_fees.empty?
- = form_for(@loan, :action => url(:controller => :loans, :action => :levy_fees, :id => @loan.id)) do
- = submit 'Apply Fees'
- = partial "applicable_fees/list", :layout => false, :applicable_fees => @loan.applicable_fees, :return_url => url_for_loan(@loan) + "#misc"
-- if session.user.role==:admin
- %hr
+ %fieldset
+ %legend Applicable fees
+ - if @loan.applicable_fees.empty?
+ = form_for(@loan, :action => url(:controller => 'loans', :action => 'levy_fees', :id => @loan.id)) do
+ = submit 'Apply Fees'
+ = partial "applicable_fees/list", :layout => false, :applicable_fees => @loan.applicable_fees, :return_url => url_for_loan(@loan) + "#misc"
.box
- %h3 Write Off Loan
- = form_for(@loan, :action => url(:action => :write_off, :id => @loan.id), :method => "POST", :class => "_disable_button_") do
- %table
+ %fieldset
+ %legend Values
+ %table.tall
%tr
+ %th
+ Branch
%td
- Write off Date :
- %td
- = date_select_for @loan, :written_off_on
+ = @loan.c_branch_id
%tr
+ %th
+ Center
%td
- Select Staff Member :
+ = @loan.c_center_id
+ %tr
+ %th
+ Scheduled Maturity Date
%td
- - center = @client.center
- - branch = center.branch
- = select :written_off_by_staff_id, :id => "written_off_by_staff_id", :collection => [branch.manager, center.manager].uniq.map{|m| [m.id, m.name]}, :prompt => "select staff member"
+ = @loan.c_scheduled_maturity_date
%tr
- %td{:colspan => "2"}
- %span.greytext
- Pick the staff member who is writing off the loan
- %tr
- %td
- = submit "Write off"
-- elsif staff_member = session.user.staff_member
- = form_for(@loan, :action => url(:action => :suggest_write_off, :id => @loan.id), :method => "POST", :class => "_disable_button_") do
- %table
- %tr
- %td
- %b Suggest to Write Off this Loan
- %tr
- %td
- suggested write off Date :
- %td
- = date_select_for @loan, :suggested_written_off_on
- %tr
- %td
- Suggested by staff member :
- %td
- = staff_member.name
- %td
- %tr
- %td
- = submit "Send suggestion"
-
+ %th
+ Branch
+ %td
+ = @loan.c_branch_id
@@ -1,6 +1,6 @@
.shaded
- if @loan and @loan.disbursal_date
- .graph
+ .graph{:style => "position: relative; top: 60px"}
= ofc2(550, 430, 'http://' + (request.env['HTTP_HOST'] or 'example.org') + url(:graph_data, :action => 'loan', :id => @loan.id, :scope_unit => 'months', :scope_size => 3) )
= error_messages_for @payment
@@ -144,7 +144,7 @@
.shaded
- %div.tab_container
+ %div.tab_container.clearfix
%ul.tabs
%li#repayment_schedule Repayment Schedule
%li#repayments_made Repayments made
@@ -657,7 +657,7 @@ function fillBankAccounts(){
$("#bank_account_selector").html(data);
}
});
- });
+ });
}
}
@@ -756,7 +756,6 @@ $(document).ready(function(){
fillFundingLines();
fillCashAccounts();
fillBankAccounts();
- $('form').highlight();
//Handling targets form
$("select#target_attached_to").change(function(){
$.ajax({
Oops, something went wrong.

0 comments on commit 8994fe0

Please sign in to comment.