v4 style
[freeside.git] / httemplate / search / inventory_item.html
1 <& 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                    '', # checkbox column
38                  ],
39
40                  'fields'      => [
41                    'itemnum',
42                    'item',
43                    #'svcnum', #XXX proper full service customer link ala svc_acct
44                              # "unallocated" ?  "available" ?
45                    sub {
46                      #this could be way more efficient with a mixin
47                      # like cust_main_Mixin that let us all all the methods
48                      # on data we already have...
49                      my $inventory_item = shift;
50                      my $cust_svc = $inventory_item->cust_svc;
51                      if ( $cust_svc ) {
52                        my($label, $value) = $cust_svc->label;
53                        "$label: $value";
54                      } else {
55                        '(available)';
56                      }
57                    },
58
59                    \&FS::UI::Web::cust_fields,
60                    $sub_checkbox,
61
62                  ],
63                  'align'       => 'rll'.FS::UI::Web::cust_aligns(),
64                  'links'       => [
65                    '',
66                    '',
67                    $link,
68                    ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
69                          FS::UI::Web::cust_header()
70                    ),
71                  ],
72                  'color' => [ 
73                               '',
74                               '',
75                               '',
76                               FS::UI::Web::cust_colors(),
77                             ],
78                  'style' => [ 
79                               '',
80                               '',
81                               '',
82                               FS::UI::Web::cust_styles(),
83                             ],
84                   'html_init' => $html_form,
85                   'html_foot' => $html_foot,
86              
87 &>
88 <%init>
89
90 my $curuser = $FS::CurrentUser::CurrentUser;
91
92 die "access denied"
93   unless $curuser->access_right('Edit inventory')
94       || $curuser->access_right('Edit global inventory')
95       || $curuser->access_right('Configuration');
96
97 my $classnum = $cgi->param('classnum');
98 $classnum =~ /^(\d+)$/ or errorpage("illegal classnum $classnum");
99 $classnum = $1;
100 my $extra_sql = "WHERE inventory_item.classnum = $classnum ";
101
102 my $inventory_class = qsearchs( {
103   'table'     => 'inventory_class',
104   'hashref'   => { 'classnum' => $classnum },
105 } );
106
107 my $title = $inventory_class->classname. ' Inventory';
108
109 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
110   $extra_sql .= " AND inventory_item.agentnum = $1 ";
111   my $agent = qsearchs('agent', { 'agentnum' => $1 }) or die "unknown agentnum";
112   $title = $agent->agent. " $title";
113 }
114
115 #little false laziness with SQL fragments in inventory_class.pm
116 if ( $cgi->param('avail') ) {
117   $extra_sql .= ' AND ( svcnum IS NULL OR svcnum = 0 )';
118   $title .= ' - Available';
119 } elsif ( $cgi->param('used') ) {
120   $extra_sql .= ' AND svcnum IS NOT NULL AND svcnum > 0';
121   $title .= ' - In use';
122 }
123
124 my $count_query =
125   "SELECT COUNT(*) FROM inventory_item $extra_sql";
126
127 my $link = sub {
128   my $inventory_item = shift;
129   if ( $inventory_item->svcnum ) {
130
131     #[ "${p}view/svc_acct.cgi?", 'svcnum' ];
132     my $url = svc_url(
133       'm'      => $m,
134       'action' => 'view',
135       #'svcdb'  => $inventory_item->cust_svc->part_svc->svcdb,
136       'svcdb'  => $inventory_item->svcdb, #we have it from the joined search
137       'query'  => '',
138     );
139     [ $url, 'svcnum' ];
140   } else {
141     '';
142   }
143 };
144 my $link_cust = sub {
145   my $inventory_item = shift;
146   if ( $inventory_item->custnum ) {
147     [ "${p}view/cust_main.cgi?", 'custnum' ];
148   } else {
149     '';
150   }
151 };
152
153 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
154                 ' LEFT JOIN part_svc  USING ( svcpart ) '.
155                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
156                 FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
157
158 my $sub_checkbox = sub {
159   my $item = $_[0];
160   my $itemnum = $item->itemnum;
161   return '' if $item->svcnum;
162
163   return qq!<INPUT NAME="itemnum" TYPE="checkbox" VALUE="$itemnum">!;
164 };
165
166 my $html_form = qq!
167 <FORM NAME="itemForm" ACTION="$p/misc/inventory_item-move.cgi" METHOD="POST">
168 <INPUT TYPE="hidden" NAME="classnum" VALUE="$classnum">
169 <INPUT TYPE="hidden" NAME="avail"    VALUE="! .$cgi->param('avail') . '">';
170 #'
171
172 my $html_foot = include('elements/checkbox-foot.html',
173       actions => [
174           { submit  => 'Delete',
175             confirm => 'Delete the selected items?',
176             name    => 'delete',
177           },
178           { submit  => 'Move to',
179             name    => 'move',
180           },
181       ],
182 );
183
184 $html_foot =~ s[</DIV>][];
185 $html_foot .= 
186   include('/elements/select-agent.html',
187     'field'         => 'move_agentnum',
188     'disable_empty' => 1,
189   ) .
190   '</DIV></FORM>';
191
192 </%init>