Class: Api::OverviewController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/api/overview_controller.rb

Instance Method Summary collapse

Instance Method Details

#best_selling_productsObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/controllers/api/overview_controller.rb', line 2

def best_selling_products
 return_data=Hash.new
 prod_array=Array.new
 best=ActiveRecord::Base.connection.execute("Select A.id,A.name,sum(C.quantity) qty from spree_products A, spree_variants B, spree_line_items C,spree_orders D where A.id=B.product_id and B.id=C.variant_id and C.order_id=D.id and D.payment_state in ('paid','completed','payment','complete') group by A.id,A.name order by 3,1")
 best.each do |pr|
  prod_dtl=Hash.new
  prod_dtl[:id]=pr[0]
  prod_dtl[:name]=pr[1]
  prod_dtl[:qty]=pr[2]
  prod_array.push prod_dtl
 end
 return_data[:products] = prod_array
 render :json => return_data.to_json, :status => 201
end

#day_order_countObject



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/api/overview_controller.rb', line 78

def day_order_count
	 return_data=Hash.new
	  prod_array=Array.new
	  best=ActiveRecord::Base.connection.execute("Select DATE(created_at),count(*) from spree_orders where payment_state in ('paid','completed','payment','complete') group by DATE(created_at) order by 1 DESC")
	  best.each do |pr|
		  prod_dtl=Hash.new
		  prod_dtl[:order_date]=pr[0]
		  prod_dtl[:order_count]=pr[1]
		  prod_array.push prod_dtl
	  end
	  return_data[:orders] = prod_array
	  render :json => return_data.to_json, :status => 201
end

#day_order_valueObject



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/api/overview_controller.rb', line 91

def day_order_value
	 return_data=Hash.new
	  prod_array=Array.new
	  best=ActiveRecord::Base.connection.execute("Select DATE(created_at),sum(total) from spree_orders where payment_state in ('paid','completed','payment','complete') group by DATE(created_at) order by 1 DESC")
	  best.each do |pr|
		  prod_dtl=Hash.new
		  prod_dtl[:order_date]=pr[0]
		  prod_dtl[:total_order_value]=pr[1]
		  prod_array.push prod_dtl
	  end
	  return_data[:orders] = prod_array
	  render :json => return_data.to_json, :status => 201
end

#gross_selling_productsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/api/overview_controller.rb', line 17

def gross_selling_products
 return_data=Hash.new
 prod_array=Array.new
 best=ActiveRecord::Base.connection.execute("Select A.id,A.name,sum(B.cost_price * C.quantity) amount from spree_products A, spree_variants B, spree_line_items C,spree_orders D where A.id=B.product_id and B.id=C.variant_id and C.order_id=D.id and D.payment_state in ('paid','completed','payment','complete') group by A.id,A.name order by 3,1")
 best.each do |pr|
  prod_dtl=Hash.new
  prod_dtl[:id]=pr[0]
  prod_dtl[:name]=pr[1]
  prod_dtl[:amount]=pr[2]
  prod_array.push prod_dtl
 end
 return_data[:products] = prod_array
 render :json => return_data.to_json, :status => 201
end

#month_order_countObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/api/overview_controller.rb', line 118

def month_order_count
 return_data=Hash.new
  prod_array=Array.new
  best=ActiveRecord::Base.connection.execute("Select Month(created_at),Year(created_at),count(*) from spree_orders where payment_state in ('paid','completed','payment','complete') group by Month(created_at),Year(created_at) order by 2 DESC ,1 DESC")
  best.each do |pr|
	  prod_dtl=Hash.new
	  prod_dtl[:order_month]=pr[0]
	  prod_dtl[:order_year]=pr[1]
	  prod_dtl[:order_count]=pr[2]
	  prod_array.push prod_dtl
  end
  return_data[:orders] = prod_array
  render :json => return_data.to_json, :status => 201
end

#month_order_valueObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/controllers/api/overview_controller.rb', line 104

def month_order_value
	 return_data=Hash.new
	  prod_array=Array.new
	  best=ActiveRecord::Base.connection.execute("Select Month(created_at),Year(created_at),sum(total) from spree_orders where payment_state in ('paid','completed','payment','complete') group by Month(created_at),Year(created_at) order by 2 DESC ,1 DESC")
	  best.each do |pr|
		  prod_dtl=Hash.new
		  prod_dtl[:order_month]=pr[0]
		  prod_dtl[:order_year]=pr[1]
		  prod_dtl[:total_order_value]=pr[2]
		  prod_array.push prod_dtl
	  end
	  return_data[:orders] = prod_array
	  render :json => return_data.to_json, :status => 201
end

#out_of_stockObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/api/overview_controller.rb', line 64

def out_of_stock
	 return_data=Hash.new
	  prod_array=Array.new
	  best=ActiveRecord::Base.connection.execute("Select A.id,A.name,B.count_on_hand from spree_products A, spree_variants B where A.id=B.product_id and B.count_on_hand <=0 order by 1,2")
	  best.each do |pr|
		  prod_dtl=Hash.new
		  prod_dtl[:id]=pr[0]
		  prod_dtl[:name]=pr[1]
		  prod_dtl[:count_on_hand]=pr[2]
		  prod_array.push prod_dtl
	  end
	  return_data[:products] = prod_array
	  render :json => return_data.to_json, :status => 201
end

#recent_ordersObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/api/overview_controller.rb', line 47

def  recent_orders
	 return_data=Hash.new
  prod_array=Array.new
  best=ActiveRecord::Base.connection.execute("Select A.id,A.email,D.id,D.number,D.created_at,D.total from spree_users A, spree_orders D where A.id=D.user_id and D.payment_state in ('paid','completed','payment','complete') order by 4,3")
  best.each do |pr|
	  prod_dtl=Hash.new
	  prod_dtl[:user_id]=pr[0]
	  prod_dtl[:email]=pr[1]
	  prod_dtl[:order_id]=pr[2]
	  prod_dtl[:order_number]=pr[3]
	  prod_dtl[:order_date]=pr[4]
	  prod_dtl[:order_total]=pr[5]
	  prod_array.push prod_dtl
  end
  return_data[:orders] = prod_array
  render :json => return_data.to_json, :status => 201
end

#top_spendersObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/api/overview_controller.rb', line 32

def top_spenders
 return_data=Hash.new
  prod_array=Array.new
  best=ActiveRecord::Base.connection.execute("Select A.id,A.email,sum(C.quantity),sum(B.cost_price * C.quantity) from spree_users A, spree_variants B, spree_line_items C, spree_orders D where A.id=D.user_id and B.id=C.variant_id and C.order_id=D.id and D.payment_state in ('paid','completed','payment','complete') group by A.id,A.email order by 4,1")
  best.each do |pr|
	  prod_dtl=Hash.new
	  prod_dtl[:id]=pr[0]
	  prod_dtl[:email]=pr[1]
	  prod_dtl[:qty]=pr[2]
	  prod_dtl[:value]=pr[3]
	  prod_array.push prod_dtl
  end
  return_data[:spenders] = prod_array
  render :json => return_data.to_json, :status => 201
end