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('pkgpartbatch') =~ /^([\w\/\-\:\. ]+)$/ ) {
80 push @where, "pkgpartbatch = '$1' ";
83 if ( $cgi->param('missing_recur_fee') ) {
84 push @where, "NOT EXISTS ( SELECT 1 FROM part_pkg_option
85 WHERE optionname = 'recur_fee'
86 AND part_pkg_option.pkgpart = part_pkg.pkgpart
87 AND CAST( optionvalue AS NUMERIC ) > 0
91 if ( $cgi->param('family') =~ /^(\d+)$/ ) {
93 push @where, "family_pkgpart = $1";
94 # Hiding disabled or one-time charges and limiting by classnum aren't
95 # very useful in this mode, so all links should still refer back to the
96 # non-family-limited display.
97 $cgi->param('showdisabled', 1);
98 $cgi->delete('family');
101 push @where, FS::part_pkg->curuser_pkgs_sql
102 unless $acl_edit_global;
104 my $extra_sql = scalar(@where)
105 ? ( scalar(keys %hash) ? ' AND ' : ' WHERE ' ).
106 join( 'AND ', @where)
109 my $agentnums_sql = $curuser->agentnums_sql( 'table'=>'cust_main' );
110 my $count_cust_pkg = "
111 SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
112 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
115 my $count_cust_pkg_cancel = "
116 SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
117 LEFT JOIN cust_pkg AS cust_pkg_next
118 ON (cust_pkg.pkgnum = cust_pkg_next.change_pkgnum)
119 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
121 AND cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0
129 AND ( setup IS NULL OR setup = 0 )
130 AND ( cancel IS NULL OR cancel = 0 )
131 AND ( susp IS NULL OR susp = 0 )
132 ) AS num_not_yet_billed,
135 AND setup IS NOT NULL AND setup != 0
136 AND ( cancel IS NULL OR cancel = 0 )
137 AND ( susp IS NULL OR susp = 0 )
141 AND ( cancel IS NULL OR cancel = 0 )
142 AND susp IS NOT NULL AND susp != 0
143 AND setup IS NOT NULL AND setup != 0
147 AND ( cancel IS NULL OR cancel = 0 )
148 AND susp IS NOT NULL AND susp != 0
149 AND ( setup IS NULL OR setup = 0 )
152 ( $count_cust_pkg_cancel
153 AND (cust_pkg_next.pkgnum IS NULL
154 OR cust_pkg_next.pkgpart != cust_pkg.pkgpart)
158 # About the num_cancelled expression: packages that were changed, but
159 # kept the same pkgpart, are considered "moved", not "canceled" (because
160 # this is the part_pkg UI). We could show the count of those but it's
161 # probably not interesting.
164 One or more service definitions are grouped together into a package
165 definition and given pricing information. Customers purchase packages
166 rather than purchase services directly.<BR><BR>
167 <FORM METHOD="GET" ACTION="${p}edit/part_pkg.cgi">
168 <A HREF="${p}edit/part_pkg.cgi"><I>Add a new package definition</I></A>
170 !.include('/elements/select-part_pkg.html', 'element_name' => 'clone' ). qq!
171 <INPUT TYPE="submit" VALUE="Clone existing package">
176 $cgi->param('dummy', 1);
179 qq(\n<SCRIPT TYPE="text/javascript">\n).
180 "function filter_change() {".
181 " window.location = '". $cgi->self_url.
182 ";classnum=' + document.getElementById('classnum').options[document.getElementById('classnum').selectedIndex].value".
186 #restore this so pagination works
187 $cgi->param('classnum', $classnum) if length($classnum);
189 #should hide this if there aren't any classes
191 "$filter_change\n<BR>( show class: ".
192 include('/elements/select-pkg_class.html',
193 #'curr_value' => $classnum,
194 'value' => $classnum, #insist on 0 :/
195 'onchange' => 'filter_change()',
196 'pre_options' => [ '-1' => 'all',
198 'disable_empty' => 1,
202 my $recur_toggle = $cgi->param('recurring') ? 'show' : 'hide';
203 $cgi->param('recurring', $cgi->param('recurring') ^ 1 );
206 '( <A HREF="'. $cgi->self_url.'">'. "$recur_toggle one-time charges</A> )";
208 $cgi->param('recurring', $cgi->param('recurring') ^ 1 ); #put it back
212 my $link = [ $p.'edit/part_pkg.cgi?', 'pkgpart' ];
214 my @header = ( '#', 'Package', 'Comment', 'Custom' );
215 my @fields = ( 'pkgpart', 'pkg', 'comment',
216 sub{ '<B><FONT COLOR="#0000CC">'.$_[0]->custom.'</FONT></B>' }
219 my @links = ( $link, $link, '', '' );
221 unless ( 0 ) { #already showing only one class or something?
222 push @header, 'Class';
223 push @fields, sub { shift->classname || '(none)'; };
227 if ( $conf->exists('pkg-addon_classnum') ) {
228 push @header, "Add'l order class";
229 push @fields, sub { shift->addon_classname || '(none)'; };
233 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
235 tie my %plan_labels, 'Tie::IxHash',
236 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
239 push @header, 'Pricing';
242 my $part_pkg = shift;
243 (my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ / /g;
244 my $is_recur = ( $part_pkg->freq ne '0' );
245 my @discounts = sort { $a->months <=> $b->months }
247 $part_pkg->part_pkg_discount;
250 # Line 0: Family package link (if applicable)
251 ( !$family_pkgpart &&
252 $part_pkg->pkgpart == $part_pkg->family_pkgpart ? () : [
257 'data' => '<b>Show all versions</b>',
258 'link' => $p.'browse/part_pkg.cgi?family='.$part_pkg->family_pkgpart,
261 [ # Line 1: Plan type (Anniversary, Prorate, Call Rating, etc.)
267 [ # Line 2: Setup fee
268 { data =>$money_char.
269 sprintf('%.2f ', $part_pkg->option('setup_fee') ),
272 { data => ( ( $is_recur ? ' setup' : ' one-time' ).
273 ( $part_pkg->option('recur_fee') == 0
274 && $part_pkg->setup_show_zero
275 ? ' (printed on invoices)'
282 [ # Line 3: Recurring fee
285 ? $money_char. sprintf('%.2f', $part_pkg->option('recur_fee'))
286 : $part_pkg->freq_pretty
288 align=> ( $is_recur ? 'right' : 'center' ),
289 colspan=> ( $is_recur ? 1 : 2 ),
292 ? { data => ' '. $part_pkg->freq_pretty.
293 ( $part_pkg->option('recur_fee') == 0
294 && $part_pkg->recur_show_zero
295 ? ' (printed on invoices)'
303 [ { data => ' ' }, ], # Line 4: empty
304 ( $part_pkg->adjourn_months ?
305 [ # Line 5: Adjourn months
306 { data => mt('After [quant,_1,month], <strong>suspend</strong> the package.',
307 $part_pkg->adjourn_months),
314 ( $part_pkg->contract_end_months ?
315 [ # Line 6: Contract end months
316 { data => mt('After [quant,_1,month], <strong>contract ends</strong>.',
317 $part_pkg->contract_end_months),
324 ( $part_pkg->expire_months ?
325 [ # Line 7: Expire months and automatic transfer
326 { data => $part_pkg->change_to_pkgpart ?
327 mt('After [quant,_1,month], <strong>change to</strong> ',
328 $part_pkg->expire_months) .
329 qq(<a href="${p}edit/part_pkg.cgi?) .
330 $part_pkg->change_to_pkgpart .
331 qq(">) . $part_pkg->change_to_pkg->pkg . qq(</a>) . '.'
332 : mt('After [quant,_1,month], <strong>cancel</strong> the package.',
333 $part_pkg->expire_months)
342 map { my $amount = $_->amount / ($_->target_info->{multiplier} || 1);
343 my $label = $_->target_info->{label};
345 { data => "Plus $money_char". $_->price. ' '.
346 ( $_->action eq 'increment' ? 'per' : 'for' ).
347 " $amount $label",
348 align => 'center', #left?
353 $part_pkg->part_pkg_usageprice
356 map { my $dst_pkg = $_->dst_pkg;
358 { data => 'Supplemental: '.
359 '<A HREF="#'. $dst_pkg->pkgpart . '">' .
360 $dst_pkg->pkg . '</A>',
366 $part_pkg->supp_part_pkg_link
368 ( # Billing add-ons/bundle packages
370 my $dst_pkg = $_->dst_pkg;
372 { data => 'Add-on: '.$dst_pkg->pkg_comment,
378 $part_pkg->bill_part_pkg_link
380 ( # Discounts available
383 { data => '<b>Discounts</b>',
393 { data => $_->months. ':',
396 { data => $_->amount ? '$'. $_->amount : $_->percent. '%'
403 ]; # end of "middle column"
405 # $plan_labels{$part_pkg->plan}.'<BR>'.
406 # $money_char.sprintf('%.2f setup<BR>', $part_pkg->option('setup_fee') ).
407 # ( $part_pkg->freq ne '0'
408 # ? $money_char.sprintf('%.2f ', $part_pkg->option('recur_fee') )
411 # $part_pkg->freq_pretty; #.'<BR>'
414 push @header, 'Cost tracking';
417 my $part_pkg = shift;
418 #(my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ / /g;
419 my $is_recur = ( $part_pkg->freq ne '0' );
423 { data => ' ', # $plan,
429 { data =>$money_char.
430 sprintf('%.2f ', $part_pkg->setup_cost ),
433 { data => ( $is_recur ? ' setup' : ' one-time' ),
440 ? $money_char. sprintf('%.2f', $part_pkg->recur_cost)
441 : '(no recurring)' #$part_pkg->freq_pretty
443 align=> ( $is_recur ? 'right' : 'center' ),
444 colspan=> ( $is_recur ? 1 : 2 ),
447 ? { data => ( $is_recur
448 ? ' '. $part_pkg->freq_pretty
460 # Agent goes here if displayed
464 if ( $acl_edit_global ) {
465 #really we just want a count, but this is fine unless someone has tons
466 my @all_agent_types = map {$_->typenum}
467 qsearch('agent_type', { 'disabled'=>'' });
468 if ( scalar(@all_agent_types) > 1 ) {
469 push @header, 'Agent types';
470 my $typelink = $p. 'edit/agent_type.cgi?';
471 push @fields, sub { my $part_pkg = shift;
473 map { my $agent_type = $_->agent_type;
475 { 'data' => $agent_type->atype, #escape?
477 'link' => ( $acl_config
492 #if ( $cgi->param('active') ) {
493 push @header, 'Customer<BR>packages';
494 my %col = %{ FS::cust_pkg->statuscolors };
495 my $cust_pkg_link = $p. 'search/cust_pkg.cgi?pkgpart=';
496 push @fields, sub { my $part_pkg = shift;
501 if ( $magic eq 'active' && $part_pkg->freq == 0 ) {
503 #$label = 'one-time charge';
506 $label= 'not yet billed' if $magic eq 'not_yet_billed';
507 $label= 'on hold' if $magic eq 'on_hold';
511 'data' => '<B><FONT COLOR="#'. $col{$label}. '">'.
512 $part_pkg->get("num_$_").
518 ( $part_pkg->get("num_$_") != 1
519 && $label =~ /charge$/
524 'link' => ( $part_pkg->get("num_$_")
532 } (qw( on_hold not_yet_billed active suspended cancelled ))
536 { 'data' => '<FONT SIZE="-1">[ '.
537 include('/elements/popup_link.html',
539 'action' => "${p}edit/bulk-cust_pkg.html?".
540 'pkgpart='.$part_pkg->pkgpart,
541 'actionlabel' => 'Change Packages',
554 push @header, 'Taxclass';
555 push @fields, sub { shift->taxclass() || ' '; };
559 # make a table of report class optionnames => the actual
560 my %report_optionname_name = map { 'report_option_'.$_->num, $_->name }
561 qsearch('part_pkg_report_option', { disabled => '' });
563 push @header, 'Plan options',
565 #'Service', 'Quan', 'Primary';
569 my $part_pkg = shift;
570 if ( $part_pkg->plan ) {
572 my %options = $part_pkg->options;
573 # gather any options that are really report options,
574 # convert them to their user-friendly names,
575 # and sort them (I think?)
578 map { $report_optionname_name{$_} }
580 and exists($report_optionname_name{$_}) }
589 { 'data' => $part_pkg->format($_,$options{$_}),
594 grep { $options{$_} =~ /\S/ }
595 grep { $_ !~ /^(setup|recur)_fee$/
596 and $_ !~ /^report_option_\d+$/ }
599 if ( @report_options ) {
601 [ { 'data' => 'Report classes',
603 'style' => 'font-weight: bold',
606 foreach (@report_options) {
613 } # foreach @report_options
614 } # if @report_options
618 } else { # should never happen...
625 'data' => $part_pkg->$_(),
638 my $part_pkg = shift;
639 my @part_pkg_usage = sort { $a->priority <=> $b->priority }
640 $part_pkg->part_pkg_usage;
645 my $part_svc = $pkg_svc->part_svc;
646 my $svc = $part_svc->svc;
647 if ( $pkg_svc->primary_svc =~ /^Y/i ) {
648 $svc = "<B>$svc (PRIMARY)</B>";
650 $svc =~ s/ +/ /g;
654 'data' => '<B>'. $pkg_svc->quantity. '</B>',
660 'link' => ( $acl_config
661 ? $p. 'edit/part_svc.cgi?'.
668 sort { $b->primary_svc =~ /^Y/i
669 <=> $a->primary_svc =~ /^Y/i
671 $part_pkg->pkg_svc('disable_linked'=>1)
674 my $dst_pkg = $_->dst_pkg;
676 { data => 'Add-on: '.$dst_pkg->pkg_comment,
682 $part_pkg->svc_part_pkg_link
684 ( scalar(@part_pkg_usage) ?
685 [ { data => 'Usage minutes',
689 link => $p.'browse/part_pkg_usage.html#pkgpart'.
695 [ { data => $_->minutes,
698 { data => $_->description,
708 $align .= 'lrl'; #rr';
712 my $count_extra_sql = $extra_sql;
713 $count_extra_sql =~ s/^\s*AND /WHERE /i;
714 $extra_count = ( $count_extra_sql ? ' AND ' : ' WHERE ' ). $extra_count
716 my $count_query = "SELECT COUNT(*) FROM part_pkg $count_extra_sql $extra_count";
720 if ( $acl_edit_bulk ) {
721 # insert a checkbox column
724 '<INPUT TYPE="checkbox" NAME="pkgpart" VALUE=' . $_[0]->pkgpart .'>';
728 $html_form = qq!<FORM ACTION="${p}edit/bulk-part_pkg.html" METHOD="POST">!;
729 $html_foot = include('/search/elements/checkbox-foot.html',
730 submit => 'edit report classes', # for now it's only report classes
735 # show this if there are any voip_cdr packages defined
736 if ( FS::part_pkg->count("plan = 'voip_cdr'") ) {
737 push @menubar, 'Per-package usage minutes' => $p.'browse/part_pkg_usage.html';