Skip to content

Instantly share code, notes, and snippets.

@bmsolutions
Created March 16, 2011 16:47
Show Gist options
  • Save bmsolutions/872803 to your computer and use it in GitHub Desktop.
Save bmsolutions/872803 to your computer and use it in GitHub Desktop.
Merchant Show
<%= form_for(cashadvance) do |f| %>
<fieldset id='current_advance'>
<legend>Cash Advance</legend>
<p>
<b>Start Date:</b>
<%=h cashadvance.start_date.strftime("%m/%d/%Y") %>
</p>
<p>
<b>Expected Duration:</b>
<%=h "#{cashadvance.expected_duration} months" %>
</p>
<p>
<b>Split Percentage:</b>
<%=h number_to_percentage cashadvance.split_percentage %>
</p>
<p>
<b>Advance Amount:</b>
<%=h number_to_currency cashadvance.advance_amount %>
</p>
<p>
<b>Factor Rate:</b>
<%=h number_to_percentage cashadvance.factor_rate %>
</p>
<p>
<b>Payback Amount:</b>
<%=h number_to_currency cashadvance.payback_amount %>
</p>
<p>
<b>Amount Paid:</b>
<%=h number_to_currency cashadvance.amount_paid %>
</p>
<p>
<b>Percentage Paid:</b>
<%=h number_to_percentage cashadvance.percentage_paid %>
</p>
<p>
<b>Amount Owed:</b>
<%=h number_to_currency cashadvance.amount_owed %>
</p>
<p>
<%= link_to 'Edit Cash Advance', edit_cashadvance_path(cashadvance) %> |
<%= link_to 'Back', merchants_path %>
</p>
<p>
<%= submit_tag 'Create Refi', :id => "refi_button", :class => "renewal_button" %>
<%= submit_tag 'Create Reup', :id => "reup_button", :class => "renewal_button" %>
</p>
</fieldset>
<fieldset id='payments'>
<legend>Payments</legend>
<%= link_to "Add Payment", new_cashadvance_payment_path(cashadvance) %>
<table>
<tr>
<th>Date</th>
<th>Amount</th>
<th>Manual Release</th>
<th></th>
<th></th>
</tr>
<% cashadvance.payments.each do |pay| %>
<tr>
<td><%=h pay.created_at.strftime("%m/%d/%Y") %></td>
<td><%=h number_to_currency pay.amount %></td>
<td><%= pay.manual_release? ? "YES" : "NO" %></td>
<td><%=h pay.note %></td>
<td><%= link_to "Edit Payment", edit_payment_path(pay) %></td>
<td>
<%= link_to "Delete Payment", payment_path(pay), :confirm =>
'Are you sure you want to delete this payment?',:method =>
:delete %>
</td>
</tr>
<% end %>
</table>
</fieldset>
<% end %>
<div id="renewal_container">
<%= render :partial => "cashadvances/renew.html.erb", :locals => { :renewal => cashadvance.renewal } %>
</div>
<%= content_for :javascript_includes do %>
<%= javascript_include_tag "renew_advance" %>
<% end %>
<%= form_for(renewal, :url => renew_cashadvance_path(renewal.current_id)) do |f| %>
<%= f.error_messages %>
<%= f.hidden_field :current_id %>
<div class="field">
<%= f.label :current_balance %><br />
<%= f.text_field :current_balance %>
</div>
<div class="fields">
<%= f.label :purchase_price %><br />
<%= f.text_field :purchase_price %>
</div>
<div class="fields">
<%= f.label :factor_rate %><br />
<%= f.text_field :factor_rate %>
</div>
<div class="fields">
<%= f.label :purchased_amount %><br />
<%= f.text_field :purchased_amount %>
</div>
<div class="fields" id="withheld">
<%= f.label :amount_withheld %><br />
<%= f.text_field :amount_withheld %>
</div>
<div class="fields">
<%= f.label :split_percentage %><br />
<%= f.text_field :split_percentage %>
</div>
<div class="fields">
<%= f.label :expected_duration %><br />
<%= f.text_field :expected_duration %>
</div>
<p>
<%= f.submit "Create Renewal" %>
</p>
<% end %>
class Cashadvance < ActiveRecord::Base
belongs_to :merchant, :inverse_of => :cashadvances
has_many :payments
validates_presence_of :start_date, :factor_rate, :advance_amount, :expected_duration, :merchant
before_create :verify_zero_owed
def payback_amount
advance_amount * factor_rate
end
def amount_owed
payback_amount - payments.sum(:amount)
end
def amount_paid
payments.sum(:amount)
end
def percentage_paid
(amount_paid * 100) / payback_amount
end
def renewal
Renewal.new :current_id => id, :current_balance => amount_owed
end
private
def verify_zero_owed
merchant.cashadvances.each do |advance|
if advance.amount_owed > 0 && !advance.id.nil?
errors.add(:base, "can't add new cash advance, merchant owes on previous cash advance.")
return false
end
end
end
end
class Merchant < ActiveRecord::Base
has_many :cashadvances, :inverse_of => :merchant
belongs_to :user
belongs_to :funder
belongs_to :iso
belongs_to :administrator
accepts_nested_attributes_for :cashadvances
before_validation(:on => :create) { initialize_cashadvances }
validates_presence_of :mid, :dba
def current_cashadvance
sorted = cashadvances.sort_by { |ca| ca.start_date }
sorted.reverse.first
end
private
def initialize_cashadvances
cashadvances.each { |ca| ca.merchant = self }
end
end
class Renewal
extend ActiveModel::Naming
attr_accessor :current_id, :current_balance, :purchase_price, :factor_rate,
:purchased_amount, :amount_withheld, :split_percentage, :expected_duration
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def to_key
end
def to_param
to_key
end
def renew
current_advance = Cashadvance.find(current_id)
new_advance = Cashadvance.new(:advance_amount => purchase_price, :factor_rate =>
factor_rate, :split_percentage => split_percentage,
:expected_duration => expected_duration, :start_date =>
Date.today, :merchant => current_advance.merchant)
if new_advance.valid?
currently_owes = current_advance.amount_owed
current_advance.payments.build(:amount => currently_owes, :manual_release => true,
:note => "System Generated during renewal, balance forwarded to new advance")
current_advance.save
new_advance.save
new_advance.payments.build(:amount => -1 * currently_owes, :manual_release => true,
:note => "System Generated during renewal, balance forwarded from previous advance")
new_advance.save
return new_advance
else
return false
end
end
end
<fieldset id="merchant_details">
<legend>Merchant Details</legend>
<p>
<b>Mid:</b>
<%=h @merchant.mid %>
</p>
<p>
<b>Dba:</b>
<%=h @merchant.dba %>
</p>
<p>
<b>Funder:</b>
<%=h @merchant.funder.name if @merchant.funder %>
</p>
<p>
<%= link_to 'Edit Merchant', edit_merchant_path(@merchant) %> |
<%= link_to 'Back', merchants_path %>
</p>
</fieldset>
<% unless @merchant.current_cashadvance.nil? %>
<%= render :partial => "cashadvances/cashadvance_payments_show", :locals =>
{ :cashadvance => @merchant.current_cashadvance } %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment