From 095792aa5bfb3e5a1ca27fb2d846d383d67b7b48 Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Fri, 7 Apr 2017 16:44:34 +0200 Subject: [PATCH] Fixed issue #926 - Accounted Time. --- app/models/ticket.rb | 23 ++-- app/models/ticket/article.rb | 1 + app/models/ticket/time_accounting.rb | 3 + .../ticket_article_time_accouting_test.rb | 118 ++++++++++++++++++ 4 files changed, 134 insertions(+), 11 deletions(-) create mode 100644 test/unit/ticket_article_time_accouting_test.rb diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 213405800..4fafa19d0 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -51,17 +51,18 @@ class Ticket < ApplicationModel :article_count, :preferences - belongs_to :group, class_name: 'Group' - has_many :articles, class_name: 'Ticket::Article', after_add: :cache_update, after_remove: :cache_update - belongs_to :organization, class_name: 'Organization' - belongs_to :state, class_name: 'Ticket::State' - belongs_to :priority, class_name: 'Ticket::Priority' - belongs_to :owner, class_name: 'User' - belongs_to :customer, class_name: 'User' - belongs_to :created_by, class_name: 'User' - belongs_to :updated_by, class_name: 'User' - belongs_to :create_article_type, class_name: 'Ticket::Article::Type' - belongs_to :create_article_sender, class_name: 'Ticket::Article::Sender' + belongs_to :group, class_name: 'Group' + has_many :articles, class_name: 'Ticket::Article', after_add: :cache_update, after_remove: :cache_update + has_many :ticket_time_accounting, class_name: 'Ticket::TimeAccounting', dependent: :destroy + belongs_to :organization, class_name: 'Organization' + belongs_to :state, class_name: 'Ticket::State' + belongs_to :priority, class_name: 'Ticket::Priority' + belongs_to :owner, class_name: 'User' + belongs_to :customer, class_name: 'User' + belongs_to :created_by, class_name: 'User' + belongs_to :updated_by, class_name: 'User' + belongs_to :create_article_type, class_name: 'Ticket::Article::Type' + belongs_to :create_article_sender, class_name: 'Ticket::Article::Sender' self.inheritance_column = nil diff --git a/app/models/ticket/article.rb b/app/models/ticket/article.rb index e9dc0dbdc..69e400263 100644 --- a/app/models/ticket/article.rb +++ b/app/models/ticket/article.rb @@ -9,6 +9,7 @@ class Ticket::Article < ApplicationModel include Ticket::Article::Assets belongs_to :ticket + has_one :ticket_time_accounting, class_name: 'Ticket::TimeAccounting', foreign_key: :ticket_article_id, dependent: :destroy belongs_to :type, class_name: 'Ticket::Article::Type' belongs_to :sender, class_name: 'Ticket::Article::Sender' belongs_to :created_by, class_name: 'User' diff --git a/app/models/ticket/time_accounting.rb b/app/models/ticket/time_accounting.rb index 8a2f39cb4..b2ac5d2b0 100644 --- a/app/models/ticket/time_accounting.rb +++ b/app/models/ticket/time_accounting.rb @@ -1,6 +1,9 @@ # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/ class Ticket::TimeAccounting < ApplicationModel + belongs_to :ticket + belongs_to :ticket_article, class_name: 'Ticket::Article' + after_create :ticket_time_unit_update after_update :ticket_time_unit_update diff --git a/test/unit/ticket_article_time_accouting_test.rb b/test/unit/ticket_article_time_accouting_test.rb new file mode 100644 index 000000000..4bb650c7c --- /dev/null +++ b/test/unit/ticket_article_time_accouting_test.rb @@ -0,0 +1,118 @@ +# encoding: utf-8 +require 'test_helper' + +class TicketArticleTimeAccoutingTest < ActiveSupport::TestCase + + test 'destroy dependent time accounting for ticket and article' do + ticket_test = Ticket.create( + title: 'com test 1', + group: Group.lookup(name: 'Users'), + customer_id: 2, + state: Ticket::State.lookup(name: 'new'), + priority: Ticket::Priority.lookup(name: '2 normal'), + updated_by_id: 1, + created_by_id: 1, + ) + assert(ticket_test, 'ticket created') + + article_test1 = Ticket::Article.create( + ticket_id: ticket_test.id, + from: 'some_customer_com-1@example.com', + to: 'some_zammad_com-1@example.com', + subject: 'com test 1', + message_id: 'some@id_com_1', + body: 'some message 123', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + Ticket::TimeAccounting.create!( + ticket_id: ticket_test.id, + ticket_article_id: article_test1.id, + time_unit: 10, + created_by_id: 1, + ) + + article_test2 = Ticket::Article.create( + ticket_id: ticket_test.id, + from: 'some_customer_com-1@example.com', + to: 'some_zammad_com-1@example.com', + subject: 'com test 1', + message_id: 'some@id_com_1', + body: 'some message 123', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + Ticket::TimeAccounting.create!( + ticket_id: ticket_test.id, + ticket_article_id: article_test2.id, + time_unit: 10, + created_by_id: 1, + ) + + article_test3 = Ticket::Article.create( + ticket_id: ticket_test.id, + from: 'some_customer_com-1@example.com', + to: 'some_zammad_com-1@example.com', + subject: 'com test 1', + message_id: 'some@id_com_1', + body: 'some message 123', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + Ticket::TimeAccounting.create!( + ticket_id: ticket_test.id, + ticket_article_id: article_test3.id, + time_unit: 10, + created_by_id: 1, + ) + + Ticket::TimeAccounting.create!( + ticket_id: ticket_test.id, + time_unit: 10, + created_by_id: 1, + ) + + time_accouting_list = Ticket::TimeAccounting.where( + ticket_id: ticket_test.id, + ) + + found = time_accouting_list.select { |t| t.ticket_article_id.in?([article_test1.id, article_test2.id, article_test3.id]) } + assert_equal(3, found.count) + + article_test1.destroy + + time_accouting_list.reload + found = time_accouting_list.select { |t| t.ticket_article_id.in?([article_test1.id, article_test2.id, article_test3.id]) } + assert_equal(2, found.count) + + article_test2.destroy + + time_accouting_list.reload + found = time_accouting_list.select { |t| t.ticket_article_id.in?([article_test1.id, article_test2.id, article_test3.id]) } + assert_equal(1, found.count) + + article_test3.destroy + + time_accouting_list.reload + found = time_accouting_list.select { |t| t.ticket_article_id.in?([article_test1.id, article_test2.id, article_test3.id]) } + assert_equal(0, found.count) + + # one accouting left for the ticket only + assert_equal(1, time_accouting_list.count) + ticket_test.destroy + time_accouting_list.reload + assert_equal(0, time_accouting_list.count) + end +end