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({});
41 $(document).ready(function(){
48 my $curuser = $FS::CurrentUser::CurrentUser;
50 my $edit = 'Edit package definitions';
51 my $edit_global = 'Edit global package definitions';
52 my $acl_edit = $curuser->access_right($edit);
53 my $acl_edit_global = $curuser->access_right($edit_global);
54 my $acl_config = $curuser->access_right('Configuration'); #to edit services
57 my $acl_edit_bulk = $curuser->access_right('Bulk edit package definitions');
60 unless $acl_edit || $acl_edit_global;
62 my $conf = new FS::Conf;
63 my $taxclasses = $conf->exists('enable_taxclasses');
64 my $taxvendor = $conf->config('tax_data_vendor');
65 my $money_char = $conf->config('money_char') || '$';
66 my $disable_counts = $conf->exists('config-disable_counts') ? 1 : 0;
69 my $orderby = 'pkgpart';
74 if ( $cgi->param('active') ) {
75 $orderby = 'num_active DESC';
80 #if ( $cgi->param('activeONLY') ) {
81 # push @where, ' WHERE num_active > 0 '; #XXX doesn't affect count...
84 if ( $cgi->param('recurring') ) {
85 $hash{'freq'} = { op=>'!=', value=>'0' };
86 $extra_count = " freq != '0' ";
90 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
92 push @where, $classnum ? "classnum = $classnum"
95 $cgi->delete('classnum');
97 if ( $cgi->param('pkgpartbatch') =~ /^([\w\/\-\:\. ]+)$/ ) {
98 push @where, "pkgpartbatch = '$1' ";
101 if ( $cgi->param('missing_recur_fee') ) {
102 push @where, "NOT EXISTS ( SELECT 1 FROM part_pkg_option
103 WHERE optionname = 'recur_fee'
104 AND part_pkg_option.pkgpart = part_pkg.pkgpart
105 AND CAST( optionvalue AS NUMERIC ) > 0
109 if ( $cgi->param('ratenum') =~ /^(\d+)$/ ) {
110 push @where, "EXISTS( SELECT 1 FROM part_pkg_option
111 WHERE optionname LIKE '%ratenum'
112 AND optionvalue = '$1'
113 AND part_pkg_option.pkgpart = part_pkg.pkgpart
117 if ( $cgi->param('family') =~ /^(\d+)$/ ) {
118 $family_pkgpart = $1;
119 push @where, "family_pkgpart = $1";
120 # Hiding disabled or one-time charges and limiting by classnum aren't
121 # very useful in this mode, so all links should still refer back to the
122 # non-family-limited display.
123 $cgi->param('showdisabled', 1);
124 $cgi->delete('family');
127 push @where, FS::part_pkg->curuser_pkgs_sql
128 unless $acl_edit_global;
130 my $extra_sql = scalar(@where)
131 ? ( scalar(keys %hash) ? ' AND ' : ' WHERE ' ).
132 join( 'AND ', @where)
135 my $agentnums_sql = $curuser->agentnums_sql( 'table'=>'cust_main' );
136 my $count_cust_pkg = "
137 SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
138 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
141 my $count_cust_pkg_cancel = "
142 SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
143 LEFT JOIN cust_pkg AS cust_pkg_next
144 ON (cust_pkg.pkgnum = cust_pkg_next.change_pkgnum)
145 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
147 AND cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0
150 unless ( $disable_counts ) {
156 AND ( setup IS NULL OR setup = 0 )
157 AND ( cancel IS NULL OR cancel = 0 )
158 AND ( susp IS NULL OR susp = 0 )
159 ) AS num_not_yet_billed,
162 AND setup IS NOT NULL AND setup != 0
163 AND ( cancel IS NULL OR cancel = 0 )
164 AND ( susp IS NULL OR susp = 0 )
168 AND ( cancel IS NULL OR cancel = 0 )
169 AND susp IS NOT NULL AND susp != 0
170 AND setup IS NOT NULL AND setup != 0
174 AND ( cancel IS NULL OR cancel = 0 )
175 AND susp IS NOT NULL AND susp != 0
176 AND ( setup IS NULL OR setup = 0 )
179 ( $count_cust_pkg_cancel
180 AND (cust_pkg_next.pkgnum IS NULL
181 OR cust_pkg_next.pkgpart != cust_pkg.pkgpart)
187 # About the num_cancelled expression: packages that were changed, but
188 # kept the same pkgpart, are considered "moved", not "canceled" (because
189 # this is the part_pkg UI). We could show the count of those but it's
190 # probably not interesting.
193 One or more service definitions are grouped together into a package
194 definition and given pricing information. Customers purchase packages
195 rather than purchase services directly.<BR><BR>
196 <FORM METHOD="GET" ACTION="${p}edit/part_pkg.cgi">
197 <A HREF="${p}edit/part_pkg.cgi"><I>Add a new package definition</I></A>
199 !.include('/elements/select-part_pkg.html', 'element_name' => 'clone' ). qq!
200 <INPUT TYPE="submit" VALUE="Clone existing package">
204 $html_init .= include('.style');
206 $cgi->param('dummy', 1);
209 qq(\n<SCRIPT TYPE="text/javascript">\n).
210 "function filter_change() {".
211 " window.location = '". $cgi->self_url.
212 ";classnum=' + document.getElementById('classnum').options[document.getElementById('classnum').selectedIndex].value".
216 #restore this so pagination works
217 $cgi->param('classnum', $classnum) if length($classnum);
219 #should hide this if there aren't any classes
221 "$filter_change\n<BR>( show class: ".
222 include('/elements/select-pkg_class.html',
223 #'curr_value' => $classnum,
224 'value' => $classnum, #insist on 0 :/
225 'onchange' => 'filter_change()',
226 'pre_options' => [ '-1' => 'all',
228 'disable_empty' => 1,
232 my $recur_toggle = $cgi->param('recurring') ? 'show' : 'hide';
233 $cgi->param('recurring', $cgi->param('recurring') ^ 1 );
236 '( <A HREF="'. $cgi->self_url.'">'. "$recur_toggle one-time charges</A> )";
238 $cgi->param('recurring', $cgi->param('recurring') ^ 1 ); #put it back
242 my $link = [ $p.'edit/part_pkg.cgi?', 'pkgpart' ];
244 my @header = ( '#', 'Package', 'Comment', 'Custom' );
245 my @fields = ( 'pkgpart', 'pkg', 'comment',
246 sub{ '<B><FONT COLOR="#0000CC">'.$_[0]->custom.'</FONT></B>' }
249 my @links = ( $link, $link, '', '' );
251 unless ( 0 ) { #already showing only one class or something?
252 push @header, 'Class';
253 push @fields, sub { shift->classname || '(none)'; };
257 if ( $conf->exists('pkg-addon_classnum') ) {
258 push @header, "Add'l order class";
259 push @fields, sub { shift->addon_classname || '(none)'; };
263 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
265 tie my %plan_labels, 'Tie::IxHash',
266 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
269 push @header, 'Pricing';
272 my $part_pkg = shift;
273 (my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ / /g;
274 my $is_recur = ( $part_pkg->freq ne '0' );
275 my @discounts = sort { $a->months <=> $b->months }
277 $part_pkg->part_pkg_discount;
280 # Line 0: Family package link (if applicable)
281 ( !$family_pkgpart &&
282 $part_pkg->pkgpart == $part_pkg->family_pkgpart ? () : [
287 'data' => '<b>Show all versions</b>',
288 'link' => $p.'browse/part_pkg.cgi?family='.$part_pkg->family_pkgpart,
291 [ # Line 1: Plan type (Anniversary, Prorate, Call Rating, etc.)
297 [ # Line 2: Setup fee
298 { data =>$money_char.
299 sprintf('%.2f ', $part_pkg->option('setup_fee') ),
302 { data => ( ( $is_recur ? ' setup' : ' one-time' ).
303 ( $part_pkg->option('recur_fee') == 0
304 && $part_pkg->setup_show_zero
305 ? ' (printed on invoices)'
312 [ # Line 3: Recurring fee
315 ? $money_char. sprintf('%.2f', $part_pkg->option('recur_fee'))
316 : $part_pkg->freq_pretty
318 align=> ( $is_recur ? 'right' : 'center' ),
319 colspan=> ( $is_recur ? 1 : 2 ),
322 ? { data => ' '. $part_pkg->freq_pretty.
323 ( $part_pkg->option('recur_fee') == 0
324 && $part_pkg->recur_show_zero
325 ? ' (printed on invoices)'
333 [ { data => ' ' }, ], # Line 4: empty
334 ( $part_pkg->adjourn_months ?
335 [ # Line 5: Adjourn months
336 { data => mt('After [quant,_1,month], <strong>suspend</strong> the package.',
337 $part_pkg->adjourn_months),
344 ( $part_pkg->contract_end_months ?
345 [ # Line 6: Contract end months
346 { data => mt('After [quant,_1,month], <strong>contract ends</strong>.',
347 $part_pkg->contract_end_months),
354 ( $part_pkg->expire_months ?
355 [ # Line 7: Expire months and automatic transfer
356 { data => $part_pkg->change_to_pkgpart ?
357 mt('After [quant,_1,month], <strong>change to</strong> ',
358 $part_pkg->expire_months) .
359 qq(<a href="${p}edit/part_pkg.cgi?) .
360 $part_pkg->change_to_pkgpart .
361 qq(">) . $part_pkg->change_to_pkg->pkg . qq(</a>) . '.'
362 : mt('After [quant,_1,month], <strong>cancel</strong> the package.',
363 $part_pkg->expire_months)
372 map { my $amount = $_->amount / ($_->target_info->{multiplier} || 1);
373 my $label = $_->target_info->{label};
375 { data => "Plus $money_char". $_->price. ' '.
376 ( $_->action eq 'increment' ? 'per' : 'for' ).
377 " $amount $label",
378 align => 'center', #left?
383 $part_pkg->part_pkg_usageprice
386 map { my $dst_pkg = $_->dst_pkg;
388 { data => 'Supplemental: '.
389 '<A HREF="#'. $dst_pkg->pkgpart . '">' .
390 $dst_pkg->pkg . '</A>',
396 $part_pkg->supp_part_pkg_link
398 ( # Billing add-ons/bundle packages
400 my $dst_pkg = $_->dst_pkg;
402 { data => 'Add-on: '.$dst_pkg->pkg_comment,
408 $part_pkg->bill_part_pkg_link
410 ( # Discounts available
413 { data => '<b>Discounts</b>',
423 { data => $_->months. ':',
426 { data => $_->amount ? '$'. $_->amount : $_->percent. '%'
433 ]; # end of "middle column"
435 # $plan_labels{$part_pkg->plan}.'<BR>'.
436 # $money_char.sprintf('%.2f setup<BR>', $part_pkg->option('setup_fee') ).
437 # ( $part_pkg->freq ne '0'
438 # ? $money_char.sprintf('%.2f ', $part_pkg->option('recur_fee') )
441 # $part_pkg->freq_pretty; #.'<BR>'
444 push @header, 'Cost tracking';
447 my $part_pkg = shift;
448 #(my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ / /g;
449 my $is_recur = ( $part_pkg->freq ne '0' );
453 { data => ' ', # $plan,
459 { data =>$money_char.
460 sprintf('%.2f ', $part_pkg->setup_cost ),
463 { data => ( $is_recur ? ' setup' : ' one-time' ),
470 ? $money_char. sprintf('%.2f', $part_pkg->recur_cost)
471 : '(no recurring)' #$part_pkg->freq_pretty
473 align=> ( $is_recur ? 'right' : 'center' ),
474 colspan=> ( $is_recur ? 1 : 2 ),
477 ? { data => ( $is_recur
478 ? ' '. $part_pkg->freq_pretty
490 # Agent goes here if displayed
494 if ( $acl_edit_global ) {
495 #really we just want a count, but this is fine unless someone has tons
496 my @all_agent_types = map {$_->typenum}
497 qsearch('agent_type', { 'disabled'=>'' });
498 if ( scalar(@all_agent_types) > 1 ) {
499 push @header, 'Agent types';
500 my $typelink = $p. 'edit/agent_type.cgi?';
501 push @fields, sub { my $part_pkg = shift;
503 map { my $agent_type = $_->agent_type;
505 { 'data' => $agent_type->atype, #escape?
507 'link' => ( $acl_config
522 #if ( $cgi->param('active') ) {
523 push @header, 'Customer<BR>packages';
524 my %col = %{ FS::cust_pkg->statuscolors };
525 my $cust_pkg_link = $p. 'search/cust_pkg.cgi?pkgpart=';
526 push @fields, sub { my $part_pkg = shift;
531 if ( $magic eq 'active' && $part_pkg->freq == 0 ) {
533 #$label = 'one-time charge';
536 $label= 'not yet billed' if $magic eq 'not_yet_billed';
537 $label= 'on hold' if $magic eq 'on_hold';
541 'data' => '<B><FONT COLOR="#'. $col{$label}. '">'.
542 $part_pkg->get("num_$_").
548 ( $part_pkg->get("num_$_") != 1
549 && $label =~ /charge$/
554 'link' => ( $part_pkg->get("num_$_") || $disable_counts
562 } (qw( on_hold not_yet_billed active suspended cancelled ))
566 { 'data' => '<FONT SIZE="-1">[ '.
567 include('/elements/popup_link.html',
569 'action' => "${p}edit/bulk-cust_pkg.html?".
570 'pkgpart='.$part_pkg->pkgpart,
571 'actionlabel' => 'Change Packages',
584 push @header, 'Taxclass';
585 push @fields, sub { shift->taxclass() || ' '; };
587 } elsif ( $taxvendor ) {
588 push @header, 'Tax product';
589 my @classnums = ( 'setup', 'recur' );
590 my @classnames = ( 'Setup', 'Recur' );
591 foreach ( qsearch('usage_class', { disabled => '' }) ) {
592 push @classnums, $_->classnum;
593 push @classnames, $_->classname;
595 my $taxproduct_sub = sub {
597 '<SPAN CLASS="taxproduct_desc" TITLE="' .
598 encode_entities($ppt->description) .
599 '">' . encode_entities($ppt->taxproduct) . '</SPAN>'
601 my $taxproduct_list_sub = sub {
602 my $part_pkg = shift;
603 my $base_ppt = $part_pkg->taxproduct;
607 { 'data' => '', 'align' => 'left' },
608 { 'data' => &$taxproduct_sub($base_ppt), 'align' => 'right' },
611 if ( my $units_ppt = $part_pkg->units_taxproduct ) {
613 { 'data' => emt('Lines'), 'align' => 'left' },
614 { 'data' => &$taxproduct_sub($units_ppt), 'align' => 'right' },
617 for (my $i = 0; $i < scalar @classnums; $i++) {
618 my $num = $part_pkg->option('usage_taxproductnum_' . $classnums[$i]);
620 my $ppt = FS::part_pkg_taxproduct->by_key($num);
622 { 'data' => $classnames[$i], 'align' => 'left', },
623 { 'data' => &$taxproduct_sub($ppt), 'align' => 'right' },
628 push @fields, $taxproduct_list_sub;
632 # make a table of report class optionnames => the actual
633 my %report_optionname_name = map { 'report_option_'.$_->num, $_->name }
634 qsearch('part_pkg_report_option', { disabled => '' });
636 push @header, 'Plan options',
638 #'Service', 'Quan', 'Primary';
642 my $part_pkg = shift;
643 if ( $part_pkg->plan ) {
645 my %options = $part_pkg->options;
646 # gather any options that are really report options,
647 # convert them to their user-friendly names,
648 # and sort them (I think?)
651 map { $report_optionname_name{$_} }
653 and exists($report_optionname_name{$_}) }
662 { 'data' => $part_pkg->format($_,$options{$_}),
668 grep { $options{$_} =~ /\S/ }
669 grep { $_ !~ /^(setup|recur)_fee$/
670 and $_ !~ /^report_option_\d+$/
671 and $_ !~ /^usage_taxproductnum_/
675 if ( @report_options ) {
677 [ { 'data' => 'Report classes',
679 'style' => 'font-weight: bold',
682 foreach (@report_options) {
689 } # foreach @report_options
690 } # if @report_options
694 } else { # should never happen...
701 'data' => $part_pkg->$_(),
714 my $part_pkg = shift;
715 my @part_pkg_usage = sort { $a->priority <=> $b->priority }
716 $part_pkg->part_pkg_usage;
721 my $part_svc = $pkg_svc->part_svc;
722 my $svc = $part_svc->svc;
723 if ( $pkg_svc->primary_svc =~ /^Y/i ) {
724 $svc = "<B>$svc (PRIMARY)</B>";
726 $svc =~ s/ +/ /g;
730 'data' => '<B>'. $pkg_svc->quantity. '</B> ',
736 'link' => ( $acl_config
737 ? $p. 'edit/part_svc.cgi?'.
744 sort { $b->primary_svc =~ /^Y/i
745 <=> $a->primary_svc =~ /^Y/i
747 $part_pkg->pkg_svc('disable_linked'=>1)
750 my $dst_pkg = $_->dst_pkg;
752 { data => 'Add-on: '.$dst_pkg->pkg_comment,
758 $part_pkg->svc_part_pkg_link
760 ( scalar(@part_pkg_usage) ?
761 [ { data => 'Usage minutes',
765 link => $p.'browse/part_pkg_usage.html#pkgpart'.
771 [ { data => $_->minutes,
774 { data => $_->description,
784 $align .= 'lrl'; #rr';
788 my $count_extra_sql = $extra_sql;
789 $count_extra_sql =~ s/^\s*AND /WHERE /i;
790 $extra_count = ( $count_extra_sql ? ' AND ' : ' WHERE ' ). $extra_count
792 my $count_query = "SELECT COUNT(*) FROM part_pkg $count_extra_sql $extra_count";
796 if ( $acl_edit_bulk ) {
797 # insert a checkbox column
800 '<INPUT TYPE="checkbox" NAME="pkgpart" VALUE=' . $_[0]->pkgpart .'>';
804 $html_form = qq!<FORM ACTION="${p}edit/bulk-part_pkg.html" METHOD="POST">!;
805 $html_foot = include('/search/elements/checkbox-foot.html',
807 { label => 'edit packages',
808 onclick=> include('/elements/popup_link_onclick.html',
811 '${p}edit/bulk-part_pkg.html?' + \$('input[name=pkgpart]').serialize()
813 'actionlabel' => 'Bulk edit packages',
818 { label => 'change customers packages',
819 onclick=> include('/elements/popup_link_onclick.html',
822 '${p}edit/bulk-cust_pkg.html?' + \$('input[name=pkgpart]').serialize()
824 'actionlabel' => 'Change customer packages',
835 # show this if there are any voip_cdr packages defined
836 if ( FS::part_pkg->count("plan = 'voip_cdr'") ) {
837 push @menubar, 'Per-package usage minutes' => $p.'browse/part_pkg_usage.html';