1 <% include( 'elements/browse.html',
2 'title' => 'Package Definitions',
3 'html_init' => $html_init,
4 'html_form' => $html_form,
5 'html_posttotal' => $html_posttotal,
6 'name' => 'package definitions',
8 'disabled_statuspos' => 4,
10 'agent_null_right' => [ $edit, $edit_global ],
11 'agent_null_right_link' => $edit_global,
13 'query' => { 'select' => $select,
14 'table' => 'part_pkg',
16 'extra_sql' => $extra_sql,
17 'order_by' => "ORDER BY $orderby"
19 'count_query' => $count_query,
24 'html_init' => $html_init,
25 'html_foot' => $html_foot,
30 my $curuser = $FS::CurrentUser::CurrentUser;
32 my $edit = 'Edit package definitions';
33 my $edit_global = 'Edit global package definitions';
34 my $acl_edit = $curuser->access_right($edit);
35 my $acl_edit_global = $curuser->access_right($edit_global);
36 my $acl_config = $curuser->access_right('Configuration'); #to edit services
39 my $acl_edit_bulk = $curuser->access_right('Bulk edit package definitions');
42 unless $acl_edit || $acl_edit_global;
44 my $conf = new FS::Conf;
45 my $taxclasses = $conf->exists('enable_taxclasses');
46 my $money_char = $conf->config('money_char') || '$';
49 my $orderby = 'pkgpart';
53 if ( $cgi->param('active') ) {
54 $orderby = 'num_active DESC';
59 #if ( $cgi->param('activeONLY') ) {
60 # push @where, ' WHERE num_active > 0 '; #XXX doesn't affect count...
63 if ( $cgi->param('recurring') ) {
64 $hash{'freq'} = { op=>'!=', value=>'0' };
65 $extra_count = " freq != '0' ";
69 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
71 push @where, $classnum ? "classnum = $classnum"
74 $cgi->delete('classnum');
76 if ( $cgi->param('missing_recur_fee') ) {
77 push @where, "0 = ( SELECT COUNT(*) FROM part_pkg_option
78 WHERE optionname = 'recur_fee'
79 AND part_pkg_option.pkgpart = part_pkg.pkgpart
80 AND CAST( optionvalue AS NUMERIC ) > 0
84 push @where, FS::part_pkg->curuser_pkgs_sql
85 unless $acl_edit_global;
87 my $extra_sql = scalar(@where)
88 ? ( scalar(keys %hash) ? ' AND ' : ' WHERE ' ).
92 my $agentnums_sql = $curuser->agentnums_sql( 'table'=>'cust_main' );
93 my $count_cust_pkg = "
94 SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
95 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
104 AND ( setup IS NULL OR setup = 0 )
105 AND ( cancel IS NULL OR cancel = 0 )
106 AND ( susp IS NULL OR susp = 0 )
107 ) AS num_not_yet_billed,
110 AND setup IS NOT NULL AND setup != 0
111 AND ( cancel IS NULL OR cancel = 0 )
112 AND ( susp IS NULL OR susp = 0 )
116 AND ( cancel IS NULL OR cancel = 0 )
117 AND susp IS NOT NULL AND susp != 0
121 AND cancel IS NOT NULL AND cancel != 0
127 One or more service definitions are grouped together into a package
128 definition and given pricing information. Customers purchase packages
129 rather than purchase services directly.<BR><BR>
130 <FORM METHOD="POST" ACTION="${p}edit/part_pkg.cgi">
131 <A HREF="${p}edit/part_pkg.cgi"><I>Add a new package definition</I></A>
133 !.include('/elements/select-part_pkg.html', 'element_name' => 'clone' ). qq!
134 <INPUT TYPE="submit" VALUE="Clone existing package">
139 $cgi->param('dummy', 1);
142 qq(\n<SCRIPT TYPE="text/javascript">\n).
143 "function filter_change() {".
144 " window.location = '". $cgi->self_url.
145 ";classnum=' + document.getElementById('classnum').options[document.getElementById('classnum').selectedIndex].value".
149 #restore this so pagination works
150 $cgi->param('classnum', $classnum) if length($classnum);
152 #should hide this if there aren't any classes
154 "$filter_change\n<BR>( show class: ".
155 include('/elements/select-pkg_class.html',
156 #'curr_value' => $classnum,
157 'value' => $classnum, #insist on 0 :/
158 'onchange' => 'filter_change()',
159 'pre_options' => [ '-1' => 'all',
161 'disable_empty' => 1,
165 my $recur_toggle = $cgi->param('recurring') ? 'show' : 'hide';
166 $cgi->param('recurring', $cgi->param('recurring') ^ 1 );
169 '( <A HREF="'. $cgi->self_url.'">'. "$recur_toggle one-time charges</A> )";
171 $cgi->param('recurring', $cgi->param('recurring') ^ 1 ); #put it back
175 my $link = [ $p.'edit/part_pkg.cgi?', 'pkgpart' ];
177 my @header = ( '#', 'Package', 'Comment', 'Custom' );
178 my @fields = ( 'pkgpart', 'pkg', 'comment',
179 sub{ '<B><FONT COLOR="#0000CC">'.$_[0]->custom.'</FONT></B>' }
182 my @links = ( $link, $link, '', '' );
184 unless ( 0 ) { #already showing only one class or something?
185 push @header, 'Class';
186 push @fields, sub { shift->classname || '(none)'; };
190 if ( $conf->exists('pkg-addon_classnum') ) {
191 push @header, "Add'l order class";
192 push @fields, sub { shift->addon_classname || '(none)'; };
196 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
198 tie my %plan_labels, 'Tie::IxHash',
199 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
202 push @header, 'Pricing';
205 my $part_pkg = shift;
206 (my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ / /g;
207 my $is_recur = ( $part_pkg->freq ne '0' );
208 my @discounts = sort { $a->months <=> $b->months }
210 $part_pkg->part_pkg_discount;
220 { data =>$money_char.
221 sprintf('%.2f', $part_pkg->option('setup_fee') ),
224 { data => ( ( $is_recur ? ' setup' : ' one-time' ).
225 ( $part_pkg->option('recur_fee') == 0
226 && $part_pkg->setup_show_zero
227 ? ' (printed on invoices)'
237 ? $money_char. sprintf('%.2f ', $part_pkg->option('recur_fee'))
238 : $part_pkg->freq_pretty
240 align=> ( $is_recur ? 'right' : 'center' ),
241 colspan=> ( $is_recur ? 1 : 2 ),
244 ? { data => ( $is_recur
245 ? $part_pkg->freq_pretty.
246 ( $part_pkg->option('recur_fee') == 0
247 && $part_pkg->recur_show_zero
248 ? ' (printed on invoices)'
258 my $dst_pkg = $_->dst_pkg;
260 { data => 'Add-on: '.$dst_pkg->pkg_comment,
266 $part_pkg->bill_part_pkg_link
270 { data => '<b>Discounts</b>',
280 { data => $_->months. ':',
283 { data => $_->amount ? '$'. $_->amount : $_->percent. '%'
292 # $plan_labels{$part_pkg->plan}.'<BR>'.
293 # $money_char.sprintf('%.2f setup<BR>', $part_pkg->option('setup_fee') ).
294 # ( $part_pkg->freq ne '0'
295 # ? $money_char.sprintf('%.2f ', $part_pkg->option('recur_fee') )
298 # $part_pkg->freq_pretty; #.'<BR>'
302 # Agent goes here if displayed
306 if ( $acl_edit_global ) {
307 #really we just want a count, but this is fine unless someone has tons
308 my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
309 if ( scalar(@all_agent_types) > 1 ) {
310 push @header, 'Agent types';
311 my $typelink = $p. 'edit/agent_type.cgi?';
312 push @fields, sub { my $part_pkg = shift;
314 map { my $agent_type = $_->agent_type;
316 { 'data' => $agent_type->atype, #escape?
318 'link' => ( $acl_config
333 #if ( $cgi->param('active') ) {
334 push @header, 'Customer<BR>packages';
336 'not yet billed' => '009999', #teal? cyan?
337 'active' => '00CC00',
338 'suspended' => 'FF9900',
339 'cancelled' => 'FF0000',
340 #'one-time charge' => '000000',
341 'charge' => '000000',
343 my $cust_pkg_link = $p. 'search/cust_pkg.cgi?pkgpart=';
344 push @fields, sub { my $part_pkg = shift;
349 if ( $magic eq 'active' && $part_pkg->freq == 0 ) {
351 #$label = 'one-time charge',
354 $label= 'not yet billed' if $magic eq 'not_yet_billed';
358 'data' => '<B><FONT COLOR="#'. $col{$label}. '">'.
359 $part_pkg->get("num_$_").
365 ( $part_pkg->get("num_$_") != 1
366 && $label =~ /charge$/
371 'link' => ( $part_pkg->get("num_$_")
379 } (qw( not_yet_billed active suspended cancelled ))
383 { 'data' => '<FONT SIZE="-1">[ '.
384 include('/elements/popup_link.html',
386 'action' => "${p}edit/bulk-cust_pkg.html?".
387 'pkgpart='.$part_pkg->pkgpart,
388 'actionlabel' => 'Change Packages',
401 push @header, 'Taxclass';
402 push @fields, sub { shift->taxclass() || ' '; };
406 # make a table of report class optionnames => the actual
407 my %report_optionname_name = map { 'report_option_'.$_->num, $_->name }
408 qsearch('part_pkg_report_option', { disabled => '' });
410 push @header, 'Plan options',
412 #'Service', 'Quan', 'Primary';
416 my $part_pkg = shift;
417 if ( $part_pkg->plan ) {
419 my %options = $part_pkg->options;
420 # gather any options that are really report options,
421 # convert them to their user-friendly names,
422 # and sort them (I think?)
425 map { $report_optionname_name{$_} }
427 and exists($report_optionname_name{$_}) }
436 { 'data' => $part_pkg->format($_,$options{$_}),
441 grep { $options{$_} =~ /\S/ }
442 grep { $_ !~ /^(setup|recur)_fee$/
443 and $_ !~ /^report_option_\d+$/ }
446 if ( @report_options ) {
448 [ { 'data' => 'Report classes',
450 'style' => 'font-weight: bold',
453 foreach (@report_options) {
460 } # foreach @report_options
461 } # if @report_options
465 } else { # should never happen...
472 'data' => $part_pkg->$_(),
485 my $part_pkg = shift;
490 my $part_svc = $pkg_svc->part_svc;
491 my $svc = $part_svc->svc;
492 if ( $pkg_svc->primary_svc =~ /^Y/i ) {
493 $svc = "<B>$svc (PRIMARY)</B>";
495 $svc =~ s/ +/ /g;
499 'data' => '<B>'. $pkg_svc->quantity. '</B>',
505 'link' => ( $acl_config
506 ? $p. 'edit/part_svc.cgi?'.
513 sort { $b->primary_svc =~ /^Y/i
514 <=> $a->primary_svc =~ /^Y/i
516 $part_pkg->pkg_svc('disable_linked'=>1)
519 my $dst_pkg = $_->dst_pkg;
521 { data => 'Add-on: '.$dst_pkg->pkg_comment,
527 $part_pkg->svc_part_pkg_link
533 $align .= 'lrl'; #rr';
537 my $count_extra_sql = $extra_sql;
538 $count_extra_sql =~ s/^\s*AND /WHERE /i;
539 $extra_count = ( $count_extra_sql ? ' AND ' : ' WHERE ' ). $extra_count
541 my $count_query = "SELECT COUNT(*) FROM part_pkg $count_extra_sql $extra_count";
545 if ( $acl_edit_bulk ) {
546 # insert a checkbox column
549 '<INPUT TYPE="checkbox" NAME="pkgpart" VALUE=' . $_[0]->pkgpart .'>';
553 $html_form = qq!<FORM ACTION="${p}edit/bulk-part_pkg.html" METHOD="POST">!;
554 $html_foot = include('/search/elements/checkbox-foot.html',
555 submit => 'edit report classes', # for now it's only report classes