change credit reasons from freetext to new reason/reason type system (#2777)
[freeside.git] / httemplate / edit / process / cust_credit.cgi
1 %
2 %
3 %$cgi->param('custnum') =~ /^(\d*)$/ or die "Illegal custnum!";
4 %my $custnum = $1;
5 %
6 %$cgi->param('reasonnum') =~ /^(-?\d+)$/ or die "Illegal reasonnum";
7 %my $reasonnum = $1;
8 %
9 %my $oldAutoCommit = $FS::UID::AutoCommit;
10 %local $FS::UID::AutoCommit = 0;
11 %my $dbh = dbh;
12 %
13 %my $error = '';
14 %if ($reasonnum == -1) {
15 %
16 %  $error = 'Enter a new reason (or select an existing one)'
17 %    unless $cgi->param('newreasonnum') !~ /^\s*$/;
18 %  my $reason = new FS::reason({ 'reason_type' => $cgi->param('newreasonnumT'),
19 %                                'reason'      => $cgi->param('newreasonnum'),
20 %                              });
21 %  $error ||= $reason->insert;
22 %  $cgi->param('reasonnum', $reason->reasonnum)
23 %    unless $error;
24 %}
25 %
26 %unless ($error) {
27 %  my $new = new FS::cust_credit ( {
28 %    map {
29 %      $_, scalar($cgi->param($_));
30 %    } fields('cust_credit')
31 %  } );
32 %  $error = $new->insert;
33 %}
34 %
35 %if ( $error ) {
36 %  $cgi->param('reasonnum', $reasonnum);
37 %  $cgi->param('error', $error);
38 %  $dbh->rollback if $oldAutoCommit;
39 %
40 %  
41 <% $cgi->redirect(popurl(2). "cust_credit.cgi?". $cgi->query_string ) %>
42 %
43 %
44 %} else {
45 %
46 %  if ( $cgi->param('apply') eq 'yes' ) {
47 %    my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum })
48 %      or die "unknown custnum $custnum";
49 %    $cust_main->apply_credits;
50 %  }
51 %  #print $cgi->redirect(popurl(3). "view/cust_main.cgi?$custnum");
52 %
53 %  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
54 %  
55 <% header('Credit sucessful') %>
56   <SCRIPT TYPE="text/javascript">
57     window.top.location.reload();
58   </SCRIPT>
59
60   </BODY></HTML>
61 % } 
62