checkpoint tax editors and correct a blunder
[freeside.git] / httemplate / edit / part_pkg.cgi
1 <% include('/elements/header.html', "$action Package Definition", menubar(
2   'View all packages' => popurl(2). 'browse/part_pkg.cgi',
3 )) %>
4 % #), ' onLoad="visualize()"'); 
5
6 <SCRIPT TYPE="text/javascript" SRC="<%$fsurl%>elements/overlibmws.js"></SCRIPT>
7 <SCRIPT TYPE="text/javascript" SRC="<%$fsurl%>elements/overlibmws_iframe.js"></SCRIPT>
8 <SCRIPT TYPE="text/javascript" SRC="<%$fsurl%>elements/overlibmws_draggable.js"></SCRIPT>
9 <SCRIPT TYPE="text/javascript" SRC="<%$fsurl%>elements/iframecontentmws.js"></SCRIPT>
10
11 <% include('/elements/error.html') %>
12
13 <FORM NAME="dummy">
14
15 <% itable('',8,1) %><TR><TD VALIGN="top">
16
17 Package information
18
19 <% ntable("#cccccc",2) %>
20   <TR>
21     <TD ALIGN="right">Package Definition #</TD>
22     <TD BGCOLOR="#ffffff">
23       <% $hashref->{pkgpart} ? $hashref->{pkgpart} : "(NEW)" %>
24     </TD>
25   </TR>
26   <TR>
27     <TD ALIGN="right">Package (customer-visible)</TD>
28     <TD>
29       <INPUT TYPE="text" NAME="pkg" SIZE=32 VALUE="<% $part_pkg->pkg %>">
30     </TD>
31   </TR>
32   <TR>
33     <TD ALIGN="right">Comment (customer-hidden)</TD>
34     <TD>
35       <INPUT TYPE="text" NAME="comment" SIZE=32 VALUE="<%$part_pkg->comment%>">
36     </TD>
37   </TR>
38   <% include( '/elements/tr-select-pkg_class.html',
39                 'curr_value' => $part_pkg->classnum,
40             )
41   %>
42   <TR>
43     <TD ALIGN="right">Promotional code</TD>
44     <TD>
45       <INPUT TYPE="text" NAME="promo_code" SIZE=32 VALUE="<%$part_pkg->promo_code%>">
46     </TD>
47   </TR>
48   <TR>
49     <TD ALIGN="right">Disable new orders</TD>
50     <TD>
51       <INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"<% $hashref->{disabled} eq 'Y' ? ' CHECKED' : '' %>
52     </TD>
53   </TR>
54
55 </TABLE>
56
57 </TD><TD VALIGN="top">
58
59 Tax information
60 <% ntable("#cccccc", 2) %>
61   <TR>
62     <TD ALIGN="right">Setup fee tax exempt</TD>
63     <TD>
64       <INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y" <% $hashref->{setuptax} eq 'Y' ? ' CHECKED' : '' %>>
65     </TD>
66   </TR>
67   <TR>
68     <TD ALIGN="right">Recurring fee tax exempt</TD>
69     <TD>
70       <INPUT TYPE="checkbox" NAME="recurtax" VALUE="Y" <% $hashref->{recurtax} eq 'Y' ? ' CHECKED' : '' %>>
71     </TD>
72   </TR>
73
74 % if ( $conf->exists('enable_taxclasses') ) { 
75
76   <TR>
77     <TD align="right">Tax class</TD>
78     <TD>
79       <% include('/elements/select-taxclass.html', $hashref->{taxclass} ) %>
80     </TD>
81   </TR>
82
83 % } else { 
84
85   <% include('/elements/select-taxclass.html', $hashref->{taxclass} ) %>
86
87 % } 
88
89 % if ( $conf->exists('enable_taxproducts') ) { 
90
91   <TR><TD colspan="2">
92     <% ntable("#cccccc", 2) %>
93       <TR>
94         <TD align="right">Tax product</TD>
95         <TD>
96           <INPUT name="part_pkg_taxproduct_taxproductnum" id="taxproductnum" type="hidden" value="<% $hashref->{'taxproductnum'}%>">
97           <INPUT name="part_pkg_taxproduct_description" id="taxproduct_description" type="text" value="<% $taxproduct_description %>" size="12" onclick="overlib( OLiframeContent('part_pkg_taxproduct.html?'+document.getElementById('taxproductnum').value, 1000, 400, 'tax_product_popup'), CAPTION, 'Select product', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK); return false;">
98         </TD>
99       </TR>
100       <TR>
101         <TD colspan="2" align="right">
102           <INPUT name="tax_override" id="tax_override" type="hidden" value="<% $tax_override %>">
103           <A href="javascript:void(0)" onclick="overlib( OLiframeContent('part_pkg_taxoverride.html?selected='+document.getElementById('tax_override').value, 1100, 600, 'tax_product_popup'), CAPTION, 'Edit product tax overrides', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK); return false;">
104             <% $tax_override ? 'Edit tax overrides' : 'Override taxes' %>
105           </A>
106         </TD>
107       </TR>
108     </TABLE>
109   </TD></TR>
110
111 % } else { 
112
113   <INPUT TYPE="hidden" NAME="taxproductnum" VALUE="<% $hashref->{taxproductnum} %>">
114
115 % } 
116
117 </TABLE>
118 <BR>
119
120 Line-item revenue recognition
121 <% ntable("#cccccc", 2) %>
122 % tie my %weight, 'Tie::IxHash',
123 %   'pay_weight'    => 'Payment',
124 %   'credit_weight' => 'Credit'
125 % ;
126 % foreach my $weight (keys %weight) {
127     <TR>
128       <TD ALIGN="right"><% $weight{$weight} %> weight</TD>
129       <TD>
130         <INPUT TYPE="text" NAME="<% $weight %>" SIZE=6 VALUE=<% $hashref->{$weight} || 0 %>>
131       </TD>
132     </TR>
133 % }
134 </TABLE>
135
136 </TD><TD VALIGN="top">
137
138 % if ( $cgi->param('clone') ) {
139
140     <INPUT TYPE="hidden" NAME="agent_type" VALUE="">
141
142 % } elsif ( scalar(@all_agent_types) == 1) {
143
144     <INPUT TYPE="hidden" NAME="agent_type" VALUE="<% $all_agent_types[0] %>">
145
146 % } else {
147
148     Reseller information
149     <% ntable("#cccccc", 2) %>
150       <TR>
151         <TD ALIGN="right"><% 'Agent Types' %></TD>
152         <TD>
153           <% include( '/elements/select-table.html',
154                       'element_name' => 'agent_type',
155                       'table'        => 'agent_type',
156                       'name_col'     => 'atype',
157                       'value'        => \@agent_type,
158                       'multiple'     =>  '1',
159                       'element_etc'  => 'size="10"',
160                     )
161           %>
162         </TD>
163       </TR>
164     </TABLE>
165
166 % }
167
168 </TD></TR></TABLE>
169
170 %my $thead =  "\n\n". ntable('#cccccc', 2).
171 %             '<TR><TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Quan.</FONT></TH>'.
172 %             '<TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Primary</FONT></TH>'.
173 %             '<TH BGCOLOR="#dcdcdc">Service</TH></TR>';
174
175 <BR><BR>Services included
176 <% itable('', 4, 1) %><TR><TD VALIGN="top">
177 <% $thead %>
178 %
179 %
180 %my $where =  "WHERE disabled IS NULL OR disabled = ''";
181 %if ( $pkgpart ) {
182 %  $where .=  "   OR 0 < ( SELECT quantity FROM pkg_svc
183 %                           WHERE pkg_svc.svcpart = part_svc.svcpart
184 %                             AND pkgpart = $pkgpart
185 %                        )";
186 %}
187 %my @part_svc = qsearch('part_svc', {}, '', $where);
188 %my $q_part_pkg = $clone_part_pkg || $part_pkg;
189 %my %pkg_svc = map { $_->svcpart => $_ } $q_part_pkg->pkg_svc;
190 %
191 %my @fixups = ();
192 %my $count = 0;
193 %my $columns = 3;
194 %foreach my $part_svc ( @part_svc ) {
195 %  my $svcpart = $part_svc->svcpart;
196 %  my $pkg_svc = $pkg_svc{$svcpart}
197 %             || new FS::pkg_svc ( {
198 %                                   'pkgpart'     => $pkgpart,
199 %                                   'svcpart'     => $svcpart,
200 %                                   'quantity'    => 0,
201 %                                   'primary_svc' => '',
202 %                                } );
203 %  if ( $cgi->param('error') ) {
204 %    my $primary_svc = ( $pkg_svc->primary_svc =~ /^Y/i );
205 %    my $pkg_svc_primary = scalar($cgi->param('pkg_svc_primary'));
206 %    $pkg_svc->primary_svc('')
207 %      if $primary_svc && $pkg_svc_primary != $svcpart;
208 %    $pkg_svc->primary_svc('Y')
209 %      if ! $primary_svc && $pkg_svc_primary == $svcpart;
210 %  }
211 %
212 %  push @fixups, "pkg_svc$svcpart";
213 %
214 %  my $quan = 0;
215 %  if ( $cgi->param("pkg_svc$svcpart") =~ /^\s*(\d+)\s*$/ ) {
216 %    $quan = $1;
217 %  } elsif ( $pkg_svc->quantity ) {
218 %    $quan = $pkg_svc->quantity;
219 %  }
220
221
222   <TR>
223     <TD>
224       <INPUT TYPE="text" NAME="pkg_svc<% $svcpart %>" SIZE=4 MAXLENGTH=3 VALUE="<% $quan %>">
225     </TD>
226    
227     <TD>
228       <INPUT TYPE="radio" NAME="pkg_svc_primary" VALUE="<% $svcpart %>" <% $pkg_svc->primary_svc =~ /^Y/i ? ' CHECKED' : '' %>>
229     </TD>
230
231     <TD>
232       <A HREF="part_svc.cgi?<% $part_svc->svcpart %>"><% $part_svc->svc %></A>      <% $part_svc->disabled =~ /^Y/i ? ' (DISABLED' : '' %>
233     </TD>
234   </TR>
235 % foreach ( 1 .. $columns-1 ) {
236 %       if ( $count == int( $_ * scalar(@part_svc) / $columns ) ) { 
237 %  
238
239          </TABLE></TD><TD VALIGN="top"><% $thead %>
240 %   }
241 %     }
242 %     $count++;
243 %  
244 % } 
245
246
247 </TR></TABLE></TD></TR></TABLE>
248 % foreach my $f ( qw( clone pkgnum ) ) { #safe, these were untained in %init 
249     <INPUT TYPE="hidden" NAME="<% $f %>" VALUE="<% $cgi->param($f) %>">
250 % }
251
252 <INPUT TYPE="hidden" NAME="pkgpart" VALUE="<% $part_pkg->pkgpart %>">
253 %
254 %
255 %# prolly should be in database
256 %tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
257 %
258 %my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
259 %                    split("\n", ($clone_part_pkg||$part_pkg)->plandata );
260 %#warn join("\n", map { "$_: $plandata{$_}" } keys %plandata ). "\n";
261 %
262 %tie my %options, 'Tie::IxHash', map { $_=>$plans{$_}->{'name'} } keys %plans;
263 %
264 %#my @form_select = ('classnum');
265 %#if ( $conf->exists('enable_taxclasses') ) {
266 %#  push @form_select, 'taxclass';
267 %#} else {
268 %#  push @fixups, 'taxclass'; #hidden
269 %#}
270 %my @form_elements = ( 'classnum', 'taxclass', 'agent_type', 'tax_override' );
271 %
272 %my @form_radio = ( 'pkg_svc_primary' );
273 %
274 %tie my %freq, 'Tie::IxHash', %{FS::part_pkg->freqs_href()};
275 %if ( $part_pkg->dbdef_table->column('freq')->type =~ /(int)/i ) {
276 %  delete $freq{$_} foreach grep { ! /^\d+$/ } keys %freq;
277 %}
278 %
279 %#this should be replaced by /elements/selectlayers.html
280 %my $widget = new HTML::Widgets::SelectLayers(
281 %  'selected_layer' => $part_pkg->plan,
282 %  'options'        => \%options,
283 %  'form_name'      => 'dummy',
284 %  'form_action'    => 'process/part_pkg.cgi',
285 %  'form_elements'  => \@form_elements,
286 %  'form_text'      => [ qw(pkg comment promo_code clone pkgnum pkgpart),
287 %                        qw(pay_weight credit_weight), #keys(%weight),
288 %                        qw(taxproductnum),
289 %                        @fixups,
290 %                      ],
291 %  'form_checkbox'  => [ qw(setuptax recurtax disabled) ],
292 %  'form_radio'     => \@form_radio,
293 %  'layer_callback' => sub {
294 %    my $layer = shift;
295 %    my $html = qq!<INPUT TYPE="hidden" NAME="plan" VALUE="$layer">!.
296 %               ntable("#cccccc",2);
297 %    $html .= '
298 %      <TR>
299 %        <TD ALIGN="right">Recurring fee frequency </TD>
300 %        <TD><SELECT NAME="freq">
301 %    ';
302 %
303 %    my @freq = keys %freq;
304 %    @freq = grep { /^\d+$/ } @freq
305 %      if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
306 %    foreach my $freq ( @freq ) {
307 %      $html .= qq(<OPTION VALUE="$freq");
308 %      $html .= ' SELECTED' if $freq eq $part_pkg->freq;
309 %      $html .= ">$freq{$freq}";
310 %    }
311 %    $html .= '</SELECT></TD></TR>';
312 %
313 %    my $href = $plans{$layer}->{'fields'};
314 %    foreach my $field ( exists($plans{$layer}->{'fieldorder'})
315 %                          ? @{$plans{$layer}->{'fieldorder'}}
316 %                          : keys %{ $href }
317 %                      ) {
318 %
319 %      $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
320 %
321 %      my $format = sub { shift };
322 %      $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
323 %
324 %      if ( ! exists($href->{$field}{'type'}) ) {
325 %
326 %        $html .= qq!<INPUT TYPE="text" NAME="$field" VALUE="!.
327 %                 ( exists($plandata{$field})
328 %                     ? &$format($plandata{$field})
329 %                     : $href->{$field}{'default'} ).
330 %                 qq!" onChange="fchanged(this)">!;
331 %
332 %      } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
333 %
334 %        $html .= qq!<INPUT TYPE="checkbox" NAME="$field" VALUE=1 !.
335 %                 ( exists($plandata{$field}) && $plandata{$field}
336 %                   ? ' CHECKED'
337 %                   : ''
338 %                 ). '>';
339 %
340 %      } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
341 %
342 %        $html .= '<SELECT';
343 %        $html .= ' MULTIPLE'
344 %          if $href->{$field}{'type'} eq 'select_multiple';
345 %        $html .= qq! NAME="$field" onChange="fchanged(this)">!;
346 %
347 %        if ( $href->{$field}{'select_table'} ) {
348 %          foreach my $record (
349 %            qsearch( $href->{$field}{'select_table'},
350 %                     $href->{$field}{'select_hash'}   )
351 %          ) {
352 %            my $value = $record->getfield($href->{$field}{'select_key'});
353 %            $html .= qq!<OPTION VALUE="$value"!.
354 %                     (  $plandata{$field} =~ /(^|, *)$value *(,|$)/
355 %                          ? ' SELECTED'
356 %                          : ''
357 %                     ).
358 %                     '>'. $record->getfield($href->{$field}{'select_label'});
359 %          }
360 %        } elsif ( $href->{$field}{'select_options'} ) {
361 %          foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
362 %            my $label = $href->{$field}{'select_options'}{$key};
363 %            $html .= qq!<OPTION VALUE="$key"!.
364 %                     ( $plandata{$field} =~ /(^|, *)$key *(,|$)/ #XXX fix
365 %                         ? ' SELECTED'
366 %                         : ''
367 %                     ).
368 %                     '>'. $label;
369 %          }
370 %
371 %        } else {
372 %          $html .= '<font color="#ff0000">warning: '.
373 %                   "don't know how to retreive options for $field select field".
374 %                   '</font>';
375 %        }
376 %        $html .= '</SELECT>';
377 %
378 %      } elsif ( $href->{$field}{'type'} eq 'radio' ) {
379 %
380 %        my $radio =
381 %          qq!<INPUT TYPE="radio" NAME="$field" onChange="fchanged(this)"!;
382 %
383 %        foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
384 %          my $label = $href->{$field}{'options'}{$key};
385 %          $html .= qq!$radio VALUE="$key"!.
386 %                   ( $plandata{$field} =~ /(^|, *)$key *(,|$)/ #XXX fix
387 %                       ? ' CHECKED'
388 %                       : ''
389 %                   ).
390 %                   "> $label<BR>";
391 %        }
392 %
393 %      }
394 %
395 %      $html .= '</TD></TR>';
396 %    }
397 %    $html .= '</TABLE>';
398 %
399 %    $html .= '<INPUT TYPE="hidden" NAME="plandata" VALUE="'.
400 %             join(',', keys %{ $href } ). '">'.
401 %             '<BR><BR>';
402 %             
403 %    $html .= '<INPUT TYPE="submit" VALUE="'.
404 %               ( $action eq 'Custom'
405 %                   ? 'Customize package'
406 %                   : ( $hashref->{pkgpart} ? "Apply changes" : "Add package" )
407 %               ).
408 %             '" onClick="fchanged(this)">';
409 %
410 %    $html;
411 %
412 %  },
413 %);
414 %
415 %
416
417
418 <BR><BR>Price plan <% $widget->html %>
419
420 <% include('/elements/footer.html') %>
421 <%init>
422
423 if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {
424   $cgi->param('clone', $1);
425 } else {
426   $cgi->param('clone', '');
427 }
428 if ( $cgi->param('pkgnum') && $cgi->param('pkgnum') =~ /^(\d+)$/ ) {
429   $cgi->param('pkgnum', $1);
430 } else {
431   $cgi->param('pkgnum', '');
432 }
433
434 my $curuser = $FS::CurrentUser::CurrentUser;
435
436 die "access denied"
437   unless $curuser->access_right('Edit package definitions')
438       || $curuser->access_right('Edit global package definitions')
439       || ( $cgi->param('pkgnum') && $curuser->access_right('Customize customer package') );
440
441 my ($query) = $cgi->keywords;
442
443 my $conf = new FS::Conf; 
444 my $part_pkg = '';
445 my @agent_type = ();
446 my $tax_override;
447 my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
448 if ( $cgi->param('error') ) {
449   $part_pkg = new FS::part_pkg ( {
450     map { $_, scalar($cgi->param($_)) } fields('part_pkg')
451   } );
452   (@agent_type) = $cgi->param('agent_type');
453   $tax_override = $cgi->param('tax_override');
454 }
455
456 my $action = '';
457 my $clone_part_pkg = '';
458 my $pkgpart = '';
459 if ( $cgi->param('clone') ) {
460   $pkgpart = $cgi->param('clone');
461   #$action = 'Custom Pricing';
462   $action = 'Custom';
463   $clone_part_pkg= qsearchs('part_pkg', { 'pkgpart' => $cgi->param('clone') } );
464   $part_pkg ||= $clone_part_pkg->clone;
465   $part_pkg->disabled('Y') unless $cgi->param('error');
466 } elsif ( $query && $query =~ /^(\d+)$/ ) {
467   (@agent_type) = map {$_->typenum} qsearch('type_pkgs',{'pkgpart'=>$1})
468     unless $part_pkg;
469   $tax_override =
470     join (",", map {$_->taxclassnum}
471                qsearch('part_pkg_taxoverride',{'pkgpart'=>$1}))
472     unless $part_pkg;
473   $part_pkg ||= qsearchs('part_pkg',{'pkgpart'=>$1});
474   $pkgpart = $part_pkg->pkgpart;
475 } else {
476   unless ( $part_pkg ) {
477     $part_pkg = new FS::part_pkg {};
478     $part_pkg->plan('flat');
479     @agent_type = @all_agent_types if $conf->exists('agent_defaultpkg');
480       
481   }
482 }
483 unless ( $part_pkg->plan ) { #backwards-compat
484   $part_pkg->plan('flat');
485   $part_pkg->plandata("setup_fee=". $part_pkg->setup. "\n".
486                       "recur_fee=". $part_pkg->recur. "\n");
487 }
488 $action ||= $part_pkg->pkgpart ? 'Edit' : 'Add';
489 my $hashref = $part_pkg->hashref;
490 my $taxproduct_description = $part_pkg->taxproduct_description;
491
492 </%init>