class Api::BatchesController < ApplicationController
  skip_before_filter  :verify_authenticity_token
	def create
		session_token=params[:session_token]    
		check_member= Member.authenticate_session_token(session_token)
		if !check_member.nil?
		    date = DateTime.now.beginning_of_day.utc.to_time.strftime("%Y-%m-%d") 
		    order_ref = params[:order_ref]
        order_ref=order_ref.delete(' ')	    
        adult_or_child=params[:adult_or_child]
        client_id=params[:client_id]
        product_category_id=params[:product_category_id]

        find_batch=Batch.find_by_order_ref(order_ref)
        if find_batch.nil?
            find_client=Client.find_by_id(client_id)
            if !find_client.nil? 
                find_product_category=ProductCategory.find_by_id(product_category_id)
                if !find_product_category.nil? 
                   user_id=check_member.user_id             

        		        batch = Batch.create_batch(date,order_ref,user_id,adult_or_child,client_id,product_category_id)
            		    if !batch.nil?
            		    	 @out=true,batch.id
            		    else
            		    	 @out=false,'Error occurs in creating batch!'
            		    end
                else
                   @out=false,'Product Category does not exists!'
                end
            else
              @out = false, "Client does not exist!" 
            end
        else
            @out=false,order_ref.to_s+ " is already taken!"
        end
		else
			  @out = false, "Sorry!Unauthorized user!"   
		end
  end
  def batch_end
  		session_token=params[:session_token]  
  		batch_id=params[:batch_id]
  	 
  		check_member= Member.authenticate_session_token(session_token)

  		if  !check_member.nil?
	  		batch=Batch.find_by_id(batch_id)
	  		if !batch.nil?
		  		batch.batch_end_time = DateTime.now.beginning_of_day.utc.to_time.strftime("%Y-%m-%d")
		    	batch.batch_end = true
		    	if batch.save
		    		@out=true,"Batch end process is successfully finished."
		    	else
		    		@out=false,'Error occurs in  batch end process!'
		    	end
		    else
		    	@out=false,'Invalid batch no!'
		    end
	    else
	    	@out = false, "Sorry!Unauthorized user!"  
	    end
  	end
  	def resume_batch
  		session_token=params[:session_token]
  	    batch_id=params[:batch_id]

  		check_member= Member.authenticate_session_token(session_token)
  		if !check_member.nil?  			 
  			batch=Batch.find_by_id(batch_id)
        batch_line_count=0
        batch_line_items= BatchLineItem.limit(1)   
        if !batch_line_items.empty?
  			   batch_line_count=batch.batch_line_item.count
        end
  			@out=true,batch_line_count
  		else
  			@out = false, "Sorry!Unauthorized user!"  
  		end
  	end
  	def batch_progress_list 
     session_token=params[:session_token]  
  	 user_id=params[:created_by]  

     check_member= Member.authenticate_session_token(session_token)
      if !check_member.nil?  
    	    if !user_id.nil?  	    	
    	    	batches = Batch.where('batch_end is null and user_id=?', user_id)    	    
    	    	if !batches.empty?
    	    		@out=true,batches
    	    	else
    	    		@out = false, batches
    	    	end
    	    else
    	    	@out = false, "Sorry!Unauthorized user!"  
    	    end   	
      else
            @out = false, "Sorry!Unauthorized user!" 
      end
  	end

    def get_ticket_settings
      session_token=params[:session_token]    
      check_member= Member.authenticate_session_token(session_token)
      if !check_member.nil?
        settings =Setting.where("key=?",'ticket_definition_code')
        arr_setting=Array.new
        settings.each do |set|
          str={:id => set.id,:value => set.value,:name => set.name}
                arr_setting.push(str)
              end
              @out=true,arr_setting                                                                                                                                                                                                                                                                                                                                               
      else
        @out=false,"Sorry!Unauthorized user!" 
      end
    end

    
end