per-agent invoice_from addresses
[freeside.git] / FS / FS / cust_bill.pm
index 367e1bc..d0583b2 100644 (file)
@@ -5,6 +5,7 @@ use vars qw( @ISA $conf $money_char );
 use vars qw( $invoice_lines @buf ); #yuck
 use Date::Format;
 use Text::Template;
+use File::Temp 0.14;
 use FS::UID qw( datasrc );
 use FS::Record qw( qsearch qsearchs );
 use FS::Misc qw( send_email );
@@ -316,7 +317,7 @@ sub owed {
   $balance;
 }
 
-=item send [ TEMPLATENAME [ , AGENTNUM ] ]
+=item send [ TEMPLATENAME [ , AGENTNUM [ , INVOICE_FROM ] ] ]
 
 Sends this invoice to the destinations configured for this customer: send
 emails or print.  See L<FS::cust_main_invoice>.
@@ -326,12 +327,15 @@ TEMPLATENAME, if specified, is the name of a suffix for alternate invoices.
 AGENTNUM, if specified, means that this invoice will only be sent for customers
 of the specified agent.
 
+INVOICE_FROM, if specified, overrides the default email invoice From: address.
+
 =cut
 
 sub send {
   my $self = shift;
   my $template = scalar(@_) ? shift : '';
   return '' if scalar(@_) && $_[0] && $self->cust_main->agentnum ne shift;
+  my $invoice_from = scalar(@_) ? shift : $conf->config('invoice_from');
 
   my @print_text = $self->print_text('', $template);
   my @invoicing_list = $self->cust_main->invoicing_list;
@@ -339,10 +343,10 @@ sub send {
   if ( grep { $_ ne 'POST' } @invoicing_list or !@invoicing_list  ) { #email
 
     #better to notify this person than silence
-    @invoicing_list = ($conf->config('invoice_from')) unless @invoicing_list;
+    @invoicing_list = ($invoice_from) unless @invoicing_list;
 
     my $error = send_email(
-      'from'    => $conf->config('invoice_from'),
+      'from'    => $invoice_from,
       'to'      => [ grep { $_ ne 'POST' } @invoicing_list ],
       'subject' => 'Invoice',
       'body'    => \@print_text,
@@ -1108,17 +1112,17 @@ sub print_latex {
     $var;
   }
 
-  my $dir = '/tmp'; #! /usr/local/etc/freeside/invoices.datasrc/
-  my $unique = int(rand(2**31)); #UGH... use File::Temp or something
-
-  chdir($dir);
-  my $file = $self->invnum. ".$unique";
+  my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
+  my $fh = new File::Temp( TEMPLATE => 'invoice.'. $self->invnum. '.XXXXXXXX',
+                           DIR      => $dir,
+                           SUFFIX   => '.tex',
+                           UNLINK   => 0,
+                         ) or die "can't open temp file: $!\n";
+  print $fh join("\n", @filled_in ), "\n";
+  close $fh;
 
-  open(TEX,">$file.tex") or die "can't open $file.tex: $!\n";
-  print TEX join("\n", @filled_in ), "\n";
-  close TEX;
-
-  return $file;
+  $fh->filename =~ /^(.*).tex$/ or die "unparsable filename: ". $fh->filename;
+  return $1;
 
 }
 
@@ -1138,13 +1142,19 @@ sub print_ps {
 
   my $file = $self->print_latex(@_);
 
-  #error checking!!
-  system('pslatex', "$file.tex");
-  system('pslatex', "$file.tex");
-  system('dvips', '-q', '-t', 'letter', "$file.dvi", '-o', "$file.ps" );
+  my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
+  chdir($dir);
+
+  system("pslatex $file.tex >/dev/null 2>&1") == 0
+    or die "pslatex $file.tex failed: $!";
+  system("pslatex $file.tex >/dev/null 2>&1") == 0
+    or die "pslatex $file.tex failed: $!";
+
+  system('dvips', '-q', '-t', 'letter', "$file.dvi", '-o', "$file.ps" ) == 0
+    or die "dvips failed: $!";
 
   open(POSTSCRIPT, "<$file.ps")
-    or die "can't open $file.ps (probable error in LaTeX template): $!\n";
+    or die "can't open $file.ps: $! (error in LaTeX template?)\n";
 
   unlink("$file.dvi", "$file.log", "$file.aux", "$file.ps", "$file.tex");
 
@@ -1175,19 +1185,28 @@ sub print_pdf {
 
   my $file = $self->print_latex(@_);
 
+  my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
+  chdir($dir);
+
   #system('pdflatex', "$file.tex");
   #system('pdflatex', "$file.tex");
   #! LaTeX Error: Unknown graphics extension: .eps.
 
-  #error checking!!
-  system('pslatex', "$file.tex");
-  system('pslatex', "$file.tex");
+  system("pslatex $file.tex >/dev/null 2>&1") == 0
+    or die "pslatex $file.tex failed: $!";
+  system("pslatex $file.tex >/dev/null 2>&1") == 0
+    or die "pslatex $file.tex failed: $!";
 
   #system('dvipdf', "$file.dvi", "$file.pdf" );
-  system("dvips -q -t letter -f $file.dvi | gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$file.pdf -c save pop -");
+  system(
+    "dvips -q -t letter -f $file.dvi ".
+    "| gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$file.pdf ".
+    "     -c save pop -"
+  ) == 0
+    or die "dvips | gs failed: $!";
 
   open(PDF, "<$file.pdf")
-    or die "can't open $file.pdf (probably error in LaTeX tempalte: $!\n";
+    or die "can't open $file.pdf: $! (error in LaTeX template?)\n";
 
   unlink("$file.dvi", "$file.log", "$file.aux", "$file.pdf", "$file.tex");