ACLs: finish group edit (agents + rights) & browse
[freeside.git] / httemplate / edit / part_pkg.cgi
1 <%
2
3 if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {
4   $cgi->param('clone', $1);
5 } else {
6   $cgi->param('clone', '');
7 }
8 if ( $cgi->param('pkgnum') && $cgi->param('pkgnum') =~ /^(\d+)$/ ) {
9   $cgi->param('pkgnum', $1);
10 } else {
11   $cgi->param('pkgnum', '');
12 }
13
14 my ($query) = $cgi->keywords;
15
16 my $part_pkg = '';
17 if ( $cgi->param('error') ) {
18   $part_pkg = new FS::part_pkg ( {
19     map { $_, scalar($cgi->param($_)) } fields('part_pkg')
20   } );
21 }
22
23 my $action = '';
24 my $clone_part_pkg = '';
25 my $pkgpart = '';
26 if ( $cgi->param('clone') ) {
27   $pkgpart = $cgi->param('clone');
28   $action = 'Custom Pricing';
29   $clone_part_pkg= qsearchs('part_pkg', { 'pkgpart' => $cgi->param('clone') } );
30   $part_pkg ||= $clone_part_pkg->clone;
31   $part_pkg->disabled('Y'); #isn't sticky on errors
32 } elsif ( $query && $query =~ /^(\d+)$/ ) {
33   $part_pkg ||= qsearchs('part_pkg',{'pkgpart'=>$1});
34   $pkgpart = $part_pkg->pkgpart;
35 } else {
36   unless ( $part_pkg ) {
37     $part_pkg = new FS::part_pkg {};
38     $part_pkg->plan('flat');
39   }
40 }
41 unless ( $part_pkg->plan ) { #backwards-compat
42   $part_pkg->plan('flat');
43   $part_pkg->plandata("setup_fee=". $part_pkg->setup. "\n".
44                       "recur_fee=". $part_pkg->recur. "\n");
45 }
46 $action ||= $part_pkg->pkgpart ? 'Edit' : 'Add';
47 my $hashref = $part_pkg->hashref;
48
49 %>
50
51 <%= include("/elements/header.html","$action Package Definition", menubar(
52   'Main Menu' => popurl(2),
53   'View all packages' => popurl(2). 'browse/part_pkg.cgi',
54 )) %>
55
56 <% #), ' onLoad="visualize()"'); %>
57
58 <% if ( $cgi->param('error') ) { %>
59   <FONT SIZE="+1" COLOR="#ff0000">Error: <%= $cgi->param('error') %></FONT>
60 <% } %>
61
62 <FORM NAME="dummy">
63
64 <%= itable('',8,1) %><TR><TD VALIGN="top">
65
66 Package information
67
68 <%= ntable("#cccccc",2) %>
69   <TR>
70     <TD ALIGN="right">Package Definition #</TD>
71     <TD BGCOLOR="#ffffff">
72       <%= $hashref->{pkgpart} ? $hashref->{pkgpart} : "(NEW)" %>
73     </TD>
74   </TR>
75   <TR>
76     <TD ALIGN="right">Package (customer-visible)</TD>
77     <TD>
78       <INPUT TYPE="text" NAME="pkg" SIZE=32 VALUE="<%= $part_pkg->pkg %>">
79     </TD>
80   </TR>
81   <TR>
82     <TD ALIGN="right">Comment (customer-hidden)</TD>
83     <TD>
84       <INPUT TYPE="text" NAME="comment" SIZE=32 VALUE="<%=$part_pkg->comment%>">
85     </TD>
86   </TR>
87   <%= include( '/elements/tr-select-pkg_class.html', $part_pkg->classnum ) %>
88   <TR>
89     <TD ALIGN="right">Promotional code</TD>
90     <TD>
91       <INPUT TYPE="text" NAME="promo_code" SIZE=32 VALUE="<%=$part_pkg->promo_code%>">
92     </TD>
93   </TR>
94   <TR>
95     <TD ALIGN="right">Disable new orders</TD>
96     <TD>
97       <INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"<%= $hashref->{disabled} eq 'Y' ? ' CHECKED' : '' %>
98     </TD>
99   </TR>
100
101 </TABLE>
102
103 </TD><TD VALIGN="top">
104
105 Tax information
106 <%= ntable("#cccccc", 2) %>
107   <TR>
108     <TD ALIGN="right">Setup fee tax exempt</TD>
109     <TD>
110       <INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y" <%= $hashref->{setuptax} eq 'Y' ? ' CHECKED' : '' %>>
111     </TD>
112   </TR>
113   <TR>
114     <TD ALIGN="right">Recurring fee tax exempt</TD>
115     <TD>
116       <INPUT TYPE="checkbox" NAME="recurtax" VALUE="Y" <%= $hashref->{recurtax} eq 'Y' ? ' CHECKED' : '' %>>
117     </TD>
118   </TR>
119
120 <% my $conf = new FS::Conf; %>
121 <% if ( $conf->exists('enable_taxclasses') ) { %>
122
123   <TR>
124     <TD align="right">Tax class</TD>
125     <TD>
126       <%= include('/elements/select-taxclass.html', $hashref->{taxclass} ) %>
127     </TD>
128   </TR>
129
130 <% } else { %>
131
132   <%= include('/elements/select-taxclass.html', $hashref->{taxclass} ) %>
133
134 <% } %>
135
136 </TABLE>
137
138 </TD></TR></TABLE>
139
140 <%
141
142 my $thead =  "\n\n". ntable('#cccccc', 2).
143              '<TR><TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Quan.</FONT></TH>';
144 $thead .=  '<TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Primary</FONT></TH>'
145   if dbdef->table('pkg_svc')->column('primary_svc');
146 $thead .= '<TH BGCOLOR="#dcdcdc">Service</TH></TR>';
147
148 %>
149
150 <BR><BR>Services included
151 <%= itable('', 4, 1) %><TR><TD VALIGN="top">
152 <%= $thead %>
153
154 <%
155
156 my $where =  "WHERE disabled IS NULL OR disabled = ''";
157 if ( $pkgpart ) {
158   $where .=  "   OR 0 < ( SELECT quantity FROM pkg_svc
159                            WHERE pkg_svc.svcpart = part_svc.svcpart
160                              AND pkgpart = $pkgpart
161                         )";
162 }
163 my @part_svc = qsearch('part_svc', {}, '', $where);
164 my $q_part_pkg = $clone_part_pkg || $part_pkg;
165 my %pkg_svc = map { $_->svcpart => $_ } $q_part_pkg->pkg_svc;
166
167 my @fixups = ();
168 my $count = 0;
169 my $columns = 3;
170 foreach my $part_svc ( @part_svc ) {
171   my $svcpart = $part_svc->svcpart;
172   my $pkg_svc = $pkg_svc{$svcpart}
173              || new FS::pkg_svc ( {
174                                    'pkgpart'     => $pkgpart,
175                                    'svcpart'     => $svcpart,
176                                    'quantity'    => 0,
177                                    'primary_svc' => '',
178                                 } );
179
180   push @fixups, "pkg_svc$svcpart";
181
182 %>
183
184   <TR>
185     <TD>
186       <INPUT TYPE="text" NAME="pkg_svc<%= $svcpart %>" SIZE=4 MAXLENGTH=3 VALUE="<%= $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0 %>">
187     </TD>
188    
189     <TD>
190       <INPUT TYPE="radio" NAME="pkg_svc_primary" VALUE="<%= $svcpart %>" <%= $pkg_svc->primary_svc =~ /^Y/i ? ' CHECKED' : '' %>>
191     </TD>
192
193     <TD>
194       <A HREF="part_svc.cgi?<%= $part_svc->svcpart %>"><%= $part_svc->svc %></A>      <%= $part_svc->disabled =~ /^Y/i ? ' (DISABLED' : '' %>
195     </TD>
196   </TR>
197
198   <% foreach ( 1 .. $columns-1 ) {
199        if ( $count == int( $_ * scalar(@part_svc) / $columns ) ) { 
200   %>
201          </TABLE></TD><TD VALIGN="top"><%= $thead %>
202
203   <%   }
204      }
205      $count++;
206   %>
207
208 <% } %>
209
210 </TR></TABLE></TD></TR></TABLE>
211
212 <% foreach my $f ( qw( clone pkgnum ) ) { %>
213   <INPUT TYPE="hidden" NAME="<%= $f %>" VALUE="<%= $cgi->param($f) %>">
214 <% } %>
215 <INPUT TYPE="hidden" NAME="pkgpart" VALUE="<%= $part_pkg->pkgpart %>">
216
217 <%
218
219 # prolly should be in database
220 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
221
222 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
223                     split("\n", ($clone_part_pkg||$part_pkg)->plandata );
224 #warn join("\n", map { "$_: $plandata{$_}" } keys %plandata ). "\n";
225
226 tie my %options, 'Tie::IxHash', map { $_=>$plans{$_}->{'name'} } keys %plans;
227
228 my @form_select = ('classnum');
229 if ( $conf->exists('enable_taxclasses') ) {
230   push @form_select, 'taxclass';
231 } else {
232   push @fixups, 'taxclass'; #hidden
233 }
234
235 my @form_radio = ();
236 if ( dbdef->table('pkg_svc')->column('primary_svc') ) {
237   push @form_radio, 'pkg_svc_primary';
238 }
239
240 tie my %freq, 'Tie::IxHash', %{FS::part_pkg->freqs_href()};
241 if ( $part_pkg->dbdef_table->column('freq')->type =~ /(int)/i ) {
242   delete $freq{$_} foreach grep { ! /^\d+$/ } keys %freq;
243 }
244
245 my $widget = new HTML::Widgets::SelectLayers(
246   'selected_layer' => $part_pkg->plan,
247   'options'        => \%options,
248   'form_name'      => 'dummy',
249   'form_action'    => 'process/part_pkg.cgi',
250   'form_text'      => [ qw(pkg comment promo_code clone pkgnum pkgpart),
251                         @fixups
252                       ],
253   'form_checkbox'  => [ qw(setuptax recurtax disabled) ],
254   'form_radio'     => \@form_radio,
255   'form_select'    => \@form_select,
256   'layer_callback' => sub {
257     my $layer = shift;
258     my $html = qq!<INPUT TYPE="hidden" NAME="plan" VALUE="$layer">!.
259                ntable("#cccccc",2);
260     $html .= '
261       <TR>
262         <TD ALIGN="right">Recurring fee frequency </TD>
263         <TD><SELECT NAME="freq">
264     ';
265
266     my @freq = keys %freq;
267     @freq = grep { /^\d+$/ } @freq
268       if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
269     foreach my $freq ( @freq ) {
270       $html .= qq(<OPTION VALUE="$freq");
271       $html .= ' SELECTED' if $freq eq $part_pkg->freq;
272       $html .= ">$freq{$freq}";
273     }
274     $html .= '</SELECT></TD></TR>';
275
276     my $href = $plans{$layer}->{'fields'};
277     foreach my $field ( exists($plans{$layer}->{'fieldorder'})
278                           ? @{$plans{$layer}->{'fieldorder'}}
279                           : keys %{ $href }
280                       ) {
281
282       $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
283
284       if ( ! exists($href->{$field}{'type'}) ) {
285         $html .= qq!<INPUT TYPE="text" NAME="$field" VALUE="!.
286                  ( exists($plandata{$field})
287                      ? $plandata{$field}
288                      : $href->{$field}{'default'} ).
289                  qq!" onChange="fchanged(this)">!;
290       } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
291         $html .= qq!<INPUT TYPE="checkbox" NAME="$field" VALUE=1 !.
292                  ( exists($plandata{$field}) && $plandata{$field}
293                    ? ' CHECKED'
294                    : ''
295                  ). '>';
296       } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
297         $html .= '<SELECT';
298         $html .= ' MULTIPLE'
299           if $href->{$field}{'type'} eq 'select_multiple';
300         $html .= qq! NAME="$field" onChange="fchanged(this)">!;
301
302         if ( $href->{$field}{'select_table'} ) {
303           foreach my $record (
304             qsearch( $href->{$field}{'select_table'},
305                      $href->{$field}{'select_hash'}   )
306           ) {
307             my $value = $record->getfield($href->{$field}{'select_key'});
308             $html .= qq!<OPTION VALUE="$value"!.
309                      (  $plandata{$field} =~ /(^|, *)$value *(,|$)/
310                           ? ' SELECTED'
311                           : ''
312                      ).
313                      '>'. $record->getfield($href->{$field}{'select_label'});
314           }
315         } elsif ( $href->{$field}{'select_options'} ) {
316           foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
317             my $value = $href->{$field}{'select_options'}{$key};
318             $html .= qq!<OPTION VALUE="$key"!.
319                      ( $plandata{$field} =~ /(^|, *)$value *(,|$)/
320                          ? ' SELECTED'
321                          : ''
322                      ).
323                      '>'. $value;
324           }
325
326         } else {
327           $html .= '<font color="#ff0000">warning: '.
328                    "don't know how to retreive options for $field select field".
329                    '</font>';
330         }
331         $html .= '</SELECT>';
332       }
333
334       $html .= '</TD></TR>';
335     }
336     $html .= '</TABLE>';
337
338     $html .= '<INPUT TYPE="hidden" NAME="plandata" VALUE="'.
339              join(',', keys %{ $href } ). '">'.
340              '<BR><BR>';
341              
342     $html .= '<INPUT TYPE="submit" VALUE="'.
343              ( $hashref->{pkgpart} ? "Apply changes" : "Add package" ).
344              '" onClick="fchanged(this)">';
345
346     $html;
347
348   },
349 );
350
351 %>
352
353 <BR><BR>Price plan <%= $widget->html %>
354   </BODY>
355 </HTML>