backoffice API: add new_customer, RT#22830
[freeside.git] / httemplate / elements / tr-select-reason.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/tr-select-reason.html',
6
7     #required 
8     'field'         => 'reasonnum',
9     'reason_class'  => 'C', # currently 'C', 'R',  or 'S'
10                            # for cancel, credit, or suspend
11
12     #recommended
13     'cgi' => $cgi, #easiest way for things to be properly "sticky" on errors
14
15     #optional
16     'control_button' => 'element_name', #button to be enabled when a reason is
17                                         #selected
18     'id'             => 'element_id',
19
20     #deprecated ways to keep things "sticky" on errors
21     # (requires duplicate code in each using file to parse cgi params)
22     'curr_value'     => $curr_value,
23     'curr_value'     => {
24                           'typenum' => $typenum,
25                           'reason'  => $reason,
26                         },
27
28   )
29
30 </%doc>
31
32 <SCRIPT TYPE="text/javascript">
33   function sh_add<% $func_suffix %>()
34   {
35     var hints = <% encode_json(\@hints) %>;
36     var select_reason = document.getElementById('<% $id %>');
37
38 % if ( $class eq 'S' ) {    
39     document.getElementById('<% $id %>_hint').innerHTML =
40       hints[select_reason.selectedIndex];
41 % }
42
43     if (select_reason.selectedIndex == 0){
44       <% $controlledbutton ? $controlledbutton.'.disabled = true;' : ';' %>
45     }else{
46       <% $controlledbutton ? $controlledbutton.'.disabled = false;' : ';' %>
47     }
48
49 %if ($curuser->access_right($add_access_right)){
50
51     if (select_reason.selectedIndex == 
52          (select_reason.length - 1)) {
53       document.getElementById('new<% $id %>').disabled = false;
54       document.getElementById('new<% $id %>').style.display = 'inline';
55       document.getElementById('new<% $id %>Label').style.display = 'inline';
56       document.getElementById('new<% $id %>T').disabled = false;
57       document.getElementById('new<% $id %>T').style.display = 'inline';
58       document.getElementById('new<% $id %>TLabel').style.display = 'inline';
59     } else {
60       document.getElementById('new<% $id %>').disabled = true;
61       document.getElementById('new<% $id %>').style.display = 'none';
62       document.getElementById('new<% $id %>Label').style.display = 'none';
63       document.getElementById('new<% $id %>T').disabled = true;
64       document.getElementById('new<% $id %>T').style.display = 'none';
65       document.getElementById('new<% $id %>TLabel').style.display = 'none';
66     }
67
68 %}
69
70   }
71 </SCRIPT>
72
73 <TR>
74   <TD ALIGN="right"><% mt('Reason') |h %></TD>
75   <TD>
76     <SELECT id="<% $id %>" name="<% $name %>" onFocus="sh_add<% $func_suffix %>()" onChange="sh_add<% $func_suffix %>()">
77       <OPTION VALUE="" <% ($init_reason eq '') ? 'SELECTED' : '' %>><% mt('Select Reason...') |h %></OPTION>
78 %    foreach my $reason (@reasons) {
79       <OPTION VALUE="<% $reason->reasonnum %>" <% ($init_reason == $reason->reasonnum) ? 'SELECTED' : '' %>><% $reason->reasontype->type %> : <% $reason->reason %></OPTION>
80 %    }
81 %    if ($curuser->access_right($add_access_right)) {
82       <OPTION VALUE="-1" <% ($init_reason == -1) ? 'SELECTED' : '' %>><% mt('Add new reason') |h %></OPTION>
83 %    }
84 %
85     </SELECT>
86   </TD>
87 </TR>
88
89 %   my @types = qsearch( 'reason_type', { 'class' => $class } );
90 %   if (scalar(@types) < 1) {  # we should never reach this
91 <TR>
92   <TD ALIGN="right">
93     <P><% mt('No reason types. Please add some.') |h %></P>
94   </TD>
95 </TR>
96 %   }elsif (scalar(@types) == 1) {
97 <TR>
98   <TD ALIGN="right">
99     <P id="new<% $name %>TLabel" style="display:<% $display %>"><% mt('Reason Type') |h %></P>
100   </TD>
101   <TD>
102     <P id="new<% $name %>T" disabled="<% $disabled %>" style="display:<% $display %>"><% $types[0]->type %>
103     <INPUT type="hidden" name="new<% $name %>T" value="<% $types[0]->typenum %>">
104   </TD>
105 </TR>
106
107 %   }else{
108
109 <TR>
110   <TD ALIGN="right">
111     <P id="new<% $id %>TLabel" style="display:<% $display %>"><% mt('Reason Type') |h %></P>
112   </TD>
113   <TD>
114     <SELECT id="new<% $id %>T" name="new<% $name %>T" "<% $disabled %>" style="display:<% $display %>">
115 %     for my $type (@types) {
116         <OPTION VALUE="<% $type->typenum %>" <% ($init_type == $type->typenum) ? 'SELECTED' : '' %>><% $type->type %></OPTION>
117 %     }
118     </SELECT>
119   </TD>
120 </TR>
121 %   }
122
123 % if ( $class eq 'S' ) {
124 <TR>
125   <TD COLSPAN=2 ALIGN="center" id="<% $id %>_hint">
126   </TD>
127 </TR>
128 % }
129
130 <TR>
131   <TD ALIGN="right">
132     <P id="new<% $id %>Label" style="display:<% $display %>"><% mt('New Reason') |h %></P>
133   </TD>
134   <TD><INPUT id="new<% $id %>" name="new<% $name %>" type="text" value="<% $init_newreason |h %>" "<% $disabled %>" style="display:<% $display %>"></TD>
135 </TR>
136
137 <%init>
138
139 my %opt = @_;
140
141 my $name = $opt{'field'};
142 my $class = $opt{'reason_class'};
143
144 my $init_reason;
145 if ( $opt{'cgi'} ) {
146   $init_reason = $opt{'cgi'}->param($name);
147 } else {
148   $init_reason = $opt{'curr_value'};
149 }
150
151 my $controlledbutton = $opt{'control_button'};
152
153 ( my $func_suffix = $name ) =~ s/\./_/g;
154
155 my $id = $opt{'id'} || $func_suffix;
156
157 my $add_access_right;
158 if ($class eq 'C') {
159   $add_access_right = 'Add on-the-fly cancel reason';
160 } elsif ($class eq 'S') {
161   $add_access_right = 'Add on-the-fly suspend reason';
162 } elsif ($class eq 'R') {
163   $add_access_right = 'Add on-the-fly credit reason';
164 } else {
165   die "illegal class: $class";
166 }
167
168 my( $display, $disabled ) = ( 'none', 'DISABLED' );
169 my( $init_type, $init_newreason ) = ( '', '' );
170 if ($init_reason == -1 || ref($init_reason) ) {
171
172   $display = 'inline';
173   $disabled = '';
174
175   if ( ref($init_reason) ) {
176     $init_type      = $init_reason->{'typenum'};
177     $init_newreason = $init_reason->{'reason'};
178     $init_reason = -1;
179   } elsif ( $opt{'cgi'} ) {
180     $init_type      = $opt{'cgi'}->param( "new${name}T" );
181     $init_newreason = $opt{'cgi'}->param( "new$name"    );
182   }
183
184 }
185
186 my $extra_sql =
187   "WHERE class = '$class' and (disabled = '' OR disabled is NULL)";
188
189 my @reasons = qsearch({
190   table     => 'reason', 
191   hashref   => {},
192   extra_sql => $extra_sql,
193   addl_from => 'LEFT JOIN reason_type '.
194                ' ON reason_type.typenum = reason.reason_type',
195   order_by  => 'ORDER BY reason_type.type ASC, reason.reason ASC',
196 });
197
198 my @hints;
199 if ( $class eq 'S' ) {
200   my $conf = FS::Conf->new;
201   @hints = ( '' );
202   foreach my $reason (@reasons) {
203     if ( $reason->unsuspend_pkgpart ) {
204       my $part_pkg = FS::part_pkg->by_key($reason->unsuspend_pkgpart);
205       if ( $part_pkg ) {
206         if ( $part_pkg->option('setup_fee',1) > 0 and 
207              $part_pkg->option('recur_fee',1) == 0 ) {
208           # the usual case
209           push @hints,
210             mt('A [_1] unsuspension fee will apply.', 
211                ($conf->config('money_char') || '$') .
212                sprintf('%.2f', $part_pkg->option('setup_fee'))
213                );
214         } else {
215           # oddball cases--not really supported
216           push @hints,
217             mt('An unsuspension package will apply: [_1]',
218               $part_pkg->price_info
219               );
220         }
221       } else { #no $part_pkg
222         push @hints,
223           '<FONT COLOR="#ff0000">Unsuspend pkg #'.$reason->unsuspend_pkgpart.
224           ' not found.</FONT>';
225       }
226     } else { #no unsuspend_pkgpart
227       push @hints, '';
228     }
229   }
230   push @hints, ''; # for the "new reason" case
231   @hints = map {'<FONT SIZE="-1">'.$_.'</FONT>'} @hints;
232 }
233
234
235 my $curuser = $FS::CurrentUser::CurrentUser;
236
237 </%init>