add per-customer "Invoice reports" link, RT#13802
[freeside.git] / httemplate / search / inventory_item.html
index bd74f56..086c8e9 100644 (file)
@@ -1,8 +1,107 @@
-<%
+<% include( 'elements/search.html',
+                 'title'       => $title,
+
+                 'menubar'     => [ 'View inventory classes' =>
+                                      $p.'browse/inventory_class.html',
+                                    'Upload '. PL($inventory_class->classname)=>
+                                      $p.'misc/inventory_item-import.html?'.
+                                      "classnum=$classnum"
+                                  ],
+
+                 'name'        => PL($inventory_class->classname),
+
+                 'query'       => {
+                                    'table'   => 'inventory_item',
+                                    'hashref' => {},
+                                    'select'  => join(', ',
+                                        'inventory_item.*',
+                                        'part_svc.svcdb',
+                                        'cust_main.custnum',
+                                        FS::UI::Web::cust_sql_fields(),
+                                      ),
+                                    'extra_sql' => $extra_sql,
+                                    'addl_from' => $addl_from,
+                                  },
+
+                 'count_query' => $count_query,
+
+                 'agent_virt' => 1,
+                 'agent_null' => 1,
+                 'agent_pos'  => 2,
+
+                 'header'      => [
+                   '#',
+                   $inventory_class->classname,
+                   'Service',
+                   FS::UI::Web::cust_header(),
+                   '', # checkbox column
+                 ],
+
+                 'fields'      => [
+                   'itemnum',
+                   'item',
+                   #'svcnum', #XXX proper full service customer link ala svc_acct
+                             # "unallocated" ?  "available" ?
+                   sub {
+                     #this could be way more efficient with a mixin
+                     # like cust_main_Mixin that let us all all the methods
+                     # on data we already have...
+                     my $inventory_item = shift;
+                     my $cust_svc = $inventory_item->cust_svc;
+                     if ( $cust_svc ) {
+                       my($label, $value) = $cust_svc->label;
+                       "$label: $value";
+                     } else {
+                       '(available)';
+                     }
+                   },
+
+                   \&FS::UI::Web::cust_fields,
+                   $sub_checkbox,
+
+                 ],
+                 'align'       => 'rll'.FS::UI::Web::cust_aligns(),
+                 'links'       => [
+                   '',
+                   '',
+                   $link,
+                   ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
+                         FS::UI::Web::cust_header()
+                   ),
+                 ],
+                 'color' => [ 
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_colors(),
+                            ],
+                 'style' => [ 
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_styles(),
+                            ],
+                  'html_form' => 
+                    qq!
+<FORM NAME="itemForm" ACTION="$p/misc/inventory_item-move.cgi" METHOD="POST">
+<INPUT TYPE="hidden" NAME="classnum" VALUE="$classnum">
+<INPUT TYPE="hidden" NAME="avail"    VALUE="! .$cgi->param('avail') . '">', #'
+                  'html_foot' => $sub_foot,
+             )
+%>
+<%init>
+
+my $curuser = $FS::CurrentUser::CurrentUser;
+
+die "access denied"
+  unless $curuser->access_right('Edit inventory')
+      || $curuser->access_right('Edit global inventory')
+      || $curuser->access_right('Configuration');
 
 my $classnum = $cgi->param('classnum');
-$classnum =~ /^(\d+)$/ or eidiot "illegal classnum $classnum";
+$classnum =~ /^(\d+)$/ or errorpage("illegal classnum $classnum");
 $classnum = $1;
+my $extra_sql = "WHERE inventory_item.classnum = $classnum ";
 
 my $inventory_class = qsearchs( {
   'table'     => 'inventory_class',
@@ -11,42 +110,89 @@ my $inventory_class = qsearchs( {
 
 my $title = $inventory_class->classname. ' Inventory';
 
+if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
+  $extra_sql .= " AND inventory_item.agentnum = $1 ";
+  my $agent = qsearchs('agent', { 'agentnum' => $1 }) or die "unknown agentnum";
+  $title = $agent->agent. " $title";
+}
+
 #little false laziness with SQL fragments in inventory_class.pm
-my $extra_sql = '';
 if ( $cgi->param('avail') ) {
-  $extra_sql = 'AND ( svcnum IS NULL OR svcnum = 0 )';
+  $extra_sql .= ' AND ( svcnum IS NULL OR svcnum = 0 )';
   $title .= ' - Available';
 } elsif ( $cgi->param('used') ) {
-  $extra_sql = 'AND svcnum IS NOT NULL AND svcnum > 0';
+  $extra_sql .= ' AND svcnum IS NOT NULL AND svcnum > 0';
   $title .= ' - In use';
 }
 
 my $count_query =
-  "SELECT COUNT(*) FROM inventory_item WHERE classnum = $classnum $extra_sql";
+  "SELECT COUNT(*) FROM inventory_item $extra_sql";
 
-%><%= include( 'elements/search.html',
-                 'title'       => $title,
+my $link = sub {
+  my $inventory_item = shift;
+  if ( $inventory_item->svcnum ) {
 
-                 #less lame to use Lingua:: something to pluralize
-                 'name'        => $inventory_class->classname. 's',
+    #[ "${p}view/svc_acct.cgi?", 'svcnum' ];
+    my $url = svc_url(
+      'm'      => $m,
+      'action' => 'view',
+      #'svcdb'  => $inventory_item->cust_svc->part_svc->svcdb,
+      'svcdb'  => $inventory_item->svcdb, #we have it from the joined search
+      'query'  => '',
+    );
+    [ $url, 'svcnum' ];
+  } else {
+    '';
+  }
+};
+my $link_cust = sub {
+  my $inventory_item = shift;
+  if ( $inventory_item->custnum ) {
+    [ "${p}view/cust_main.cgi?", 'custnum' ];
+  } else {
+    '';
+  }
+};
 
-                 'query'       => {
-                                    'table'   => 'inventory_item',
-                                    'hashref' => { 'classnum' => $classnum },
-                                    'extra_sql' => $extra_sql,
-                                  },
+my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
+                ' LEFT JOIN part_svc  USING ( svcpart ) '.
+                ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
+                ' LEFT JOIN cust_main USING ( custnum ) ';
+my $areboxes = 0;
 
-                 'count_query' => $count_query,
+my $sub_checkbox = sub {
+  my $item = $_[0];
+  my $itemnum = $item->itemnum;
+  #return '' if $item->svcnum;
+  $areboxes = 1;
+  return qq!<INPUT NAME="itemnum$itemnum" TYPE="checkbox" VALUE="1">!;
+};
 
-                 # XXX proper full service/customer link ala svc_acct
-                 'header'      => [ '#', $inventory_class->classname, 'svcnum' ],
+my $sub_foot = sub {
+  return if !$areboxes;
+  my $foot =
+'<BR><INPUT TYPE="button" VALUE="Select all" onClick="setAll(true)">
+<INPUT TYPE="button" VALUE="Unselect all" onClick="setAll(false)">
+<BR><INPUT TYPE="submit" NAME="action" VALUE="Move to agent">
+<SELECT NAME="move_agentnum">';
+  foreach my $agent ($curuser->agents) {
+    $foot .= '<OPTION VALUE="'.$agent->agentnum.'">'.
+             $agent->agent . '</OPTION>
+             ';
+  }
+  $foot .= '</SELECT>
+<SCRIPT TYPE="text/javascript">
+ function setAll(setTo) {
+   theForm = document.itemForm;
+   for (i=0,n=theForm.elements.length;i<n;i++)
+     if (theForm.elements[i].name.indexOf("itemnum") != -1)
+       theForm.elements[i].checked = setTo;
+ }
+</SCRIPT>';
+  $foot;
+};
+    
 
-                 'fields'      => [
-                   'itemnum',
-                   'item',
-                   'svcnum', #XXX proper full service customer link ala svc_acct
-                             # "unallocated" ?  "available" ?
-                 ],
+  
 
-             )
-%>
+</%init>