Guest User

Untitled

a guest
Jun 20th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. ## Deposit Model
  2. class Deposit < ActiveRecord::Base
  3. include Money
  4. before_save :negatize
  5. belongs_to :business_day
  6. validate :must_not_have_discrepancy
  7. before_create :not_zero
  8.  
  9. named_scope :positive, :conditions => 'amount > 0'
  10. named_scope :negative, :conditions => 'amount < 0'
  11.  
  12. def not_zero
  13. amount != 0
  14. end
  15.  
  16. def total
  17. pennies + nickels + dimes + quarters + ones + fives + tens + twenties + fifties + hundreds
  18. end
  19.  
  20. def discrepancy?
  21. total != amount.abs
  22. end
  23.  
  24. def direction
  25. amount >= 0
  26. end
  27.  
  28. def direction=(value)
  29. @direction = %w(true t 1).include?(value.to_s.downcase)
  30. end
  31.  
  32. protected
  33. def must_not_have_discrepancy
  34. errors.add("amount", "does not match totals given.") if discrepancy?
  35. end
  36.  
  37. def negatize
  38. unless @direction
  39. write_attribute :amount, amount * -1
  40. write_attribute :hundreds, hundreds * -1
  41. write_attribute :fifties, fifties * -1
  42. write_attribute :twenties, twenties * -1
  43. write_attribute :tens, tens * -1
  44. write_attribute :fives, fives * -1
  45. write_attribute :ones, ones * -1
  46. write_attribute :quarters, quarters * -1
  47. write_attribute :dimes, dimes * -1
  48. write_attribute :nickels, nickels * -1
  49. write_attribute :pennies, pennies * -1
  50. end
  51. end
  52.  
  53. end
  54.  
  55. ##Where it is used in adjustments view
  56. <fieldset>
  57. <legend>Deposits and Withdrawals: <%= @business_day.created_on.strftime("%a, %b %e") %></legend>
  58. <table cellspacing="0">
  59. <tr>
  60. <th>&nbsp;</th>
  61. <th>Total</th>
  62. <th>100</th>
  63. <th>50</th>
  64. <th>20</th>
  65. <th>10</th>
  66. <th>5</th>
  67. <th>1</th>
  68. <th>0.25</th>
  69. <th>0.10</th>
  70. <th>0.05</th>
  71. <th>0.01</th>
  72. </tr>
  73. <% for deposit in @business_day.deposits %>
  74. <% if deposit.check.blank? %>
  75. <tr class="<%= cycle("odd","even") -%>">
  76. <td><%= link_to((deposit.direction ? "Withdrawal" : "Deposit"), edit_adjustments_deposit_path(deposit)) %></td>
  77. <td style="background-color:#FFFFCC"><%= acg_formatted_money(deposit.amount.abs) %></td>
  78. <td><%= acg_formatted_money(deposit.hundreds.abs) %></td>
  79. <td><%= acg_formatted_money(deposit.fifties.abs) %></td>
  80. <td><%= acg_formatted_money(deposit.twenties.abs) %></td>
  81. <td><%= acg_formatted_money(deposit.tens.abs) %></td>
  82. <td><%= acg_formatted_money(deposit.fives.abs) %></td>
  83. <td><%= acg_formatted_money(deposit.ones.abs) %></td>
  84. <td><%= acg_formatted_money(deposit.quarters.abs) %></td>
  85. <td><%= acg_formatted_money(deposit.dimes.abs) %></td>
  86. <td><%= acg_formatted_money(deposit.nickels.abs) %></td>
  87. <td><%= acg_formatted_money(deposit.pennies.abs) %></td>
  88. </tr>
  89. <% end %>
  90. <% end %>
  91. <tr>
  92. <% form_for :deposit, @deposit, :url => adjustment_deposit_path do |f| %>
  93. <td><%= f.select :direction, [['Deposit', false], ['Withdrawal', true]], :size => 10 %></td>
  94. <td><%= text_field_tag "deposit[amount]", "0.00", :class => 'text short' %></td>
  95. <td><%= text_field_tag "deposit[hundreds]", "0.00", :class => 'text short' %></td>
  96. <td><%= text_field_tag "deposit[fifties]", "0.00", :class => 'text short' %></td>
  97. <td><%= text_field_tag "deposit[twenties]", "0.00", :class => 'text short' %></td>
  98. <td><%= text_field_tag "deposit[tens]", "0.00", :class => 'text short' %></td>
  99. <td><%= text_field_tag "deposit[fives]", "0.00", :class => 'text short' %></td>
  100. <td><%= text_field_tag "deposit[ones]", "0.00", :class => 'text short' %></td>
  101. <td><%= text_field_tag "deposit[quarters]", "0.00", :class => 'text short' %></td>
  102. <td><%= text_field_tag "deposit[dimes]", "0.00", :class => 'text short' %></td>
  103. <td><%= text_field_tag "deposit[nickels]", "0.00", :class => 'text short' %></td>
  104. <td><%= text_field_tag "deposit[pennies]", "0.00", :class => 'text short' %></td>
  105. </tr>
  106. </table>
  107. <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? %> >
  108. <%= image_tag "pencil.png", :size => "16x16", :alt => "Accept" %>
  109. Save
  110. </button>
  111. <% end %>
  112. </fieldset>
Add Comment
Please, Sign In to add comment