summaryrefslogtreecommitdiff
path: root/httemplate/misc/process/change_pkg_start.html
blob: 17a8518f93840ff4fb97917f8f8d027cb16625ca (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
<& /elements/header-popup.html &>
  <SCRIPT TYPE="text/javascript">
    window.top.location.reload();
  </SCRIPT>
  </BODY>
</HTML>
<%init>

my $curuser = $FS::CurrentUser::CurrentUser;
die "access denied"
  unless $curuser->access_right('Change package start date');

$cgi->param('pkgnum') =~ /^(\d+)$/
  or die "illegal pkgnum";
my $pkgnum = $1;

my $cust_pkg = qsearchs({
  table     => 'cust_pkg',
  addl_from => ' JOIN cust_main USING (custnum) ',
  hashref   => { 'pkgnum' => $pkgnum },
  extra_sql => ' AND '. $curuser->agentnums_sql,
}) or die "Unknown pkgnum: $pkgnum";

my $cust_main = $cust_pkg->cust_main;

my $error;
my $start_date;
if ( $cgi->param('when') eq 'now' ) {
  # start it the next time billing runs
  $start_date = '';
} elsif ( $cgi->param('when') eq 'next_bill_date' ) {
  $start_date = $cust_main->next_bill_date;
} elsif ( $cgi->param('when') eq 'date' ) {
  $start_date = parse_datetime($cgi->param('start_date'));
}

if ( $cust_pkg->setup ) {
  # shouldn't happen
  $error = 'This package has already started billing.';
} else {
  local $FS::UID::AutoCommit = 0;
  foreach my $pkg ($cust_pkg, $cust_pkg->supplemental_pkgs) {
    $pkg->set('start_date', $start_date);
    $error ||= $pkg->replace;
  }
  $error ? dbh->rollback : dbh->commit;
}

if ( $error ) {
  $cgi->param('error', $error);
  print $cgi->redirect($fsurl.'misc/change_pkg_start.html?', $cgi->query_string);
}
</%init>