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('missing_recur_fee') ) {
80 push @where, "0 = ( SELECT COUNT(*) FROM part_pkg_option
81 WHERE optionname = 'recur_fee'
82 AND part_pkg_option.pkgpart = part_pkg.pkgpart
83 AND CAST( optionvalue AS NUMERIC ) > 0
87 if ( $cgi->param('family') =~ /^(\d+)$/ ) {
89 push @where, "family_pkgpart = $1";
90 # Hiding disabled or one-time charges and limiting by classnum aren't
91 # very useful in this mode, so all links should still refer back to the
92 # non-family-limited display.
93 $cgi->param('showdisabled', 1);
94 $cgi->delete('family');
97 push @where, FS::part_pkg->curuser_pkgs_sql
98 unless $acl_edit_global;
100 my $extra_sql = scalar(@where)
101 ? ( scalar(keys %hash) ? ' AND ' : ' WHERE ' ).
102 join( 'AND ', @where)
105 my $agentnums_sql = $curuser->agentnums_sql( 'table'=>'cust_main' );
106 my $count_cust_pkg = "
107 SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
108 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
111 my $count_cust_pkg_cancel = "
112 SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
113 LEFT JOIN cust_pkg AS cust_pkg_next
114 ON (cust_pkg.pkgnum = cust_pkg_next.change_pkgnum)
115 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
117 AND cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0
125 AND ( setup IS NULL OR setup = 0 )
126 AND ( cancel IS NULL OR cancel = 0 )
127 AND ( susp IS NULL OR susp = 0 )
128 ) AS num_not_yet_billed,
131 AND setup IS NOT NULL AND setup != 0
132 AND ( cancel IS NULL OR cancel = 0 )
133 AND ( susp IS NULL OR susp = 0 )
137 AND ( cancel IS NULL OR cancel = 0 )
138 AND susp IS NOT NULL AND susp != 0
139 AND setup IS NOT NULL AND setup != 0
143 AND ( cancel IS NULL OR cancel = 0 )
144 AND susp IS NOT NULL AND susp != 0
145 AND ( setup IS NULL OR setup = 0 )
148 ( $count_cust_pkg_cancel
149 AND (cust_pkg_next.pkgnum IS NULL
150 OR cust_pkg_next.pkgpart != cust_pkg.pkgpart)
154 # About the num_cancelled expression: packages that were changed, but
155 # kept the same pkgpart, are considered "moved", not "canceled" (because
156 # this is the part_pkg UI). We could show the count of those but it's
157 # probably not interesting.
160 One or more service definitions are grouped together into a package
161 definition and given pricing information. Customers purchase packages
162 rather than purchase services directly.<BR><BR>
163 <FORM METHOD="GET" ACTION="${p}edit/part_pkg.cgi">
164 <A HREF="${p}edit/part_pkg.cgi"><I>Add a new package definition</I></A>
166 !.include('/elements/select-part_pkg.html', 'element_name' => 'clone' ). qq!
167 <INPUT TYPE="submit" VALUE="Clone existing package">
172 $cgi->param('dummy', 1);
175 qq(\n<SCRIPT TYPE="text/javascript">\n).
176 "function filter_change() {".
177 " window.location = '". $cgi->self_url.
178 ";classnum=' + document.getElementById('classnum').options[document.getElementById('classnum').selectedIndex].value".
182 #restore this so pagination works
183 $cgi->param('classnum', $classnum) if length($classnum);
185 #should hide this if there aren't any classes
187 "$filter_change\n<BR>( show class: ".
188 include('/elements/select-pkg_class.html',
189 #'curr_value' => $classnum,
190 'value' => $classnum, #insist on 0 :/
191 'onchange' => 'filter_change()',
192 'pre_options' => [ '-1' => 'all',
194 'disable_empty' => 1,
198 my $recur_toggle = $cgi->param('recurring') ? 'show' : 'hide';
199 $cgi->param('recurring', $cgi->param('recurring') ^ 1 );
202 '( <A HREF="'. $cgi->self_url.'">'. "$recur_toggle one-time charges</A> )";
204 $cgi->param('recurring', $cgi->param('recurring') ^ 1 ); #put it back
208 my $link = [ $p.'edit/part_pkg.cgi?', 'pkgpart' ];
210 my @header = ( '#', 'Package', 'Comment', 'Custom' );
211 my @fields = ( 'pkgpart', 'pkg', 'comment',
212 sub{ '<B><FONT COLOR="#0000CC">'.$_[0]->custom.'</FONT></B>' }
215 my @links = ( $link, $link, '', '' );
217 unless ( 0 ) { #already showing only one class or something?
218 push @header, 'Class';
219 push @fields, sub { shift->classname || '(none)'; };
223 if ( $conf->exists('pkg-addon_classnum') ) {
224 push @header, "Add'l order class";
225 push @fields, sub { shift->addon_classname || '(none)'; };
229 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
231 tie my %plan_labels, 'Tie::IxHash',
232 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
235 push @header, 'Pricing';
238 my $part_pkg = shift;
239 (my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ / /g;
240 my $is_recur = ( $part_pkg->freq ne '0' );
241 my @discounts = sort { $a->months <=> $b->months }
243 $part_pkg->part_pkg_discount;
246 ( !$family_pkgpart &&
247 $part_pkg->pkgpart == $part_pkg->family_pkgpart ? () : [
252 'data' => '<b>Show all versions</b>',
253 'link' => $p.'browse/part_pkg.cgi?family='.$part_pkg->family_pkgpart,
263 { data =>$money_char.
264 sprintf('%.2f ', $part_pkg->option('setup_fee') ),
267 { data => ( ( $is_recur ? ' setup' : ' one-time' ).
268 ( $part_pkg->option('recur_fee') == 0
269 && $part_pkg->setup_show_zero
270 ? ' (printed on invoices)'
280 ? $money_char. sprintf('%.2f', $part_pkg->option('recur_fee'))
281 : $part_pkg->freq_pretty
283 align=> ( $is_recur ? 'right' : 'center' ),
284 colspan=> ( $is_recur ? 1 : 2 ),
287 ? { data => ( $is_recur
288 ? ' '. $part_pkg->freq_pretty.
289 ( $part_pkg->option('recur_fee') == 0
290 && $part_pkg->recur_show_zero
291 ? ' (printed on invoices)'
300 ( map { my $dst_pkg = $_->dst_pkg;
302 { data => 'Supplemental: '.
303 '<A HREF="#'. $dst_pkg->pkgpart . '">' .
304 $dst_pkg->pkg . '</A>',
310 $part_pkg->supp_part_pkg_link
313 my $dst_pkg = $_->dst_pkg;
315 { data => 'Add-on: '.$dst_pkg->pkg_comment,
321 $part_pkg->bill_part_pkg_link
325 { data => '<b>Discounts</b>',
335 { data => $_->months. ':',
338 { data => $_->amount ? '$'. $_->amount : $_->percent. '%'
347 # $plan_labels{$part_pkg->plan}.'<BR>'.
348 # $money_char.sprintf('%.2f setup<BR>', $part_pkg->option('setup_fee') ).
349 # ( $part_pkg->freq ne '0'
350 # ? $money_char.sprintf('%.2f ', $part_pkg->option('recur_fee') )
353 # $part_pkg->freq_pretty; #.'<BR>'
356 push @header, 'Cost tracking';
359 my $part_pkg = shift;
360 #(my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ / /g;
361 my $is_recur = ( $part_pkg->freq ne '0' );
365 { data => ' ', # $plan,
371 { data =>$money_char.
372 sprintf('%.2f ', $part_pkg->setup_cost ),
375 { data => ( $is_recur ? ' setup' : ' one-time' ),
382 ? $money_char. sprintf('%.2f', $part_pkg->recur_cost)
383 : '(no recurring)' #$part_pkg->freq_pretty
385 align=> ( $is_recur ? 'right' : 'center' ),
386 colspan=> ( $is_recur ? 1 : 2 ),
389 ? { data => ( $is_recur
390 ? ' '. $part_pkg->freq_pretty
402 # Agent goes here if displayed
406 if ( $acl_edit_global ) {
407 #really we just want a count, but this is fine unless someone has tons
408 my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
409 if ( scalar(@all_agent_types) > 1 ) {
410 push @header, 'Agent types';
411 my $typelink = $p. 'edit/agent_type.cgi?';
412 push @fields, sub { my $part_pkg = shift;
414 map { my $agent_type = $_->agent_type;
416 { 'data' => $agent_type->atype, #escape?
418 'link' => ( $acl_config
433 #if ( $cgi->param('active') ) {
434 push @header, 'Customer<BR>packages';
436 'on hold' => '7E0079', #purple!
437 'not yet billed' => '009999', #teal? cyan?
438 'active' => '00CC00',
439 'suspended' => 'FF9900',
440 'cancelled' => 'FF0000',
441 #'one-time charge' => '000000',
442 'charge' => '000000',
444 my $cust_pkg_link = $p. 'search/cust_pkg.cgi?pkgpart=';
445 push @fields, sub { my $part_pkg = shift;
450 if ( $magic eq 'active' && $part_pkg->freq == 0 ) {
452 #$label = 'one-time charge';
455 $label= 'not yet billed' if $magic eq 'not_yet_billed';
456 $label= 'on hold' if $magic eq 'on_hold';
460 'data' => '<B><FONT COLOR="#'. $col{$label}. '">'.
461 $part_pkg->get("num_$_").
467 ( $part_pkg->get("num_$_") != 1
468 && $label =~ /charge$/
473 'link' => ( $part_pkg->get("num_$_")
481 } (qw( on_hold not_yet_billed active suspended cancelled ))
485 { 'data' => '<FONT SIZE="-1">[ '.
486 include('/elements/popup_link.html',
488 'action' => "${p}edit/bulk-cust_pkg.html?".
489 'pkgpart='.$part_pkg->pkgpart,
490 'actionlabel' => 'Change Packages',
503 push @header, 'Taxclass';
504 push @fields, sub { shift->taxclass() || ' '; };
508 # make a table of report class optionnames => the actual
509 my %report_optionname_name = map { 'report_option_'.$_->num, $_->name }
510 qsearch('part_pkg_report_option', { disabled => '' });
512 push @header, 'Plan options',
514 #'Service', 'Quan', 'Primary';
518 my $part_pkg = shift;
519 if ( $part_pkg->plan ) {
521 my %options = $part_pkg->options;
522 # gather any options that are really report options,
523 # convert them to their user-friendly names,
524 # and sort them (I think?)
527 map { $report_optionname_name{$_} }
529 and exists($report_optionname_name{$_}) }
538 { 'data' => $part_pkg->format($_,$options{$_}),
543 grep { $options{$_} =~ /\S/ }
544 grep { $_ !~ /^(setup|recur)_fee$/
545 and $_ !~ /^report_option_\d+$/ }
548 if ( @report_options ) {
550 [ { 'data' => 'Report classes',
552 'style' => 'font-weight: bold',
555 foreach (@report_options) {
562 } # foreach @report_options
563 } # if @report_options
567 } else { # should never happen...
574 'data' => $part_pkg->$_(),
587 my $part_pkg = shift;
588 my @part_pkg_usage = sort { $a->priority <=> $b->priority }
589 $part_pkg->part_pkg_usage;
594 my $part_svc = $pkg_svc->part_svc;
595 my $svc = $part_svc->svc;
596 if ( $pkg_svc->primary_svc =~ /^Y/i ) {
597 $svc = "<B>$svc (PRIMARY)</B>";
599 $svc =~ s/ +/ /g;
603 'data' => '<B>'. $pkg_svc->quantity. '</B>',
609 'link' => ( $acl_config
610 ? $p. 'edit/part_svc.cgi?'.
617 sort { $b->primary_svc =~ /^Y/i
618 <=> $a->primary_svc =~ /^Y/i
620 $part_pkg->pkg_svc('disable_linked'=>1)
623 my $dst_pkg = $_->dst_pkg;
625 { data => 'Add-on: '.$dst_pkg->pkg_comment,
631 $part_pkg->svc_part_pkg_link
633 ( scalar(@part_pkg_usage) ?
634 [ { data => 'Usage minutes',
638 link => $p.'browse/part_pkg_usage.html#pkgpart'.
644 [ { data => $_->minutes,
647 { data => $_->description,
657 $align .= 'lrl'; #rr';
661 my $count_extra_sql = $extra_sql;
662 $count_extra_sql =~ s/^\s*AND /WHERE /i;
663 $extra_count = ( $count_extra_sql ? ' AND ' : ' WHERE ' ). $extra_count
665 my $count_query = "SELECT COUNT(*) FROM part_pkg $count_extra_sql $extra_count";
669 if ( $acl_edit_bulk ) {
670 # insert a checkbox column
673 '<INPUT TYPE="checkbox" NAME="pkgpart" VALUE=' . $_[0]->pkgpart .'>';
677 $html_form = qq!<FORM ACTION="${p}edit/bulk-part_pkg.html" METHOD="POST">!;
678 $html_foot = include('/search/elements/checkbox-foot.html',
679 submit => 'edit report classes', # for now it's only report classes
684 # show this if there are any voip_cdr packages defined
685 if ( FS::part_pkg->count("plan = 'voip_cdr'") ) {
686 push @menubar, 'Per-package usage minutes' => $p.'browse/part_pkg_usage.html';