class Inventory::InventoryDefinitionsController < BaseInventoryController before_action :set_inventory_definition, only: [:show, :edit, :update, :destroy] # GET /inventory_definitions # GET /inventory_definitions.json def index @inventory_definitions = InventoryDefinition.all end # GET /inventory_definitions/1 # GET /inventory_definitions/1.json def show end # GET /inventory_definitions/new def new @menus = Menu.order('created_at asc') @menu = MenuCategory.active.where("menu_id =#{@menus[0].id}").order('order_by asc') @inventory_definition = InventoryDefinition.new end # GET /inventory_definitions/1/edit def edit end # POST /inventory_definitions # POST /inventory_definitions.json def create inventory = InventoryDefinition.find_by_item_code(params[:item_code]) if inventory.nil? @inventory_definition = InventoryDefinition.new @inventory_definition.item_code = params[:item_code] @inventory_definition.min_order_level = params[:min_order_level] @inventory_definition.max_stock_level = params[:max_stock_level] @inventory_definition.save else @inventory_definition = InventoryDefinition.find(inventory.id) @inventory_definition.min_order_level = params[:min_order_level] @inventory_definition.max_stock_level = inventory.max_stock_level.to_i + params[:max_stock_level].to_i end @inventory_definition.created_by = current_user.id if @inventory_definition.save result = {:status=> true, :message => "Inventory definition was created successfully",:data=> @inventory_definition} else result = {:status=> false, :message => "Inventory definition was created successfully",:data=> @inventory_definition} end render :json => result.to_json # inventory = InventoryDefinition.find_by_item_code(inventory_definition_params[:item_code]) # if inventory.nil? # @inventory_definition = InventoryDefinition.new(inventory_definition_params) # else # @inventory_definition = InventoryDefinition.find(inventory.id) # @inventory_definition.min_order_level = inventory_definition_params[:min_order_level] # @inventory_definition.max_stock_level = inventory.max_stock_level.to_i + inventory_definition_params[:max_stock_level].to_i # end # @inventory_definition.created_by = current_user.id # respond_to do |format| # if @inventory_definition.save # format.html { redirect_to inventory_path, notice: 'Inventory definition was successfully created.' } # format.json { render :show, status: :created, location: @inventory_definition } # else # format.html { render :new } # format.json { render json: @inventory_definition.errors, status: :unprocessable_entity } # end # end end # PATCH/PUT /inventory_definitions/1 # PATCH/PUT /inventory_definitions/1.json def update respond_to do |format| if @inventory_definition.update(inventory_definition_params) format.html { redirect_to @inventory_definition, notice: 'Inventory definition was successfully updated.' } format.json { render :show, status: :ok, location: @inventory_definition } else format.html { render :edit } format.json { render json: @inventory_definition.errors, status: :unprocessable_entity } end end end # DELETE /inventory_definitions/1 # DELETE /inventory_definitions/1.json def destroy inventory = InventoryDefinition.find_by_id(params[:id]) @inventory_definition.destroy respond_to do |format| format.html { redirect_to inventory_inventory_definitions_url, notice: 'Inventory definition was successfully destroyed.' } format.json { head :no_content } end StockJournal.delete_stock_journal(inventory.item_code) StockCheckItem.delete_stock_check_item(inventory.item_code) if !inventory.nil? inventory.destroy flash[:message] = 'Inventory was successfully destroyed.' render :json => {:status=> "Success", :url => inventory_url }.to_json else flash[:error] = 'Inventory could not destroy! This record is using in somewhere.' render :json => {:status=> "Error", :url => inventory_url }.to_json end end private # Use callbacks to share common setup or constraints between actions. def set_inventory_definition @inventory_definition = InventoryDefinition.find_by_id(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def inventory_definition_params params.require(:inventory_definition).permit(:item_code, :min_order_level, :max_stock_level) end end