From 0d2c8c61b8c5644c7dedcffcb28210f9a0cec55d Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 5 Aug 2011 23:20:03 +0000 Subject: [PATCH] honor next-bill-ignore-time when searching for customers to bill also, RT#12570 --- FS/FS/Cron/bill.pm | 5 ++++- FS/FS/Misc/DateTime.pm | 25 ++++++++++++++++++++++++- FS/FS/cust_main/Billing.pm | 25 ++++++------------------- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/FS/FS/Cron/bill.pm b/FS/FS/Cron/bill.pm index 7388733d4..88fe69b77 100644 --- a/FS/FS/Cron/bill.pm +++ b/FS/FS/Cron/bill.pm @@ -7,6 +7,7 @@ use Date::Parse; use DBI 1.33; #The "clone" method was added in DBI 1.33. use FS::UID qw( dbh driver_name ); use FS::Record qw( qsearch qsearchs ); +use FS::Misc::DateTime qw( day_end ); use FS::queue; use FS::cust_main; use FS::part_event; @@ -184,6 +185,8 @@ sub bill_where { # generate where_pkg/where_event search clause ### + my $billtime = day_end($time); + # select * from cust_main where my $where_pkg = <<"END"; EXISTS( @@ -195,7 +198,7 @@ sub bill_where { OR ( start_date IS NOT NULL AND start_date <= $^T ) ) ) - OR bill IS NULL OR bill <= $time + OR bill IS NULL OR bill <= $billtime OR ( expire IS NOT NULL AND expire <= $^T ) OR ( adjourn IS NOT NULL AND adjourn <= $^T ) ) diff --git a/FS/FS/Misc/DateTime.pm b/FS/FS/Misc/DateTime.pm index a32c15aea..e36f3a95a 100644 --- a/FS/FS/Misc/DateTime.pm +++ b/FS/FS/Misc/DateTime.pm @@ -2,12 +2,13 @@ package FS::Misc::DateTime; use base qw( Exporter ); use vars qw( @EXPORT_OK ); +use POSIX; use Carp; use Date::Parse; use DateTime::Format::Natural; use FS::Conf; -@EXPORT_OK = qw( parse_datetime ); +@EXPORT_OK = qw( parse_datetime day_end ); =head1 NAME @@ -55,6 +56,28 @@ sub parse_datetime { } +=item day_end TIME + +If the next-bill-ignore-time configuration setting is turned off, just +returns the passed-in value. + +If the next-bill-ignore-time configuration setting is turned on, parses TIME +as an integer UNIX timestamp and returns a new timestamp with the same date but +23:59:59 for the time. + +=cut + +sub day_end { + my $time = shift; + + my $conf = new FS::Conf; + return $time unless $conf->exists('next-bill-ignore-time'); + + my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = + localtime($time); + mktime(59,59,23,$mday,$mon,$year,$wday,$yday,$isdst); +} + =back =head1 BUGS diff --git a/FS/FS/cust_main/Billing.pm b/FS/FS/cust_main/Billing.pm index 20986b0c8..0b4210904 100644 --- a/FS/FS/cust_main/Billing.pm +++ b/FS/FS/cust_main/Billing.pm @@ -7,6 +7,7 @@ use Data::Dumper; use List::Util qw( min ); use FS::UID qw( dbh ); use FS::Record qw( qsearch qsearchs dbdef ); +use FS::Misc::DateTime qw( day_end ); use FS::cust_bill; use FS::cust_bill_pkg; use FS::cust_bill_pkg_display; @@ -20,7 +21,6 @@ use FS::cust_bill_pkg_tax_rate_location; use FS::part_event; use FS::part_event_condition; use FS::pkg_category; -use POSIX; # 1 is mostly method/subroutine entry and options # 2 traces progress of some operations @@ -113,7 +113,7 @@ sub bill_and_collect { my $job = $options{'job'}; $job->update_statustext('0,cleaning expired packages') if $job; - $error = $self->cancel_expired_pkgs( $self->day_end( $options{actual_time} ) ); + $error = $self->cancel_expired_pkgs( day_end( $options{actual_time} ) ); if ( $error ) { $error = "Error expiring custnum ". $self->custnum. ": $error"; if ( $options{fatal} && $options{fatal} eq 'return' ) { return $error; } @@ -121,7 +121,7 @@ sub bill_and_collect { else { warn $error; } } - $error = $self->suspend_adjourned_pkgs( $self->day_end( $options{actual_time} ) ); + $error = $self->suspend_adjourned_pkgs( day_end( $options{actual_time} ) ); if ( $error ) { $error = "Error adjourning custnum ". $self->custnum. ": $error"; if ( $options{fatal} && $options{fatal} eq 'return' ) { return $error; } @@ -165,19 +165,6 @@ sub bill_and_collect { } -sub day_end { - # XXX: sometimes "incorrect" if crossing DST boundaries? - - my $self = shift; - my $time = shift; - - return $time unless $conf->exists('next-bill-ignore-time'); - - my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = - localtime($time); - mktime(59,59,23,$mday,$mon,$year,$wday,$yday,$isdst); -} - sub cancel_expired_pkgs { my ( $self, $time, %options ) = @_; @@ -882,7 +869,7 @@ sub _make_lines { and ( $options{'resetup'} || ( ! $cust_pkg->setup && ( ! $cust_pkg->start_date - || $cust_pkg->start_date <= $self->day_end($time) + || $cust_pkg->start_date <= day_end($time) ) && ( ! $conf->exists('disable_setup_suspended_pkgs') || ( $conf->exists('disable_setup_suspended_pkgs') && @@ -927,7 +914,7 @@ sub _make_lines { if ( ! $cust_pkg->start_date and ( ! $cust_pkg->susp || $part_pkg->option('suspend_bill', 1) ) and - ( $part_pkg->freq ne '0' && ( $cust_pkg->bill || 0 ) <= $self->day_end($time) ) + ( $part_pkg->freq ne '0' && ( $cust_pkg->bill || 0 ) <= day_end($time) ) || ( $part_pkg->plan eq 'voip_cdr' && $part_pkg->option('bill_every_call') ) @@ -951,7 +938,7 @@ sub _make_lines { #over two params! lets at least switch to a hashref for the rest... my $increment_next_bill = ( $part_pkg->freq ne '0' - && ( $cust_pkg->getfield('bill') || 0 ) <= $self->day_end($time) + && ( $cust_pkg->getfield('bill') || 0 ) <= day_end($time) && !$options{cancel} ); my %param = ( 'precommit_hooks' => $precommit_hooks, -- 2.11.0