| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- class CashRegistersMove < ActiveRecord::Base
- belongs_to :open_cash_register
- belongs_to :payment_method
- belongs_to :expense
- belongs_to :sale
- belongs_to :purchase
- belongs_to :credit_payment
- belongs_to :products_return
- ##--- Llevar registro de Actividad del usuario
- audited
- enum move_type: [:egreso, :ingreso]
- enum status: [:inactive, :active]
- enum concept: [:sale, :purchase, :expense, :credit_payment, :reserved_payment, :products_return, :reserved_first_payment, :reserved_last_payment]
- attr_accessor :skip_received_validation
- validates_presence_of :quantity, message: "Debe indicar cantidad."
- validates_presence_of :payment_method_id, message: "Debe seleccionar método de pago."
- validates_presence_of :received, message: "Debe seleccionar monto recibido.", if: :in_cash?, unless: :skip_received_validation
- validate :valid_info?, on: :create
- scope :activos, -> { where(cash_registers_moves: { status: 1 }) }
- scope :incomings, ->(open_cash_register) { includes(:open_cash_register).where(open_cash_register_id: open_cash_register, status: 1, move_type: 1) }
- scope :outgoings, ->(open_cash_register) { includes(:open_cash_register).where(open_cash_register_id: open_cash_register, status: 1, move_type: 0) }
- def in_cash?
- cash_payment_id = PaymentMethod.find_by(isCash: 1).id
- if payment_method_id == cash_payment_id
- return true
- else
- return false
- end
- end
- def valid_info?
- case payment_method.method
- when "Tarjeta de credito", "Tarjeta de debito" then
- if !cardnumber.present?
- errors.add(:cardnumber, "Debe capturar los últimos 4 dígitos de la tarjeta.")
- else
- true
- end
- when "Transferencia bancaria" then
- if !customer_code.present?
- errors.add(:customer_code, "Debe capturar la referencia del cliente.")
- else
- true
- end
- when "Cheque" then
- if !bank_name.present? || !check_number.present? || !customer_account.present?
- errors.add(:base, "Debe capturar toda la información adicional del método de pago.")
- else
- true
- end
- else
- true
- end
- end
- def show_extras
- case payment_method.method
- when "Tarjeta de credito", "Tarjeta de debito" then # tarjetas
- cardnumber
- when "Transferencia bancaria" then # transferencia bancaria
- "Referencia: #{customer_code}"
- when "Cheque" then # cheque
- "Banco: #{bank_name}"
- when "Efectivo" then # efectivo
- ""
- end
- end
- def calculate_quantities
- if payment_method.isCash?
- if sale.present?
- self.concept = :sale
- total = sale.total
- already_paid = CashRegistersMove.where(sale_id: sale_id, open_cash_register_id: open_cash_register_id).sum(:quantity)
- elsif products_return.present?
- total = products_return.difference_amount
- already_paid = CashRegistersMove.where(products_return_id: products_return_id, open_cash_register_id: open_cash_register_id).sum(:quantity)
- end
- rest = total - already_paid
- if sale.cash?
- self.change = rest > received.to_f ? 0 : received.to_f - rest
- self.quantity = received.to_f > rest ? rest : received.to_f
- elsif sale.reserved? || sale.credit?
- self.change = received.to_f > quantity ? received.to_f - quantity : 0
- end
- else
- # self.quantity = received.to_f
- self.received = ""
- self.change = 0
- end
- end
- def self.incomings_per_period(period)
- all_incomings = Array.new
- case period
- when 'day'
- beg_period = Date.current.beginning_of_day
- end_period = Date.current.end_of_day
- when 'week'
- beg_period = Date.current.beginning_of_week
- end_period = Date.current.end_of_week
- when 'month'
- beg_period = Date.current.beginning_of_month
- end_period = Date.current.end_of_month
- end
- Pointsale.activos.each do |pointsale|
- obj = {}
- obj[:pointsale] = pointsale.name.tr(" ", "\n")
- obj[:total] = pointsale.cash_registers_moves.where(created_at: beg_period..end_period, move_type: '1', status: '1').sum(:quantity)
- # sales = pointsale.sales.activas.where(date_sale: beg_period..end_period)
- # obj[:total] = CashRegistersMove.activos.where("sale_id IN (?)", sales.pluck(:id)).where(created_at: beg_period..end_period).sum(:quantity)
- all_incomings << obj
- end
- all_incomings.to_json
- end
- def choose_concept_when_reserved
- reserve_has_moves = CashRegistersMove.where(sale_id: sale_id, move_type: 1, status: 1).any?
- self.concept =
- if reserve_has_moves
- sale.reserve_debt > quantity ? :reserved_payment : :reserved_last_payment
- else
- :reserved_first_payment
- end
- end
- end
|