expense.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. class Expense < ActiveRecord::Base
  2. belongs_to :expensesconcept
  3. belongs_to :open_cash_register
  4. has_many :cash_registers_moves
  5. ##--- Llevar registro de Actividad del usuario
  6. audited
  7. enum status: [:cancelled, :active, :registered]
  8. attr_accessor :skip_open_cash_validation, :skip_open_cash_has_money
  9. validates_presence_of :quantity, message: "Se debe indicar la cantidad."
  10. validates_numericality_of :quantity, greater_than: 0.00, message: "La cantidad debe ser mayor a $0"
  11. validates_presence_of :open_cash_register_id, message: "Se debe elegir caja registradora.", unless: :skip_open_cash_validation
  12. validates_presence_of :expensesconcept_id, message: "Se debe elegir concepto de egreso."
  13. validate :open_cash_has_money?, unless: :skip_open_cash_has_money, on: :create
  14. scope :activos, -> { where(status: 2).order("created_at") }
  15. def open_cash_has_money?
  16. money_in_cash = 0
  17. money_in_cash += open_cash_register.initial_cash
  18. CashRegistersMove.where(open_cash_register_id: open_cash_register.id, move_type: '0').each do |move|
  19. money_in_cash -= move.quantity
  20. end
  21. CashRegistersMove.where(open_cash_register_id: open_cash_register.id, move_type: '1').each do |move|
  22. money_in_cash += move.quantity
  23. end
  24. if money_in_cash >= quantity
  25. return true
  26. else
  27. errors.add :quantity, "La cantidad es superior al efectivo disponible en caja."
  28. end
  29. end
  30. end