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