Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Deposit Model
- class Deposit < ActiveRecord::Base
- include Money
- before_save :negatize
- belongs_to :business_day
- validate :must_not_have_discrepancy
- before_create :not_zero
- named_scope :positive, :conditions => 'amount > 0'
- named_scope :negative, :conditions => 'amount < 0'
- def not_zero
- amount != 0
- end
- def total
- pennies + nickels + dimes + quarters + ones + fives + tens + twenties + fifties + hundreds
- end
- def discrepancy?
- total != amount.abs
- end
- def direction
- amount >= 0
- end
- def direction=(value)
- @direction = %w(true t 1).include?(value.to_s.downcase)
- end
- protected
- def must_not_have_discrepancy
- errors.add("amount", "does not match totals given.") if discrepancy?
- end
- def negatize
- unless @direction
- write_attribute :amount, amount * -1
- write_attribute :hundreds, hundreds * -1
- write_attribute :fifties, fifties * -1
- write_attribute :twenties, twenties * -1
- write_attribute :tens, tens * -1
- write_attribute :fives, fives * -1
- write_attribute :ones, ones * -1
- write_attribute :quarters, quarters * -1
- write_attribute :dimes, dimes * -1
- write_attribute :nickels, nickels * -1
- write_attribute :pennies, pennies * -1
- end
- end
- end
- ##Where it is used in adjustments view
- <fieldset>
- <legend>Deposits and Withdrawals: <%= @business_day.created_on.strftime("%a, %b %e") %></legend>
- <table cellspacing="0">
- <tr>
- <th> </th>
- <th>Total</th>
- <th>100</th>
- <th>50</th>
- <th>20</th>
- <th>10</th>
- <th>5</th>
- <th>1</th>
- <th>0.25</th>
- <th>0.10</th>
- <th>0.05</th>
- <th>0.01</th>
- </tr>
- <% for deposit in @business_day.deposits %>
- <% if deposit.check.blank? %>
- <tr class="<%= cycle("odd","even") -%>">
- <td><%= link_to((deposit.direction ? "Withdrawal" : "Deposit"), edit_adjustments_deposit_path(deposit)) %></td>
- <td style="background-color:#FFFFCC"><%= acg_formatted_money(deposit.amount.abs) %></td>
- <td><%= acg_formatted_money(deposit.hundreds.abs) %></td>
- <td><%= acg_formatted_money(deposit.fifties.abs) %></td>
- <td><%= acg_formatted_money(deposit.twenties.abs) %></td>
- <td><%= acg_formatted_money(deposit.tens.abs) %></td>
- <td><%= acg_formatted_money(deposit.fives.abs) %></td>
- <td><%= acg_formatted_money(deposit.ones.abs) %></td>
- <td><%= acg_formatted_money(deposit.quarters.abs) %></td>
- <td><%= acg_formatted_money(deposit.dimes.abs) %></td>
- <td><%= acg_formatted_money(deposit.nickels.abs) %></td>
- <td><%= acg_formatted_money(deposit.pennies.abs) %></td>
- </tr>
- <% end %>
- <% end %>
- <tr>
- <% form_for :deposit, @deposit, :url => adjustment_deposit_path do |f| %>
- <td><%= f.select :direction, [['Deposit', false], ['Withdrawal', true]], :size => 10 %></td>
- <td><%= text_field_tag "deposit[amount]", "0.00", :class => 'text short' %></td>
- <td><%= text_field_tag "deposit[hundreds]", "0.00", :class => 'text short' %></td>
- <td><%= text_field_tag "deposit[fifties]", "0.00", :class => 'text short' %></td>
- <td><%= text_field_tag "deposit[twenties]", "0.00", :class => 'text short' %></td>
- <td><%= text_field_tag "deposit[tens]", "0.00", :class => 'text short' %></td>
- <td><%= text_field_tag "deposit[fives]", "0.00", :class => 'text short' %></td>
- <td><%= text_field_tag "deposit[ones]", "0.00", :class => 'text short' %></td>
- <td><%= text_field_tag "deposit[quarters]", "0.00", :class => 'text short' %></td>
- <td><%= text_field_tag "deposit[dimes]", "0.00", :class => 'text short' %></td>
- <td><%= text_field_tag "deposit[nickels]", "0.00", :class => 'text short' %></td>
- <td><%= text_field_tag "deposit[pennies]", "0.00", :class => 'text short' %></td>
- </tr>
- </table>
- <button class="positive" type="submit" <%= "onclick=\"if (!confirm('You are about to edit past days information. Are you sure you want to do that?')){return false;}\"" if @business_day.old? %> >
- <%= image_tag "pencil.png", :size => "16x16", :alt => "Accept" %>
- Save
- </button>
- <% end %>
- </fieldset>
Add Comment
Please, Sign In to add comment