agent-virtualize credit card surcharge percentage, RT#72961
[freeside.git] / FS / FS / svc_Tower_Mixin.pm
1 package FS::svc_Tower_Mixin;
2
3 use strict;
4
5 =item tower_sector
6
7 =item tower_sector_sql HASHREF
8
9 Class method which returns a list of WHERE clause fragments to 
10 search for services with tower/sector given by HASHREF.  Can 
11 contain 'towernum' and 'sectornum' keys, either of which can be 
12 an arrayref or a single value.  To use this, the search needs to
13 join to tower_sector.
14
15 towernum or sectornum can also contain 'none' to allow null values.
16
17 =cut
18
19 sub tower_sector_sql {
20   my( $class, $params ) = @_;
21   return () unless keys %$params;
22
23   my @where = ();
24   for my $field (qw(towernum sectornum)) {
25     my $value = $params->{$field} or next;
26     if ( ref $value and grep { $_ } @$value ) {
27       my $in = join(',', map { /^(\d+)$/ ? $1 : () } @$value);
28       my @orwhere;
29       push @orwhere, "tower_sector.$field IN ($in)" if $in;
30       if ( grep /^none$/, @$value ) {
31         # then allow this field to be null
32         push @orwhere, "tower_sector.$field IS NULL";
33         # and if this field is the sector, also allow the default sector
34         # on the tower
35         push @orwhere, "sectorname = '_default'" if $field eq 'sectornum';
36       }
37       push @where, '( '.join(' OR ', @orwhere).' )';
38     }
39     elsif ( $value =~ /^(\d+)$/ ) {
40       push @where, "tower_sector.$field = $1";
41     }
42     elsif ( $value eq 'none' ) {
43       push @where, "tower_sector.$field IS NULL";
44     }
45   }
46   @where;
47 }
48
49 1;