beginnings of one-screen new customer entry and some other miscellania
[freeside.git] / site_perl / cust_main.pm
index 979b6f4..5a83741 100644 (file)
@@ -5,12 +5,14 @@ use vars qw($paymentserversecret $paymentserverport $paymentserverhost);
 package FS::cust_main;
 
 use strict;
-use vars qw(@ISA $conf $lpr $processor $xaction $E_NoErr);
+use vars qw(@ISA $conf $lpr $processor $xaction $E_NoErr $invoice_from);
 use Safe;
 use Carp;
 use Time::Local;
 use Date::Format;
 use Date::Manip;
+use Mail::Internet;
+use Mail::Header;
 use Business::CreditCard;
 use FS::UID qw( getotaker );
 use FS::Record qw( qsearchs qsearch );
@@ -31,6 +33,7 @@ use FS::cust_main_invoice;
 $FS::UID::callback{'FS::cust_main'} = sub { 
   $conf = new FS::Conf;
   $lpr = $conf->config('lpr');
+  $invoice_from = $conf->config('invoice_from');
 
   if ( $conf->exists('cybercash3.2') ) {
     require CCMckLib3_2;
@@ -356,7 +359,7 @@ If there is an error, returns the error, otherwise returns false.
 
 sub bill {
   my( $self, %options ) = @_;
-  my $time = $options{'time'} || $^T;
+  my $time = $options{'time'} || time;
 
   my $error;
 
@@ -366,6 +369,7 @@ sub bill {
   local $SIG{QUIT} = 'IGNORE';
   local $SIG{TERM} = 'IGNORE';
   local $SIG{TSTP} = 'IGNORE';
+  local $SIG{PIPE} = 'IGNORE';
 
   # find the packages which are due for billing, find out how much they are
   # & generate invoice database.
@@ -543,7 +547,7 @@ return an error.  By default, they don't.
 
 sub collect {
   my( $self, %options ) = @_;
-  my $invoice_time = $options{'invoice_time'} || $^T;
+  my $invoice_time = $options{'invoice_time'} || time;
 
   my $total_owed = $self->balance;
   return '' unless $total_owed > 0; #redundant?????
@@ -554,6 +558,7 @@ sub collect {
   local $SIG{QUIT} = 'IGNORE';
   local $SIG{TERM} = 'IGNORE';
   local $SIG{TSTP} = 'IGNORE';
+  local $SIG{PIPE} = 'IGNORE';
 
   foreach my $cust_bill (
     qsearch('cust_bill', { 'custnum' => $self->custnum, } )
@@ -583,11 +588,30 @@ sub collect {
            && ( $cust_bill->printed * 2592000 ) <= $since
       ) {
 
-        open(LPR, "|$lpr") or die "Can't open pipe to $lpr: $!";
-        print LPR $cust_bill->print_text; #( date )
-        close LPR
-          or die $! ? "Error closing $lpr: $!"
-                       : "Exit status $? from $lpr";
+        #my @print_text = $cust_bill->print_text; #( date )
+        my @invoicing_list = $self->invoicing_list;
+        if ( grep { $_ ne 'POST' } @invoicing_list ) { #email invoice
+          my $header = new Mail::Header ( [
+            "From: $invoice_from",
+            "To: ". join(', ', grep { $_ ne 'POST' } @invoicing_list ),
+            "Sender: $invoice_from",
+            "Reply-To: $invoice_from",
+            "Date: ". time2str("%a, %d %b %Y %X %z", time),
+            "Subject: Invoice",
+          ] );
+          my $message = new Mail::Internet (
+            'Header' => $header,
+            'Body' => [ $cust_bill->print_text ], #( date)
+          );
+          $message->smtpsend or die "Can't send invoice email!"; #die?  warn?
+
+        } elsif ( ! @invoicing_list || grep { $_ eq 'POST' } @invoicing_list ) {
+          open(LPR, "|$lpr") or die "Can't open pipe to $lpr: $!";
+          print LPR $cust_bill->print_text; #( date )
+          close LPR
+            or die $! ? "Error closing $lpr: $!"
+                         : "Exit status $? from $lpr";
+        }
 
         my %hash = $cust_bill->hash;
         $hash{'printed'}++;
@@ -609,6 +633,7 @@ sub collect {
       my $error = $cust_pay->insert;
       return 'Error COMPing invnum #' . $cust_bill->invnum .
              ':' . $error if $error;
+
     } elsif ( $self->payby eq 'CARD' ) {
 
       if ( $options{'batch_card'} ne 'yes' ) {
@@ -710,6 +735,10 @@ sub collect {
       return "Unknown payment type ". $self->payby;
     }
 
+
+
+
+
   }
   '';
 
@@ -820,7 +849,10 @@ sub check_invoicing_list {
       'custnum' => $self->custnum,
       'dest'    => $address,
     } );
-    my $error = $cust_main_invoice->check;
+    my $error = $self->custnum
+                ? $cust_main_invoice->check
+                : $cust_main_invoice->checkdest
+    ;
     return $error if $error;
   }
   '';
@@ -830,7 +862,7 @@ sub check_invoicing_list {
 
 =head1 VERSION
 
-$Id: cust_main.pm,v 1.9 1999-01-18 09:22:41 ivan Exp $
+$Id: cust_main.pm,v 1.11 1999-02-23 08:09:27 ivan Exp $
 
 =head1 BUGS
 
@@ -886,7 +918,13 @@ enable cybercash, cybercash v3 support, don't need to import
 FS::UID::{datasrc,checkruid} ivan@sisd.com 98-sep-19-21
 
 $Log: cust_main.pm,v $
-Revision 1.9  1999-01-18 09:22:41  ivan
+Revision 1.11  1999-02-23 08:09:27  ivan
+beginnings of one-screen new customer entry and some other miscellania
+
+Revision 1.10  1999/01/25 12:26:09  ivan
+yet more mod_perl stuff
+
+Revision 1.9  1999/01/18 09:22:41  ivan
 changes to track email addresses for email invoicing
 
 Revision 1.8  1998/12/29 11:59:39  ivan