This commit was generated by cvs2svn to compensate for changes in r11022,
[freeside.git] / FS / FS / did_order.pm
1 package FS::did_order;
2
3 use strict;
4 use base qw( FS::o2m_Common FS::Record );
5 use FS::Record qw( qsearch qsearchs dbh );
6
7 =head1 NAME
8
9 FS::did_order - Object methods for did_order records
10
11 =head1 SYNOPSIS
12
13   use FS::did_order;
14
15   $record = new FS::did_order \%hash;
16   $record = new FS::did_order { 'column' => 'value' };
17
18   $error = $record->insert;
19
20   $error = $new_record->replace($old_record);
21
22   $error = $record->delete;
23
24   $error = $record->check;
25
26 =head1 DESCRIPTION
27
28 An FS::did_order object represents a bulk DID order.  FS::did_order inherits from
29 FS::Record.  The following fields are currently supported:
30
31 =over 4
32
33 =item ordernum
34
35 primary key
36
37 =item vendornum
38
39 vendornum
40
41 =item vendor_order_id
42
43 vendor_order_id
44
45 =item submitted
46
47 submitted
48
49 =item confirmed
50
51 confirmed
52
53 =item received
54
55 received
56
57
58 =back
59
60 =head1 METHODS
61
62 =over 4
63
64 =item new HASHREF
65
66 Creates a new bulk DID order.  To add it to the database, see L<"insert">.
67
68 Note that this stores the hash reference, not a distinct copy of the hash it
69 points to.  You can ask the object for a copy with the I<hash> method.
70
71 =cut
72
73 # the new method can be inherited from FS::Record, if a table method is defined
74
75 sub table { 'did_order'; }
76
77 =item insert
78
79 Adds this record to the database.  If there is an error, returns the error,
80 otherwise returns false.
81
82 =cut
83
84 # the insert method can be inherited from FS::Record
85
86 =item delete
87
88 Delete this record from the database.
89
90 =cut
91
92 sub delete {
93   my $self = shift;
94
95   return "Can't delete a DID order which has DIDs received"
96     if qsearch( 'phone_avail', { 'ordernum' => $self->ordernum } );
97
98   local $SIG{HUP} = 'IGNORE';
99   local $SIG{INT} = 'IGNORE';
100   local $SIG{QUIT} = 'IGNORE';
101   local $SIG{TERM} = 'IGNORE';
102   local $SIG{TSTP} = 'IGNORE';
103   local $SIG{PIPE} = 'IGNORE';
104
105   my $oldAutoCommit = $FS::UID::AutoCommit;
106   local $FS::UID::AutoCommit = 0;
107   my $dbh = dbh;
108
109   my @did_order_item = $self->did_order_item;
110
111   foreach my $did_order_item ( @did_order_item ) {
112     my $error = $did_order_item->delete;
113     if ( $error ) {
114       $dbh->rollback if $oldAutoCommit;
115       return "can't delete DID order item "
116                                 . $did_order_item->orderitemnum . ": $error";
117     }
118   }
119
120   my $error = $self->SUPER::delete(@_);
121   if ( $error ) {
122     $dbh->rollback if $oldAutoCommit;
123     return $error;
124   }
125
126   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
127 }
128
129
130 =item replace OLD_RECORD
131
132 Replaces the OLD_RECORD with this one in the database.  If there is an error,
133 returns the error, otherwise returns false.
134
135 =cut
136
137 # the replace method can be inherited from FS::Record
138
139 =item check
140
141 Checks all fields to make sure this is a valid bulk DID order.  If there is
142 an error, returns the error, otherwise returns false.  Called by the insert
143 and replace methods.
144
145 =cut
146
147 # the check method should currently be supplied - FS::Record contains some
148 # data checking routines
149
150 sub check {
151   my $self = shift;
152
153   my $error = 
154     $self->ut_numbern('ordernum')
155     || $self->ut_foreign_key('vendornum', 'did_vendor', 'vendornum' )
156     || $self->ut_textn('vendor_order_id')
157     || $self->ut_number('submitted')
158     || $self->ut_numbern('confirmed')
159     || $self->ut_numbern('received')
160   ;
161   return $error if $error;
162
163   $self->SUPER::check;
164 }
165
166 =item did_order_item
167
168 Returns the did_order_items (see L<FS::did_order_item>) associated with this bulk DID order.
169
170 =cut
171
172 sub did_order_item {
173   my $self = shift;
174   qsearch( 'did_order_item', { 'ordernum' => $self->ordernum } );
175 }
176
177 =item cust_main
178
179 Returns the cust_main (see L<FS::cust_main>), if any, associated with this bulk DID order.
180
181 =cut
182
183 sub cust_main {
184   my $self = shift;
185   return '' unless $self->custnum;
186   qsearchs('cust_main', { 'custnum' => $self->custnum } );
187 }
188
189 =item provisioned
190
191 Returns the provisioned DIDs, if any, as phone_avail (see L<FS::phone_avail>) objects.
192
193 =cut
194
195 sub provisioned {
196   my $self = shift;
197   qsearch({ table   => 'phone_avail',
198               hashref => { 'ordernum' => $self->ordernum, },
199               select  => 'phone_avail.*',
200               extra_sql => ' and svcnum is not null ',
201          });
202 }
203
204 =back
205
206 =head1 SEE ALSO
207
208 L<FS::Record>, schema.html from the base documentation.
209
210 =cut
211
212 1;
213