fix 'Can't call method "setup" on an undefined value' error when using into rates...
[freeside.git] / FS / FS / Maestro.pm
1 package FS::Maestro;
2
3 use strict;
4 use Date::Format;
5 use FS::Conf;
6 use FS::Record qw( qsearchs );
7 use FS::cust_main;
8 use FS::cust_pkg;
9 use FS::part_svc;
10
11 #i guess this is kind of deprecated in favor of service_status, but keeping it
12 #around until they say they don't need it.
13 sub customer_status {
14   my( $custnum ) = shift; #@_;
15   my $svcnum = @_ ? shift : '';
16
17   my $curuser = $FS::CurrentUser::CurrentUser;
18
19   my $cust_main = qsearchs({
20     'table'     => 'cust_main',
21     'hashref'   => { 'custnum' => $custnum },
22     'extra_sql' => ' AND '. $curuser->agentnums_sql,
23   })
24     or return { 'status' => 'E',
25                 'error'  => "custnum $custnum not found" };
26
27   return service_status($svcnum) if $svcnum;
28
29   ###
30   # regular customer to maestro (single package)
31   ###
32
33   my %result = ();
34
35   my @cust_pkg = $cust_main->cust_pkg;
36
37   #things specific to the non-reseller scenario
38
39   $result{'status'} = substr($cust_main->ucfirst_status,0,1);
40
41   $result{'products'} =
42     [ map $_->pkgpart, grep !$_->get('cancel'), @cust_pkg ];
43
44   #find svc_pbx
45
46   my @cust_svc = map $_->cust_svc, @cust_pkg;
47
48   my @cust_svc_pbx =
49     grep { my($n,$l,$t) = $_->label; $t eq 'svc_pbx' }
50     @cust_svc;
51
52   if ( ! @cust_svc_pbx ) {
53     return { 'status' => 'E',
54              'error'  => "customer $custnum has no conference service" };
55   } elsif ( scalar(@cust_svc_pbx) > 1 ) {
56     return { 'status' => 'E',
57              'error'  =>
58                "customer $custnum has more than one conference".
59                " service (reseller?); specify a svcnum as a second argument",
60            };
61   }
62
63   my $cust_svc_pbx = $cust_svc_pbx[0];
64
65   my $svc_pbx = $cust_svc_pbx->svc_x;
66
67   # find "outbound service" y/n
68
69   my $conf = new FS::Conf;
70   my %outbound_pkgs = map { $_=>1 } $conf->config('mc-outbound_packages');
71   $result{'outbound_service'} =
72     scalar( grep { $outbound_pkgs{ $_->pkgpart }
73                      && !$_->get('cancel')
74                  }
75                  @cust_pkg
76           )
77     ? 1 : 0;
78
79   # find "good till" date/time stamp
80
81   my @active_cust_pkg =
82     sort { $a->bill <=> $b->bill }
83     grep { !$_->get('cancel') && $_->part_pkg->freq ne '0' }
84     @cust_pkg;
85   $result{'good_till'} = time2str('%c', $active_cust_pkg[0]->bill || time );
86
87   return { 
88     'name'    => $cust_main->name,
89     'email'   => $cust_main->invoicing_list_emailonly_scalar,
90     #'agentnum' => $cust_main->agentnum,
91     #'agent'    => $cust_main->agent->agent,
92     'max_lines'        => $svc_pbx ? $svc_pbx->max_extensions : '',
93     'max_simultaneous' => $svc_pbx ? $svc_pbx->max_simultaneous : '',
94     %result,
95   };
96
97 }
98
99 sub service_status {
100   my $svcnum = shift;
101
102   my $svc_pbx = qsearchs({
103     'table'      => 'svc_pbx',
104     'addl_from'  => ' LEFT JOIN cust_svc USING ( svcnum ) '.
105                     ' LEFT JOIN cust_pkg USING ( pkgnum ) ',
106     'hashref'   => { 'svcnum' => $svcnum },
107     #'extra_sql' => " AND custnum = $custnum",
108   })
109     or return { 'status' => 'E',
110                 'error'  => "svcnum $svcnum not found" };
111
112   my $cust_pkg = $svc_pbx->cust_svc->cust_pkg;
113   my $cust_main = $cust_pkg->cust_main;
114
115   my %result = ();
116
117   #status in the reseller scenario
118   $result{'status'} = substr($cust_pkg->ucfirst_status,0,1);
119   $result{'status'} = 'A' if $result{'status'} eq 'N';
120
121   # find "outbound service" y/n
122   my @cust_pkg = $cust_main->cust_pkg;
123   #XXX what about outbound service per-reseller ?
124   my $conf = new FS::Conf;
125   my %outbound_pkgs = map { $_=>1 } $conf->config('mc-outbound_packages');
126   $result{'outbound_service'} =
127     scalar( grep { $outbound_pkgs{ $_->pkgpart }
128                      && !$_->get('cancel')
129                  }
130                  @cust_pkg
131           )
132     ? 1 : 0;
133
134   # find "good till" date/time stamp (this package)
135   $result{'good_till'} = time2str('%c', $cust_pkg->bill || time );
136
137   return { 
138     'custnum' => $cust_main->custnum,
139     'name'    => ( $svc_pbx->title || $cust_main->name ),
140     'email'   => $cust_main->invoicing_list_emailonly_scalar,
141     #'agentnum' => $cust_main->agentnum,
142     #'agent'    => $cust_main->agent->agent,
143     'max_lines'        => $svc_pbx->max_extensions,
144     'max_simultaneous' => $svc_pbx->max_simultaneous,
145     %result,
146   };
147
148 }
149
150 #some false laziness w/ MyAccount order_pkg
151 sub order_pkg {
152   my $opt = ref($_[0]) ? shift : { @_ };
153
154   $opt->{'title'} = delete $opt->{'name'}
155     if !exists($opt->{'title'}) && exists($opt->{'name'});
156
157   my $custnum = $opt->{'custnum'};
158
159   my $curuser = $FS::CurrentUser::CurrentUser;
160
161   my $cust_main = qsearchs({
162     'table'     => 'cust_main',
163     'hashref'   => { 'custnum' => $custnum },
164     'extra_sql' => ' AND '. $curuser->agentnums_sql,
165   })
166     or return { 'error'  => "custnum $custnum not found" };
167
168   my $status = $cust_main->status;
169   #false laziness w/ClientAPI/Signup.pm
170
171   my $cust_pkg = new FS::cust_pkg ( {
172     'custnum' => $custnum,
173     'pkgpart' => $opt->{'pkgpart'},
174   } );
175   my $error = $cust_pkg->check;
176   return { 'error' => $error } if $error;
177
178   my @svc = ();
179   unless ( $opt->{'svcpart'} eq 'none' ) {
180
181     my $svcpart = '';
182     if ( $opt->{'svcpart'} =~ /^(\d+)$/ ) {
183       $svcpart = $1;
184     } else {
185       $svcpart = $cust_pkg->part_pkg->svcpart; #($svcdb);
186     }
187
188     my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
189     return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
190
191     my $svcdb = $part_svc->svcdb;
192
193     my %fields = (
194       'svc_acct'     => [ qw( username domsvc _password sec_phrase popnum ) ],
195       'svc_domain'   => [ qw( domain ) ],
196       'svc_phone'    => [ qw( phonenum pin sip_password phone_name ) ],
197       'svc_external' => [ qw( id title ) ],
198       'svc_pbx'      => [ qw( id title ) ],
199     );
200   
201     my $svc_x = "FS::$svcdb"->new( {
202       'svcpart'   => $svcpart,
203       map { $_ => $opt->{$_} } @{$fields{$svcdb}}
204     } );
205     
206     #snarf processing not necessary here (or probably at all, anymore)
207     
208     my $y = $svc_x->setdefault; # arguably should be in new method
209     return { 'error' => $y } if $y && !ref($y);
210   
211     $error = $svc_x->check;
212     return { 'error' => $error } if $error;
213
214     push @svc, $svc_x;
215
216   }
217
218   use Tie::RefHash;
219   tie my %hash, 'Tie::RefHash';
220   %hash = ( $cust_pkg => \@svc );
221   #msgcat
222   $error = $cust_main->order_pkgs( \%hash, 'noexport' => 1 );
223   return { 'error' => $error } if $error;
224
225 # currently they're using this in the reseller scenario, so don't
226 # bill the package immediately
227 #  my $conf = new FS::Conf;
228 #  if ( $conf->exists('signup_server-realtime') ) {
229 #
230 #    my $bill_error = _do_bop_realtime( $cust_main, $status );
231 #
232 #    if ($bill_error) {
233 #      $cust_pkg->cancel('quiet'=>1);
234 #      return $bill_error;
235 #    } else {
236 #      $cust_pkg->reexport;
237 #    }
238 #
239 #  } else {
240     $cust_pkg->reexport;
241 #  }
242
243   my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
244
245   return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };
246
247 }
248
249 1;