improvements

This commit is contained in:
2012-09-06 14:49:22 +02:00
parent 27d7fd304e
commit 161da7a82d
18 changed files with 209 additions and 81 deletions
+13 -2
View File
@@ -39,7 +39,7 @@ class SupplierController < ApplicationController
h = @supplier.as_json
h[:orders] = []
list_total = 0.0
for order in @supplier.active_orders
for order in @supplier.active_orders(section_id: params[:section_id].presence)
ho = {products: []}
order_total = 0.0
for product_order in order.product_orders
@@ -70,7 +70,7 @@ class SupplierController < ApplicationController
h = @supplier.as_json
h[:lists] = []
grand_total = 0.0
for list in @supplier.active_lists
for list in @supplier.active_lists(section_id: params[:section_id].presence)
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]
@@ -111,4 +111,15 @@ class SupplierController < ApplicationController
@order.is_delivered!
render nothing: true
end
# GET /supplier/lists/1
def show_list
@list = List.find_by_supplier_id_and_id(current_supplier.id, params[:list_id])
respond_to do |format|
format.html {}
format.json do
render json: @list.with_orders_as_json
end
end
end
end
+12 -41
View File
@@ -58,7 +58,7 @@ class UserController < ApplicationController
products = list.supplier.products
products.include_relation(:product_categories)
products.sort_by!{|p| p.product_category.try(:position) || 90000}
h = products.inject({table_number: list.table_number}){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h}
h = products.inject({table_number: list.table_number, supplier_name: @supplier.name}){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h}
render json: h
end
end
@@ -130,7 +130,7 @@ class UserController < ApplicationController
products = @table.supplier.products
products.include_relation(:product_categories)
products.sort_by!{|p| p.product_category.try(:position) || 90000}
h = products.inject({table_number: @table.number}){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h}
h = products.inject({table_number: @table.number, supplier_name: @table.supplier.name}){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h}
render json: h
end
end
@@ -146,35 +146,7 @@ class UserController < ApplicationController
render layout: 'phone'
end
format.json do
@list = list
@list.orders.include_relations(product_orders: :product)
h = @list.as_json
# Handle join requests
if @list.join_requests.any?
h[:join_requests] = []
for user in CouchPotato.database.load_document(@list.join_requests)
h[:join_requests] << {user_id: user.id, user_email: user.email}
end
end
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
render json: list.with_orders_and_join_requests_as_json.merge(supplier_name: list.supplier.name)
end
end
end
@@ -191,15 +163,7 @@ class UserController < ApplicationController
render json: {list_active: false}
return
else
list_obj = list.as_json.merge(list_active: list.active? )
# Handle join requests
if list.join_requests.any?
list_obj[:join_requests] = []
for user in CouchPotato.database.load_document(list.join_requests)
list_obj[:join_requests] << {user_id: user.id, user_email: user.email}
end
end
list_obj = list.as_json.merge(list_active: list.active? ).merge(list.join_requests_as_json)
render json: list_obj
end
end
@@ -248,7 +212,14 @@ class UserController < ApplicationController
flash.now[:notice] = t('messages.the_list_has_been_closed', list: List.model_name.human)
end
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'
respond_to do |format|
format.html do
render layout: 'phone'
end
format.json do
render json: list.with_orders_as_json
end
end
end