fix UI for package editing w/recur_show_zero, add setup_show_zero, RT#9777
[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 my $html_posttotal =
152   "$filter_change\n<BR>( show class: ".
153   include('/elements/select-pkg_class.html',
154             #'curr_value'    => $classnum,
155             'value'         => $classnum, #insist on 0 :/
156             'onchange'      => 'filter_change()',
157             'pre_options'   => [ '-1' => 'all',
158                                  '0'  => '(none)', ],
159             'disable_empty' => 1,
160          ).
161   ' )';
162
163 my $recur_toggle = $cgi->param('recurring') ? 'show' : 'hide';
164 $cgi->param('recurring', $cgi->param('recurring') ^ 1 );
165
166 $html_posttotal .=
167   '( <A HREF="'. $cgi->self_url.'">'. "$recur_toggle one-time charges</A> )";
168
169 $cgi->param('recurring', $cgi->param('recurring') ^ 1 ); #put it back
170
171 # ------
172
173 my $link = [ $p.'edit/part_pkg.cgi?', 'pkgpart' ];
174
175 my @header = ( '#', 'Package', 'Comment', 'Custom' );
176 my @fields = ( 'pkgpart', 'pkg', 'comment',
177                sub{ '<B><FONT COLOR="#0000CC">'.$_[0]->custom.'</FONT></B>' }
178              );
179 my $align = 'rllc';
180 my @links = ( $link, $link, '', '' );
181
182 unless ( 0 ) { #already showing only one class or something?
183   push @header, 'Class';
184   push @fields, sub { shift->classname || '(none)'; };
185   $align .= 'l';
186 }
187
188 if ( $conf->exists('pkg-addon_classnum') ) {
189   push @header, "Add'l order class";
190   push @fields, sub { shift->addon_classname || '(none)'; };
191   $align .= 'l';
192 }
193
194 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
195
196 tie my %plan_labels, 'Tie::IxHash',
197   map {  $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
198       keys %plans;
199
200 push @header, 'Pricing';
201 $align .= 'r'; #?
202 push @fields, sub {
203   my $part_pkg = shift;
204   (my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ /&nbsp;/g;
205   my $is_recur = ( $part_pkg->freq ne '0' );
206   my @discounts = sort { $a->months <=> $b->months }
207                   map { $_->discount  }
208                   $part_pkg->part_pkg_discount;
209
210   [
211     [
212       { data =>$plan,
213         align=>'center',
214         colspan=>2,
215       },
216     ],
217     [
218       { data =>$money_char.
219                sprintf('%.2f', $part_pkg->option('setup_fee') ),
220         align=>'right'
221       },
222       { data => ( ( $is_recur ? ' setup' : ' one-time' ).
223                   ( $part_pkg->option('recur_fee') == 0
224                       && $part_pkg->setup_show_zero
225                     ? ' (printed on invoices)'
226                     : ''
227                   )
228                 ),
229         align=>'left',
230       },
231     ],
232     [
233       { data=>(
234           $is_recur
235             ? $money_char. sprintf('%.2f ', $part_pkg->option('recur_fee'))
236             : $part_pkg->freq_pretty
237         ),
238         align=> ( $is_recur ? 'right' : 'center' ),
239         colspan=> ( $is_recur ? 1 : 2 ),
240       },
241       ( $is_recur
242         ?  { data => ( $is_recur
243                ? $part_pkg->freq_pretty.
244                  ( $part_pkg->option('recur_fee') == 0
245                      && $part_pkg->recur_show_zero
246                    ? ' (printed on invoices)'
247                    : ''
248                  )
249                : '' ),
250              align=>'left',
251            }
252         : ()
253       ),
254     ],
255     ( map { 
256             my $dst_pkg = $_->dst_pkg;
257             [ 
258               { data => 'Add-on:&nbsp;'.$dst_pkg->pkg_comment,
259                 align=>'center', #?
260                 colspan=>2,
261               }
262             ]
263           }
264       $part_pkg->bill_part_pkg_link
265     ),
266     ( scalar(@discounts)
267         ?  [ 
268               { data => '<b>Discounts</b>',
269                 align=>'center', #?
270                 colspan=>2,
271               }
272             ]
273         : ()  
274     ),
275     ( scalar(@discounts)
276         ? map { 
277             [ 
278               { data  => $_->months. ':',
279                 align => 'right',
280               },
281               { data => $_->amount ? '$'. $_->amount : $_->percent. '%'
282               }
283             ]
284           }
285           @discounts
286         : ()
287     ),
288   ];
289
290 #  $plan_labels{$part_pkg->plan}.'<BR>'.
291 #    $money_char.sprintf('%.2f setup<BR>', $part_pkg->option('setup_fee') ).
292 #    ( $part_pkg->freq ne '0'
293 #      ? $money_char.sprintf('%.2f ', $part_pkg->option('recur_fee') )
294 #      : ''
295 #    ).
296 #    $part_pkg->freq_pretty; #.'<BR>'
297 };
298
299 ###
300 # Agent goes here if displayed
301 ###
302
303 #agent type
304 if ( $acl_edit_global ) {
305   #really we just want a count, but this is fine unless someone has tons
306   my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
307   if ( scalar(@all_agent_types) > 1 ) {
308     push @header, 'Agent types';
309     my $typelink = $p. 'edit/agent_type.cgi?';
310     push @fields, sub { my $part_pkg = shift;
311                         [
312                           map { my $agent_type = $_->agent_type;
313                                 [ 
314                                   { 'data'  => $agent_type->atype, #escape?
315                                     'align' => 'left',
316                                     'link'  => ( $acl_config
317                                                    ? $typelink.
318                                                      $agent_type->typenum
319                                                    : ''
320                                                ),
321                                   },
322                                 ];
323                               }
324                               $part_pkg->type_pkgs
325                         ];
326                       };
327     $align .= 'l';
328   }
329 }
330
331 #if ( $cgi->param('active') ) {
332   push @header, 'Customer<BR>packages';
333   my %col = (
334     'not yet billed'  => '009999', #teal? cyan?
335     'active'          => '00CC00',
336     'suspended'       => 'FF9900',
337     'cancelled'       => 'FF0000',
338     #'one-time charge' => '000000',
339     'charge'          => '000000',
340   );
341   my $cust_pkg_link = $p. 'search/cust_pkg.cgi?pkgpart=';
342   push @fields, sub { my $part_pkg = shift;
343                         [
344                         map( {
345                               my $magic = $_;
346                               my $label = $_;
347                               if ( $magic eq 'active' && $part_pkg->freq == 0 ) {
348                                 $magic = 'inactive';
349                                 #$label = 'one-time charge',
350                                 $label = 'charge',
351                               }
352                               $label= 'not yet billed' if $magic eq 'not_yet_billed';
353                           
354                               [
355                                 {
356                                  'data'  => '<B><FONT COLOR="#'. $col{$label}. '">'.
357                                             $part_pkg->get("num_$_").
358                                             '</FONT></B>',
359                                  'align' => 'right',
360                                 },
361                                 {
362                                  'data'  => $label.
363                                               ( $part_pkg->get("num_$_") != 1
364                                                 && $label =~ /charge$/
365                                                   ? 's'
366                                                   : ''
367                                               ),
368                                  'align' => 'left',
369                                  'link'  => ( $part_pkg->get("num_$_")
370                                                 ? $cust_pkg_link.
371                                                   $part_pkg->pkgpart.
372                                                   ";magic=$magic"
373                                                 : ''
374                                             ),
375                                 },
376                               ],
377                             } (qw( not_yet_billed active suspended cancelled ))
378                           ),
379                       ($acl_config ? 
380                         [ {}, 
381                           { 'data'  => '<FONT SIZE="-1">[ '.
382                               include('/elements/popup_link.html',
383                                 'label'       => 'change',
384                                 'action'      => "${p}edit/bulk-cust_pkg.html?".
385                                                  'pkgpart='.$part_pkg->pkgpart,
386                                 'actionlabel' => 'Change Packages',
387                                 'width'       => 569,
388                                 'height'      => 210,
389                               ).' ]</FONT>',
390                             'align' => 'left',
391                           } 
392                         ] : () ),
393                       ]; 
394   };
395   $align .= 'r';
396 #}
397
398 if ( $taxclasses ) {
399   push @header, 'Taxclass';
400   push @fields, sub { shift->taxclass() || '&nbsp;'; };
401   $align .= 'l';
402 }
403
404 push @header, 'Plan options',
405               'Services';
406               #'Service', 'Quan', 'Primary';
407
408 push @fields, 
409               sub {
410                     my $part_pkg = shift;
411                     if ( $part_pkg->plan ) {
412
413                       my %options = $part_pkg->options;
414
415                       [ map { 
416                               [
417                                 { 'data'  => $_,
418                                   'align' => 'right',
419                                 },
420                                 { 'data'  => $part_pkg->format($_,$options{$_}),
421                                   'align' => 'left',
422                                 },
423                               ];
424                             }
425                         grep { $options{$_} =~ /\S/ } 
426                         grep { $_ !~ /^(setup|recur)_fee$/ }
427                         keys %options
428                       ];
429
430                     } else {
431
432                       [ map { [
433                                 { 'data'  => uc($_),
434                                   'align' => 'right',
435                                 },
436                                 {
437                                   'data'  => $part_pkg->$_(),
438                                   'align' => 'left',
439                                 },
440                               ];
441                             }
442                         (qw(setup recur))
443                       ];
444
445                     }
446
447                   },
448
449               sub {
450                     my $part_pkg = shift;
451
452                     [ 
453                       (map {
454                              my $pkg_svc = $_;
455                              my $part_svc = $pkg_svc->part_svc;
456                              my $svc = $part_svc->svc;
457                              if ( $pkg_svc->primary_svc =~ /^Y/i ) {
458                                $svc = "<B>$svc (PRIMARY)</B>";
459                              }
460                              $svc =~ s/ +/&nbsp;/g;
461
462                              [
463                                {
464                                  'data'  => '<B>'. $pkg_svc->quantity. '</B>',
465                                  'align' => 'right'
466                                },
467                                {
468                                  'data'  => $svc,
469                                  'align' => 'left',
470                                  'link'  => ( $acl_config
471                                                 ? $p. 'edit/part_svc.cgi?'.
472                                                   $part_svc->svcpart
473                                                 : ''
474                                             ),
475                                },
476                              ];
477                            }
478                       sort {     $b->primary_svc =~ /^Y/i
479                              <=> $a->primary_svc =~ /^Y/i
480                            }
481                            $part_pkg->pkg_svc('disable_linked'=>1)
482                       ),
483                       ( map { 
484                               my $dst_pkg = $_->dst_pkg;
485                               [
486                                 { data => 'Add-on:&nbsp;'.$dst_pkg->pkg_comment,
487                                   align=>'center', #?
488                                   colspan=>2,
489                                 }
490                               ]
491                             }
492                         $part_pkg->svc_part_pkg_link
493                       )
494                     ];
495
496                   };
497
498 $align .= 'lrl'; #rr';
499
500 # --------
501
502 my $count_extra_sql = $extra_sql;
503 $count_extra_sql =~ s/^\s*AND /WHERE /i;
504 $extra_count = ( $count_extra_sql ? ' AND ' : ' WHERE ' ). $extra_count
505   if $extra_count;
506 my $count_query = "SELECT COUNT(*) FROM part_pkg $count_extra_sql $extra_count";
507
508 </%init>