RT#41394: Add advertising source to customer fields selection [new solution, for...
[freeside.git] / FS / FS / cust_main / Search.pm
index 097f2fb..cc23f50 100644 (file)
@@ -531,10 +531,12 @@ sub email_search {
       if $DEBUG;
 
     push @cust_main,
-      map $_->cust_main,
+      map { $_->cust_main }
+      map { $_->cust_contact }
+      map { $_->contact }
           qsearch( {
-                     'table'     => 'cust_main_invoice',
-                     'hashref'   => { 'dest' => $email },
+                     'table'     => 'contact_email',
+                     'hashref'   => { 'emailaddress' => $email },
                    }
                  );
 
@@ -618,6 +620,10 @@ listref (list returned by FS::UI::Web::parse_lt_gt($cgi, 'current_balance'))
 
 bool
 
+=item select_referral
+
+bool, join to part_referral and select part_referral.referral
+
 =back
 
 =cut
@@ -808,30 +814,24 @@ sub search {
   ##
 
   push @where,
-    'EXISTS ( SELECT 1 FROM cust_main_invoice
-                WHERE cust_main_invoice.custnum = cust_main.custnum
-                  AND length(dest) > 5
-            )'  # AND dest LIKE '%@%'
+    'EXISTS ( SELECT 1 FROM contact_email
+                JOIN cust_contact USING (contactnum)
+                WHERE cust_contact.custnum = cust_main.custnum
+            )'
     if $params->{'with_email'};
 
   ##
   # "with postal mail invoices" checkbox
   ##
 
-  push @where,
-    "EXISTS ( SELECT 1 FROM cust_main_invoice
-                WHERE cust_main_invoice.custnum = cust_main.custnum
-                  AND dest = 'POST' )"
+  push @where, "cust_main.postal_invoice = 'Y'"
     if $params->{'POST'};
 
   ##
   # "without postal mail invoices" checkbox
   ##
 
-  push @where,
-    "NOT EXISTS ( SELECT 1 FROM cust_main_invoice
-                    WHERE cust_main_invoice.custnum = cust_main.custnum
-                      AND dest = 'POST' )"
+  push @where, "cust_main.postal_invoice IS NULL"
     if $params->{'no_POST'};
 
   ##
@@ -1031,6 +1031,13 @@ sub search {
   my(@extra_headers) = ();
   my(@extra_fields)  = ();
 
+  if ($params->{'select_referral'}) {
+    $addl_from .= ' LEFT JOIN part_referral ON ( cust_main.refnum = part_referral.refnum ) ';
+    push @select, 'part_referral.referral';
+    push @extra_headers, 'Advertising Source';
+    push @extra_fields, 'referral';
+  }
+    
   if ($params->{'flattened_pkgs'}) {
 
     #my $pkg_join = '';