From d0fc25693dd91869c6e1fe1372bcae35fde1a827 Mon Sep 17 00:00:00 2001 From: Mark Wells Date: Thu, 31 Dec 2015 17:44:04 -0800 Subject: [PATCH] allow back-billing by changing start dates, #38883 --- FS/FS/AccessRight.pm | 1 + FS/FS/access_right.pm | 1 + FS/FS/cust_main/Billing.pm | 13 ++-- httemplate/misc/change_pkg_start.html | 99 ++++++++++++++++++++++++++ httemplate/misc/process/change_pkg_start.html | 53 ++++++++++++++ httemplate/view/cust_main/packages/status.html | 29 +++++++- 6 files changed, 191 insertions(+), 5 deletions(-) create mode 100755 httemplate/misc/change_pkg_start.html create mode 100755 httemplate/misc/process/change_pkg_start.html diff --git a/FS/FS/AccessRight.pm b/FS/FS/AccessRight.pm index 51e47adaf..a24d736ee 100644 --- a/FS/FS/AccessRight.pm +++ b/FS/FS/AccessRight.pm @@ -152,6 +152,7 @@ tie my %rights, 'Tie::IxHash', 'View appointments', #NEWNEW 'Make appointment', 'View package definition costs', #NEWNEW + 'Change package start date', ], ### diff --git a/FS/FS/access_right.pm b/FS/FS/access_right.pm index 0da718c64..57f67ded3 100644 --- a/FS/FS/access_right.pm +++ b/FS/FS/access_right.pm @@ -253,6 +253,7 @@ sub _upgrade_data { # class method 'Generate quotation' => 'Disable quotation', 'Add on-the-fly void credit reason' => 'Add on-the-fly void reason', '_ALL' => 'Employee preference telephony integration', + 'Edit customer package dates' => 'Change package start date', #4.x ); # foreach my $old_acl ( keys %onetime ) { diff --git a/FS/FS/cust_main/Billing.pm b/FS/FS/cust_main/Billing.pm index 29535f25c..1157ba98f 100644 --- a/FS/FS/cust_main/Billing.pm +++ b/FS/FS/cust_main/Billing.pm @@ -1042,10 +1042,15 @@ sub _make_lines { } } - $cust_pkg->setfield('setup', $time) - unless $cust_pkg->setup; - #do need it, but it won't get written to the db - #|| $cust_pkg->pkgpart != $real_pkgpart; + if ( $cust_pkg->get('setup') ) { + # don't change it + } elsif ( $cust_pkg->get('start_date') ) { + # this allows start_date to be used to set the first bill date + $cust_pkg->set('setup', $cust_pkg->get('start_date')); + } else { + # if unspecified, start it right now + $cust_pkg->set('setup', $time); + } $cust_pkg->setfield('start_date', '') if $cust_pkg->start_date; diff --git a/httemplate/misc/change_pkg_start.html b/httemplate/misc/change_pkg_start.html new file mode 100755 index 000000000..5a890c86e --- /dev/null +++ b/httemplate/misc/change_pkg_start.html @@ -0,0 +1,99 @@ +<& /elements/header-popup.html, mt($title) &> + +<& /elements/error.html &> + +% # only slightly different from unhold_pkg. +
+ + +
+<% emt('Start billing [_1]', $part_pkg->pkg_comment(cust_pkg => $cust_pkg)) %> + + + +
+ + + +<%init> + +my $curuser = $FS::CurrentUser::CurrentUser; +die "access denied" + unless $curuser->access_right('Change package start date'); + +my $pkgnum; +if ( $cgi->param('pkgnum') =~ /^(\d+)$/ ) { + $pkgnum = $1; +} else { + die "illegal query ". $cgi->keywords; +} + +my $conf = new FS::Conf; +my $date_format = $conf->config('date_format') || '%m/%d/%Y'; + +my $title = 'Start billing package'; + +my $cust_pkg = qsearchs({ + table => 'cust_pkg', + addl_from => ' JOIN cust_main USING (custnum) ', + hashref => { 'pkgnum' => $pkgnum }, + extra_sql => ' AND '. $curuser->agentnums_sql, +}) or die "Unknown pkgnum: $pkgnum"; + +my $next_bill_date = $cust_pkg->cust_main->next_bill_date; + +my $part_pkg = $cust_pkg->part_pkg; + +# defaults: +# sticky on error, then the existing start date if any, then the customer's +# next bill date, and if none of those, default to now +my $when = $cgi->param('when'); + +if (!$when) { + if ($cust_pkg->start_date) { + $when = 'date'; + } elsif ($next_bill_date) { + $when = 'next_bill_date'; + } else { + $when = 'now'; + } +} + diff --git a/httemplate/misc/process/change_pkg_start.html b/httemplate/misc/process/change_pkg_start.html new file mode 100755 index 000000000..17a8518f9 --- /dev/null +++ b/httemplate/misc/process/change_pkg_start.html @@ -0,0 +1,53 @@ +<& /elements/header-popup.html &> + + + +<%init> + +my $curuser = $FS::CurrentUser::CurrentUser; +die "access denied" + unless $curuser->access_right('Change package start date'); + +$cgi->param('pkgnum') =~ /^(\d+)$/ + or die "illegal pkgnum"; +my $pkgnum = $1; + +my $cust_pkg = qsearchs({ + table => 'cust_pkg', + addl_from => ' JOIN cust_main USING (custnum) ', + hashref => { 'pkgnum' => $pkgnum }, + extra_sql => ' AND '. $curuser->agentnums_sql, +}) or die "Unknown pkgnum: $pkgnum"; + +my $cust_main = $cust_pkg->cust_main; + +my $error; +my $start_date; +if ( $cgi->param('when') eq 'now' ) { + # start it the next time billing runs + $start_date = ''; +} elsif ( $cgi->param('when') eq 'next_bill_date' ) { + $start_date = $cust_main->next_bill_date; +} elsif ( $cgi->param('when') eq 'date' ) { + $start_date = parse_datetime($cgi->param('start_date')); +} + +if ( $cust_pkg->setup ) { + # shouldn't happen + $error = 'This package has already started billing.'; +} else { + local $FS::UID::AutoCommit = 0; + foreach my $pkg ($cust_pkg, $cust_pkg->supplemental_pkgs) { + $pkg->set('start_date', $start_date); + $error ||= $pkg->replace; + } + $error ? dbh->rollback : dbh->commit; +} + +if ( $error ) { + $cgi->param('error', $error); + print $cgi->redirect($fsurl.'misc/change_pkg_start.html?', $cgi->query_string); +} + diff --git a/httemplate/view/cust_main/packages/status.html b/httemplate/view/cust_main/packages/status.html index a149562c2..1d3de85b7 100644 --- a/httemplate/view/cust_main/packages/status.html +++ b/httemplate/view/cust_main/packages/status.html @@ -175,7 +175,7 @@ % } -% } else { +% } else { # recurring package, not yet billed <% pkg_status_row_colspan($cust_pkg, emt("Not yet billed ($billed_or_prepaid [_1])", myfreq($part_pkg) ), '', %opt ) %> @@ -188,6 +188,21 @@ <% pkg_status_row_if($cust_pkg, emt('Start billing'), 'start_date', %opt) %> <% pkg_status_row_if($cust_pkg, emt('Un-cancelled'), 'uncancel', %opt ) %> +% if ( !$opt{no_links} +% and !$change_from +% and !$supplemental # can be changed from its main package +% and $curuser->access_right('Change package start date') ) +% { + + + > + + ( <% pkg_change_start_link($cust_pkg) %> ) + + + +% } + % } % % } else { #setup @@ -725,6 +740,18 @@ sub pkg_change_later_link { ) } +sub pkg_change_start_link { + my $cust_pkg = shift; + include( '/elements/popup_link-cust_pkg.html', + 'action' => $p . 'misc/change_pkg_start.html?', + 'label' => emt('Set start date'), + 'actionlabel' => emt('Set start of billing for'), + 'cust_pkg' => $cust_pkg, + 'width' => 510, + 'height' => 310, + ) +} + sub svc_recharge_link { include( '/elements/popup_link-cust_svc.html', 'action' => $p. 'misc/recharge_svc.html', -- 2.11.0