summaryrefslogtreecommitdiff
path: root/Condition/OnRiskThresholdMet.pm
blob: b7ec54b8b1c561e7faaba4f4cb43bd6c0b04ac21 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package RT::Condition::OnRiskThresholdMet;

use strict;
use warnings;
use base 'RT::Condition';

sub getRiskValue {
    my $cfname  = shift;
    my $cfvalue = shift;
    my $retval  = 1;

    my $cfs = RT::CustomField->new(RT->SystemUser);
    $cfs->Load($cfname);
    my $cfvs = $cfs->Values();

    $RT::Logger->info("Getting risk value for '$cfname/$cfvalue'");
    while ( my $valuerow = $cfvs->Next ) {
        if ($valuerow->Name eq $cfvalue && $cfvalue ne "") {
            $retval = int($valuerow->SortOrder);
            last;
        }
    }

    return $retval;
}

sub IsApplicable {
    my $self  = shift;
    my $txn   = $self->TransactionObj;
    my $type  = $txn->Type;
    my @risks = ('Risk Probability', 'Risk Severity');
    my @names = ('Risk Probability', 'Risk Severity', 'Reportable Event');
    my $risk  = 1;
    my $capaQueue = 22;

    if ( $self->TicketObj->Queue == $capaQueue ) {
        # Skip CAPA queue events
        return(undef);

    } elsif ( $type eq 'Create' ) {
        return(undef) unless $self->TicketObj->FirstCustomFieldValue('Reportable Event') eq 'No';

    } elsif ( $self->TicketObj->DependsOn and ( $type eq 'Create' or $type eq 'CustomField' ) ) {
        # Check if ticket already has a dependant CAPA ticket
        while ( my $dependant = $self->TicketObj->DependsOn->Next ) {
            my $depTicket = new RT::Ticket($RT::SystemUser);
            $depTicket->Load($dependant->LocalTarget);
            if( $depTicket->Subject =~ /CAPA Assessment/ ) {
                # $RT::Logger->info("CAPA Assessment ticket already created, aborting");
                return(undef);
            }
        }

    } elsif ( $type eq 'CustomField' ) {
        # This condition is set when any custom field is changed
        my $cf = RT::CustomField->new( $self->CurrentUser );
        $cf->Load( $txn->Field );

        # Bail out if the transaction didn't affect any of our target custom fields
        return(undef) unless $cf->Name eq 'Risk Probability' or
        $cf->Name eq 'Risk Severity'                         or
        $cf->Name eq 'Reportable Event';

        # $RT::Logger->info("txn: " . $cf->Name . ", " . $txn->OldValue . " -> " . $txn->NewValue);

        # Return true immediately if reportable event set to Yes
        return(1) if $cf->Name eq 'Reportable Event' and $txn->NewValue eq 'Yes';

    } else {
        $RT::Logger->info("Not a valid transaction type for this condition");
        return(undef);
    }

    $RT::Logger->info("Condition 'On Risk Threshold Met' triggered "
        ."for ticket #". $self->TicketObj->id
        ." transaction #". $txn->id
    );

    # Now iterate through our targets and calculate risk
    foreach my $riskname (@risks) {
        my $cfvalue = $self->TicketObj->FirstCustomFieldValue($riskname);
        my $riskval = 1;
        if(! $cfvalue || $cfvalue eq '') {
            # Check if risks are filled out, bail if one is empty
            $RT::Logger->info($riskname . " is empty");
            return(undef);
        } else {
            # Calc Risk
            $risk = $risk * getRiskValue($riskname, $cfvalue);
        }
    }

    $RT::Logger->info("Calculated risk is: " . $risk);
    if ($risk >= 3) {
        return(1);
    } else {
        return(undef);
    }

    return(undef);
}

1;