X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=httemplate%2Fsearch%2Ffuture_autobill.html;h=2e723ec79d025d281cc8818e51b288800e73fb76;hb=fad1695f2df61d3fadc5d6fc85527e09d083bf20;hp=d6438d9dcea74cbcdac6f50f61048a020a285bd8;hpb=b89e874e8258288d15c98ed3799a9fede6515fd5;p=freeside.git diff --git a/httemplate/search/future_autobill.html b/httemplate/search/future_autobill.html index d6438d9dc..2e723ec79 100644 --- a/httemplate/search/future_autobill.html +++ b/httemplate/search/future_autobill.html @@ -11,7 +11,12 @@ This report relies on the ability to safely run bill_and_collect(), with all exports and messaging disabled, and then to roll back the results. +This report takes time. If 200 customers have automatic +payment methods, and requester is looking one week ahead, +there will be 1,400 billing and payment cycles simulated + +

<% $report_subtitle %>

<& elements/grid-report.html, title => $report_title, rows => \@rows, @@ -27,16 +32,58 @@ results. td.gridreport { margin: 0 .2em; padding: 0 .4em; } ', + suppress_header => $job ? 1 : 0, + suppress_footer => $job ? 1 : 0, &> +% if ( %pmt_type_subtotal ) { + + + + +% for my $pmt_type ( sort keys %pmt_type_subtotal ) { + + + + +% } +% if ( keys %pmt_type_subtotal > 1 ) { +% $pmt_type_subtotal{Total} += $_ for values %pmt_type_subtotal; + + + + +
+ Summary +
+ <% sprintf '$%.2f', $pmt_type_subtotal{ $pmt_type } %> + + <% $pmt_type |h %> +
+ <% sprintf( '$%.2f', $pmt_type_subtotal{Total} ) %> + + Total +
+% } +% } <%init> - use FS::UID qw( dbh myconnect ); + use DateTime; + use FS::Misc::Savepoint; + use FS::Report::Queued::FutureAutobill; + use FS::UID qw( dbh ); die "access denied" unless $FS::CurrentUser::CurrentUser->access_right('Financial reports'); + my $job = $FS::Report::Queued::FutureAutobill::job; + + $job->update_statustext('0,Finding customers') if $job; + my $DEBUG = $cgi->param('DEBUG') || 0; + my $agentnum = $cgi->param('agentnum') + if $cgi->param('agentnum') =~ /^\d+/; + my $target_dt; my @target_dates; @@ -56,16 +103,20 @@ results. # Get target date from form if ($cgi->param('target_date')) { + # DateTime::Format::DateParse would be better my ($mm, $dd, $yy) = split /[\-\/]/,$cgi->param('target_date'); + ( $yy, $mm, $dd ) = ( $mm, $dd, $yy ) if $mm > 1900; + $target_dt = DateTime->new( month => $mm, day => $dd, year => $yy, %noon, - ) if $mm && $dd & $yy; + ) if $mm && $dd && $yy; # Catch a date from the past: time only travels in one direction - $target_dt = undef if $target_dt->epoch < $now_dt->epoch; + $target_dt = undef + unless $target_dt && $now_dt && $now_dt <= $target_dt; } # without a target date, default to tomorrow @@ -73,6 +124,13 @@ results. $target_dt = $now_dt->clone->add( days => 1 ); } + my $report_title = FS::cust_payby->future_autobill_report_title; + my $report_subtitle = sprintf( + '(%s through %s)', + $now_dt->mdy('/'), + $target_dt->mdy('/'), + ); + # Create a range of dates from today until the given report date # (leaving the probably useless 'quick-report' mode, but disabled) if ( 1 || $cgi->param('multiple_billing_dates')) { @@ -87,29 +145,33 @@ 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') - OR ( paydate > '".$target_dt->ymd."') + table => 'cust_payby', + addl_from => 'JOIN cust_main USING (custnum)', + hashref => { weight => { op => '>', value => '0' }}, + order_by => " ORDER BY weight DESC ", + extra_sql => + "AND ( + cust_payby.payby IN ('CHEK','DCHK','DCHEK') + OR ( cust_payby.paydate > '".$target_dt->ymd."') ) - ", + AND " . $FS::CurrentUser::CurrentUser->agentnums_sql + . ($agentnum ? "AND cust_main.agentnum = $agentnum" : ''), }); + my $completion_target = scalar(keys %cust_payby) * scalar( @target_dates ); + my $completion_progress = 0; + my $fakebill_time = time(); my %abreport; my @rows; + my %pmt_type_subtotal; local $@; local $SIG{__DIE__}; 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; @@ -121,6 +183,9 @@ results. local $FS::cust_main::Billing_Realtime::BOP_TESTING = 1; local $FS::cust_main::Billing_Realtime::BOP_TESTING_SUCCESS = 1; + my $savepoint_label = 'future_autobill'; + savepoint_create( $savepoint_label ); + warn sprintf "Report involves %s customers", scalar keys %cust_payby if $DEBUG; @@ -149,8 +214,18 @@ results. ); warn "!!! $error (simulating future billing)\n" if $error; + + my $statustext = sprintf( + '%s,Simulating upcoming invoices and payments', + int( ( ++$completion_progress / $completion_target ) * 100 ) + ); + $job->update_statustext( $statustext ) if $job; + warn "[ $completion_progress / $completion_target ] $statustext\n" + if $DEBUG; + } + # Generate report rows from recorded payments in cust_pay for my $cust_pay ( qsearch( cust_pay => { @@ -195,13 +270,17 @@ results. ] }; + $pmt_type_subtotal{ $cust_pay->paycardtype || $cust_pay-> payby } + += $cust_pay->paid; + } # /foreach payment # Roll back database at the end of each customer # 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; + savepoint_rollback( $savepoint_label ); dbh->rollback if $oldAutoCommit; } # /foreach $custnum @@ -226,21 +305,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, - ); -