27b9a82f4a971ac2bdc9a0eebc8dab58d5dcdcce
[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 ( $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 = 'the current day' 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   } else {
123     push @confirm, '';
124   }
125
126   if ( @supp_pkgs ) {
127     push @changes, '';
128     if ( $cust_pkg->get('bill') and $hash{'bill'} ) {
129       # the package already has a bill date, so adjust the dates 
130       # of supplementals by the same interval
131       my $diff = $hash{'bill'} - $cust_pkg->get('bill');
132       my $sign = $diff < 0 ? -1 : 1;
133       $diff = $diff * $sign / 86400;
134       if ( $diff < 1 ) {
135         $diff = mt('[quant,_1,hour]', int($diff * 24));
136       } else {
137         $diff = mt('[quant,_1,day]', int($diff));
138       }
139       push @confirm,
140         mt('[_1] supplemental package will also be billed [_2] [_3].',
141             (@supp_pkgs > 1 ? 'Each' : 'The'),
142             $diff,
143             ($sign > 0 ? 'later' : 'earlier')
144         );
145     } else {
146       # the package hasn't been billed yet, or you've set bill = null
147       push @confirm,
148         mt('[_1] supplemental package will also be billed on [_2].',
149             (@supp_pkgs > 1 ? 'Each' : 'The'),
150             $bill
151         );
152     }
153   } #if @supp_pkgs
154
155   if ( $main_pkg ) {
156     push @changes, '';
157     push @confirm,
158       mt('This package is a supplemental package.  The bill date of its '.
159          'main package will not be adjusted.');
160   }
161 }
162
163 # Contract end change
164 if ( $hash{'contract_end'} != $cust_pkg->get('contract_end') ) {
165   if ( $hash{'contract_end'} ) {
166     my $contract_end = time2str($date_format, $hash{'contract_end'});
167     push @changes,
168       mt('Set this package\'s contract end date to [_1]', $contract_end);
169   } else {
170     push @changes, mt('Remove this package\'s contract end date.');
171   }
172   if ( @supp_pkgs ) {
173     my $text = 'This change will also apply to ' .
174       (@supp_pkgs > 1 ?
175         'all supplemental packages.':
176         'the supplemental package.');
177     push @confirm, mt($text);
178   } else {
179     push @confirm, '';
180   }
181 }
182
183 my $title = '';
184 if ( @errors ) {
185   $title = 'Error changing package dates';
186 } else {
187   $title = 'Confirm date changes';
188 }
189 </%init>
190 <& /elements/header-popup.html, { title => $title, etc => 'BGCOLOR=""' } &>
191 <STYLE TYPE="text/css">
192 .error { 
193   color: #ff0000;
194   font-weight: bold;
195   text-align: center;
196 }
197 .confirm { color: #ff0000 }
198 .button-container {
199   position: fixed;
200   bottom: 5px;
201   text-align: center;
202   width: 100%
203 }
204 </STYLE>
205 <DIV STYLE="text-align: center; padding:1em">
206 <% emt('Package #') %><B><% $pkgnum %></B>: <B><% $cust_pkg->part_pkg->pkg %></B><BR>
207 % if ( @changes ) {
208   <% emt('The following changes will be made:') %>
209 % } else {
210   <% emt('No changes will be made.') %>
211 % }
212 </DIV>
213 <TABLE WIDTH="100%">
214 % if ( @errors ) {
215 %   foreach my $error ( @errors ) {
216 <TR>
217   <TD><IMG SRC="<%$p%>images/cross.png"></TD>
218   <TD CLASS="error"><% $error %></TD>
219 </TR>
220 %   }
221 % } else {
222 %   while (@changes, @confirm) {
223 %     my $text = shift @changes;
224 %     if (length $text) {
225 <TR>
226   <TD><IMG SRC="<%$p%>images/tick.png"></TD>
227   <TD><% $text %></TD>
228 </TR>
229 %     }
230 %     $text = shift @confirm;
231 %     if (length $text) {
232 <TR>
233   <TD>
234     <INPUT TYPE="checkbox" NAME="areyousure" VALUE=1 onclick="submit_ready()">
235   </TD>
236   <TD CLASS="confirm"><% $text %></TD>
237 </TR>
238 %     }
239 %   }
240 % }
241 </TABLE>
242 %# action buttons
243 <DIV CLASS="button-container">
244   <BUTTON TYPE="button" STYLE="width:145px" ID="submit_cancel"\
245     onclick="submit_cancel()">
246     <IMG SRC="<%$p%>images/cross.png" ALT=""> Cancel
247   </BUTTON>
248 % if (!@errors ) {
249   <BUTTON TYPE="button" STYLE="width:145px" ID="submit_continue"\
250     onclick="submit_continue()">
251     <IMG SRC="<%$p%>images/tick.png" ALT=""> Continue
252   </BUTTON>
253 </DIV>
254 % }
255 <FORM NAME="DateEditForm" STYLE="display:none" TARGET="_parent" ACTION="<%$p%>edit/process/REAL_cust_pkg.cgi" METHOD="POST">
256 % foreach (keys %hash) {
257 <INPUT TYPE="hidden" NAME="<%$_%>" VALUE="<% $hash{$_} |h%>">
258 % }
259 </FORM>
260 <SCRIPT>
261 function submit_ready() {
262   var ready = true;
263   var checkboxes = document.getElementsByName('areyousure');
264   var i;
265   for (i=0; i < checkboxes.length; i++) {
266     if (! checkboxes[i].checked ) {
267       ready = false;
268     }
269   }
270   document.getElementById('submit_continue').disabled = !ready;
271   return ready;
272 }
273 function submit_cancel() {
274   parent.nd(1);
275 }
276 function submit_continue() {
277   if ( submit_ready() ) {
278     document.forms.DateEditForm.submit();
279   }
280 }
281 submit_ready();
282 </SCRIPT>
283 <& /elements/footer.html &>