RT# 78547 Future autobill report - agent virt, dynamic title
authorMitch Jackson <mitch@freeside.biz>
Tue, 11 Sep 2018 09:51:11 +0000 (05:51 -0400)
committerMitch Jackson <mitch@freeside.biz>
Wed, 19 Sep 2018 16:06:06 +0000 (12:06 -0400)
FS/FS/cust_payby.pm
httemplate/search/future_autobill.html
httemplate/search/report_future_autobill.html

index 301eb61..9d8be12 100644 (file)
@@ -1,5 +1,6 @@
 package FS::cust_payby;
 use base qw( FS::payinfo_Mixin FS::cust_main_Mixin FS::Record );
 package FS::cust_payby;
 use base qw( FS::payinfo_Mixin FS::cust_main_Mixin FS::Record );
+use feature 'state';
 
 use strict;
 use Scalar::Util qw( blessed );
 
 use strict;
 use Scalar::Util qw( blessed );
@@ -914,31 +915,79 @@ sub search_sql {
 
 =back
 
 
 =back
 
-=item count_autobill_cards
+=item has_autobill_cards
 
 Returns the number of unexpired cards configured for autobill
 
 =cut
 
 
 Returns the number of unexpired cards configured for autobill
 
 =cut
 
-sub count_autobill_cards {
-  shift->count("
-    weight > 0
-    AND payby IN ('CARD','DCRD')
-    AND paydate > '".DateTime->now->ymd."'
-  ");
+sub has_autobill_cards {
+  scalar FS::Record::qsearch({
+    table     => 'cust_payby',
+    addl_from => 'JOIN cust_main USING (custnum)',
+    order_by  => 'LIMIT 1',
+    hashref   => {
+        paydate => { op => '>', value => DateTime->now->ymd },
+        weight  => { op => '>',  value => 0 },
+    },
+    extra_sql =>
+      "AND payby IN ('CARD', 'DCRD') ".
+      'AND '.
+      $FS::CurrentUser::CurrentUser->agentnums_sql( table => 'cust_main' ),
+  });
 }
 
 }
 
-=item count_autobill_checks
+=item has_autobill_checks
 
 Returns the number of check accounts configured for autobill
 
 =cut
 
 
 Returns the number of check accounts configured for autobill
 
 =cut
 
-sub count_autobill_checks {
-  shift->count("
-    weight > 0
-    AND payby IN ('CHEK','DCHEK')
-  ");
+sub has_autobill_checks {
+  scalar FS::Record::qsearch({
+    table     => 'cust_payby',
+    addl_from => 'JOIN cust_main USING (custnum)',
+    order_by  => 'LIMIT 1',
+    hashref   => {
+        weight  => { op => '>',  value => 0 },
+    },
+    extra_sql =>
+      "AND payby IN ('CHEK','DCHEK','DCHK') ".
+      'AND '.
+      $FS::CurrentUser::CurrentUser->agentnums_sql( table => 'cust_main' ),
+  });
+}
+
+=item future_autobill_report_title
+
+Determine if the future_autobill report should be available.
+If so, return a dynamic title for it
+
+=cut
+
+sub future_autobill_report_title {
+  # Perhaps this function belongs somewhere else
+  state $title;
+  return $title if defined $title;
+
+  # Report incompatible with tax engines
+  return $title = '' if FS::TaxEngine->new->info->{batch};
+
+  my $has_cards  = has_autobill_cards();
+  my $has_checks = has_autobill_checks();
+  my $_title = 'Future %s transactions';
+
+  if ( $has_cards && $has_checks ) {
+    $title = sprintf $_title, 'credit card and electronic check';
+  } elsif ( $has_cards ) {
+    $title = sprintf $_title, 'credit card';
+  } elsif ( $has_checks ) {
+    $title = sprintf $_title, 'electronic check';
+  } else {
+    $title = '';
+  }
+
+  $title;
 }
 
 sub _upgrade_data {
 }
 
 sub _upgrade_data {
index d6438d9..d4ad8e5 100644 (file)
@@ -30,13 +30,17 @@ results.
 &>
 
 <%init>
 &>
 
 <%init>
-  use FS::UID qw( dbh myconnect );
+  use FS::UID qw( dbh );
 
   die "access denied"
     unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
 
   my $DEBUG = $cgi->param('DEBUG') || 0;
 
 
   die "access denied"
     unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
 
   my $DEBUG = $cgi->param('DEBUG') || 0;
 
+  my $report_title = FS::cust_payby->future_autobill_report_title;
+  my $agentnum = $cgi->param('agentnum')
+    if $cgi->param('agentnum') =~ /^\d+/;
+
   my $target_dt;
   my @target_dates;
 
   my $target_dt;
   my @target_dates;
 
@@ -87,17 +91,17 @@ results.
 
   # List all customers with an auto-bill method that's not expired
   my %cust_payby = map {$_->custnum => $_} qsearch({
 
   # List all customers with an auto-bill method that's not expired
   my %cust_payby = map {$_->custnum => $_} qsearch({
-    table => 'cust_payby',
-    hashref => {
-      weight  => { op => '>', value => '0' },
-    },
-    order_by => " ORDER BY weight DESC ",
-    extra_sql => "
-      AND (
-        payby IN ('CHEK','DCHK')
+    table     => 'cust_payby',
+    addl_from => 'JOIN cust_main USING (custnum)',
+    hashref   => {  weight  => { op => '>', value => '0' }},
+    order_by  => " ORDER BY weight DESC ",
+    extra_sql =>
+      "AND (
+        payby IN ('CHEK','DCHK','DCHEK')
         OR ( paydate > '".$target_dt->ymd."')
       )
         OR ( paydate > '".$target_dt->ymd."')
       )
-    ",
+      AND " . $FS::CurrentUser::CurrentUser->agentnums_sql
+      . ($agentnum ? "AND cust_main.agentnum = $agentnum" : ''),
   });
 
   my $fakebill_time = time();
   });
 
   my $fakebill_time = time();
@@ -109,7 +113,7 @@ results.
 
   eval { # Sandbox
 
 
   eval { # Sandbox
 
-    # Create new database handle and supress all COMMIT statements
+    # Supress COMMIT statements
     my $oldAutoCommit = $FS::UID::AutoCommit;
     local $FS::UID::AutoCommit = 0;
     local $FS::UID::ForceObeyAutoCommit = 1;
     my $oldAutoCommit = $FS::UID::AutoCommit;
     local $FS::UID::AutoCommit = 0;
     local $FS::UID::ForceObeyAutoCommit = 1;
@@ -201,7 +205,7 @@ results.
       # Makes the report slighly slower, but ensures only one customer row
       #   locked at a time
 
       # Makes the report slighly slower, but ensures only one customer row
       #   locked at a time
 
-      warn "-- custnum $custnum -- rollback()\n";
+      warn "-- custnum $custnum -- rollback()\n" if $DEBUG;
       dbh->rollback if $oldAutoCommit;
 
     } # /foreach $custnum
       dbh->rollback if $oldAutoCommit;
 
     } # /foreach $custnum
@@ -226,21 +230,4 @@ results.
   # grid-report.html requires a parallel @rows parameter to accompany @cells
   @rows = map { {class => 'gridreport'} } 1..scalar(@cells);
 
   # grid-report.html requires a parallel @rows parameter to accompany @cells
   @rows = map { {class => 'gridreport'} } 1..scalar(@cells);
 
-  # Dynamic report title
-  my $title_types = '';
-  my $card_count = FS::cust_payby->count_autobill_cards;
-  my $check_count = FS::cust_payby->count_autobill_checks;
-  if ( $card_count && $check_count ) {
-    $title_types = 'Card and Check';
-  } elsif ( $card_count ) {
-    $title_types = 'Card';
-  } elsif ( $check_count ) {
-    $title_types = 'Check';
-  }
-
-  my $report_title = sprintf(
-    'Upcoming Auto Bill %s Transactions',
-    $title_types,
-  );
-
 </%init>
 </%init>
index ff2f857..ccde299 100644 (file)
@@ -25,6 +25,12 @@ Display date selector for the future_autobill.html report
     }
   &>
 
     }
   &>
 
+  <% include('/elements/tr-select-agent.html',
+              'label'         => 'For agent: ',
+              'disable_empty' => 0,
+            )
+  %>
+
   </TABLE>
 
   <BR>
   </TABLE>
 
   <BR>
@@ -39,28 +45,13 @@ Display date selector for the future_autobill.html report
 
 <%init>
 use FS::cust_payby;
 
 <%init>
 use FS::cust_payby;
+use FS::CurrentUser;
 
 die "access denied"
   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
 
 my $target_date = DateTime->now->add(days => 1)->mdy('/');
 
 die "access denied"
   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
 
 my $target_date = DateTime->now->add(days => 1)->mdy('/');
-
-# Dynamic report title
-my $title_types = '';
-my $card_count = FS::cust_payby->count_autobill_cards;
-my $check_count = FS::cust_payby->count_autobill_checks;
-if ( $card_count && $check_count ) {
-  $title_types = 'Card and Check';
-} elsif ( $card_count ) {
-  $title_types = 'Card';
-} elsif ( $check_count ) {
-  $title_types = 'Check';
-}
-
-my $report_title = sprintf(
-  'Upcoming Auto Bill %s Transactions',
-  $title_types,
-);
+my $report_title = FS::cust_payby->future_autobill_report_title;
 
 </%init>
 
 
 </%init>