summaryrefslogtreecommitdiff
path: root/rt/lib/RT/Action/Accumulate.pm
blob: 0da7d2ef51811f0bd42bcadf1f4d45de87a087ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package RT::Action::Accumulate;
use base 'RT::Action';

use strict;

=head1 NAME 

RT::Action::Accumulate - Accumulate a running total in a ticket custom field.

This action requires a transaction and ticket custom field with the same name.
When a transaction is submitted with a numeric value in that field, the field 
value for the ticket will be incremented by that amount.  Use this to create 
custom fields that behave like the "TimeWorked" field.

Best used with an "On Update" condition that triggers on any transaction.  The 
ticket custom field update itself does not a create a transaction.

The argument to this action is the name of the custom field.  They must have 
the same name, and should be single-valued fields.

=cut

sub Prepare {
    my $self = shift;
    my $cfname = $self->Argument or return 0;
    #RT::Logger->info('Accumulate::Prepare called on transaction '.
    #                   $self->TransactionObj->Id." field $cfname");
    my $TransObj = $self->TransactionObj;
    my $TicketObj = $self->TicketObj;
    if ( $TransObj->Type eq 'Create' and
         !defined($TransObj->FirstCustomFieldValue($cfname)) ) {
        # special case: we're creating a new ticket, and the initial value
        # may have been set on the ticket instead of the transaction, so
        # update the transaction to match
        $self->{'obj'} = $TransObj;
        $self->{'inc_by'} = $TicketObj->FirstCustomFieldValue($cfname);
    } else {
        # the usual case when updating an existing ticket
        $self->{'obj'} = $TicketObj;
        $self->{'inc_by'} = $TransObj->FirstCustomFieldValue($cfname) 
                            || '';
    }
    return ( $self->{'inc_by'} =~ /^(\d+)$/ ); # else it's empty
}

sub Commit {
    my $self = shift;
    my $cfname = $self->Argument;
    my $obj = $self->{'obj'};
    my $newval = $self->{'inc_by'} + 
      ($obj->FirstCustomFieldValue($cfname) || 0);
      #RT::Logger->info('Accumulate::Commit called on '.ref($obj).' '.
      #                 $obj->Id." field $cfname");
    my ($val) = $obj->AddCustomFieldValue(
        Field => $cfname,
        Value => $newval,
        RecordTransaction => 0,
    );
    return $val;
}

1;