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