don't redirect to a GET with sensitive data, RT#26099
[freeside.git] / httemplate / browse / part_pkg.cgi
1 <% include( 'elements/browse.html',
2                  'title'                 => 'Package Definitions',
3                  'html_init'             => $html_init,
4                  'html_form'             => $html_form,
5                  'html_posttotal'        => $html_posttotal,
6                  'name'                  => 'package definitions',
7                  'disableable'           => 1,
8                  'disabled_statuspos'    => 4,
9                  'agent_virt'            => 1,
10                  'agent_null_right'      => [ $edit, $edit_global ],
11                  'agent_null_right_link' => $edit_global,
12                  'agent_pos'             => 6,
13                  'query'                 => { 'select'    => $select,
14                                               'table'     => 'part_pkg',
15                                               'hashref'   => \%hash,
16                                               'extra_sql' => $extra_sql,
17                                               'order_by'  => "ORDER BY $orderby"
18                                             },
19                  'count_query'           => $count_query,
20                  'header'                => \@header,
21                  'fields'                => \@fields,
22                  'links'                 => \@links,
23                  'align'                 => $align,
24                  'html_init'             => $html_init,
25                  'html_foot'             => $html_foot,
26              )
27 %>
28 <%init>
29
30 my $curuser = $FS::CurrentUser::CurrentUser;
31
32 my $edit        = 'Edit package definitions';
33 my $edit_global = 'Edit global package definitions';
34 my $acl_edit        = $curuser->access_right($edit);
35 my $acl_edit_global = $curuser->access_right($edit_global);
36 my $acl_config      = $curuser->access_right('Configuration'); #to edit services
37                                                                #and agent types
38                                                                #and bulk change
39 my $acl_edit_bulk   = $curuser->access_right('Bulk edit package definitions');
40
41 die "access denied"
42   unless $acl_edit || $acl_edit_global;
43
44 my $conf = new FS::Conf;
45 my $taxclasses = $conf->exists('enable_taxclasses');
46 my $money_char = $conf->config('money_char') || '$';
47
48 my $select = '*';
49 my $orderby = 'pkgpart';
50 my %hash = ();
51 my $extra_count = '';
52
53 if ( $cgi->param('active') ) {
54   $orderby = 'num_active DESC';
55 }
56
57 my @where = ();
58
59 #if ( $cgi->param('activeONLY') ) {
60 #  push @where, ' WHERE num_active > 0 '; #XXX doesn't affect count...
61 #}
62
63 if ( $cgi->param('recurring') ) {
64   $hash{'freq'} = { op=>'!=', value=>'0' };
65   $extra_count = " freq != '0' ";
66 }
67
68 my $classnum = '';
69 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
70   $classnum = $1;
71   push @where, $classnum ? "classnum =  $classnum"
72                          : "classnum IS NULL";
73 }
74 $cgi->delete('classnum');
75
76 if ( $cgi->param('missing_recur_fee') ) {
77   push @where, "0 = ( SELECT COUNT(*) FROM part_pkg_option
78                         WHERE optionname = 'recur_fee'
79                           AND part_pkg_option.pkgpart = part_pkg.pkgpart
80                           AND CAST( optionvalue AS NUMERIC ) > 0
81                     )";
82 }
83
84 push @where, FS::part_pkg->curuser_pkgs_sql
85   unless $acl_edit_global;
86
87 my $extra_sql = scalar(@where)
88                 ? ( scalar(keys %hash) ? ' AND ' : ' WHERE ' ).
89                   join( 'AND ', @where)
90                 : '';
91
92 my $agentnums_sql = $curuser->agentnums_sql( 'table'=>'cust_main' );
93 my $count_cust_pkg = "
94   SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
95     WHERE cust_pkg.pkgpart = part_pkg.pkgpart
96       AND $agentnums_sql
97 ";
98
99 $select = "
100
101   *,
102
103   ( $count_cust_pkg
104       AND ( setup IS NULL OR setup = 0 )
105       AND ( cancel IS NULL OR cancel = 0 )
106       AND ( susp IS NULL OR susp = 0 )
107   ) AS num_not_yet_billed,
108
109   ( $count_cust_pkg
110       AND setup IS NOT NULL AND setup != 0
111       AND ( cancel IS NULL OR cancel = 0 )
112       AND ( susp IS NULL OR susp = 0 )
113   ) AS num_active,
114
115   ( $count_cust_pkg
116       AND ( cancel IS NULL OR cancel = 0 )
117       AND susp IS NOT NULL AND susp != 0
118   ) AS num_suspended,
119
120   ( $count_cust_pkg
121       AND cancel IS NOT NULL AND cancel != 0
122   ) AS num_cancelled
123
124 ";
125
126 my $html_init = qq!
127     One or more service definitions are grouped together into a package 
128     definition and given pricing information.  Customers purchase packages
129     rather than purchase services directly.<BR><BR>
130     <FORM METHOD="POST" ACTION="${p}edit/part_pkg.cgi">
131     <A HREF="${p}edit/part_pkg.cgi"><I>Add a new package definition</I></A>
132     or
133     !.include('/elements/select-part_pkg.html', 'element_name' => 'clone' ). qq!
134     <INPUT TYPE="submit" VALUE="Clone existing package">
135     </FORM>
136     <BR><BR>
137   !;
138
139 $cgi->param('dummy', 1);
140
141 my $filter_change =
142   qq(\n<SCRIPT TYPE="text/javascript">\n).
143   "function filter_change() {".
144   "  window.location = '". $cgi->self_url.
145        ";classnum=' + document.getElementById('classnum').options[document.getElementById('classnum').selectedIndex].value".
146   "}".
147   "\n</SCRIPT>\n";
148
149 #restore this so pagination works
150 $cgi->param('classnum', $classnum) if length($classnum);
151
152 #should hide this if there aren't any classes
153 my $html_posttotal =
154   "$filter_change\n<BR>( show class: ".
155   include('/elements/select-pkg_class.html',
156             #'curr_value'    => $classnum,
157             'value'         => $classnum, #insist on 0 :/
158             'onchange'      => 'filter_change()',
159             'pre_options'   => [ '-1' => 'all',
160                                  '0'  => '(none)', ],
161             'disable_empty' => 1,
162          ).
163   ' )';
164
165 my $recur_toggle = $cgi->param('recurring') ? 'show' : 'hide';
166 $cgi->param('recurring', $cgi->param('recurring') ^ 1 );
167
168 $html_posttotal .=
169   '( <A HREF="'. $cgi->self_url.'">'. "$recur_toggle one-time charges</A> )";
170
171 $cgi->param('recurring', $cgi->param('recurring') ^ 1 ); #put it back
172
173 # ------
174
175 my $link = [ $p.'edit/part_pkg.cgi?', 'pkgpart' ];
176
177 my @header = ( '#', 'Package', 'Comment', 'Custom' );
178 my @fields = ( 'pkgpart', 'pkg', 'comment',
179                sub{ '<B><FONT COLOR="#0000CC">'.$_[0]->custom.'</FONT></B>' }
180              );
181 my $align = 'rllc';
182 my @links = ( $link, $link, '', '' );
183
184 unless ( 0 ) { #already showing only one class or something?
185   push @header, 'Class';
186   push @fields, sub { shift->classname || '(none)'; };
187   $align .= 'l';
188 }
189
190 if ( $conf->exists('pkg-addon_classnum') ) {
191   push @header, "Add'l order class";
192   push @fields, sub { shift->addon_classname || '(none)'; };
193   $align .= 'l';
194 }
195
196 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
197
198 tie my %plan_labels, 'Tie::IxHash',
199   map {  $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
200       keys %plans;
201
202 push @header, 'Pricing';
203 $align .= 'r'; #?
204 push @fields, sub {
205   my $part_pkg = shift;
206   (my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ /&nbsp;/g;
207   my $is_recur = ( $part_pkg->freq ne '0' );
208   my @discounts = sort { $a->months <=> $b->months }
209                   map { $_->discount  }
210                   $part_pkg->part_pkg_discount;
211
212   [
213     [
214       { data =>$plan,
215         align=>'center',
216         colspan=>2,
217       },
218     ],
219     [
220       { data =>$money_char.
221                sprintf('%.2f', $part_pkg->option('setup_fee') ),
222         align=>'right'
223       },
224       { data => ( ( $is_recur ? ' setup' : ' one-time' ).
225                   ( $part_pkg->option('recur_fee') == 0
226                       && $part_pkg->setup_show_zero
227                     ? ' (printed on invoices)'
228                     : ''
229                   )
230                 ),
231         align=>'left',
232       },
233     ],
234     [
235       { data=>(
236           $is_recur
237             ? $money_char. sprintf('%.2f ', $part_pkg->option('recur_fee'))
238             : $part_pkg->freq_pretty
239         ),
240         align=> ( $is_recur ? 'right' : 'center' ),
241         colspan=> ( $is_recur ? 1 : 2 ),
242       },
243       ( $is_recur
244         ?  { data => ( $is_recur
245                ? $part_pkg->freq_pretty.
246                  ( $part_pkg->option('recur_fee') == 0
247                      && $part_pkg->recur_show_zero
248                    ? ' (printed on invoices)'
249                    : ''
250                  )
251                : '' ),
252              align=>'left',
253            }
254         : ()
255       ),
256     ],
257     ( map { 
258             my $dst_pkg = $_->dst_pkg;
259             [ 
260               { data => 'Add-on:&nbsp;'.$dst_pkg->pkg_comment,
261                 align=>'center', #?
262                 colspan=>2,
263               }
264             ]
265           }
266       $part_pkg->bill_part_pkg_link
267     ),
268     ( scalar(@discounts)
269         ?  [ 
270               { data => '<b>Discounts</b>',
271                 align=>'center', #?
272                 colspan=>2,
273               }
274             ]
275         : ()  
276     ),
277     ( scalar(@discounts)
278         ? map { 
279             [ 
280               { data  => $_->months. ':',
281                 align => 'right',
282               },
283               { data => $_->amount ? '$'. $_->amount : $_->percent. '%'
284               }
285             ]
286           }
287           @discounts
288         : ()
289     ),
290   ];
291
292 #  $plan_labels{$part_pkg->plan}.'<BR>'.
293 #    $money_char.sprintf('%.2f setup<BR>', $part_pkg->option('setup_fee') ).
294 #    ( $part_pkg->freq ne '0'
295 #      ? $money_char.sprintf('%.2f ', $part_pkg->option('recur_fee') )
296 #      : ''
297 #    ).
298 #    $part_pkg->freq_pretty; #.'<BR>'
299 };
300
301 ###
302 # Agent goes here if displayed
303 ###
304
305 #agent type
306 if ( $acl_edit_global ) {
307   #really we just want a count, but this is fine unless someone has tons
308   my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
309   if ( scalar(@all_agent_types) > 1 ) {
310     push @header, 'Agent types';
311     my $typelink = $p. 'edit/agent_type.cgi?';
312     push @fields, sub { my $part_pkg = shift;
313                         [
314                           map { my $agent_type = $_->agent_type;
315                                 [ 
316                                   { 'data'  => $agent_type->atype, #escape?
317                                     'align' => 'left',
318                                     'link'  => ( $acl_config
319                                                    ? $typelink.
320                                                      $agent_type->typenum
321                                                    : ''
322                                                ),
323                                   },
324                                 ];
325                               }
326                               $part_pkg->type_pkgs
327                         ];
328                       };
329     $align .= 'l';
330   }
331 }
332
333 #if ( $cgi->param('active') ) {
334   push @header, 'Customer<BR>packages';
335   my %col = (
336     'not yet billed'  => '009999', #teal? cyan?
337     'active'          => '00CC00',
338     'suspended'       => 'FF9900',
339     'cancelled'       => 'FF0000',
340     #'one-time charge' => '000000',
341     'charge'          => '000000',
342   );
343   my $cust_pkg_link = $p. 'search/cust_pkg.cgi?pkgpart=';
344   push @fields, sub { my $part_pkg = shift;
345                         [
346                         map( {
347                               my $magic = $_;
348                               my $label = $_;
349                               if ( $magic eq 'active' && $part_pkg->freq == 0 ) {
350                                 $magic = 'inactive';
351                                 #$label = 'one-time charge',
352                                 $label = 'charge',
353                               }
354                               $label= 'not yet billed' if $magic eq 'not_yet_billed';
355                           
356                               [
357                                 {
358                                  'data'  => '<B><FONT COLOR="#'. $col{$label}. '">'.
359                                             $part_pkg->get("num_$_").
360                                             '</FONT></B>',
361                                  'align' => 'right',
362                                 },
363                                 {
364                                  'data'  => $label.
365                                               ( $part_pkg->get("num_$_") != 1
366                                                 && $label =~ /charge$/
367                                                   ? 's'
368                                                   : ''
369                                               ),
370                                  'align' => 'left',
371                                  'link'  => ( $part_pkg->get("num_$_")
372                                                 ? $cust_pkg_link.
373                                                   $part_pkg->pkgpart.
374                                                   ";magic=$magic"
375                                                 : ''
376                                             ),
377                                 },
378                               ],
379                             } (qw( not_yet_billed active suspended cancelled ))
380                           ),
381                       ($acl_config ? 
382                         [ {}, 
383                           { 'data'  => '<FONT SIZE="-1">[ '.
384                               include('/elements/popup_link.html',
385                                 'label'       => 'change',
386                                 'action'      => "${p}edit/bulk-cust_pkg.html?".
387                                                  'pkgpart='.$part_pkg->pkgpart,
388                                 'actionlabel' => 'Change Packages',
389                                 'width'       => 569,
390                                 'height'      => 210,
391                               ).' ]</FONT>',
392                             'align' => 'left',
393                           } 
394                         ] : () ),
395                       ]; 
396   };
397   $align .= 'r';
398 #}
399
400 if ( $taxclasses ) {
401   push @header, 'Taxclass';
402   push @fields, sub { shift->taxclass() || '&nbsp;'; };
403   $align .= 'l';
404 }
405
406 # make a table of report class optionnames =>  the actual 
407 my %report_optionname_name = map { 'report_option_'.$_->num, $_->name }
408   qsearch('part_pkg_report_option', { disabled => '' });
409
410 push @header, 'Plan options',
411               'Services';
412               #'Service', 'Quan', 'Primary';
413
414 push @fields, 
415               sub {
416                     my $part_pkg = shift;
417                     if ( $part_pkg->plan ) {
418
419                       my %options = $part_pkg->options;
420                       # gather any options that are really report options,
421                       # convert them to their user-friendly names,
422                       # and sort them (I think?)
423                       my @report_options =
424                         sort { $a cmp $b }
425                         map { $report_optionname_name{$_} }
426                         grep { $options{$_}
427                                and exists($report_optionname_name{$_}) }
428                         keys %options;
429
430                       my @rows = (
431                         map { 
432                               [
433                                 { 'data'  => "$_: ",
434                                   'align' => 'right',
435                                 },
436                                 { 'data'  => $part_pkg->format($_,$options{$_}),
437                                   'align' => 'left',
438                                 },
439                               ];
440                             }
441                         grep { $options{$_} =~ /\S/ } 
442                         grep { $_ !~ /^(setup|recur)_fee$/ 
443                                and $_ !~ /^report_option_\d+$/ }
444                         keys %options
445                       );
446                       if ( @report_options ) {
447                         push @rows,
448                           [ { 'data'  => 'Report classes',
449                               'align' => 'center',
450                               'style' => 'font-weight: bold',
451                               'colspan' => 2
452                             } ];
453                         foreach (@report_options) {
454                           push @rows, [
455                             { 'data'  => $_,
456                               'align' => 'center',
457                               'colspan' => 2
458                             }
459                           ];
460                         } # foreach @report_options
461                       } # if @report_options
462
463                       return \@rows;
464
465                     } else { # should never happen...
466
467                       [ map { [
468                                 { 'data'  => uc($_),
469                                   'align' => 'right',
470                                 },
471                                 {
472                                   'data'  => $part_pkg->$_(),
473                                   'align' => 'left',
474                                 },
475                               ];
476                             }
477                         (qw(setup recur))
478                       ];
479
480                     }
481
482                   },
483
484               sub {
485                     my $part_pkg = shift;
486
487                     [ 
488                       (map {
489                              my $pkg_svc = $_;
490                              my $part_svc = $pkg_svc->part_svc;
491                              my $svc = $part_svc->svc;
492                              if ( $pkg_svc->primary_svc =~ /^Y/i ) {
493                                $svc = "<B>$svc (PRIMARY)</B>";
494                              }
495                              $svc =~ s/ +/&nbsp;/g;
496
497                              [
498                                {
499                                  'data'  => '<B>'. $pkg_svc->quantity. '</B>',
500                                  'align' => 'right'
501                                },
502                                {
503                                  'data'  => $svc,
504                                  'align' => 'left',
505                                  'link'  => ( $acl_config
506                                                 ? $p. 'edit/part_svc.cgi?'.
507                                                   $part_svc->svcpart
508                                                 : ''
509                                             ),
510                                },
511                              ];
512                            }
513                       sort {     $b->primary_svc =~ /^Y/i
514                              <=> $a->primary_svc =~ /^Y/i
515                            }
516                            $part_pkg->pkg_svc('disable_linked'=>1)
517                       ),
518                       ( map { 
519                               my $dst_pkg = $_->dst_pkg;
520                               [
521                                 { data => 'Add-on:&nbsp;'.$dst_pkg->pkg_comment,
522                                   align=>'center', #?
523                                   colspan=>2,
524                                 }
525                               ]
526                             }
527                         $part_pkg->svc_part_pkg_link
528                       )
529                     ];
530
531                   };
532
533 $align .= 'lrl'; #rr';
534
535 # --------
536
537 my $count_extra_sql = $extra_sql;
538 $count_extra_sql =~ s/^\s*AND /WHERE /i;
539 $extra_count = ( $count_extra_sql ? ' AND ' : ' WHERE ' ). $extra_count
540   if $extra_count;
541 my $count_query = "SELECT COUNT(*) FROM part_pkg $count_extra_sql $extra_count";
542
543 my $html_form = '';
544 my $html_foot = '';
545 if ( $acl_edit_bulk ) {
546   # insert a checkbox column
547   push @header, '';
548   push @fields, sub {
549     '<INPUT TYPE="checkbox" NAME="pkgpart" VALUE=' . $_[0]->pkgpart .'>';
550   };
551   push @links, '';
552   $align .= 'c';
553   $html_form = qq!<FORM ACTION="${p}edit/bulk-part_pkg.html" METHOD="POST">!;
554   $html_foot = include('/search/elements/checkbox-foot.html',
555       submit  => 'edit report classes', # for now it's only report classes
556   ) . '</FORM>';
557 }
558
559 </%init>