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