From fe14c0ff8a609d9488c40e81eb25babfa691b34c Mon Sep 17 00:00:00 2001 From: Mitch Jackson Date: Tue, 11 Sep 2018 05:51:11 -0400 Subject: [PATCH] RT# 78547 Future autobill report - agent virt, dynamic title --- FS/FS/cust_payby.pm | 75 ++++++++++++++++++++++----- httemplate/search/future_autobill.html | 45 ++++++---------- httemplate/search/report_future_autobill.html | 25 +++------ 3 files changed, 86 insertions(+), 59 deletions(-) diff --git a/FS/FS/cust_payby.pm b/FS/FS/cust_payby.pm index 301eb6106..9d8be120a 100644 --- a/FS/FS/cust_payby.pm +++ b/FS/FS/cust_payby.pm @@ -1,5 +1,6 @@ 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 ); @@ -914,31 +915,79 @@ sub search_sql { =back -=item count_autobill_cards +=item has_autobill_cards 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 -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 { diff --git a/httemplate/search/future_autobill.html b/httemplate/search/future_autobill.html index d6438d9dc..d4ad8e524 100644 --- a/httemplate/search/future_autobill.html +++ b/httemplate/search/future_autobill.html @@ -30,13 +30,17 @@ results. &> <%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; + 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; @@ -87,17 +91,17 @@ results. # 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."') ) - ", + AND " . $FS::CurrentUser::CurrentUser->agentnums_sql + . ($agentnum ? "AND cust_main.agentnum = $agentnum" : ''), }); my $fakebill_time = time(); @@ -109,7 +113,7 @@ results. 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; @@ -201,7 +205,7 @@ results. # 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 @@ -226,21 +230,4 @@ results. # 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, - ); - diff --git a/httemplate/search/report_future_autobill.html b/httemplate/search/report_future_autobill.html index ff2f85715..ccde299e9 100644 --- a/httemplate/search/report_future_autobill.html +++ b/httemplate/search/report_future_autobill.html @@ -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, + ) + %> +
@@ -39,28 +45,13 @@ Display date selector for the future_autobill.html report <%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('/'); - -# 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; -- 2.11.0