TimeWorked-like custom fields, RT#11168
[freeside.git] / rt / lib / RT / Action / Accumulate.pm
1 package RT::Action::Accumulate;
2 use base 'RT::Action';
3
4 use strict;
5
6 =head1 NAME 
7
8 RT::Action::Accumulate - Accumulate a running total in a ticket custom field.
9
10 This action requires a transaction and ticket custom field with the same name.
11 When a transaction is submitted with a numeric value in that field, the field 
12 value for the ticket will be incremented by that amount.  Use this to create 
13 custom fields that behave like the "TimeWorked" field.
14
15 Best used with an "On Update" condition that triggers on any transaction.  The 
16 ticket custom field update itself does not a create a transaction.
17
18 The argument to this action is the name of the custom field.  They must have 
19 the same name, and should be single-valued fields.
20
21 =cut
22
23 sub Prepare {
24     my $self = shift;
25     my $cfname = $self->Argument or return 0;
26     $self->{'inc_by'} = $self->TransactionObj->FirstCustomFieldValue($cfname);
27     return ( $self->{'inc_by'} =~ /^(\d+)$/ );
28 }
29
30 sub Commit {
31     my $self = shift;
32     my $cfname = $self->Argument;
33     my $newval = $self->{'inc_by'} + 
34       ($self->TicketObj->FirstCustomFieldValue($cfname) || 0);
35     my ($val) = $self->TicketObj->AddCustomFieldValue(
36       Field => 'Support time',
37       Value => $newval,
38       RecordTransaction => 0,
39     );
40     return $val;
41 }
42
43 1;
44