Class: Checkout

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#operatorObject

Returns the value of attribute operator.



47
48
49
# File 'app/models/checkout.rb', line 47

def operator
  @operator
end

Class Method Details

.manifestations_count(start_date, end_date, manifestation) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'app/models/checkout.rb', line 117

def self.manifestations_count(start_date, end_date, manifestation)
  self.where(
    self.arel_table[:created_at].gteq start_date
  ).where(
    self.arel_table[:created_at].lt end_date
  )
  .where(
    item_id: manifestation.items.pluck('items.id')
  ).count
end

.remove_all_history(user) ⇒ Object



155
156
157
# File 'app/models/checkout.rb', line 155

def self.remove_all_history(user)
  user.checkouts.returned.update_all(user_id: nil)
end

.send_due_date_notificationObject



128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/checkout.rb', line 128

def self.send_due_date_notification
  template = 'recall_item'
  queues = []
  User.find_each do |user|
    # 未来の日時を指定する
    checkouts = user.checkouts.due_date_on(user.profile.user_group.number_of_day_to_notify_due_date.days.from_now.beginning_of_day)
    unless checkouts.empty?
      queues << user.send_message(template, manifestations: checkouts.collect(&:item).collect(&:manifestation))
    end
  end
  queues.size
end

.send_overdue_notificationObject



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/models/checkout.rb', line 141

def self.send_overdue_notification
  template = 'recall_overdue_item'
  queues = []
  User.find_each do |user|
    user.profile.user_group.number_of_time_to_notify_overdue.times do |i|
      checkouts = user.checkouts.due_date_on((user.profile.user_group.number_of_day_to_notify_overdue * (i + 1)).days.ago.beginning_of_day)
      unless checkouts.empty?
        queues << user.profile.user.send_message(template, manifestations: checkouts.collect(&:item).collect(&:manifestation))
      end
    end
  end
  queues.size
end

Instance Method Details

#get_new_due_dateObject



106
107
108
109
110
111
112
113
114
115
# File 'app/models/checkout.rb', line 106

def get_new_due_date
  return nil unless user
  if item
    if checkout_renewal_count <= item.checkout_status(user).checkout_renewal_limit
      new_due_date = Time.zone.now.advance(days: item.checkout_status(user).checkout_period).beginning_of_day
    else
      new_due_date = due_date
    end
  end
end

#is_not_checked?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'app/models/checkout.rb', line 51

def is_not_checked?
  checkout = Checkout.not_returned.where(item_id: item_id)
  unless checkout.empty?
    errors[:base] << I18n.t('activerecord.errors.messages.checkin.already_checked_out')
  end
end

#is_today_due_date?Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
# File 'app/models/checkout.rb', line 98

def is_today_due_date?
  if Time.zone.now.beginning_of_day == due_date.beginning_of_day
    return true
  else
    return false
  end
end

#over_checkout_renewal_limit?Boolean

Returns:

  • (Boolean)


85
86
87
88
# File 'app/models/checkout.rb', line 85

def over_checkout_renewal_limit?
  return nil unless item.checkout_status(user)
  return true if item.checkout_status(user).checkout_renewal_limit < checkout_renewal_count
end

#overdue?Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
# File 'app/models/checkout.rb', line 90

def overdue?
  if Time.zone.now > due_date.tomorrow.beginning_of_day
    return true
  else
    return false
  end
end

#renewable?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/checkout.rb', line 58

def renewable?
  return nil if checkin
  messages = []
  if !operator and overdue?
    messages << I18n.t('checkout.you_have_overdue_item')
  end
  if !operator and reserved?
    messages << I18n.t('checkout.this_item_is_reserved')
  end
  if !operator and over_checkout_renewal_limit?
    messages << I18n.t('checkout.excessed_renewal_limit')
  end
  if messages.empty?
    true
  else
    messages.each do |message|
      errors[:base] << message
    end
    false
  end
end

#reserved?Boolean

Returns:

  • (Boolean)


80
81
82
83
# File 'app/models/checkout.rb', line 80

def reserved?
  return true if item.try(:reserved?)
  false
end