delete invoices, RT#4048
[freeside.git] / FS / FS / cust_bill.pm
index 1dbfe54..e6d0b0d 100644 (file)
@@ -139,7 +139,49 @@ Really, don't use it.
 sub delete {
   my $self = shift;
   return "Can't delete closed invoice" if $self->closed =~ /^Y/i;
-  $self->SUPER::delete(@_);
+
+  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;
+
+  foreach my $table (qw(
+    cust_bill_event
+    cust_credit_bill
+    cust_bill_pay
+    cust_bill_pay
+    cust_credit_bill
+    cust_pay_batch
+    cust_bill_pay_batch
+    cust_bill_pkg
+  )) {
+
+    foreach my $linked ( $self->$table() ) {
+      my $error = $linked->delete;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return $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
@@ -353,6 +395,16 @@ sub cust_pay {
   #;
 }
 
+sub cust_pay_batch {
+  my $self = shift;
+  qsearch('cust_pay_batch', { 'invnum' => $self->invnum } );
+}
+
+sub cust_bill_pay_batch {
+  my $self = shift;
+  qsearch('cust_bill_pay_batch', { 'invnum' => $self->invnum } );
+}
+
 =item cust_bill_pay
 
 Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice.
@@ -367,6 +419,8 @@ sub cust_bill_pay {
 
 =item cust_credited
 
+=item cust_credit_bill
+
 Returns all applied credits (see L<FS::cust_credit_bill>) for this invoice.
 
 =cut
@@ -378,6 +432,10 @@ sub cust_credited {
   ;
 }
 
+sub cust_credit_bill {
+  shift->cust_credited(@_);
+}
+
 =item tax
 
 Returns the tax amount (see L<FS::cust_bill_pkg>) for this invoice.
@@ -733,7 +791,7 @@ sub mimebuild_pdf {
     'Encoding'    => 'base64',
     'Data'        => [ $self->print_pdf(@_) ],
     'Disposition' => 'attachment',
-    'Filename'    => 'invoice.pdf',
+    'Filename'    => 'invoice-'. $self->invnum. '.pdf',
   );
 }
 
@@ -845,10 +903,13 @@ sub email {
   #better to notify this person than silence
   @invoicing_list = ($invoice_from) unless @invoicing_list;
 
+  my $subject = $self->email_subject($template);
+
   my $error = send_email(
     $self->generate_email(
       'from'       => $invoice_from,
       'to'         => [ grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list ],
+      'subject'    => $subject,
       'template'   => $template,
     )
   );
@@ -857,6 +918,23 @@ sub email {
 
 }
 
+sub email_subject {
+  my $self = shift;
+
+  #my $template = scalar(@_) ? shift : '';
+  #per-template?
+
+  my $subject = $conf->config('invoice_subject') || 'Invoice';
+
+  my $cust_main = $self->cust_main;
+  my $name = $cust_main->name;
+  my $name_short = $cust_main->name_short;
+  my $invoice_number = $self->invnum;
+  my $invoice_date = $self->_date_pretty;
+
+  eval qq("$subject");
+}
+
 =item lpr_data [ TEMPLATENAME ]
 
 Returns the postscript or plaintext for this invoice as an arrayref.
@@ -1930,7 +2008,7 @@ sub print_latex {
                 !~ /^%%EndDetail\s*$/                            ) {
           push @line_item, $line_item_line;
         }
-        foreach my $line_item ( $self->_items ) {
+        foreach my $line_item ( $self->_items ) { #( 'format'=>'latex' ) ) {
         #foreach my $line_item ( $self->_items_pkg ) {
           $invoice_data{'ref'} = $line_item->{'pkgnum'};
           $invoice_data{'description'} =
@@ -2472,7 +2550,18 @@ Returns a string with the invoice number and date, for example:
 
 sub invnum_date_pretty {
   my $self = shift;
-  'Invoice #'. $self->invnum. ' ('. time2str('%x', $self->_date). ')';
+  'Invoice #'. $self->invnum. ' ('. $self->_date_pretty. ')';
+}
+
+=item _date_pretty
+
+Returns a string with the date, for example: "3/20/2008"
+
+=cut
+
+sub _date_pretty {
+  my $self = shift;
+  time2str('%x', $self->_date);
 }
 
 sub _items {
@@ -2548,6 +2637,8 @@ sub _items_cust_bill_pkg {
     my $cust_pkg = $cust_bill_pkg->cust_pkg;
 
     my $desc = $cust_bill_pkg->desc;
+    $desc = substr($desc, 0, 50). '...'
+      if $format eq 'latex' && length($desc) > 50;
 
     my %details_opt = ( 'format'          => $format,
                         'escape_function' => $escape_function,
@@ -2560,8 +2651,11 @@ sub _items_cust_bill_pkg {
         my $description = $desc;
         $description .= ' Setup' if $cust_bill_pkg->recur != 0;
 
-        my @d = map &{$escape_function}($_),
-                       $cust_pkg->h_labels_short($self->_date);
+        my @d = ();
+        push @d, map &{$escape_function}($_),
+                     $cust_pkg->h_labels_short($self->_date)
+          unless $cust_pkg->part_pkg->hide_svc_detail;
+
         push @d, $cust_bill_pkg->details(%details_opt)
           if $cust_bill_pkg->recur == 0;
 
@@ -2584,12 +2678,17 @@ sub _items_cust_bill_pkg {
                           " - ". time2str("%x", $cust_bill_pkg->edate). ")";
         }
 
+        my @d = ();
+
         #at least until cust_bill_pkg has "past" ranges in addition to
         #the "future" sdate/edate ones... see #3032
-        my @d = map &{$escape_function}($_),
-                    $cust_pkg->h_labels_short($self->_date);
-                                              #$cust_bill_pkg->edate,
-                                              #$cust_bill_pkg->sdate),
+        push @d, map &{$escape_function}($_),
+                     $cust_pkg->h_labels_short($self->_date)
+                                               #$cust_bill_pkg->edate,
+                                               #$cust_bill_pkg->sdate),
+          unless $cust_pkg->part_pkg->hide_svc_detail
+              || $cust_bill_pkg->itemdesc;
+
         push @d, $cust_bill_pkg->details(%details_opt);
 
         push @b, {