+sub _banned_pay_hashref {
+ my $self = shift;
+
+ my %payby2ban = (
+ 'CARD' => 'CARD',
+ 'DCRD' => 'CARD',
+ 'CHEK' => 'CHEK',
+ 'DCHK' => 'CHEK'
+ );
+
+ {
+ 'payby' => $payby2ban{$self->payby},
+ 'payinfo' => $self->payinfo,
+ #don't ever *search* on reason! #'reason' =>
+ };
+}
+
+sub _new_banned_pay_hashref {
+ my $self = shift;
+ my $hr = $self->_banned_pay_hashref;
+ $hr->{payinfo_hash} = 'SHA512';
+ $hr->{payinfo} = sha512_base64($hr->{payinfo});
+ $hr;
+}
+
+=item paydate_mon_year
+
+Returns a two element list consisting of the paydate month and year.
+
+=cut
+
+sub paydate_mon_year {
+ my $self = shift;
+
+ my $date = $self->paydate; # || '12-2037';
+
+ #false laziness w/elements/select-month_year.html
+ if ( $date =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #PostgreSQL date format
+ ( $2, $1 );
+ } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
+ ( $1, $3 );
+ } else {
+ warn "unrecognized expiration date format: $date";
+ ( '', '' );
+ }
+
+}
+
+=item realtime_bop
+
+=cut
+
+sub realtime_bop {
+ my( $self, %opt ) = @_;
+
+ $opt{$_} = $self->$_() for qw( payinfo payname paydate );
+
+ if ( $self->locationnum ) {
+ my $cust_location = $self->cust_location;
+ $opt{$_} = $cust_location->$_() for qw( address1 address2 city state zip );
+ }
+
+ $self->cust_main->realtime_bop({
+ 'method' => FS::payby->payby2bop( $self->payby ),
+ %opt,
+ });
+
+}
+
+=item paytypes
+
+Returns a list of valid values for the paytype field (bank account type for
+electronic check payment).
+
+=cut
+
+sub paytypes {
+ #my $class = shift;
+
+ ('', 'Personal checking', 'Personal savings', 'Business checking', 'Business savings');
+}
+
+=item cgi_cust_payby_fields
+
+Returns the field names used in the web interface (including some pseudo-fields).
+
+=cut
+
+sub cgi_cust_payby_fields {
+ #my $class = shift;
+ [qw( payby payinfo paydate_month paydate_year paycvv payname weight
+ payinfo1 payinfo2 payinfo3 paytype paystate payname_CHEK )];
+}
+
+=item cgi_hash_callback HASHREF
+
+Subroutine (not a class or object method). Processes a hash reference
+of web interface contet (transfers the data from pseudo-fields to real fields).
+
+=cut
+
+sub cgi_hash_callback {
+ my $hashref = shift;
+
+ my %noauto = (
+ 'CARD' => 'DCRD',
+ 'CHEK' => 'DCHK',
+ );
+ $hashref->{payby} = $noauto{$hashref->{payby}}
+ if ! $hashref->{weight} && exists $noauto{$hashref->{payby}};
+
+ if ( $hashref->{payby} =~ /^(CHEK|DCHK)$/ ) {
+
+ unless ( grep $hashref->{$_}, qw(payinfo1 payinfo2 payinfo3 payname_CHEK)) {
+ %$hashref = ();
+ return;
+ }
+
+ $hashref->{payinfo} = $hashref->{payinfo1}. '@';
+ $hashref->{payinfo} .= $hashref->{payinfo3}.'.'
+ if $conf->config('echeck-country') eq 'CA';
+ $hashref->{payinfo} .= $hashref->{'payinfo2'};
+
+ $hashref->{payname} = $hashref->{'payname_CHEK'};
+
+ } elsif ( $hashref->{payby} =~ /^(CARD|DCRD)$/ ) {
+
+ unless ( grep $hashref->{$_}, qw( payinfo paycvv payname ) ) {
+ %$hashref = ();
+ return;
+ }
+
+ }
+
+ $hashref->{paydate}= $hashref->{paydate_month}. '-'. $hashref->{paydate_year};
+
+}
+
+=item search_sql
+
+Class method.
+
+Returns a qsearch hash expression to search for parameters specified in HASHREF.
+Valid paramters are:
+
+=over 4
+
+=item payby
+
+listref
+
+=item paydate_year
+
+=item paydate_month
+
+
+=back
+
+=cut
+
+sub search_sql {
+ my ($class, $params) = @_;
+
+ my @where = ();
+ my $orderby;
+
+ # initialize these to prevent warnings
+ $params = {
+ 'paydate_year' => '',
+ %$params
+ };
+
+ ###
+ # payby
+ ###
+
+ if ( $params->{'payby'} ) {
+
+ my @payby = ref( $params->{'payby'} )
+ ? @{ $params->{'payby'} }
+ : ( $params->{'payby'} );
+
+ @payby = grep /^([A-Z]{4})$/, @payby;
+ my $in_payby = 'IN(' . join(',', map {"'$_'"} @payby) . ')';
+ push @where, "cust_payby.payby $in_payby"
+ if @payby;
+ }
+
+ ###
+ # paydate_year / paydate_month
+ ###
+
+ if ( $params->{'paydate_year'} =~ /^(\d{4})$/ ) {
+ my $year = $1;
+ $params->{'paydate_month'} =~ /^(\d\d?)$/
+ or die "paydate_year without paydate_month?";
+ my $month = $1;
+
+ push @where,
+ 'cust_payby.paydate IS NOT NULL',
+ "cust_payby.paydate != ''",
+ "CAST(cust_payby.paydate AS timestamp) < CAST('$year-$month-01' AS timestamp )"
+;
+ }
+ ##
+ # setup queries, subs, etc. for the search
+ ##
+
+ $orderby ||= 'ORDER BY custnum';
+
+ # here is the agent virtualization
+ push @where,
+ $FS::CurrentUser::CurrentUser->agentnums_sql(table => 'cust_main');
+
+ my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
+
+ my $addl_from = ' LEFT JOIN cust_main USING ( custnum ) ';
+ # always make address fields available in results
+ for my $pre ('bill_', 'ship_') {
+ $addl_from .=
+ ' LEFT JOIN cust_location AS '.$pre.'location '.
+ 'ON (cust_main.'.$pre.'locationnum = '.$pre.'location.locationnum) ';
+ }
+
+ my $count_query = "SELECT COUNT(*) FROM cust_payby $addl_from $extra_sql";
+
+ my @select = ( 'cust_payby.*',
+ #'cust_main.custnum',
+ # there's a good chance that we'll need these
+ 'cust_main.bill_locationnum',
+ 'cust_main.ship_locationnum',
+ FS::UI::Web::cust_sql_fields($params->{'cust_fields'}),
+ );
+
+ my $select = join(', ', @select);
+
+ my $sql_query = {
+ 'table' => 'cust_payby',
+ 'select' => $select,
+ 'addl_from' => $addl_from,
+ 'hashref' => {},
+ 'extra_sql' => $extra_sql,
+ 'order_by' => $orderby,
+ 'count_query' => $count_query,
+ };
+ $sql_query;
+
+}
+