Files
mozo-backend/app/controllers/lists_controller.rb
T
2012-08-24 16:20:03 +02:00

133 lines
3.2 KiB
Ruby

class ListsController < ApplicationController
before_filter :set_relation_options, only: [:new, :edit, :create, :update]
# GET /lists
# GET /lists.json
def index
@lists = List.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @lists }
end
end
# GET /lists/1
# GET /lists/1.json
def show
@list = List.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @list }
end
end
# GET /lists/new
# GET /lists/new.json
def new
@list = List.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @list }
end
end
# GET /lists/1/edit
def edit
@list = List.find(params[:id])
end
# POST /lists
# POST /lists.json
def create
@list = List.new(params[:list])
respond_to do |format|
if @list.save
format.html { redirect_to @list, notice: t('action.create.successfull', model: List.model_name.human) }
format.json { render json: @list, status: :created, location: @list }
else
format.html { render action: "new" }
format.json { render json: @list.errors, status: :unprocessable_entity }
end
end
end
# PUT /lists/1
# PUT /lists/1.json
def update
@list = List.find(params[:id])
respond_to do |format|
if @list.update_attributes(params[:list])
format.html { redirect_to @list, notice: t('action.update.successfull', model: List.model_name.human) }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @list.errors, status: :unprocessable_entity }
end
end
end
# DELETE /lists/1
# DELETE /lists/1.json
def destroy
@list = List.find(params[:id])
@list.destroy
respond_to do |format|
format.html { redirect_to lists_url, notice: t('action.destroy.successfull', model: List.model_name.human) }
format.json { head :no_content }
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
end
end