taqua accountcode billing, part 2 of 2, RT12181
[freeside.git] / FS / FS / did_order.pm
index f46d72b..c1b34c3 100644 (file)
@@ -2,7 +2,7 @@ package FS::did_order;
 
 use strict;
 use base qw( FS::o2m_Common FS::Record );
-use FS::Record qw( qsearch qsearchs );
+use FS::Record qw( qsearch qsearchs dbh );
 
 =head1 NAME
 
@@ -89,7 +89,43 @@ Delete this record from the database.
 
 =cut
 
-# the delete method can be inherited from FS::Record
+sub delete {
+  my $self = shift;
+
+  return "Can't delete a DID order which has DIDs received"
+    if qsearch( 'phone_avail', { 'ordernum' => $self->ordernum } );
+
+  local $SIG{HUP} = 'IGNORE';
+  local $SIG{INT} = 'IGNORE';
+  local $SIG{QUIT} = 'IGNORE';
+  local $SIG{TERM} = 'IGNORE';
+  local $SIG{TSTP} = 'IGNORE';
+  local $SIG{PIPE} = 'IGNORE';
+
+  my $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
+
+  my @did_order_item = $self->did_order_item;
+
+  foreach my $did_order_item ( @did_order_item ) {
+    my $error = $did_order_item->delete;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "can't delete DID order item "
+                                . $did_order_item->orderitemnum . ": $error";
+    }
+  }
+
+  my $error = $self->SUPER::delete(@_);
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
+  }
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+}
+
 
 =item replace OLD_RECORD
 
@@ -138,6 +174,32 @@ sub did_order_item {
   qsearch( 'did_order_item', { 'ordernum' => $self->ordernum } );
 }
 
+=item cust_main
+
+Returns the cust_main (see L<FS::cust_main>), if any, associated with this bulk DID order.
+
+=cut
+
+sub cust_main {
+  my $self = shift;
+  return '' unless $self->custnum;
+  qsearchs('cust_main', { 'custnum' => $self->custnum } );
+}
+
+=item provisioned
+
+Returns the provisioned DIDs, if any, as phone_avail (see L<FS::phone_avail>) objects.
+
+=cut
+
+sub provisioned {
+  my $self = shift;
+  qsearch({ table   => 'phone_avail',
+              hashref => { 'ordernum' => $self->ordernum, },
+              select  => 'phone_avail.*',
+              extra_sql => ' and svcnum is not null ',
+         });
+}
 
 =back