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