1 package FS::part_export::ikano;
5 use vars qw(@ISA %info %loopType $me);
7 use Date::Format qw( time2str );
8 use Date::Parse qw( str2time );
9 use FS::Record qw(qsearch qsearchs dbh);
14 @ISA = qw(FS::part_export);
15 $me= '[' . __PACKAGE__ . ']';
17 tie my %options, 'Tie::IxHash',
18 'keyid' => { label=>'Ikano keyid' },
19 'username' => { label=>'Ikano username',
22 'password' => { label=>'Ikano password' },
23 'check_networks' => { label => 'Check Networks',
24 default => 'ATT,BELLCA',
26 'debug' => { label => 'Debug Mode', type => 'checkbox' },
31 'desc' => 'Provision DSL to Ikano',
32 'options' => \%options,
34 Requires installation of
35 <a href="http://search.cpan.org/dist/Net-Ikano">Net::Ikano</a> from CPAN.
39 %loopType = ( '' => 'Line-share', '0' => 'Standalone' );
41 sub rebless { shift; }
43 sub external_pkg_map { 1; }
46 # we distinguish between invalid new data (return error) versus data that
47 # has legitimately changed (may eventually execute hooks; now just update)
48 # if we do add hooks later, we should work on a copy of svc_dsl and pass
49 # the old and new svc_dsl to the hooks so they know what changed
51 # current assumptions of what won't change (from their side):
52 # vendor_order_id, vendor_qual_id, vendor_order_type, pushed, monitored,
53 # last_pull, address (from qual), contact info, ProductCustomId
54 my($self, $svc_dsl, $threshold) = (shift, shift, shift);
56 my $result = $self->valid_order($svc_dsl,'pull');
57 return $result unless $result eq '';
60 if($now - $svc_dsl->last_pull < $threshold) {
61 warn "$me skipping pull since threshold not reached (svcnum="
62 . $svc_dsl->svcnum . ",now=$now,threshold=$threshold,last_pull="
63 . $svc_dsl->last_pull .")" if $self->option('debug');
67 $result = $self->ikano_command('ORDERSTATUS',
68 { OrderId => $svc_dsl->vendor_order_id } );
69 return $result unless ref($result); # scalar (string) is an error
71 # now we're getting an OrderResponse which should have one Order in it
72 warn "$me pull OrderResponse hash:\n".Dumper($result)
73 if $self->option('debug');
75 return 'Invalid order response' unless defined $result->{'Order'};
76 $result = $result->{'Order'};
78 return 'No order id or status returned'
79 unless defined $result->{'Status'} && defined $result->{'OrderId'};
81 local $SIG{HUP} = 'IGNORE';
82 local $SIG{INT} = 'IGNORE';
83 local $SIG{QUIT} = 'IGNORE';
84 local $SIG{TERM} = 'IGNORE';
85 local $SIG{TSTP} = 'IGNORE';
86 local $SIG{PIPE} = 'IGNORE';
88 my $oldAutoCommit = $FS::UID::AutoCommit;
89 local $FS::UID::AutoCommit = 0;
93 my $order_status = grep($_ eq $result->{'Status'}, @Net::Ikano::orderStatus)
94 ? $result->{'Status'} : '';
95 return 'Invalid new status' if $order_status eq '';
96 $svc_dsl->vendor_order_status($order_status)
97 if($svc_dsl->vendor_order_status ne $order_status);
98 $svc_dsl->monitored('')
99 if ($order_status eq 'CANCELLED' || $order_status eq 'COMPLETED');
101 # 2. fields we don't care much about
102 my %justUpdate = ( 'first' => 'FirstName',
103 'last' => 'LastName',
104 'company' => 'CompanyName',
105 'username' => 'Username',
106 'password' => 'Password' );
109 while (($fsf, $ikanof) = each %justUpdate) {
110 $svc_dsl->$fsf($result->{$ikanof})
111 if $result->{$ikanof} ne $svc_dsl->$fsf;
114 # let's look inside the <Product> response element
115 my @product = $result->{'Product'};
116 return 'Invalid number of products on order' if scalar(@product) != 1;
117 my $product = $result->{'Product'}[0];
120 if($svc_dsl->loop_type eq '') { # line-share
121 # TN may change only if sub changes it and New or Change order in Completed status
122 my $tn = $product->{'PhoneNumber'};
123 if($tn ne $svc_dsl->phonenum) {
124 if( ($svc_dsl->vendor_order_type eq 'NEW'
125 || $svc_dsl->vendor_order_type eq 'CHANGE')
126 && $svc_dsl->vendor_order_status eq 'COMPLETED' ) {
127 $svc_dsl->phonenum($tn);
129 else { return 'TN has changed in an invalid state'; }
132 elsif($svc_dsl->loop_type eq '0') { # dry loop
133 # TN may change only if it's assigned while a New or Change order is in progress
134 return 'Invalid PhoneNumber value for a dry loop'
135 if $product->{'PhoneNumber'} ne 'STANDALONE';
136 my $tn = $product->{'VirtualPhoneNumber'};
137 if($tn ne $svc_dsl->phonenum) {
138 if( ($svc_dsl->vendor_order_type eq 'NEW'
139 || $svc_dsl->vendor_order_type eq 'CHANGE')
140 && $svc_dsl->vendor_order_status ne 'COMPLETED'
141 && $svc_dsl->vendor_order_status ne 'CANCELLED') {
142 $svc_dsl->phonenum($tn);
144 else { return 'TN has changed in an invalid state'; }
148 # 4. desired_due_date - may change if manually changed
149 if($svc_dsl->vendor_order_type eq 'NEW'
150 || $svc_dsl->vendor_order_type eq 'CHANGE'){
151 my $f = str2time($product->{'DateToOrder'});
152 return 'Invalid DateToOrder' unless $f;
153 $svc_dsl->desired_due_date($f) if $svc_dsl->desired_due_date ne $f;
154 # XXX: optionally sync back to start_date or whatever...
156 elsif($svc_dsl->vendor_order_type eq 'CANCEL'){
157 my $f = str2time($product->{'DateToDisconnect'});
158 return 'Invalid DateToDisconnect' unless $f;
159 $svc_dsl->desired_due_date($f) if $svc_dsl->desired_due_date ne $f;
160 # XXX: optionally sync back to expire or whatever...
164 if($svc_dsl->vendor_order_type eq 'NEW'
165 || $svc_dsl->vendor_order_type eq 'CHANGE') {
166 my $f = str2time($product->{'ActivationDate'});
167 if($svc_dsl->vendor_order_status ne 'NEW'
168 && $svc_dsl->vendor_order_status ne 'CANCELLED') {
169 return 'Invalid ActivationDate' unless $f;
170 $svc_dsl->due_date($f) if $svc_dsl->due_date ne $f;
173 # Ikano API does not implement the proper disconnect date,
174 # so we can't do anything about it
176 # 6. staticips - for now just comma-separate them
177 my $tstatics = $result->{'StaticIps'};
178 my @istatics = defined $tstatics ? @$tstatics : ();
179 my $ostatics = $svc_dsl->staticips;
180 my @ostatics = split(',',$ostatics);
181 # more horrible search/sync code below...
182 my $staticsChanged = 0;
183 foreach my $istatic ( @istatics ) { # they have, we don't
184 unless ( grep($_ eq $istatic, @ostatics) ) {
185 push @ostatics, $istatic;
189 for(my $i=0; $i < scalar(@ostatics); $i++) {
190 unless ( grep($_ eq $ostatics[$i], @istatics) ) {
191 splice(@ostatics,$i,1);
196 $svc_dsl->staticips(join(',',@ostatics)) if $staticsChanged;
198 # 7. notes - put them into the common format and compare
199 my $tnotes = $result->{'OrderNotes'};
200 my @tnotes = defined $tnotes ? @$tnotes : ();
201 my @inotes = (); # all Ikano OrderNotes as FS::dsl_note objects
202 my $notesChanged = 0;
203 foreach my $tnote ( @tnotes ) {
204 my $inote = $self->ikano2fsnote($tnote,$svc_dsl->svcnum);
205 return 'Cannot parse note' unless ref($inote);
206 push @inotes, $inote;
208 my @onotes = $svc_dsl->notes;
209 # assume notes we already have don't change & no notes added from our side
210 # so using the horrible code below just find what we're missing and add it
212 foreach my $inote ( @inotes ) {
214 foreach my $onote ( @onotes ) {
215 if($onote->date == $inote->date && $onote->note eq $inote->note) {
220 $error = $inote->insert unless ( $found );
222 $dbh->rollback if $oldAutoCommit;
223 return "Cannot add note: $error";
227 $svc_dsl->last_pull((time));
228 local $FS::svc_Common::noexport_hack = 1;
229 $error = $svc_dsl->replace;
231 $dbh->rollback if $oldAutoCommit;
232 return "Cannot update DSL data: $error";
235 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
241 my($self,$n,$svcnum) = (shift,shift,shift);
242 my @ikanoRequired = qw( HighPriority StaffId Date Text CompanyStaffId );
243 return '' unless defined $n->{'HighPriority'}
244 && defined $n->{'StaffId'}
245 && defined $n->{'CompanyStaffId'}
246 && defined $n->{'Date'}
247 && defined $n->{'Text'}
250 $by = "Ikano" if $n->{'CompanyStaffId'} == -1 && $n->{'StaffId'} != -1;
251 $by = "Us" if $n->{'StaffId'} == -1 && $n->{'CompanyStaffId'} != -1;
256 'priority' => $n->{'HighPriority'} eq 'false' ? 'N' : 'H',
257 '_date' => int(str2time($n->{'Date'})),
258 'note' => $n->{'Text'},
263 my($self,$qual) = (shift,shift);
264 # address always required for Ikano qual, TN optional (assume dry if not given)
266 my %location_hash = $qual->location;
267 return 'No address provided' unless %location_hash;
268 my $svctn = $qual->phonenum;
270 my $result = $self->ikano_command('PREQUAL',
271 { AddressLine1 => $location_hash{address1},
272 AddressUnitType => $location_hash{location_type},
273 AddressUnitValue => $location_hash{location_number},
274 AddressCity => $location_hash{city},
275 AddressState => $location_hash{state},
276 ZipCode => $location_hash{zip},
277 Country => $location_hash{country},
278 LocationType => $location_hash{location_kind},
279 PhoneNumber => length($svctn) > 1 ? $svctn : "STANDALONE",
280 RequestClientIP => '127.0.0.1',
281 CheckNetworks => $self->option('check_networks'),
283 return $result unless ref($result); # error case
284 return 'Invalid prequal response' unless defined $result->{'PrequalId'};
287 # lame data structure traversal...
288 # don't spend much time here, just get TermsId and ProductCustomId
289 my $networks = $result->{'Network'};
290 my @networks = defined $networks ? @$networks : ();
292 foreach my $network ( @networks ) {
293 my $productgroups = $network->{'ProductGroup'};
294 my @productgroups = defined $productgroups ? @$productgroups : ();
296 foreach my $productgroup ( @productgroups ) {
297 my $prefix = "ikano_Network_$netcount"."_ProductGroup_$pgcount"."_";
298 $qoptions->{$prefix."TermsId"} = $productgroup->{'TermsId'};
299 my $products = $productgroup->{'Product'};
300 my @products = defined $products ? @$products : ();
302 foreach my $product ( @products ) {
303 $qoptions->{$prefix."Product_$prodcount"."_ProductCustomId"} = $product->{'ProductCustomId'};
311 { 'vendor_qual_id' => $result->{'PrequalId'},
312 'status' => scalar(@networks) ? 'Q' : 'D',
313 'options' => $qoptions,
318 my($self,$qual) = (shift,shift);
320 my %qual_options = $qual->options;
321 my @externalids = ();
322 my( $optionname, $optionvalue );
323 while (($optionname, $optionvalue) = each %qual_options) {
324 push @externalids, $optionvalue
325 if ( $optionname =~ /^ikano_Network_(\d+)_ProductGroup_(\d+)_Product_(\d+)_ProductCustomId$/
326 && $optionvalue ne '' );
329 # XXX: eventually perhaps this should return both the packages a link to
330 # order each package and go to the svc prov with the prequal id filled in
331 # but only if cust, not prospect!
332 my $list = "<B>Qualifying Packages:</B><UL>";
333 my @part_pkgs = qsearch( 'part_pkg', { 'disabled' => '' } );
334 foreach my $part_pkg ( @part_pkgs ) {
335 my $externalid = $part_pkg->option('externalid',1);
337 $list .= "<LI>".$part_pkg->pkgpart.": ".$part_pkg->pkg." - "
338 .$part_pkg->comment."</LI>"
339 if grep( $_ eq $externalid, @externalids );
347 my($self,$svc_dsl) = (shift,shift);
348 my $conf = new FS::Conf;
349 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
350 my @notes = $svc_dsl->notes;
351 my $html = '<TABLE border="1" cellspacing="2" cellpadding="2" id="dsl_notes">
352 <TR><TH>Date</TH><TH>By</TH><TH>Priority</TH><TH>Note</TH></TR>';
353 foreach my $note ( @notes ) {
355 <TD>".time2str("$date_format %H:%M",$note->date)."</TD>
356 <TD>".$note->by."</TD>
357 <TD>". ($note->priority eq 'N' ? 'Normal' : 'High') ."</TD>
358 <TD>".$note->note."</TD></TR>";
364 sub loop_type_long { # sub, not a method
365 my($svc_dsl) = (shift);
366 return $loopType{$svc_dsl->loop_type};
370 my( $self, $command, $args ) = @_;
374 my $ikano = Net::Ikano->new(
375 'keyid' => $self->option('keyid'),
376 'username' => $self->option('username'),
377 'password' => $self->option('password'),
378 'debug' => $self->option('debug'),
381 $ikano->$command($args);
385 eval "use Net::Ikano;";
390 my( $self, $svc_dsl, $action ) = (shift, shift, shift);
394 warn "$me valid_order action=$action svc_dsl:\n". Dumper($svc_dsl)
395 if $self->option('debug');
397 # common to all order types/status/loop_type
398 my $error = !($svc_dsl->desired_due_date
399 && grep($_ eq $svc_dsl->vendor_order_type, Net::Ikano->orderTypes)
402 && defined $svc_dsl->loop_type
403 && $svc_dsl->vendor_qual_id
405 return 'Missing or invalid order data' if $error;
407 return 'Package does not have an external id configured'
408 if $svc_dsl->cust_svc->cust_pkg->part_pkg->options('externalid',1) eq '';
410 return 'No valid qualification for this order'
411 unless qsearch( 'qual', { 'vendor_qual_id' => $svc_dsl->vendor_qual_id });
413 # now go by order type
414 # weird ifs & long lines for readability and ease of understanding - don't change
415 if($svc_dsl->vendor_order_type eq 'NEW') {
416 if($svc_dsl->pushed) {
417 $error = !( ($action eq 'pull' || $action eq 'statuschg'
418 || $action eq 'delete')
419 && length($svc_dsl->vendor_order_id) > 0
420 && length($svc_dsl->vendor_order_status) > 0
422 return 'Invalid order data' if $error;
424 return 'Phone number required for status change'
425 if ($action eq 'statuschg' && length($svc_dsl->phonenum) < 1);
427 else { # unpushed New order - cannot do anything other than push it
428 $error = !($action eq 'insert'
429 && length($svc_dsl->vendor_order_id) < 1
430 && length($svc_dsl->vendor_order_status) < 1
431 && ( ($svc_dsl->phonenum eq '' && $svc_dsl->loop_type eq '0') # dry
432 || ($svc_dsl->phonenum ne '' && $svc_dsl->loop_type eq '') # line-share
435 return 'Invalid order data' if $error;
438 elsif($svc_dsl->vendor_order_type eq 'CANCEL') {
440 elsif($svc_dsl->vendor_order_type eq 'CHANGE') {
447 my ($self,$vendor_qual_id,$ProductCustomId) = (shift,shift,shift);
448 my $qual = qsearchs( 'qual', { 'vendor_qual_id' => $vendor_qual_id });
449 return '' unless $qual;
450 my %qual_options = $qual->options;
451 my( $optionname, $optionvalue );
452 while (($optionname, $optionvalue) = each %qual_options) {
453 if ( $optionname =~ /^ikano_Network_(\d+)_ProductGroup_(\d+)_Product_(\d+)_ProductCustomId$/
454 && $optionvalue eq $ProductCustomId ) {
456 my $productgroup = $2;
457 return $qual->option("ikano_Network_".$network."_ProductGroup_".$productgroup."_TermsId");
464 my( $self, $svc_dsl ) = (shift, shift);
468 my $result = $self->valid_order($svc_dsl,'insert');
469 return $result unless $result eq '';
471 my $isp_chg = $svc_dsl->isp_chg eq 'Y' ? 'YES' : 'NO';
472 my $contactTN = $svc_dsl->cust_svc->cust_pkg->cust_main->daytime;
473 $contactTN =~ s/[^0-9]//g;
475 my $ProductCustomId = $svc_dsl->cust_svc->cust_pkg->part_pkg->option('externalid',1);
479 ProductCustomId => $ProductCustomId,
480 TermsId => $self->qual2termsid($svc_dsl->vendor_qual_id,$ProductCustomId),
481 DSLPhoneNumber => $svc_dsl->loop_type eq '0' ? 'STANDALONE'
482 : $svc_dsl->phonenum,
483 Password => $svc_dsl->password,
484 PrequalId => $svc_dsl->vendor_qual_id,
485 CompanyName => $svc_dsl->company,
486 FirstName => $svc_dsl->first,
487 LastName => $svc_dsl->last,
489 ContactMethod => 'PHONE',
490 ContactPhoneNumber => $contactTN,
491 ContactEmail => 'x@x.xx',
493 DateToOrder => time2str("%Y-%m-%d",$svc_dsl->desired_due_date),
494 RequestClientIP => '127.0.0.1',
495 IspChange => $isp_chg,
496 IspPrevious => $isp_chg eq 'YES' ? $svc_dsl->isp_prev : '',
497 CurrentProvider => $isp_chg eq 'NO' ? $svc_dsl->isp_prev : '',
500 $result = $self->ikano_command('ORDER',$args);
501 return $result unless ref($result); # scalar (string) is an error
503 # now we're getting an OrderResponse which should have one Order in it
504 warn "$me _export_insert OrderResponse hash:\n".Dumper($result)
505 if $self->option('debug');
507 return 'Invalid order response' unless defined $result->{'Order'};
508 $result = $result->{'Order'};
510 return 'No/invalid order id or status returned'
511 unless defined $result->{'Status'} && defined $result->{'OrderId'}
512 && grep($_ eq $result->{'Status'}, @Net::Ikano::orderStatus);
514 $svc_dsl->pushed(time);
515 $svc_dsl->last_pull((time)+1);
516 $svc_dsl->vendor_order_id($result->{'OrderId'});
517 $svc_dsl->vendor_order_status($result->{'Status'});
518 $svc_dsl->username($result->{'Username'});
519 local $FS::svc_Common::noexport_hack = 1;
520 $result = $svc_dsl->replace;
521 return "Error setting DSL fields: $result" if $result;
525 sub _export_replace {
526 my( $self, $new, $old ) = (shift, shift, shift);
527 # XXX only supports password changes now, but should return error if
528 # another change is attempted?
530 if($new->password ne $old->password) {
531 my $result = $self->valid_order($new,'statuschg');
532 return $result unless $result eq '';
534 $result = $self->ikano_command('PASSWORDCHANGE',
535 { DSLPhoneNumber => $new->phonenum,
536 NewPassword => $new->password,
538 return $result unless ref($result); # scalar (string) is an error
540 return 'Error changing password' unless defined $result->{'Password'}
541 && $result->{'Password'} eq $new->password;
548 my( $self, $svc_dsl ) = (shift, shift);
550 my $result = $self->valid_order($svc_dsl,'delete');
551 return $result unless $result eq '';
553 # for now allow an immediate cancel only on New orders in New/Pending status
554 #XXX: add support for Chance and Cancel orders in New/Pending status later
556 if($svc_dsl->vendor_order_type eq 'NEW') {
557 if($svc_dsl->vendor_order_status eq 'NEW'
558 || $svc_dsl->vendor_order_status eq 'PENDING') {
559 my $result = $self->ikano_command('CANCEL',
560 { OrderId => $svc_dsl->vendor_order_id, } );
561 return $result unless ref($result); # scalar (string) is an error
562 return 'Unable to cancel order' unless $result->{'Order'};
563 $result = $result->{'Order'};
564 return 'Invalid cancellation response'
565 unless $result->{'Status'} eq 'CANCELLED'
566 && $result->{'OrderId'} eq $svc_dsl->vendor_order_id;
568 # we're supposed to do a pull, but it will break everything, so don't
569 # this is very wrong...
572 return "Cannot cancel a NEW order unless it's in NEW or PENDING status";
576 return 'Canceling orders other than NEW orders is not currently implemented';
583 my( $self, $svc_dsl, $type ) = (shift, shift, shift);
585 my $result = $self->valid_order($svc_dsl,'statuschg');
586 return $result unless $result eq '';
588 # get the DSLServiceId
589 $result = $self->ikano_command('CUSTOMERLOOKUP',
590 { PhoneNumber => $svc_dsl->phonenum } );
591 return $result unless ref($result); # scalar (string) is an error
592 return 'No DSLServiceId found' unless defined $result->{'DSLServiceId'};
593 my $DSLServiceId = $result->{'DSLServiceId'};
595 $result = $self->ikano_command('ACCOUNTSTATUSCHANGE',
596 { DSLPhoneNumber => $svc_dsl->phonenum,
597 DSLServiceId => $DSLServiceId,
600 return $result unless ref($result); # scalar (string) is an error
604 sub _export_suspend {
605 my( $self, $svc_dsl ) = (shift, shift);
606 $self->statuschg($svc_dsl,"SUSPEND");
609 sub _export_unsuspend {
610 my( $self, $svc_dsl ) = (shift, shift);
611 $self->statuschg($svc_dsl,"UNSUSPEND");