sale.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 codigo 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( "sales.status != 1") }
  27. def get_pointsale
  28. cash_register = OpenCashRegister.find(self.open_cash_register_id).cash_register
  29. pointsale = CashRegister.find(cash_register.id).pointsale
  30. return pointsale
  31. end
  32. def reserve_is_expired?
  33. if self.expiration_date >= Date.today
  34. false
  35. else
  36. true
  37. end
  38. end
  39. def self.sales_chart_by_period_of_time(period)
  40. all_sales = Array.new
  41. if period == 'week'
  42. beg_period = Date.current.beginning_of_week
  43. end_period = Date.current.end_of_week
  44. elsif period == 'month'
  45. beg_period = Date.current.beginning_of_month
  46. end_period = Date.current.end_of_month
  47. elsif period == 'day'
  48. beg_period = Date.current.beginning_of_day
  49. end_period = Date.current.end_of_day
  50. end
  51. Pointsale.activos.each do |pointsale|
  52. obj = {}
  53. obj[:pointsale] = pointsale.name.gsub(" ", "\n")
  54. obj[:total] = pointsale.sales.activas.where(:date_sale => beg_period..end_period).where('sales.status > 1').sum(:total)
  55. all_sales << obj
  56. end
  57. all_sales = all_sales.to_json
  58. return all_sales
  59. end
  60. def reserve_debt
  61. total_in_moves = self.cash_registers_moves.where(:status => 1).sum(:quantity)
  62. self.total - total_in_moves
  63. end
  64. def can_be_cancelled?
  65. daysToCancel = PosConfig.first.days_cancel_sale
  66. if (self.date_sale + daysToCancel.days) >= Date.today && !self.cancelled?
  67. # checar si la venta es de credito y ver si tiene pagos
  68. if self.credit?
  69. has_payments = CreditPayment.where(:credit_id => self.credit.id).any?
  70. has_payments ? (return false) : (return true)
  71. else
  72. return true
  73. end
  74. else
  75. return false
  76. end
  77. end
  78. end