find svc_pbx service correctly and cope anyway if it isn't there, RT#8712
[freeside.git] / FS / FS / Maestro.pm
1 package FS::Maestro;
2
3 use Date::Format;
4 use FS::Conf;
5 use FS::Record qw( qsearchs );
6 use FS::cust_main;
7
8 sub customer_status {
9   my( $custnum ) = shift; #@_;
10
11   my $cust_main = qsearchs( 'cust_main' => { 'custnum' => $custnum } )
12    or return { 'status' => 'E',
13                'error'  => "$custnum not found" };
14
15   my @cust_pkg = $cust_main->cust_pkg;
16
17   my @cust_svc = map $_->cust_svc, @cust_pkg;
18
19   ###
20   # find $svc_pbx
21   ##
22
23   my @cust_svc_pbx =
24     grep { my($n,$l,$t) = $_->label; $t eq 'svc_pbx' }
25     @cust_svc;
26
27   #i tried sofa king hard to explain to them why passing a custnum instead
28   #of a conference id was a bad idea, but i can't make them understand...
29   if ( ! @cust_svc_pbx ) {
30     return { 'status' => 'E',
31              'error'  => "customer $custnum has no conference service" };
32   } elsif ( scalar(@cust_svc_pbx) > 1 ) {
33     return { 'status' => 'E',
34              'error'  => "customer $custnum has more than one conference service; there should be a way to specify which one you want",
35            }; #maybe list them...  and work with a pkgnum
36   }
37
38   my $cust_svc_pbx = $cust_svc_pbx[0];
39
40   my $svc_pbx = $cust_svc_pbx->svc_x;
41
42   ###
43   # find "outbound service" y/n
44   ###
45
46   my $conf = new FS::Conf;
47   my %outbound_pkgs = map { $_=>1 } $conf->config('mc-outbound_packages');
48   my $outbound_service =
49     scalar( grep { $outbound_pkgs{ $_->pkgpart }
50                      && !$_->get('cancel')
51                  }
52                  @cust_pkg
53           )
54     ? 1 : 0;
55
56   ###
57   # find "good till" date/time stamp
58   ###
59
60   my @active_cust_pkg =
61     sort { $a->bill <=> $b->bill }
62     grep { !$_->get('cancel') && $_->part_pkg->freq ne '0' }
63     @cust_pkg;
64   my $good_till =time2str('%c', $active_cust_pkg[0]->bill || time );
65
66   ###
67   # return the info
68   ###
69
70   { 
71     'status' => substr($cust_main->ucfirst_status,0,1), #what they asked for..
72     'name'   => $cust_main->name,
73     'email'  => $cust_main->invoicing_list_emailonly_scalar,
74     'max_lines'        => $svc_pbx ? $svc_pbx->max_extensions : '',
75     'max_simultaneous' => $svc_pbx ? $svc_pbx->max_simultaneous : '',
76     'outbound_service' => $outbound_service,
77     'good_till' => $good_till,
78     'products'  => [ map $_->pkgpart, grep !$_->get('cancel'), @cust_pkg ],
79   };
80
81 }
82
83 1;