This commit was generated by cvs2svn to compensate for changes in r4888,
[freeside.git] / FS / FS / inventory_item.pm
1 package FS::inventory_item;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( dbh qsearch qsearchs );
6 use FS::cust_main_Mixin;
7 use FS::inventory_class;
8 use FS::cust_svc;
9
10 @ISA = qw( FS::cust_main_Mixin FS::Record );
11
12 =head1 NAME
13
14 FS::inventory_item - Object methods for inventory_item records
15
16 =head1 SYNOPSIS
17
18   use FS::inventory_item;
19
20   $record = new FS::inventory_item \%hash;
21   $record = new FS::inventory_item { 'column' => 'value' };
22
23   $error = $record->insert;
24
25   $error = $new_record->replace($old_record);
26
27   $error = $record->delete;
28
29   $error = $record->check;
30
31 =head1 DESCRIPTION
32
33 An FS::inventory_item object represents a specific piece of (real or virtual)
34 inventory, such as a specific DID or serial number.  FS::inventory_item
35 inherits from FS::Record.  The following fields are currently supported:
36
37 =over 4
38
39 =item itemnum - primary key
40
41 =item classnum - Inventory class (see L<FS::inventory_class>)
42
43 =item item - Item identifier (unique within its inventory class)
44
45 =item svcnum - Customer servcie (see L<FS::cust_svc>)
46
47 =back
48
49 =head1 METHODS
50
51 =over 4
52
53 =item new HASHREF
54
55 Creates a new item.  To add the item to the database, see L<"insert">.
56
57 Note that this stores the hash reference, not a distinct copy of the hash it
58 points to.  You can ask the object for a copy with the I<hash> method.
59
60 =cut
61
62 # the new method can be inherited from FS::Record, if a table method is defined
63
64 sub table { 'inventory_item'; }
65
66 =item insert
67
68 Adds this record to the database.  If there is an error, returns the error,
69 otherwise returns false.
70
71 =cut
72
73 # the insert method can be inherited from FS::Record
74
75 =item delete
76
77 Delete this record from the database.
78
79 =cut
80
81 # the delete method can be inherited from FS::Record
82
83 =item replace OLD_RECORD
84
85 Replaces the OLD_RECORD with this one in the database.  If there is an error,
86 returns the error, otherwise returns false.
87
88 =cut
89
90 # the replace method can be inherited from FS::Record
91
92 =item check
93
94 Checks all fields to make sure this is a valid item.  If there is
95 an error, returns the error, otherwise returns false.  Called by the insert
96 and replace methods.
97
98 =cut
99
100 # the check method should currently be supplied - FS::Record contains some
101 # data checking routines
102
103 sub check {
104   my $self = shift;
105
106   my $error = 
107     $self->ut_numbern('itemnum')
108     || $self->ut_foreign_key('classnum', 'inventory_class', 'classnum' )
109     || $self->ut_text('item')
110     || $self->ut_foreign_keyn('svcnum', 'cust_svc', 'svcnum' )
111   ;
112   return $error if $error;
113
114   $self->SUPER::check;
115 }
116
117 =item cust_svc
118
119 Returns the customer service associated with this inventory item, if the
120 item has been used (see L<FS::cust_svc>).
121
122 =cut
123
124 sub cust_svc {
125   my $self = shift;
126   return '' unless $self->svcnum;
127   qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
128 }
129
130 =back
131
132 =head1 CLASS METHODS
133
134 =over 4
135
136 =item batch_import
137
138 =cut
139
140 sub batch_import {
141   my $param = shift;
142
143   my $fh = $param->{filehandle};
144
145   my $imported = 0;
146
147   local $SIG{HUP} = 'IGNORE';
148   local $SIG{INT} = 'IGNORE';
149   local $SIG{QUIT} = 'IGNORE';
150   local $SIG{TERM} = 'IGNORE';
151   local $SIG{TSTP} = 'IGNORE';
152   local $SIG{PIPE} = 'IGNORE';
153
154   my $oldAutoCommit = $FS::UID::AutoCommit;
155   local $FS::UID::AutoCommit = 0;
156   my $dbh = dbh;
157   
158   my $line;
159   while ( defined($line=<$fh>) ) {
160
161     chomp $line;
162
163     my $inventory_item = new FS::inventory_item {
164       'classnum' => $param->{'classnum'},
165       'item'     => $line,
166     };
167
168     my $error = $inventory_item->insert;
169
170     if ( $error ) {
171       $dbh->rollback if $oldAutoCommit;
172       return $error;
173
174       #or just skip?
175       #next;
176     }
177
178     $imported++;
179   }
180
181   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
182
183   #might want to disable this if we skip records for any reason...
184   return "Empty file!" unless $imported;
185
186   '';
187
188 }
189
190 =back
191
192 =head1 BUGS
193
194 maybe batch_import should be a regular method in FS::inventory_class
195
196 =head1 SEE ALSO
197
198 L<inventory_class>, L<cust_svc>, L<FS::Record>, schema.html from the base
199 documentation.
200
201 =cut
202
203 1;
204