finish at least the automatic provisioning part
[freeside.git] / httemplate / search / inventory_item.html
1 <%
2
3 my $classnum = $cgi->param('classnum');
4 $classnum =~ /^(\d+)$/ or eidiot "illegal classnum $classnum";
5 $classnum = $1;
6
7 my $inventory_class = qsearchs( {
8   'table'     => 'inventory_class',
9   'hashref'   => { 'classnum' => $classnum },
10 } );
11
12 my $title = $inventory_class->classname. ' Inventory';
13
14 #little false laziness with SQL fragments in inventory_class.pm
15 my $extra_sql = '';
16 if ( $cgi->param('avail') ) {
17   $extra_sql = 'AND ( svcnum IS NULL OR svcnum = 0 )';
18   $title .= ' - Available';
19 } elsif ( $cgi->param('used') ) {
20   $extra_sql = 'AND svcnum IS NOT NULL AND svcnum > 0';
21   $title .= ' - In use';
22 }
23
24 my $count_query =
25   "SELECT COUNT(*) FROM inventory_item WHERE classnum = $classnum $extra_sql";
26
27 my $link = sub {
28   my $inventory_item = shift;
29   if ( $inventory_item->svcnum ) {
30     [ "${p}view/svc_acct.cgi?", 'svcnum' ];
31   } else {
32     '';
33   }
34 };
35 my $link_cust = sub {
36   my $inventory_item = shift;
37   if ( $inventory_item->custnum ) {
38     [ "${p}view/cust_main.cgi?", 'custnum' ];
39   } else {
40     '';
41   }
42 };
43
44 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
45                 ' LEFT JOIN part_svc  USING ( svcpart ) '.
46                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
47                 ' LEFT JOIN cust_main USING ( custnum ) ';
48
49 %><%= include( 'elements/search.html',
50
51                  'title'       => $title,
52
53                  #less lame to use Lingua:: something to pluralize
54                  'name'        => $inventory_class->classname. 's',
55
56                  'query'       => {
57                                     'table'   => 'inventory_item',
58                                     'hashref' => { 'classnum' => $classnum },
59                                     'select'  => join(', ',
60                                         'inventory_item.*',
61                                         'cust_main.custnum',
62                                         FS::UI::Web::cust_sql_fields(),
63                                       ),
64                                     'extra_sql' => $extra_sql,
65                                     'addl_from' => $addl_from,
66                                   },
67
68                  'count_query' => $count_query,
69
70                  'header'      => [
71                    '#',
72                    $inventory_class->classname,
73                    'Service',
74                    FS::UI::Web::cust_header(),
75                  ],
76
77                  'fields'      => [
78                    'itemnum',
79                    'item',
80                    #'svcnum', #XXX proper full service customer link ala svc_acct
81                              # "unallocated" ?  "available" ?
82                    sub {
83                      #this could be way more efficient with a mixin
84                      # like cust_main_Mixin that let us all all the methods
85                      # on data we already have...
86                      my $inventory_item = shift;
87                      my $cust_svc = $inventory_item->cust_svc;
88                      if ( $cust_svc ) {
89                        my($label, $value) = $cust_svc->label;
90                        "$label: $value";
91                      } else {
92                        '(available)';
93                      }
94                    },
95
96                    \&FS::UI::Web::cust_fields,
97
98                  ],
99
100                  'links'       => [
101                    '',
102                    '',
103                    $link,
104                    ( map { $link_cust } FS::UI::Web::cust_header() ),
105                  ],
106
107              )
108 %>