user_ticket_counter.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class Observer::Ticket::UserTicketCounter < ActiveRecord::Observer
  3. observe 'ticket'
  4. def after_create(record)
  5. user_ticket_counter_update(record)
  6. end
  7. def after_update(record)
  8. user_ticket_counter_update(record)
  9. end
  10. def user_ticket_counter_update(record)
  11. return if !record.customer_id
  12. # open ticket count
  13. state_open = Ticket::State.by_category( 'open' )
  14. tickets_open = Ticket.where(
  15. :customer_id => record.customer_id,
  16. :state_id => state_open,
  17. ).count()
  18. # closed ticket count
  19. state_closed = Ticket::State.by_category( 'closed' )
  20. tickets_closed = Ticket.where(
  21. :customer_id => record.customer_id,
  22. :state_id => state_closed,
  23. ).count()
  24. # check if update is needed
  25. customer = User.lookup( :id => record.customer_id )
  26. need_update = false
  27. if customer[:preferences][:tickets_open] != tickets_open
  28. need_update = true
  29. customer[:preferences][:tickets_open] = tickets_open
  30. end
  31. if customer[:preferences][:tickets_closed] != tickets_closed
  32. need_update = true
  33. customer[:preferences][:tickets_closed] = tickets_closed
  34. end
  35. if need_update
  36. customer.save
  37. end
  38. end
  39. end