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 <SCRIPT TYPE="text/javascript">
33 $(document).ready(function(){
41 my $curuser = $FS::CurrentUser::CurrentUser;
43 my $edit = 'Edit package definitions';
44 my $edit_global = 'Edit global package definitions';
45 my $acl_edit = $curuser->access_right($edit);
46 my $acl_edit_global = $curuser->access_right($edit_global);
47 my $acl_config = $curuser->access_right('Configuration'); #to edit services
50 my $acl_edit_bulk = $curuser->access_right('Bulk edit package definitions');
53 unless $acl_edit || $acl_edit_global;
55 my $conf = new FS::Conf;
56 my $taxclasses = $conf->exists('enable_taxclasses');
57 my $money_char = $conf->config('money_char') || '$';
60 my $orderby = 'pkgpart';
65 if ( $cgi->param('active') ) {
66 $orderby = 'num_active DESC';
71 #if ( $cgi->param('activeONLY') ) {
72 # push @where, ' WHERE num_active > 0 '; #XXX doesn't affect count...
75 if ( $cgi->param('recurring') ) {
76 $hash{'freq'} = { op=>'!=', value=>'0' };
77 $extra_count = " freq != '0' ";
81 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
83 push @where, $classnum ? "classnum = $classnum"
86 $cgi->delete('classnum');
88 if ( $cgi->param('pkgpartbatch') =~ /^([\w\/\-\:\. ]+)$/ ) {
89 push @where, "pkgpartbatch = '$1' ";
92 if ( $cgi->param('missing_recur_fee') ) {
93 push @where, "NOT EXISTS ( SELECT 1 FROM part_pkg_option
94 WHERE optionname = 'recur_fee'
95 AND part_pkg_option.pkgpart = part_pkg.pkgpart
96 AND CAST( optionvalue AS NUMERIC ) > 0
100 if ( $cgi->param('ratenum') =~ /^(\d+)$/ ) {
101 push @where, "EXISTS( SELECT 1 FROM part_pkg_option
102 WHERE optionname LIKE '%ratenum'
103 AND optionvalue = '$1'
104 AND part_pkg_option.pkgpart = part_pkg.pkgpart
108 if ( $cgi->param('family') =~ /^(\d+)$/ ) {
109 $family_pkgpart = $1;
110 push @where, "family_pkgpart = $1";
111 # Hiding disabled or one-time charges and limiting by classnum aren't
112 # very useful in this mode, so all links should still refer back to the
113 # non-family-limited display.
114 $cgi->param('showdisabled', 1);
115 $cgi->delete('family');
118 push @where, FS::part_pkg->curuser_pkgs_sql
119 unless $acl_edit_global;
121 my $extra_sql = scalar(@where)
122 ? ( scalar(keys %hash) ? ' AND ' : ' WHERE ' ).
123 join( 'AND ', @where)
126 my $agentnums_sql = $curuser->agentnums_sql( 'table'=>'cust_main' );
127 my $count_cust_pkg = "
128 SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
129 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
132 my $count_cust_pkg_cancel = "
133 SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
134 LEFT JOIN cust_pkg AS cust_pkg_next
135 ON (cust_pkg.pkgnum = cust_pkg_next.change_pkgnum)
136 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
138 AND cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0
146 AND ( setup IS NULL OR setup = 0 )
147 AND ( cancel IS NULL OR cancel = 0 )
148 AND ( susp IS NULL OR susp = 0 )
149 ) AS num_not_yet_billed,
152 AND setup IS NOT NULL AND setup != 0
153 AND ( cancel IS NULL OR cancel = 0 )
154 AND ( susp IS NULL OR susp = 0 )
158 AND ( cancel IS NULL OR cancel = 0 )
159 AND susp IS NOT NULL AND susp != 0
160 AND setup IS NOT NULL AND setup != 0
164 AND ( cancel IS NULL OR cancel = 0 )
165 AND susp IS NOT NULL AND susp != 0
166 AND ( setup IS NULL OR setup = 0 )
169 ( $count_cust_pkg_cancel
170 AND (cust_pkg_next.pkgnum IS NULL
171 OR cust_pkg_next.pkgpart != cust_pkg.pkgpart)
175 # About the num_cancelled expression: packages that were changed, but
176 # kept the same pkgpart, are considered "moved", not "canceled" (because
177 # this is the part_pkg UI). We could show the count of those but it's
178 # probably not interesting.
181 One or more service definitions are grouped together into a package
182 definition and given pricing information. Customers purchase packages
183 rather than purchase services directly.<BR><BR>
184 <FORM METHOD="GET" ACTION="${p}edit/part_pkg.cgi">
185 <A HREF="${p}edit/part_pkg.cgi"><I>Add a new package definition</I></A>
187 !.include('/elements/select-part_pkg.html', 'element_name' => 'clone' ). qq!
188 <INPUT TYPE="submit" VALUE="Clone existing package">
193 $cgi->param('dummy', 1);
196 qq(\n<SCRIPT TYPE="text/javascript">\n).
197 "function filter_change() {".
198 " window.location = '". $cgi->self_url.
199 ";classnum=' + document.getElementById('classnum').options[document.getElementById('classnum').selectedIndex].value".
203 #restore this so pagination works
204 $cgi->param('classnum', $classnum) if length($classnum);
206 #should hide this if there aren't any classes
208 "$filter_change\n<BR>( show class: ".
209 include('/elements/select-pkg_class.html',
210 #'curr_value' => $classnum,
211 'value' => $classnum, #insist on 0 :/
212 'onchange' => 'filter_change()',
213 'pre_options' => [ '-1' => 'all',
215 'disable_empty' => 1,
219 my $recur_toggle = $cgi->param('recurring') ? 'show' : 'hide';
220 $cgi->param('recurring', $cgi->param('recurring') ^ 1 );
223 '( <A HREF="'. $cgi->self_url.'">'. "$recur_toggle one-time charges</A> )";
225 $cgi->param('recurring', $cgi->param('recurring') ^ 1 ); #put it back
229 my $link = [ $p.'edit/part_pkg.cgi?', 'pkgpart' ];
231 my @header = ( '#', 'Package', 'Comment', 'Custom' );
232 my @fields = ( 'pkgpart', 'pkg', 'comment',
233 sub{ '<B><FONT COLOR="#0000CC">'.$_[0]->custom.'</FONT></B>' }
236 my @links = ( $link, $link, '', '' );
238 unless ( 0 ) { #already showing only one class or something?
239 push @header, 'Class';
240 push @fields, sub { shift->classname || '(none)'; };
244 if ( $conf->exists('pkg-addon_classnum') ) {
245 push @header, "Add'l order class";
246 push @fields, sub { shift->addon_classname || '(none)'; };
250 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
252 tie my %plan_labels, 'Tie::IxHash',
253 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
256 push @header, 'Pricing';
259 my $part_pkg = shift;
260 (my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ / /g;
261 my $is_recur = ( $part_pkg->freq ne '0' );
262 my @discounts = sort { $a->months <=> $b->months }
264 $part_pkg->part_pkg_discount;
267 ( !$family_pkgpart &&
268 $part_pkg->pkgpart == $part_pkg->family_pkgpart ? () : [
273 'data' => '<b>Show all versions</b>',
274 'link' => $p.'browse/part_pkg.cgi?family='.$part_pkg->family_pkgpart,
284 { data =>$money_char.
285 sprintf('%.2f ', $part_pkg->option('setup_fee') ),
288 { data => ( ( $is_recur ? ' setup' : ' one-time' ).
289 ( $part_pkg->option('recur_fee') == 0
290 && $part_pkg->setup_show_zero
291 ? ' (printed on invoices)'
301 ? $money_char. sprintf('%.2f', $part_pkg->option('recur_fee'))
302 : $part_pkg->freq_pretty
304 align=> ( $is_recur ? 'right' : 'center' ),
305 colspan=> ( $is_recur ? 1 : 2 ),
308 ? { data => ( $is_recur
309 ? ' '. $part_pkg->freq_pretty.
310 ( $part_pkg->option('recur_fee') == 0
311 && $part_pkg->recur_show_zero
312 ? ' (printed on invoices)'
321 ( map { my $dst_pkg = $_->dst_pkg;
323 { data => 'Supplemental: '.
324 '<A HREF="#'. $dst_pkg->pkgpart . '">' .
325 $dst_pkg->pkg . '</A>',
331 $part_pkg->supp_part_pkg_link
334 my $dst_pkg = $_->dst_pkg;
336 { data => 'Add-on: '.$dst_pkg->pkg_comment,
342 $part_pkg->bill_part_pkg_link
346 { data => '<b>Discounts</b>',
356 { data => $_->months. ':',
359 { data => $_->amount ? '$'. $_->amount : $_->percent. '%'
368 # $plan_labels{$part_pkg->plan}.'<BR>'.
369 # $money_char.sprintf('%.2f setup<BR>', $part_pkg->option('setup_fee') ).
370 # ( $part_pkg->freq ne '0'
371 # ? $money_char.sprintf('%.2f ', $part_pkg->option('recur_fee') )
374 # $part_pkg->freq_pretty; #.'<BR>'
377 push @header, 'Cost tracking';
380 my $part_pkg = shift;
381 #(my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ / /g;
382 my $is_recur = ( $part_pkg->freq ne '0' );
386 { data => ' ', # $plan,
392 { data =>$money_char.
393 sprintf('%.2f ', $part_pkg->setup_cost ),
396 { data => ( $is_recur ? ' setup' : ' one-time' ),
403 ? $money_char. sprintf('%.2f', $part_pkg->recur_cost)
404 : '(no recurring)' #$part_pkg->freq_pretty
406 align=> ( $is_recur ? 'right' : 'center' ),
407 colspan=> ( $is_recur ? 1 : 2 ),
410 ? { data => ( $is_recur
411 ? ' '. $part_pkg->freq_pretty
423 # Agent goes here if displayed
427 if ( $acl_edit_global ) {
428 #really we just want a count, but this is fine unless someone has tons
429 my @all_agent_types = map {$_->typenum}
430 qsearch('agent_type', { 'disabled'=>'' });
431 if ( scalar(@all_agent_types) > 1 ) {
432 push @header, 'Agent types';
433 my $typelink = $p. 'edit/agent_type.cgi?';
434 push @fields, sub { my $part_pkg = shift;
436 map { my $agent_type = $_->agent_type;
438 { 'data' => $agent_type->atype, #escape?
440 'link' => ( $acl_config
455 #if ( $cgi->param('active') ) {
456 push @header, 'Customer<BR>packages';
457 my %col = %{ FS::cust_pkg->statuscolors };
458 my $cust_pkg_link = $p. 'search/cust_pkg.cgi?pkgpart=';
459 push @fields, sub { my $part_pkg = shift;
464 if ( $magic eq 'active' && $part_pkg->freq == 0 ) {
466 #$label = 'one-time charge';
469 $label= 'not yet billed' if $magic eq 'not_yet_billed';
470 $label= 'on hold' if $magic eq 'on_hold';
474 'data' => '<B><FONT COLOR="#'. $col{$label}. '">'.
475 $part_pkg->get("num_$_").
481 ( $part_pkg->get("num_$_") != 1
482 && $label =~ /charge$/
487 'link' => ( $part_pkg->get("num_$_")
495 } (qw( on_hold not_yet_billed active suspended cancelled ))
499 { 'data' => '<FONT SIZE="-1">[ '.
500 include('/elements/popup_link.html',
502 'action' => "${p}edit/bulk-cust_pkg.html?".
503 'pkgpart='.$part_pkg->pkgpart,
504 'actionlabel' => 'Change Packages',
517 push @header, 'Taxclass';
518 push @fields, sub { shift->taxclass() || ' '; };
522 # make a table of report class optionnames => the actual
523 my %report_optionname_name = map { 'report_option_'.$_->num, $_->name }
524 qsearch('part_pkg_report_option', { disabled => '' });
526 push @header, 'Plan options',
528 #'Service', 'Quan', 'Primary';
532 my $part_pkg = shift;
533 if ( $part_pkg->plan ) {
535 my %options = $part_pkg->options;
536 # gather any options that are really report options,
537 # convert them to their user-friendly names,
538 # and sort them (I think?)
541 map { $report_optionname_name{$_} }
543 and exists($report_optionname_name{$_}) }
552 { 'data' => $part_pkg->format($_,$options{$_}),
558 grep { $options{$_} =~ /\S/ }
559 grep { $_ !~ /^(setup|recur)_fee$/
560 and $_ !~ /^report_option_\d+$/ }
563 if ( @report_options ) {
565 [ { 'data' => 'Report classes',
567 'style' => 'font-weight: bold',
570 foreach (@report_options) {
577 } # foreach @report_options
578 } # if @report_options
582 } else { # should never happen...
589 'data' => $part_pkg->$_(),
602 my $part_pkg = shift;
603 my @part_pkg_usage = sort { $a->priority <=> $b->priority }
604 $part_pkg->part_pkg_usage;
609 my $part_svc = $pkg_svc->part_svc;
610 my $svc = $part_svc->svc;
611 if ( $pkg_svc->primary_svc =~ /^Y/i ) {
612 $svc = "<B>$svc (PRIMARY)</B>";
614 $svc =~ s/ +/ /g;
618 'data' => '<B>'. $pkg_svc->quantity. '</B>',
624 'link' => ( $acl_config
625 ? $p. 'edit/part_svc.cgi?'.
632 sort { $b->primary_svc =~ /^Y/i
633 <=> $a->primary_svc =~ /^Y/i
635 $part_pkg->pkg_svc('disable_linked'=>1)
638 my $dst_pkg = $_->dst_pkg;
640 { data => 'Add-on: '.$dst_pkg->pkg_comment,
646 $part_pkg->svc_part_pkg_link
648 ( scalar(@part_pkg_usage) ?
649 [ { data => 'Usage minutes',
653 link => $p.'browse/part_pkg_usage.html#pkgpart'.
659 [ { data => $_->minutes,
662 { data => $_->description,
672 $align .= 'lrl'; #rr';
676 my $count_extra_sql = $extra_sql;
677 $count_extra_sql =~ s/^\s*AND /WHERE /i;
678 $extra_count = ( $count_extra_sql ? ' AND ' : ' WHERE ' ). $extra_count
680 my $count_query = "SELECT COUNT(*) FROM part_pkg $count_extra_sql $extra_count";
684 if ( $acl_edit_bulk ) {
685 # insert a checkbox column
688 '<INPUT TYPE="checkbox" NAME="pkgpart" VALUE=' . $_[0]->pkgpart .'>';
692 $html_form = qq!<FORM ACTION="${p}edit/bulk-part_pkg.html" METHOD="POST">!;
693 $html_foot = include('/search/elements/checkbox-foot.html',
695 { label => 'edit packages',
696 onclick=> include('/elements/popup_link_onclick.html',
699 '${p}edit/bulk-part_pkg.html?' + \$('input[name=pkgpart]').serialize()
701 'actionlabel' => 'Bulk edit packages',
706 { label => 'change customers packages',
707 onclick=> include('/elements/popup_link_onclick.html',
710 '${p}edit/bulk-cust_pkg.html?' + \$('input[name=pkgpart]').serialize()
712 'actionlabel' => 'Change customer packages',
723 # show this if there are any voip_cdr packages defined
724 if ( FS::part_pkg->count("plan = 'voip_cdr'") ) {
725 push @menubar, 'Per-package usage minutes' => $p.'browse/part_pkg_usage.html';