<commit_before># -*- encoding: utf-8 -*-
class ItemsController < ApplicationController
  authorize_function
  add_breadcrumb "I18n.t('activerecord.models.item')", 'items_path'
  add_breadcrumb "I18n.t('page.new', :model => I18n.t('activerecord.models.item'))", 'new_item_path', :only => [:new, :create]
  add_breadcrumb "I18n.t('page.editing', :model => I18n.t('activerecord.models.item'))", 'edit_item_path(params[:id])', :only => [:edit, :update]
  include NotificationSound
  load_and_authorize_resource :except => :numbering
  before_filter :get_user
  before_filter :get_agent, :get_manifestation
  helper_method :get_shelf
  helper_method :get_library
  before_filter :get_version, :only => [:show]
  before_filter :check_status, :only => [:edit]
  #before_filter :store_location
  after_filter :solr_commit, :only => [:create, :update, :destroy]
  after_filter :convert_charset, :only => :index

  # GET /items
  # GET /items.json
  def index
    query = params[:query].to_s.strip
    per_page = Item.default_per_page
    @count = {}
    if user_signed_in?
      if current_user.has_role?('Librarian')
        if params[:format] == 'csv'
          per_page = 65534
        elsif params[:mode] == 'barcode'
          per_page = 40
        end
      end
    end

    search = Sunspot.new_search(Item)
    set_role_query(current_user, search)

    @query = query.dup
    unless query.blank?
      search.build do
        fulltext query
      end
    end

    agent = @agent
    manifestation = @manifestation
    shelf = get_shelf
    order = @order = Order.find(params[:order_id]) if params[:order_id]
    unless params[:mode] == 'add'
      search.build do
        with(:agent_ids).equal_to agent.id if agent
        with(:manifestation_id).equal_to manifestation.id if manifestation
        with(:shelf_id).equal_to shelf.id if shelf
        with(:order_id).equal_to order.id if order
      end
    end

    search.build do
      order_by(:created_at, :desc)
    end

    role = current_user.try(:role) || Role.default_role
    search.build do
      with(:required_role_id).less_than role.id
    end

    page = params[:page] || 1
    search.query.paginate(page.to_i, per_page)
    @items = search.execute!.results
    @count[:total] = @items.total_entries

    if params[:mode] == 'barcode'
      render :action => 'barcode', :layout => false
      return
    end

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @items }
      format.csv  { render :layout => false }
      format.atom
    end
  end

  # GET /items/1
  # GET /items/1.json
  def show
    @item = @item.versions.find(@version).item if @version

    # 書誌と所蔵が１：１の場合 manifestations#showにリダイレクト
    if SystemConfiguration.get("manifestation.has_one_item") == true
      redirect_to manifestation_url(@item.manifestation)
      return
    end

    # for book_jacket plugin
    @manifestation = @item.manifestation

    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @item }
    end
  end

  # GET /items/new
  def new
    if Shelf.real.blank?
      flash[:notice] = t('item.create_shelf_first')
      redirect_to libraries_url
      return
    end
    unless @manifestation
      flash[:notice] = t('item.specify_manifestation')
      redirect_to manifestations_url
      return
    end
    original_item = Item.where(:id => params[:item_id]).first if params[:item_id]
    if original_item
      @item = original_item.dup
      @item.item_identifier = nil
      @item.identifier = nil
      @item.rank = 1 if original_item.rank == 0
      @item.use_restriction_id = original_item.use_restriction.id
      @item.library_id = original_item.shelf.library.id
      @item.claim = nil
    else
      @item = Item.new
    end
    # TODO
    @item.manifestation_id = @manifestation.id
    @item.manifestation = @manifestation
    unless @manifestation.article?
      @circulation_statuses = CirculationStatus.order(:position)
      @item.circulation_status = CirculationStatus.where(:name => 'In Process').first unless @item.try(:circulation_status)
      @item.checkout_type = @manifestation.carrier_type.checkout_types.first unless @item.try(:checkout_type)
      @item.use_restriction_id = UseRestriction.where(:name => 'Limited Circulation, Normal Loan Period').select(:id).first.id unless @item.use_restriction_id
      @item.call_number = @manifestation.items.where(:rank => 0).first.call_number unless @item.try(:call_number) rescue nil
      if @item.call_number.blank? && @manifestation.classifications.present?
        @item.call_number = @manifestation.classifications.first.classification_identifier
      end
    else
      @item.circulation_status = CirculationStatus.where(:name => 'Not Available').first unless @item.try(:circulation_status)
      @item.checkout_type = CheckoutType.where(:name => 'article').first unless @item.try(:checkout_type)
      @item.use_restriction_id = UseRestriction.where(:name => 'Not For Loan').select(:id).first.id unless @item.use_restriction_id

      #@item.shelf = @library.article_shelf unless @item.try(:shelf)
      @item.shelf = @library.shelves.first unless @item.try(:shelf)
    end
    @item.acquired_at_string = Date.today unless @item.acquired_at_string
    prepare_options
    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @item }
    end
  end

  # GET /items/1/edit<commit_msg>所蔵の新規作成時に価格の自動入力を追加<commit_after># -*- encoding: utf-8 -*-
class ItemsController < ApplicationController
  authorize_function
  add_breadcrumb "I18n.t('activerecord.models.item')", 'items_path'
  add_breadcrumb "I18n.t('page.new', :model => I18n.t('activerecord.models.item'))", 'new_item_path', :only => [:new, :create]
  add_breadcrumb "I18n.t('page.editing', :model => I18n.t('activerecord.models.item'))", 'edit_item_path(params[:id])', :only => [:edit, :update]
  include NotificationSound
  load_and_authorize_resource :except => :numbering
  before_filter :get_user
  before_filter :get_agent, :get_manifestation
  helper_method :get_shelf
  helper_method :get_library
  before_filter :get_version, :only => [:show]
  before_filter :check_status, :only => [:edit]
  #before_filter :store_location
  after_filter :solr_commit, :only => [:create, :update, :destroy]
  after_filter :convert_charset, :only => :index

  # GET /items
  # GET /items.json
  def index
    query = params[:query].to_s.strip
    per_page = Item.default_per_page
    @count = {}
    if user_signed_in?
      if current_user.has_role?('Librarian')
        if params[:format] == 'csv'
          per_page = 65534
        elsif params[:mode] == 'barcode'
          per_page = 40
        end
      end
    end

    search = Sunspot.new_search(Item)
    set_role_query(current_user, search)

    @query = query.dup
    unless query.blank?
      search.build do
        fulltext query
      end
    end

    agent = @agent
    manifestation = @manifestation
    shelf = get_shelf
    order = @order = Order.find(params[:order_id]) if params[:order_id]
    unless params[:mode] == 'add'
      search.build do
        with(:agent_ids).equal_to agent.id if agent
        with(:manifestation_id).equal_to manifestation.id if manifestation
        with(:shelf_id).equal_to shelf.id if shelf
        with(:order_id).equal_to order.id if order
      end
    end

    search.build do
      order_by(:created_at, :desc)
    end

    role = current_user.try(:role) || Role.default_role
    search.build do
      with(:required_role_id).less_than role.id
    end

    page = params[:page] || 1
    search.query.paginate(page.to_i, per_page)
    @items = search.execute!.results
    @count[:total] = @items.total_entries

    if params[:mode] == 'barcode'
      render :action => 'barcode', :layout => false
      return
    end

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @items }
      format.csv  { render :layout => false }
      format.atom
    end
  end

  # GET /items/1
  # GET /items/1.json
  def show
    @item = @item.versions.find(@version).item if @version

    # 書誌と所蔵が１：１の場合 manifestations#showにリダイレクト
    if SystemConfiguration.get("manifestation.has_one_item") == true
      redirect_to manifestation_url(@item.manifestation)
      return
    end

    # for book_jacket plugin
    @manifestation = @item.manifestation

    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @item }
    end
  end

  # GET /items/new
  def new
    if Shelf.real.blank?
      flash[:notice] = t('item.create_shelf_first')
      redirect_to libraries_url
      return
    end
    unless @manifestation
      flash[:notice] = t('item.specify_manifestation')
      redirect_to manifestations_url
      return
    end
    original_item = Item.where(:id => params[:item_id]).first if params[:item_id]
    if original_item
      @item = original_item.dup
      @item.item_identifier = nil
      @item.identifier = nil
      @item.rank = 1 if original_item.rank == 0
      @item.use_restriction_id = original_item.use_restriction.id
      @item.library_id = original_item.shelf.library.id
      @item.claim = nil
    else
      @item = Item.new
    end
    # TODO
    @item.manifestation_id = @manifestation.id
    @item.manifestation = @manifestation
    unless @manifestation.article?
      @circulation_statuses = CirculationStatus.order(:position)
      @item.circulation_status = CirculationStatus.where(:name => 'In Process').first unless @item.try(:circulation_status)
      @item.checkout_type = @manifestation.carrier_type.checkout_types.first unless @item.try(:checkout_type)
      @item.use_restriction_id = UseRestriction.where(:name => 'Limited Circulation, Normal Loan Period').select(:id).first.id unless @item.use_restriction_id
      @item.call_number = @manifestation.items.where(:rank => 0).first.call_number unless @item.try(:call_number) rescue nil
      if @item.call_number.blank? && @manifestation.classifications.present?
        @item.call_number = @manifestation.classifications.first.classification_identifier
      end
    else
      @item.circulation_status = CirculationStatus.where(:name => 'Not Available').first unless @item.try(:circulation_status)
      @item.checkout_type = CheckoutType.where(:name => 'article').first unless @item.try(:checkout_type)
      @item.use_restriction_id = UseRestriction.where(:name => 'Not For Loan').select(:id).first.id unless @item.use_restriction_id

      #@item.shelf = @library.article_shelf unless @item.try(:shelf)
      @item.shelf = @library.shelves.first unless @item.try(:shelf)
    end
    @item.acquired_at_string = Date.today unless @item.acquired_at_string

    # 所蔵の価格設定
    if @manifestation.price?
      today = Date.today.to_date
      tax_rate = TaxRate.where(["start_date <= ? and end_date >= ?", today, today]).first
      @item.tax = @manifestation.price * tax_rate.rate * 0.01
      @item.price = @manifestation.price + @item.tax
      @item.excluding_tax = @manifestation.price
      @item.tax_rate_id = tax_rate.id
    end

    prepare_options
    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @item }
    end
  end

  # GET /items/1/edit
{'fraction_non_alphanumeric': 0.11761575306013837, 'fraction_numerical': 0.005055880787653007, 'mean_word_length': 3.264372163388805, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 48, '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': '3610728', 'n_tokens_mistral': 3850, 'n_tokens_neox': 3638, 'n_words': 730}