[Bps-public-commit] r9454 - in RT-Extension-SLA/lib/RT: Action

ruz at bestpractical.com ruz at bestpractical.com
Thu Oct 25 01:47:55 EDT 2007


Author: ruz
Date: Thu Oct 25 01:47:54 2007
New Revision: 9454

Modified:
   RT-Extension-SLA/lib/RT/Action/SLA_SetDue.pm
   RT-Extension-SLA/lib/RT/Extension/SLA.pm

Log:
* rewrite SetDue action, now it's much closer to the spec

Modified: RT-Extension-SLA/lib/RT/Action/SLA_SetDue.pm
==============================================================================
--- RT-Extension-SLA/lib/RT/Action/SLA_SetDue.pm	(original)
+++ RT-Extension-SLA/lib/RT/Action/SLA_SetDue.pm	Thu Oct 25 01:47:54 2007
@@ -41,7 +41,37 @@
         return 1;
     }
 
-    my $due = $self->EarliestDue( $level );
+    my $txn = $self->TransactionObj;
+
+    my $last_reply = $self->LastCorrespond;
+    $RT::Logger->debug('Last reply to ticket #'. $ticket->id .' is txn #'. $last_reply->id );
+    my $is_requestors_act = $self->IsRequestorsAct( $last_reply );
+    $RT::Logger->debug('Txn #'. $last_reply->id .' is requestors\' action') if $is_requestors_act;
+
+    my $response_due = $self->Due(
+        Level => $level,
+        Type => 'Response',
+        Time => $last_reply->CreatedObj->Unix,
+    );
+
+    my $resolve_due = $self->Due(
+        Level => $level,
+        Type => 'Resolve',
+        Time => $ticket->CreatedObj->Unix,
+    );
+
+    my $type = $txn->Type;
+
+    my $due;
+    $due = $response_due if defined $response_due && $is_requestors_act;
+    $due = $resolve_due unless defined $due;
+    $due = $resolve_due if defined $due && defined $resolve_due && $resolve_due < $due;
+
+    if ( defined $due ) {
+        return 1 if $ticket->DueObj->Unix == $due;
+    } else {
+        return 1 if $ticket->DueObj->Unix <= 0;
+    }
 
     my $date = RT::Date->new( $RT::SystemUser );
     $date->Set( Format => 'unix', Value => $due );
@@ -54,21 +84,31 @@
     return 1;
 }
 
-sub EarliestDue {
+sub IsRequestorsAct {
     my $self = shift;
-    my $level = shift;
+    my $txn = shift || $self->TransactionObj;
 
-    my $response_time = $self->TransactionObj->CreatedObj->Unix;
-    my $response_due = $self->Agreements(
-        Type => 'Response', Time => $response_time
-    )->Due( $response_time, $level );
-
-    my $create_time = $self->TicketObj->CreatedObj->Unix;
-    my $resolve_due  = $self->Agreements(
-        Type => 'Resolve', Time => $create_time
-    )->Due( $create_time, $level );
+    return $self->TicketObj->Requestors->HasMemberRecursively(
+        $txn->CreatorObj->PrincipalObj
+    )? 1 : 0;
+}
 
-    return $resolve_due < $response_due? $resolve_due : $response_due;
+sub LastCorrespond {
+    my $self = shift;
+    
+    my $txn = $self->TransactionObj;
+    return $txn if $txn->Type eq 'Create'
+        || $txn->Type eq 'Correspond';
+
+    my $txns = $self->TicketObj->Transactions;
+    $txns->Limit( FIELD => 'Type', VALUE => 'Correspond' );
+    $txns->Limit( FIELD => 'Type', VALUE => 'Create' );
+    $txns->OrderByCols(
+        { FIELD => 'Created', ORDER => 'DESC' },
+        { FIELD => 'id', ORDER => 'DESC' },
+    );
+    $txns->RowsPerPage(1);
+    return $txns->First;
 }
 
 1;

Modified: RT-Extension-SLA/lib/RT/Extension/SLA.pm
==============================================================================
--- RT-Extension-SLA/lib/RT/Extension/SLA.pm	(original)
+++ RT-Extension-SLA/lib/RT/Extension/SLA.pm	Thu Oct 25 01:47:54 2007
@@ -209,7 +209,7 @@
         $res{'StartImmediately'} = $meta->{'StartImmediately'};
     }
 
-    if ( $meta->{'OutOfHours'}{ $args{'Type'} } && $args{'Time'} ) {
+    if ( $args{'Time'} and my $tmp = $meta->{'OutOfHours'}{ $args{'Type'} } ) {
         my $bhours = $self->BusinessHours;
         if ( $bhours->first_after( $args{'Time'} ) != $args{'Time'} ) {
             foreach ( qw(RealMinutes BusinessMinutes) ) {
@@ -223,6 +223,24 @@
     return \%res;
 }
 
+sub Due {
+    my $self = shift;
+    my %args = ( Level => undef, Type => undef, Time => undef, @_ );
+
+    my $agreement = $self->Agreement( %args );
+    return undef unless $agreement;
+
+    my $res = $args{'Time'};
+    if ( defined $agreement->{'BusinessMinutes'} ) {
+        my $bhours = $self->BusinessHours;
+        $res = $bhours->add_seconds( $res, 60 * $agreement->{'BusinessMinutes'} );
+    }
+    $res += $agreement->{'RealMinutes'}
+        if defined $agreement->{'RealMinutes'};
+
+    return $res;
+}
+
 =head2 Agreements [ Type => 'Response' ]
 
 Returns an instance of L<Business::SLA> class filled with



More information about the Bps-public-commit mailing list