purchase.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. class Purchase < ActiveRecord::Base
  2. ## Associaciones
  3. belongs_to :supplier
  4. belongs_to :pointsale
  5. belongs_to :warehouse
  6. belongs_to :user
  7. has_many :cash_registers_moves
  8. has_many :purchase_details
  9. has_many :inventories_moves
  10. has_many :products, :through => :purchase_details
  11. ##--- Llevar registro de Actividad del usuario
  12. audited
  13. enum status: [:notpaid, :cancelled, :paid, :parcial]
  14. accepts_nested_attributes_for :purchase_details
  15. ##--- Validaciones previas de guardar
  16. validates :supplier_id, presence: { message: "Debe seleccionar el proveedor." }
  17. validates :pointsale_id, presence: { message: "Debe seleccionar un almacén o un punto de venta." }, if: Proc.new { |c| c.warehouse_id.blank? }
  18. validates :warehouse_id, presence: { message: "Debe seleccionar un almacén o un punto de venta." }, if: Proc.new { |c| c.pointsale_id.blank? }
  19. validates :exchange, presence: { message: "Debe capturar el tipo de cambio." }, if: Proc.new { |c| c.is_in_dollars? }
  20. ##--- Tipo de vistas / consultas
  21. scope :notpaid, -> { where(status: 0) }
  22. scope :paid, -> { where(status: 2) }
  23. scope :cancelled, -> { where(status: 1) }
  24. scope :active, -> { where.not(status: 1) }
  25. def code_with_price
  26. "Codigo: #{purchase_code} - $ #{total}"
  27. end
  28. end