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);
120 # find "outbound service" y/n
121 my @cust_pkg = $cust_main->cust_pkg;
122 #XXX what about outbound service per-reseller ?
123 my $conf = new FS::Conf;
124 my %outbound_pkgs = map { $_=>1 } $conf->config('mc-outbound_packages');
125 $result{'outbound_service'} =
126 scalar( grep { $outbound_pkgs{ $_->pkgpart }
127 && !$_->get('cancel')
133 # find "good till" date/time stamp (this package)
134 $result{'good_till'} = time2str('%c', $cust_pkg->bill || time );
137 'custnum' => $cust_main->custnum,
138 'name' => $cust_main->name,
139 'email' => $cust_main->invoicing_list_emailonly_scalar,
140 #'agentnum' => $cust_main->agentnum,
141 #'agent' => $cust_main->agent->agent,
142 'max_lines' => $svc_pbx->max_extensions,
143 'max_simultaneous' => $svc_pbx->max_simultaneous,
149 #some false laziness w/ MyAccount order_pkg
151 my $opt = ref($_[0]) ? shift : { @_ };
153 $opt->{'title'} = delete $opt->{'name'}
154 if !exists($opt->{'title'}) && exists($opt->{'name'});
156 my $custnum = $opt->{'custnum'};
158 my $curuser = $FS::CurrentUser::CurrentUser;
160 my $cust_main = qsearchs({
161 'table' => 'cust_main',
162 'hashref' => { 'custnum' => $custnum },
163 'extra_sql' => ' AND '. $curuser->agentnums_sql,
165 or return { 'error' => "custnum $custnum not found" };
167 my $status = $cust_main->status;
168 #false laziness w/ClientAPI/Signup.pm
170 my $cust_pkg = new FS::cust_pkg ( {
171 'custnum' => $custnum,
172 'pkgpart' => $opt->{'pkgpart'},
174 my $error = $cust_pkg->check;
175 return { 'error' => $error } if $error;
178 unless ( $opt->{'svcpart'} eq 'none' ) {
181 if ( $opt->{'svcpart'} =~ /^(\d+)$/ ) {
184 $svcpart = $cust_pkg->part_pkg->svcpart; #($svcdb);
187 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
188 return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
190 my $svcdb = $part_svc->svcdb;
193 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
194 'svc_domain' => [ qw( domain ) ],
195 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
196 'svc_external' => [ qw( id title ) ],
197 'svc_pbx' => [ qw( id title ) ],
200 my $svc_x = "FS::$svcdb"->new( {
201 'svcpart' => $svcpart,
202 map { $_ => $opt->{$_} } @{$fields{$svcdb}}
205 #snarf processing not necessary here (or probably at all, anymore)
207 my $y = $svc_x->setdefault; # arguably should be in new method
208 return { 'error' => $y } if $y && !ref($y);
210 $error = $svc_x->check;
211 return { 'error' => $error } if $error;
218 tie my %hash, 'Tie::RefHash';
219 %hash = ( $cust_pkg => \@svc );
221 $error = $cust_main->order_pkgs( \%hash, 'noexport' => 1 );
222 return { 'error' => $error } if $error;
224 # currently they're using this in the reseller scenario, so don't
225 # bill the package immediately
226 # my $conf = new FS::Conf;
227 # if ( $conf->exists('signup_server-realtime') ) {
229 # my $bill_error = _do_bop_realtime( $cust_main, $status );
232 # $cust_pkg->cancel('quiet'=>1);
233 # return $bill_error;
235 # $cust_pkg->reexport;
242 my $svcnum = $svc[0] ? $svc[0]->svcnum : '';
244 return { error=>'', pkgnum=>$cust_pkg->pkgnum, svcnum=>$svcnum };