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