2 my $curuser = $FS::CurrentUser::CurrentUser;
5 unless $curuser->access_right('Edit customer package dates');
9 my $pkgnum = $arg{'pkgnum'};
10 $pkgnum =~ /^\d+$/ or die "bad pkgnum '$pkgnum'";
11 my $cust_pkg = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
12 my %hash = $cust_pkg->hash;
13 foreach (qw( start_date setup bill last_bill contract_end )) {
14 # adjourn, expire, resume not editable this way
15 if( $arg{$_} =~ /^\d+$/ ) {
17 } elsif ( $arg{$_} ) {
18 $hash{$_} = parse_datetime($arg{$_});
24 my (@changes, @confirm, @errors);
26 my $part_pkg = $cust_pkg->part_pkg;
27 my @supp_pkgs = $cust_pkg->supplemental_pkgs;
28 my $main_pkg = $cust_pkg->main_pkg;
30 my $conf = FS::Conf->new;
31 my $date_format = $conf->config('date_format') || '%b %o, %Y';
33 if ( $hash{'start_date'} != $cust_pkg->get('start_date') and !$hash{'setup'} ) {
35 $start = time2str($date_format, $hash{'start_date'}) if $hash{'start_date'};
36 my $text = 'Set this package';
38 $text .= ' and all its supplemental packages';
40 $text .= ' to start billing';
43 push @changes, mt($text, $start);
45 $text .= ' immediately.';
46 push @changes, mt($text);
52 if ( $hash{'setup'} != $cust_pkg->get('setup') ) {
53 my $setup = time2str($date_format, $hash{'setup'});
54 my $has_setup_fee = grep { $_->part_pkg->option('setup_fee',1) > 0 }
55 $cust_pkg, @supp_pkgs;
56 if ( !$hash{'setup'} ) {
57 my $text = 'Remove the setup date';
58 $text .= ' from this and all its supplemental packages' if @supp_pkgs;
60 push @changes, mt($text);
61 if ( $has_setup_fee ) {
62 push @confirm, mt('This will re-charge the customer for the setup fee.');
66 } elsif ( $hash{'setup'} and !$cust_pkg->get('setup') ) {
67 my $text = 'Add a setup date of [_1]';
68 $text .= ' to this and all its supplemental packages' if @supp_pkgs;
70 push @changes, mt($text, $setup);
71 if ( $has_setup_fee ) {
72 push @confirm, mt('This will prevent charging the setup fee.');
77 my $text = 'Set the setup date to [_1]';
78 $text .= ' on this and all its supplemental packages' if @supp_pkgs;
80 push @changes, mt($text, $setup);
85 # Check for start date + setup date
86 if ( $hash{'start_date'} and $hash{'setup'} ) {
87 if ( $cust_pkg->get('setup') ) {
88 push @errors, mt('Since the package has already started billing, it '.
89 'cannot have a start date.');
91 push @errors, mt('You cannot set both a start date and a setup date on '.
96 # Last bill date change
97 if ( $hash{'last_bill'} != $cust_pkg->get('last_bill') ) {
98 my $last_bill = time2str($date_format, $hash{'last_bill'});
99 my $name = 'last bill date';
100 $name = 'last renewal date' if $part_pkg->is_prepaid;
101 if ( $hash{'last_bill'} ) {
102 push @changes, mt('Set the [_1] to [_2].', $name, $last_bill);
104 push @changes, mt('Remove the [_1].', $name);
107 # I don't think we want to adjust this on supplemental packages.
111 if ( $hash{'bill'} != $cust_pkg->get('bill') ) {
112 my $bill = time2str($date_format, $hash{'bill'});
113 $bill = 'today' if !$hash{'bill'}; # or 'the end of today'?...
114 my $name = 'next bill date';
115 $name = 'end of the prepaid period' if $part_pkg->is_prepaid;
116 push @changes, mt('Set the [_1] to [_2].', $name, $bill);
118 if ( $hash{'bill'} < time and $hash{'bill'} ) {
120 mt('The customer will be charged for the interval from [_1] until now.',
122 } elsif ( !$hash{'bill'} and ($hash{'last_bill'} or $hash{'setup'}) ) {
124 time2str($date_format, $hash{'last_bill'} || $hash{'setup'});
126 mt('The customer will be charged for the interval from [_1] until now.',
134 if ( $cust_pkg->get('bill') and $hash{'bill'} ) {
135 # the package already has a bill date, so adjust the dates
136 # of supplementals by the same interval
137 my $diff = $hash{'bill'} - $cust_pkg->get('bill');
138 my $sign = $diff < 0 ? -1 : 1;
139 $diff = $diff * $sign / 86400;
141 $diff = mt('[quant,_1,hour]', int($diff * 24));
143 $diff = mt('[quant,_1,day]', int($diff));
146 mt('[_1] supplemental package will also be billed [_2] [_3].',
147 (@supp_pkgs > 1 ? 'Each' : 'The'),
149 ($sign > 0 ? 'later' : 'earlier')
152 # the package hasn't been billed yet, or you've set bill = null
154 mt('[_1] supplemental package will also be billed on [_2].',
155 (@supp_pkgs > 1 ? 'Each' : 'The'),
164 mt('This package is a supplemental package. The bill date of its '.
165 'main package will not be adjusted.');
169 # Contract end change
170 if ( $hash{'contract_end'} != $cust_pkg->get('contract_end') ) {
171 if ( $hash{'contract_end'} ) {
172 my $contract_end = time2str($date_format, $hash{'contract_end'});
174 mt('Set this package\'s contract end date to [_1]', $contract_end);
176 push @changes, mt('Remove this package\'s contract end date.');
179 my $text = 'This change will also apply to ' .
181 'all supplemental packages.':
182 'the supplemental package.');
183 push @confirm, mt($text);
191 $title = 'Error changing package dates';
193 $title = 'Confirm date changes';
196 <& /elements/header-popup.html, { title => $title, etc => 'BGCOLOR=""' } &>
197 <STYLE TYPE="text/css">
203 .confirm { color: #ff0000 }
211 <DIV STYLE="text-align: center; padding:1em">
212 <% emt('Package #') %><B><% $pkgnum %></B>: <B><% $cust_pkg->part_pkg->pkg %></B><BR>
214 <% emt('The following changes will be made:') %>
216 <% emt('No changes will be made.') %>
221 % foreach my $error ( @errors ) {
223 <TD><IMG SRC="<%$p%>images/cross.png"></TD>
224 <TD CLASS="error"><% $error %></TD>
228 % while (@changes, @confirm) {
229 % my $text = shift @changes;
230 % if (length $text) {
232 <TD><IMG SRC="<%$p%>images/tick.png"></TD>
236 % $text = shift @confirm;
237 % if (length $text) {
240 <INPUT TYPE="checkbox" NAME="areyousure" VALUE=1 onclick="submit_ready()">
242 <TD CLASS="confirm"><% $text %></TD>
249 <DIV CLASS="button-container">
250 <BUTTON TYPE="button" STYLE="width:145px" ID="submit_cancel"\
251 onclick="submit_cancel()">
252 <IMG SRC="<%$p%>images/cross.png" ALT=""> Cancel
255 <BUTTON TYPE="button" STYLE="width:145px" ID="submit_continue"\
256 onclick="submit_continue()">
257 <IMG SRC="<%$p%>images/tick.png" ALT=""> Continue
261 <FORM NAME="DateEditForm" STYLE="display:none" TARGET="_parent" ACTION="<%$p%>edit/process/REAL_cust_pkg.cgi" METHOD="POST">
262 % foreach (keys %hash) {
263 <INPUT TYPE="hidden" NAME="<%$_%>" VALUE="<% $hash{$_} |h%>">
267 function submit_ready() {
269 var checkboxes = document.getElementsByName('areyousure');
271 for (i=0; i < checkboxes.length; i++) {
272 if (! checkboxes[i].checked ) {
276 document.getElementById('submit_continue').disabled = !ready;
279 function submit_cancel() {
282 function submit_continue() {
283 if ( submit_ready() ) {
284 document.forms.DateEditForm.submit();
289 <& /elements/footer.html &>