unique checking for svc_phone like svc_acct, closes: RT#4204 (also a few lines of...
[freeside.git] / FS / FS / svc_Common.pm
index 21df6a7..2866bfe 100644 (file)
@@ -49,7 +49,7 @@ sub search_sql_field {
   my( $class, $field, $string ) = @_;
   my $table = $class->table;
   my $q_string = dbh->quote($string);
-  "lc($table.$field) = lc($q_string)";
+  "LOWER($table.$field) = LOWER($q_string)";
 }
 
 #fallback for services that don't provide a search... 
@@ -249,6 +249,7 @@ sub insert {
 
   my $error =    $self->set_auto_inventory
               || $self->check
+              || $self->_check_duplicate
               || $self->SUPER::insert;
   if ( $error ) {
     $dbh->rollback if $oldAutoCommit;
@@ -314,6 +315,10 @@ sub insert {
   '';
 }
 
+#fallbacks
+sub _check_duplcate { ''; }
+sub table_dupcheck_fields { (); }
+
 =item delete [ , OPTION => VALUE ... ]
 
 Deletes this account from the database.  If there is an error, returns the
@@ -390,6 +395,25 @@ sub replace {
     return $error;
   }
 
+  #redundant, but so any duplicate fields are maniuplated as appropriate
+  # (svc_phone.phonenum)
+  my $error = $new->check;
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
+  }
+
+  #if ( $old->username ne $new->username || $old->domsvc != $new->domsvc ) {
+  if ( grep { $old->$_ ne $new->$_ } $new->table_dupcheck_fields ) {
+
+    $new->svcpart( $new->cust_svc->svcpart ) unless $new->svcpart;
+    $error = $new->_check_duplicate;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return $error;
+    }
+  }
+
   $error = $new->SUPER::replace($old);
   if ($error) {
     $dbh->rollback if $oldAutoCommit;
@@ -511,7 +535,7 @@ sub setx {
   return $error if $error;
 
   my $part_svc = $self->part_svc;
-  return "Unkonwn svcpart" unless $part_svc;
+  return "Unknown svcpart" unless $part_svc;
 
   #set default/fixed/whatever fields from part_svc