dangling cust_credit_refund not allowed
[freeside.git] / FS / FS / Misc.pm
index 92780f7..e2143cf 100644 (file)
@@ -4,11 +4,20 @@ use strict;
 use vars qw ( @ISA @EXPORT_OK $DEBUG );
 use Exporter;
 use Carp;
+use FS::Record qw(dbh qsearch);
+use FS::cust_credit_refund;
+#use FS::cust_credit_bill;
+#use FS::cust_bill_pay;
+#use FS::cust_pay_refund;
+use Data::Dumper;
 
 @ISA = qw( Exporter );
-@EXPORT_OK = qw( send_email send_fax );
+@EXPORT_OK = qw( send_email send_fax
+                 states_hash counties state_label
+                 card_types prune_applications
+               );
 
-$DEBUG = 1;
+$DEBUG = 0;
 
 =head1 NAME
 
@@ -69,6 +78,12 @@ FS::UID->install_callback( sub {
 
 sub send_email {
   my(%options) = @_;
+  if ( $DEBUG ) {
+    my %doptions = %options;
+    $doptions{'body'} = '(full body not shown in debug)';
+    warn "FS::Misc::send_email called with options:\n  ". Dumper(\%doptions);
+#         join("\n", map { "  $_: ". $options{$_} } keys %options ). "\n"
+  }
 
   $ENV{MAILADDRESS} = $options{'from'};
   my $to = ref($options{to}) ? join(', ', @{ $options{to} } ) : $options{to};
@@ -118,8 +133,15 @@ sub send_email {
 
   }
 
-  $options{'from'} =~ /\@([\w\.\-]+)/ or $1 = 'example.com';
-  my $message_id = join('.', rand()*(2**32), $$, time). "\@$1";
+  my $domain;
+  if ( $options{'from'} =~ /\@([\w\.\-]+)/ ) {
+    $domain = $1;
+  } else {
+    warn 'no domain found in invoice from address '. $options{'from'}.
+         '; constructing Message-ID @example.com'; 
+    $domain = 'example.com';
+  }
+  my $message_id = join('.', rand()*(2**32), $$, time). "\@$domain";
 
   my $message = MIME::Entity->build(
     'From'       => $options{'from'},
@@ -171,6 +193,80 @@ sub send_email {
 
 }
 
+#this kludges a "mysmtpsend" method into Mail::Internet for send_email above
+package Mail::Internet;
+
+use Mail::Address;
+use Net::SMTP;
+
+sub Mail::Internet::mysmtpsend {
+    my $src  = shift;
+    my %opt = @_;
+    my $host = $opt{Host};
+    my $envelope = $opt{MailFrom};
+    my $noquit = 0;
+    my $smtp;
+    my @hello = defined $opt{Hello} ? (Hello => $opt{Hello}) : ();
+
+    push(@hello, 'Port', $opt{'Port'})
+       if exists $opt{'Port'};
+
+    push(@hello, 'Debug', $opt{'Debug'})
+       if exists $opt{'Debug'};
+
+    if(ref($host) && UNIVERSAL::isa($host,'Net::SMTP')) {
+       $smtp = $host;
+       $noquit = 1;
+    }
+    else {
+       #local $SIG{__DIE__};
+       #$smtp = eval { Net::SMTP->new($host, @hello) };
+       $smtp = new Net::SMTP $host, @hello;
+    }
+
+    unless ( defined($smtp) ) {
+      my $err = $!;
+      $err =~ s/Invalid argument/Unknown host/;
+      return "can't connect to $host: $err"
+    }
+
+    my $hdr = $src->head->dup;
+
+    _prephdr($hdr);
+
+    # Who is it to
+
+    my @rcpt = map { ref($_) ? @$_ : $_ } grep { defined } @opt{'To','Cc','Bcc'};
+    @rcpt = map { $hdr->get($_) } qw(To Cc Bcc)
+       unless @rcpt;
+    my @addr = map($_->address, Mail::Address->parse(@rcpt));
+
+    return 'No valid destination addresses found!'
+       unless(@addr);
+
+    $hdr->delete('Bcc'); # Remove blind Cc's
+
+    # Send it
+
+    #warn "Headers: \n" . join('',@{$hdr->header});
+    #warn "Body: \n" . join('',@{$src->body});
+
+    my $ok = $smtp->mail( $envelope ) &&
+               $smtp->to(@addr) &&
+               $smtp->data(join("", @{$hdr->header},"\n",@{$src->body}));
+
+    if ( $ok ) {
+      $smtp->quit
+          unless $noquit;
+      return '';
+    } else {
+      return $smtp->code. ' '. $smtp->message;
+    }
+
+}
+package FS::Misc;
+#eokludge
+
 =item send_fax OPTION => VALUE ...
 
 Options:
@@ -245,86 +341,233 @@ sub send_fax {
 
   if ($faxjob->success) {
     warn "Successfully queued fax to '$options{dialstring}' with jobid " .
-            $faxjob->jobid;
+           $faxjob->jobid
+      if $DEBUG;
+    return '';
   } else {
     return 'Error while sending FAX: ' . $faxjob->trace;
   }
 
-  return '';
+}
+
+=item states_hash COUNTRY
 
+Returns a list of key/value pairs containing state (or other sub-country
+division) abbriviations and names.
+
+=cut
+
+use FS::Record qw(qsearch);
+use Locale::SubCountry;
+
+sub states_hash {
+  my($country) = @_;
+
+  my @states = 
+#     sort
+     map { s/[\n\r]//g; $_; }
+     map { $_->state; }
+         qsearch({ 
+                   'select'    => 'state',
+                   'table'     => 'cust_main_county',
+                   'hashref'   => { 'country' => $country },
+                   'extra_sql' => 'GROUP BY state',
+                });
+
+  #it could throw a fatal "Invalid country code" error (for example "AX")
+  my $subcountry = eval { new Locale::SubCountry($country) }
+    or return ( '', '(n/a)' );
+
+  #"i see your schwartz is as big as mine!"
+  map  { ( $_->[0] => $_->[1] ) }
+  sort { $a->[1] cmp $b->[1] }
+  map  { [ $_ => state_label($_, $subcountry) ] }
+       @states;
 }
 
-package Mail::Internet;
+=item counties STATE COUNTRY
 
-use Mail::Address;
-use Net::SMTP;
+Returns a list of counties for this state and country.
 
-sub Mail::Internet::mysmtpsend {
-    my $src  = shift;
-    my %opt = @_;
-    my $host = $opt{Host};
-    my $envelope = $opt{MailFrom};
-    my $noquit = 0;
-    my $smtp;
-    my @hello = defined $opt{Hello} ? (Hello => $opt{Hello}) : ();
+=cut
 
-    push(@hello, 'Port', $opt{'Port'})
-       if exists $opt{'Port'};
+sub counties {
+  my( $state, $country ) = @_;
+
+  sort map { s/[\n\r]//g; $_; }
+       map { $_->county }
+           qsearch({
+             'select'  => 'DISTINCT county',
+             'table'   => 'cust_main_county',
+             'hashref' => { 'state'   => $state,
+                            'country' => $country,
+                          },
+           });
+}
 
-    push(@hello, 'Debug', $opt{'Debug'})
-       if exists $opt{'Debug'};
+=item state_label STATE COUNTRY_OR_LOCALE_SUBCOUNRY_OBJECT
 
-    if(ref($host) && UNIVERSAL::isa($host,'Net::SMTP')) {
-       $smtp = $host;
-       $noquit = 1;
-    }
-    else {
-       #local $SIG{__DIE__};
-       #$smtp = eval { Net::SMTP->new($host, @hello) };
-       $smtp = new Net::SMTP $host, @hello;
-    }
+=cut
 
-    unless ( defined($smtp) ) {
-      my $err = $!;
-      $err =~ s/Invalid argument/Unknown host/;
-      return "can't connect to $host: $err"
-    }
+sub state_label {
+  my( $state, $country ) = @_;
 
-    my $hdr = $src->head->dup;
+  unless ( ref($country) ) {
+    $country = eval { new Locale::SubCountry($country) }
+      or return'(n/a)';
 
-    _prephdr($hdr);
+  }
 
-    # Who is it to
+  # US kludge to avoid changing existing behaviour 
+  # also we actually *use* the abbriviations...
+  my $full_name = $country->country_code eq 'US'
+                    ? ''
+                    : $country->full_name($state);
 
-    my @rcpt = map { ref($_) ? @$_ : $_ } grep { defined } @opt{'To','Cc','Bcc'};
-    @rcpt = map { $hdr->get($_) } qw(To Cc Bcc)
-       unless @rcpt;
-    my @addr = map($_->address, Mail::Address->parse(@rcpt));
+  $full_name = '' if $full_name eq 'unknown';
+  $full_name =~ s/\(see also.*\)\s*$//;
+  $full_name .= " ($state)" if $full_name;
 
-    return 'No valid destination addresses found!'
-       unless(@addr);
+  $full_name || $state || '(n/a)';
 
-    $hdr->delete('Bcc'); # Remove blind Cc's
+}
 
-    # Send it
+=item card_types
 
-    #warn "Headers: \n" . join('',@{$hdr->header});
-    #warn "Body: \n" . join('',@{$src->body});
+Returns a hash reference of the accepted credit card types.  Keys are shorter
+identifiers and values are the longer strings used by the system (see
+L<Business::CreditCard>).
 
-    my $ok = $smtp->mail( $envelope ) &&
-               $smtp->to(@addr) &&
-               $smtp->data(join("", @{$hdr->header},"\n",@{$src->body}));
+=cut
 
-    if ( $ok ) {
-      $smtp->quit
-          unless $noquit;
-      return '';
-    } else {
-      return $smtp->code. ' '. $smtp->message;
-    }
+#$conf from above
+
+sub card_types {
+  my $conf = new FS::Conf;
+
+  my %card_types = (
+    #displayname                    #value (Business::CreditCard)
+    "VISA"                       => "VISA card",
+    "MasterCard"                 => "MasterCard",
+    "Discover"                   => "Discover card",
+    "American Express"           => "American Express card",
+    "Diner's Club/Carte Blanche" => "Diner's Club/Carte Blanche",
+    "enRoute"                    => "enRoute",
+    "JCB"                        => "JCB",
+    "BankCard"                   => "BankCard",
+    "Switch"                     => "Switch",
+    "Solo"                       => "Solo",
+  );
+  my @conf_card_types = grep { ! /^\s*$/ } $conf->config('card-types');
+  if ( @conf_card_types ) {
+    #perhaps the hash is backwards for this, but this way works better for
+    #usage in selfservice
+    %card_types = map  { $_ => $card_types{$_} }
+                  grep {
+                         my $d = $_;
+                          grep { $card_types{$d} eq $_ } @conf_card_types
+                       }
+                   keys %card_types;
+  }
 
+  \%card_types;
 }
-package FS::Misc;
+
+=item prune_applications OPTION_HASH
+
+Removes applications of credits to refunds in the event that the database
+is corrupt and either the credits or refunds are missing (see
+L<FS::cust_credit>, L<FS::cust_refund>, and L<FS::cust_credit_refund>).
+If the OPTION_HASH contains the element 'dry_run' then a report of
+affected records is returned rather than actually deleting the records.
+
+=cut
+
+sub prune_applications {
+  my $options = shift;
+  my $dbh = dbh
+
+  local $DEBUG = 1 if exists($options->{debug});
+  my $ccr = <<EOW;
+    WHERE
+         0 = (select count(*) from cust_credit
+               where cust_credit_refund.crednum = cust_credit.crednum)
+      or 
+         0 = (select count(*) from cust_refund
+               where cust_credit_refund.refundnum = cust_refund.refundnum)
+EOW
+  my $ccb = <<EOW;
+    WHERE
+         0 = (select count(*) from cust_credit
+               where cust_credit_bill.crednum = cust_credit.crednum)
+      or 
+         0 = (select count(*) from cust_bill
+               where cust_credit_bill.invnum = cust_bill.invnum)
+EOW
+  my $cbp = <<EOW;
+    WHERE
+         0 = (select count(*) from cust_bill
+               where cust_bill_pay.invnum = cust_bill.invnum)
+      or 
+         0 = (select count(*) from cust_pay
+               where cust_bill_pay.paynum = cust_pay.paynum)
+EOW
+  my $cpr = <<EOW;
+    WHERE
+         0 = (select count(*) from cust_pay
+               where cust_pay_refund.paynum = cust_pay.paynum)
+      or 
+         0 = (select count(*) from cust_refund
+               where cust_pay_refund.refundnum = cust_refund.refundnum)
+EOW
+
+  my %strays = (
+    'cust_credit_refund' => { clause => $ccr,
+                              link1  => 'crednum',
+                              link2  => 'refundnum',
+                            },
+#    'cust_credit_bill'   => { clause => $ccb,
+#                              link1  => 'crednum',
+#                              link2  => 'refundnum',
+#                            },
+#    'cust_bill_pay'      => { clause => $cbp,
+#                              link1  => 'crednum',
+#                              link2  => 'refundnum',
+#                            },
+#    'cust_pay_refund'    => { clause => $cpr,
+#                              link1  => 'crednum',
+#                              link2  => 'refundnum',
+#                            },
+  );
+
+  if ( exists($options->{dry_run}) ) {
+    my @response = ();
+    foreach my $table (keys %strays) {
+      my $clause = $strays{$table}->{clause};
+      my $link1  = $strays{$table}->{link1};
+      my $link2  = $strays{$table}->{link2};
+      my @rec = qsearch($table, {}, '', $clause);
+      my $keyname = $rec[0]->primary_key if $rec[0];
+      foreach (@rec) {
+        push @response, "$table " .$_->$keyname . " claims attachment to ".
+               "$link1 " . $_->$link1 . " and $link2 " . $_->$link2 . "\n";
+      }
+    }
+    return (@response);
+  } else {
+    foreach (keys %strays) {
+      my $statement = "DELETE FROM $_ " . $strays{$_}->{clause};
+      warn $statement if $DEBUG;
+      my $sth = $dbh->prepare($statement)
+        or die $dbh->errstr;
+      $sth->execute
+        or die $sth->errstr;
+    }
+    return ();
+  }
+}
+
+=back
 
 =head1 BUGS