cash_registers_move.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 metodo de pago."
  17. validates_presence_of :received, message: "Debe seleccionar monto recibido.", if: :in_cash?, unless: :skip_received_validation
  18. scope :activos, -> { where("cash_registers_moves.status = 1") }
  19. def in_cash?
  20. cash_payment_id = PaymentMethod.find_by(isCash: 1).id
  21. if payment_method_id == cash_payment_id
  22. return true
  23. else
  24. return false
  25. end
  26. end
  27. # rubocop:disable Metrics/BlockNesting
  28. def calculate_quantities
  29. cash_payment_method_id = PaymentMethod.find_by(isCash: 1).id
  30. # ventas
  31. if sale.present?
  32. if sale.cash?
  33. if payment_method_id == cash_payment_method_id
  34. sale_total = sale.total
  35. already_paid = CashRegistersMove.where(sale_id: sale_id, open_cash_register_id: open_cash_register_id).sum(:quantity)
  36. rest = sale_total - already_paid
  37. if received.present?
  38. self.change = received - (sale_total - already_paid)
  39. self.quantity = received > rest ? rest : received
  40. end
  41. else
  42. self.quantity = received
  43. self.change = 0
  44. end
  45. elsif sale.reserved? || sale.credit?
  46. self.change = received.present? ? (received - quantity) : 0
  47. end
  48. # devoluciones
  49. elsif products_return.present?
  50. if payment_method_id == cash_payment_method_id
  51. total = products_return.difference_amount
  52. already_paid = CashRegistersMove.where(products_return_id: products_return_id, open_cash_register_id: open_cash_register_id).sum(:quantity)
  53. rest = total - already_paid
  54. if received.present?
  55. self.change = received - (total - already_paid)
  56. self.quantity = received > rest ? rest : received
  57. end
  58. else
  59. self.quantity = received
  60. self.change = 0
  61. end
  62. end
  63. end
  64. # rubocop:enable Metrics/BlockNesting
  65. def self.incomings_per_period(period)
  66. all_incomings = Array.new
  67. case period
  68. when 'day'
  69. beg_period = Date.current.beginning_of_day
  70. end_period = Date.current.end_of_day
  71. when 'week'
  72. beg_period = Date.current.beginning_of_week
  73. end_period = Date.current.end_of_week
  74. when 'month'
  75. beg_period = Date.current.beginning_of_month
  76. end_period = Date.current.end_of_month
  77. end
  78. Pointsale.activos.each do |pointsale|
  79. obj = {}
  80. obj[:pointsale] = pointsale.name.tr(" ", "\n")
  81. # obj[:total] = pointsale.cash_registers_moves.where(created_at: beg_period..end_period, move_type: '1', status: '1').sum(:quantity)
  82. sales = pointsale.sales.activas.where(date_sale: beg_period..end_period)
  83. obj[:total] = CashRegistersMove.activos.where("sale_id IN (?)", sales.pluck(:id)).where(created_at: beg_period..end_period).sum(:quantity)
  84. all_incomings << obj
  85. end
  86. all_incomings.to_json
  87. end
  88. def choose_concept_when_reserved
  89. reserve_has_moves = CashRegistersMove.where(sale_id: sale_id, move_type: 1, status: 1).any?
  90. # rubocop:disable Style/ConditionalAssignment
  91. if reserve_has_moves
  92. self.concept = sale.reserve_debt > quantity ? :reserved_payment : :reserved_last_payment
  93. else
  94. self.concept = :reserved_first_payment
  95. end
  96. # rubocop:enable Style/ConditionalAssignment
  97. end
  98. end