4 use base qw( FS::o2m_Common FS::Record );
5 use FS::Record qw( qsearch qsearchs dbh );
9 FS::did_order - Object methods for did_order records
15 $record = new FS::did_order \%hash;
16 $record = new FS::did_order { 'column' => 'value' };
18 $error = $record->insert;
20 $error = $new_record->replace($old_record);
22 $error = $record->delete;
24 $error = $record->check;
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:
66 Creates a new bulk DID order. To add it to the database, see L<"insert">.
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.
73 # the new method can be inherited from FS::Record, if a table method is defined
75 sub table { 'did_order'; }
79 Adds this record to the database. If there is an error, returns the error,
80 otherwise returns false.
84 # the insert method can be inherited from FS::Record
88 Delete this record from the database.
95 return "Can't delete a DID order which has DIDs received"
96 if qsearch( 'phone_avail', { 'ordernum' => $self->ordernum } );
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';
105 my $oldAutoCommit = $FS::UID::AutoCommit;
106 local $FS::UID::AutoCommit = 0;
109 my @did_order_item = $self->did_order_item;
111 foreach my $did_order_item ( @did_order_item ) {
112 my $error = $did_order_item->delete;
114 $dbh->rollback if $oldAutoCommit;
115 return "can't delete DID order item "
116 . $did_order_item->orderitemnum . ": $error";
120 my $error = $self->SUPER::delete(@_);
122 $dbh->rollback if $oldAutoCommit;
126 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
131 =item merge SOURCE_ORDER
133 Merges the DID order given by SOURCE_ORDER into THIS order.
135 The following fields from the source order are transferred, only if they aren't
141 DID order items are transferred into this order. Per-order customer is cleared
142 if any order items are assigned to a customer.
144 The source order is deleted.
146 The operation fails if:
147 -either order has a received time; or
148 -the DID vendors do not match between the orders
155 return "invalid source order" unless $src;
157 return "DIDs received for either order"
158 if $src->received || $self->received;
160 return "DID vendors do not match"
161 if $src->vendornum != $self->vendornum;
163 local $SIG{HUP} = 'IGNORE';
164 local $SIG{INT} = 'IGNORE';
165 local $SIG{QUIT} = 'IGNORE';
166 local $SIG{TERM} = 'IGNORE';
167 local $SIG{TSTP} = 'IGNORE';
168 local $SIG{PIPE} = 'IGNORE';
170 my $oldAutoCommit = $FS::UID::AutoCommit;
171 local $FS::UID::AutoCommit = 0;
174 my @move_if_unset = qw( vendor_order_id confirmed custnum );
175 foreach my $f ( @move_if_unset ) {
176 $self->$f($src->$f) if !$self->$f;
180 my $item_has_cust = 0;
181 my @did_order_item = $src->did_order_item;
182 foreach my $did_order_item ( @did_order_item ) {
183 $did_order_item->ordernum($self->ordernum);
184 $item_has_cust = 1 if $did_order_item->custnum;
185 $error = $did_order_item->replace;
187 $dbh->rollback if $oldAutoCommit;
188 return "can't replace did order item "
189 . $did_order_item->orderitemnum . ": $error";
193 @did_order_item = $self->did_order_item;
194 foreach my $did_order_item ( @did_order_item ) {
195 $item_has_cust = 1 if $did_order_item->custnum;
198 $self->custnum('') if $item_has_cust;
200 $error = $src->delete;
202 $dbh->rollback if $oldAutoCommit;
203 return "can't delete source order: $error";
206 $error = $self->replace;
208 $dbh->rollback if $oldAutoCommit;
209 return "can't replace target order: $error";
212 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
217 =item replace OLD_RECORD
219 Replaces the OLD_RECORD with this one in the database. If there is an error,
220 returns the error, otherwise returns false.
224 # the replace method can be inherited from FS::Record
228 Checks all fields to make sure this is a valid bulk DID order. If there is
229 an error, returns the error, otherwise returns false. Called by the insert
234 # the check method should currently be supplied - FS::Record contains some
235 # data checking routines
241 $self->ut_numbern('ordernum')
242 || $self->ut_foreign_key('vendornum', 'did_vendor', 'vendornum' )
243 || $self->ut_textn('vendor_order_id')
244 || $self->ut_number('submitted')
245 || $self->ut_numbern('confirmed')
246 || $self->ut_numbern('received')
248 return $error if $error;
255 Returns the did_order_items (see L<FS::did_order_item>) associated with this bulk DID order.
261 qsearch( 'did_order_item', { 'ordernum' => $self->ordernum } );
266 Returns all cust_main (see L<FS::cust_main>), if any, associated with this
273 my @did_order_item = $self->did_order_item;
275 push @custnums, $self->custnum if $self->custnum;
276 foreach my $did_order_item ( @did_order_item ) {
277 push @custnums, $did_order_item->custnum if $did_order_item->custnum;
280 foreach my $custnum ( @custnums ) {
281 push @cust_main, qsearchs('cust_main', { 'custnum' => $custnum } );
289 Returns true if and only if the order has any stock order items.
295 my $items_with_custnum = 0;
296 my @did_order_item = $self->did_order_item;
297 foreach my $did_order_item ( @did_order_item ) {
298 $items_with_custnum++ if $did_order_item->custnum;
301 return 0 if ($items_with_custnum == scalar(@did_order_item)
302 && $items_with_custnum != 0 && !$self->custnum)
310 Returns the provisioned DIDs, if any, as phone_avail (see L<FS::phone_avail>) objects.
316 qsearch({ table => 'phone_avail',
317 hashref => { 'ordernum' => $self->ordernum, },
318 select => 'phone_avail.*',
319 extra_sql => ' and svcnum is not null ',
327 L<FS::Record>, schema.html from the base documentation.