1 package FS::inventory_class;
2 use base qw(FS::Record);
5 use FS::Record qw( dbh ); # qsearch qsearchs dbh );
9 FS::inventory_class - Object methods for inventory_class records
13 use FS::inventory_class;
15 $record = new FS::inventory_class \%hash;
16 $record = new FS::inventory_class { 'column' => 'value' };
18 $error = $record->insert;
20 $error = $new_record->replace($old_record);
22 $error = $record->delete;
24 $error = $record->check;
28 An FS::inventory_class object represents a class of inventory, such as "DID
29 numbers" or "physical equipment serials". FS::inventory_class inherits from
30 FS::Record. The following fields are currently supported:
34 =item classnum - primary key
36 =item classname - Name of this class
47 Creates a new inventory class. To add the class to the database, see
50 Note that this stores the hash reference, not a distinct copy of the hash it
51 points to. You can ask the object for a copy with the I<hash> method.
55 # the new method can be inherited from FS::Record, if a table method is defined
57 sub table { 'inventory_class'; }
61 Adds this record to the database. If there is an error, returns the error,
62 otherwise returns false.
66 # the insert method can be inherited from FS::Record
70 Delete this record from the database.
74 # the delete method can be inherited from FS::Record
76 =item replace OLD_RECORD
78 Replaces the OLD_RECORD with this one in the database. If there is an error,
79 returns the error, otherwise returns false.
83 # the replace method can be inherited from FS::Record
87 Checks all fields to make sure this is a valid inventory class. If there is
88 an error, returns the error, otherwise returns false. Called by the insert
93 # the check method should currently be supplied - FS::Record contains some
94 # data checking routines
100 $self->ut_numbern('classnum')
101 || $self->ut_textn('classname')
103 return $error if $error;
110 Returns the number of available (unused/unallocated) inventory items of this
111 class (see L<FS::inventory_item>).
116 my( $self, $sql ) = @_;
117 $sql .= ' AND ' if length($sql);
118 $sql .= '( svcnum IS NULL OR svcnum = 0 )';
119 $self->num_sql($sql);
123 my( $self, $sql ) = @_;
124 $sql = "AND $sql" if length($sql);
126 my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql(
128 'table' => 'inventory_item',
131 my $st = "SELECT COUNT(*) FROM inventory_item ".
132 " WHERE classnum = ? AND $agentnums_sql $sql";
133 my $sth = dbh->prepare($st) or die dbh->errstr. " preparing $st";
134 $sth->execute($self->classnum) or die $sth->errstr. " executing $st";
135 $sth->fetchrow_arrayref->[0];
140 Returns the number of used (allocated) inventory items of this class (see
141 L<FS::inventory_class>).
146 my( $self, $sql ) = @_;
147 $sql .= ' AND ' if length($sql);
148 $sql .= 'svcnum IS NOT NULL AND svcnum > 0 ';
149 $self->num_sql($sql);
154 Returns the total number of inventory items of this class (see
155 L<FS::inventory_class>).
160 my( $self, $sql ) = @_;
161 $self->num_sql($sql);
170 =item searchcell_factory
174 sub countcell_factory {
175 my($class, %opt) = @_;
179 my $sql = $opt{'agentnum'} ? 'agentnum = '.$opt{'agentnum'} : '';
182 tie my %labels, 'Tie::IxHash',
183 'num_avail' => 'Available', # <FONT SIZE="-1"><A HREF="eventually">(upload batch)</A></FONT>',
184 'num_used' => 'In use', #'Used', #'Allocated',
185 'num_total' => 'Total',
189 'num_avail' => ';avail=1',
190 'num_used' => ';used=1',
194 my %inv_action_link = (
195 'num_avail' => [ 'upload batch',
196 $p.'misc/inventory_item-import.html?classnum=',
202 my $inventory_class = shift;
205 $p. 'search/inventory_item.html?'.
206 'classnum='. $inventory_class->classnum;
207 $link .= ';agentnum='.$opt{'agentnum'} if $opt{'agentnum'};
210 foreach ( keys %inv_action_link ) {
211 my($label, $baseurl, $method) =
212 @{ $inv_action_link{$_} };
213 my $url = $baseurl. $inventory_class->$method();
217 '<A HREF="'.$url.'">'.
225 $_ => $inventory_class->$_($sql);
231 'data' => '<B>'. $num{$_}. '</B>',
235 'data' => $labels{$_},
242 { 'data' => $actioncol{$_},
257 L<FS::inventory_item>, L<FS::Record>, schema.html from the base documentation.