DID inventory/import / bulk DID orders - phase 2, RT12754
[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 =item merge SOURCE_ORDER
130
131 Merges the DID order given by SOURCE_ORDER into THIS order. 
132
133 The following fields from the source order are transferred, only if they aren't
134 set in this order:
135 -vendor order #
136 -submitted
137 -confirmed
138 -customer
139
140 DID order items are transferred into this order.
141
142 The source order is deleted.
143
144 The operation fails if:
145 -either order has a received time; or
146 -the DID vendors do not match between the orders
147
148 =cut
149
150 sub merge {
151     my $self = shift;
152     my $src = shift;
153     return "invalid source order" unless $src;
154
155     return "DIDs received for either order" 
156         if $src->received || $self->received;
157
158     return "DID vendors do not match"
159         if $src->vendornum != $self->vendornum;
160
161     my @move_if_unset = qw( vendor_order_id submitted confirmed custnum );
162     foreach my $f ( @move_if_unset ) {
163         $self->$f($src->$f) if !$self->$f;
164     }
165
166     my $error = '';
167     my @did_order_items = qsearch('did_order_item', { 'ordernum' => $src->ordernum });
168     foreach my $did_order_item ( @did_order_items ) {
169         $did_order_item->ordernum($self->ordernum);
170         $error = $did_order_item->replace;
171         return $error if $error;
172     }
173
174     $error = $src->delete;
175     return $error if !$error;
176
177     $error = $self->replace;
178     return $error if !$error;
179
180     '';
181 }
182
183 =item replace OLD_RECORD
184
185 Replaces the OLD_RECORD with this one in the database.  If there is an error,
186 returns the error, otherwise returns false.
187
188 =cut
189
190 # the replace method can be inherited from FS::Record
191
192 =item check
193
194 Checks all fields to make sure this is a valid bulk DID order.  If there is
195 an error, returns the error, otherwise returns false.  Called by the insert
196 and replace methods.
197
198 =cut
199
200 # the check method should currently be supplied - FS::Record contains some
201 # data checking routines
202
203 sub check {
204   my $self = shift;
205
206   my $error = 
207     $self->ut_numbern('ordernum')
208     || $self->ut_foreign_key('vendornum', 'did_vendor', 'vendornum' )
209     || $self->ut_textn('vendor_order_id')
210     || $self->ut_number('submitted')
211     || $self->ut_numbern('confirmed')
212     || $self->ut_numbern('received')
213   ;
214   return $error if $error;
215
216   $self->SUPER::check;
217 }
218
219 =item did_order_item
220
221 Returns the did_order_items (see L<FS::did_order_item>) associated with this bulk DID order.
222
223 =cut
224
225 sub did_order_item {
226   my $self = shift;
227   qsearch( 'did_order_item', { 'ordernum' => $self->ordernum } );
228 }
229
230 =item cust_main
231
232 Returns all cust_main (see L<FS::cust_main>), if any, associated with this
233 bulk DID order.
234
235 =cut
236
237 sub cust_main {
238   my $self = shift;
239   my @did_order_item = $self->did_order_item;
240   my @custnums;
241   push @custnums, $self->custnum if $self->custnum;
242   foreach my $did_order_item ( @did_order_item ) {
243        push @custnums, $did_order_item->custnum if $did_order_item->custnum; 
244   }
245   my @cust_main;
246   foreach my $custnum ( @custnums ) {
247       push @cust_main, qsearchs('cust_main', { 'custnum' => $custnum } );
248   }
249   @cust_main; 
250 }
251
252
253 =item has_stock 
254
255 Returns true if and only if the order has any stock order items.
256
257 =cut
258
259 sub has_stock {
260     my $self = shift;
261     my $items_with_custnum = 0;
262     my @did_order_item = $self->did_order_item;
263     foreach my $did_order_item ( @did_order_item ) {
264         $items_with_custnum++ if $did_order_item->custnum;
265     }
266
267     return 0 if ($items_with_custnum == scalar(@did_order_item) 
268                     && $items_with_custnum != 0 && !$self->custnum) 
269                 || $self->custnum;
270     1;
271 }
272
273
274 =item provisioned
275
276 Returns the provisioned DIDs, if any, as phone_avail (see L<FS::phone_avail>) objects.
277
278 =cut
279
280 sub provisioned {
281   my $self = shift;
282   qsearch({ table   => 'phone_avail',
283               hashref => { 'ordernum' => $self->ordernum, },
284               select  => 'phone_avail.*',
285               extra_sql => ' and svcnum is not null ',
286          });
287 }
288
289 =back
290
291 =head1 SEE ALSO
292
293 L<FS::Record>, schema.html from the base documentation.
294
295 =cut
296
297 1;
298