module CheckOrChangeStatus

Public Class Methods

check(barcode) click to toggle source
# File lib/rails_admin_opening_states_changer.rb, line 106
def self.check(barcode)
  if barcode.blank?
    message = {error: "#{I18n.t(:barcode_cannot_be_empty)}"}
    status = 400
  else
    # Only if the Item exists and is in status deposited
    item = Item.find_by(code: barcode)

    if item.blank?
      message = {error: "#{I18n.t(:barcode_not_exists)}: #{barcode}"}
      status = 404
    else
      # It exists, Thus I return the relevant info for printing
      now = Time.now
      message = {
        info: "#{I18n.t(:current_item_state, state: PhasesModels.compute_states(item))}",
        used: (!item.not_after_time.blank? && item.not_after_time < now),
        expired: (!item.expiration_date.blank? && item.expiration_date < now),
        not_before: item.not_before_time,
        not_after: item.not_after_time,
        expiration_date: item.expiration_date,
        states: item.checks,
        barcode: barcode
      }
      status = 200
    end
  end
  [message, status]
end
log(title, description, passed, item_id, user_id) click to toggle source
# File lib/rails_admin_opening_states_changer.rb, line 3
def self.log(title, description, passed, item_id, user_id)
  puts "LOGGING:"
  puts "TITLE: #{title}"
  puts "DESC: #{description}"
  puts "PASSED: #{passed}"
  puts "ITEM ID: #{item_id}"
  puts "USER ID: #{user_id}"
  Check.create! title: title, description: description, passed: passed, item_id: item_id, user_id: user_id
end
perform(barcode, before, after, user_id, filename_date_part = "%Y%m%d_%H%M%S.%L") click to toggle source
# File lib/rails_admin_opening_states_changer.rb, line 12
def self.perform(barcode, before, after, user_id, filename_date_part = "%Y%m%d_%H%M%S.%L")
  # "%d%m%Y_%H%M%S"
  username = User.find(user_id).username
  current_time = Time.now.strftime(filename_date_part)
  puts "PASSED PARAMS: "
  puts "- BARCODE: #{barcode}"
  puts "- before: #{before}"
  puts "- after: #{after}"
  puts "- user_id: #{user_id}"
  if barcode.blank?
    message = {error: "#{I18n.t(:barcode_cannot_be_empty)}"}
    status = 400
    return [message, status]
  else
    # Only if the Item exists and is in status deposited
    item = Item.find_by(code: barcode)

    if item.blank?
      message = {error: "#{I18n.t(:barcode_not_exists)}: #{barcode}"}
      status = 404
      return [message, status]
    else
      puts "Checking if it's in previous state"
      puts "BEFORE: #{before}"
      puts "- STATE IDS FROM DB: #{State.where(name: before).pluck(:id).inspect}"
      puts "- item.state_id: #{item.state_id}"
      if !State.where(name: before).pluck(:id).include?(item.state_id)
        #error
        message = {error: "#{I18n.t(:barcode_state_not, after: I18n.t("admin.actions.#{after}.title"), state: I18n.t("admin.actions.#{before}.done"), at: current_time, user: username)}"}
        status = 404
        log I18n.t(:not_found_error), message[:error], false, item.id, user_id
        return [message, status]
      else
        # Item exists, but are we in the right timeframe?
        # Now must be between not_before_time and expiration_date
        now = Time.now
        if !item.not_after_time.blank? && item.not_after_time < now
          # (!item.not_before_time.blank? || !item.expiration_date.blank?) && (item.not_before_time > now || item.expiration_date < now)
          # error
          # create the error file (need specs)
          xml_string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<error>
  <code>#{barcode.split[1]}</code>
  <not_before>#{item.not_before_time.strftime("%H:%M")}</not_before>
  <not_after>#{item.not_after_time.strftime("%d/%m/%y")}</not_after>
  <survey_at>#{now}</survey_at>
</error>\n"
          File.open(File.expand_path("#{Settings.ns("errors").save_path}/timeframe_error_#{current_time}.xml"), "w") { |f| f.write(xml_string) }
          message = {error: "#{I18n.t(:item_expired)}"}
          status = 401
          
          log I18n.t(:timeframe_error), message[:error], false, item.id, user_id
          return [message, status]
        end
        if !item.not_before_time.blank? && item.not_before_time > now
          # #{created_item.not_after_time.strftime("%H:%M")} del #{created_item.not_after_time.strftime("%d/%m/%y")}\r\n"
          message = {error: "#{I18n.t(:dont_use_before, 
            now: I18n.l(now), 
            user: username, 
            not_before: I18n.l(item.not_before_time), 
            not_after: I18n.l(item.not_after_time), 
            not_before_time: item.not_before_time.strftime("%H:%M"),
            not_before_date: item.not_before_time.strftime("%d/%m/%y"),
            not_after_time: item.not_after_time.strftime("%H:%M"),
            not_after_date: item.not_after_time.strftime("%d/%m/%y"),
            distance_before: distance_of_time_in_words_to_now(item.not_before_time),
            distance_after: distance_of_time_in_words_to_now(item.not_after_time),
            barcode: barcode)}"}
          status = 401
          
          log I18n.t(:timeframe_error), message[:error], false, item.id, user_id
          return [message, status]
        end
        # It exists and is deposited, Thus I set it to picked up
        item.state_id = State.find_by(name: after).id
        if !item.save
          # --- Se non è così, allora errore
          message = {error: "#{I18n.t(:item_state_not_changed_to, state: I18n.t("admin.actions.#{after}.title"), at: current_time, user: username)} (#{item.errors.full_messages.inspect})"}
          status = 404
          log I18n.t(:not_saved_error), message[:error], false, item.id, user_id
          return [message, status]
        else            
          # --- If it managed to save, then
          message = {info: "#{I18n.t(:item_correctly_state_changed, state: I18n.t("admin.actions.#{after}.title"), at: current_time, user: username)}", barcode: barcode}
          log I18n.t(:success_save), message[:info], true, item.id, user_id
          status = 200
          return [message, status]
        end
      end
      
    end
  end
end