X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_export%2Fikano.pm;h=5d8285e6be0c5d0464712b887793569f1cee0ffa;hb=f9a181e4c2e505df84de16190ee3b75011326f3f;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..5d8285e6b 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 ); @@ -38,6 +40,8 @@ END sub rebless { shift; } +sub external_pkg_map { 1; } + 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,10 +51,17 @@ 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($self, $svc_dsl, $threshold) = (shift, shift, shift); 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 } ); @@ -93,6 +104,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; @@ -167,7 +179,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 +199,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; @@ -196,9 +208,9 @@ sub dsl_pull { # 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; @@ -237,17 +249,128 @@ 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'}, } ); } sub qual { - ''; + my($self,$qual) = (shift,shift); +# address always required for Ikano qual, TN optional (assume dry if not given) + + my %location_hash = $qual->location; + return 'No address provided' unless %location_hash; + my $svctn = $qual->phonenum; + + 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 => $location_hash{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 $result = { 'header' => 'Qualifying Packages', + 'pkglist' => \%pkglist, + }; + + 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." - ".$part_pkg->comment; + } + } + + $result; +} + +sub quals_by_cust_and_pkg { + my($self, $custnum, $pkgpart) = (shift,shift,shift); + + die "invalid custnum or pkgpart" + unless ($custnum =~ /^\d+$/ && $pkgpart =~ /^\d+$/); + + my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } ); + die "no part_pkg found" unless $part_pkg; + my %vendor_pkg_ids = $part_pkg->vendor_pkg_ids; + my $external_id = $vendor_pkg_ids{$self->exportnum}; + die "no vendor package id defined on this package" unless $external_id; + + my $extra_sql = "where custnum = $custnum or locationnum in (select " + . "locationnum from cust_location where custnum = $custnum)"; + my @quals = qsearch( { 'table' => 'qual', 'extra_sql' => $extra_sql, } ); + + my @filtered_quals; + foreach my $qual ( @quals ) { + my %qual_options = $qual->options; + my( $optionname, $optionvalue ); + while (($optionname, $optionvalue) = each %qual_options) { + push @filtered_quals, $qual + if ( $optionname =~ /^ikano_Network_(\d+)_ProductGroup_(\d+)_Product_(\d+)_ProductCustomId$/ + && $optionvalue eq $external_id ); + } + } + + @filtered_quals; } sub notes_html { @@ -257,7 +380,7 @@ sub notes_html { my @notes = $svc_dsl->notes; my $html = ''; - foreach $note ( @notes ) { + foreach my $note ( @notes ) { $html .= " @@ -303,16 +426,17 @@ sub valid_order { # common to all order types/status/loop_type my $error = !($svc_dsl->desired_due_date - && grep($_ eq $svc_dsl->vendor_order_type, @Net::Ikano::orderType) + && grep($_ eq $svc_dsl->vendor_order_type, Net::Ikano->orderTypes) && $svc_dsl->first && $svc_dsl->last && defined $svc_dsl->loop_type && $svc_dsl->vendor_qual_id ); return 'Missing or invalid order data' if $error; - + + my %vendor_pkg_ids = $svc_dsl->cust_svc->cust_pkg->part_pkg->vendor_pkg_ids; return 'Package does not have an external id configured' - if $svc_dsl->cust_svc->cust_pkg->part_pkg->options('externalid',1) eq ''; + unless defined $vendor_pkg_ids{$self->exportnum}; return 'No valid qualification for this order' unless qsearch( 'qual', { 'vendor_qual_id' => $svc_dsl->vendor_qual_id }); @@ -322,7 +446,7 @@ sub valid_order { if($svc_dsl->vendor_order_type eq 'NEW') { if($svc_dsl->pushed) { $error = !( ($action eq 'pull' || $action eq 'statuschg' - || $action eq 'delete') + || $action eq 'delete' || $action eq 'expire') && length($svc_dsl->vendor_order_id) > 0 && length($svc_dsl->vendor_order_status) > 0 ); @@ -355,6 +479,7 @@ sub qual2termsid { my $qual = qsearchs( 'qual', { 'vendor_qual_id' => $vendor_qual_id }); return '' unless $qual; my %qual_options = $qual->options; + my( $optionname, $optionvalue ); while (($optionname, $optionvalue) = each %qual_options) { if ( $optionname =~ /^ikano_Network_(\d+)_ProductGroup_(\d+)_Product_(\d+)_ProductCustomId$/ && $optionvalue eq $ProductCustomId ) { @@ -369,8 +494,6 @@ sub qual2termsid { sub _export_insert { my( $self, $svc_dsl ) = (shift, shift); - $self->loadmod; - my $result = $self->valid_order($svc_dsl,'insert'); return $result unless $result eq ''; @@ -378,7 +501,8 @@ sub _export_insert { my $contactTN = $svc_dsl->cust_svc->cust_pkg->cust_main->daytime; $contactTN =~ s/[^0-9]//g; - my $ProductCustomId = $svc_dsl->cust_svc->cust_pkg->part_pkg->option('externalid',1); + my %vendor_pkg_ids = $svc_dsl->cust_svc->cust_pkg->part_pkg->vendor_pkg_ids; + my $ProductCustomId = $vendor_pkg_ids{$self->exportnum}; my $args = { orderType => 'NEW', @@ -423,9 +547,8 @@ sub _export_insert { $svc_dsl->vendor_order_status($result->{'Status'}); $svc_dsl->username($result->{'Username'}); local $FS::svc_Common::noexport_hack = 1; - local $FS::UID::AutoCommit = 0; $result = $svc_dsl->replace; - return 'Error setting DSL fields' if $result; + return "Error setting DSL fields: $result" if $result; ''; } @@ -458,7 +581,7 @@ sub _export_delete { return $result unless $result eq ''; # for now allow an immediate cancel only on New orders in New/Pending status - #XXX: add support for Chance and Cancel orders in New/Pending status later + #XXX: add support for Change and Cancel orders in New/Pending status later if($svc_dsl->vendor_order_type eq 'NEW') { if($svc_dsl->vendor_order_status eq 'NEW' @@ -466,13 +589,23 @@ sub _export_delete { my $result = $self->ikano_command('CANCEL', { OrderId => $svc_dsl->vendor_order_id, } ); return $result unless ref($result); # scalar (string) is an error - - return $self->dsl_pull($svc_dsl); + return 'Unable to cancel order' unless $result->{'Order'}; + $result = $result->{'Order'}; + return 'Invalid cancellation response' + unless $result->{'Status'} eq 'CANCELLED' + && $result->{'OrderId'} eq $svc_dsl->vendor_order_id; + + # we're supposed to do a pull, but it will break everything, so don't + # this is very wrong... } else { return "Cannot cancel a NEW order unless it's in NEW or PENDING status"; } } + elsif($svc_dsl->vendor_order_type eq 'CANCEL') { + return 'Cannot cancel a CANCEL order unless expire was set' + unless $svc_dsl->cust_svc->cust_pkg->expire > 0; + } else { return 'Canceling orders other than NEW orders is not currently implemented'; } @@ -480,6 +613,84 @@ sub _export_delete { ''; } +sub export_expire { + my($self, $svc_dsl, $date) = (shift, shift, shift); + + my $result = $self->valid_order($svc_dsl,'expire'); + return $result unless $result eq ''; + + # for now allow a proper cancel only on New orders in Completed status + #XXX: add support for some other cases in future + + if($svc_dsl->vendor_order_type eq 'NEW' + && $svc_dsl->vendor_order_status eq 'COMPLETED') { + + my $contactTN = $svc_dsl->cust_svc->cust_pkg->cust_main->daytime; + $contactTN =~ s/[^0-9]//g; + + my %vendor_pkg_ids = $svc_dsl->cust_svc->cust_pkg->part_pkg->vendor_pkg_ids; + my $ProductCustomId = $vendor_pkg_ids{$self->exportnum}; + + # we are now a cancel order + $svc_dsl->desired_due_date($date); + $svc_dsl->vendor_order_type('CANCEL'); + + my $args = { + orderType => 'CANCEL', + ProductCustomId => $ProductCustomId, + TermsId => $self->qual2termsid($svc_dsl->vendor_qual_id,$ProductCustomId), + DSLPhoneNumber => $svc_dsl->loop_type eq '0' ? 'STANDALONE' + : $svc_dsl->phonenum, + Password => $svc_dsl->password, + PrequalId => $svc_dsl->vendor_qual_id, + CompanyName => $svc_dsl->company, + FirstName => $svc_dsl->first, + LastName => $svc_dsl->last, + MiddleName => '', + ContactMethod => 'PHONE', + ContactPhoneNumber => $contactTN, + ContactEmail => 'x@x.xx', + ContactFax => '', + DateToOrder => time2str("%Y-%m-%d",$date), + RequestClientIP => '127.0.0.1', + IspChange => 'NO', + IspPrevious => '', + CurrentProvider => '', + }; + + $args->{'VirtualPhoneNumber'} = $svc_dsl->phonenum + if $svc_dsl->loop_type eq '0'; + + $result = $self->ikano_command('ORDER',$args); + return $result unless ref($result); # scalar (string) is an error + + # now we're getting an OrderResponse which should have one Order in it + warn "$me _export_insert OrderResponse hash:\n".Dumper($result) + if $self->option('debug'); + + return 'Invalid order response' unless defined $result->{'Order'}; + $result = $result->{'Order'}; + + return 'No/invalid order id or status returned' + unless defined $result->{'Status'} && defined $result->{'OrderId'} + && grep($_ eq $result->{'Status'}, @Net::Ikano::orderStatus); + + $svc_dsl->pushed(time); + $svc_dsl->last_pull((time)+1); + $svc_dsl->vendor_order_id($result->{'OrderId'}); + $svc_dsl->vendor_order_status($result->{'Status'}); + $svc_dsl->monitored('Y'); + local $FS::svc_Common::noexport_hack = 1; + $result = $svc_dsl->replace; + return "Error setting DSL fields: $result" if $result; + } + else { + return "Cancelling anything other than NEW orders in COMPLETED status is " + . "not currently implemented"; + } + ''; +} + sub statuschg { my( $self, $svc_dsl, $type ) = (shift, shift, shift);
DateByPriorityNote
".time2str("$date_format %H:%M",$note->date)." ".$note->by."