add agent type list to package def browse, RT#4880
[freeside.git] / httemplate / browse / part_pkg.cgi
index 45fa8f2..801c09f 100755 (executable)
@@ -1,61 +1,82 @@
 <% include( 'elements/browse.html',
-                 'title'              => 'Package Definitions',
-                 'html_init'          => $html_init,
-                 'name'               => 'package definitions',
-                 'disableable'        => 1,
-                 'disabled_statuspos' => 3,
-                 'agent_virt'         => 1,
-                 'agent_null_right'   => 'Edit global package definitions',
-                 'agent_pos'          => 5,
-                 'query'              => { 'select'   => $select,
-                                           'table'    => 'part_pkg',
-                                           'hashref'  => {},
-                                           'order_by' => "ORDER BY $orderby",
-                                         },
-                 'count_query'        => $count_query,
-                 'header'             => \@header,
-                 'fields'             => \@fields,
-                 'links'              => \@links,
-                 'align'              => $align,
+                 'title'                 => 'Package Definitions',
+                 'html_init'             => $html_init,
+                 'name'                  => 'package definitions',
+                 'disableable'           => 1,
+                 'disabled_statuspos'    => 3,
+                 'agent_virt'            => 1,
+                 'agent_null_right'      => [ $edit, $edit_global ],
+                 'agent_null_right_link' => $edit_global,
+                 'agent_pos'             => 5,
+                 'query'                 => { 'select'    => $select,
+                                              'table'     => 'part_pkg',
+                                              'hashref'   => {},
+                                              'extra_sql' => $extra_sql,
+                                              'order_by'  => "ORDER BY $orderby"
+                                            },
+                 'count_query'           => $count_query,
+                 'header'                => \@header,
+                 'fields'                => \@fields,
+                 'links'                 => \@links,
+                 'align'                 => $align,
              )
 %>
 <%init>
 
+my $curuser = $FS::CurrentUser::CurrentUser;
+
+my $edit        = 'Edit package definitions';
+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 $FS::CurrentUser::CurrentUser->access_right('Edit package definitions')
-      || $FS::CurrentUser::CurrentUser->access_right('Edit global package definitions');
+  unless $acl_edit || $acl_edit_global;
+
+my $conf = new FS::Conf;
+my $taxclasses = $conf->exists('enable_taxclasses');
+my $money_char = $conf->config('money_char') || '$';
 
 my $select = '*';
 my $orderby = 'pkgpart';
 if ( $cgi->param('active') ) {
-
   $orderby = 'num_active DESC';
 }
-  $select = "
 
-    *,
+my $extra_sql = '';
 
-    ( SELECT COUNT(*) FROM cust_pkg WHERE cust_pkg.pkgpart = part_pkg.pkgpart
-       AND ( cancel IS NULL OR cancel = 0 )
-       AND ( susp IS NULL OR susp = 0 )
-    ) AS num_active,
+unless ( $acl_edit_global ) {
+  $extra_sql .= ' WHERE '.  FS::part_pkg->curuser_pkgs_sql;
+}
 
-    ( SELECT COUNT(*) FROM cust_pkg WHERE cust_pkg.pkgpart = part_pkg.pkgpart
-        AND ( cancel IS NULL OR cancel = 0 )
-        AND susp IS NOT NULL AND susp != 0
-    ) AS num_suspended,
+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
+      AND cust_main.agentnum IN ($agentnums)
+";
 
-    ( SELECT COUNT(*) FROM cust_pkg WHERE cust_pkg.pkgpart = part_pkg.pkgpart
-        AND cancel IS NOT NULL AND cancel != 0
-    ) AS num_cancelled
+$select = "
 
-  ";
+  *,
 
-#}
+  ( $count_cust_pkg
+      AND ( cancel IS NULL OR cancel = 0 )
+      AND ( susp IS NULL OR susp = 0 )
+  ) AS num_active,
 
-my $conf = new FS::Conf;
-my $taxclasses = $conf->exists('enable_taxclasses');
-my $money_char = $conf->config('money_char') || '$';
+  ( $count_cust_pkg
+      AND ( cancel IS NULL OR cancel = 0 )
+      AND susp IS NOT NULL AND susp != 0
+  ) AS num_suspended,
+
+  ( $count_cust_pkg
+      AND cancel IS NOT NULL AND cancel != 0
+  ) AS num_cancelled
+
+";
 
 my $html_init;
 #unless ( $cgi->param('active') ) {
@@ -63,7 +84,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>
   !;
 #}
@@ -149,6 +175,40 @@ 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 { warn $_;
+                                my $agent_type = $_->agent_type;
+                                warn $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 = (
@@ -270,15 +330,18 @@ push @fields,
                                {
                                  'data'  => $svc,
                                  'align' => 'left',
-                                 'link'  => $p. 'edit/part_svc.cgi?'.
-                                            $part_svc->svcpart,
+                                 'link'  => ( $acl_config
+                                                ? $p. 'edit/part_svc.cgi?'.
+                                                  $part_svc->svcpart
+                                                : ''
+                                            ),
                                },
                              ];
                            }
                       sort {     $b->primary_svc =~ /^Y/i
                              <=> $a->primary_svc =~ /^Y/i
                            }
-                           $part_pkg->pkg_svc
+                           $part_pkg->pkg_svc('disable_linked'=>1)
                       ),
                       ( map { 
                               my $dst_pkg = $_->dst_pkg;
@@ -299,9 +362,6 @@ $align .= 'lrl'; #rr';
 
 # --------
 
-my $count_query = 'SELECT COUNT(*) FROM part_pkg WHERE '.
-                    $FS::CurrentUser::CurrentUser->agentnums_sql(
-                      'null_right' => 'Edit global package definitions',
-                    );
+my $count_query = "SELECT COUNT(*) FROM part_pkg $extra_sql";
 
 </%init>