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