Fixed issue #926 - Accounted Time.

This commit is contained in:
Rolf Schmidt 2017-04-07 16:44:34 +02:00
parent 5cd5b7379a
commit 095792aa5b
4 changed files with 134 additions and 11 deletions

View file

@ -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

View file

@ -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'

View file

@ -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

View file

@ -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