cash_registers_move.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. class CashRegistersMove < ActiveRecord::Base
  2. belongs_to :open_cash_register
  3. belongs_to :payment_method
  4. belongs_to :expense
  5. belongs_to :sale
  6. belongs_to :purchase
  7. belongs_to :credit_payment
  8. belongs_to :products_return
  9. ##--- Llevar registro de Actividad del usuario
  10. audited
  11. enum move_type: [:egreso, :ingreso]
  12. enum status: [:inactive, :active]
  13. enum concept: [:sale, :purchase, :expense, :credit_payment, :reserved_payment, :products_return, :reserved_first_payment, :reserved_last_payment]
  14. attr_accessor :skip_received_validation
  15. validates_presence_of :quantity, message: "Debe indicar cantidad."
  16. validates_presence_of :payment_method_id, message: "Debe seleccionar método de pago."
  17. validates_presence_of :received, message: "Debe seleccionar monto recibido.", if: :in_cash?, unless: :skip_received_validation
  18. validate :valid_info?, on: :create
  19. scope :activos, -> { where(cash_registers_moves: { status: 1 }) }
  20. scope :incomings, ->(open_cash_register) { includes(:open_cash_register).where(open_cash_register_id: open_cash_register, status: 1, move_type: 1) }
  21. scope :outgoings, ->(open_cash_register) { includes(:open_cash_register).where(open_cash_register_id: open_cash_register, status: 1, move_type: 0) }
  22. def in_cash?
  23. cash_payment_id = PaymentMethod.find_by(isCash: 1).id
  24. if payment_method_id == cash_payment_id
  25. return true
  26. else
  27. return false
  28. end
  29. end
  30. def valid_info?
  31. case payment_method.method
  32. when "Tarjeta de credito", "Tarjeta de debito" then
  33. if !cardnumber.present?
  34. errors.add(:cardnumber, "Debe capturar los últimos 4 dígitos de la tarjeta.")
  35. else
  36. true
  37. end
  38. when "Transferencia bancaria" then
  39. if !customer_code.present?
  40. errors.add(:customer_code, "Debe capturar la referencia del cliente.")
  41. else
  42. true
  43. end
  44. when "Cheque" then
  45. if !bank_name.present? || !check_number.present? || !customer_account.present?
  46. errors.add(:base, "Debe capturar toda la información adicional del método de pago.")
  47. else
  48. true
  49. end
  50. else
  51. true
  52. end
  53. end
  54. def show_extras
  55. case payment_method.method
  56. when "Tarjeta de credito", "Tarjeta de debito" then # tarjetas
  57. cardnumber
  58. when "Transferencia bancaria" then # transferencia bancaria
  59. "Referencia: #{customer_code}"
  60. when "Cheque" then # cheque
  61. "Banco: #{bank_name}"
  62. when "Efectivo" then # efectivo
  63. ""
  64. end
  65. end
  66. def calculate_quantities
  67. if payment_method.isCash?
  68. if sale.present?
  69. self.concept = :sale
  70. total = sale.total
  71. already_paid = CashRegistersMove.where(sale_id: sale_id, open_cash_register_id: open_cash_register_id).sum(:quantity)
  72. rest = total - already_paid
  73. if sale.cash?
  74. self.change = rest > received.to_f ? 0 : received.to_f - rest
  75. self.quantity = received.to_f > rest ? rest : received.to_f
  76. elsif sale.reserved? || sale.credit?
  77. self.change = received.to_f > quantity ? received.to_f - quantity : 0
  78. end
  79. elsif products_return.present?
  80. total = products_return.difference_amount
  81. already_paid = CashRegistersMove.where(products_return_id: products_return_id, open_cash_register_id: open_cash_register_id).sum(:quantity)
  82. self.change = total > received.to_f ? 0 : received.to_f - total
  83. self.quantity = received.to_f > total ? total : received.to_f
  84. end
  85. else
  86. self.quantity = received.to_f
  87. self.change = 0
  88. end
  89. puts "quantity #{quantity}"
  90. puts "change #{change}"
  91. end
  92. def self.incomings_per_period(period)
  93. all_incomings = Array.new
  94. case period
  95. when 'day'
  96. beg_period = Date.current.beginning_of_day
  97. end_period = Date.current.end_of_day
  98. when 'week'
  99. beg_period = Date.current.beginning_of_week
  100. end_period = Date.current.end_of_week
  101. when 'month'
  102. beg_period = Date.current.beginning_of_month
  103. end_period = Date.current.end_of_month
  104. end
  105. Pointsale.activos.each do |pointsale|
  106. obj = {}
  107. obj[:pointsale] = pointsale.name.tr(" ", "\n")
  108. obj[:total] = pointsale.cash_registers_moves.where(created_at: beg_period..end_period, move_type: '1', status: '1').sum(:quantity)
  109. # sales = pointsale.sales.activas.where(date_sale: beg_period..end_period)
  110. # obj[:total] = CashRegistersMove.activos.where("sale_id IN (?)", sales.pluck(:id)).where(created_at: beg_period..end_period).sum(:quantity)
  111. all_incomings << obj
  112. end
  113. all_incomings.to_json
  114. end
  115. def choose_concept_when_reserved
  116. reserve_has_moves = CashRegistersMove.where(sale_id: sale_id, move_type: 1, status: 1).any?
  117. self.concept =
  118. if reserve_has_moves
  119. sale.reserve_debt > quantity ? :reserved_payment : :reserved_last_payment
  120. else
  121. :reserved_first_payment
  122. end
  123. end
  124. end