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,
31 <STYLE TYPE="text/css">
34 text-decoration: underline dotted;
37 <SCRIPT TYPE="text/javascript">
38 $().ready(function() {
39 $('.taxproduct_desc').tooltip({});
45 my $curuser = $FS::CurrentUser::CurrentUser;
47 my $edit = 'Edit package definitions';
48 my $edit_global = 'Edit global package definitions';
49 my $acl_edit = $curuser->access_right($edit);
50 my $acl_edit_global = $curuser->access_right($edit_global);
51 my $acl_config = $curuser->access_right('Configuration'); #to edit services
54 my $acl_edit_bulk = $curuser->access_right('Bulk edit package definitions');
57 unless $acl_edit || $acl_edit_global;
59 my $conf = new FS::Conf;
60 my $taxclasses = $conf->exists('enable_taxclasses');
61 my $taxvendor = $conf->config('tax_data_vendor');
62 my $money_char = $conf->config('money_char') || '$';
63 my $disable_counts = $conf->exists('config-disable_counts') ? 1 : 0;
66 my $orderby = 'pkgpart';
71 if ( $cgi->param('active') ) {
72 $orderby = 'num_active DESC';
77 #if ( $cgi->param('activeONLY') ) {
78 # push @where, ' WHERE num_active > 0 '; #XXX doesn't affect count...
81 if ( $cgi->param('recurring') ) {
82 $hash{'freq'} = { op=>'!=', value=>'0' };
83 $extra_count = " freq != '0' ";
87 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
89 push @where, $classnum ? "classnum = $classnum"
92 $cgi->delete('classnum');
94 if ( $cgi->param('pkgpartbatch') =~ /^([\w\/\-\:\. ]+)$/ ) {
95 push @where, "pkgpartbatch = '$1' ";
98 if ( $cgi->param('missing_recur_fee') ) {
99 push @where, "NOT EXISTS ( SELECT 1 FROM part_pkg_option
100 WHERE optionname = 'recur_fee'
101 AND part_pkg_option.pkgpart = part_pkg.pkgpart
102 AND CAST( optionvalue AS NUMERIC ) > 0
106 if ( $cgi->param('ratenum') =~ /^(\d+)$/ ) {
107 push @where, "EXISTS( SELECT 1 FROM part_pkg_option
108 WHERE optionname LIKE '%ratenum'
109 AND optionvalue = '$1'
110 AND part_pkg_option.pkgpart = part_pkg.pkgpart
114 if ( $cgi->param('family') =~ /^(\d+)$/ ) {
115 $family_pkgpart = $1;
116 push @where, "family_pkgpart = $1";
117 # Hiding disabled or one-time charges and limiting by classnum aren't
118 # very useful in this mode, so all links should still refer back to the
119 # non-family-limited display.
120 $cgi->param('showdisabled', 1);
121 $cgi->delete('family');
124 push @where, FS::part_pkg->curuser_pkgs_sql
125 unless $acl_edit_global;
127 my $extra_sql = scalar(@where)
128 ? ( scalar(keys %hash) ? ' AND ' : ' WHERE ' ).
129 join( 'AND ', @where)
132 my $agentnums_sql = $curuser->agentnums_sql( 'table'=>'cust_main' );
133 my $count_cust_pkg = "
134 SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
135 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
138 my $count_cust_pkg_cancel = "
139 SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
140 LEFT JOIN cust_pkg AS cust_pkg_next
141 ON (cust_pkg.pkgnum = cust_pkg_next.change_pkgnum)
142 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
144 AND cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0
147 unless ( $disable_counts ) {
153 AND ( setup IS NULL OR setup = 0 )
154 AND ( cancel IS NULL OR cancel = 0 )
155 AND ( susp IS NULL OR susp = 0 )
156 ) AS num_not_yet_billed,
159 AND setup IS NOT NULL AND setup != 0
160 AND ( cancel IS NULL OR cancel = 0 )
161 AND ( susp IS NULL OR susp = 0 )
165 AND ( cancel IS NULL OR cancel = 0 )
166 AND susp IS NOT NULL AND susp != 0
167 AND setup IS NOT NULL AND setup != 0
171 AND ( cancel IS NULL OR cancel = 0 )
172 AND susp IS NOT NULL AND susp != 0
173 AND ( setup IS NULL OR setup = 0 )
176 ( $count_cust_pkg_cancel
177 AND (cust_pkg_next.pkgnum IS NULL
178 OR cust_pkg_next.pkgpart != cust_pkg.pkgpart)
184 # About the num_cancelled expression: packages that were changed, but
185 # kept the same pkgpart, are considered "moved", not "canceled" (because
186 # this is the part_pkg UI). We could show the count of those but it's
187 # probably not interesting.
190 One or more service definitions are grouped together into a package
191 definition and given pricing information. Customers purchase packages
192 rather than purchase services directly.<BR><BR>
193 <FORM METHOD="GET" ACTION="${p}edit/part_pkg.cgi">
194 <A HREF="${p}edit/part_pkg.cgi"><I>Add a new package definition</I></A>
196 !.include('/elements/select-part_pkg.html', 'element_name' => 'clone' ). qq!
197 <INPUT TYPE="submit" VALUE="Clone existing package">
201 $html_init .= include('.style');
203 $cgi->param('dummy', 1);
206 qq(\n<SCRIPT TYPE="text/javascript">\n).
207 "function filter_change() {".
208 " window.location = '". $cgi->self_url.
209 ";classnum=' + document.getElementById('classnum').options[document.getElementById('classnum').selectedIndex].value".
213 #restore this so pagination works
214 $cgi->param('classnum', $classnum) if length($classnum);
216 #should hide this if there aren't any classes
218 "$filter_change\n<BR>( show class: ".
219 include('/elements/select-pkg_class.html',
220 #'curr_value' => $classnum,
221 'value' => $classnum, #insist on 0 :/
222 'onchange' => 'filter_change()',
223 'pre_options' => [ '-1' => 'all',
225 'disable_empty' => 1,
229 my $recur_toggle = $cgi->param('recurring') ? 'show' : 'hide';
230 $cgi->param('recurring', $cgi->param('recurring') ^ 1 );
233 '( <A HREF="'. $cgi->self_url.'">'. "$recur_toggle one-time charges</A> )";
235 $cgi->param('recurring', $cgi->param('recurring') ^ 1 ); #put it back
239 my $link = [ $p.'edit/part_pkg.cgi?', 'pkgpart' ];
241 my @header = ( '#', 'Package', 'Comment', 'Custom' );
242 my @fields = ( 'pkgpart', 'pkg', 'comment',
243 sub{ '<B><FONT COLOR="#0000CC">'.$_[0]->custom.'</FONT></B>' }
246 my @links = ( $link, $link, '', '' );
248 unless ( 0 ) { #already showing only one class or something?
249 push @header, 'Class';
250 push @fields, sub { shift->classname || '(none)'; };
254 if ( $conf->exists('pkg-addon_classnum') ) {
255 push @header, "Add'l order class";
256 push @fields, sub { shift->addon_classname || '(none)'; };
260 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
262 tie my %plan_labels, 'Tie::IxHash',
263 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
266 push @header, 'Pricing';
269 my $part_pkg = shift;
270 (my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ / /g;
271 my $is_recur = ( $part_pkg->freq ne '0' );
272 my @discounts = sort { $a->months <=> $b->months }
274 $part_pkg->part_pkg_discount;
277 # Line 0: Family package link (if applicable)
278 ( !$family_pkgpart &&
279 $part_pkg->pkgpart == $part_pkg->family_pkgpart ? () : [
284 'data' => '<b>Show all versions</b>',
285 'link' => $p.'browse/part_pkg.cgi?family='.$part_pkg->family_pkgpart,
288 [ # Line 1: Plan type (Anniversary, Prorate, Call Rating, etc.)
294 [ # Line 2: Setup fee
295 { data =>$money_char.
296 sprintf('%.2f ', $part_pkg->option('setup_fee') ),
299 { data => ( ( $is_recur ? ' setup' : ' one-time' ).
300 ( $part_pkg->option('recur_fee') == 0
301 && $part_pkg->setup_show_zero
302 ? ' (printed on invoices)'
309 [ # Line 3: Recurring fee
312 ? $money_char. sprintf('%.2f', $part_pkg->option('recur_fee'))
313 : $part_pkg->freq_pretty
315 align=> ( $is_recur ? 'right' : 'center' ),
316 colspan=> ( $is_recur ? 1 : 2 ),
319 ? { data => ' '. $part_pkg->freq_pretty.
320 ( $part_pkg->option('recur_fee') == 0
321 && $part_pkg->recur_show_zero
322 ? ' (printed on invoices)'
330 [ { data => ' ' }, ], # Line 4: empty
331 ( $part_pkg->adjourn_months ?
332 [ # Line 5: Adjourn months
333 { data => mt('After [quant,_1,month], <strong>suspend</strong> the package.',
334 $part_pkg->adjourn_months),
341 ( $part_pkg->contract_end_months ?
342 [ # Line 6: Contract end months
343 { data => mt('After [quant,_1,month], <strong>contract ends</strong>.',
344 $part_pkg->contract_end_months),
351 ( $part_pkg->expire_months ?
352 [ # Line 7: Expire months and automatic transfer
353 { data => $part_pkg->change_to_pkgpart ?
354 mt('After [quant,_1,month], <strong>change to</strong> ',
355 $part_pkg->expire_months) .
356 qq(<a href="${p}edit/part_pkg.cgi?) .
357 $part_pkg->change_to_pkgpart .
358 qq(">) . $part_pkg->change_to_pkg->pkg . qq(</a>) . '.'
359 : mt('After [quant,_1,month], <strong>cancel</strong> the package.',
360 $part_pkg->expire_months)
369 map { my $amount = $_->amount / ($_->target_info->{multiplier} || 1);
370 my $label = $_->target_info->{label};
372 { data => "Plus $money_char". $_->price. ' '.
373 ( $_->action eq 'increment' ? 'per' : 'for' ).
374 " $amount $label",
375 align => 'center', #left?
380 $part_pkg->part_pkg_usageprice
383 map { my $dst_pkg = $_->dst_pkg;
385 { data => 'Supplemental: '.
386 '<A HREF="#'. $dst_pkg->pkgpart . '">' .
387 $dst_pkg->pkg . '</A>',
393 $part_pkg->supp_part_pkg_link
395 ( # Billing add-ons/bundle packages
397 my $dst_pkg = $_->dst_pkg;
399 { data => 'Add-on: '.$dst_pkg->pkg_comment,
405 $part_pkg->bill_part_pkg_link
407 ( # Discounts available
410 { data => '<b>Discounts</b>',
420 { data => $_->months. ':',
423 { data => $_->amount ? '$'. $_->amount : $_->percent. '%'
430 ]; # end of "middle column"
432 # $plan_labels{$part_pkg->plan}.'<BR>'.
433 # $money_char.sprintf('%.2f setup<BR>', $part_pkg->option('setup_fee') ).
434 # ( $part_pkg->freq ne '0'
435 # ? $money_char.sprintf('%.2f ', $part_pkg->option('recur_fee') )
438 # $part_pkg->freq_pretty; #.'<BR>'
441 push @header, 'Cost tracking';
444 my $part_pkg = shift;
445 #(my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ / /g;
446 my $is_recur = ( $part_pkg->freq ne '0' );
450 { data => ' ', # $plan,
456 { data =>$money_char.
457 sprintf('%.2f ', $part_pkg->setup_cost ),
460 { data => ( $is_recur ? ' setup' : ' one-time' ),
467 ? $money_char. sprintf('%.2f', $part_pkg->recur_cost)
468 : '(no recurring)' #$part_pkg->freq_pretty
470 align=> ( $is_recur ? 'right' : 'center' ),
471 colspan=> ( $is_recur ? 1 : 2 ),
474 ? { data => ( $is_recur
475 ? ' '. $part_pkg->freq_pretty
487 # Agent goes here if displayed
491 if ( $acl_edit_global ) {
492 #really we just want a count, but this is fine unless someone has tons
493 my @all_agent_types = map {$_->typenum}
494 qsearch('agent_type', { 'disabled'=>'' });
495 if ( scalar(@all_agent_types) > 1 ) {
496 push @header, 'Agent types';
497 my $typelink = $p. 'edit/agent_type.cgi?';
498 push @fields, sub { my $part_pkg = shift;
500 map { my $agent_type = $_->agent_type;
502 { 'data' => $agent_type->atype, #escape?
504 'link' => ( $acl_config
519 #if ( $cgi->param('active') ) {
520 push @header, 'Customer<BR>packages';
521 my %col = %{ FS::cust_pkg->statuscolors };
522 my $cust_pkg_link = $p. 'search/cust_pkg.cgi?pkgpart=';
523 push @fields, sub { my $part_pkg = shift;
528 if ( $magic eq 'active' && $part_pkg->freq == 0 ) {
530 #$label = 'one-time charge';
533 $label= 'not yet billed' if $magic eq 'not_yet_billed';
534 $label= 'on hold' if $magic eq 'on_hold';
538 'data' => '<B><FONT COLOR="#'. $col{$label}. '">'.
539 $part_pkg->get("num_$_").
545 ( $part_pkg->get("num_$_") != 1
546 && $label =~ /charge$/
551 'link' => ( $part_pkg->get("num_$_") || $disable_counts
559 } (qw( on_hold not_yet_billed active suspended cancelled ))
563 { 'data' => '<FONT SIZE="-1">[ '.
564 include('/elements/popup_link.html',
566 'action' => "${p}edit/bulk-cust_pkg.html?".
567 'pkgpart='.$part_pkg->pkgpart,
568 'actionlabel' => 'Change Packages',
581 push @header, 'Taxclass';
582 push @fields, sub { shift->taxclass() || ' '; };
584 } elsif ( $taxvendor ) {
585 push @header, 'Tax product';
586 my @classnums = ( 'setup', 'recur' );
587 my @classnames = ( 'Setup', 'Recur' );
588 foreach ( qsearch('usage_class', { disabled => '' }) ) {
589 push @classnums, $_->classnum;
590 push @classnames, $_->classname;
592 my $taxproduct_sub = sub {
594 '<SPAN CLASS="taxproduct_desc" TITLE="' .
595 encode_entities($ppt->description) .
596 '">' . encode_entities($ppt->taxproduct) . '</SPAN>'
598 my $taxproduct_list_sub = sub {
599 my $part_pkg = shift;
600 my $base_ppt = $part_pkg->taxproduct;
604 { 'data' => '', 'align' => 'left' },
605 { 'data' => &$taxproduct_sub($base_ppt), 'align' => 'right' },
608 for (my $i = 0; $i < scalar @classnums; $i++) {
609 my $num = $part_pkg->option('usage_taxproductnum_' . $classnums[$i]);
611 my $ppt = FS::part_pkg_taxproduct->by_key($num);
613 { 'data' => $classnames[$i] . ': ', 'align' => 'left', },
614 { 'data' => &$taxproduct_sub($ppt), 'align' => 'right' },
619 push @fields, $taxproduct_list_sub;
623 # make a table of report class optionnames => the actual
624 my %report_optionname_name = map { 'report_option_'.$_->num, $_->name }
625 qsearch('part_pkg_report_option', { disabled => '' });
627 push @header, 'Plan options',
629 #'Service', 'Quan', 'Primary';
633 my $part_pkg = shift;
634 if ( $part_pkg->plan ) {
636 my %options = $part_pkg->options;
637 # gather any options that are really report options,
638 # convert them to their user-friendly names,
639 # and sort them (I think?)
642 map { $report_optionname_name{$_} }
644 and exists($report_optionname_name{$_}) }
653 { 'data' => $part_pkg->format($_,$options{$_}),
659 grep { $options{$_} =~ /\S/ }
660 grep { $_ !~ /^(setup|recur)_fee$/
661 and $_ !~ /^report_option_\d+$/
662 and $_ !~ /^usage_taxproductnum_/
666 if ( @report_options ) {
668 [ { 'data' => 'Report classes',
670 'style' => 'font-weight: bold',
673 foreach (@report_options) {
680 } # foreach @report_options
681 } # if @report_options
685 } else { # should never happen...
692 'data' => $part_pkg->$_(),
705 my $part_pkg = shift;
706 my @part_pkg_usage = sort { $a->priority <=> $b->priority }
707 $part_pkg->part_pkg_usage;
712 my $part_svc = $pkg_svc->part_svc;
713 my $svc = $part_svc->svc;
714 if ( $pkg_svc->primary_svc =~ /^Y/i ) {
715 $svc = "<B>$svc (PRIMARY)</B>";
717 $svc =~ s/ +/ /g;
721 'data' => '<B>'. $pkg_svc->quantity. '</B>',
727 'link' => ( $acl_config
728 ? $p. 'edit/part_svc.cgi?'.
735 sort { $b->primary_svc =~ /^Y/i
736 <=> $a->primary_svc =~ /^Y/i
738 $part_pkg->pkg_svc('disable_linked'=>1)
741 my $dst_pkg = $_->dst_pkg;
743 { data => 'Add-on: '.$dst_pkg->pkg_comment,
749 $part_pkg->svc_part_pkg_link
751 ( scalar(@part_pkg_usage) ?
752 [ { data => 'Usage minutes',
756 link => $p.'browse/part_pkg_usage.html#pkgpart'.
762 [ { data => $_->minutes,
765 { data => $_->description,
775 $align .= 'lrl'; #rr';
779 my $count_extra_sql = $extra_sql;
780 $count_extra_sql =~ s/^\s*AND /WHERE /i;
781 $extra_count = ( $count_extra_sql ? ' AND ' : ' WHERE ' ). $extra_count
783 my $count_query = "SELECT COUNT(*) FROM part_pkg $count_extra_sql $extra_count";
787 if ( $acl_edit_bulk ) {
788 # insert a checkbox column
791 '<INPUT TYPE="checkbox" NAME="pkgpart" VALUE=' . $_[0]->pkgpart .'>';
795 $html_form = qq!<FORM ACTION="${p}edit/bulk-part_pkg.html" METHOD="POST">!;
796 $html_foot = include('/search/elements/checkbox-foot.html',
798 { submit => 'edit report classes', },
799 { label => 'change customer packages',
800 onclick=> include('/elements/popup_link_onclick.html',
803 '${p}edit/bulk-cust_pkg.html?' + \$('input[name=pkgpart]').serialize()
805 'actionlabel' => 'Change customer packages',
816 # show this if there are any voip_cdr packages defined
817 if ( FS::part_pkg->count("plan = 'voip_cdr'") ) {
818 push @menubar, 'Per-package usage minutes' => $p.'browse/part_pkg_usage.html';