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') || '$';
65 my $orderby = 'pkgpart';
70 if ( $cgi->param('active') ) {
71 $orderby = 'num_active DESC';
76 #if ( $cgi->param('activeONLY') ) {
77 # push @where, ' WHERE num_active > 0 '; #XXX doesn't affect count...
80 if ( $cgi->param('recurring') ) {
81 $hash{'freq'} = { op=>'!=', value=>'0' };
82 $extra_count = " freq != '0' ";
86 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
88 push @where, $classnum ? "classnum = $classnum"
91 $cgi->delete('classnum');
93 if ( $cgi->param('pkgpartbatch') =~ /^([\w\/\-\:\. ]+)$/ ) {
94 push @where, "pkgpartbatch = '$1' ";
97 if ( $cgi->param('missing_recur_fee') ) {
98 push @where, "NOT EXISTS ( SELECT 1 FROM part_pkg_option
99 WHERE optionname = 'recur_fee'
100 AND part_pkg_option.pkgpart = part_pkg.pkgpart
101 AND CAST( optionvalue AS NUMERIC ) > 0
105 if ( $cgi->param('ratenum') =~ /^(\d+)$/ ) {
106 push @where, "EXISTS( SELECT 1 FROM part_pkg_option
107 WHERE optionname LIKE '%ratenum'
108 AND optionvalue = '$1'
109 AND part_pkg_option.pkgpart = part_pkg.pkgpart
113 if ( $cgi->param('family') =~ /^(\d+)$/ ) {
114 $family_pkgpart = $1;
115 push @where, "family_pkgpart = $1";
116 # Hiding disabled or one-time charges and limiting by classnum aren't
117 # very useful in this mode, so all links should still refer back to the
118 # non-family-limited display.
119 $cgi->param('showdisabled', 1);
120 $cgi->delete('family');
123 push @where, FS::part_pkg->curuser_pkgs_sql
124 unless $acl_edit_global;
126 my $extra_sql = scalar(@where)
127 ? ( scalar(keys %hash) ? ' AND ' : ' WHERE ' ).
128 join( 'AND ', @where)
131 my $agentnums_sql = $curuser->agentnums_sql( 'table'=>'cust_main' );
132 my $count_cust_pkg = "
133 SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
134 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
137 my $count_cust_pkg_cancel = "
138 SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
139 LEFT JOIN cust_pkg AS cust_pkg_next
140 ON (cust_pkg.pkgnum = cust_pkg_next.change_pkgnum)
141 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
143 AND cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0
151 AND ( setup IS NULL OR setup = 0 )
152 AND ( cancel IS NULL OR cancel = 0 )
153 AND ( susp IS NULL OR susp = 0 )
154 ) AS num_not_yet_billed,
157 AND setup IS NOT NULL AND setup != 0
158 AND ( cancel IS NULL OR cancel = 0 )
159 AND ( susp IS NULL OR susp = 0 )
163 AND ( cancel IS NULL OR cancel = 0 )
164 AND susp IS NOT NULL AND susp != 0
165 AND setup IS NOT NULL AND setup != 0
169 AND ( cancel IS NULL OR cancel = 0 )
170 AND susp IS NOT NULL AND susp != 0
171 AND ( setup IS NULL OR setup = 0 )
174 ( $count_cust_pkg_cancel
175 AND (cust_pkg_next.pkgnum IS NULL
176 OR cust_pkg_next.pkgpart != cust_pkg.pkgpart)
180 # About the num_cancelled expression: packages that were changed, but
181 # kept the same pkgpart, are considered "moved", not "canceled" (because
182 # this is the part_pkg UI). We could show the count of those but it's
183 # probably not interesting.
186 One or more service definitions are grouped together into a package
187 definition and given pricing information. Customers purchase packages
188 rather than purchase services directly.<BR><BR>
189 <FORM METHOD="GET" ACTION="${p}edit/part_pkg.cgi">
190 <A HREF="${p}edit/part_pkg.cgi"><I>Add a new package definition</I></A>
192 !.include('/elements/select-part_pkg.html', 'element_name' => 'clone' ). qq!
193 <INPUT TYPE="submit" VALUE="Clone existing package">
197 $html_init .= include('.style');
199 $cgi->param('dummy', 1);
202 qq(\n<SCRIPT TYPE="text/javascript">\n).
203 "function filter_change() {".
204 " window.location = '". $cgi->self_url.
205 ";classnum=' + document.getElementById('classnum').options[document.getElementById('classnum').selectedIndex].value".
209 #restore this so pagination works
210 $cgi->param('classnum', $classnum) if length($classnum);
212 #should hide this if there aren't any classes
214 "$filter_change\n<BR>( show class: ".
215 include('/elements/select-pkg_class.html',
216 #'curr_value' => $classnum,
217 'value' => $classnum, #insist on 0 :/
218 'onchange' => 'filter_change()',
219 'pre_options' => [ '-1' => 'all',
221 'disable_empty' => 1,
225 my $recur_toggle = $cgi->param('recurring') ? 'show' : 'hide';
226 $cgi->param('recurring', $cgi->param('recurring') ^ 1 );
229 '( <A HREF="'. $cgi->self_url.'">'. "$recur_toggle one-time charges</A> )";
231 $cgi->param('recurring', $cgi->param('recurring') ^ 1 ); #put it back
235 my $link = [ $p.'edit/part_pkg.cgi?', 'pkgpart' ];
237 my @header = ( '#', 'Package', 'Comment', 'Custom' );
238 my @fields = ( 'pkgpart', 'pkg', 'comment',
239 sub{ '<B><FONT COLOR="#0000CC">'.$_[0]->custom.'</FONT></B>' }
242 my @links = ( $link, $link, '', '' );
244 unless ( 0 ) { #already showing only one class or something?
245 push @header, 'Class';
246 push @fields, sub { shift->classname || '(none)'; };
250 if ( $conf->exists('pkg-addon_classnum') ) {
251 push @header, "Add'l order class";
252 push @fields, sub { shift->addon_classname || '(none)'; };
256 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
258 tie my %plan_labels, 'Tie::IxHash',
259 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
262 push @header, 'Pricing';
265 my $part_pkg = shift;
266 (my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ / /g;
267 my $is_recur = ( $part_pkg->freq ne '0' );
268 my @discounts = sort { $a->months <=> $b->months }
270 $part_pkg->part_pkg_discount;
273 # Line 0: Family package link (if applicable)
274 ( !$family_pkgpart &&
275 $part_pkg->pkgpart == $part_pkg->family_pkgpart ? () : [
280 'data' => '<b>Show all versions</b>',
281 'link' => $p.'browse/part_pkg.cgi?family='.$part_pkg->family_pkgpart,
284 [ # Line 1: Plan type (Anniversary, Prorate, Call Rating, etc.)
290 [ # Line 2: Setup fee
291 { data =>$money_char.
292 sprintf('%.2f ', $part_pkg->option('setup_fee') ),
295 { data => ( ( $is_recur ? ' setup' : ' one-time' ).
296 ( $part_pkg->option('recur_fee') == 0
297 && $part_pkg->setup_show_zero
298 ? ' (printed on invoices)'
305 [ # Line 3: Recurring fee
308 ? $money_char. sprintf('%.2f', $part_pkg->option('recur_fee'))
309 : $part_pkg->freq_pretty
311 align=> ( $is_recur ? 'right' : 'center' ),
312 colspan=> ( $is_recur ? 1 : 2 ),
315 ? { data => ' '. $part_pkg->freq_pretty.
316 ( $part_pkg->option('recur_fee') == 0
317 && $part_pkg->recur_show_zero
318 ? ' (printed on invoices)'
326 [ { data => ' ' }, ], # Line 4: empty
327 ( $part_pkg->adjourn_months ?
328 [ # Line 5: Adjourn months
329 { data => mt('After [quant,_1,month], <strong>suspend</strong> the package.',
330 $part_pkg->adjourn_months),
337 ( $part_pkg->contract_end_months ?
338 [ # Line 6: Contract end months
339 { data => mt('After [quant,_1,month], <strong>contract ends</strong>.',
340 $part_pkg->contract_end_months),
347 ( $part_pkg->expire_months ?
348 [ # Line 7: Expire months and automatic transfer
349 { data => $part_pkg->change_to_pkgpart ?
350 mt('After [quant,_1,month], <strong>change to</strong> ',
351 $part_pkg->expire_months) .
352 qq(<a href="${p}edit/part_pkg.cgi?) .
353 $part_pkg->change_to_pkgpart .
354 qq(">) . $part_pkg->change_to_pkg->pkg . qq(</a>) . '.'
355 : mt('After [quant,_1,month], <strong>cancel</strong> the package.',
356 $part_pkg->expire_months)
365 map { my $amount = $_->amount / ($_->target_info->{multiplier} || 1);
366 my $label = $_->target_info->{label};
368 { data => "Plus $money_char". $_->price. ' '.
369 ( $_->action eq 'increment' ? 'per' : 'for' ).
370 " $amount $label",
371 align => 'center', #left?
376 $part_pkg->part_pkg_usageprice
379 map { my $dst_pkg = $_->dst_pkg;
381 { data => 'Supplemental: '.
382 '<A HREF="#'. $dst_pkg->pkgpart . '">' .
383 $dst_pkg->pkg . '</A>',
389 $part_pkg->supp_part_pkg_link
391 ( # Billing add-ons/bundle packages
393 my $dst_pkg = $_->dst_pkg;
395 { data => 'Add-on: '.$dst_pkg->pkg_comment,
401 $part_pkg->bill_part_pkg_link
403 ( # Discounts available
406 { data => '<b>Discounts</b>',
416 { data => $_->months. ':',
419 { data => $_->amount ? '$'. $_->amount : $_->percent. '%'
426 ]; # end of "middle column"
428 # $plan_labels{$part_pkg->plan}.'<BR>'.
429 # $money_char.sprintf('%.2f setup<BR>', $part_pkg->option('setup_fee') ).
430 # ( $part_pkg->freq ne '0'
431 # ? $money_char.sprintf('%.2f ', $part_pkg->option('recur_fee') )
434 # $part_pkg->freq_pretty; #.'<BR>'
437 push @header, 'Cost tracking';
440 my $part_pkg = shift;
441 #(my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ / /g;
442 my $is_recur = ( $part_pkg->freq ne '0' );
446 { data => ' ', # $plan,
452 { data =>$money_char.
453 sprintf('%.2f ', $part_pkg->setup_cost ),
456 { data => ( $is_recur ? ' setup' : ' one-time' ),
463 ? $money_char. sprintf('%.2f', $part_pkg->recur_cost)
464 : '(no recurring)' #$part_pkg->freq_pretty
466 align=> ( $is_recur ? 'right' : 'center' ),
467 colspan=> ( $is_recur ? 1 : 2 ),
470 ? { data => ( $is_recur
471 ? ' '. $part_pkg->freq_pretty
483 # Agent goes here if displayed
487 if ( $acl_edit_global ) {
488 #really we just want a count, but this is fine unless someone has tons
489 my @all_agent_types = map {$_->typenum}
490 qsearch('agent_type', { 'disabled'=>'' });
491 if ( scalar(@all_agent_types) > 1 ) {
492 push @header, 'Agent types';
493 my $typelink = $p. 'edit/agent_type.cgi?';
494 push @fields, sub { my $part_pkg = shift;
496 map { my $agent_type = $_->agent_type;
498 { 'data' => $agent_type->atype, #escape?
500 'link' => ( $acl_config
515 #if ( $cgi->param('active') ) {
516 push @header, 'Customer<BR>packages';
517 my %col = %{ FS::cust_pkg->statuscolors };
518 my $cust_pkg_link = $p. 'search/cust_pkg.cgi?pkgpart=';
519 push @fields, sub { my $part_pkg = shift;
524 if ( $magic eq 'active' && $part_pkg->freq == 0 ) {
526 #$label = 'one-time charge';
529 $label= 'not yet billed' if $magic eq 'not_yet_billed';
530 $label= 'on hold' if $magic eq 'on_hold';
534 'data' => '<B><FONT COLOR="#'. $col{$label}. '">'.
535 $part_pkg->get("num_$_").
541 ( $part_pkg->get("num_$_") != 1
542 && $label =~ /charge$/
547 'link' => ( $part_pkg->get("num_$_")
555 } (qw( on_hold not_yet_billed active suspended cancelled ))
559 { 'data' => '<FONT SIZE="-1">[ '.
560 include('/elements/popup_link.html',
562 'action' => "${p}edit/bulk-cust_pkg.html?".
563 'pkgpart='.$part_pkg->pkgpart,
564 'actionlabel' => 'Change Packages',
577 push @header, 'Taxclass';
578 push @fields, sub { shift->taxclass() || ' '; };
580 } elsif ( $taxvendor ) {
581 push @header, 'Tax product';
582 my @classnums = ( 'setup', 'recur' );
583 my @classnames = ( 'Setup', 'Recur' );
584 foreach ( qsearch('usage_class', { disabled => '' }) ) {
585 push @classnums, $_->classnum;
586 push @classnames, $_->classname;
588 my $taxproduct_sub = sub {
590 '<SPAN CLASS="taxproduct_desc" TITLE="' .
591 encode_entities($ppt->description) .
592 '">' . encode_entities($ppt->taxproduct) . '</SPAN>'
594 my $taxproduct_list_sub = sub {
595 my $part_pkg = shift;
596 my $base_ppt = $part_pkg->taxproduct;
600 { 'data' => '', 'align' => 'left' },
601 { 'data' => &$taxproduct_sub($base_ppt), 'align' => 'right' },
604 if ( my $units_ppt = $part_pkg->units_taxproduct ) {
606 { 'data' => emt('Lines'), 'align' => 'left' },
607 { 'data' => &$taxproduct_sub($units_ppt), 'align' => 'right' },
610 for (my $i = 0; $i < scalar @classnums; $i++) {
611 my $num = $part_pkg->option('usage_taxproductnum_' . $classnums[$i]);
613 my $ppt = FS::part_pkg_taxproduct->by_key($num);
615 { 'data' => $classnames[$i], 'align' => 'left', },
616 { 'data' => &$taxproduct_sub($ppt), 'align' => 'right' },
621 push @fields, $taxproduct_list_sub;
625 # make a table of report class optionnames => the actual
626 my %report_optionname_name = map { 'report_option_'.$_->num, $_->name }
627 qsearch('part_pkg_report_option', { disabled => '' });
629 push @header, 'Plan options',
631 #'Service', 'Quan', 'Primary';
635 my $part_pkg = shift;
636 if ( $part_pkg->plan ) {
638 my %options = $part_pkg->options;
639 # gather any options that are really report options,
640 # convert them to their user-friendly names,
641 # and sort them (I think?)
644 map { $report_optionname_name{$_} }
646 and exists($report_optionname_name{$_}) }
655 { 'data' => $part_pkg->format($_,$options{$_}),
661 grep { $options{$_} =~ /\S/ }
662 grep { $_ !~ /^(setup|recur)_fee$/
663 and $_ !~ /^report_option_\d+$/
664 and $_ !~ /^usage_taxproductnum_/
668 if ( @report_options ) {
670 [ { 'data' => 'Report classes',
672 'style' => 'font-weight: bold',
675 foreach (@report_options) {
682 } # foreach @report_options
683 } # if @report_options
687 } else { # should never happen...
694 'data' => $part_pkg->$_(),
707 my $part_pkg = shift;
708 my @part_pkg_usage = sort { $a->priority <=> $b->priority }
709 $part_pkg->part_pkg_usage;
714 my $part_svc = $pkg_svc->part_svc;
715 my $svc = $part_svc->svc;
716 if ( $pkg_svc->primary_svc =~ /^Y/i ) {
717 $svc = "<B>$svc (PRIMARY)</B>";
719 $svc =~ s/ +/ /g;
723 'data' => '<B>'. $pkg_svc->quantity. '</B>',
729 'link' => ( $acl_config
730 ? $p. 'edit/part_svc.cgi?'.
737 sort { $b->primary_svc =~ /^Y/i
738 <=> $a->primary_svc =~ /^Y/i
740 $part_pkg->pkg_svc('disable_linked'=>1)
743 my $dst_pkg = $_->dst_pkg;
745 { data => 'Add-on: '.$dst_pkg->pkg_comment,
751 $part_pkg->svc_part_pkg_link
753 ( scalar(@part_pkg_usage) ?
754 [ { data => 'Usage minutes',
758 link => $p.'browse/part_pkg_usage.html#pkgpart'.
764 [ { data => $_->minutes,
767 { data => $_->description,
777 $align .= 'lrl'; #rr';
781 my $count_extra_sql = $extra_sql;
782 $count_extra_sql =~ s/^\s*AND /WHERE /i;
783 $extra_count = ( $count_extra_sql ? ' AND ' : ' WHERE ' ). $extra_count
785 my $count_query = "SELECT COUNT(*) FROM part_pkg $count_extra_sql $extra_count";
789 if ( $acl_edit_bulk ) {
790 # insert a checkbox column
793 '<INPUT TYPE="checkbox" NAME="pkgpart" VALUE=' . $_[0]->pkgpart .'>';
797 $html_form = qq!<FORM ACTION="${p}edit/bulk-part_pkg.html" METHOD="POST">!;
798 $html_foot = include('/search/elements/checkbox-foot.html',
800 { submit => 'edit report classes', },
801 { label => 'change customer packages',
802 onclick=> include('/elements/popup_link_onclick.html',
805 '${p}edit/bulk-cust_pkg.html?' + \$('input[name=pkgpart]').serialize()
807 'actionlabel' => 'Change customer packages',
818 # show this if there are any voip_cdr packages defined
819 if ( FS::part_pkg->count("plan = 'voip_cdr'") ) {
820 push @menubar, 'Per-package usage minutes' => $p.'browse/part_pkg_usage.html';