Sha256: 98473f6a09700e5ec4b967809d4fcfcef3215b22afc5cc3e8133cbf06f9fdd3f

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

module ExpenseGun
  class Expense < ActiveRecord::Base
    include AASM
    
    has_many :expense_lines, inverse_of: :expense, dependent: :destroy
    
    validates :name, presence: true
    validates :date, presence: true
    
    def initialize(*args)
      super
      self.date = Date.today if self.date.nil?
    end
    
    # Sum of line amounts
    def total_all_taxes
      expense_lines.map(&:total_all_taxes).sum
    end
    
    # Sum of line emplee payback
    def total_employee_payback
      expense_lines.map(&:employee_payback).sum
    end
    
    # Sum of deductible deductible vat
    def total_vat_deductible
      expense_lines.map(&:total_vat_deductible).sum
    end
    
    def current_state
      aasm.current_state
    end
    
    aasm(column: :state, whiny_transitions: false) do
      state :new, initial: true
      state :submited
      state :accepted
      state :refused
      state :canceled
      
      event :submit do
        transitions from: :new, to: :submited
      end
      
      event :accept do
        transitions from: :submited, to: :accepted
      end
      
      event :refuse do
        transitions from: :submited, to: :refused
      end
      
      event :cancel do
        transitions from: [:new, :submited, :accepted], to: :canceled
      end
    end
    
    def may_edit?
      current_state == :new
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
expense_gun-1.0.3 app/models/expense_gun/expense.rb
expense_gun-1.0.2 app/models/expense_gun/expense.rb
expense_gun-1.0.1 app/models/expense_gun/expense.rb