summaryrefslogtreecommitdiff
path: root/httemplate/misc/confirm-cust_pkg-edit_dates.html
blob: 8e548527a026850259e1c822af197504aa50db2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<%init>
my $curuser = $FS::CurrentUser::CurrentUser;

die "access denied"
  unless $curuser->access_right('Edit customer package dates');

my %arg = $cgi->Vars;

my $pkgnum = $arg{'pkgnum'};
$pkgnum =~ /^\d+$/ or die "bad pkgnum '$pkgnum'";
my $cust_pkg = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum});
my %hash = $cust_pkg->hash;
foreach (qw( start_date setup bill last_bill contract_end )) {
  # adjourn, expire, resume not editable this way
  if( $arg{$_} =~ /^\d+$/ ) {
    $hash{$_} = $arg{$_};
  } elsif ( $arg{$_} ) {
    $hash{$_} = parse_datetime($arg{$_});
  } else {
    $hash{$_} = '';
  }
}

my (@changes, @confirm, @errors);

my $part_pkg = $cust_pkg->part_pkg;
my @supp_pkgs = $cust_pkg->supplemental_pkgs;
my $main_pkg = $cust_pkg->main_pkg;

my $conf = FS::Conf->new;
my $date_format = $conf->config('date_format') || '%b %o, %Y';
# Start date
if ( $hash{'start_date'} != $cust_pkg->get('start_date') and !$hash{'setup'} ) {
  my $start = '';
  $start = time2str($date_format, $hash{'start_date'}) if $hash{'start_date'};
  my $text = 'Set this package';
  if ( @supp_pkgs ) {
    $text .= ' and all its supplemental packages';
  }
  $text .= ' to start billing';
  if ( $start ) {
    $text .= ' on [_1].';
    push @changes, mt($text, $start);
  } else {
    $text .= ' immediately.';
    push @changes, mt($text);
  }
  push @confirm, '';
}

# Setup date changes
if ( $hash{'setup'} != $cust_pkg->get('setup') ) {
  my $setup = time2str($date_format, $hash{'setup'});
  my $has_setup_fee = grep { $_->part_pkg->option('setup_fee',1) > 0 }
                      $cust_pkg, @supp_pkgs;
  if ( !$hash{'setup'} ) {
    my $text = 'Remove the setup date';
    $text .= ' from this and all its supplemental packages' if @supp_pkgs;
    $text .= '.';
    push @changes, mt($text);
    if ( $has_setup_fee ) {
      push @confirm, mt('This will re-charge the customer for the setup fee.');
    } else {
      push @confirm, '';
    }
  } elsif ( $hash{'setup'} and !$cust_pkg->get('setup') ) {
    my $text = 'Add a setup date of [_1]';
    $text .= ' to this and all its supplemental packages' if @supp_pkgs;
    $text .= '.';
    push @changes, mt($text, $setup);
    if ( $has_setup_fee ) {
      push @confirm, mt('This will prevent charging the setup fee.');
    } else {
      push @confirm, '';
    }
  } else {
    my $text = 'Set the setup date to [_1]';
    $text .= ' on this and all its supplemental packages' if @supp_pkgs;
    $text .= '.';
    push @changes, mt($text, $setup);
    push @confirm, '';
  }
}

# Check for start date + setup date
if ( $hash{'start_date'} and $hash{'setup'} ) {
  if ( $cust_pkg->get('setup') ) {
    push @errors, mt('Since the package has already started billing, it '.
                     'cannot have a start date.');
  } else {
    push @errors, mt('You cannot set both a start date and a setup date on '.
                     'the same package.');
  }
}

# Last bill date change
if ( $hash{'last_bill'} != $cust_pkg->get('last_bill') ) {
  my $last_bill = time2str($date_format, $hash{'last_bill'});
  my $name = 'last bill date';
  $name = 'last renewal date' if $part_pkg->is_prepaid;
  if ( $hash{'last_bill'} ) {
    push @changes, mt('Set the [_1] to [_2].', $name, $last_bill);
  } else {
    push @changes, mt('Remove the [_1].', $name);
  }
  push @confirm, '';
  # I don't think we want to adjust this on supplemental packages.
}

# Bill date change
if ( $hash{'bill'} != $cust_pkg->get('bill') ) {
  my $bill = time2str($date_format, $hash{'bill'});
  $bill = 'today' if !$hash{'bill'}; # or 'the end of today'?...
  my $name = 'next bill date';
  $name = 'end of the prepaid period' if $part_pkg->is_prepaid;
  push @changes, mt('Set the [_1] to [_2].', $name, $bill);

  if ( $hash{'bill'} < time and $hash{'bill'} ) {
    push @confirm, 
      mt('The customer will be charged for the interval from [_1] until now.',
         $bill);
  } elsif ( !$hash{'bill'} and ($hash{'last_bill'} or $hash{'setup'}) ) {
    my $last_bill = 
      time2str($date_format, $hash{'last_bill'} || $hash{'setup'});
    push @confirm,
      mt('The customer will be charged for the interval from [_1] until now.',
        $last_bill);
  } else {
    push @confirm, '';
  }

  if ( @supp_pkgs ) {
    push @changes, '';
    if ( $cust_pkg->get('bill') and $hash{'bill'} ) {
      # the package already has a bill date, so adjust the dates 
      # of supplementals by the same interval
      my $diff = $hash{'bill'} - $cust_pkg->get('bill');
      my $sign = $diff < 0 ? -1 : 1;
      $diff = $diff * $sign / 86400;
      if ( $diff < 1 ) {
        $diff = mt('[quant,_1,hour]', int($diff * 24));
      } else {
        $diff = mt('[quant,_1,day]', int($diff));
      }
      push @confirm,
        mt('[_1] supplemental package will also be billed [_2] [_3].',
            (@supp_pkgs > 1 ? 'Each' : 'The'),
            $diff,
            ($sign > 0 ? 'later' : 'earlier')
        );
    } else {
      # the package hasn't been billed yet, or you've set bill = null
      push @confirm,
        mt('[_1] supplemental package will also be billed on [_2].',
            (@supp_pkgs > 1 ? 'Each' : 'The'),
            $bill
        );
    }
  } #if @supp_pkgs

  if ( $main_pkg ) {
    push @changes, '';
    push @confirm,
      mt('This package is a supplemental package.  The bill date of its '.
         'main package will not be adjusted.');
  }
}

# Contract end change
if ( $hash{'contract_end'} != $cust_pkg->get('contract_end') ) {
  if ( $hash{'contract_end'} ) {
    my $contract_end = time2str($date_format, $hash{'contract_end'});
    push @changes,
      mt('Set this package\'s contract end date to [_1]', $contract_end);
  } else {
    push @changes, mt('Remove this package\'s contract end date.');
  }
  if ( @supp_pkgs ) {
    my $text = 'This change will also apply to ' .
      (@supp_pkgs > 1 ?
        'all supplemental packages.':
        'the supplemental package.');
    push @confirm, mt($text);
  } else {
    push @confirm, '';
  }
}

my $title = '';
if ( @errors ) {
  $title = 'Error changing package dates';
} else {
  $title = 'Confirm date changes';
}
</%init>
<& /elements/header-popup.html, { title => $title, etc => 'BGCOLOR=""' } &>
<STYLE TYPE="text/css">
.error { 
  color: #ff0000;
  font-weight: bold;
  text-align: center;
}
.confirm { color: #ff0000 }
.button-container {
  position: fixed;
  bottom: 5px;
  text-align: center;
  width: 100%
}
</STYLE>
<DIV STYLE="text-align: center; padding:1em">
<% emt('Package #') %><B><% $pkgnum %></B>: <B><% $cust_pkg->part_pkg->pkg %></B><BR>
% if ( @changes ) {
  <% emt('The following changes will be made:') %>
% } else {
  <% emt('No changes will be made.') %>
% }
</DIV>
<TABLE WIDTH="100%">
% if ( @errors ) {
%   foreach my $error ( @errors ) {
<TR>
  <TD><IMG SRC="<%$p%>images/cross.png"></TD>
  <TD CLASS="error"><% $error %></TD>
</TR>
%   }
% } else {
%   while (@changes, @confirm) {
%     my $text = shift @changes;
%     if (length $text) {
<TR>
  <TD><IMG SRC="<%$p%>images/tick.png"></TD>
  <TD><% $text %></TD>
</TR>
%     }
%     $text = shift @confirm;
%     if (length $text) {
<TR>
  <TD>
    <INPUT TYPE="checkbox" NAME="areyousure" VALUE=1 onclick="submit_ready()">
  </TD>
  <TD CLASS="confirm"><% $text %></TD>
</TR>
%     }
%   }
% }
</TABLE>
%# action buttons
<DIV CLASS="button-container">
  <BUTTON TYPE="button" STYLE="width:145px" ID="submit_cancel"\
    onclick="submit_cancel()">
    <IMG SRC="<%$p%>images/cross.png" ALT=""> Cancel
  </BUTTON>
% if (!@errors ) {
  <BUTTON TYPE="button" STYLE="width:145px" ID="submit_continue"\
    onclick="submit_continue()">
    <IMG SRC="<%$p%>images/tick.png" ALT=""> Continue
  </BUTTON>
</DIV>
% }
<FORM NAME="DateEditForm" STYLE="display:none" TARGET="_parent" ACTION="<%$p%>edit/process/REAL_cust_pkg.cgi" METHOD="POST">
% foreach (keys %hash) {
<INPUT TYPE="hidden" NAME="<%$_%>" VALUE="<% $hash{$_} |h%>">
% }
</FORM>
<SCRIPT>
function submit_ready() {
  var ready = true;
  var checkboxes = document.getElementsByName('areyousure');
  var i;
  for (i=0; i < checkboxes.length; i++) {
    if (! checkboxes[i].checked ) {
      ready = false;
    }
  }
  document.getElementById('submit_continue').disabled = !ready;
  return ready;
}
function submit_cancel() {
  parent.nd(1);
}
function submit_continue() {
  if ( submit_ready() ) {
    document.forms.DateEditForm.submit();
  }
}
submit_ready();
</SCRIPT>
<& /elements/footer.html &>