reporting on agent inventory, RT#7010
[freeside.git] / httemplate / search / inventory_item.html
1 <% include( 'elements/search.html',
2                  'title'       => $title,
3
4                  'menubar'     => [ 'View inventory classes' =>
5                                       $p.'browse/inventory_class.html',
6                                     'Upload '. PL($inventory_class->classname)=>
7                                       $p.'misc/inventory_item-import.html?'.
8                                       "classnum=$classnum"
9                                   ],
10
11                  'name'        => PL($inventory_class->classname),
12
13                  'query'       => {
14                                     'table'   => 'inventory_item',
15                                     'hashref' => {},
16                                     'select'  => join(', ',
17                                         'inventory_item.*',
18                                         'part_svc.svcdb',
19                                         'cust_main.custnum',
20                                         FS::UI::Web::cust_sql_fields(),
21                                       ),
22                                     'extra_sql' => $extra_sql,
23                                     'addl_from' => $addl_from,
24                                   },
25
26                  'count_query' => $count_query,
27
28                  'agent_virt' => 1,
29                  'agent_null' => 1,
30                  'agent_pos'  => 2,
31
32                  'header'      => [
33                    '#',
34                    $inventory_class->classname,
35                    'Service',
36                    FS::UI::Web::cust_header(),
37                  ],
38
39                  'fields'      => [
40                    'itemnum',
41                    'item',
42                    #'svcnum', #XXX proper full service customer link ala svc_acct
43                              # "unallocated" ?  "available" ?
44                    sub {
45                      #this could be way more efficient with a mixin
46                      # like cust_main_Mixin that let us all all the methods
47                      # on data we already have...
48                      my $inventory_item = shift;
49                      my $cust_svc = $inventory_item->cust_svc;
50                      if ( $cust_svc ) {
51                        my($label, $value) = $cust_svc->label;
52                        "$label: $value";
53                      } else {
54                        '(available)';
55                      }
56                    },
57
58                    \&FS::UI::Web::cust_fields,
59
60                  ],
61                  'align'       => 'rll'.FS::UI::Web::cust_aligns(),
62                  'links'       => [
63                    '',
64                    '',
65                    $link,
66                    ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
67                          FS::UI::Web::cust_header()
68                    ),
69                  ],
70                  'color' => [ 
71                               '',
72                               '',
73                               '',
74                               FS::UI::Web::cust_colors(),
75                             ],
76                  'style' => [ 
77                               '',
78                               '',
79                               '',
80                               FS::UI::Web::cust_styles(),
81                             ],
82
83              )
84 %>
85 <%init>
86
87 my $curuser = $FS::CurrentUser::CurrentUser;
88
89 die "access denied"
90   unless $curuser->access_right('Edit inventory')
91       || $curuser->access_right('Edit global inventory')
92       || $curuser->access_right('Configuration');
93
94 my $classnum = $cgi->param('classnum');
95 $classnum =~ /^(\d+)$/ or errorpage("illegal classnum $classnum");
96 $classnum = $1;
97 my $extra_sql = "WHERE inventory_item.classnum = $classnum ";
98
99 my $inventory_class = qsearchs( {
100   'table'     => 'inventory_class',
101   'hashref'   => { 'classnum' => $classnum },
102 } );
103
104 my $title = $inventory_class->classname. ' Inventory';
105
106 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
107   $extra_sql .= " AND inventory_item.agentnum = $1 ";
108   my $agent = qsearchs('agent', { 'agentnum' => $1 }) or die "unknown agentnum";
109   $title = $agent->agent. " $title";
110 }
111
112 #little false laziness with SQL fragments in inventory_class.pm
113 if ( $cgi->param('avail') ) {
114   $extra_sql .= ' AND ( svcnum IS NULL OR svcnum = 0 )';
115   $title .= ' - Available';
116 } elsif ( $cgi->param('used') ) {
117   $extra_sql .= ' AND svcnum IS NOT NULL AND svcnum > 0';
118   $title .= ' - In use';
119 }
120
121 my $count_query =
122   "SELECT COUNT(*) FROM inventory_item $extra_sql";
123
124 my $link = sub {
125   my $inventory_item = shift;
126   if ( $inventory_item->svcnum ) {
127
128     #[ "${p}view/svc_acct.cgi?", 'svcnum' ];
129     my $url = svc_url(
130       'm'      => $m,
131       'action' => 'view',
132       #'svcdb'  => $inventory_item->cust_svc->part_svc->svcdb,
133       'svcdb'  => $inventory_item->svcdb, #we have it from the joined search
134       'query'  => '',
135     );
136     [ $url, 'svcnum' ];
137   } else {
138     '';
139   }
140 };
141 my $link_cust = sub {
142   my $inventory_item = shift;
143   if ( $inventory_item->custnum ) {
144     [ "${p}view/cust_main.cgi?", 'custnum' ];
145   } else {
146     '';
147   }
148 };
149
150 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
151                 ' LEFT JOIN part_svc  USING ( svcpart ) '.
152                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
153                 ' LEFT JOIN cust_main USING ( custnum ) ';
154
155 </%init>