seller.rb 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. class Seller < ActiveRecord::Base
  2. belongs_to :pointsale
  3. has_many :sales
  4. has_many :sellerscommissions
  5. ##--- Llevar registro de Actividad del usuario
  6. audited
  7. enum status: [:erased, :active, :inactive]
  8. attr_accessor :skip_pointsale
  9. ##--- Validaciones previas de guardar
  10. validates :pointsale_id, presence: { message: "Debe seleccionar punto de venta para el vendedor." }, on: [:create, :update], unless: :skip_pointsale
  11. validates :name, presence: { message: "Debe indicar nombre para el vendedor." }, on: [:create, :update]
  12. validates :last_name, presence: { message: "Debe indicar apellido para el vendedor." }, on: [:create, :update]
  13. scope :vigentes, -> { where.not(status: 0) }
  14. ##--- Funciones personalizadas
  15. def full_name
  16. "#{name} #{last_name}"
  17. end
  18. def self.get_ranking(period, pointsale)
  19. all_serllers = Array.new
  20. if period == 'week'
  21. beg_period = Date.current.beginning_of_week
  22. end_period = Date.current.end_of_week
  23. elsif period == 'month'
  24. beg_period = Date.current.beginning_of_month
  25. end_period = Date.current.end_of_month
  26. end
  27. 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')
  28. quantities_top_products
  29. end
  30. def get_sales_by_period(initial_date, final_date, type)
  31. if type == 'commission'
  32. sales_ids = Sale.where(date_sale: initial_date..final_date, seller_id: id).activas.pluck(:id)
  33. commissions_paid = SalesSellercommission.where("sale_id IN (?)", sales_ids).pluck(:sale_id)
  34. news_to_pay = sales_ids - commissions_paid
  35. sales_by_period = Sale.where("sales.id IN (?)", news_to_pay)
  36. else
  37. sales_by_period = Sale.where(date_sale: initial_date..final_date, seller_id: id).activas
  38. end
  39. sales_by_period
  40. end
  41. def get_data_for_commissions(initial_date, final_date)
  42. 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)
  43. 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)
  44. sales_by_period = Sale.where(date_sale: initial_date..final_date, seller_id: id).activas
  45. already_generated_sales_comm_ids = sales_by_period.present? ? SalesSellercommission.where('sale_id IN (?)', sales_by_period.pluck(:id)).pluck(:id) : Array.new
  46. sales_ids = sales_by_period.pluck(:id) - already_generated_sales_comm_ids
  47. total_sold_cash = Sale.where('id in (?)', sales_ids).where(saletype: '1').sum(:total)
  48. total_sold_credits_apartments = Sale.where('id in (?)', sales_ids).where('saletype != 1').sum(:total)
  49. obj = Hash.new
  50. obj['total_sold_cash'] = total_sold_cash
  51. obj['total_sold_credits_apartments'] = total_sold_credits_apartments
  52. obj['sales_count'] = sales_ids.count
  53. obj['income_by_reserved_and_credits'] = income_by_reserved_and_credits
  54. obj['income_by_cash'] = income_by_cash
  55. obj
  56. # moves = CashRegistersMove.joins(:sale).where(move_type: '1', created_at: initial_date..final_date).where("sales.seller_id = ?", id)
  57. # all_sales_ids = moves.pluck(:id)
  58. # already_paid_ids = SalesSellercommission.includes(:sale).where('sale_id IN (?) and sales.saletype = ?', all_sales_ids, 1).pluck(:sale_id)
  59. # sales_with_moves_to_pay = all_sales_ids - already_paid_ids
  60. # received_money = moves.sum(:quantity)
  61. # obj = Hash.new
  62. # obj['moves'] = moves
  63. # obj['sales_with_movs'] = Sale.where('id IN (?)', sales_with_moves_to_pay)
  64. # obj
  65. # sales_ids = Sale.where(date_sale: initial_date..final_date, seller_id: id).activas.pluck(:id)
  66. # commissions_paid = SalesSellercommission.where("sale_id IN (?)", sales_ids).pluck(:sale_id)
  67. # news_to_pay = sales_ids - commissions_paid
  68. # sales_by_period = Sale.where("sales.id IN (?)", news_to_pay)
  69. end
  70. end