change credit reasons from freetext to new reason/reason type system (#2777)
[freeside.git] / httemplate / elements / tr-select-reason.html
1 <SCRIPT TYPE="text/javascript">
2   function sh_add<% $func_suffix %>()
3   {
4
5     if (document.getElementById('<% $id %>').selectedIndex == 0){
6       <% $controlledbutton ? $controlledbutton.'.disabled = true;' : ';' %>
7     }else{
8       <% $controlledbutton ? $controlledbutton.'.disabled = false;' : ';' %>
9     }
10
11 %if ($curuser->access_right($add_access_right)){
12
13     if (document.getElementById('<% $id %>').selectedIndex == 
14          (document.getElementById('<% $id %>').length - 1)) {
15       document.getElementById('new<% $id %>').disabled = false;
16       document.getElementById('new<% $id %>').style.display = 'inline';
17       document.getElementById('new<% $id %>Label').style.display = 'inline';
18       document.getElementById('new<% $id %>T').disabled = false;
19       document.getElementById('new<% $id %>T').style.display = 'inline';
20       document.getElementById('new<% $id %>TLabel').style.display = 'inline';
21     } else {
22       document.getElementById('new<% $id %>').disabled = true;
23       document.getElementById('new<% $id %>').style.display = 'none';
24       document.getElementById('new<% $id %>Label').style.display = 'none';
25       document.getElementById('new<% $id %>T').disabled = true;
26       document.getElementById('new<% $id %>T').style.display = 'none';
27       document.getElementById('new<% $id %>TLabel').style.display = 'none';
28     }
29
30 %}
31
32   }
33 </SCRIPT>
34
35 <TR>
36   <TD ALIGN="right">Reason</TD>
37   <TD>
38     <SELECT id="<% $id %>" name="<% $name %>" onFocus="sh_add<% $func_suffix %>()" onChange="sh_add<% $func_suffix %>()">
39 %    my @reasons = qsearch( { table =>'reason', 
40 %                             hashref => {},
41 %                             extra_sql => $extra_sql,
42 %                             addl_from => 'LEFT JOIN reason_type ON reason_type.typenum = reason.reason_type',
43 %                            });
44       <OPTION VALUE="" <% ($init_reason eq "") ? 'SELECTED' : '' %>>Select Reason...</OPTION>
45 %    foreach my $reason (@reasons) {
46       <OPTION VALUE="<% $reason->reasonnum %>" <% ($init_reason == $reason->reasonnum) ? 'SELECTED' : '' %>><% $reason->reasontype->type %> : <% $reason->reason %></OPTION>
47 %    }
48 %    if ($curuser->access_right($add_access_right)) {
49       <OPTION VALUE="-1" <% ($init_reason == -1) ? 'SELECTED' : '' %>>Add new reason</OPTION>
50 %    }
51 %
52     </SELECT>
53   </TD>
54 </TR>
55
56 %     my @types = qsearch( 'reason_type', { 'class' => $class } );
57 %   if (scalar(@types) < 1) {  # we should never reach this
58 <TR>
59   <TD ALIGN="right">
60     <P>No reason types.  Go add some. </P>
61   </TD>
62 </TR>
63 %   }elsif (scalar(@types) == 1) {
64 <TR>
65   <TD ALIGN="right">
66     <P id="new<% $name %>TLabel" style="display:<% $display %>">Reason Type</P>
67   </TD>
68   <TD>
69     <P id="new<% $name %>T" disabled="<% $disabled %>" style="display:<% $display %>"><% $types[0]->type %>
70     <INPUT type="hidden" name="new<% $name %>T" value="<% $types[0]->typenum %>">
71   </TD>
72 </TR>
73
74 %   }else{
75
76 <TR>
77   <TD ALIGN="right">
78     <P id="new<% $id %>TLabel" style="display:<% $display %>">Reason Type</P>
79   </TD>
80   <TD>
81     <SELECT id="new<% $id %>T" name="new<% $name %>T" "<% $disabled %>" style="display:<% $display %>">
82 %     for my $type (@types)){
83         <OPTION VALUE="<% $type->typenum %>" <% ($init_type == $type->typenum) ? 'SELECTED' : '' %>><% $type->type %></OPTION>
84 %     }
85     </SELECT>
86   </TD>
87 </TR>
88 %   }
89
90 <TR>
91   <TD ALIGN="right">
92     <P id="new<% $id %>Label" style="display:<% $display %>">New Reason</P>
93   </TD>
94   <TD><INPUT id="new<% $id %>" name="new<% $name %>" type="text" value="<% $init_newreason %>" "<% $disabled %>" style="display:<% $display %>"></TD>
95 </TR>
96
97 <%init>
98
99 my %opt = @_;
100
101 my $name = $opt{'field'};
102 my $class = $opt{'reason_class'};
103 my $init_reason = $opt{'curr_value'};
104
105 my $controlledbutton = $opt{'control_button'};
106
107 ( my $func_suffix = $name ) =~ s/\./_/g;
108
109 my $id = $opt{'id'} || $func_suffix;
110
111 my( $add_access_right, $access_right ); 
112 if ($class eq 'C') {
113   $access_right = 'Cancel customer';
114   $add_access_right = 'Add on-the-fly cancel reason';
115 } elsif ($class eq 'S') {
116   $access_right = 'Suspend customer package';
117   $add_access_right = 'Add on-the-fly suspend reason';
118 } elsif ($class eq 'S') {
119   $access_right = 'Post credit';
120   $add_access_right = 'Add on-the-fly credit reason';
121 } else {
122   die "illegal class: $class";
123 }
124
125 my( $display, $disabled ) = ( 'none', 'DISABLED' );
126 my( $init_type, $init_newreason ) = ( '', '' );
127 if ($init_reason == -1 || ref($init_reason) ) {
128
129   $display = 'inline';
130   $disabled = '';
131
132   if ( ref($init_reason) ) {
133     $init_type      = $init_reason->{'typenum'};
134     $init_newreason = $init_reason->{'reason'};
135     $init_reason = -1;
136   }
137
138 }
139
140 my $extra_sql = "WHERE class = '$class' and (disabled = '' OR disabled is NULL) ".
141                 "ORDER BY reason_type";
142 my $curuser = $FS::CurrentUser::CurrentUser;
143
144 </%init>