Will things ever be the same again?
[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 %
50 <% include( 'elements/search.html',
51
52                  'title'       => $title,
53
54                  #less lame to use Lingua:: something to pluralize
55                  'name'        => $inventory_class->classname. 's',
56
57                  'query'       => {
58                                     'table'   => 'inventory_item',
59                                     'hashref' => { 'classnum' => $classnum },
60                                     'select'  => join(', ',
61                                         'inventory_item.*',
62                                         'cust_main.custnum',
63                                         FS::UI::Web::cust_sql_fields(),
64                                       ),
65                                     'extra_sql' => $extra_sql,
66                                     'addl_from' => $addl_from,
67                                   },
68
69                  'count_query' => $count_query,
70
71                  'header'      => [
72                    '#',
73                    $inventory_class->classname,
74                    'Service',
75                    FS::UI::Web::cust_header(),
76                  ],
77
78                  'fields'      => [
79                    'itemnum',
80                    'item',
81                    #'svcnum', #XXX proper full service customer link ala svc_acct
82                              # "unallocated" ?  "available" ?
83                    sub {
84                      #this could be way more efficient with a mixin
85                      # like cust_main_Mixin that let us all all the methods
86                      # on data we already have...
87                      my $inventory_item = shift;
88                      my $cust_svc = $inventory_item->cust_svc;
89                      if ( $cust_svc ) {
90                        my($label, $value) = $cust_svc->label;
91                        "$label: $value";
92                      } else {
93                        '(available)';
94                      }
95                    },
96
97                    \&FS::UI::Web::cust_fields,
98
99                  ],
100
101                  'links'       => [
102                    '',
103                    '',
104                    $link,
105                    ( map { $link_cust } FS::UI::Web::cust_header() ),
106                  ],
107
108              )
109 %>