6 use FS::Record qw( qsearchs );
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.
14 my( $custnum ) = shift; #@_;
15 my $svcnum = @_ ? shift : '';
17 my $curuser = $FS::CurrentUser::CurrentUser;
19 my $cust_main = qsearchs({
20 'table' => 'cust_main',
21 'hashref' => { 'custnum' => $custnum },
22 'extra_sql' => ' AND '. $curuser->agentnums_sql,
24 or return { 'status' => 'E',
25 'error' => "custnum $custnum not found" };
27 return service_status($svcnum) if $svcnum;
30 # regular customer to maestro (single package)
35 my @cust_pkg = $cust_main->cust_pkg;
37 #things specific to the non-reseller scenario
39 $result{'status'} = substr($cust_main->ucfirst_status,0,1);
42 [ map $_->pkgpart, grep !$_->get('cancel'), @cust_pkg ];
46 my @cust_svc = map $_->cust_svc, @cust_pkg;
49 grep { my($n,$l,$t) = $_->label; $t eq 'svc_pbx' }
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',
58 "customer $custnum has more than one conference".
59 " service (reseller?); specify a svcnum as a second argument",
63 my $cust_svc_pbx = $cust_svc_pbx[0];
65 my $svc_pbx = $cust_svc_pbx->svc_x;
67 # find "outbound service" y/n
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 }
79 # find "good till" date/time stamp
82 sort { $a->bill <=> $b->bill }
83 grep { !$_->get('cancel') && $_->part_pkg->freq ne '0' }
85 $result{'good_till'} = time2str('%c', $active_cust_pkg[0]->bill || time );
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 : '',
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",
109 or return { 'status' => 'E',
110 'error' => "svcnum $svcnum not found" };
112 my $cust_pkg = $svc_pbx->cust_svc->cust_pkg;
113 my $cust_main = $cust_pkg->cust_main;
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';
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')
134 # find "good till" date/time stamp (this package)
135 $result{'good_till'} = time2str('%c', $cust_pkg->bill || time );
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,
150 #some false laziness w/ MyAccount order_pkg
152 my $opt = ref($_[0]) ? shift : { @_ };
154 $opt->{'title'} = delete $opt->{'name'}
155 if !exists($opt->{'title'}) && exists($opt->{'name'});
157 my $custnum = $opt->{'custnum'};
159 my $curuser = $FS::CurrentUser::CurrentUser;
161 my $cust_main = qsearchs({
162 'table' => 'cust_main',
163 'hashref' => { 'custnum' => $custnum },
164 'extra_sql' => ' AND '. $curuser->agentnums_sql,
166 or return { 'error' => "custnum $custnum not found" };
168 my $status = $cust_main->status;
169 #false laziness w/ClientAPI/Signup.pm
171 my $cust_pkg = new FS::cust_pkg ( {
172 'custnum' => $custnum,
173 'pkgpart' => $opt->{'pkgpart'},
175 my $error = $cust_pkg->check;
176 return { 'error' => $error } if $error;
179 unless ( $opt->{'svcpart'} eq 'none' ) {
182 if ( $opt->{'svcpart'} =~ /^(\d+)$/ ) {
185 $svcpart = $cust_pkg->part_pkg->svcpart; #($svcdb);
188 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
189 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
191 my $svcdb = $part_svc->svcdb;
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 ) ],
201 my $svc_x = "FS::$svcdb"->new( {
202 'svcpart' => $svcpart,
203 map { $_ => $opt->{$_} } @{$fields{$svcdb}}
206 #snarf processing not necessary here (or probably at all, anymore)
208 my $y = $svc_x->setdefault; # arguably should be in new method
209 return { 'error' => $y } if $y && !ref($y);
211 $error = $svc_x->check;
212 return { 'error' => $error } if $error;
219 tie my %hash, 'Tie::RefHash';
220 %hash = ( $cust_pkg => \@svc );
222 $error = $cust_main->order_pkgs( \%hash, 'noexport' => 1 );
223 return { 'error' => $error } if $error;
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') ) {
230 # my $bill_error = _do_bop_realtime( $cust_main, $status );
233 # $cust_pkg->cancel('quiet'=>1);
234 # return $bill_error;
236 # $cust_pkg->reexport;
243 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
245 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };