payment receipts!
authorivan <ivan>
Wed, 30 Jun 2004 17:57:03 +0000 (17:57 +0000)
committerivan <ivan>
Wed, 30 Jun 2004 17:57:03 +0000 (17:57 +0000)
FS/FS/Conf.pm
FS/FS/cust_bill_pay.pm
FS/FS/cust_credit_bill.pm
FS/FS/cust_pay.pm
FS/FS/cust_pay_refund.pm
conf/payment_receipt_email [new file with mode: 0644]

index d35b1ac..c553f0c 100644 (file)
@@ -542,12 +542,19 @@ httemplate/docs/config.html
 
   {
     'key'         => 'invoice_send_receipts',
 
   {
     'key'         => 'invoice_send_receipts',
-    'section'     => 'billing',
-    'description' => 'Send receipts for payments and credits.',
+    'section'     => 'deprecated',q
+    'description' => '<b>DEPRECATED</b>, this used to send an invoice copy on payments and credits.  See the payment_receipt_email and  instead.',
     'type'        => 'checkbox',
   },
 
   {
     'type'        => 'checkbox',
   },
 
   {
+    'key'         => 'payment_receipt_email',
+    'section'     => 'billing',
+    'description' => 'Template file for payment receipts.',
+    'type'        => 'textarea',
+  },
+
+  {
     'key'         => 'lpr',
     'section'     => 'required',
     'description' => 'Print command for paper invoices, for example `lpr -h\'',
     'key'         => 'lpr',
     'section'     => 'required',
     'description' => 'Print command for paper invoices, for example `lpr -h\'',
index f0cb132..7abbe9a 100644 (file)
@@ -70,21 +70,6 @@ sub table { 'cust_bill_pay'; }
 Adds this record to the database.  If there is an error, returns the error,
 otherwise returns false.
 
 Adds this record to the database.  If there is an error, returns the error,
 otherwise returns false.
 
-=cut
-
-sub insert {
-  my $self = shift;
-  my $error = $self->SUPER::insert;
-  return $error if $error;
-
- if ( $conf->exists('invoice_send_receipts') ) {
-    my $send_error = $self->cust_bill->send;
-    warn "Error sending receipt: $send_error\n" if $send_error;
-  }
-
-  '';
-}
-
 =item delete
 
 Deletes this payment application, unless the closed flag for the parent payment
 =item delete
 
 Deletes this payment application, unless the closed flag for the parent payment
index 0e5885f..695df6e 100644 (file)
@@ -74,21 +74,6 @@ sub table { 'cust_credit_bill'; }
 Adds this cust_credit_bill to the database ("Posts" all or part of a credit).
 If there is an error, returns the error, otherwise returns false.
 
 Adds this cust_credit_bill to the database ("Posts" all or part of a credit).
 If there is an error, returns the error, otherwise returns false.
 
-=cut
-
-sub insert {
-  my $self = shift;
-  my $error = $self->SUPER::insert(@_);
-  return $error if $error;
-
-  if ( $conf->exists('invoice_send_receipts') ) {
-    my $send_error = $self->cust_bill->send;
-    warn "Error sending receipt: $send_error\n" if $send_error;
-  }
-
-  '';
-}
-
 =item delete
 
 Currently unimplemented.
 =item delete
 
 Currently unimplemented.
index 111de05..d592313 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use vars qw( @ISA $conf $unsuspendauto );
 use Date::Format;
 use Business::CreditCard;
 use vars qw( @ISA $conf $unsuspendauto );
 use Date::Format;
 use Business::CreditCard;
+use Text::Template;
 use FS::UID qw( dbh );
 use FS::Record qw( dbh qsearch qsearchs dbh );
 use FS::Misc qw(send_email);
 use FS::UID qw( dbh );
 use FS::Record qw( dbh qsearch qsearchs dbh );
 use FS::Misc qw(send_email);
@@ -112,7 +113,7 @@ sub insert {
     $self->custnum($cust_bill->custnum );
   }
 
     $self->custnum($cust_bill->custnum );
   }
 
-  my $cust_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
+  my $cust_main = $self->cust_main;
   my $old_balance = $cust_main->balance;
 
   my $error = $self->check;
   my $old_balance = $cust_main->balance;
 
   my $error = $self->check;
@@ -162,64 +163,44 @@ sub insert {
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 
-  '';
-
-}
-
-sub upgrade_replace { #1.3.x->1.4.x
-  my $self = shift;
-
-  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;
-
-  my $error = $self->check;
-  return $error if $error;
+  #my $cust_main = $self->cust_main;
+  if ( $conf->exists('payment_receipt_email')
+       && grep { $_ ne 'POST' } $cust_main->invoicing_list
+  ) {
+
+    my $receipt_template = new Text::Template (
+      TYPE   => 'ARRAY',
+      SOURCE => [ map "$_\n", $conf->config('payment_receipt_email') ],
+    ) or do {
+      warn "can't create payment receipt template: $Text::Template::ERROR";
+      return '';
+    };
 
 
-  my %new = $self->hash;
-  my $new = FS::cust_pay->new(\%new);
+    my @invoicing_list = grep { $_ ne 'POST' } $cust_main->invoicing_list;
 
 
-  if ( $self->invnum ) {
-    my $cust_bill_pay = new FS::cust_bill_pay {
-      'invnum' => $self->invnum,
-      'paynum' => $self->paynum,
-      'amount' => $self->paid,
-      '_date'  => $self->_date,
-    };
-    $error = $cust_bill_pay->insert;
-    if ( $error =~ 
-           /total cust_bill_pay.amount and cust_credit_bill.amount .* for invnum .* greater than cust_bill.charged/ ) {
-      #warn $error;
-      my $cust_bill = qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
-      $new->custnum($cust_bill->custnum);
-    } elsif ( $error ) {
-      $dbh->rollback if $oldAutoCommit;
-      return $error;
-    } else {
-      $new->custnum($cust_bill_pay->cust_bill->custnum);
+    my $error = send_email(
+      'from'    => $conf->config('invoice_from'), #??? well as good as any
+      'to'      => \@invoicing_list,
+      'subject' => 'Payment receipt',
+      'body'    => $receipt_template->fill_in( HASH => {
+                     'date'    => str2time("%a %B %o, %Y", $self->_date),
+                     'paynum'  => $self->paynum,
+                     'paid'    => $self->paid,
+                     'payby'   => ucfirst(lc($self->payby)),
+                     'payinfo' => ( $self->payby eq 'CARD'
+                                      ? $self->payinfo_masked
+                                      : $self->payinfo        ),
+                     'balance' => $cust_main->balance,
+                   } ),
+    );
+    if ( $error ) {
+      warn "can't send payment receipt: $error";
     }
     }
-  } else {
-    die;
-  }
 
 
-  $error = $new->SUPER::replace($self);
-  if ( $error ) {
-    $dbh->rollback if $oldAutoCommit;
-    return $error;
   }
 
   }
 
-  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
-
   '';
 
   '';
 
-
 }
 
 =item delete
 }
 
 =item delete
@@ -260,7 +241,7 @@ sub delete {
 
   if ( $conf->config('deletepayments') ne '' ) {
 
 
   if ( $conf->config('deletepayments') ne '' ) {
 
-    my $cust_main = qsearchs('cust_main',{ 'custnum' => $self->custnum });
+    my $cust_main = $self->cust_main;
 
     my $error = send_email(
       'from'    => $conf->config('invoice_from'), #??? well as good as any
 
     my $error = send_email(
       'from'    => $conf->config('invoice_from'), #??? well as good as any
index 55a5eb7..af25f17 100644 (file)
@@ -73,16 +73,6 @@ sub table { 'cust_pay_refund'; }
 Adds this cust_pay_refund to the database.  If there is an error, returns the
 error, otherwise returns false.
 
 Adds this cust_pay_refund to the database.  If there is an error, returns the
 error, otherwise returns false.
 
-=cut
-
-sub insert {
-  my $self = shift;
-  my $error = $self->SUPER::insert(@_);
-  return $error if $error;
-
-  '';
-}
-
 =item delete
 
 =cut
 =item delete
 
 =cut
diff --git a/conf/payment_receipt_email b/conf/payment_receipt_email
new file mode 100644 (file)
index 0000000..fa5a735
--- /dev/null
@@ -0,0 +1,26 @@
+
+{ $date }
+
+Dear { $name },
+
+This message is to inform you that your payment of ${ $amount } has been
+received.
+
+Payment ID: { $paynum }
+Date:       { $date }
+Amount:     { $paid } 
+Type:       { $payby } # { $payinfo }
+
+{
+  if ( $balance > 0 ) {
+    $OUT .= "Your current balance is now \$$balance.\n\n";
+  } elsif ( $balance < 0 ) {
+    $OUT .= 'You have a credit balance of $'. sprintf("%.2f",0-$balance).
+              ".\n".
+            "Future charges will be deducted from this balance before billing ".
+              "you again.\n\n";
+            
+  }
+}
+Thank you for your business.
+