Class: Checkout

Inherits:
ApplicationRecord
  • 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.



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

def operator
  @operator
end

Class Method Details

.manifestations_count(start_date, end_date, manifestation) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'app/models/checkout.rb', line 123

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

.remove_all_history(user) ⇒ Object



161
162
163
# File 'app/models/checkout.rb', line 161

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

.send_due_date_notificationObject



134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/checkout.rb', line 134

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



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/models/checkout.rb', line 147

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



112
113
114
115
116
117
118
119
120
121
# File 'app/models/checkout.rb', line 112

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



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

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



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

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



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

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



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

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

#renewable?Boolean



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

def renewable?
  return nil if checkin
  messages = []
  if !operator && overdue?
    messages << I18n.t('checkout.you_have_overdue_item')
  end
  if !operator && reserved?
    messages << I18n.t('checkout.this_item_is_reserved')
  end
  if !operator && 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



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

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

#set_new_due_dateObject



108
109
110
# File 'app/models/checkout.rb', line 108

def set_new_due_date
  self.due_date = due_date.try(:end_of_day)
end