fix "column reference "refnum" is ambiguous" is credit detail report (when no adverti...
[freeside.git] / httemplate / search / cust_credit_bill.html
index 7f9eb78..fb71061 100644 (file)
@@ -1,4 +1,4 @@
-<% include( 'elements/search.html',
+<& elements/search.html,
                 'title'       => $title,
                 'name'        => 'net credits',
                 'query'       => $sql_query,
@@ -64,8 +64,8 @@
                              '',
                              FS::UI::Web::cust_styles(),
                            ],
-          )
-%>
+          
+&>
 <%init>
 
 die "access denied"
@@ -79,12 +79,19 @@ my $title = 'Net Credit Search Results';
 my @search = ();
 
 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
-  push @search, "agentnum = $1";
+  push @search, "cust_main.agentnum = $1";
   my $agent = qsearchs('agent', { 'agentnum' => $1 } );
   die "unknown agentnum $1" unless $agent;
   $title = $agent->agent. " $title";
 }
 
+if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
+  push @search, "cust_main.refnum = $1";
+  my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
+  die "unknown refnum $1" unless $part_referral;
+  $title = $part_referral->referral. " $title";
+}
+
 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
 push @search, "cust_bill._date >= $beginning ",
               "cust_bill._date <= $ending";
@@ -96,8 +103,8 @@ my $where = 'WHERE '. join(' AND ', @search);
 #
 my $count_query = 'SELECT COUNT(*), SUM(amount)
                    FROM cust_credit_bill
-                     LEFT JOIN cust_bill USING ( invnum  )
-                     LEFT JOIN cust_main USING ( custnum ) '.
+                     LEFT JOIN cust_bill USING ( invnum  ) '.
+                  FS::UI::Web::join_cust_main('cust_bill') .
                   $where;
 
 my $sql_query   = {
@@ -114,8 +121,8 @@ my $sql_query   = {
   'hashref'   => {},
   'extra_sql' => $where,
   'addl_from' => 'LEFT JOIN cust_bill   USING ( invnum  )
-                  LEFT JOIN cust_credit USING ( crednum )
-                  LEFT JOIN cust_main ON ( cust_bill.custnum = cust_main.custnum )',
+                  LEFT JOIN cust_credit USING ( crednum )'.
+                  FS::UI::Web::join_cust_main('cust_bill')
 };
 
 my $cust_bill_link = sub {