self-service: make payment UI done
[freeside.git] / FS / FS / cust_pkg.pm
index e83b951..bd3d1f5 100644 (file)
@@ -1,7 +1,8 @@
 package FS::cust_pkg;
 
 use strict;
-use vars qw(@ISA);
+use vars qw(@ISA $disable_agentcheck);
+use vars qw( $quiet );
 use FS::UID qw( getotaker dbh );
 use FS::Record qw( qsearch qsearchs );
 use FS::cust_svc;
@@ -19,8 +20,17 @@ use FS::svc_domain;
 use FS::svc_www;
 use FS::svc_forward;
 
+# need all this for sending cancel emails in sub cancel
+
+use FS::Conf;
+use Date::Format;
+use Mail::Internet 1.44;
+use Mail::Header;
+
 @ISA = qw( FS::Record );
 
+$disable_agentcheck = 0;
+
 sub _cache {
   my $self = shift;
   my ( $hashref, $cache ) = @_;
@@ -142,10 +152,13 @@ sub insert {
   my $cust_main = $self->cust_main;
   return "Unknown customer ". $self->custnum unless $cust_main;
 
-  my $agent = qsearchs( 'agent', { 'agentnum' => $cust_main->agentnum } );
-  my $pkgpart_href = $agent->pkgpart_hashref;
-  return "agent ". $agent->agentnum. " can't purchase pkgpart ". $self->pkgpart
-    unless $pkgpart_href->{ $self->pkgpart };
+  unless ( $disable_agentcheck ) {
+    my $agent = qsearchs( 'agent', { 'agentnum' => $cust_main->agentnum } );
+    my $pkgpart_href = $agent->pkgpart_hashref;
+    return "agent ". $agent->agentnum.
+           " can't purchase pkgpart ". $self->pkgpart
+      unless $pkgpart_href->{ $self->pkgpart };
+  }
 
   $self->SUPER::insert;
 
@@ -290,7 +303,43 @@ sub cancel {
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 
+  my $conf = new FS::Conf;
+
+  if ( !$quiet && $conf->exists('emailcancel')
+       && grep { $_ ne 'POST' } $self->cust_main->invoicing_list) {
+  
+      my @invoicing_list = $self->cust_main->invoicing_list;
+  
+      my $invoice_from = $conf->config('invoice_from');
+      my @print_text = map "$_\n", $conf->config('cancelmessage');
+      my $subject = $conf->config('cancelsubject');
+      my $smtpmachine = $conf->config('smtpmachine');
+      
+      if ( grep { $_ ne 'POST' } @invoicing_list ) { #email invoice
+         #false laziness w/FS::cust_pay::delete & fs_signup_server && ::realtime_card
+         #$ENV{SMTPHOSTS} = $smtpmachine;
+         $ENV{MAILADDRESS} = $invoice_from;
+         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: $subject",           
+                                     ] );
+         my $message = new Mail::Internet (
+              'Header' => $header,
+              'Body' => [ @print_text ],      
+                                      );
+         $!=0;
+         $message->smtpsend( Host => $smtpmachine )
+             or $message->smtpsend( Host => $smtpmachine, Debug => 1 );
+         #should this return an error?
+         }
+  }
+
   ''; #no errors
+
 }
 
 =item suspend
@@ -428,6 +477,10 @@ Useful for billing metered services.
 
 sub last_bill {
   my $self = shift;
+  if ( $self->dbdef_table->column('last_bill') ) {
+    return $self->setfield('last_bill', $_[0]) if @_;
+    return $self->getfield('last_bill') if $self->getfield('last_bill');
+  }    
   my $cust_bill_pkg = qsearchs('cust_bill_pkg', { 'pkgnum' => $self->pkgnum,
                                                   'edate'  => $self->bill,  } );
   $cust_bill_pkg ? $cust_bill_pkg->sdate : $self->setup || 0;