enable CardFortress in test database, #71513
[freeside.git] / FS / FS / did_order.pm
1 package FS::did_order;
2 use base qw( FS::o2m_Common FS::Record );
3
4 use strict;
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
131 =item merge SOURCE_ORDER
132
133 Merges the DID order given by SOURCE_ORDER into THIS order. 
134
135 The following fields from the source order are transferred, only if they aren't
136 set in this order:
137 -vendor order #
138 -confirmed
139 -customer
140
141 DID order items are transferred into this order. Per-order customer is cleared
142 if any order items are assigned to a customer.
143
144 The source order is deleted.
145
146 The operation fails if:
147 -either order has a received time; or
148 -the DID vendors do not match between the orders
149
150 =cut
151
152 sub merge {
153     my $self = shift;
154     my $src = shift;
155     return "invalid source order" unless $src;
156
157     return "DIDs received for either order" 
158         if $src->received || $self->received;
159
160     return "DID vendors do not match"
161         if $src->vendornum != $self->vendornum;
162         
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';
169
170     my $oldAutoCommit = $FS::UID::AutoCommit;
171     local $FS::UID::AutoCommit = 0;
172     my $dbh = dbh;
173
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;
177     }
178
179     my $error = '';
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;
186         if ( $error ) {
187           $dbh->rollback if $oldAutoCommit;
188           return "can't replace did order item "
189                                   . $did_order_item->orderitemnum . ": $error";
190         }
191     }
192
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;
196     }
197
198     $self->custnum('') if $item_has_cust;
199
200     $error = $src->delete;
201     if ( $error ) {
202         $dbh->rollback if $oldAutoCommit;
203         return "can't delete source order: $error"; 
204     }
205
206     $error = $self->replace;
207     if ( $error ) {
208         $dbh->rollback if $oldAutoCommit;
209         return "can't replace target order: $error"; 
210     }
211
212     $dbh->commit or die $dbh->errstr if $oldAutoCommit;
213
214     '';
215 }
216
217 =item replace OLD_RECORD
218
219 Replaces the OLD_RECORD with this one in the database.  If there is an error,
220 returns the error, otherwise returns false.
221
222 =cut
223
224 # the replace method can be inherited from FS::Record
225
226 =item check
227
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
230 and replace methods.
231
232 =cut
233
234 # the check method should currently be supplied - FS::Record contains some
235 # data checking routines
236
237 sub check {
238   my $self = shift;
239
240   my $error = 
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')
247   ;
248   return $error if $error;
249
250   $self->SUPER::check;
251 }
252
253 =item did_order_item
254
255 Returns the did_order_items (see L<FS::did_order_item>) associated with this bulk DID order.
256
257 =item cust_main
258
259 Returns all cust_main (see L<FS::cust_main>), if any, associated with this
260 bulk DID order.
261
262 =cut
263
264 sub cust_main {
265   my $self = shift;
266   my @did_order_item = $self->did_order_item;
267   my @custnums;
268   push @custnums, $self->custnum if $self->custnum;
269   foreach my $did_order_item ( @did_order_item ) {
270        push @custnums, $did_order_item->custnum if $did_order_item->custnum; 
271   }
272   my @cust_main;
273   foreach my $custnum ( @custnums ) {
274       push @cust_main, qsearchs('cust_main', { 'custnum' => $custnum } );
275   }
276   @cust_main; 
277 }
278
279
280 =item has_stock 
281
282 Returns true if and only if the order has any stock order items.
283
284 =cut
285
286 sub has_stock {
287     my $self = shift;
288     my $items_with_custnum = 0;
289     my @did_order_item = $self->did_order_item;
290     foreach my $did_order_item ( @did_order_item ) {
291         $items_with_custnum++ if $did_order_item->custnum;
292     }
293
294     return 0 if ($items_with_custnum == scalar(@did_order_item) 
295                     && $items_with_custnum != 0 && !$self->custnum) 
296                 || $self->custnum;
297     1;
298 }
299
300
301 =item provisioned
302
303 Returns the provisioned DIDs, if any, as phone_avail (see L<FS::phone_avail>) objects.
304
305 =cut
306
307 sub provisioned {
308   my $self = shift;
309   qsearch({ table   => 'phone_avail',
310               hashref => { 'ordernum' => $self->ordernum, },
311               select  => 'phone_avail.*',
312               extra_sql => ' and svcnum is not null ',
313          });
314 }
315
316 =back
317
318 =head1 SEE ALSO
319
320 L<FS::Record>, schema.html from the base documentation.
321
322 =cut
323
324 1;
325