fix "Customer has automatic payment information" condition when migrated from v3
[freeside.git] / FS / FS / part_event / Condition / has_cust_payby_auto.pm
1 package FS::part_event::Condition::has_cust_payby_auto;
2 use base qw( FS::part_event::Condition );
3
4 use strict;
5 use Tie::IxHash;
6 use FS::payby;
7 use FS::Record qw(qsearch);
8
9 sub description {
10   'Customer has automatic payment information';
11 }
12
13 tie my %payby, 'Tie::IxHash', FS::payby->cust_payby2shortname;
14 delete $payby{'DCRD'};
15 delete $payby{'DCHK'};
16
17 sub option_fields {
18   (
19     'payby' => { 
20                  label         => 'Has automatic payment info',
21                  type          => 'select',
22                  options       => [ keys %payby ],
23                  option_labels => \%payby,
24                },
25   );
26 }
27
28 sub condition {
29   my( $self, $object ) = @_;
30
31   my $cust_main = $self->cust_main($object);
32
33   #handle multiple (HASH) type options migrated from a v3 payby.pm condition
34   # (and maybe we should be a select-multiple or checkbox-multiple too?)
35   my @payby = ();
36   my $payby = $self->option('payby');
37   if ( ref($payby) ) {
38     @payby = keys %$payby;
39   } elsif ( $payby ) {
40     @payby = ( $payby );
41   }
42
43   scalar( qsearch({ 
44     'table'     => 'cust_payby',
45     'hashref'   => { 'custnum' => $cust_main->custnum,
46                      #'payby'   => $self->option('payby')
47                    },
48     'extra_sql' => 'AND payby IN ( '.
49                      join(',', map dbh->quote($_), @payby).
50                    ' ) ',
51     'order_by'  => 'LIMIT 1',
52   }) );
53
54 }
55
56 1;