include elements/search.html the right way to avoid problems with XLS download, ...
[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_form' => 
85                     qq!
86 <FORM NAME="itemForm" ACTION="$p/misc/inventory_item-move.cgi" METHOD="POST">
87 <INPUT TYPE="hidden" NAME="classnum" VALUE="$classnum">
88 <INPUT TYPE="hidden" NAME="avail"    VALUE="! .$cgi->param('avail') . '">', #'
89                   'html_foot' => $sub_foot,
90              
91 &>
92 <%init>
93
94 my $curuser = $FS::CurrentUser::CurrentUser;
95
96 die "access denied"
97   unless $curuser->access_right('Edit inventory')
98       || $curuser->access_right('Edit global inventory')
99       || $curuser->access_right('Configuration');
100
101 my $classnum = $cgi->param('classnum');
102 $classnum =~ /^(\d+)$/ or errorpage("illegal classnum $classnum");
103 $classnum = $1;
104 my $extra_sql = "WHERE inventory_item.classnum = $classnum ";
105
106 my $inventory_class = qsearchs( {
107   'table'     => 'inventory_class',
108   'hashref'   => { 'classnum' => $classnum },
109 } );
110
111 my $title = $inventory_class->classname. ' Inventory';
112
113 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
114   $extra_sql .= " AND inventory_item.agentnum = $1 ";
115   my $agent = qsearchs('agent', { 'agentnum' => $1 }) or die "unknown agentnum";
116   $title = $agent->agent. " $title";
117 }
118
119 #little false laziness with SQL fragments in inventory_class.pm
120 if ( $cgi->param('avail') ) {
121   $extra_sql .= ' AND ( svcnum IS NULL OR svcnum = 0 )';
122   $title .= ' - Available';
123 } elsif ( $cgi->param('used') ) {
124   $extra_sql .= ' AND svcnum IS NOT NULL AND svcnum > 0';
125   $title .= ' - In use';
126 }
127
128 my $count_query =
129   "SELECT COUNT(*) FROM inventory_item $extra_sql";
130
131 my $link = sub {
132   my $inventory_item = shift;
133   if ( $inventory_item->svcnum ) {
134
135     #[ "${p}view/svc_acct.cgi?", 'svcnum' ];
136     my $url = svc_url(
137       'm'      => $m,
138       'action' => 'view',
139       #'svcdb'  => $inventory_item->cust_svc->part_svc->svcdb,
140       'svcdb'  => $inventory_item->svcdb, #we have it from the joined search
141       'query'  => '',
142     );
143     [ $url, 'svcnum' ];
144   } else {
145     '';
146   }
147 };
148 my $link_cust = sub {
149   my $inventory_item = shift;
150   if ( $inventory_item->custnum ) {
151     [ "${p}view/cust_main.cgi?", 'custnum' ];
152   } else {
153     '';
154   }
155 };
156
157 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
158                 ' LEFT JOIN part_svc  USING ( svcpart ) '.
159                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
160                 FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
161 my $areboxes = 0;
162
163 my $sub_checkbox = sub {
164   my $item = $_[0];
165   my $itemnum = $item->itemnum;
166   #return '' if $item->svcnum;
167   $areboxes = 1;
168   return qq!<INPUT NAME="itemnum$itemnum" TYPE="checkbox" VALUE="1">!;
169 };
170
171 my $sub_foot = sub {
172   return if !$areboxes;
173   my $foot =
174 '<BR><INPUT TYPE="button" VALUE="Select all" onClick="setAll(true)">
175 <INPUT TYPE="button" VALUE="Unselect all" onClick="setAll(false)">
176 <BR><INPUT TYPE="submit" NAME="action" VALUE="Move to agent">
177 <SELECT NAME="move_agentnum">';
178   foreach my $agent ($curuser->agents) {
179     $foot .= '<OPTION VALUE="'.$agent->agentnum.'">'.
180              $agent->agent . '</OPTION>
181              ';
182   }
183   $foot .= '</SELECT>
184 <SCRIPT TYPE="text/javascript">
185  function setAll(setTo) {
186    theForm = document.itemForm;
187    for (i=0,n=theForm.elements.length;i<n;i++)
188      if (theForm.elements[i].name.indexOf("itemnum") != -1)
189        theForm.elements[i].checked = setTo;
190  }
191 </SCRIPT>';
192   $foot;
193 };
194     
195
196   
197
198 </%init>