166 lines
5.3 KiB
Ruby
166 lines
5.3 KiB
Ruby
module Suppliers
|
|
class ListsController < Suppliers::ApplicationController
|
|
# GET /lists
|
|
# GET /lists.json
|
|
def index
|
|
if params[:state] == 'active'
|
|
@lists = List.active_for_supplier(current_supplier, page: params[:page], per_page: params[:per_page] || 25)
|
|
elsif params[:date].present?
|
|
@date = params[:date].present? ? (Date.parse(params[:date]) rescue Date.today) : Date.today
|
|
@time = @date.to_time(:utc)
|
|
@start_time = @time.beginning_of_day
|
|
@end_time = @time.end_of_day
|
|
if current_supplier.night_offset.present?
|
|
@start_time += current_supplier.night_offset.to_f.hours
|
|
@end_time += current_supplier.night_offset.to_f.hours
|
|
end
|
|
@lists = List.for_supplier_created_at current_supplier, @start_time..@end_time
|
|
#@lists.include_relation(:table) # for number
|
|
else
|
|
@lists = List.for_supplier(current_supplier, page: params[:page], per_page: params[:per_page] || 25)
|
|
end
|
|
@lists.include_relation(:table, :users, orders: {product_orders: :product})
|
|
|
|
render json: @lists, each_serializer: SupplierListSerializer
|
|
end
|
|
|
|
|
|
def active
|
|
@lists = List.active_for_supplier(current_supplier)
|
|
@lists.include_relation(:table) # for number
|
|
|
|
respond_to do |format|
|
|
format.html # index.html.erb
|
|
format.json { render json: @lists }
|
|
end
|
|
end
|
|
|
|
def at_date
|
|
@date = params[:date].present? ? (Date.parse(params[:date]) rescue Date.today) : Date.today
|
|
@time = @date.to_time(:utc)
|
|
@start_time = @time.beginning_of_day
|
|
@end_time = @time.end_of_day
|
|
if current_supplier.night_offset.present?
|
|
@start_time += current_supplier.night_offset.to_f.hours
|
|
@end_time += current_supplier.night_offset.to_f.hours
|
|
end
|
|
@lists = List.for_supplier_created_at current_supplier, @start_time..@end_time
|
|
@lists.include_relation(:table) # for number
|
|
|
|
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_by_supplier_id_and_id!(current_supplier.id, params[:id])
|
|
respond_to do |format|
|
|
format.html {}
|
|
format.json do
|
|
if params[:old_style] then
|
|
render json: @list.with_orders_as_json
|
|
else
|
|
render json: @list, serializer: SupplierListSerializer
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
# GET /lists/1/extra_info
|
|
# GET /lists/1/extra_info.json
|
|
def extra_info
|
|
@list = List.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
|
|
render layout: false
|
|
end
|
|
|
|
# GET /lists/new
|
|
# GET /lists/new.json
|
|
def new
|
|
@list = List.new
|
|
@list.section_id = params[:section_id].presence
|
|
@tables = current_supplier.active_tables
|
|
|
|
respond_to do |format|
|
|
format.html # new.html.erb
|
|
format.json { render json: @list }
|
|
end
|
|
end
|
|
|
|
# POST /lists
|
|
# POST /lists.json
|
|
def create
|
|
@list = List.new(list_params)
|
|
@list.supplier = current_supplier
|
|
|
|
respond_to do |format|
|
|
if @list.save
|
|
format.html { redirect_to [:suppliers, @list.section || @list], notice: t('action.create.successfull', model: List.model_name.human) }
|
|
format.json { render json: @list, status: :created, location: @list }
|
|
else
|
|
format.html do
|
|
@tables = current_supplier.active_tables
|
|
render action: "new"
|
|
end
|
|
format.json { render json: {errors: @list.errors}, status: :unprocessable_entity }
|
|
end
|
|
end
|
|
end
|
|
|
|
# GET /lists/1/edit
|
|
def edit
|
|
@list = List.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
|
|
@tables = current_supplier.active_tables
|
|
end
|
|
|
|
# PUT /lists/1
|
|
# PUT /lists/1.json
|
|
def update
|
|
@list = List.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
|
|
|
|
respond_to do |format|
|
|
if @list.update_attributes(list_params)
|
|
format.html { redirect_to [:suppliers, @list], notice: t('action.update.successfull', model: List.model_name.human) }
|
|
format.json { render json: @list }
|
|
else
|
|
format.html { render action: "edit" }
|
|
format.json { render json: {errors: @list.errors}, status: :unprocessable_entity }
|
|
end
|
|
end
|
|
end
|
|
|
|
# DELETE /lists/1
|
|
# DELETE /lists/1.json
|
|
def destroy
|
|
@list = List.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
|
|
@list.destroy
|
|
|
|
respond_to do |format|
|
|
format.html { redirect_to suppliers_lists_url, notice: t('action.destroy.successfull', model: List.model_name.human) }
|
|
format.json { head :no_content }
|
|
end
|
|
end
|
|
# POST /supplier/lists/1/close
|
|
def close
|
|
@list = List.find_by_supplier_id_and_id(current_supplier.id, params[:id])
|
|
@list.close!
|
|
head :ok
|
|
end
|
|
|
|
# POST /supplier/lists/1/mark_helped
|
|
def mark_helped
|
|
@list = List.find_by_supplier_id_and_id(current_supplier.id, params[:id])
|
|
@list.mark_helped!
|
|
head :ok
|
|
end
|
|
|
|
private
|
|
|
|
def list_params
|
|
params.require(:list).permit(:state, :needs_help, :needs_payment, :closed_at, :join_requests, :price, :is_paid, :paid_at, :table_id, :section_id)
|
|
end
|
|
end
|
|
end
|