NENA2 E911 export and batch-oriented exports in general, #14049
[freeside.git] / FS / FS / export_batch.pm
1 package FS::export_batch;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6 use FS::part_export;
7 use FS::export_batch_item;
8
9 =head1 NAME
10
11 FS::export_batch - Object methods for export_batch records
12
13 =head1 SYNOPSIS
14
15   use FS::export_batch;
16
17   $record = new FS::export_batch \%hash;
18   $record = new FS::export_batch { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
30 An FS::export_batch object represents a batch of records being processed
31 by an export.  This mechanism allows exports to process multiple pending
32 service changes at the end of day or some other scheduled time, rather 
33 than doing everything in realtime or near-realtime (via the job queue).
34
35 FS::export_batch inherits from FS::Record.  The following fields are 
36 currently supported:
37
38 =over 4
39
40 =item batchnum
41
42 primary key
43
44 =item exportnum
45
46 The L<FS::part_export> object that created this batch.
47
48 =item _date
49
50 The time the batch was created.
51
52 =item status
53
54 A status string.  Allowed values are "open" (for a newly created batch that
55 can receive additional items), "closed" (for a batch that is no longer 
56 allowed to receive items but is still being processed), "done" (for a batch
57 that is finished processing), and "failed" (if there has been an error 
58 exporting the batch).
59
60 =item statustext
61
62 Free-text field for any status information from the remote machine or whatever
63 else the export is doing.  If status is "failed" this MUST contain a value.
64
65 =back
66
67 =head1 METHODS
68
69 =over 4
70
71 =item new HASHREF
72
73 Creates a new batch.  To add the example to the database, see L<"insert">.
74
75 Note that this stores the hash reference, not a distinct copy of the hash it
76 points to.  You can ask the object for a copy with the I<hash> method.
77
78 =cut
79
80 sub table { 'export_batch'; }
81
82 =item insert
83
84 Adds this record to the database.  If there is an error, returns the error,
85 otherwise returns false.
86
87 =item delete
88
89 Delete this record from the database.  Don't ever do this.
90
91 =item replace OLD_RECORD
92
93 Replaces the OLD_RECORD with this one in the database.  If there is an error,
94 returns the error, otherwise returns false.
95
96 =item check
97
98 Checks all fields to make sure this is a valid batch.  If there is
99 an error, returns the error, otherwise returns false.  Called by the insert
100 and replace methods.
101
102 =cut
103
104 sub check {
105   my $self = shift;
106
107   $self->set('status' => 'open') unless $self->get('status');
108   $self->set('_date' => time) unless $self->get('_date');
109
110   my $error = 
111     $self->ut_numbern('batchnum')
112     || $self->ut_number('exportnum')
113     || $self->ut_foreign_key('exportnum', 'part_export', 'exportnum')
114     || $self->ut_number('_date')
115     || $self->ut_enum('status', [ qw(open closed done failed) ])
116     || $self->ut_textn('statustext')
117   ;
118   return $error if $error;
119
120   $self->SUPER::check;
121 }
122
123 # stubs, removed in 4.x
124
125 sub export_batch_item {
126   my $self = shift;
127   qsearch('export_batch_item', { batchnum => $self->batchnum });
128 }
129
130 sub part_export {
131   my $self = shift;
132   FS::part_export->by_key($self->exportnum);
133 }
134
135 =back
136
137 =head1 BUGS
138
139 =head1 SEE ALSO
140
141 L<FS::part_export>, L<FS::export_batch_item>
142
143 =cut
144
145 1;
146