-<%init>
-my $curuser = $FS::CurrentUser::CurrentUser;
-
-die "access denied"
- unless $curuser->access_right('Edit customer package dates');
-
-my %arg = $cgi->Vars;
-
-my $pkgnum = $arg{'pkgnum'};
-$pkgnum =~ /^\d+$/ or die "bad pkgnum '$pkgnum'";
-my $cust_pkg = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
-my %hash = $cust_pkg->hash;
-foreach (qw( start_date setup bill last_bill contract_end )) {
- # adjourn, expire, resume not editable this way
- if( $arg{$_} =~ /^\d+$/ ) {
- $hash{$_} = $arg{$_};
- } elsif ( $arg{$_} ) {
- $hash{$_} = parse_datetime($arg{$_});
- } else {
- $hash{$_} = '';
- }
-}
-
-my (@changes, @confirm, @errors);
-
-my $part_pkg = $cust_pkg->part_pkg;
-my @supp_pkgs = $cust_pkg->supplemental_pkgs;
-my $main_pkg = $cust_pkg->main_pkg;
-
-my $conf = FS::Conf->new;
-my $date_format = $conf->config('date_format') || '%b %o, %Y';
-# Start date
-if ( $hash{'start_date'} != $cust_pkg->get('start_date') and !$hash{'setup'} ) {
- my $start = '';
- $start = time2str($date_format, $hash{'start_date'}) if $hash{'start_date'};
- my $text = 'Set this package';
- if ( @supp_pkgs ) {
- $text .= ' and all its supplemental packages';
- }
- $text .= ' to start billing';
- if ( $start ) {
- $text .= ' on [_1].';
- push @changes, mt($text, $start);
- } else {
- $text .= ' immediately.';
- push @changes, mt($text);
- }
- push @confirm, '';
-}
-
-# Setup date changes
-if ( $hash{'setup'} != $cust_pkg->get('setup') ) {
- my $setup = time2str($date_format, $hash{'setup'});
- my $has_setup_fee = grep { $_->part_pkg->option('setup_fee',1) > 0 }
- $cust_pkg, @supp_pkgs;
- if ( !$hash{'setup'} ) {
- my $text = 'Remove the setup date';
- $text .= ' from this and all its supplemental packages' if @supp_pkgs;
- $text .= '.';
- push @changes, mt($text);
- if ( $has_setup_fee ) {
- push @confirm, mt('This will re-charge the customer for the setup fee.');
- } else {
- push @confirm, '';
- }
- } elsif ( $hash{'setup'} and !$cust_pkg->get('setup') ) {
- my $text = 'Add a setup date of [_1]';
- $text .= ' to this and all its supplemental packages' if @supp_pkgs;
- $text .= '.';
- push @changes, mt($text, $setup);
- if ( $has_setup_fee ) {
- push @confirm, mt('This will prevent charging the setup fee.');
- } else {
- push @confirm, '';
- }
- } else {
- my $text = 'Set the setup date to [_1]';
- $text .= ' on this and all its supplemental packages' if @supp_pkgs;
- $text .= '.';
- push @changes, mt($text, $setup);
- push @confirm, '';
- }
-}
-
-# Check for start date + setup date
-if ( $hash{'start_date'} and $hash{'setup'} ) {
- if ( $cust_pkg->get('setup') ) {
- push @errors, mt('Since the package has already started billing, it '.
- 'cannot have a start date.');
- } else {
- push @errors, mt('You cannot set both a start date and a setup date on '.
- 'the same package.');
- }
-}
-
-# Last bill date change
-if ( $hash{'last_bill'} != $cust_pkg->get('last_bill') ) {
- my $last_bill = time2str($date_format, $hash{'last_bill'});
- my $name = 'last bill date';
- $name = 'last renewal date' if $part_pkg->is_prepaid;
- if ( $hash{'last_bill'} ) {
- push @changes, mt('Set the [_1] to [_2].', $name, $last_bill);
- } else {
- push @changes, mt('Remove the [_1].', $name);
- }
- push @confirm, '';
- # I don't think we want to adjust this on supplemental packages.
-}
-
-# Bill date change
-if ( $hash{'bill'} != $cust_pkg->get('bill') ) {
- my $bill = time2str($date_format, $hash{'bill'});
- $bill = 'today' if !$hash{'bill'}; # or 'the end of today'?...
- my $name = 'next bill date';
- $name = 'end of the prepaid period' if $part_pkg->is_prepaid;
- push @changes, mt('Set the [_1] to [_2].', $name, $bill);
-
- if ( $hash{'bill'} < time and $hash{'bill'} ) {
- push @confirm,
- mt('The customer will be charged for the interval from [_1] until now.',
- $bill);
- } elsif ( !$hash{'bill'} and ($hash{'last_bill'} or $hash{'setup'}) ) {
- my $last_bill =
- time2str($date_format, $hash{'last_bill'} || $hash{'setup'});
- push @confirm,
- mt('The customer will be charged for the interval from [_1] until now.',
- $last_bill);
- } else {
- push @confirm, '';
- }
-
- if ( @supp_pkgs ) {
- push @changes, '';
- if ( $cust_pkg->get('bill') and $hash{'bill'} ) {
- # the package already has a bill date, so adjust the dates
- # of supplementals by the same interval
- my $diff = $hash{'bill'} - $cust_pkg->get('bill');
- my $sign = $diff < 0 ? -1 : 1;
- $diff = $diff * $sign / 86400;
- if ( $diff < 1 ) {
- $diff = mt('[quant,_1,hour]', int($diff * 24));
- } else {
- $diff = mt('[quant,_1,day]', int($diff));
- }
- push @confirm,
- mt('[_1] supplemental package will also be billed [_2] [_3].',
- (@supp_pkgs > 1 ? 'Each' : 'The'),
- $diff,
- ($sign > 0 ? 'later' : 'earlier')
- );
- } else {
- # the package hasn't been billed yet, or you've set bill = null
- push @confirm,
- mt('[_1] supplemental package will also be billed on [_2].',
- (@supp_pkgs > 1 ? 'Each' : 'The'),
- $bill
- );
- }
- } #if @supp_pkgs
-
- if ( $main_pkg ) {
- push @changes, '';
- push @confirm,
- mt('This package is a supplemental package. The bill date of its '.
- 'main package will not be adjusted.');
- }
-}
-
-# Contract end change
-if ( $hash{'contract_end'} != $cust_pkg->get('contract_end') ) {
- if ( $hash{'contract_end'} ) {
- my $contract_end = time2str($date_format, $hash{'contract_end'});
- push @changes,
- mt('Set this package\'s contract end date to [_1]', $contract_end);
- } else {
- push @changes, mt('Remove this package\'s contract end date.');
- }
- if ( @supp_pkgs ) {
- my $text = 'This change will also apply to ' .
- (@supp_pkgs > 1 ?
- 'all supplemental packages.':
- 'the supplemental package.');
- push @confirm, mt($text);
- } else {
- push @confirm, '';
- }
-}
-
-my $title = '';
-if ( @errors ) {
- $title = 'Error changing package dates';
-} else {
- $title = 'Confirm date changes';
-}
-</%init>
-<& /elements/header-popup.html, { title => $title, etc => 'BGCOLOR=""' } &>
-<STYLE TYPE="text/css">
-.error {
- color: #ff0000;
- font-weight: bold;
- text-align: center;
-}
-.confirm { color: #ff0000 }
-.button-container {
- position: fixed;
- bottom: 5px;
- text-align: center;
- width: 100%
-}
-</STYLE>
-<DIV STYLE="text-align: center; padding:1em">
-<% emt('Package #') %><B><% $pkgnum %></B>: <B><% $cust_pkg->part_pkg->pkg %></B><BR>
-% if ( @changes ) {
- <% emt('The following changes will be made:') %>
-% } else {
- <% emt('No changes will be made.') %>
-% }
-</DIV>
-<TABLE WIDTH="100%">
-% if ( @errors ) {
-% foreach my $error ( @errors ) {
-<TR>
- <TD><IMG SRC="<%$p%>images/cross.png"></TD>
- <TD CLASS="error"><% $error %></TD>
-</TR>
-% }
-% } else {
-% while (@changes, @confirm) {
-% my $text = shift @changes;
-% if (length $text) {
-<TR>
- <TD><IMG SRC="<%$p%>images/tick.png"></TD>
- <TD><% $text %></TD>
-</TR>
-% }
-% $text = shift @confirm;
-% if (length $text) {
-<TR>
- <TD>
- <INPUT TYPE="checkbox" NAME="areyousure" VALUE=1 onclick="submit_ready()">
- </TD>
- <TD CLASS="confirm"><% $text %></TD>
-</TR>
-% }
-% }
-% }
-</TABLE>
-%# action buttons
-<DIV CLASS="button-container">
- <BUTTON TYPE="button" STYLE="width:145px" ID="submit_cancel"\
- onclick="submit_cancel()">
- <IMG SRC="<%$p%>images/cross.png" ALT=""> Cancel
- </BUTTON>
-% if (!@errors ) {
- <BUTTON TYPE="button" STYLE="width:145px" ID="submit_continue"\
- onclick="submit_continue()">
- <IMG SRC="<%$p%>images/tick.png" ALT=""> Continue
- </BUTTON>
-</DIV>
-% }
-<FORM NAME="DateEditForm" STYLE="display:none" TARGET="_parent" ACTION="<%$p%>edit/process/REAL_cust_pkg.cgi" METHOD="POST">
-% foreach (keys %hash) {
-<INPUT TYPE="hidden" NAME="<%$_%>" VALUE="<% $hash{$_} |h%>">
-% }
-</FORM>
-<SCRIPT>
-function submit_ready() {
- var ready = true;
- var checkboxes = document.getElementsByName('areyousure');
- var i;
- for (i=0; i < checkboxes.length; i++) {
- if (! checkboxes[i].checked ) {
- ready = false;
- }
- }
- document.getElementById('submit_continue').disabled = !ready;
- return ready;
-}
-function submit_cancel() {
- parent.nd(1);
-}
-function submit_continue() {
- if ( submit_ready() ) {
- document.forms.DateEditForm.submit();
- }
-}
-submit_ready();
-</SCRIPT>
-<& /elements/footer.html &>