finish up useful agent stuff on the config editor: adding, deleting overrides too...
[freeside.git] / httemplate / browse / part_pkg.cgi
1 <% include( 'elements/browse.html',
2                  'title'                 => 'Package Definitions',
3                  'html_init'             => $html_init,
4                  'name'                  => 'package definitions',
5                  'disableable'           => 1,
6                  'disabled_statuspos'    => 3,
7                  'agent_virt'            => 1,
8                  'agent_null_right'      => [ $edit, $edit_global ],
9                  'agent_null_right_link' => $edit_global,
10                  'agent_pos'             => 5,
11                  'query'                 => { 'select'    => $select,
12                                               'table'     => 'part_pkg',
13                                               'hashref'   => \%hash,
14                                               'extra_sql' => $extra_sql,
15                                               'order_by'  => "ORDER BY $orderby"
16                                             },
17                  'count_query'           => $count_query,
18                  'header'                => \@header,
19                  'fields'                => \@fields,
20                  'links'                 => \@links,
21                  'align'                 => $align,
22              )
23 %>
24 <%init>
25
26 my $curuser = $FS::CurrentUser::CurrentUser;
27
28 my $edit        = 'Edit package definitions';
29 my $edit_global = 'Edit global package definitions';
30 my $acl_edit        = $curuser->access_right($edit);
31 my $acl_edit_global = $curuser->access_right($edit_global);
32 my $acl_config      = $curuser->access_right('Configuration'); #to edit services
33                                                                #and agent types
34
35 die "access denied"
36   unless $acl_edit || $acl_edit_global;
37
38 my $conf = new FS::Conf;
39 my $taxclasses = $conf->exists('enable_taxclasses');
40 my $money_char = $conf->config('money_char') || '$';
41
42 my $select = '*';
43 my $orderby = 'pkgpart';
44 my %hash = ();
45 my $extra_count = '';
46
47 if ( $cgi->param('active') ) {
48   $orderby = 'num_active DESC';
49 }
50
51 my $extra_sql = '';
52
53 #if ( $cgi->param('activeONLY') ) {
54 #  $extra_sql = ' WHERE num_active > 0 '; #XXX doesn't affect count...
55 #}
56
57 if ( $cgi->param('recurring') ) {
58   $hash{'freq'} = { op=>'!=', value=>'0' };
59   $extra_count = ' freq != 0 ';
60 }
61
62 if ( $cgi->param('missing_recur_fee') ) {
63   my $missing = "0 = ( SELECT COUNT(*) FROM part_pkg_option
64                          WHERE optionname = 'recur_fee'
65                            AND part_pkg_option.pkgpart = part_pkg.pkgpart
66                            AND CAST ( optionvalue AS NUMERIC ) > 0
67                      )";
68   $extra_sql .= ( ( scalar(keys %hash) || $extra_sql ) ? ' AND ' : ' WHERE ' ).
69                 $missing;
70 }
71
72 unless ( $acl_edit_global ) {
73   $extra_sql .= ( ( scalar(keys %hash) || $extra_sql ) ? ' AND ' : ' WHERE ' ).
74                  FS::part_pkg->curuser_pkgs_sql;
75 }
76
77 my $agentnums = join(',', $curuser->agentnums);
78 my $count_cust_pkg = "
79   SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
80     WHERE cust_pkg.pkgpart = part_pkg.pkgpart
81       AND cust_main.agentnum IN ($agentnums)
82 ";
83
84 $select = "
85
86   *,
87
88   ( $count_cust_pkg
89       AND ( cancel IS NULL OR cancel = 0 )
90       AND ( susp IS NULL OR susp = 0 )
91   ) AS num_active,
92
93   ( $count_cust_pkg
94       AND ( cancel IS NULL OR cancel = 0 )
95       AND susp IS NOT NULL AND susp != 0
96   ) AS num_suspended,
97
98   ( $count_cust_pkg
99       AND cancel IS NOT NULL AND cancel != 0
100   ) AS num_cancelled
101
102 ";
103
104 my $html_init;
105 #unless ( $cgi->param('active') ) {
106   $html_init = qq!
107     One or more service definitions are grouped together into a package 
108     definition and given pricing information.  Customers purchase packages
109     rather than purchase services directly.<BR><BR>
110     <FORM METHOD="POST" ACTION="${p}edit/part_pkg.cgi">
111     <A HREF="${p}edit/part_pkg.cgi"><I>Add a new package definition</I></A>
112     or
113     !.include('/elements/select-part_pkg.html', 'element_name' => 'clone' ). qq!
114     <INPUT TYPE="submit" VALUE="Clone existing package">
115     </FORM>
116     <BR><BR>
117   !;
118 #}
119
120 # ------
121
122 my $link = [ $p.'edit/part_pkg.cgi?', 'pkgpart' ];
123
124 my @header = ( '#', 'Package', 'Comment' );
125 my @fields = ( 'pkgpart', 'pkg', 'comment' );
126 my $align = 'rll';
127 my @links = ( $link, $link, '' );
128
129 unless ( 0 ) { #already showing only one class or something?
130   push @header, 'Class';
131   push @fields, sub { shift->classname || '(none)'; };
132   $align .= 'l';
133 }
134
135 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
136
137 tie my %plan_labels, 'Tie::IxHash',
138   map {  $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
139       keys %plans;
140
141 push @header, 'Pricing';
142 $align .= 'r'; #?
143 push @fields, sub {
144   my $part_pkg = shift;
145   (my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ /&nbsp;/g;
146   my $is_recur = ( $part_pkg->freq ne '0' );
147
148   [
149     [
150       { data =>$plan,
151         align=>'center',
152         colspan=>2,
153       },
154     ],
155     [
156       { data =>$money_char.
157                sprintf('%.2f', $part_pkg->option('setup_fee') ),
158         align=>'right'
159       },
160       { data => ( $is_recur ? ' setup' : ' one-time' ),
161         align=>'left',
162       },
163     ],
164     [
165       { data=>( $is_recur
166                   ? $money_char.sprintf('%.2f ', $part_pkg->option('recur_fee') )
167                   : $part_pkg->freq_pretty
168               ),
169         align=> ( $is_recur ? 'right' : 'center' ),
170         colspan=> ( $is_recur ? 1 : 2 ),
171       },
172       ( $is_recur
173         ?  { data => ( $is_recur ? $part_pkg->freq_pretty : '' ),
174              align=>'left',
175            }
176         : ()
177       ),
178     ],
179     ( map { 
180             my $dst_pkg = $_->dst_pkg;
181             [ 
182               { data => 'Add-on:&nbsp;'.$dst_pkg->pkg_comment,
183                 align=>'center', #?
184                 colspan=>2,
185               }
186             ]
187           }
188       $part_pkg->bill_part_pkg_link
189     ),
190   ];
191
192 #  $plan_labels{$part_pkg->plan}.'<BR>'.
193 #    $money_char.sprintf('%.2f setup<BR>', $part_pkg->option('setup_fee') ).
194 #    ( $part_pkg->freq ne '0'
195 #      ? $money_char.sprintf('%.2f ', $part_pkg->option('recur_fee') )
196 #      : ''
197 #    ).
198 #    $part_pkg->freq_pretty; #.'<BR>'
199 };
200
201 ###
202 # Agent goes here if displayed
203 ###
204
205 #agent type
206 if ( $acl_edit_global ) {
207   #really we just want a count, but this is fine unless someone has tons
208   my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
209   if ( scalar(@all_agent_types) > 1 ) {
210     push @header, 'Agent types';
211     my $typelink = $p. 'edit/agent_type.cgi?';
212     push @fields, sub { my $part_pkg = shift;
213                         [
214                           map { my $agent_type = $_->agent_type;
215                                 [ 
216                                   { 'data'  => $agent_type->atype, #escape?
217                                     'align' => 'left',
218                                     'link'  => ( $acl_config
219                                                    ? $typelink.
220                                                      $agent_type->typenum
221                                                    : ''
222                                                ),
223                                   },
224                                 ];
225                               }
226                               $part_pkg->type_pkgs
227                         ];
228                       };
229     $align .= 'l';
230   }
231 }
232
233 #if ( $cgi->param('active') ) {
234   push @header, 'Customer<BR>packages';
235   my %col = (
236     'active'          => '00CC00',
237     'suspended'       => 'FF9900',
238     'cancelled'       => 'FF0000',
239     #'one-time charge' => '000000',
240     'charge'          => '000000',
241   );
242   my $cust_pkg_link = $p. 'search/cust_pkg.cgi?pkgpart=';
243   push @fields, sub { my $part_pkg = shift;
244                       [
245                         map {
246                               my $magic = $_;
247                               my $label = $_;
248                               if ( $magic eq 'active' && $part_pkg->freq == 0 ) {
249                                 $magic = 'inactive';
250                                 #$label = 'one-time charge',
251                                 $label = 'charge',
252                               }
253                           
254                               [
255                                 {
256                                  'data'  => '<B><FONT COLOR="#'. $col{$label}. '">'.
257                                             $part_pkg->get("num_$_").
258                                             '</FONT></B>',
259                                  'align' => 'right',
260                                 },
261                                 {
262                                  'data'  => $label.
263                                               ( $part_pkg->get("num_$_") != 1
264                                                 && $label =~ /charge$/
265                                                   ? 's'
266                                                   : ''
267                                               ),
268                                  'align' => 'left',
269                                  'link'  => ( $part_pkg->get("num_$_")
270                                                 ? $cust_pkg_link.
271                                                   $part_pkg->pkgpart.
272                                                   ";magic=$magic"
273                                                 : ''
274                                             ),
275                                 },
276                               ],
277                             } (qw( active suspended cancelled ))
278                       ]; };
279   $align .= 'r';
280 #}
281
282 if ( $taxclasses ) {
283   push @header, 'Taxclass';
284   push @fields, sub { shift->taxclass() || '&nbsp;'; };
285   $align .= 'l';
286 }
287
288 push @header, 'Plan options',
289               'Services';
290               #'Service', 'Quan', 'Primary';
291
292 push @fields, 
293               sub {
294                     my $part_pkg = shift;
295                     if ( $part_pkg->plan ) {
296
297                       my %options = $part_pkg->options;
298
299                       [ map { 
300                               [
301                                 { 'data'  => $_,
302                                   'align' => 'right',
303                                 },
304                                 { 'data'  => $part_pkg->format($_,$options{$_}),
305                                   'align' => 'left',
306                                 },
307                               ];
308                             }
309                         grep { $options{$_} =~ /\S/ } 
310                         grep { $_ !~ /^(setup|recur)_fee$/ }
311                         keys %options
312                       ];
313
314                     } else {
315
316                       [ map { [
317                                 { 'data'  => uc($_),
318                                   'align' => 'right',
319                                 },
320                                 {
321                                   'data'  => $part_pkg->$_(),
322                                   'align' => 'left',
323                                 },
324                               ];
325                             }
326                         (qw(setup recur))
327                       ];
328
329                     }
330
331                   },
332
333               sub {
334                     my $part_pkg = shift;
335
336                     [ 
337                       (map {
338                              my $pkg_svc = $_;
339                              my $part_svc = $pkg_svc->part_svc;
340                              my $svc = $part_svc->svc;
341                              if ( $pkg_svc->primary_svc =~ /^Y/i ) {
342                                $svc = "<B>$svc (PRIMARY)</B>";
343                              }
344                              $svc =~ s/ +/&nbsp;/g;
345
346                              [
347                                {
348                                  'data'  => '<B>'. $pkg_svc->quantity. '</B>',
349                                  'align' => 'right'
350                                },
351                                {
352                                  'data'  => $svc,
353                                  'align' => 'left',
354                                  'link'  => ( $acl_config
355                                                 ? $p. 'edit/part_svc.cgi?'.
356                                                   $part_svc->svcpart
357                                                 : ''
358                                             ),
359                                },
360                              ];
361                            }
362                       sort {     $b->primary_svc =~ /^Y/i
363                              <=> $a->primary_svc =~ /^Y/i
364                            }
365                            $part_pkg->pkg_svc('disable_linked'=>1)
366                       ),
367                       ( map { 
368                               my $dst_pkg = $_->dst_pkg;
369                               [
370                                 { data => 'Add-on:&nbsp;'.$dst_pkg->pkg_comment,
371                                   align=>'center', #?
372                                   colspan=>2,
373                                 }
374                               ]
375                             }
376                         $part_pkg->svc_part_pkg_link
377                       )
378                     ];
379
380                   };
381
382 $align .= 'lrl'; #rr';
383
384 # --------
385
386 my $count_extra_sql = $extra_sql;
387 $count_extra_sql =~ s/^\s*AND /WHERE /i;
388 $extra_count = ( $count_extra_sql ? ' AND ' : ' WHERE ' ). $extra_count
389   if $extra_count;
390 my $count_query = "SELECT COUNT(*) FROM part_pkg $count_extra_sql $extra_count";
391
392 </%init>