eWay self-signup fixes
[freeside.git] / FS / FS / inventory_class.pm
1 package FS::inventory_class;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( dbh qsearch qsearchs );
6
7 @ISA = qw(FS::Record);
8
9 =head1 NAME
10
11 FS::inventory_class - Object methods for inventory_class records
12
13 =head1 SYNOPSIS
14
15   use FS::inventory_class;
16
17   $record = new FS::inventory_class \%hash;
18   $record = new FS::inventory_class { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
30 An FS::inventory_class object represents a class of inventory, such as "DID 
31 numbers" or "physical equipment serials".  FS::inventory_class inherits from
32 FS::Record.  The following fields are currently supported:
33
34 =over 4
35
36 =item classnum - primary key
37
38 =item classname - Name of this class
39
40
41 =back
42
43 =head1 METHODS
44
45 =over 4
46
47 =item new HASHREF
48
49 Creates a new inventory class.  To add the class to the database, see
50 L<"insert">.
51
52 Note that this stores the hash reference, not a distinct copy of the hash it
53 points to.  You can ask the object for a copy with the I<hash> method.
54
55 =cut
56
57 # the new method can be inherited from FS::Record, if a table method is defined
58
59 sub table { 'inventory_class'; }
60
61 =item insert
62
63 Adds this record to the database.  If there is an error, returns the error,
64 otherwise returns false.
65
66 =cut
67
68 # the insert method can be inherited from FS::Record
69
70 =item delete
71
72 Delete this record from the database.
73
74 =cut
75
76 # the delete method can be inherited from FS::Record
77
78 =item replace OLD_RECORD
79
80 Replaces the OLD_RECORD with this one in the database.  If there is an error,
81 returns the error, otherwise returns false.
82
83 =cut
84
85 # the replace method can be inherited from FS::Record
86
87 =item check
88
89 Checks all fields to make sure this is a valid inventory class.  If there is
90 an error, returns the error, otherwise returns false.  Called by the insert
91 and replace methods.
92
93 =cut
94
95 # the check method should currently be supplied - FS::Record contains some
96 # data checking routines
97
98 sub check {
99   my $self = shift;
100
101   my $error = 
102     $self->ut_numbern('classnum')
103     || $self->ut_textn('classname')
104   ;
105   return $error if $error;
106
107   $self->SUPER::check;
108 }
109
110 =item num_avail 
111
112 Returns the number of available (unused/unallocated) inventory items of this
113 class (see L<FS::inventory_item>).
114
115 =cut
116
117 sub num_avail {
118   my( $self, $sql ) = @_;
119   $sql .= ' AND ' if length($sql);
120   $sql .= '( svcnum IS NULL OR svcnum = 0 )';
121   $self->num_sql($sql);
122 }
123
124 sub num_sql {
125   my( $self, $sql ) = @_;
126   $sql = "AND $sql" if length($sql);
127
128   my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql(
129     'null'  => 1,
130     'table' => 'inventory_item',
131   );
132
133   my $st = "SELECT COUNT(*) FROM inventory_item ".
134            " WHERE classnum = ? AND $agentnums_sql $sql";
135   my $sth = dbh->prepare($st)    or die  dbh->errstr. " preparing $st";
136   $sth->execute($self->classnum) or die $sth->errstr. " executing $st";
137   $sth->fetchrow_arrayref->[0];
138 }
139
140 =item num_used
141
142 Returns the number of used (allocated) inventory items of this class (see
143 L<FS::inventory_class>).
144
145 =cut
146
147 sub num_used {
148   my( $self, $sql ) = @_;
149   $sql .= ' AND ' if length($sql);
150   $sql .= 'svcnum IS NOT NULL AND svcnum > 0 ';
151   $self->num_sql($sql);
152 }
153
154 =item num_total
155
156 Returns the total number of inventory items of this class (see
157 L<FS::inventory_class>).
158
159 =cut
160
161 sub num_total {
162   my( $self, $sql ) = @_;
163   $self->num_sql($sql);
164 }
165
166 =back
167
168 =head1 CLASS METHODS
169
170 =over 4
171
172 =item searchcell_factory
173
174 =cut
175
176 sub countcell_factory {
177   my($class, %opt) = @_;
178
179   my $p = $opt{p};
180
181   my $sql = $opt{'agentnum'} ? 'agentnum = '.$opt{'agentnum'} : '';
182
183   use Tie::IxHash;
184   tie my %labels, 'Tie::IxHash',
185     'num_avail' => 'Available', #  <FONT SIZE="-1"><A HREF="eventually">(upload batch)</A></FONT>',
186     'num_used'  => 'In use', #'Used', #'Allocated',
187     'num_total' => 'Total',
188   ;
189
190   my %link = (
191     'num_avail' => ';avail=1',
192     'num_used'  => ';used=1',
193     'num_total' => '',
194   );
195
196   my %inv_action_link = (
197     'num_avail' => [ 'upload batch',
198                      $p.'misc/inventory_item-import.html?classnum=',
199                      'classnum'
200                    ],
201   );
202
203   sub {
204     my $inventory_class = shift;
205
206     my $link =
207       $p. 'search/inventory_item.html?'.
208       'classnum='. $inventory_class->classnum;
209     $link .= ';agentnum='.$opt{'agentnum'} if $opt{'agentnum'};
210
211     my %actioncol = ();
212     foreach ( keys %inv_action_link ) {
213       my($label, $baseurl, $method) =
214         @{ $inv_action_link{$_} };
215       my $url = $baseurl. $inventory_class->$method();
216       $actioncol{$_} =
217         '<FONT SIZE="-1">'.
218         '('.
219         '<A HREF="'.$url.'">'.
220         $label.
221         '</A>'.
222         ')'.
223         '</FONT>';
224     }
225
226     my %num = map { 
227       $_ => $inventory_class->$_($sql);
228     } keys %labels;
229
230     [ map {
231             [
232               {
233                 'data'  => '<B>'. $num{$_}. '</B>',
234                 'align' => 'right',
235               },
236               {
237                 'data'  => $labels{$_},
238                 'align' => 'left',
239                 'link'  => ( $num{$_}
240                                ? $link.$link{$_}
241                                : ''
242                            ),
243               },
244               { 'data'  => $actioncol{$_},
245                 'align'  => 'left',
246               },
247             ]
248           } keys %labels
249     ];
250   };
251 }
252
253 =back
254
255 =head1 BUGS
256
257 =head1 SEE ALSO
258
259 L<FS::inventory_item>, L<FS::Record>, schema.html from the base documentation.
260
261 =cut
262
263 1;
264