eWay self-signup fixes
[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_foreign_keyn('agentnum', 'agent', 'agentnum' )
110     || $self->ut_agentnum_acl('agentnum', ['Configuration',
111                                            'Edit global inventory'] )
112     || $self->ut_text('item')
113     || $self->ut_foreign_keyn('svcnum', 'cust_svc', 'svcnum' )
114   ;
115   return $error if $error;
116
117   $self->SUPER::check;
118 }
119
120 =item cust_svc
121
122 Returns the customer service associated with this inventory item, if the
123 item has been used (see L<FS::cust_svc>).
124
125 =cut
126
127 sub cust_svc {
128   my $self = shift;
129   return '' unless $self->svcnum;
130   qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
131 }
132
133 =item agent 
134
135 Returns the associated agent for this event, if any, as an FS::agent object.
136
137 =cut
138
139 sub agent {
140   my $self = shift;
141   qsearchs('agent', { 'agentnum' => $self->agentnum } );
142 }
143
144 =back
145
146 =head1 SUBROUTINES
147
148 =over 4
149
150 =item process_batch_import
151
152 =cut
153
154 sub process_batch_import {
155   my $job = shift;
156
157   my $opt = { 'table'   => 'inventory_item',
158               #'params'  => [ 'itembatch', 'classnum', ],
159               'params'  => [ 'classnum', 'agentnum', ],
160               'formats' => { 'default' => [ 'item' ] },
161               'default_csv' => 1,
162             };
163
164   FS::Record::process_batch_import( $job, $opt, @_ );
165
166 }
167
168 =back
169
170 =head1 BUGS
171
172 maybe batch_import should be a regular method in FS::inventory_class
173
174 =head1 SEE ALSO
175
176 L<inventory_class>, L<cust_svc>, L<FS::Record>, schema.html from the base
177 documentation.
178
179 =cut
180
181 1;
182