Class: Stock
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Stock
- Includes:
- HasAccounts::Model
- Defined in:
- app/models/stock.rb
Constant Summary collapse
- STATES =
States
['available', 'amortized', 'sold', 'removed']
Instance Method Summary collapse
- #amount_changes(value_date) ⇒ Object
-
#build_booking(params = {}, template_code = nil) ⇒ Object
Build booking.
-
#direct_account ⇒ Object
Guess direct_account.
-
#to_s(format = :default) ⇒ Object
String.
-
#write_downs(value_date) ⇒ Object
Calculations.
Instance Method Details
#amount_changes(value_date) ⇒ Object
89 90 91 |
# File 'app/models/stock.rb', line 89 def amount_changes(value_date) -(balance(value_date.first) - balance(value_date.last) - write_downs(value_date)) end |
#build_booking(params = {}, template_code = nil) ⇒ Object
Build booking
We use the value_date of the purchase invoice but our own amount.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/models/stock.rb', line 68 def build_booking(params = {}, template_code = nil) template_code = self.class.to_s.underscore + ':activate' # Prepare booking parameters booking_params = {:amount => amount} if purchase_invoice booking_params[:value_date] = purchase_invoice.value_date else booking_params[:value_date] = Date.today end booking_params.merge!(params) # Build and assign booking super(booking_params, template_code) end |
#direct_account ⇒ Object
Guess direct_account
We simply take the first booking and exclude accounts with codes 1100 and 2000 (credit and debit invoices) as candidates.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/models/stock.rb', line 52 def direct_account # We don't care if no bookings return nil if bookings.empty? # Take any booking booking = bookings.first involved_accounts = [booking.credit_account, booking.debit_account] relevant_account = involved_accounts - [Account.find_by_code("1100"), Account.find_by_code("2000")] return relevant_account.first end |
#to_s(format = :default) ⇒ Object
String
10 11 12 |
# File 'app/models/stock.rb', line 10 def to_s(format = :default) title end |
#write_downs(value_date) ⇒ Object
Calculations
85 86 87 |
# File 'app/models/stock.rb', line 85 def write_downs(value_date) bookings.direct_balance(value_date, Account.find_by_code('6900')) end |