X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_export%2Fikano.pm;h=23917bf9ea7cd951b143da09822ffd07afd4c58e;hb=e92c3b71a6182bdca0831ca4755ae0127e7e5180;hp=444e9b8781a28c67924eac1f9caf13a3c38e1162;hpb=4e1e6d1a69d6c1f08c24859f7ed70b4489b691bf;p=freeside.git diff --git a/FS/FS/part_export/ikano.pm b/FS/FS/part_export/ikano.pm index 444e9b878..23917bf9e 100644 --- a/FS/FS/part_export/ikano.pm +++ b/FS/FS/part_export/ikano.pm @@ -1,5 +1,7 @@ package FS::part_export::ikano; +use strict; +use warnings; use vars qw(@ISA %info %loopType $me); use Tie::IxHash; use Date::Format qw( time2str ); @@ -22,12 +24,14 @@ tie my %options, 'Tie::IxHash', default => 'ATT,BELLCA', }, 'debug' => { label => 'Debug Mode', type => 'checkbox' }, + 'import' => { label => 'Import Mode', type => 'checkbox' }, ; %info = ( 'svc' => 'svc_dsl', 'desc' => 'Provision DSL to Ikano', 'options' => \%options, + 'no_machine' => 1, 'notes' => <<'END' Requires installation of Net::Ikano from CPAN. @@ -38,6 +42,40 @@ END sub rebless { shift; } +sub external_pkg_map { 1; } + +sub location_types { + ( + '' => '(None)', + 'APT' => 'Apartment', + 'BLDG' => 'Building', + 'FLR' => 'Floor', + 'LOT' => 'Lot', + 'RM' => 'Room', + 'SLIP' => 'Slip', + 'SUIT' => 'Suite', + 'TRLR' => 'Trailer', + 'UNIT' => 'Unit', + 'WING' => 'Wing', + ); +} + +sub location_types_parse { + my $class = shift; + my %t = $class->location_types; + delete $t{''}; + ( + (map { $_ => $_ } keys %t), + (reverse %t), + 'STE' => 'SUIT', #USPS + ); +} + +sub import_mode { + my $self = shift; + $self->option('import'); +} + sub dsl_pull { # we distinguish between invalid new data (return error) versus data that # has legitimately changed (may eventually execute hooks; now just update) @@ -47,13 +85,30 @@ sub dsl_pull { # current assumptions of what won't change (from their side): # vendor_order_id, vendor_qual_id, vendor_order_type, pushed, monitored, # last_pull, address (from qual), contact info, ProductCustomId - my($self, $svc_dsl) = (shift, shift); - $self->loadmod; - my $result = $self->valid_order($svc_dsl,'pull'); - return $result unless $result eq ''; + my($self, $svc_dsl, $threshold, $import) = (shift, shift, shift, shift); + + return 'Invalid operation - Import Mode is enabled' + if $self->import_mode && !$import; + + return 'invalid arguments' if $import && !$self->import_mode; + + warn "$me dsl_pull: import mode" if $self->option('debug'); + + unless ( $import ) { + my $result = $self->valid_order($svc_dsl,'pull'); + return $result unless $result eq ''; + } + + my $now = time; + if($now - $svc_dsl->last_pull < $threshold) { + warn "$me skipping pull since threshold not reached (svcnum=" + . $svc_dsl->svcnum . ",now=$now,threshold=$threshold,last_pull=" + . $svc_dsl->last_pull .")" if $self->option('debug'); + return ''; + } - $result = $self->ikano_command('ORDERSTATUS', - { OrderId => $svc_dsl->vendor_order_id } ); + my $result = $self->ikano_command('ORDERSTATUS', + { OrderId => $svc_dsl->vendor_order_id } ); return $result unless ref($result); # scalar (string) is an error # now we're getting an OrderResponse which should have one Order in it @@ -93,6 +148,7 @@ sub dsl_pull { 'username' => 'Username', 'password' => 'Password' ); + my($fsf, $ikanof); while (($fsf, $ikanof) = each %justUpdate) { $svc_dsl->$fsf($result->{$ikanof}) if $result->{$ikanof} ne $svc_dsl->$fsf; @@ -103,8 +159,20 @@ sub dsl_pull { return 'Invalid number of products on order' if scalar(@product) != 1; my $product = $result->{'Product'}[0]; + $svc_dsl->vendor_order_type('NEW') if $import; # lame + # 3. phonenum - if($svc_dsl->loop_type eq '') { # line-share + if ( $import ) { + if ( $product->{'PhoneNumber'} eq 'STANDALONE' ) { + $svc_dsl->phonenum($product->{'VirtualPhoneNumber'}); + $svc_dsl->loop_type('0'); + } + else { + $svc_dsl->phonenum($product->{'PhoneNumber'}); + $svc_dsl->loop_type(''); + } + } + elsif($svc_dsl->loop_type eq '') { # line-share # TN may change only if sub changes it and New or Change order in Completed status my $tn = $product->{'PhoneNumber'}; if($tn ne $svc_dsl->phonenum) { @@ -167,7 +235,7 @@ sub dsl_pull { my @ostatics = split(',',$ostatics); # more horrible search/sync code below... my $staticsChanged = 0; - foreach $istatic ( @istatics ) { # they have, we don't + foreach my $istatic ( @istatics ) { # they have, we don't unless ( grep($_ eq $istatic, @ostatics) ) { push @ostatics, $istatic; $staticsChanged = 1; @@ -187,7 +255,7 @@ sub dsl_pull { my @tnotes = defined $tnotes ? @$tnotes : (); my @inotes = (); # all Ikano OrderNotes as FS::dsl_note objects my $notesChanged = 0; - foreach $tnote ( @tnotes ) { + foreach my $tnote ( @tnotes ) { my $inote = $self->ikano2fsnote($tnote,$svc_dsl->svcnum); return 'Cannot parse note' unless ref($inote); push @inotes, $inote; @@ -195,16 +263,15 @@ sub dsl_pull { my @onotes = $svc_dsl->notes; # assume notes we already have don't change & no notes added from our side # so using the horrible code below just find what we're missing and add it - my $error; - foreach $inote ( @inotes ) { + foreach my $inote ( @inotes ) { my $found = 0; - foreach $onote ( @onotes ) { + foreach my $onote ( @onotes ) { if($onote->date == $inote->date && $onote->note eq $inote->note) { $found = 1; last; } } - $error = $inote->insert unless ( $found ); + my $error = $inote->insert unless ( $found ); if ( $error ) { $dbh->rollback if $oldAutoCommit; return "Cannot add note: $error"; @@ -213,7 +280,7 @@ sub dsl_pull { $svc_dsl->last_pull((time)); local $FS::svc_Common::noexport_hack = 1; - $error = $svc_dsl->replace; + my $error = $svc_dsl->replace; if ( $error ) { $dbh->rollback if $oldAutoCommit; return "Cannot update DSL data: $error"; @@ -237,35 +304,142 @@ sub ikano2fsnote { $by = "Ikano" if $n->{'CompanyStaffId'} == -1 && $n->{'StaffId'} != -1; $by = "Us" if $n->{'StaffId'} == -1 && $n->{'CompanyStaffId'} != -1; - $fsnote = new FS::dsl_note( { + new FS::dsl_note( { 'svcnum' => $svcnum, - 'by' => $by, + 'author' => $by, 'priority' => $n->{'HighPriority'} eq 'false' ? 'N' : 'H', - 'date' => int(str2time($n->{'Date'})), + '_date' => int(str2time($n->{'Date'})), 'note' => $n->{'Text'}, } ); } +# address always required for Ikano qual, TN optional (assume dry if not given) sub qual { - ''; + my($self,$qual) = (shift,shift); + + return 'Invalid operation - Import Mode is enabled' if $self->import_mode; + + my %location_hash = $qual->location_hash; + return 'No address provided' unless keys %location_hash; + + return 'Location kind is required' unless $location_hash{location_kind}; + + my $svctn = $qual->phonenum; + + my $zip = $location_hash{zip}; + $zip =~ s/(\d{5})-\d{4}/$1/; + + my $result = $self->ikano_command('PREQUAL', + { AddressLine1 => $location_hash{address1}, + AddressUnitType => $location_hash{location_type}, + AddressUnitValue => $location_hash{location_number}, + AddressCity => $location_hash{city}, + AddressState => $location_hash{state}, + ZipCode => $zip, + Country => $location_hash{country}, + LocationType => $location_hash{location_kind}, + PhoneNumber => length($svctn) > 1 ? $svctn : "STANDALONE", + RequestClientIP => '127.0.0.1', + CheckNetworks => $self->option('check_networks'), + } ); + return $result unless ref($result); # error case + return 'Invalid prequal response' unless defined $result->{'PrequalId'}; + + my $qoptions = {}; + # lame data structure traversal... + # don't spend much time here, just get TermsId and ProductCustomId + my $networks = $result->{'Network'}; + my @networks = defined $networks ? @$networks : (); + my $netcount = 0; + foreach my $network ( @networks ) { + my $productgroups = $network->{'ProductGroup'}; + my @productgroups = defined $productgroups ? @$productgroups : (); + my $pgcount = 0; + foreach my $productgroup ( @productgroups ) { + my $prefix = "ikano_Network_$netcount"."_ProductGroup_$pgcount"."_"; + $qoptions->{$prefix."TermsId"} = $productgroup->{'TermsId'}; + my $products = $productgroup->{'Product'}; + my @products = defined $products ? @$products : (); + my $prodcount = 0; + foreach my $product ( @products ) { + $qoptions->{$prefix."Product_$prodcount"."_ProductCustomId"} = $product->{'ProductCustomId'}; + $prodcount++; + } + $pgcount++; + } + $netcount++; + } + + { 'vendor_qual_id' => $result->{'PrequalId'}, + 'status' => scalar(@networks) ? 'Q' : 'D', + 'options' => $qoptions, + }; +} + +sub qual_result { + my($self,$qual) = (shift,shift); + + my %qual_options = $qual->options; + my @externalids = (); + my( $optionname, $optionvalue ); + while (($optionname, $optionvalue) = each %qual_options) { + push @externalids, $optionvalue + if ( $optionname =~ /^ikano_Network_(\d+)_ProductGroup_(\d+)_Product_(\d+)_ProductCustomId$/ + && $optionvalue ne '' ); + } + + my %pkglist = (); + my %found = (); + my @part_pkgs = qsearch( 'part_pkg', { 'disabled' => '' } ); + foreach my $part_pkg ( @part_pkgs ) { + my %vendor_pkg_ids = $part_pkg->vendor_pkg_ids; + my $externalid = $vendor_pkg_ids{$self->exportnum} + if defined $vendor_pkg_ids{$self->exportnum}; + if ( $externalid && grep { $_ eq $externalid } @externalids ) { + $pkglist{$part_pkg->pkgpart} = $part_pkg->pkg_comment; + $found{$externalid}++; + } + } + + my %not_avail = (); + foreach my $externalid ( grep !$found{$_}, @externalids ) { + $not_avail{$externalid} = $externalid; #a better label? + } + + { + 'pkglist' => \%pkglist, + 'not_avail' => \%not_avail, + }; } -sub notes_html { - my($self,$svc_dsl) = (shift,shift); - my $conf = new FS::Conf; - my $date_format = $conf->config('date_format') || '%m/%d/%Y'; - my @notes = $svc_dsl->notes; - my $html = '
Date | By | Priority | Note |
---|---|---|---|
".time2str("$date_format %H:%M",$note->date)." | -".$note->by." | -". ($note->priority eq 'N' ? 'Normal' : 'High') ." | -".$note->note." |