show labels for select(select_hash) config options, RT#3997
[freeside.git] / httemplate / browse / part_pkg.cgi
index cdaa2c9..26028b6 100755 (executable)
@@ -10,7 +10,7 @@
                  'agent_pos'             => 5,
                  'query'                 => { 'select'    => $select,
                                               'table'     => 'part_pkg',
-                                              'hashref'   => {},
+                                              'hashref'   => \%hash,
                                               'extra_sql' => $extra_sql,
                                               'order_by'  => "ORDER BY $orderby"
                                             },
@@ -30,6 +30,7 @@ my $edit_global = 'Edit global package definitions';
 my $acl_edit        = $curuser->access_right($edit);
 my $acl_edit_global = $curuser->access_right($edit_global);
 my $acl_config      = $curuser->access_right('Configuration'); #to edit services
+                                                               #and agent types
 
 die "access denied"
   unless $acl_edit || $acl_edit_global;
@@ -40,31 +41,40 @@ my $money_char = $conf->config('money_char') || '$';
 
 my $select = '*';
 my $orderby = 'pkgpart';
+my %hash = ();
+my $extra_count = '';
+
 if ( $cgi->param('active') ) {
   $orderby = 'num_active DESC';
 }
 
 my $extra_sql = '';
 
-#false laziness w/elements/select-part_pkg.html
-my $agentnums = join(',', $curuser->agentnums);
+#if ( $cgi->param('activeONLY') ) {
+#  $extra_sql = ' WHERE num_active > 0 '; #XXX doesn't affect count...
+#}
+
+if ( $cgi->param('recurring') ) {
+  $hash{'freq'} = { op=>'!=', value=>'0' };
+  $extra_count = ' freq != 0 ';
+}
+
+if ( $cgi->param('missing_recur_fee') ) {
+  my $missing = "0 = ( SELECT COUNT(*) FROM part_pkg_option
+                         WHERE optionname = 'recur_fee'
+                           AND part_pkg_option.pkgpart = part_pkg.pkgpart
+                           AND CAST ( optionvalue AS NUMERIC ) > 0
+                     )";
+  $extra_sql .= ( ( scalar(keys %hash) || $extra_sql ) ? ' AND ' : ' WHERE ' ).
+                $missing;
+}
 
 unless ( $acl_edit_global ) {
-  $extra_sql .= "
-    WHERE (
-      agentnum IS NOT NULL OR 0 < (
-        SELECT COUNT(*)
-          FROM type_pkgs
-            LEFT JOIN agent_type USING ( typenum )
-            LEFT JOIN agent AS typeagent USING ( typenum )
-          WHERE type_pkgs.pkgpart = part_pkg.pkgpart
-            AND typeagent.agentnum IN ($agentnums)
-      )
-    )
-  ";
+  $extra_sql .= ( ( scalar(keys %hash) || $extra_sql ) ? ' AND ' : ' WHERE ' ).
+                 FS::part_pkg->curuser_pkgs_sql;
 }
-#eofalse
 
+my $agentnums = join(',', $curuser->agentnums);
 my $count_cust_pkg = "
   SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
     WHERE cust_pkg.pkgpart = part_pkg.pkgpart
@@ -97,7 +107,12 @@ my $html_init;
     One or more service definitions are grouped together into a package 
     definition and given pricing information.  Customers purchase packages
     rather than purchase services directly.<BR><BR>
+    <FORM METHOD="POST" ACTION="${p}edit/part_pkg.cgi">
     <A HREF="${p}edit/part_pkg.cgi"><I>Add a new package definition</I></A>
+    or
+    !.include('/elements/select-part_pkg.html', 'element_name' => 'clone' ). qq!
+    <INPUT TYPE="submit" VALUE="Clone existing package">
+    </FORM>
     <BR><BR>
   !;
 #}
@@ -183,6 +198,38 @@ push @fields, sub {
 #    $part_pkg->freq_pretty; #.'<BR>'
 };
 
+###
+# Agent goes here if displayed
+###
+
+#agent type
+if ( $acl_edit_global ) {
+  #really we just want a count, but this is fine unless someone has tons
+  my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
+  if ( scalar(@all_agent_types) > 1 ) {
+    push @header, 'Agent types';
+    my $typelink = $p. 'edit/agent_type.cgi?';
+    push @fields, sub { my $part_pkg = shift;
+                        [
+                          map { my $agent_type = $_->agent_type;
+                                [ 
+                                  { 'data'  => $agent_type->atype, #escape?
+                                    'align' => 'left',
+                                    'link'  => ( $acl_config
+                                                   ? $typelink.
+                                                     $agent_type->typenum
+                                                   : ''
+                                               ),
+                                  },
+                                ];
+                              }
+                              $part_pkg->type_pkgs
+                        ];
+                      };
+    $align .= 'l';
+  }
+}
+
 #if ( $cgi->param('active') ) {
   push @header, 'Customer<BR>packages';
   my %col = (
@@ -336,6 +383,10 @@ $align .= 'lrl'; #rr';
 
 # --------
 
-my $count_query = "SELECT COUNT(*) FROM part_pkg $extra_sql";
+my $count_extra_sql = $extra_sql;
+$count_extra_sql =~ s/^\s*AND /WHERE /i;
+$extra_count = ( $count_extra_sql ? ' AND ' : ' WHERE ' ). $extra_count
+  if $extra_count;
+my $count_query = "SELECT COUNT(*) FROM part_pkg $count_extra_sql $extra_count";
 
 </%init>