This commit was manufactured by cvs2svn to create branch
[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   my $svcnum = @_ ? shift : '';
11
12   my $curuser = $FS::CurrentUser::CurrentUser;
13
14   my $cust_main = qsearchs({
15     'table'     => 'cust_main',
16     'hashref'   => { 'custnum' => $custnum },
17     'extra_sql' => ' AND '. $curuser->agentnums_sql,
18   })
19     or return { 'status' => 'E',
20                 'error'  => "custnum $custnum not found" };
21
22   my( $svc_pbx, $good_till, $outbound_service ) = ( '', '', '' );
23   my %result = ();
24   if ( $svcnum ) {
25    
26     ###
27     # reseller scenario to maestro (customer w/ multiple packages)
28     ###
29
30     # find $svc_pbx
31
32     $svc_pbx = qsearchs({
33       'table'      => 'svc_pbx',
34       'addl_from'  => ' LEFT JOIN cust_svc USING ( svcnum ) '.
35                       ' LEFT JOIN cust_pkg USING ( pkgnum ) ',
36       'hashref'   => { 'svcnum' => $svcnum },
37       'extra_sql' => " AND custnum = $custnum",
38     })
39       or return { 'status' => 'E',
40                   'error'  => "svcnum $svcnum not found" };
41
42     #status in the reseller scenario
43
44     my $cust_pkg = $svc_pbx->cust_svc->cust_pkg;
45
46     $result{'status'} = substr($cust_pkg->ucfirst_status,0,1);
47
48     # find "outbound service" y/n
49
50     #XXX outbound service per-reseller ?
51     #my @cust_pkg = $cust_main->cust_pkg;
52     #
53     #my $conf = new FS::Conf;
54     #my %outbound_pkgs = map { $_=>1 } $conf->config('mc-outbound_packages');
55     #my $outbound_service =
56     #  scalar( grep { $outbound_pkgs{ $_->pkgpart }
57     #                   && !$_->get('cancel')
58     #               }
59     #               @cust_pkg
60     #        )
61     #  ? 1 : 0;
62
63     # find "good till" date/time stamp (this package)
64
65     $good_till  = time2str('%c', $cust_pkg->bill || time );
66
67   } else {
68
69     ###
70     # regular customer to maestro (single package)
71     ###
72
73     my @cust_pkg = $cust_main->cust_pkg;
74
75     #things specific to the non-reseller scenario
76
77     $result{'status'} = substr($cust_main->ucfirst_status,0,1);
78
79     $result{'products'} =
80       [ map $_->pkgpart, grep !$_->get('cancel'), @cust_pkg ];
81
82     #find svc_pbx
83
84     my @cust_svc = map $_->cust_svc, @cust_pkg;
85
86     my @cust_svc_pbx =
87       grep { my($n,$l,$t) = $_->label; $t eq 'svc_pbx' }
88       @cust_svc;
89
90     if ( ! @cust_svc_pbx ) {
91       return { 'status' => 'E',
92                'error'  => "customer $custnum has no conference service" };
93     } elsif ( scalar(@cust_svc_pbx) > 1 ) {
94       return { 'status' => 'E',
95                'error'  =>
96                  "customer $custnum has more than one conference".
97                  " service (reseller?); specify a svcnum as a second argument",
98              };
99     }
100
101     my $cust_svc_pbx = $cust_svc_pbx[0];
102
103     $svc_pbx = $cust_svc_pbx->svc_x;
104
105     # find "outbound service" y/n
106
107     my $conf = new FS::Conf;
108     my %outbound_pkgs = map { $_=>1 } $conf->config('mc-outbound_packages');
109     $outbound_service =
110       scalar( grep { $outbound_pkgs{ $_->pkgpart }
111                        && !$_->get('cancel')
112                    }
113                    @cust_pkg
114             )
115       ? 1 : 0;
116
117     # find "good till" date/time stamp
118
119     my @active_cust_pkg =
120       sort { $a->bill <=> $b->bill }
121       grep { !$_->get('cancel') && $_->part_pkg->freq ne '0' }
122       @cust_pkg;
123     $good_till = time2str('%c', $active_cust_pkg[0]->bill || time );
124
125   }
126
127   return { 
128     'name'   => $cust_main->name,
129     'email'  => $cust_main->invoicing_list_emailonly_scalar,
130     'max_lines'        => $svc_pbx ? $svc_pbx->max_extensions : '',
131     'max_simultaneous' => $svc_pbx ? $svc_pbx->max_simultaneous : '',
132     'outbound_service' => $outbound_service,
133     'good_till' => $good_till,
134     %result,
135   };
136
137 }
138
139 1;