sets_close_time.rb 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # Adds close time (if missing) when tickets are closed.
  3. module Ticket::SetsCloseTime
  4. extend ActiveSupport::Concern
  5. included do
  6. before_create :ticket_set_close_time
  7. before_update :ticket_set_close_time
  8. end
  9. private
  10. def ticket_set_close_time
  11. # return if we run import mode
  12. return true if Setting.get('import_mode')
  13. # check if ticket is closed now
  14. return true if !ticket_closed?(self)
  15. # set last close_at
  16. if changes_to_save['state_id'].present? || last_close_at.blank?
  17. self.last_close_at = Time.zone.now
  18. end
  19. # check if close_at is already set
  20. return true if close_at
  21. # set close_at
  22. self.close_at = Time.zone.now
  23. end
  24. def ticket_closed?(ticket)
  25. return false if !ticket.state_id
  26. state = Ticket::State.lookup(id: ticket.state_id)
  27. state_type = Ticket::StateType.lookup(id: state.state_type_id)
  28. return true if state_type.name == 'closed'
  29. false
  30. end
  31. end