X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Fcontact.pm;h=26f39eda509b99b005a6912f3b0160fc0d19dadf;hp=957f96cf89453e18d399f2608d2003d1f65129fc;hb=HEAD;hpb=8994fc466c8ef3e7ba30d0a4e54cdf77be667e05 diff --git a/FS/FS/contact.pm b/FS/FS/contact.pm index 957f96cf8..26f39eda5 100644 --- a/FS/FS/contact.pm +++ b/FS/FS/contact.pm @@ -168,7 +168,6 @@ sub insert { my $existing_contact = ''; my @contact_emails = (); my %contact_nums = (); - $contact_nums{$self->contactnum} = '1' if $self->contactnum; if ( $self->get('emailaddress') =~ /\S/ ) { @@ -202,6 +201,8 @@ sub insert { return $error; } + $contact_nums{$self->contactnum} = '1' if $self->contactnum; + my $cust_contact = ''; # if $self->custnum was set, then the customer-specific properties # (custnum, classnum, invoice_dest, selfservice_access, comment) are in @@ -449,7 +450,8 @@ sub replace { ## if found, just add that contact to cust_contact with link_hash credentials ## as email can not be tied to two contacts. my @contact_emails = (); - my @contact_nums = ($self->contactnum,); + my %contact_nums = (); + $contact_nums{$self->contactnum} = '1' if $self->contactnum; if ( $self->get('emailaddress') =~ /\S/ ) { foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) { @@ -462,11 +464,18 @@ sub replace { push @contact_emails, $email; } else { - push @contact_nums, $contact->contactnum; + $contact_nums{$contact->contactnum} = '1'; } } + ## were all emails duplicates? if so reset original emails + if (scalar @contact_emails < 1 && scalar (keys %contact_nums) > 1) { + foreach (qsearch('contact_email', {'contactnum' => $self->contactnum})) { + push @contact_emails, $_->emailaddress; + } + } + my $emails = join(' , ', @contact_emails); $self->emailaddress($emails); @@ -488,9 +497,9 @@ sub replace { # fields. if ( $custnum ) { - foreach my $contactnum (@contact_nums) { + foreach my $contactnum (keys %contact_nums) { - my %hash = ( 'contactnum' => $contactnum, #$self->contactnum, + my %hash = ( 'contactnum' => $contactnum, 'custnum' => $custnum, ); my $error; @@ -819,7 +828,38 @@ or there isn't one, returns the empty string. =cut sub by_selfservice_email { - my($class, $email) = @_; + my($class, $email, $case_insensitive) = @_; + + my $email_search = "emailaddress = '".$email."'"; + $email_search = "LOWER(emailaddress) = LOWER('".$email."')" if $case_insensitive; + + my $contact_email = qsearchs({ + 'table' => 'contact_email', + 'addl_from' => ' LEFT JOIN contact USING ( contactnum ) ', + 'extra_sql' => " + WHERE $email_search + AND ( contact.disabled IS NULL ) + AND EXISTS ( SELECT 1 FROM cust_contact + WHERE contact.contactnum = cust_contact.contactnum + AND cust_contact.selfservice_access = 'Y' + ) + ", + }) or return ''; + + $contact_email->contact; + +} + +=item by_selfservice_email_custnum EMAILADDRESS, CUSTNUM + +Alternate search constructor (class method). Given an email address and custnum, returns +the contact for that address and custnum. If that contact doesn't have selfservice access, +or there isn't one, returns the empty string. + +=cut + +sub by_selfservice_email_custnum { + my($class, $email, $custnum) = @_; my $contact_email = qsearchs({ 'table' => 'contact_email', @@ -830,6 +870,7 @@ sub by_selfservice_email { AND EXISTS ( SELECT 1 FROM cust_contact WHERE contact.contactnum = cust_contact.contactnum AND cust_contact.selfservice_access = 'Y' + AND cust_contact.custnum = $custnum ) ", }) or return '';