end of day commit for users and suppliers authenticated handling

This commit is contained in:
2012-08-25 15:55:56 +02:00
parent b7c57d41ce
commit e56badcbf8
29 changed files with 435 additions and 582 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ class ApplicationController < ActionController::Base
end
def active_list
return nil unless session[:active_list_id]
return nil unless session[:active_list_id].present?
@active_list ||= List.find(session[:active_list_id])
end
+13 -60
View File
@@ -15,92 +15,45 @@ class DashboardController < ApplicationController
render layout: 'phone'
end
def create_list
@table = Table.find(params[:table_id])
if @table.occupied?
redirect_to root_path, alert: t('table.is_occupied')
else
@list = List.new(table: @table)
#@list.add_user(current_user)
@list.save
session[:active_list_id] = @list.id
redirect_to action: :show_products
end
end
def show_products
@supplier = Supplier.first
render layout: 'phone'
end
def order_active_products_list
respond_to do |format|
format.html do
redirect_to(root_path, alert: t('messages.cannot_order_without_list_id')) and return if params[:list_id].blank?
@list = List.find(params[:list_id])
redirect_to(root_path, alert: t('messages.cannot_order_on_non_active_list')) and return unless @list.active?
@list.place_order params[:products]
@list.place_order current_user, params[:products]
redirect_to root_path, notice: t('messages.order_is_placed')
end
format.js do
render js: js_alert(t('messages.cannot_order_without_list_id')) and return if params[:list_id].blank?
@list = List.find(params[:list_id])
render js: js_alert(t('messages.cannot_order_on_non_active_list')) and return unless @list.active?
@list.place_order params[:products]
@list.place_order current_user, params[:products]
render js: js_notice( t('messages.order_is_placed') )
end
end
end
def view_active_list
redirect_to(root_path, alert: t('messages.there_is_no_list_active')) and return unless session[:active_list_id].present?
render layout: 'phone'
end
##
# Displays the closed lists of the user
def user_history
render layout: 'phone'
end
def user_list_info
# GET /suppliers/1/product_list
# GET /suppliers/1/product_list.json
def product_list
@supplier = active_list.supplier
respond_to do |format|
format.html # show.html.erb
format.json do
render json: {list_active: false} and return unless session[:active_list_id].present?
render json: active_list.as_json.merge(list_active: true)
products = active_list.supplier.products
products.include_relation(:product_categories)
products.sort_by!{|p| p.product_category.try(:position) || 90000}
h = products.inject({}){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h}
render json: h
end
end
end
def supplier_home
redirect_to active_orders_supplier_path(Supplier.first)
end
def supplier_lists
redirect_to active_lists_supplier_path(Supplier.first)
end
# POST /active_user_list_needs_help.json
def active_user_list_needs_help
respond_to do |format|
format.json do
render json: {list_active: false} and return unless session[:active_list_id].present?
active_list.needs_help = true
active_list.save
render json: active_list.as_json.merge(list_active: true)
end
end
end
# POST /active_user_list_needs_payment.json
def active_user_list_needs_payment
respond_to do |format|
format.json do
render json: {list_active: false} and return unless session[:active_list_id].present?
active_list.needs_payment = true
active_list.save
render json: active_list.as_json.merge(list_active: true)
end
end
end
end
+1 -42
View File
@@ -82,51 +82,10 @@ class ListsController < ApplicationController
end
end
def current
@list = List.find(params[:id])
@list.orders.include_relations(product_orders: :product)
respond_to do |format|
format.json do
h = @list.as_json
h[:orders] = []
list_total = 0.0
for order in @list.orders
ho = {products: []}
order_total = 0.0
for product_order in order.product_orders
order_total += (product_order.amount * product_order.price).round(2)
ho[:products] << {name: product_order.product.name, id: product_order.product_id, number: product_order.amount, price: product_order.price}
end
ho[:total_amount] = order_total.round(2)
ho[:state] = order.state
list_total += ho[:total_amount]
h[:orders] << ho
end
h[:total_amount] = list_total.round(2)
render json: h
end
end
end
# POST /orders/1/is_closed
def is_closed
@list = List.find(params[:id])
@list.close!
render nothing: true
end
# POST /orders/1/is_helped
def is_helped
@list = List.find(params[:id])
@list.needs_help = false
@list.save
render nothing: true
end
private
def set_relation_options
@tables = Table.all
@suppliers = Supplier.all
end
end
-14
View File
@@ -82,20 +82,6 @@ class OrdersController < ApplicationController
end
end
# POST /orders/1/is_being_processed
def is_being_processed
@order = Order.find(params[:id])
@order.is_being_processed!
render nothing: true
end
# POST /orders/1/is_delivered
def is_delivered
@order = Order.find(params[:id])
@order.is_delivered!
render nothing: true
end
private
def set_relation_options
+84
View File
@@ -0,0 +1,84 @@
class SupplierController < ApplicationController
before_filter :authenticate_supplier!
# GET /suppliers/1/active_orders
# GET /suppliers/1/active_orders.json
def active_orders
@supplier = current_supplier
respond_to do |format|
format.html { render layout: 'tablet' }
format.json do
h = @supplier.as_json
h[:orders] = []
list_total = 0.0
for order in @supplier.active_orders
ho = {products: []}
order_total = 0.0
for product_order in order.product_orders
order_total += (product_order.amount * product_order.price).round(2)
ho[:products] << {name: product_order.product.name, id: product_order.product_id, number: product_order.amount, price: product_order.price}
end
ho[:total_amount] = order_total.round(2)
ho[:state] = order.state
ho[:table_number] = order.table_number
ho[:id] = order.id
list_total += ho[:total_amount]
h[:orders] << ho
end
h[:total_amount] = list_total.round(2)
render json: h, layout: 'tablet'
end
end
end
# GET /suppliers/1/active_lists
# GET /suppliers/1/active_lists.json
def active_lists
@supplier = current_supplier
respond_to do |format|
format.html { render layout: 'tablet' }
format.json do
h = @supplier.as_json
h[:lists] = []
grand_total = 0.0
for list in @supplier.active_lists
hl = list.as_json
hl[:total_amount] = list.orders.inject(0.0){|sum, o| sum + o.product_orders.inject(0.0){|s, po| s + (po.amount * po.price).round(2)}}.round(2)
grand_total += hl[:total_amount]
h[:lists] << hl
end
h[:total_amount] = grand_total.round(2)
render json: h, layout: 'tablet'
end
end
end
# POST /supplier/close_list
def close_list
@list = List.find_by_supplier_id_and_id(current_supplier.id, params[:list_id])
@list.close!
render nothing: true
end
# POST /orders/1/is_helped
def mark_list_as_helped
@list = List.find_by_supplier_id_and_id(current_supplier.id, params[:list_id])
@list.needs_help = false
@list.save
render nothing: true
end
# POST /orders/1/is_being_processed
def mark_order_in_process
@order = Order.find_by_supplier_id_and_id(current_supplier.id, params[:order_id])
@order.is_being_processed!
render nothing: true
end
# POST /orders/1/is_delivered
def order_is_delivered
@order = Order.find_by_supplier_id_and_id(current_supplier.id, params[:order_id])
@order.is_delivered!
render nothing: true
end
end
-69
View File
@@ -82,75 +82,6 @@ class SuppliersController < ApplicationController
end
end
# GET /suppliers/1/product_list
# GET /suppliers/1/product_list.json
def product_list
@supplier = Supplier.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json do
products = @supplier.products
products.include_relation(:product_categories)
products.sort_by!{|p| p.product_category.try(:position) || 90000}
h = products.inject({}){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h}
render json: h
end
end
end
# GET /suppliers/1/active_orders
# GET /suppliers/1/active_orders.json
def active_orders
@supplier = Supplier.find(params[:id])
respond_to do |format|
format.html { render layout: 'tablet' }
format.json do
h = @supplier.as_json
h[:orders] = []
list_total = 0.0
for order in @supplier.active_orders
ho = {products: []}
order_total = 0.0
for product_order in order.product_orders
order_total += (product_order.amount * product_order.price).round(2)
ho[:products] << {name: product_order.product.name, id: product_order.product_id, number: product_order.amount, price: product_order.price}
end
ho[:total_amount] = order_total.round(2)
ho[:state] = order.state
ho[:table_number] = order.table_number
ho[:id] = order.id
list_total += ho[:total_amount]
h[:orders] << ho
end
h[:total_amount] = list_total.round(2)
render json: h, layout: 'tablet'
end
end
end
# GET /suppliers/1/active_lists
# GET /suppliers/1/active_lists.json
def active_lists
@supplier = Supplier.find(params[:id])
respond_to do |format|
format.html { render layout: 'tablet' }
format.json do
h = @supplier.as_json
h[:lists] = []
grand_total = 0.0
for list in @supplier.active_lists
hl = list.as_json
hl[:total_amount] = list.orders.inject(0.0){|sum, o| sum + o.product_orders.inject(0.0){|s, po| s + (po.amount * po.price).round(2)}}.round(2)
grand_total += hl[:total_amount]
h[:lists] << hl
end
h[:total_amount] = grand_total.round(2)
render json: h, layout: 'tablet'
end
end
end
private
def set_relation_options
+111
View File
@@ -0,0 +1,111 @@
class UserController < ApplicationController
before_filter :authenticate_user!
alias :list :active_list
# POST /user/create_list {table_id: 1234}
def create_list
@table = Table.find(params[:table_id])
if @table.occupied?
redirect_to root_path, alert: t('table.is_occupied')
else
@list = List.new(table: @table, supplier_id: @table.supplier_id)
@list.add_user current_user
#@list.add_user(current_user)
@list.save
session[:active_list_id] = @list.id
redirect_to user_list_products_path
end
end
def list_products
@supplier = Supplier.first
render layout: 'phone'
end
# GET /user/current_list.json
# Information about the currently active list
# This information includes detailed order information
def active_list
respond_to do |format|
format.html do
redirect_to(root_path, alert: t('messages.there_is_no_list_active')) and return unless list.present?
render layout: 'phone'
end
format.json do
@list = list
@list.orders.include_relations(product_orders: :product)
h = @list.as_json
h[:orders] = []
h[:list_active] = @list.active?
list_total = 0.0
for order in @list.orders
ho = {products: []}
order_total = 0.0
for product_order in order.product_orders
order_total += (product_order.amount * product_order.price).round(2)
ho[:products] << {name: product_order.product.name, id: product_order.product_id, number: product_order.amount, price: product_order.price}
end
ho[:total_amount] = order_total.round(2)
ho[:state] = order.state
list_total += ho[:total_amount]
h[:orders] << ho
end
h[:total_amount] = list_total.round(2)
render json: h
end
end
end
# GET /user/list_info.json
# Information about the currently active list
# Fast version to verify wether the is is still currently active
def list_info
respond_to do |format|
format.json do
render json: {list_active: false} and return unless list.present?
render json: list.as_json.merge(list_active: true)
end
end
end
# POST /user/needs_help.json
def needs_help
respond_to do |format|
format.json do
render json: {list_active: false} and return unless list.present?
list.needs_help = true
list.save
render json: list.as_json.merge(list_active: true)
end
end
end
# POST /user/list_needs_payment.json
def list_needs_payment
respond_to do |format|
format.json do
render json: {list_active: false} and return unless list.present?
list.needs_payment = true
list.save
render json: list.as_json.merge(list_active: true)
end
end
end
##
# Displays the closed lists of the user
def list_history
render layout: 'phone'
end
##
# Displays a closed list of the user
# GET /user/list_history/:list_id
def history_list
@list = List.find(params[:list_id])
redirect_to user_root_path, alert: t('messages.illegal_history_list_attempt') and return unless @list.user_ids.include?(current_user.id)
render layout: 'phone'
end
end