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,
27 my $curuser = $FS::CurrentUser::CurrentUser;
29 my $edit = 'Edit package definitions';
30 my $edit_global = 'Edit global package definitions';
31 my $acl_edit = $curuser->access_right($edit);
32 my $acl_edit_global = $curuser->access_right($edit_global);
33 my $acl_config = $curuser->access_right('Configuration'); #to edit services
37 unless $acl_edit || $acl_edit_global;
39 my $conf = new FS::Conf;
40 my $taxclasses = $conf->exists('enable_taxclasses');
41 my $money_char = $conf->config('money_char') || '$';
44 my $orderby = 'pkgpart';
48 if ( $cgi->param('active') ) {
49 $orderby = 'num_active DESC';
54 #if ( $cgi->param('activeONLY') ) {
55 # push @where, ' WHERE num_active > 0 '; #XXX doesn't affect count...
58 if ( $cgi->param('recurring') ) {
59 $hash{'freq'} = { op=>'!=', value=>'0' };
60 $extra_count = " freq != '0' ";
64 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
66 push @where, $classnum ? "classnum = $classnum"
69 $cgi->delete('classnum');
71 if ( $cgi->param('missing_recur_fee') ) {
72 push @where, "0 = ( SELECT COUNT(*) FROM part_pkg_option
73 WHERE optionname = 'recur_fee'
74 AND part_pkg_option.pkgpart = part_pkg.pkgpart
75 AND CAST ( optionvalue AS NUMERIC ) > 0
79 push @where, FS::part_pkg->curuser_pkgs_sql
80 unless $acl_edit_global;
82 my $extra_sql = scalar(@where)
83 ? ( scalar(keys %hash) ? ' AND ' : ' WHERE ' ).
87 my $agentnums = join(',', $curuser->agentnums);
88 my $count_cust_pkg = "
89 SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
90 WHERE cust_pkg.pkgpart = part_pkg.pkgpart
91 AND cust_main.agentnum IN ($agentnums)
99 AND ( setup IS NULL OR cancel = 0 )
100 AND ( cancel IS NULL OR cancel = 0 )
101 AND ( susp IS NULL OR susp = 0 )
102 ) AS num_not_yet_billed,
105 AND setup IS NOT NULL AND setup != 0
106 AND ( cancel IS NULL OR cancel = 0 )
107 AND ( susp IS NULL OR susp = 0 )
111 AND ( cancel IS NULL OR cancel = 0 )
112 AND susp IS NOT NULL AND susp != 0
116 AND cancel IS NOT NULL AND cancel != 0
122 #unless ( $cgi->param('active') ) {
124 One or more service definitions are grouped together into a package
125 definition and given pricing information. Customers purchase packages
126 rather than purchase services directly.<BR><BR>
127 <FORM METHOD="POST" ACTION="${p}edit/part_pkg.cgi">
128 <A HREF="${p}edit/part_pkg.cgi"><I>Add a new package definition</I></A>
130 !.include('/elements/select-part_pkg.html', 'element_name' => 'clone' ). qq!
131 <INPUT TYPE="submit" VALUE="Clone existing package">
137 $cgi->param('dummy', 1);
140 qq(\n<SCRIPT TYPE="text/javascript">\n).
141 "function filter_change() {".
142 " window.location = '". $cgi->self_url.
143 ";classnum=' + document.getElementById('classnum').options[document.getElementById('classnum').selectedIndex].value".
147 #restore this so pagination works
148 $cgi->param('classnum', $classnum) if length($classnum);
151 "$filter_change\n<BR>( show class: ".
152 include('/elements/select-pkg_class.html',
153 #'curr_value' => $classnum,
154 'value' => $classnum, #insist on 0 :/
155 'onchange' => 'filter_change()',
156 'pre_options' => [ '-1' => 'all',
158 'disable_empty' => 1,
162 my $recur_toggle = $cgi->param('recurring') ? 'show' : 'hide';
163 $cgi->param('recurring', $cgi->param('recurring') ^ 1 );
166 '( <A HREF="'. $cgi->self_url.'">'. "$recur_toggle one-time charges</A> )";
168 $cgi->param('recurring', $cgi->param('recurring') ^ 1 ); #put it back
172 my $link = [ $p.'edit/part_pkg.cgi?', 'pkgpart' ];
174 my @header = ( '#', 'Package', 'Comment', 'Custom' );
175 my @fields = ( 'pkgpart', 'pkg', 'comment',
176 sub{ '<B><FONT COLOR="#0000CC">'.$_[0]->custom.'</FONT></B>' }
179 my @links = ( $link, $link, '', '' );
181 unless ( 0 ) { #already showing only one class or something?
182 push @header, 'Class';
183 push @fields, sub { shift->classname || '(none)'; };
187 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
189 tie my %plan_labels, 'Tie::IxHash',
190 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
193 push @header, 'Pricing';
196 my $part_pkg = shift;
197 (my $plan = $plan_labels{$part_pkg->plan} ) =~ s/ / /g;
198 my $is_recur = ( $part_pkg->freq ne '0' );
208 { data =>$money_char.
209 sprintf('%.2f', $part_pkg->option('setup_fee') ),
212 { data => ( $is_recur ? ' setup' : ' one-time' ),
218 ? $money_char.sprintf('%.2f ', $part_pkg->option('recur_fee') )
219 : $part_pkg->freq_pretty
221 align=> ( $is_recur ? 'right' : 'center' ),
222 colspan=> ( $is_recur ? 1 : 2 ),
225 ? { data => ( $is_recur ? $part_pkg->freq_pretty : '' ),
232 my $dst_pkg = $_->dst_pkg;
234 { data => 'Add-on: '.$dst_pkg->pkg_comment,
240 $part_pkg->bill_part_pkg_link
244 # $plan_labels{$part_pkg->plan}.'<BR>'.
245 # $money_char.sprintf('%.2f setup<BR>', $part_pkg->option('setup_fee') ).
246 # ( $part_pkg->freq ne '0'
247 # ? $money_char.sprintf('%.2f ', $part_pkg->option('recur_fee') )
250 # $part_pkg->freq_pretty; #.'<BR>'
254 # Agent goes here if displayed
258 if ( $acl_edit_global ) {
259 #really we just want a count, but this is fine unless someone has tons
260 my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
261 if ( scalar(@all_agent_types) > 1 ) {
262 push @header, 'Agent types';
263 my $typelink = $p. 'edit/agent_type.cgi?';
264 push @fields, sub { my $part_pkg = shift;
266 map { my $agent_type = $_->agent_type;
268 { 'data' => $agent_type->atype, #escape?
270 'link' => ( $acl_config
285 #if ( $cgi->param('active') ) {
286 push @header, 'Customer<BR>packages';
288 'active' => '00CC00',
289 'suspended' => 'FF9900',
290 'cancelled' => 'FF0000',
291 #'one-time charge' => '000000',
292 'charge' => '000000',
293 #'not yet billed' => '000000',
294 'not yet billed' => '000000',
296 my $cust_pkg_link = $p. 'search/cust_pkg.cgi?pkgpart=';
297 push @fields, sub { my $part_pkg = shift;
302 if ( $magic eq 'active' && $part_pkg->freq == 0 ) {
304 #$label = 'one-time charge',
307 $label =~ s/_/ /g;
312 'data' => '<B><FONT COLOR="#'. $col{$label}. '">'.
313 $part_pkg->get("num_$_").
319 ( $part_pkg->get("num_$_") != 1
320 && $label =~ /charge$/
325 'link' => ( $part_pkg->get("num_$_")
333 } qw(not_yet_billed active suspended cancelled)
339 push @header, 'Taxclass';
340 push @fields, sub { shift->taxclass() || ' '; };
344 push @header, 'Plan options',
346 #'Service', 'Quan', 'Primary';
350 my $part_pkg = shift;
351 if ( $part_pkg->plan ) {
353 my %options = $part_pkg->options;
360 { 'data' => $part_pkg->format($_,$options{$_}),
365 grep { $options{$_} =~ /\S/ }
366 grep { $_ !~ /^(setup|recur)_fee$/ }
377 'data' => $part_pkg->$_(),
390 my $part_pkg = shift;
395 my $part_svc = $pkg_svc->part_svc;
396 my $svc = $part_svc->svc;
397 if ( $pkg_svc->primary_svc =~ /^Y/i ) {
398 $svc = "<B>$svc (PRIMARY)</B>";
400 $svc =~ s/ +/ /g;
404 'data' => '<B>'. $pkg_svc->quantity. '</B>',
410 'link' => ( $acl_config
411 ? $p. 'edit/part_svc.cgi?'.
418 sort { $b->primary_svc =~ /^Y/i
419 <=> $a->primary_svc =~ /^Y/i
421 $part_pkg->pkg_svc('disable_linked'=>1)
424 my $dst_pkg = $_->dst_pkg;
426 { data => 'Add-on: '.$dst_pkg->pkg_comment,
432 $part_pkg->svc_part_pkg_link
438 $align .= 'lrl'; #rr';
442 my $count_extra_sql = $extra_sql;
443 $count_extra_sql =~ s/^\s*AND /WHERE /i;
444 $extra_count = ( $count_extra_sql ? ' AND ' : ' WHERE ' ). $extra_count
446 my $count_query = "SELECT COUNT(*) FROM part_pkg $count_extra_sql $extra_count";