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