summaryrefslogtreecommitdiff
path: root/httemplate/edit/process/router.cgi
blob: a2fa46dd9016fbe6e485001f34d68b0e6cb1f05c (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
<%

local $FS::UID::AutoCommit=0;

sub check {
  my $error = shift;
  if($error) {
    $cgi->param('error', $error);
    print $cgi->redirect(popurl(3) . "edit/router.cgi?". $cgi->query_string);
    dbh->rollback;
    exit;
  }
}

my $error = '';
my $routernum  = $cgi->param('routernum');
my $routername = $cgi->param('routername');
my $old = qsearchs('router', { routernum => $routernum });
my @old_psr;

my $new = new FS::router {
  map {
    ($_, scalar($cgi->param($_)));
  } fields('router')
};

if($old) {
  $error = $new->replace($old);
} else {
  $error = $new->insert;
  $routernum = $new->routernum;
}

check($error);

if ($old) {
  @old_psr = $old->part_svc_router;
  foreach my $psr (@old_psr) {
    if($cgi->param('svcpart_'.$psr->svcpart) eq 'ON') {
      # do nothing
    } else {
      $error = $psr->delete;
    }
  }
  check($error);
}

foreach($cgi->param) {
  if($cgi->param($_) eq 'ON' and /^svcpart_(\d+)$/) {
    my $svcpart = $1;
    if(grep {$_->svcpart == $svcpart} @old_psr) {
      # do nothing
    } else {
      my $new_psr = new FS::part_svc_router { svcpart   => $svcpart,
                                              routernum => $routernum };
      $error = $new_psr->insert;
    }
    check($error);
  }
}


# Yay, everything worked!
dbh->commit or die dbh->errstr;
print $cgi->redirect(popurl(3). "browse/router.cgi");

%>