From: Mark Wells Date: Sun, 16 Oct 2016 04:06:04 +0000 (-0700) Subject: Merge branch 'master' of git.freeside.biz:/home/git/freeside X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=commitdiff_plain;h=6a509099343ed155525c4304f1ad742cc6e4ce59;hp=d4114381c5d95e8acd0d0fc2bbc2b3528bde2ecf Merge branch 'master' of git.freeside.biz:/home/git/freeside --- diff --git a/FS/FS/Schema.pm b/FS/FS/Schema.pm index a1615b71a..f8b82f454 100644 --- a/FS/FS/Schema.pm +++ b/FS/FS/Schema.pm @@ -1641,6 +1641,7 @@ sub tables_hashref { 'accountcode_cdr', 'char', 'NULL', 1, '', '', 'billday', 'int', 'NULL', '', '', '', 'prorate_day', 'int', 'NULL', '', '', '', + 'force_prorate_day', 'char', 'NULL', 1, '', '', 'edit_subject', 'char', 'NULL', 1, '', '', 'locale', 'varchar', 'NULL', 16, '', '', 'calling_list_exempt', 'char', 'NULL', 1, '', '', diff --git a/FS/FS/cust_main.pm b/FS/FS/cust_main.pm index 11d776393..9c8e37499 100644 --- a/FS/FS/cust_main.pm +++ b/FS/FS/cust_main.pm @@ -1775,6 +1775,7 @@ sub check { || $self->ut_floatn('credit_limit') || $self->ut_numbern('billday') || $self->ut_numbern('prorate_day') + || $self->ut_flag('force_prorate_day') || $self->ut_flag('edit_subject') || $self->ut_flag('calling_list_exempt') || $self->ut_flag('invoice_noemail') diff --git a/FS/FS/part_event.pm b/FS/FS/part_event.pm index 58e01271c..1c2389989 100644 --- a/FS/FS/part_event.pm +++ b/FS/FS/part_event.pm @@ -6,6 +6,7 @@ use vars qw( $DEBUG ); use Carp qw(confess); use FS::Record qw( dbh qsearch qsearchs ); use FS::Conf; +use FS::Cursor; use FS::part_event_option; use FS::part_event_condition; use FS::cust_event; @@ -251,10 +252,28 @@ but can be useful when configuring events. =cut -sub targets { +sub targets { # may want to cursor this also my $self = shift; my %opt = @_; - my $time = $opt{'time'} || time; + my $time = $opt{'time'} ||= time; + + my $query = $self->_target_query(%opt); + my @objects = qsearch($query); + my @tested_objects; + foreach my $object ( @objects ) { + my $cust_event = $self->new_cust_event($object, 'time' => $time); + next unless $cust_event->test_conditions; + + $object->set('cust_event', $cust_event); + push @tested_objects, $object; + } + @tested_objects; +} + +sub _target_query { + my $self = shift; + my %opt = @_; + my $time = $opt{'time'}; my $eventpart = $self->eventpart; $eventpart =~ /^\d+$/ or die "bad eventpart $eventpart"; @@ -285,23 +304,15 @@ sub targets { # and don't enforce disabled because we want to be able to see targets # for a disabled event - my @objects = qsearch({ + { table => $eventtable, hashref => {}, addl_from => $join, extra_sql => "WHERE $where", - }); - my @tested_objects; - foreach my $object ( @objects ) { - my $cust_event = $self->new_cust_event($object, 'time' => $time); - next unless $cust_event->test_conditions; - - $object->set('cust_event', $cust_event); - push @tested_objects, $object; - } - @tested_objects; + }; } + =item initialize PARAMS Identify all objects eligible for this event and create L @@ -323,26 +334,26 @@ sub initialize { my $self = shift; my $error; - my $oldAutoCommit = $FS::UID::AutoCommit; - local $FS::UID::AutoCommit = 0; - my $dbh = dbh; + my $time = time; + + local $FS::UID::AutoCommit = 1; + my $cursor = FS::Cursor->new( $self->_target_query('time' => $time) ); + while (my $object = $cursor->fetch) { + + my $cust_event = $self->new_cust_event($object, 'time' => $time); + next unless $cust_event->test_conditions; - my @objects = $self->targets; - foreach my $object ( @objects ) { - my $cust_event = $object->get('cust_event'); $cust_event->status('initial'); $error = $cust_event->insert; - last if $error; + die $error if $error; } - if ( !$error and $self->disabled ) { + + # on successful completion only, re-enable the event + if ( $self->disabled ) { $self->disabled(''); $error = $self->replace; + die $error if $error; } - if ( $error ) { - $dbh->rollback; - return $error; - } - $dbh->commit if $oldAutoCommit; return; } diff --git a/FS/FS/part_pkg/flat.pm b/FS/FS/part_pkg/flat.pm index 97d4363da..504def0cf 100644 --- a/FS/FS/part_pkg/flat.pm +++ b/FS/FS/part_pkg/flat.pm @@ -173,6 +173,12 @@ sub calc_recur { sub cutoff_day { my $self = shift; my $cust_pkg = shift; + my $cust_main = $cust_pkg->cust_main; + # force it to act like a prorate package, is what this means + # because we made a distinction once between prorate and flat packages + if ( $cust_main->force_prorate_day and $cust_main->prorate_day ) { + return ( $cust_main->prorate_day ); + } if ( $self->option('sync_bill_date',1) ) { my $next_bill = $cust_pkg->cust_main->next_bill_date; if ( $next_bill ) { diff --git a/FS/FS/part_pkg/prorate_calendar.pm b/FS/FS/part_pkg/prorate_calendar.pm index c50cae0d7..a8ed8f942 100644 --- a/FS/FS/part_pkg/prorate_calendar.pm +++ b/FS/FS/part_pkg/prorate_calendar.pm @@ -72,7 +72,11 @@ sub check { sub cutoff_day { my( $self, $cust_pkg ) = @_; my @periods = @{ $freq_cutoff_days{$self->freq} }; - my @cutoffs = ($self->option('cutoff_day') || 1); # Jan 1 = 1 + my $prorate_day = $cust_pkg->cust_main->prorate_day + || $self->option('cutoff_day') + || 1; + + my @cutoffs = ($prorate_day); pop @periods; # we don't care about the last one foreach (@periods) { push @cutoffs, $cutoffs[-1] + $_; diff --git a/FS/FS/part_pkg/recur_Common.pm b/FS/FS/part_pkg/recur_Common.pm index b73c62c25..4ed83a46b 100644 --- a/FS/FS/part_pkg/recur_Common.pm +++ b/FS/FS/part_pkg/recur_Common.pm @@ -41,13 +41,14 @@ sub cutoff_day { # prorate/subscription only; we don't support sync_bill_date here my( $self, $cust_pkg ) = @_; my $recur_method = $self->option('recur_method',1) || 'anniversary'; - return () unless $recur_method eq 'prorate' - || $recur_method eq 'subscription'; + my $cust_main = $cust_pkg->cust_main; - #false laziness w/prorate.pm::cutoff_day - my $prorate_day = $cust_pkg->cust_main->prorate_day; - $prorate_day ? ( $prorate_day ) - : split(/\s*,\s*/, $self->option('cutoff_day', 1) || '1'); + if ( $cust_main->force_prorate_day and $cust_main->prorate_day ) { + return ( $cust_main->prorate_day ); + } elsif ($recur_method eq 'prorate' || $recur_method eq 'subscription') { + + return split(/\s*,\s*/, $self->option('cutoff_day', 1) || '1'); + } } sub calc_recur_Common { diff --git a/FS/FS/part_pkg/subscription.pm b/FS/FS/part_pkg/subscription.pm index 0dfe049fe..bf644d48c 100644 --- a/FS/FS/part_pkg/subscription.pm +++ b/FS/FS/part_pkg/subscription.pm @@ -88,6 +88,11 @@ use FS::part_pkg::flat; sub calc_recur { my($self, $cust_pkg, $sdate, $details, $param ) = @_; my $cutoff_day = $self->option('cutoff_day', 1) || 1; + my $cust_main = $cust_pkg->cust_main; + if ( $cust_main->force_prorate_day and $cust_main->prorate_day ) { + $cutoff_day = $cust_main->prorate_day; + } + my $mnow = $$sdate; my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($mnow) )[0,1,2,3,4,5]; diff --git a/bin/initialize-event b/bin/initialize-event new file mode 100755 index 000000000..f186e195f --- /dev/null +++ b/bin/initialize-event @@ -0,0 +1,68 @@ +#!/usr/bin/perl + +use FS::Misc::Getopt; +use FS::part_event; +use FS::cust_event; +use FS::Record 'dbdef'; +use FS::Cursor; + +getopts('e:x'); + +my $eventpart = $opt{e}; +my $part_event = FS::part_event->by_key($opt{e}) + or die "usage: initialize-event -e \n"; + + +my $eventtable = $part_event->eventtable; +my $pkey = dbdef->table($eventtable)->primary_key; +my $from = " LEFT JOIN (SELECT DISTINCT tablenum AS $pkey FROM cust_event + WHERE eventpart = $eventpart) AS done USING ($pkey)", +my $where = " WHERE done.$pkey IS NULL"; + +my $count = FS::Record->scalar_sql("SELECT COUNT(*) FROM $eventtable $from $where"); +print "Event ".$part_event->event."\n". + "Will initialize on $count $eventtable records.\n"; +if (!$opt{x}) { + print "Run with -x to make changes.\n"; + exit; +} + + +print "Disabling event.\n"; +$part_event->disabled('Y'); +my $error = $part_event->replace; +die $error if $error; +my $cursor = FS::Cursor->new({ + table => $eventtable, + addl_from => $from, + extra_sql => $where, +}); +my $user = $FS::CurrentUser::CurrentUser->username; +my $statustext = "Manually by $user"; +while (my $record = $cursor->fetch) { + my $cust_event = FS::cust_event->new({ + status => 'initial', + eventpart => $eventpart, + tablenum => $record->get($pkey), + _date => $^T, + statustext => $statustext, + }); + $error = $cust_event->insert; + if ($error) { + print "$eventtable #".$record->get($pkey).": $error\n" if $error; + } else { + $count--; + } +} +print "$count unprocessed records."; +if ($count == 0) { + print "Re-enabling event.\n"; + $part_event->disabled(''); + $error = $part_event->replace; + die $error if $error; +} else { + print "Event is still disabled.\n"; +} + +print "Finished.\n"; + diff --git a/httemplate/edit/cust_main/billing.html b/httemplate/edit/cust_main/billing.html index 50262e82c..649c4c945 100644 --- a/httemplate/edit/cust_main/billing.html +++ b/httemplate/edit/cust_main/billing.html @@ -124,6 +124,12 @@ + <& /elements/checkbox.html, + field => 'force_prorate_day', + value => 'Y', + curr_value => $cust_main->force_prorate_day + &> + diff --git a/httemplate/elements/freeside.css b/httemplate/elements/freeside.css index 8545ee596..c98fdcbc5 100644 --- a/httemplate/elements/freeside.css +++ b/httemplate/elements/freeside.css @@ -232,7 +232,8 @@ div.fstabcontainer { border-radius: .25em; } -.fsinnerbox th { +.fsinnerbox th, +.fsinnerbox label { font-weight:normal; font-size:80%; vertical-align: top; diff --git a/httemplate/view/cust_main/billing.html b/httemplate/view/cust_main/billing.html index 7ee05a357..8d3925a08 100644 --- a/httemplate/view/cust_main/billing.html +++ b/httemplate/view/cust_main/billing.html @@ -83,6 +83,7 @@ set_display_recurring(<% encode_json({'display_recurring' => [ $cust_main->displ <% mt('Prorate day of month') |h %> <% $cust_main->prorate_day %> + <% $cust_main->force_prorate_day && (''.emt('(applies to all packages)').'') %> % }