1 <% include( 'elements/browse.html',
2 'title' => 'Package Definitions',
3 'menubar' => \@menubar,
4 'html_init' => $html_init,
5 'html_form' => $html_form,
6 'html_posttotal' => $html_posttotal,
7 'name' => 'package definitions',
9 'disabled_statuspos' => 4,
11 'agent_null_right' => [ $edit, $edit_global ],
12 'agent_null_right_link' => $edit_global,
14 'query' => { 'select' => $select,
15 'table' => 'part_pkg',
17 'extra_sql' => $extra_sql,
18 'order_by' => "ORDER BY $orderby"
20 'count_query' => $count_query,
25 'link_field' => 'pkgpart',
26 'html_init' => $html_init,
27 'html_foot' => $html_foot,
32 my $curuser = $FS::CurrentUser::CurrentUser;
34 my $edit = 'Edit package definitions';
35 my $edit_global = 'Edit global package definitions';
36 my $acl_edit = $curuser->access_right($edit);
37 my $acl_edit_global = $curuser->access_right($edit_global);
38 my $acl_config = $curuser->access_right('Configuration'); #to edit services
41 my $acl_edit_bulk = $curuser->access_right('Bulk edit package definitions');
44 unless $acl_edit || $acl_edit_global;
46 my $conf = new FS::Conf;
47 my $taxclasses = $conf->exists('enable_taxclasses');
48 my $money_char = $conf->config('money_char') || '$';
51 my $orderby = 'pkgpart';
56 if ( $cgi->param('active') ) {
57 $orderby = 'num_active DESC';
62 #if ( $cgi->param('activeONLY') ) {
63 # push @where, ' WHERE num_active > 0 '; #XXX doesn't affect count...
66 if ( $cgi->param('recurring') ) {
67 $hash{'freq'} = { op=>'!=', value=>'0' };
68 $extra_count = " freq != '0' ";
72 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
74 push @where, $classnum ? "classnum = $classnum"
77 $cgi->delete('classnum');
79 if ( $cgi->param('missing_recur_fee') ) {
80 push @where, "0 = ( SELECT COUNT(*) FROM part_pkg_option
81 WHERE optionname = 'recur_fee'
82 AND part_pkg_option.pkgpart = part_pkg.pkgpart
83 AND CAST( optionvalue AS NUMERIC ) > 0
87 if ( $cgi->param('family') =~ /^(\d+)$/ ) {
89 push @where, "family_pkgpart = $1";
90 # Hiding disabled or one-time charges and limiting by classnum aren't
91 # very useful in this mode, so all links should still refer back to the
92 # non-family-limited display.
93 $cgi->param('showdisabled', 1);
94 $cgi->delete('family');
97 push @where, FS::part_pkg->curuser_pkgs_sql
98 unless $acl_edit_global;
100 my $extra_sql = scalar(@where)
101 ? ( scalar(keys %hash) ? ' AND ' : ' WHERE ' ).
102 join( 'AND ', @where)
105 my $agentnums_sql = $curuser->agentnums_sql( 'table'=>'cust_main' );
106 my $count_cust_pkg = "
107 SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
108 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
117 AND ( setup IS NULL OR setup = 0 )
118 AND ( cancel IS NULL OR cancel = 0 )
119 AND ( susp IS NULL OR susp = 0 )
120 ) AS num_not_yet_billed,
123 AND setup IS NOT NULL AND setup != 0
124 AND ( cancel IS NULL OR cancel = 0 )
125 AND ( susp IS NULL OR susp = 0 )
129 AND ( cancel IS NULL OR cancel = 0 )
130 AND susp IS NOT NULL AND susp != 0
134 AND cancel IS NOT NULL AND cancel != 0
140 One or more service definitions are grouped together into a package
141 definition and given pricing information. Customers purchase packages
142 rather than purchase services directly.<BR><BR>
143 <FORM METHOD="GET" ACTION="${p}edit/part_pkg.cgi">
144 <A HREF="${p}edit/part_pkg.cgi"><I>Add a new package definition</I></A>
146 !.include('/elements/select-part_pkg.html', 'element_name' => 'clone' ). qq!
147 <INPUT TYPE="submit" VALUE="Clone existing package">
152 $cgi->param('dummy', 1);
155 qq(\n<SCRIPT TYPE="text/javascript">\n).
156 "function filter_change() {".
157 " window.location = '". $cgi->self_url.
158 ";classnum=' + document.getElementById('classnum').options[document.getElementById('classnum').selectedIndex].value".
162 #restore this so pagination works
163 $cgi->param('classnum', $classnum) if length($classnum);
165 #should hide this if there aren't any classes
167 "$filter_change\n<BR>( show class: ".
168 include('/elements/select-pkg_class.html',
169 #'curr_value' => $classnum,
170 'value' => $classnum, #insist on 0 :/
171 'onchange' => 'filter_change()',
172 'pre_options' => [ '-1' => 'all',
174 'disable_empty' => 1,
178 my $recur_toggle = $cgi->param('recurring') ? 'show' : 'hide';
179 $cgi->param('recurring', $cgi->param('recurring') ^ 1 );
182 '( <A HREF="'. $cgi->self_url.'">'. "$recur_toggle one-time charges</A> )";
184 $cgi->param('recurring', $cgi->param('recurring') ^ 1 ); #put it back
188 my $link = [ $p.'edit/part_pkg.cgi?', 'pkgpart' ];
190 my @header = ( '#', 'Package', 'Comment', 'Custom' );
191 my @fields = ( 'pkgpart', 'pkg', 'comment',
192 sub{ '<B><FONT COLOR="#0000CC">'.$_[0]->custom.'</FONT></B>' }
195 my @links = ( $link, $link, '', '' );
197 unless ( 0 ) { #already showing only one class or something?
198 push @header, 'Class';
199 push @fields, sub { shift->classname || '(none)'; };
203 if ( $conf->exists('pkg-addon_classnum') ) {
204 push @header, "Add'l order class";
205 push @fields, sub { shift->addon_classname || '(none)'; };
209 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
211 tie my %plan_labels, 'Tie::IxHash',
212 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
215 push @header, 'Pricing';
218 my $part_pkg = shift;
219 (my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ / /g;
220 my $is_recur = ( $part_pkg->freq ne '0' );
221 my @discounts = sort { $a->months <=> $b->months }
223 $part_pkg->part_pkg_discount;
226 ( !$family_pkgpart &&
227 $part_pkg->pkgpart == $part_pkg->family_pkgpart ? () : [
232 'data' => '<b>Show all versions</b>',
233 'link' => $p.'browse/part_pkg.cgi?family='.$part_pkg->family_pkgpart,
243 { data =>$money_char.
244 sprintf('%.2f ', $part_pkg->option('setup_fee') ),
247 { data => ( ( $is_recur ? ' setup' : ' one-time' ).
248 ( $part_pkg->option('recur_fee') == 0
249 && $part_pkg->setup_show_zero
250 ? ' (printed on invoices)'
260 ? $money_char. sprintf('%.2f', $part_pkg->option('recur_fee'))
261 : $part_pkg->freq_pretty
263 align=> ( $is_recur ? 'right' : 'center' ),
264 colspan=> ( $is_recur ? 1 : 2 ),
267 ? { data => ( $is_recur
268 ? ' '. $part_pkg->freq_pretty.
269 ( $part_pkg->option('recur_fee') == 0
270 && $part_pkg->recur_show_zero
271 ? ' (printed on invoices)'
280 ( map { my $dst_pkg = $_->dst_pkg;
282 { data => 'Supplemental: '.
283 '<A HREF="#'. $dst_pkg->pkgpart . '">' .
284 $dst_pkg->pkg . '</A>',
290 $part_pkg->supp_part_pkg_link
293 my $dst_pkg = $_->dst_pkg;
295 { data => 'Add-on: '.$dst_pkg->pkg_comment,
301 $part_pkg->bill_part_pkg_link
305 { data => '<b>Discounts</b>',
315 { data => $_->months. ':',
318 { data => $_->amount ? '$'. $_->amount : $_->percent. '%'
327 # $plan_labels{$part_pkg->plan}.'<BR>'.
328 # $money_char.sprintf('%.2f setup<BR>', $part_pkg->option('setup_fee') ).
329 # ( $part_pkg->freq ne '0'
330 # ? $money_char.sprintf('%.2f ', $part_pkg->option('recur_fee') )
333 # $part_pkg->freq_pretty; #.'<BR>'
337 # Agent goes here if displayed
341 if ( $acl_edit_global ) {
342 #really we just want a count, but this is fine unless someone has tons
343 my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
344 if ( scalar(@all_agent_types) > 1 ) {
345 push @header, 'Agent types';
346 my $typelink = $p. 'edit/agent_type.cgi?';
347 push @fields, sub { my $part_pkg = shift;
349 map { my $agent_type = $_->agent_type;
351 { 'data' => $agent_type->atype, #escape?
353 'link' => ( $acl_config
368 #if ( $cgi->param('active') ) {
369 push @header, 'Customer<BR>packages';
371 'not yet billed' => '009999', #teal? cyan?
372 'active' => '00CC00',
373 'suspended' => 'FF9900',
374 'cancelled' => 'FF0000',
375 #'one-time charge' => '000000',
376 'charge' => '000000',
378 my $cust_pkg_link = $p. 'search/cust_pkg.cgi?pkgpart=';
379 push @fields, sub { my $part_pkg = shift;
384 if ( $magic eq 'active' && $part_pkg->freq == 0 ) {
386 #$label = 'one-time charge',
389 $label= 'not yet billed' if $magic eq 'not_yet_billed';
393 'data' => '<B><FONT COLOR="#'. $col{$label}. '">'.
394 $part_pkg->get("num_$_").
400 ( $part_pkg->get("num_$_") != 1
401 && $label =~ /charge$/
406 'link' => ( $part_pkg->get("num_$_")
414 } (qw( not_yet_billed active suspended cancelled ))
418 { 'data' => '<FONT SIZE="-1">[ '.
419 include('/elements/popup_link.html',
421 'action' => "${p}edit/bulk-cust_pkg.html?".
422 'pkgpart='.$part_pkg->pkgpart,
423 'actionlabel' => 'Change Packages',
436 push @header, 'Taxclass';
437 push @fields, sub { shift->taxclass() || ' '; };
441 # make a table of report class optionnames => the actual
442 my %report_optionname_name = map { 'report_option_'.$_->num, $_->name }
443 qsearch('part_pkg_report_option', { disabled => '' });
445 push @header, 'Plan options',
447 #'Service', 'Quan', 'Primary';
451 my $part_pkg = shift;
452 if ( $part_pkg->plan ) {
454 my %options = $part_pkg->options;
455 # gather any options that are really report options,
456 # convert them to their user-friendly names,
457 # and sort them (I think?)
460 map { $report_optionname_name{$_} }
462 and exists($report_optionname_name{$_}) }
471 { 'data' => $part_pkg->format($_,$options{$_}),
476 grep { $options{$_} =~ /\S/ }
477 grep { $_ !~ /^(setup|recur)_fee$/
478 and $_ !~ /^report_option_\d+$/ }
481 if ( @report_options ) {
483 [ { 'data' => 'Report classes',
485 'style' => 'font-weight: bold',
488 foreach (@report_options) {
495 } # foreach @report_options
496 } # if @report_options
500 } else { # should never happen...
507 'data' => $part_pkg->$_(),
520 my $part_pkg = shift;
521 my @part_pkg_usage = sort { $a->priority <=> $b->priority }
522 $part_pkg->part_pkg_usage;
527 my $part_svc = $pkg_svc->part_svc;
528 my $svc = $part_svc->svc;
529 if ( $pkg_svc->primary_svc =~ /^Y/i ) {
530 $svc = "<B>$svc (PRIMARY)</B>";
532 $svc =~ s/ +/ /g;
536 'data' => '<B>'. $pkg_svc->quantity. '</B>',
542 'link' => ( $acl_config
543 ? $p. 'edit/part_svc.cgi?'.
550 sort { $b->primary_svc =~ /^Y/i
551 <=> $a->primary_svc =~ /^Y/i
553 $part_pkg->pkg_svc('disable_linked'=>1)
556 my $dst_pkg = $_->dst_pkg;
558 { data => 'Add-on: '.$dst_pkg->pkg_comment,
564 $part_pkg->svc_part_pkg_link
566 ( scalar(@part_pkg_usage) ?
567 [ { data => 'Usage minutes',
571 link => $p.'browse/part_pkg_usage.html#pkgpart'.
577 [ { data => $_->minutes,
580 { data => $_->description,
590 $align .= 'lrl'; #rr';
594 my $count_extra_sql = $extra_sql;
595 $count_extra_sql =~ s/^\s*AND /WHERE /i;
596 $extra_count = ( $count_extra_sql ? ' AND ' : ' WHERE ' ). $extra_count
598 my $count_query = "SELECT COUNT(*) FROM part_pkg $count_extra_sql $extra_count";
602 if ( $acl_edit_bulk ) {
603 # insert a checkbox column
606 '<INPUT TYPE="checkbox" NAME="pkgpart" VALUE=' . $_[0]->pkgpart .'>';
610 $html_form = qq!<FORM ACTION="${p}edit/bulk-part_pkg.html" METHOD="POST">!;
611 $html_foot = include('/search/elements/checkbox-foot.html',
612 submit => 'edit report classes', # for now it's only report classes
617 # show this if there are any voip_cdr packages defined
618 if ( FS::part_pkg->count("plan = 'voip_cdr'") ) {
619 push @menubar, 'Per-package usage minutes' => $p.'browse/part_pkg_usage.html';