Class: CheckedItem

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/checked_item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#due_date_stringObject

attr_protected :user_id



18
19
20
# File 'app/models/checked_item.rb', line 18

def due_date_string
  @due_date_string
end

#ignore_restrictionObject

attr_protected :user_id



18
19
20
# File 'app/models/checked_item.rb', line 18

def ignore_restriction
  @ignore_restriction
end

#item_identifierObject

attr_protected :user_id



18
19
20
# File 'app/models/checked_item.rb', line 18

def item_identifier
  @item_identifier
end

Instance Method Details

#available_for_checkout?Boolean



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/checked_item.rb', line 20

def available_for_checkout?
  if item.blank?
    errors[:base] << I18n.t('activerecord.errors.messages.checked_item.item_not_found')
    return false
  end

  if item.rent?
    unless item.circulation_status.name == 'Missing'
      errors[:base] << I18n.t('activerecord.errors.messages.checked_item.already_checked_out')
    end
  end

  unless item.available_for_checkout?
    if item.circulation_status.name == 'Missing'
      item.circulation_status = CirculationStatus.where(name: 'Available On Shelf').first
      item.save
      set_due_date
    else
      errors[:base] << I18n.t('activerecord.errors.messages.checked_item.not_available_for_checkout')
      return false
    end
  end

  if item_checkout_type.blank?
    errors[:base] << I18n.t('activerecord.errors.messages.checked_item.this_group_cannot_checkout')
    return false
  end
  # ここまでは絶対に貸出ができない場合

  return true if ignore_restriction == "1"

  if item.not_for_loan?
    errors[:base] << I18n.t('activerecord.errors.messages.checked_item.not_available_for_checkout')
  end

  if item.reserved?
    unless item.manifestation.next_reservation.user == basket.user
      errors[:base] << I18n.t('activerecord.errors.messages.checked_item.reserved_item_included')
    end
  end

  checkout_count = basket.user.checked_item_count
  checkout_type = item_checkout_type.checkout_type
  if checkout_count[:"#{checkout_type.name}"] >= item_checkout_type.checkout_limit
    errors[:base] << I18n.t('activerecord.errors.messages.checked_item.excessed_checkout_limit')
  end
  
  if errors[:base].empty?
    true
  else
    false
  end
end

#item_checkout_typeObject



74
75
76
77
78
# File 'app/models/checked_item.rb', line 74

def item_checkout_type
  if item and basket
    basket.user.profile.user_group.user_group_has_checkout_types.available_for_item(item).first
  end
end

#set_due_dateObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/checked_item.rb', line 80

def set_due_date
  return nil unless item_checkout_type
  if due_date_string.present?
    self.due_date = Time.zone.parse(due_date_string).try(:end_of_day)
  else
    lending_rule = item.lending_rule(basket.user)
    return nil if lending_rule.nil?

    if lending_rule.fixed_due_date.blank?
      #self.due_date = item_checkout_type.checkout_period.days.since Time.zone.today
      self.due_date = lending_rule.loan_period.days.since(Time.zone.now).end_of_day
    else
      #self.due_date = item_checkout_type.fixed_due_date
      self.due_date = lending_rule.fixed_due_date.tomorrow.end_of_day
    end
    # 返却期限日が閉館日の場合
    while item.shelf.library.closed?(due_date)
      if item_checkout_type.set_due_date_before_closing_day
        self.due_date = due_date.yesterday.end_of_day
      else
        self.due_date = due_date.tomorrow.end_of_day
      end
    end
  end
  return due_date
end

#set_itemObject



107
108
109
110
111
112
113
# File 'app/models/checked_item.rb', line 107

def set_item
  identifier = item_identifier.to_s.strip
  if identifier.present?
    item = Item.where(item_identifier: identifier).first
    self.item = item
  end
end