X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2FMisc.pm;h=a535ecebf5b15bc59cfbb2d3741d7c0d26a05fe8;hp=2e383d549274db5150de269a76a4c653324b0e0d;hb=9c90d9596b2fce8744d0851443444b3a4f846297;hpb=673b9a458d9138523026963df6fa3b4683e09bae diff --git a/FS/FS/Misc.pm b/FS/FS/Misc.pm index 2e383d549..a535ecebf 100644 --- a/FS/FS/Misc.pm +++ b/FS/FS/Misc.pm @@ -7,7 +7,10 @@ use Carp; 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 + ); $DEBUG = 0; @@ -185,6 +188,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: @@ -268,77 +345,128 @@ sub send_fax { } -package Mail::Internet; +=item states_hash COUNTRY -use Mail::Address; -use Net::SMTP; +Returns a list of key/value pairs containing state (or other sub-country +division) abbriviations and names. -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'}; +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; +} - push(@hello, 'Debug', $opt{'Debug'}) - if exists $opt{'Debug'}; +=item counties STATE COUNTRY - 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; - } +Returns a list of counties for this state and country. - unless ( defined($smtp) ) { - my $err = $!; - $err =~ s/Invalid argument/Unknown host/; - return "can't connect to $host: $err" - } +=cut - my $hdr = $src->head->dup; +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, + }, + }); +} - _prephdr($hdr); +=item state_label STATE COUNTRY_OR_LOCALE_SUBCOUNRY_OBJECT - # Who is it to +=cut - 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)); +sub state_label { + my( $state, $country ) = @_; - return 'No valid destination addresses found!' - unless(@addr); + unless ( ref($country) ) { + $country = eval { new Locale::SubCountry($country) } + or return'(n/a)'; - $hdr->delete('Bcc'); # Remove blind Cc's + } - # Send it + # 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); - #warn "Headers: \n" . join('',@{$hdr->header}); - #warn "Body: \n" . join('',@{$src->body}); + $full_name = '' if $full_name eq 'unknown'; + $full_name =~ s/\(see also.*\)\s*$//; + $full_name .= " ($state)" if $full_name; - my $ok = $smtp->mail( $envelope ) && - $smtp->to(@addr) && - $smtp->data(join("", @{$hdr->header},"\n",@{$src->body})); + $full_name || $state || '(n/a)'; - if ( $ok ) { - $smtp->quit - unless $noquit; - return ''; - } else { - return $smtp->code. ' '. $smtp->message; - } +} +=item card_types + +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). + +=cut + +#$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; =back