<commit_before>class Spree::Admin::PosController < Spree::Admin::BaseController
  before_filter :check_valid_order, :except => [:new]
  helper_method :user_stock_locations
  before_filter :load_variant, :only => [:add, :remove]
  before_filter :ensure_pos_shipping_method, :only => [:new]
  before_filter :ensure_payment_method, :only => [:update_payment]

  def new
    @order = spree_current_user.unpaid_pos_orders.first
    @order ? add_error("You have an unpaid/empty order. Please either complete it or update items in the same order.") : init_pos
    redirect_to :action => :show , :number => @order.number
  end

  def find
    init_search

    #using the scope available_at_stock_location which should be defined according to app or removed if not required
    stock_location = user_stock_locations(spree_current_user).first
    @search = Spree::Variant.includes([:product]).available_at_stock_location(stock_location.id).ransack(params[:q])
    @variants = @search.result(:distinct => true).page(params[:page]).per(20)
  end

  def add
    @line_item = add_variant(@variant) if @variant.present?
    flash.notice = @line_item.errors[:base].present? ? 'Adding more than available' : Spree.t('product_added') if @line_item
    redirect_to :action => :show, :number => @order.number 
  end

  def remove
    line_item = @order.contents.remove(@variant, 1, @order.shipment)
    flash.notice = line_item.quantity != 0 ? 'Quantity Updated' : Spree.t('product_removed') 
    redirect_to :action => :show, :number => @order.number
  end

  def update_line_item_quantity
    item = @order.line_items.where(:id => params[:line_item_id]).first
    #TODO error handling
    item.quantity = params[:quantity].to_i
    item.save

    flash.notice = item.errors[:base].present? ? 'Adding more than available.' : 'Quantity Updated'      
    redirect_to :action => :show
  end

  def apply_discount
    if VALID_DISCOUNT_REGEX.match(params[:discount]) && params[:discount].to_f < 100
      @discount = params[:discount].to_f
      item = @order.line_items.where(:id => params[:line_item_id]).first
      item.price = item.variant.price * ( 1.0 - @discount/100.0 )
      item.save
    else
      flash[:notice] = 'Please enter a valid discount'
    end
    redirect_to :action => :show
  end

  def clean_order
    @order.clean!
    flash[:notice] = "Removed all items"
    redirect_to :action => :show, :number => @order.number
  end

  def associate_user
    @user = @order.associate_user_for_pos(params[:email].present? ? params[:email] : params[:new_email])
    if @user.errors.present?
      add_error "Could not add the user:#{@user.errors.full_messages.to_sentence}"
    else
      @order.save!
      flash[:notice] = 'Successfully Associated User'
    end

    redirect_to :action => :show, :number => @order.number
  end

  def update_payment
    @payment_method_id = params[:payment_method_id]
    @payment = @order.save_payment_for_pos(params[:payment_method_id], params[:card_name])
    if @payment.errors.blank?
      print
    else
      add_error @payment.errors.full_messages.to_sentence
      redirect_to :action => :show, :number => @order.number
    end
  end

  def update_stock_location
    @order.shipment.stock_location = user_stock_locations(spree_current_user).where(:id => params[:stock_location_id]).first
    @order.bill_address = @order.ship_address = @order.shipment.stock_location.address
    @order.save
    @order.shipment.save
    redirect_to :action => :show, :number => @order.number
  end

  private 
  
  def ensure_pos_shipping_method
    redirect_to '/', :flash => { :error => 'No shipping method available for POS orders. Please assign one.'} and return unless Spree::ShippingMethod.where(:name => SpreePos::Config[:pos_shipping]).first
  end

  def load_order
    @order = Spree::Order.by_number(params[:number]).includes([{ :line_items => [{ :variant => [:default_price, { :product => [:master] } ] }] } , { :adjustments => :adjustable }] ).first
    raise "No order found for -#{params[:number]}-" unless @order
  end
  
  def load_variant
    @variant = Spree::Variant.where(:id => params[:item]).first
    unless @variant
      flash[:error] = "No variant"
      render :show
    end
  end

  def check_valid_order
    load_order
    if @order.paid? || !@order.is_pos?
      flash[:error] = 'This order is already completed. Please use a new one.' if @order.paid?
      flash[:error] = 'This is not a pos order' unless @order.is_pos?
      render :show
    end
  end<commit_msg>ensure existence of user before associating with the order<commit_after>class Spree::Admin::PosController < Spree::Admin::BaseController
  before_filter :check_valid_order, :except => [:new]
  helper_method :user_stock_locations
  before_filter :load_variant, :only => [:add, :remove]
  before_filter :ensure_pos_shipping_method, :only => [:new]
  before_filter :ensure_payment_method, :only => [:update_payment]
  before_filter :ensure_existing_user, :only => [:associate_user]
  
  def new
    @order = spree_current_user.unpaid_pos_orders.first
    @order ? add_error("You have an unpaid/empty order. Please either complete it or update items in the same order.") : init_pos
    redirect_to :action => :show , :number => @order.number
  end

  def find
    init_search

    #using the scope available_at_stock_location which should be defined according to app or removed if not required
    stock_location = user_stock_locations(spree_current_user).first
    @search = Spree::Variant.includes([:product]).available_at_stock_location(stock_location.id).ransack(params[:q])
    @variants = @search.result(:distinct => true).page(params[:page]).per(20)
  end

  def add
    @line_item = add_variant(@variant) if @variant.present?
    flash.notice = @line_item.errors[:base].present? ? 'Adding more than available' : Spree.t('product_added') if @line_item
    redirect_to :action => :show, :number => @order.number 
  end

  def remove
    line_item = @order.contents.remove(@variant, 1, @order.shipment)
    flash.notice = line_item.quantity != 0 ? 'Quantity Updated' : Spree.t('product_removed') 
    redirect_to :action => :show, :number => @order.number
  end

  def update_line_item_quantity
    item = @order.line_items.where(:id => params[:line_item_id]).first
    #TODO error handling
    item.quantity = params[:quantity].to_i
    item.save

    flash.notice = item.errors[:base].present? ? 'Adding more than available.' : 'Quantity Updated'      
    redirect_to :action => :show
  end

  def apply_discount
    if VALID_DISCOUNT_REGEX.match(params[:discount]) && params[:discount].to_f < 100
      @discount = params[:discount].to_f
      item = @order.line_items.where(:id => params[:line_item_id]).first
      item.price = item.variant.price * ( 1.0 - @discount/100.0 )
      item.save
    else
      flash[:notice] = 'Please enter a valid discount'
    end
    redirect_to :action => :show
  end

  def clean_order
    @order.clean!
    flash[:notice] = "Removed all items"
    redirect_to :action => :show, :number => @order.number
  end

  def associate_user
    if params[:email] && Spree::User.where(:email => params[:email]).present?
    @user = @order.associate_user_for_pos(params[:email].present? ? params[:email] : params[:new_email])
    if @user.errors.present?
      add_error "Could not add the user:#{@user.errors.full_messages.to_sentence}"
    else
      @order.save!
      flash[:notice] = 'Successfully Associated User'
    end

    redirect_to :action => :show, :number => @order.number
  end

  def update_payment
    @payment_method_id = params[:payment_method_id]
    @payment = @order.save_payment_for_pos(params[:payment_method_id], params[:card_name])
    if @payment.errors.blank?
      print
    else
      add_error @payment.errors.full_messages.to_sentence
      redirect_to :action => :show, :number => @order.number
    end
  end

  def update_stock_location
    @order.shipment.stock_location = user_stock_locations(spree_current_user).where(:id => params[:stock_location_id]).first
    @order.bill_address = @order.ship_address = @order.shipment.stock_location.address
    @order.save
    @order.shipment.save
    redirect_to :action => :show, :number => @order.number
  end

  private 
  
  def ensure_existing_user 
    invalid_user_message = "No user with email #{params[:email]}" if params[:email].present? && Spree::User.where(:email => params[:email]).blank?
    invalid_user_message = "User Already exists for the email #{params[:new_email]}" if params[:new_email].present? && Spree::User.where(:email => params[:new_email]).present?
    redirect_to admin_pos_show_order_path, :flash => {:error => invalid_user_message} if invalid_user_message
  end

  def ensure_pos_shipping_method
    redirect_to '/', :flash => { :error => 'No shipping method available for POS orders. Please assign one.'} and return unless Spree::ShippingMethod.where(:name => SpreePos::Config[:pos_shipping]).first
  end

  def load_order
    @order = Spree::Order.by_number(params[:number]).includes([{ :line_items => [{ :variant => [:default_price, { :product => [:master] } ] }] } , { :adjustments => :adjustable }] ).first
    raise "No order found for -#{params[:number]}-" unless @order
  end
  
  def load_variant
    @variant = Spree::Variant.where(:id => params[:item]).first
    unless @variant
      flash[:error] = "No variant"
      render :show
    end
  end

  def check_valid_order
    load_order
    if @order.paid? || !@order.is_pos?
      flash[:error] = 'This order is already completed. Please use a new one.' if @order.paid?
      flash[:error] = 'This is not a pos order' unless @order.is_pos?
      render :show
    end
  end
{'fraction_non_alphanumeric': 0.12702507481168093, 'fraction_numerical': 0.0026829016613352597, 'mean_word_length': 4.006198347107438, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4929936', 'n_tokens_mistral': 3102, 'n_tokens_neox': 3010, 'n_words': 711}