qualification address handling changes, RT#7111
[freeside.git] / FS / FS / part_export / ikano.pm
index fe645e4..b04defa 100644 (file)
@@ -42,6 +42,33 @@ 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 dsl_pull {
 # we distinguish between invalid new data (return error) versus data that
 # has legitimately changed (may eventually execute hooks; now just update)
@@ -258,12 +285,16 @@ sub ikano2fsnote {
      } );
 }
 
+# address always required for Ikano qual, TN optional (assume dry if not given)
 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 %location_hash = $qual->location_hash; 
+    return 'No address provided' unless keys %location_hash;
+
+    warn Dumper(\%location_hash);
+    return 'Location kind is required' unless $location_hash{location_kind};
+
     my $svctn = $qual->phonenum;
 
     my $result = $self->ikano_command('PREQUAL',
@@ -313,7 +344,7 @@ sub qual {
     };
 }
 
-sub qual_html {
+sub qual_result {
     my($self,$qual) = (shift,shift);
     
     my %qual_options = $qual->options;
@@ -325,23 +356,52 @@ sub qual_html {
                && $optionvalue ne '' );
     }
 
-    # XXX: eventually perhaps this should return both the packages a link to
-    # order each package and go to the svc prov with the prequal id filled in
-    # but only if cust, not prospect!
-    my $list = "<B>Qualifying Packages:</B><UL>";
+    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 ) {
-           $list .= "<LI>".$part_pkg->pkgpart.": ".$part_pkg->pkg." - "
-               .$part_pkg->comment."</LI>" 
-             if grep( $_ eq $externalid, @externalids );
+       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 );
        }
     }
-    $list .= "</UL>";
-    $list;
+
+    @filtered_quals;
 }
 
 sub notes_html {