2 my $curuser = $FS::CurrentUser::CurrentUser;
5 unless $curuser->access_right('Edit customer package dates')
6 or $curuser->access_right('Change package contract end date');
10 my $contract_only = $curuser->access_right('Edit customer package dates') ? 0 : 1;
11 $contract_only = 1 if $arg{'contract_only'};
13 my $pkgnum = $arg{'pkgnum'};
14 $pkgnum =~ /^\d+$/ or die "bad pkgnum '$pkgnum'";
15 my $cust_pkg = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
16 my %hash = $cust_pkg->hash;
17 foreach ( $contract_only ? qw( contract_end ) : qw( start_date setup bill last_bill contract_end )) {
18 # adjourn, expire, resume not editable this way
19 if( $arg{$_} =~ /^\d+$/ ) {
21 } elsif ( $arg{$_} ) {
22 $hash{$_} = parse_datetime($arg{$_});
28 my (@changes, @confirm, @errors);
30 my $part_pkg = $cust_pkg->part_pkg;
31 my @supp_pkgs = $cust_pkg->supplemental_pkgs;
32 my $main_pkg = $cust_pkg->main_pkg;
34 my $conf = FS::Conf->new;
35 my $date_format = $conf->config('date_format') || '%b %o, %Y';
37 if ( $hash{'start_date'} != $cust_pkg->get('start_date') and !$hash{'setup'} ) {
39 $start = time2str($date_format, $hash{'start_date'}) if $hash{'start_date'};
40 my $text = 'Set this package';
42 $text .= ' and all its supplemental packages';
44 $text .= ' to start billing';
47 push @changes, mt($text, $start);
49 $text .= ' immediately.';
50 push @changes, mt($text);
56 if ( $hash{'setup'} != $cust_pkg->get('setup') ) {
57 my $setup = time2str($date_format, $hash{'setup'});
58 my $has_setup_fee = grep { $_->part_pkg->option('setup_fee',1) > 0 }
59 $cust_pkg, @supp_pkgs;
60 if ( !$hash{'setup'} ) {
61 my $text = 'Remove the setup date';
62 $text .= ' from this and all its supplemental packages' if @supp_pkgs;
64 push @changes, mt($text);
65 if ( $has_setup_fee ) {
66 push @confirm, mt('This will re-charge the customer for the setup fee.');
70 } elsif ( $hash{'setup'} and !$cust_pkg->get('setup') ) {
71 my $text = 'Add a setup date of [_1]';
72 $text .= ' to this and all its supplemental packages' if @supp_pkgs;
74 push @changes, mt($text, $setup);
75 if ( $has_setup_fee ) {
76 push @confirm, mt('This will prevent charging the setup fee.');
81 my $text = 'Set the setup date to [_1]';
82 $text .= ' on this and all its supplemental packages' if @supp_pkgs;
84 push @changes, mt($text, $setup);
89 # Check for start date + setup date
90 if ( $hash{'start_date'} and $hash{'setup'} ) {
91 if ( $cust_pkg->get('setup') ) {
92 push @errors, mt('Since the package has already started billing, it '.
93 'cannot have a start date.');
95 push @errors, mt('You cannot set both a start date and a setup date on '.
100 # Last bill date change
101 if ( $hash{'last_bill'} != $cust_pkg->get('last_bill') ) {
102 my $last_bill = time2str($date_format, $hash{'last_bill'});
103 my $name = 'last bill date';
104 $name = 'last renewal date' if $part_pkg->is_prepaid;
105 if ( $hash{'last_bill'} ) {
106 push @changes, mt('Set the [_1] to [_2].', $name, $last_bill);
108 push @changes, mt('Remove the [_1].', $name);
111 # I don't think we want to adjust this on supplemental packages.
115 if ( $hash{'bill'} != $cust_pkg->get('bill') ) {
116 my $bill = time2str($date_format, $hash{'bill'});
117 $bill = 'today' if !$hash{'bill'}; # or 'the end of today'?...
118 my $name = 'next bill date';
119 $name = 'end of the prepaid period' if $part_pkg->is_prepaid;
120 push @changes, mt('Set the [_1] to [_2].', $name, $bill);
122 if ( $hash{'bill'} < time and $hash{'bill'} ) {
124 mt('The customer will be charged for the interval from [_1] until now.',
126 } elsif ( !$hash{'bill'} and ($hash{'last_bill'} or $hash{'setup'}) ) {
128 time2str($date_format, $hash{'last_bill'} || $hash{'setup'});
130 mt('The customer will be charged for the interval from [_1] until now.',
138 if ( $cust_pkg->get('bill') and $hash{'bill'} ) {
139 # the package already has a bill date, so adjust the dates
140 # of supplementals by the same interval
141 my $diff = $hash{'bill'} - $cust_pkg->get('bill');
142 my $sign = $diff < 0 ? -1 : 1;
143 $diff = $diff * $sign / 86400;
145 $diff = mt('[quant,_1,hour]', int($diff * 24));
147 $diff = mt('[quant,_1,day]', int($diff));
150 mt('[_1] supplemental package will also be billed [_2] [_3].',
151 (@supp_pkgs > 1 ? 'Each' : 'The'),
153 ($sign > 0 ? 'later' : 'earlier')
156 # the package hasn't been billed yet, or you've set bill = null
158 mt('[_1] supplemental package will also be billed on [_2].',
159 (@supp_pkgs > 1 ? 'Each' : 'The'),
168 mt('This package is a supplemental package. The bill date of its '.
169 'main package will not be adjusted.');
173 # Contract end change
174 if ( $hash{'contract_end'} != $cust_pkg->get('contract_end') ) {
175 if ( $hash{'contract_end'} ) {
176 my $contract_end = time2str($date_format, $hash{'contract_end'});
178 mt('Set this package\'s contract end date to [_1]', $contract_end);
180 push @changes, mt('Remove this package\'s contract end date.');
183 my $text = 'This change will also apply to ' .
185 'all supplemental packages.':
186 'the supplemental package.');
187 push @confirm, mt($text);
195 $title = 'Error changing package dates';
197 $title = 'Confirm date changes';
200 <& /elements/header-popup.html, { title => $title, etc => 'BGCOLOR=""' } &>
201 <STYLE TYPE="text/css">
207 .confirm { color: #ff0000 }
215 <DIV STYLE="text-align: center; padding:1em">
216 <% emt('Package #') %><B><% $pkgnum %></B>: <B><% $cust_pkg->part_pkg->pkg %></B><BR>
218 <% emt('The following changes will be made:') %>
220 <% emt('No changes will be made.') %>
225 % foreach my $error ( @errors ) {
227 <TD><IMG SRC="<%$p%>images/cross.png"></TD>
228 <TD CLASS="error"><% $error %></TD>
232 % while (@changes, @confirm) {
233 % my $text = shift @changes;
234 % if (length $text) {
236 <TD><IMG SRC="<%$p%>images/tick.png"></TD>
240 % $text = shift @confirm;
241 % if (length $text) {
244 <INPUT TYPE="checkbox" NAME="areyousure" VALUE=1 onclick="submit_ready()">
246 <TD CLASS="confirm"><% $text %></TD>
253 <DIV CLASS="button-container">
254 <BUTTON TYPE="button" STYLE="width:145px" ID="submit_cancel"\
255 onclick="submit_cancel()">
256 <IMG SRC="<%$p%>images/cross.png" ALT=""> Cancel
259 <BUTTON TYPE="button" STYLE="width:145px" ID="submit_continue"\
260 onclick="submit_continue()">
261 <IMG SRC="<%$p%>images/tick.png" ALT=""> Continue
265 <FORM NAME="DateEditForm" STYLE="display:none" TARGET="_parent" ACTION="<%$p%>edit/process/REAL_cust_pkg.cgi" METHOD="POST">
266 % foreach (keys %hash) {
267 <INPUT TYPE="hidden" NAME="<%$_%>" VALUE="<% $hash{$_} |h%>">
269 % if ($contract_only) {
270 <INPUT TYPE="hidden" NAME="contract_only" VALUE="1">
274 function submit_ready() {
276 var checkboxes = document.getElementsByName('areyousure');
278 for (i=0; i < checkboxes.length; i++) {
279 if (! checkboxes[i].checked ) {
283 document.getElementById('submit_continue').disabled = !ready;
286 function submit_cancel() {
289 function submit_continue() {
290 if ( submit_ready() ) {
291 document.forms.DateEditForm.submit();
296 <& /elements/footer.html &>