adjust warnings on package date edit, #20689
[freeside.git] / httemplate / misc / confirm-cust_pkg-edit_dates.html
1 <%init>
2 my $curuser = $FS::CurrentUser::CurrentUser;
3
4 die "access denied"
5   unless $curuser->access_right('Edit customer package dates');
6
7 my %arg = $cgi->Vars;
8
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+$/ ) {
16     $hash{$_} = $arg{$_};
17   } elsif ( $arg{$_} ) {
18     $hash{$_} = parse_datetime($arg{$_});
19   } else {
20     $hash{$_} = '';
21   }
22 }
23
24 my (@changes, @confirm, @errors);
25
26 my $part_pkg = $cust_pkg->part_pkg;
27 my @supp_pkgs = $cust_pkg->supplemental_pkgs;
28 my $main_pkg = $cust_pkg->main_pkg;
29
30 my $conf = FS::Conf->new;
31 my $date_format = $conf->config('date_format') || '%b %o, %Y';
32 # Start date
33 if ( $hash{'start_date'} != $cust_pkg->get('start_date') and !$hash{'setup'} ) {
34   my $start = '';
35   $start = time2str($date_format, $hash{'start_date'}) if $hash{'start_date'};
36   my $text = 'Set this package';
37   if ( @supp_pkgs ) {
38     $text .= ' and all its supplemental packages';
39   }
40   $text .= ' to start billing';
41   if ( $start ) {
42     $text .= ' on [_1].';
43     push @changes, mt($text, $start);
44   } else {
45     $text .= ' immediately.';
46     push @changes, mt($text);
47   }
48   push @confirm, '';
49 }
50
51 # Setup date changes
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;
59     $text .= '.';
60     push @changes, mt($text);
61     if ( $has_setup_fee ) {
62       push @confirm, mt('This will re-charge the customer for the setup fee.');
63     } else {
64       push @confirm, '';
65     }
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;
69     $text .= '.';
70     push @changes, mt($text, $setup);
71     if ( $has_setup_fee ) {
72       push @confirm, mt('This will prevent charging the setup fee.');
73     } else {
74       push @confirm, '';
75     }
76   } else {
77     my $text = 'Set the setup date to [_1]';
78     $text .= ' on this and all its supplemental packages' if @supp_pkgs;
79     $text .= '.';
80     push @changes, mt($text, $setup);
81     push @confirm, '';
82   }
83 }
84
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.');
90   } else {
91     push @errors, mt('You cannot set both a start date and a setup date on '.
92                      'the same package.');
93   }
94 }
95
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);
103   } else {
104     push @changes, mt('Remove the [_1].', $name);
105   }
106   push @confirm, '';
107   # I don't think we want to adjust this on supplemental packages.
108 }
109
110 # Bill date change
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);
117
118   if ( $hash{'bill'} < time and $hash{'bill'} ) {
119     push @confirm, 
120       mt('The customer will be charged for the interval from [_1] until now.',
121          $bill);
122   } elsif ( !$hash{'bill'} and ($hash{'last_bill'} or $hash{'setup'}) ) {
123     my $last_bill = 
124       time2str($date_format, $hash{'last_bill'} || $hash{'setup'});
125     push @confirm,
126       mt('The customer will be charged for the interval from [_1] until now.',
127         $last_bill);
128   } else {
129     push @confirm, '';
130   }
131
132   if ( @supp_pkgs ) {
133     push @changes, '';
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;
140       if ( $diff < 1 ) {
141         $diff = mt('[quant,_1,hour]', int($diff * 24));
142       } else {
143         $diff = mt('[quant,_1,day]', int($diff));
144       }
145       push @confirm,
146         mt('[_1] supplemental package will also be billed [_2] [_3].',
147             (@supp_pkgs > 1 ? 'Each' : 'The'),
148             $diff,
149             ($sign > 0 ? 'later' : 'earlier')
150         );
151     } else {
152       # the package hasn't been billed yet, or you've set bill = null
153       push @confirm,
154         mt('[_1] supplemental package will also be billed on [_2].',
155             (@supp_pkgs > 1 ? 'Each' : 'The'),
156             $bill
157         );
158     }
159   } #if @supp_pkgs
160
161   if ( $main_pkg ) {
162     push @changes, '';
163     push @confirm,
164       mt('This package is a supplemental package.  The bill date of its '.
165          'main package will not be adjusted.');
166   }
167 }
168
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'});
173     push @changes,
174       mt('Set this package\'s contract end date to [_1]', $contract_end);
175   } else {
176     push @changes, mt('Remove this package\'s contract end date.');
177   }
178   if ( @supp_pkgs ) {
179     my $text = 'This change will also apply to ' .
180       (@supp_pkgs > 1 ?
181         'all supplemental packages.':
182         'the supplemental package.');
183     push @confirm, mt($text);
184   } else {
185     push @confirm, '';
186   }
187 }
188
189 my $title = '';
190 if ( @errors ) {
191   $title = 'Error changing package dates';
192 } else {
193   $title = 'Confirm date changes';
194 }
195 </%init>
196 <& /elements/header-popup.html, { title => $title, etc => 'BGCOLOR=""' } &>
197 <STYLE TYPE="text/css">
198 .error { 
199   color: #ff0000;
200   font-weight: bold;
201   text-align: center;
202 }
203 .confirm { color: #ff0000 }
204 .button-container {
205   position: fixed;
206   bottom: 5px;
207   text-align: center;
208   width: 100%
209 }
210 </STYLE>
211 <DIV STYLE="text-align: center; padding:1em">
212 <% emt('Package #') %><B><% $pkgnum %></B>: <B><% $cust_pkg->part_pkg->pkg %></B><BR>
213 % if ( @changes ) {
214   <% emt('The following changes will be made:') %>
215 % } else {
216   <% emt('No changes will be made.') %>
217 % }
218 </DIV>
219 <TABLE WIDTH="100%">
220 % if ( @errors ) {
221 %   foreach my $error ( @errors ) {
222 <TR>
223   <TD><IMG SRC="<%$p%>images/cross.png"></TD>
224   <TD CLASS="error"><% $error %></TD>
225 </TR>
226 %   }
227 % } else {
228 %   while (@changes, @confirm) {
229 %     my $text = shift @changes;
230 %     if (length $text) {
231 <TR>
232   <TD><IMG SRC="<%$p%>images/tick.png"></TD>
233   <TD><% $text %></TD>
234 </TR>
235 %     }
236 %     $text = shift @confirm;
237 %     if (length $text) {
238 <TR>
239   <TD>
240     <INPUT TYPE="checkbox" NAME="areyousure" VALUE=1 onclick="submit_ready()">
241   </TD>
242   <TD CLASS="confirm"><% $text %></TD>
243 </TR>
244 %     }
245 %   }
246 % }
247 </TABLE>
248 %# action buttons
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
253   </BUTTON>
254 % if (!@errors ) {
255   <BUTTON TYPE="button" STYLE="width:145px" ID="submit_continue"\
256     onclick="submit_continue()">
257     <IMG SRC="<%$p%>images/tick.png" ALT=""> Continue
258   </BUTTON>
259 </DIV>
260 % }
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%>">
264 % }
265 </FORM>
266 <SCRIPT>
267 function submit_ready() {
268   var ready = true;
269   var checkboxes = document.getElementsByName('areyousure');
270   var i;
271   for (i=0; i < checkboxes.length; i++) {
272     if (! checkboxes[i].checked ) {
273       ready = false;
274     }
275   }
276   document.getElementById('submit_continue').disabled = !ready;
277   return ready;
278 }
279 function submit_cancel() {
280   parent.nd(1);
281 }
282 function submit_continue() {
283   if ( submit_ready() ) {
284     document.forms.DateEditForm.submit();
285   }
286 }
287 submit_ready();
288 </SCRIPT>
289 <& /elements/footer.html &>