class Seller < ActiveRecord::Base
  belongs_to :pointsale
  has_many :sales
  has_many :sellerscommissions

  ##--- Llevar registro de Actividad del usuario
  audited

  enum status: [:erased, :active, :inactive]

  attr_accessor :skip_pointsale

  ##--- Validaciones previas de guardar
  validates :pointsale_id, presence: { message: "Debe seleccionar punto de venta para el vendedor." }, on: [:create, :update], unless: :skip_pointsale
  validates :name, presence: { message: "Debe indicar nombre para el vendedor." }, on: [:create, :update]
  validates :last_name, presence: { message: "Debe indicar apellido para el vendedor." }, on: [:create, :update]

  scope :vigentes, -> { where("status != 0") }

  ##--- Funciones personalizadas
  def full_name
    "#{name} #{last_name}"
  end

  def self.get_ranking(period, pointsale)
    all_serllers = Array.new
    if period == 'week'
      beg_period = Date.current.beginning_of_week
      end_period = Date.current.end_of_week
    elsif period == 'month'
      beg_period = Date.current.beginning_of_month
      end_period = Date.current.end_of_month
    end
    quantities_top_products = pointsale.sellers.joins(:sales).where("sales.date_sale between ? and ? and sales.status > 1", beg_period, end_period).group('sellers.name').order('sum_sales_total desc').limit(10).sum('sales.total')
    quantities_top_products
  end

  def get_sales_by_period(initial_date, final_date, type)
    if type == 'commission'
      sales_ids = Sale.where(date_sale: initial_date..final_date, seller_id: id).activas.pluck(:id)
      commissions_paid = SalesSellercommission.where("sale_id IN (?)", sales_ids).pluck(:sale_id)
      news_to_pay = sales_ids - commissions_paid
      sales_by_period = Sale.where("sales.id IN (?)", news_to_pay)
    else
      sales_by_period = Sale.where(date_sale: initial_date..final_date, seller_id: id).activas
    end
    sales_by_period
  end

  def get_data_for_commissions(initial_date, final_date)
    income_by_reserved_and_credits = CashRegistersMove.joins(:sale).where(move_type: '1', created_at: initial_date..final_date).where("sales.seller_id = ? and sales.saletype != 1", id).sum(:quantity)
    income_by_cash = CashRegistersMove.joins(:sale).where(move_type: '1', created_at: initial_date..final_date).where("sales.seller_id = ? and sales.saletype = 1", id).sum(:quantity)

    sales_by_period = Sale.where(date_sale: initial_date..final_date, seller_id: id).activas
    already_generated_sales_comm_ids = sales_by_period.present? ? SalesSellercommission.where('sale_id IN (?)', sales_by_period.pluck(:id)).pluck(:id) : Array.new
    sales_ids = sales_by_period.pluck(:id) - already_generated_sales_comm_ids
    total_sold_cash = Sale.where('id in (?)', sales_ids).where(saletype: '1').sum(:total)
    total_sold_credits_apartments = Sale.where('id in (?)', sales_ids).where('saletype != 1').sum(:total)

    obj = Hash.new
    obj['total_sold_cash'] = total_sold_cash
    obj['total_sold_credits_apartments'] = total_sold_credits_apartments
    obj['sales_count'] = sales_ids.count
    obj['income_by_reserved_and_credits'] = income_by_reserved_and_credits
    obj['income_by_cash'] = income_by_cash
    obj
    # moves = CashRegistersMove.joins(:sale).where(move_type: '1', created_at: initial_date..final_date).where("sales.seller_id = ?", id)
    # all_sales_ids = moves.pluck(:id)
    # already_paid_ids = SalesSellercommission.includes(:sale).where('sale_id IN (?) and sales.saletype = ?', all_sales_ids, 1).pluck(:sale_id)
    # sales_with_moves_to_pay = all_sales_ids - already_paid_ids
    # received_money = moves.sum(:quantity)
    # obj = Hash.new
    # obj['moves'] = moves
    # obj['sales_with_movs'] = Sale.where('id IN (?)', sales_with_moves_to_pay)
    # obj

    # sales_ids = Sale.where(date_sale: initial_date..final_date, seller_id: id).activas.pluck(:id)
    # commissions_paid = SalesSellercommission.where("sale_id IN (?)", sales_ids).pluck(:sale_id)
    # news_to_pay = sales_ids - commissions_paid
    # sales_by_period = Sale.where("sales.id IN (?)", news_to_pay)
  end
end
