delete invoices, RT#4048
[freeside.git] / FS / FS / cust_bill.pm
index 006892c..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.
@@ -913,6 +991,46 @@ sub fax_invoice {
 
 }
 
+=item ftp_invoice [ TEMPLATENAME ] 
+
+Sends this invoice data via FTP.
+
+TEMPLATENAME is unused?
+
+=cut
+
+sub ftp_invoice {
+  my $self = shift;
+  my $template = scalar(@_) ? shift : '';
+
+  $self->send_csv(
+    'protocol'   => 'ftp',
+    'server'     => $conf->config('cust_bill-ftpserver'),
+    'username'   => $conf->config('cust_bill-ftpusername'),
+    'password'   => $conf->config('cust_bill-ftppassword'),
+    'dir'        => $conf->config('cust_bill-ftpdir'),
+    'format'     => $conf->config('cust_bill-ftpformat'),
+  );
+}
+
+=item spool_invoice [ TEMPLATENAME ] 
+
+Spools this invoice data (see L<FS::spool_csv>)
+
+TEMPLATENAME is unused?
+
+=cut
+
+sub spool_invoice {
+  my $self = shift;
+  my $template = scalar(@_) ? shift : '';
+
+  $self->spool_csv(
+    'format'       => $conf->config('cust_bill-spoolformat'),
+    'agent_spools' => $conf->exists('cust_bill-spoolagent'),
+  );
+}
+
 =item send_if_newest [ TEMPLATENAME [ , AGENTNUM [ , INVOICE_FROM ] ] ]
 
 Like B<send>, but only sends the invoice if it is the newest open invoice for
@@ -1805,6 +1923,7 @@ sub print_latex {
     'state'        => _latex_escape($cust_main->state),
     #'quantity'     => 1,
     'zip'          => _latex_escape($cust_main->zip),
+    'fax'          => _latex_escape($cust_main->fax),
     'footer'       => join("\n", $conf->config_orbase('invoice_latexfooter', $template) ),
     'smallfooter'  => join("\n", $conf->config_orbase('invoice_latexsmallfooter', $template) ),
     'returnaddress' => $returnaddress,
@@ -1820,13 +1939,15 @@ sub print_latex {
     'unitprices'   => $conf->exists('invoice-unitprice'),
   );
 
+  my $countrydefault = $conf->config('countrydefault') || 'US';
   my $prefix = $cust_main->has_ship_address ? 'ship_' : '';
   foreach ( qw( contact company address1 address2 city state zip country fax) ){
     my $method = $prefix.$_;
     $invoice_data{"ship_$_"} = _latex_escape($cust_main->$method);
   }
+  $invoice_data{'ship_country'} = ''
+    if ( $invoice_data{'ship_country'} eq $countrydefault );
 
-  my $countrydefault = $conf->config('countrydefault') || 'US';
   if ( $cust_main->country eq $countrydefault ) {
     $invoice_data{'country'} = '';
   } else {
@@ -1887,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'} =
@@ -2200,6 +2321,7 @@ sub print_html {
     'city'         => encode_entities($cust_main->city),
     'state'        => encode_entities($cust_main->state),
     'zip'          => encode_entities($cust_main->zip),
+    'fax'          => encode_entities($cust_main->fax),
     'terms'        => $conf->config('invoice_default_terms')
                       || 'Payable upon receipt',
     'cid'          => $cid,
@@ -2227,6 +2349,7 @@ sub print_html {
                        s/~/&nbsp;/g;
                        s/\\\\\*?\s*$/<BR>/;
                        s/\\hyphenation\{[\w\s\-]+\}//;
+                       s/\\([&])/$1/g;
                        $_;
                      }
                      $conf->config_orbase( 'invoice_latexreturnaddress',
@@ -2427,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 {
@@ -2503,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,
@@ -2515,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;
 
@@ -2535,16 +2674,21 @@ sub _items_cust_bill_pkg {
 
         my $description = $desc;
         unless ( $conf->exists('disable_line_item_date_ranges') ) {
-          $desc .= " (" . time2str("%x", $cust_bill_pkg->sdate).
-                   " - ". time2str("%x", $cust_bill_pkg->edate). ")";
+          $description .= " (" . time2str("%x", $cust_bill_pkg->sdate).
+                          " - ". 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, {
@@ -2644,7 +2788,7 @@ sub _items_payments {
 
 =over 4
 
-=item reprint
+=item process_reprint
 
 =cut
 
@@ -2652,7 +2796,7 @@ sub process_reprint {
   process_re_X('print', @_);
 }
 
-=item reemail
+=item process_reemail
 
 =cut
 
@@ -2660,7 +2804,7 @@ sub process_reemail {
   process_re_X('email', @_);
 }
 
-=item refax
+=item process_refax
 
 =cut
 
@@ -2668,6 +2812,22 @@ sub process_refax {
   process_re_X('fax', @_);
 }
 
+=item process_reftp
+
+=cut
+
+sub process_reftp {
+  process_re_X('ftp', @_);
+}
+
+=item respool
+
+=cut
+
+sub process_respool {
+  process_re_X('spool', @_);
+}
+
 use Storable qw(thaw);
 use Data::Dumper;
 use MIME::Base64;
@@ -2711,6 +2871,8 @@ sub re_X {
     'debug' => 1,
   } );
 
+  $method .= '_invoice' unless $method eq 'email' || $method eq 'print';
+
   warn " $me re_X $method: ". scalar(@cust_bill). " invoices found\n"
     if $DEBUG;