1 <% include( 'elements/browse.html',
2 'title' => 'Package Definitions',
3 'html_init' => $html_init,
4 'html_posttotal' => $html_posttotal,
5 'name' => 'package definitions',
7 'disabled_statuspos' => 4,
9 'agent_null_right' => [ $edit, $edit_global ],
10 'agent_null_right_link' => $edit_global,
12 'query' => { 'select' => $select,
13 'table' => 'part_pkg',
15 'extra_sql' => $extra_sql,
16 'order_by' => "ORDER BY $orderby"
18 'count_query' => $count_query,
23 'link_field' => 'pkgpart',
28 my $curuser = $FS::CurrentUser::CurrentUser;
30 my $edit = 'Edit package definitions';
31 my $edit_global = 'Edit global package definitions';
32 my $acl_edit = $curuser->access_right($edit);
33 my $acl_edit_global = $curuser->access_right($edit_global);
34 my $acl_config = $curuser->access_right('Configuration'); #to edit services
39 unless $acl_edit || $acl_edit_global;
41 my $conf = new FS::Conf;
42 my $taxclasses = $conf->exists('enable_taxclasses');
43 my $money_char = $conf->config('money_char') || '$';
46 my $orderby = 'pkgpart';
51 if ( $cgi->param('active') ) {
52 $orderby = 'num_active DESC';
57 #if ( $cgi->param('activeONLY') ) {
58 # push @where, ' WHERE num_active > 0 '; #XXX doesn't affect count...
61 if ( $cgi->param('recurring') ) {
62 $hash{'freq'} = { op=>'!=', value=>'0' };
63 $extra_count = " freq != '0' ";
67 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
69 push @where, $classnum ? "classnum = $classnum"
72 $cgi->delete('classnum');
74 if ( $cgi->param('missing_recur_fee') ) {
75 push @where, "0 = ( SELECT COUNT(*) FROM part_pkg_option
76 WHERE optionname = 'recur_fee'
77 AND part_pkg_option.pkgpart = part_pkg.pkgpart
78 AND CAST( optionvalue AS NUMERIC ) > 0
82 if ( $cgi->param('family') =~ /^(\d+)$/ ) {
84 push @where, "family_pkgpart = $1";
85 # Hiding disabled or one-time charges and limiting by classnum aren't
86 # very useful in this mode, so all links should still refer back to the
87 # non-family-limited display.
88 $cgi->param('showdisabled', 1);
89 $cgi->delete('family');
92 push @where, FS::part_pkg->curuser_pkgs_sql
93 unless $acl_edit_global;
95 my $extra_sql = scalar(@where)
96 ? ( scalar(keys %hash) ? ' AND ' : ' WHERE ' ).
100 my $agentnums_sql = $curuser->agentnums_sql( 'table'=>'cust_main' );
101 my $count_cust_pkg = "
102 SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
103 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
112 AND ( setup IS NULL OR setup = 0 )
113 AND ( cancel IS NULL OR cancel = 0 )
114 AND ( susp IS NULL OR susp = 0 )
115 ) AS num_not_yet_billed,
118 AND setup IS NOT NULL AND setup != 0
119 AND ( cancel IS NULL OR cancel = 0 )
120 AND ( susp IS NULL OR susp = 0 )
124 AND ( cancel IS NULL OR cancel = 0 )
125 AND susp IS NOT NULL AND susp != 0
129 AND cancel IS NOT NULL AND cancel != 0
135 #unless ( $cgi->param('active') ) {
137 One or more service definitions are grouped together into a package
138 definition and given pricing information. Customers purchase packages
139 rather than purchase services directly.<BR><BR>
140 <FORM METHOD="GET" ACTION="${p}edit/part_pkg.cgi">
141 <A HREF="${p}edit/part_pkg.cgi"><I>Add a new package definition</I></A>
143 !.include('/elements/select-part_pkg.html', 'element_name' => 'clone' ). qq!
144 <INPUT TYPE="submit" VALUE="Clone existing package">
150 $cgi->param('dummy', 1);
153 qq(\n<SCRIPT TYPE="text/javascript">\n).
154 "function filter_change() {".
155 " window.location = '". $cgi->self_url.
156 ";classnum=' + document.getElementById('classnum').options[document.getElementById('classnum').selectedIndex].value".
160 #restore this so pagination works
161 $cgi->param('classnum', $classnum) if length($classnum);
163 #should hide this if there aren't any classes
165 "$filter_change\n<BR>( show class: ".
166 include('/elements/select-pkg_class.html',
167 #'curr_value' => $classnum,
168 'value' => $classnum, #insist on 0 :/
169 'onchange' => 'filter_change()',
170 'pre_options' => [ '-1' => 'all',
172 'disable_empty' => 1,
176 my $recur_toggle = $cgi->param('recurring') ? 'show' : 'hide';
177 $cgi->param('recurring', $cgi->param('recurring') ^ 1 );
180 '( <A HREF="'. $cgi->self_url.'">'. "$recur_toggle one-time charges</A> )";
182 $cgi->param('recurring', $cgi->param('recurring') ^ 1 ); #put it back
186 my $link = [ $p.'edit/part_pkg.cgi?', 'pkgpart' ];
188 my @header = ( '#', 'Package', 'Comment', 'Custom' );
189 my @fields = ( 'pkgpart', 'pkg', 'comment',
190 sub{ '<B><FONT COLOR="#0000CC">'.$_[0]->custom.'</FONT></B>' }
193 my @links = ( $link, $link, '', '' );
195 unless ( 0 ) { #already showing only one class or something?
196 push @header, 'Class';
197 push @fields, sub { shift->classname || '(none)'; };
201 if ( $conf->exists('pkg-addon_classnum') ) {
202 push @header, "Add'l order class";
203 push @fields, sub { shift->addon_classname || '(none)'; };
207 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
209 tie my %plan_labels, 'Tie::IxHash',
210 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
213 push @header, 'Pricing';
216 my $part_pkg = shift;
217 (my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ / /g;
218 my $is_recur = ( $part_pkg->freq ne '0' );
219 my @discounts = sort { $a->months <=> $b->months }
221 $part_pkg->part_pkg_discount;
224 ( !$family_pkgpart &&
225 $part_pkg->pkgpart == $part_pkg->family_pkgpart ? () : [
230 'data' => '<b>Show all versions</b>',
231 'link' => $p.'browse/part_pkg.cgi?family='.$part_pkg->family_pkgpart,
241 { data =>$money_char.
242 sprintf('%.2f', $part_pkg->option('setup_fee') ),
245 { data => ( ( $is_recur ? ' setup' : ' one-time' ).
246 ( $part_pkg->option('recur_fee') == 0
247 && $part_pkg->setup_show_zero
248 ? ' (printed on invoices)'
258 ? $money_char. sprintf('%.2f ', $part_pkg->option('recur_fee'))
259 : $part_pkg->freq_pretty
261 align=> ( $is_recur ? 'right' : 'center' ),
262 colspan=> ( $is_recur ? 1 : 2 ),
265 ? { data => ( $is_recur
266 ? $part_pkg->freq_pretty.
267 ( $part_pkg->option('recur_fee') == 0
268 && $part_pkg->recur_show_zero
269 ? ' (printed on invoices)'
278 ( map { my $dst_pkg = $_->dst_pkg;
280 { data => 'Supplemental: '.
281 '<A HREF="#'. $dst_pkg->pkgpart . '">' .
282 $dst_pkg->pkg . '</A>',
288 $part_pkg->supp_part_pkg_link
291 my $dst_pkg = $_->dst_pkg;
293 { data => 'Add-on: '.$dst_pkg->pkg_comment,
299 $part_pkg->bill_part_pkg_link
303 { data => '<b>Discounts</b>',
313 { data => $_->months. ':',
316 { data => $_->amount ? '$'. $_->amount : $_->percent. '%'
325 # $plan_labels{$part_pkg->plan}.'<BR>'.
326 # $money_char.sprintf('%.2f setup<BR>', $part_pkg->option('setup_fee') ).
327 # ( $part_pkg->freq ne '0'
328 # ? $money_char.sprintf('%.2f ', $part_pkg->option('recur_fee') )
331 # $part_pkg->freq_pretty; #.'<BR>'
335 # Agent goes here if displayed
339 if ( $acl_edit_global ) {
340 #really we just want a count, but this is fine unless someone has tons
341 my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
342 if ( scalar(@all_agent_types) > 1 ) {
343 push @header, 'Agent types';
344 my $typelink = $p. 'edit/agent_type.cgi?';
345 push @fields, sub { my $part_pkg = shift;
347 map { my $agent_type = $_->agent_type;
349 { 'data' => $agent_type->atype, #escape?
351 'link' => ( $acl_config
366 #if ( $cgi->param('active') ) {
367 push @header, 'Customer<BR>packages';
369 'not yet billed' => '009999', #teal? cyan?
370 'active' => '00CC00',
371 'suspended' => 'FF9900',
372 'cancelled' => 'FF0000',
373 #'one-time charge' => '000000',
374 'charge' => '000000',
376 my $cust_pkg_link = $p. 'search/cust_pkg.cgi?pkgpart=';
377 push @fields, sub { my $part_pkg = shift;
382 if ( $magic eq 'active' && $part_pkg->freq == 0 ) {
384 #$label = 'one-time charge',
387 $label= 'not yet billed' if $magic eq 'not_yet_billed';
391 'data' => '<B><FONT COLOR="#'. $col{$label}. '">'.
392 $part_pkg->get("num_$_").
398 ( $part_pkg->get("num_$_") != 1
399 && $label =~ /charge$/
404 'link' => ( $part_pkg->get("num_$_")
412 } (qw( not_yet_billed active suspended cancelled ))
416 { 'data' => '<FONT SIZE="-1">[ '.
417 include('/elements/popup_link.html',
419 'action' => "${p}edit/bulk-cust_pkg.html?".
420 'pkgpart='.$part_pkg->pkgpart,
421 'actionlabel' => 'Change Packages',
434 push @header, 'Taxclass';
435 push @fields, sub { shift->taxclass() || ' '; };
439 push @header, 'Plan options',
441 #'Service', 'Quan', 'Primary';
445 my $part_pkg = shift;
446 if ( $part_pkg->plan ) {
448 my %options = $part_pkg->options;
455 { 'data' => $part_pkg->format($_,$options{$_}),
460 grep { $options{$_} =~ /\S/ }
461 grep { $_ !~ /^(setup|recur)_fee$/ }
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";