Class: Api::AnalysesController
- Inherits:
-
ActionController::Base
- Object
- ActionController::Base
- Api::AnalysesController
- Defined in:
- app/controllers/api/analyses_controller.rb
Instance Method Summary collapse
- #best_selling_products ⇒ Object
- #day_order_count ⇒ Object
- #day_order_value ⇒ Object
- #gross_selling_products ⇒ Object
- #month_order_count ⇒ Object
- #month_order_value ⇒ Object
- #out_of_stock ⇒ Object
- #recent_orders ⇒ Object
- #top_spenders ⇒ Object
Instance Method Details
#best_selling_products ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'app/controllers/api/analyses_controller.rb', line 3 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 products A, variants B, line_items C,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_count ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'app/controllers/api/analyses_controller.rb', line 79 def day_order_count return_data=Hash.new prod_array=Array.new best=ActiveRecord::Base.connection.execute("Select DATE(created_at),count(*) from 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_value ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/controllers/api/analyses_controller.rb', line 92 def day_order_value return_data=Hash.new prod_array=Array.new best=ActiveRecord::Base.connection.execute("Select DATE(created_at),sum(total) from 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_products ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/controllers/api/analyses_controller.rb', line 18 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 products A, variants B, line_items C,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_count ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/controllers/api/analyses_controller.rb', line 119 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 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_value ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/controllers/api/analyses_controller.rb', line 105 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 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_stock ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/controllers/api/analyses_controller.rb', line 65 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 products A, 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_orders ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/controllers/api/analyses_controller.rb', line 48 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 users A, 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_spenders ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/controllers/api/analyses_controller.rb', line 33 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 users A, variants B, line_items C, 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 |