Guest User

Untitled

a guest
Jun 19th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. class SafeBalance < ActiveRecord::Base
  2. include Money
  3. belongs_to :business_day
  4.  
  5. DENOMINATIONS = {
  6. :pennies => 50,
  7. :nickels => 200,
  8. :dimes => 500,
  9. :quarters => 1000,
  10. :ones => 5000,
  11. :fives => 5000,
  12. :tens => 5000,
  13. :twenties => 0,
  14. :fifties => 0,
  15. :hundreds => 0
  16. }
  17.  
  18. SAFECOUNTS = %w{count_pennies count_nickels count_dimes count_quarters count_ones count_fives count_tens count_twenties count_fifties count_hundreds}
  19. SAFECOUNTS.each do |f|
  20. define_method("count_#{f}=") {|amount| write_attribute f.to_sym, dollars_to_cents(amount)}
  21. end
  22.  
  23. SIZES = %w(pennies nickels dimes quarters ones fives tens twenties fifties hundreds)
  24. SIZES.each do |f|
  25. define_method("register_#{f}_totals") {registers.collect(&f.to_sym).sum}
  26. define_method("new_#{f}") {send("end_#{f}") - send("begin_#{f}")}
  27. end
  28.  
  29. DENOMINATIONS.keys.each do |f|
  30. define_method("#{f}_to_register") {business_day.additions.positive.collect(&f.to_sym).sum}
  31. define_method("#{f}_to_safe") {business_day.additions.negative.collect(&f.to_sym).sum}
  32. define_method("#{f}_deposits") {business_day.deposits.negative.collect(&f.to_sym).sum}
  33. define_method("#{f}_withdrawals") {business_day.deposits.positive.collect(&f.to_sym).sum}
  34. define_method("#{f}_pettycash_out") {business_day.pettycashes.negative.collect(&f.to_sym).sum}
  35. define_method("#{f}_pettycash_in") {business_day.pettycashes.positive.collect(&f.to_sym).sum}
  36. end
  37.  
  38. DENOMINATIONS.each_pair do |f, k|
  39. define_method("end_#{f}") {send("begin_#{f}") - k - send("business_day.spent_#{f}") - send("business_day.safe_balance.#{f}_deposits") + send("business_day.register_new_#{f}") + send("business_day.safe_balance.#{f}_withdrawals") }
  40. end
  41.  
  42. def previous_business_day
  43. self.business_day.previous_business_day
  44. end
  45.  
  46. def difference(denomination)
  47. self.send("end_#{denomination}") - self.send("count_#{denomination}")
  48. end
  49.  
  50. def registers
  51. @registers ||= business_day.registers
  52. end
  53.  
  54. def pettycash
  55. @pettycash ||= business_day.pettycashes
  56. end
  57.  
  58. def newcash
  59. registers.collect(&:newcash).sum
  60. end
  61.  
  62. def begin_register
  63. registers.collect(&:begbal).sum
  64. end
  65.  
  66. def money_to_register
  67. additions = business_day.positive_additions
  68. additions.collect(&:amount).sum
  69. end
  70.  
  71. def money_to_safe
  72. additions = business_day.negative_additions
  73. additions.collect(&:amount).sum
  74. end
  75.  
  76. def deposits
  77. deposits = business_day.deposits.find(:all, :conditions => 'amount < 0')
  78. deposits.collect(&:amount).sum
  79. end
  80.  
  81. def withdrawals
  82. deposits = business_day.deposits.find(:all, :conditions => 'amount > 0')
  83. deposits.collect(&:amount).sum
  84. end
  85.  
  86. def petty_cash
  87. pettycash.collect(&:amount).sum
  88. end
  89.  
  90. def additions
  91. withdrawals.abs + newcash # + money_to_safe.abs
  92. end
  93.  
  94. def subtractions
  95. petty_cash.abs + deposits.abs # + begin_register.abs + money_to_register.abs
  96. end
  97.  
  98. def end_balance
  99. end_balance = begin_balance - subtractions + additions - (registers.carried_over.cash.count * 16750)
  100. end
  101.  
  102. DENOMINATIONS.each_pair do |key, value|
  103. define_method("adj_begin_#{key}") {send("begin_#{key}") - (@business_day.registers.carried_over.cash.count * value)}
  104. define_method("adj_end_#{key}") {send("end_#{key}") - (@business_day.registers.carried_over.cash.count * value)}
  105. end
  106.  
  107. end
Add Comment
Please, Sign In to add comment