end of day commit

This commit is contained in:
2012-08-23 18:50:06 +02:00
parent 13dd2bf335
commit 0bae1bcbed
37 changed files with 1157 additions and 83 deletions
+23 -2
View File
@@ -1,10 +1,31 @@
class ApplicationController < ActionController::Base
protect_from_forgery
private
def active_list
List.find(session[:list_id])
def check_active_list_state
if session[:active_list_id]
unless active_list.active?
session[:active_list_id] = nil
redirect_to root_path, alert: t('messages.the_list_has_been_closed')
end
end
end
def active_list
return nil unless session[:active_list_id]
@active_list ||= List.find(session[:active_list_id])
end
def js_alert(message)
{ok: false, message: message}.to_json
end
def js_notice(message)
{ok: true, message: message}.to_json
end
helper_method :active_list
end
+27 -2
View File
@@ -1,5 +1,6 @@
class DashboardController < ApplicationController
before_filter :check_active_list_state, except: :home
def home
end
@@ -17,8 +18,8 @@ class DashboardController < ApplicationController
@list = List.new(table: @table)
#@list.add_user(current_user)
@list.save
session[:list_id] = @list.id
redirect_to root_path
session[:active_list_id] = @list.id
redirect_to action: :show_products
end
end
@@ -27,4 +28,28 @@ class DashboardController < ApplicationController
end
def order_active_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]
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]
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?
end
end
+34
View File
@@ -82,6 +82,40 @@ 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.product.price).round(2)
ho[:products] << {name: product_order.product.name, id: product_order.product_id, number: product_order.amount, price: product_order.product.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_delivered
def is_closed
@list = List.find(params[:id])
@list.close!
render nothing: true
end
private
def set_relation_options
+104
View File
@@ -0,0 +1,104 @@
class OrdersController < ApplicationController
before_filter :set_relation_options, only: [:new, :edit, :create, :update]
# GET /orders
# GET /orders.json
def index
@orders = Order.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @orders }
end
end
# GET /orders/1
# GET /orders/1.json
def show
@order = Order.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @order }
end
end
# GET /orders/new
# GET /orders/new.json
def new
@order = Order.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @order }
end
end
# GET /orders/1/edit
def edit
@order = Order.find(params[:id])
end
# POST /orders
# POST /orders.json
def create
@order = Order.new(params[:order])
respond_to do |format|
if @order.save
format.html { redirect_to @order, notice: t('action.create.successfull', model: Order.model_name.human) }
format.json { render json: @order, status: :created, location: @order }
else
format.html { render action: "new" }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end
# PUT /orders/1
# PUT /orders/1.json
def update
@order = Order.find(params[:id])
respond_to do |format|
if @order.update_attributes(params[:order])
format.html { redirect_to @order, notice: t('action.update.successfull', model: Order.model_name.human) }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end
# DELETE /orders/1
# DELETE /orders/1.json
def destroy
@order = Order.find(params[:id])
@order.destroy
respond_to do |format|
format.html { redirect_to orders_url, notice: t('action.destroy.successfull', model: Order.model_name.human) }
format.json { head :no_content }
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
@lists = List.all
end
end
+52
View File
@@ -98,6 +98,58 @@ class SuppliersController < ApplicationController
end
end
end
# GET /suppliers/1/active_order_list
# GET /suppliers/1/active_order_list.json
def active_order_list
@supplier = Supplier.find(params[:id])
respond_to do |format|
format.html
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.product.price).round(2)
ho[:products] << {name: product_order.product.name, id: product_order.product_id, number: product_order.amount, price: product_order.product.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
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
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.product.price).round(2)}}.round(2)
grand_total += hl[:total_amount]
h[:lists] << hl
end
h[:total_amount] = grand_total.round(2)
render json: h
end
end
end
private
def set_relation_options