+sub send_receipt {
+ my $self = shift;
+ my $opt = ref($_[0]) ? shift : { @_ };
+
+ my $cust_bill = $opt->{'cust_bill'};
+ my $cust_main = $opt->{'cust_main'} || $self->cust_main;
+
+ my $conf = new FS::Conf;
+
+ return '' unless $conf->config_bool('payment_receipt', $cust_main->agentnum);
+
+ my @invoicing_list = $cust_main->invoicing_list_emailonly;
+ return '' unless @invoicing_list;
+
+ $cust_bill ||= ($cust_main->cust_bill)[-1]; #rather inefficient though?
+
+ my $error = '';
+
+ if ( ( exists($opt->{'manual'}) && $opt->{'manual'} )
+ #|| ! $conf->exists('invoice_html_statement')
+ || ! $cust_bill
+ )
+ {
+ my $msgnum = $conf->config('payment_receipt_msgnum', $cust_main->agentnum);
+ if ( $msgnum ) {
+
+ my %substitutions = ();
+ $substitutions{invnum} = $opt->{cust_bill}->invnum if $opt->{cust_bill};
+
+ my $queue = new FS::queue {
+ 'job' => 'FS::Misc::process_send_email',
+ 'paynum' => $self->paynum,
+ 'custnum' => $cust_main->custnum,
+ };
+ $error = $queue->insert(
+ FS::msg_template->by_key($msgnum)->prepare(
+ 'cust_main' => $cust_main,
+ 'object' => $self,
+ 'from_config' => 'payment_receipt_from',
+ 'substitutions' => \%substitutions,
+ ),
+ 'msgtype' => 'receipt', # override msg_template's default
+ );
+
+ } elsif ( $conf->exists('payment_receipt_email') ) {
+
+ 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 $payby = $self->payby;
+ my $payinfo = $self->payinfo;
+ $payby =~ s/^BILL$/Check/ if $payinfo;
+ if ( $payby eq 'CARD' || $payby eq 'CHEK' ) {
+ $payinfo = $self->paymask
+ } else {
+ $payinfo = $self->decrypt($payinfo);
+ }
+ $payby =~ s/^CHEK$/Electronic check/;
+
+ my %fill_in = (
+ 'date' => time2str("%a %B %o, %Y", $self->_date),
+ 'name' => $cust_main->name,
+ 'paynum' => $self->paynum,
+ 'paid' => sprintf("%.2f", $self->paid),
+ 'payby' => ucfirst(lc($payby)),
+ 'payinfo' => $payinfo,
+ 'balance' => $cust_main->balance,
+ 'company_name' => $conf->config('company_name', $cust_main->agentnum),
+ );
+
+ $fill_in{'invnum'} = $opt->{cust_bill}->invnum if $opt->{cust_bill};
+
+ if ( $opt->{'cust_pkg'} ) {
+ $fill_in{'pkg'} = $opt->{'cust_pkg'}->part_pkg->pkg;
+ #setup date, other things?
+ }
+
+ my $queue = new FS::queue {
+ 'job' => 'FS::Misc::process_send_generated_email',
+ 'paynum' => $self->paynum,
+ 'custnum' => $cust_main->custnum,
+ 'msgtype' => 'receipt',
+ };
+ $error = $queue->insert(
+ 'from' => $conf->config('invoice_from', $cust_main->agentnum),
+ #invoice_from??? well as good as any
+ 'to' => \@invoicing_list,
+ 'subject' => 'Payment receipt',
+ 'body' => [ $receipt_template->fill_in( HASH => \%fill_in ) ],
+ );