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