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('pkgpartbatch') =~ /^([\w\/\-\:\. ]+)$/ ) {
80 push @where, "pkgpartbatch = '$1' ";
83 if ( $cgi->param('missing_recur_fee') ) {
84 push @where, "NOT EXISTS ( SELECT 1 FROM part_pkg_option
85 WHERE optionname = 'recur_fee'
86 AND part_pkg_option.pkgpart = part_pkg.pkgpart
87 AND CAST( optionvalue AS NUMERIC ) > 0
91 if ( $cgi->param('ratenum') =~ /^(\d+)$/ ) {
92 push @where, "EXISTS( SELECT 1 FROM part_pkg_option
93 WHERE optionname LIKE '%ratenum'
94 AND optionvalue = '$1'
95 AND part_pkg_option.pkgpart = part_pkg.pkgpart
99 if ( $cgi->param('family') =~ /^(\d+)$/ ) {
100 $family_pkgpart = $1;
101 push @where, "family_pkgpart = $1";
102 # Hiding disabled or one-time charges and limiting by classnum aren't
103 # very useful in this mode, so all links should still refer back to the
104 # non-family-limited display.
105 $cgi->param('showdisabled', 1);
106 $cgi->delete('family');
109 push @where, FS::part_pkg->curuser_pkgs_sql
110 unless $acl_edit_global;
112 my $extra_sql = scalar(@where)
113 ? ( scalar(keys %hash) ? ' AND ' : ' WHERE ' ).
114 join( 'AND ', @where)
117 my $agentnums_sql = $curuser->agentnums_sql( 'table'=>'cust_main' );
118 my $count_cust_pkg = "
119 SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
120 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
123 my $count_cust_pkg_cancel = "
124 SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
125 LEFT JOIN cust_pkg AS cust_pkg_next
126 ON (cust_pkg.pkgnum = cust_pkg_next.change_pkgnum)
127 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
129 AND cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0
137 AND ( setup IS NULL OR setup = 0 )
138 AND ( cancel IS NULL OR cancel = 0 )
139 AND ( susp IS NULL OR susp = 0 )
140 ) AS num_not_yet_billed,
143 AND setup IS NOT NULL AND setup != 0
144 AND ( cancel IS NULL OR cancel = 0 )
145 AND ( susp IS NULL OR susp = 0 )
149 AND ( cancel IS NULL OR cancel = 0 )
150 AND susp IS NOT NULL AND susp != 0
151 AND setup IS NOT NULL AND setup != 0
155 AND ( cancel IS NULL OR cancel = 0 )
156 AND susp IS NOT NULL AND susp != 0
157 AND ( setup IS NULL OR setup = 0 )
160 ( $count_cust_pkg_cancel
161 AND (cust_pkg_next.pkgnum IS NULL
162 OR cust_pkg_next.pkgpart != cust_pkg.pkgpart)
166 # About the num_cancelled expression: packages that were changed, but
167 # kept the same pkgpart, are considered "moved", not "canceled" (because
168 # this is the part_pkg UI). We could show the count of those but it's
169 # probably not interesting.
172 One or more service definitions are grouped together into a package
173 definition and given pricing information. Customers purchase packages
174 rather than purchase services directly.<BR><BR>
175 <FORM METHOD="GET" ACTION="${p}edit/part_pkg.cgi">
176 <A HREF="${p}edit/part_pkg.cgi"><I>Add a new package definition</I></A>
178 !.include('/elements/select-part_pkg.html', 'element_name' => 'clone' ). qq!
179 <INPUT TYPE="submit" VALUE="Clone existing package">
184 $cgi->param('dummy', 1);
187 qq(\n<SCRIPT TYPE="text/javascript">\n).
188 "function filter_change() {".
189 " window.location = '". $cgi->self_url.
190 ";classnum=' + document.getElementById('classnum').options[document.getElementById('classnum').selectedIndex].value".
194 #restore this so pagination works
195 $cgi->param('classnum', $classnum) if length($classnum);
197 #should hide this if there aren't any classes
199 "$filter_change\n<BR>( show class: ".
200 include('/elements/select-pkg_class.html',
201 #'curr_value' => $classnum,
202 'value' => $classnum, #insist on 0 :/
203 'onchange' => 'filter_change()',
204 'pre_options' => [ '-1' => 'all',
206 'disable_empty' => 1,
210 my $recur_toggle = $cgi->param('recurring') ? 'show' : 'hide';
211 $cgi->param('recurring', $cgi->param('recurring') ^ 1 );
214 '( <A HREF="'. $cgi->self_url.'">'. "$recur_toggle one-time charges</A> )";
216 $cgi->param('recurring', $cgi->param('recurring') ^ 1 ); #put it back
220 my $link = [ $p.'edit/part_pkg.cgi?', 'pkgpart' ];
222 my @header = ( '#', 'Package', 'Comment', 'Custom' );
223 my @fields = ( 'pkgpart', 'pkg', 'comment',
224 sub{ '<B><FONT COLOR="#0000CC">'.$_[0]->custom.'</FONT></B>' }
227 my @links = ( $link, $link, '', '' );
229 unless ( 0 ) { #already showing only one class or something?
230 push @header, 'Class';
231 push @fields, sub { shift->classname || '(none)'; };
235 if ( $conf->exists('pkg-addon_classnum') ) {
236 push @header, "Add'l order class";
237 push @fields, sub { shift->addon_classname || '(none)'; };
241 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
243 tie my %plan_labels, 'Tie::IxHash',
244 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
247 push @header, 'Pricing';
250 my $part_pkg = shift;
251 (my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ / /g;
252 my $is_recur = ( $part_pkg->freq ne '0' );
253 my @discounts = sort { $a->months <=> $b->months }
255 $part_pkg->part_pkg_discount;
258 ( !$family_pkgpart &&
259 $part_pkg->pkgpart == $part_pkg->family_pkgpart ? () : [
264 'data' => '<b>Show all versions</b>',
265 'link' => $p.'browse/part_pkg.cgi?family='.$part_pkg->family_pkgpart,
275 { data =>$money_char.
276 sprintf('%.2f ', $part_pkg->option('setup_fee') ),
279 { data => ( ( $is_recur ? ' setup' : ' one-time' ).
280 ( $part_pkg->option('recur_fee') == 0
281 && $part_pkg->setup_show_zero
282 ? ' (printed on invoices)'
292 ? $money_char. sprintf('%.2f', $part_pkg->option('recur_fee'))
293 : $part_pkg->freq_pretty
295 align=> ( $is_recur ? 'right' : 'center' ),
296 colspan=> ( $is_recur ? 1 : 2 ),
299 ? { data => ( $is_recur
300 ? ' '. $part_pkg->freq_pretty.
301 ( $part_pkg->option('recur_fee') == 0
302 && $part_pkg->recur_show_zero
303 ? ' (printed on invoices)'
312 ( map { my $dst_pkg = $_->dst_pkg;
314 { data => 'Supplemental: '.
315 '<A HREF="#'. $dst_pkg->pkgpart . '">' .
316 $dst_pkg->pkg . '</A>',
322 $part_pkg->supp_part_pkg_link
325 my $dst_pkg = $_->dst_pkg;
327 { data => 'Add-on: '.$dst_pkg->pkg_comment,
333 $part_pkg->bill_part_pkg_link
337 { data => '<b>Discounts</b>',
347 { data => $_->months. ':',
350 { data => $_->amount ? '$'. $_->amount : $_->percent. '%'
359 # $plan_labels{$part_pkg->plan}.'<BR>'.
360 # $money_char.sprintf('%.2f setup<BR>', $part_pkg->option('setup_fee') ).
361 # ( $part_pkg->freq ne '0'
362 # ? $money_char.sprintf('%.2f ', $part_pkg->option('recur_fee') )
365 # $part_pkg->freq_pretty; #.'<BR>'
368 push @header, 'Cost tracking';
371 my $part_pkg = shift;
372 #(my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ / /g;
373 my $is_recur = ( $part_pkg->freq ne '0' );
377 { data => ' ', # $plan,
383 { data =>$money_char.
384 sprintf('%.2f ', $part_pkg->setup_cost ),
387 { data => ( $is_recur ? ' setup' : ' one-time' ),
394 ? $money_char. sprintf('%.2f', $part_pkg->recur_cost)
395 : '(no recurring)' #$part_pkg->freq_pretty
397 align=> ( $is_recur ? 'right' : 'center' ),
398 colspan=> ( $is_recur ? 1 : 2 ),
401 ? { data => ( $is_recur
402 ? ' '. $part_pkg->freq_pretty
414 # Agent goes here if displayed
418 if ( $acl_edit_global ) {
419 #really we just want a count, but this is fine unless someone has tons
420 my @all_agent_types = map {$_->typenum}
421 qsearch('agent_type', { 'disabled'=>'' });
422 if ( scalar(@all_agent_types) > 1 ) {
423 push @header, 'Agent types';
424 my $typelink = $p. 'edit/agent_type.cgi?';
425 push @fields, sub { my $part_pkg = shift;
427 map { my $agent_type = $_->agent_type;
429 { 'data' => $agent_type->atype, #escape?
431 'link' => ( $acl_config
446 #if ( $cgi->param('active') ) {
447 push @header, 'Customer<BR>packages';
448 my %col = %{ FS::cust_pkg->statuscolors };
449 my $cust_pkg_link = $p. 'search/cust_pkg.cgi?pkgpart=';
450 push @fields, sub { my $part_pkg = shift;
455 if ( $magic eq 'active' && $part_pkg->freq == 0 ) {
457 #$label = 'one-time charge';
460 $label= 'not yet billed' if $magic eq 'not_yet_billed';
461 $label= 'on hold' if $magic eq 'on_hold';
465 'data' => '<B><FONT COLOR="#'. $col{$label}. '">'.
466 $part_pkg->get("num_$_").
472 ( $part_pkg->get("num_$_") != 1
473 && $label =~ /charge$/
478 'link' => ( $part_pkg->get("num_$_")
486 } (qw( on_hold not_yet_billed active suspended cancelled ))
490 { 'data' => '<FONT SIZE="-1">[ '.
491 include('/elements/popup_link.html',
493 'action' => "${p}edit/bulk-cust_pkg.html?".
494 'pkgpart='.$part_pkg->pkgpart,
495 'actionlabel' => 'Change Packages',
508 push @header, 'Taxclass';
509 push @fields, sub { shift->taxclass() || ' '; };
513 # make a table of report class optionnames => the actual
514 my %report_optionname_name = map { 'report_option_'.$_->num, $_->name }
515 qsearch('part_pkg_report_option', { disabled => '' });
517 push @header, 'Plan options',
519 #'Service', 'Quan', 'Primary';
523 my $part_pkg = shift;
524 if ( $part_pkg->plan ) {
526 my %options = $part_pkg->options;
527 # gather any options that are really report options,
528 # convert them to their user-friendly names,
529 # and sort them (I think?)
532 map { $report_optionname_name{$_} }
534 and exists($report_optionname_name{$_}) }
543 { 'data' => $part_pkg->format($_,$options{$_}),
549 grep { $options{$_} =~ /\S/ }
550 grep { $_ !~ /^(setup|recur)_fee$/
551 and $_ !~ /^report_option_\d+$/ }
554 if ( @report_options ) {
556 [ { 'data' => 'Report classes',
558 'style' => 'font-weight: bold',
561 foreach (@report_options) {
568 } # foreach @report_options
569 } # if @report_options
573 } else { # should never happen...
580 'data' => $part_pkg->$_(),
593 my $part_pkg = shift;
594 my @part_pkg_usage = sort { $a->priority <=> $b->priority }
595 $part_pkg->part_pkg_usage;
600 my $part_svc = $pkg_svc->part_svc;
601 my $svc = $part_svc->svc;
602 if ( $pkg_svc->primary_svc =~ /^Y/i ) {
603 $svc = "<B>$svc (PRIMARY)</B>";
605 $svc =~ s/ +/ /g;
609 'data' => '<B>'. $pkg_svc->quantity. '</B>',
615 'link' => ( $acl_config
616 ? $p. 'edit/part_svc.cgi?'.
623 sort { $b->primary_svc =~ /^Y/i
624 <=> $a->primary_svc =~ /^Y/i
626 $part_pkg->pkg_svc('disable_linked'=>1)
629 my $dst_pkg = $_->dst_pkg;
631 { data => 'Add-on: '.$dst_pkg->pkg_comment,
637 $part_pkg->svc_part_pkg_link
639 ( scalar(@part_pkg_usage) ?
640 [ { data => 'Usage minutes',
644 link => $p.'browse/part_pkg_usage.html#pkgpart'.
650 [ { data => $_->minutes,
653 { data => $_->description,
663 $align .= 'lrl'; #rr';
667 my $count_extra_sql = $extra_sql;
668 $count_extra_sql =~ s/^\s*AND /WHERE /i;
669 $extra_count = ( $count_extra_sql ? ' AND ' : ' WHERE ' ). $extra_count
671 my $count_query = "SELECT COUNT(*) FROM part_pkg $count_extra_sql $extra_count";
675 if ( $acl_edit_bulk ) {
676 # insert a checkbox column
679 '<INPUT TYPE="checkbox" NAME="pkgpart" VALUE=' . $_[0]->pkgpart .'>';
683 $html_form = qq!<FORM ACTION="${p}edit/bulk-part_pkg.html" METHOD="POST">!;
684 $html_foot = include('/search/elements/checkbox-foot.html',
685 submit => 'edit report classes', # for now it's only report classes
690 # show this if there are any voip_cdr packages defined
691 if ( FS::part_pkg->count("plan = 'voip_cdr'") ) {
692 push @menubar, 'Per-package usage minutes' => $p.'browse/part_pkg_usage.html';