sale.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. class Sale < ActiveRecord::Base
  2. belongs_to :customer
  3. belongs_to :user
  4. belongs_to :open_cash_register
  5. belongs_to :seller
  6. has_many :cash_registers_moves
  7. has_many :inventories_moves
  8. has_many :sales_details
  9. has_many :products, through: :sales_details
  10. has_one :credit
  11. has_many :credit_payments, through: :credit
  12. has_one :sales_sellercommission
  13. has_many :products_returns
  14. has_many :pointsales, through: :open_cash_register
  15. ##--- Llevar registro de Actividad del usuario
  16. audited
  17. enum status: [:notpaid, :cancelled, :paid, :parcial, :cancelled_by_expiration]
  18. enum saletype: [:credit, :cash, :reserved]
  19. ##--- Validaciones previas de guardar
  20. validates :open_cash_register_id, presence: { message: "Se debe seleccionar caja registradora." }, on: [:create]
  21. validates :sale_code, presence: { message: "Se debe seleccionar caja registradora para generar el código de venta." }, on: [:create, :update]
  22. validates :customer_id, presence: { message: "Debe seleccionar cliente para la venta." }, on: [:create, :update]
  23. validates :seller_id, presence: { message: "Debe seleccionar vendedor para la venta." }, on: [:create, :update]
  24. validates :saletype, presence: { message: "Debe seleccionar tipo de venta." }, on: [:create, :update]
  25. accepts_nested_attributes_for :sales_details
  26. scope :activas, -> { where.not(sales: { status: 1 }) }
  27. scope :credit_pending, ->(customer) { where(saletype: 0, status: [0, 3], customer_id: customer).order(:id) }
  28. def get_pointsale
  29. user.pointsale
  30. end
  31. def reserve_is_expired?
  32. if expiration_date >= Date.today
  33. false
  34. else
  35. true
  36. end
  37. end
  38. def self.sales_chart_by_period_of_time(period)
  39. all_sales = Array.new
  40. if period == 'week'
  41. beg_period = Date.current.beginning_of_week
  42. end_period = Date.current.end_of_week
  43. elsif period == 'month'
  44. beg_period = Date.current.beginning_of_month
  45. end_period = Date.current.end_of_month
  46. elsif period == 'day'
  47. beg_period = Date.current.beginning_of_day
  48. end_period = Date.current.end_of_day
  49. end
  50. Pointsale.activos.each do |pointsale|
  51. obj = {}
  52. obj[:pointsale] = pointsale.name.gsub(" ", "\n")
  53. obj[:total] = pointsale.sales.activas.where(date_sale: beg_period..end_period).where('sales.status > 1').sum(:total)
  54. all_sales << obj
  55. end
  56. all_sales = all_sales.to_json
  57. all_sales
  58. end
  59. def reserve_debt
  60. total_in_moves = cash_registers_moves.where(status: 1).sum(:quantity)
  61. total - total_in_moves
  62. end
  63. def can_be_cancelled?(pos_config)
  64. if (date_sale + pos_config.days_cancel_sale.days) >= Date.today && !cancelled?
  65. # checar si la venta es de credito y ver si tiene pagos
  66. if credit?
  67. has_payments = CreditPayment.where(credit_id: credit.id).any?
  68. has_payments ? (return false) : (return true)
  69. else
  70. true
  71. end
  72. else
  73. false
  74. end
  75. end
  76. end