fix up re-email these events
[freeside.git] / FS / FS / cust_bill_event.pm
1 package FS::cust_bill_event;
2
3 use strict;
4 use vars qw( @ISA $DEBUG );
5 use FS::Record qw( qsearch qsearchs );
6 use FS::cust_main_Mixin;
7 use FS::cust_bill;
8 use FS::part_bill_event;
9
10 @ISA = qw(FS::cust_main_Mixin FS::Record);
11
12 $DEBUG = 0;
13
14 =head1 NAME
15
16 FS::cust_bill_event - Object methods for cust_bill_event records
17
18 =head1 SYNOPSIS
19
20   use FS::cust_bill_event;
21
22   $record = new FS::cust_bill_event \%hash;
23   $record = new FS::cust_bill_event { 'column' => 'value' };
24
25   $error = $record->insert;
26
27   $error = $new_record->replace($old_record);
28
29   $error = $record->delete;
30
31   $error = $record->check;
32
33 =head1 DESCRIPTION
34
35 An FS::cust_bill_event object represents an complete invoice event.
36 FS::cust_bill_event inherits from FS::Record.  The following fields are
37 currently supported:
38
39 =over 4
40
41 =item eventnum - primary key
42
43 =item invnum - invoice (see L<FS::cust_bill>)
44
45 =item eventpart - event definition (see L<FS::part_bill_event>)
46
47 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
48 L<Time::Local> and L<Date::Parse> for conversion functions.
49
50 =item status - event status: B<done> or B<failed>
51
52 =item statustext - additional status detail (i.e. error message)
53
54 =back
55
56 =head1 METHODS
57
58 =over 4
59
60 =item new HASHREF
61
62 Creates a new completed invoice event.  To add the compelted invoice event to
63 the database, see L<"insert">.
64
65 Note that this stores the hash reference, not a distinct copy of the hash it
66 points to.  You can ask the object for a copy with the I<hash> method.
67
68 =cut
69
70 # the new method can be inherited from FS::Record, if a table method is defined
71
72 sub table { 'cust_bill_event'; }
73
74 sub cust_linked { $_[0]->cust_main_custnum; } 
75 sub cust_unlinked_msg {
76   my $self = shift;
77   "WARNING: can't find cust_main.custnum ". $self->custnum.
78   ' (cust_bill.invnum '. $self->invnum. ')';
79 }
80
81 =item insert
82
83 Adds this record to the database.  If there is an error, returns the error,
84 otherwise returns false.
85
86 =cut
87
88 # the insert method can be inherited from FS::Record
89
90 =item delete
91
92 Delete this record from the database.
93
94 =cut
95
96 # the delete method can be inherited from FS::Record
97
98 =item replace OLD_RECORD
99
100 Replaces the OLD_RECORD with this one in the database.  If there is an error,
101 returns the error, otherwise returns false.
102
103 =cut
104
105 # the replace method can be inherited from FS::Record
106
107 =item check
108
109 Checks all fields to make sure this is a valid completed invoice event.  If
110 there is an error, returns the error, otherwise returns false.  Called by the
111 insert and replace methods.
112
113 =cut
114
115 # the check method should currently be supplied - FS::Record contains some
116 # data checking routines
117
118 sub check {
119   my $self = shift;
120
121   my $error = $self->ut_numbern('eventnum')
122     || $self->ut_number('invnum')
123     || $self->ut_number('eventpart')
124     || $self->ut_number('_date')
125     || $self->ut_enum('status', [qw( done failed )])
126     || $self->ut_anything('statustext')
127   ;
128
129   return "Unknown eventpart ". $self->eventpart
130     unless my $part_bill_event =
131       qsearchs( 'part_bill_event' ,{ 'eventpart' => $self->eventpart } );
132
133   return "Unknown invnum ". $self->invnum
134     unless qsearchs( 'cust_bill' ,{ 'invnum' => $self->invnum } );
135
136   $self->SUPER::check;
137 }
138
139 =item part_bill_event
140
141 Returns the invoice event definition (see L<FS::part_bill_event>) for this
142 completed invoice event.
143
144 =cut
145
146 sub part_bill_event {
147   my $self = shift;
148   qsearchs( 'part_bill_event', { 'eventpart' => $self->eventpart } );
149 }
150
151 =item cust_bill
152
153 Returns the invoice (see L<FS::cust_bill>) for this completed invoice event.
154
155 =cut
156
157 sub cust_bill {
158   my $self = shift;
159   qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
160 }
161
162 =item retry
163
164 Changes the status of this event from B<done> to B<failed>, allowing it to be
165 retried.
166
167 =cut
168
169 sub retry {
170   my $self = shift;
171   return '' unless $self->status eq 'done';
172   my $old = ref($self)->new( { $self->hash } );
173   $self->status('failed');
174   $self->replace($old);
175 }
176
177 =item retryable
178
179 Changes the statustext of this event to B<retriable>, rendering it 
180 retriable (should retry be called).
181
182 =cut
183
184 sub retriable {
185   my $self = shift;
186   return '' unless $self->status eq 'done';
187   my $old = ref($self)->new( { $self->hash } );
188   $self->statustext('retriable');
189   $self->replace($old);
190 }
191
192 =item search_sql HREF
193
194 Class method which returns an SQL WHERE fragment to search for parameters
195 specified in HREF.  Valid parameters are
196
197 =over 4
198 =item agentnum
199 =item beginning - an epoch date setting a lower bound for _date values
200 =item ending - an epoch date setting a upper bound for _date values
201 =item failed - limits the search to failed events if true
202 =item payby - requires that the search be JOIN'd to part_bill_event # Bug?
203 =item invnum 
204 =item currentuser - specifies the user for agent virtualization
205 =back
206
207 =cut
208
209 sub search_sql {
210   my ($class, $params) = @_;
211   my @search = ();
212
213   push @search, "agentnum = ". $params->{agentnum} if $params->{agentnum};
214
215   push @search, "cust_bill_event._date >= ". $params->{beginning}
216     if $params->{beginning};
217   push @search, "cust_bill_event._date <= ". $params->{ending}
218     if $params->{ending};
219
220   push @search, "statustext != ''",
221                 "statustext IS NOT NULL",
222                 "statustext != 'N/A'"
223     if $params->{failed};
224
225   push @search, "part_bill_event.payby = '". $params->{payby}. "'"
226     if $params->{payby};
227
228   push @search, "cust_bill_event.invnum = '". $params->{invnum}. "'"
229     if $params->{invnum};
230
231   if ($params->{CurrentUser}) {
232     my $access_user = qsearchs('access_user',
233                               {username => $params->{CurrentUser} }
234                              );
235     if ($access_user) {
236       push @search, $access_user->agentnums_sql;
237     }else{
238       push @search, "1=0";
239     }
240   }else{
241     push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
242   }
243
244   join(' AND ', @search );
245
246 }
247
248 =back
249
250 =head1 SUBROUTINES
251
252 =over 4
253
254 =item reprint
255
256 =cut
257
258 sub process_reprint {
259   process_re_X('print', @_);
260 }
261
262 =item reemail
263
264 =cut
265
266 sub process_reemail {
267   process_re_X('email', @_);
268 }
269
270 =item refax
271
272 =cut
273
274 sub process_refax {
275   process_re_X('fax', @_);
276 }
277
278 use Storable qw(thaw);
279 use Data::Dumper;
280 use MIME::Base64;
281 sub process_re_X {
282   my( $method, $job ) = ( shift, shift );
283
284   my $param = thaw(decode_base64(shift));
285   warn Dumper($param) if $DEBUG;
286
287   re_X(
288     $method,
289     $param,
290     $job,
291   );
292
293 }
294
295 sub re_X {
296   my($method, $param, $job) = @_;
297
298   my $where = FS::cust_bill_event->search_sql($param);
299   $where = " WHERE plan LIKE 'send%'". ( $where ? " AND $where" : "" );
300
301   my $from = 'LEFT JOIN part_bill_event USING ( eventpart )'.
302              'LEFT JOIN cust_bill       USING ( invnum )'.
303              'LEFT JOIN cust_main       USING ( custnum )';
304
305   my @cust_bill_event = qsearch( 'cust_bill_event', {}, '', $where, '', $from );
306
307   my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
308   foreach my $cust_bill_event ( @cust_bill_event ) {
309
310     $cust_bill_event->cust_bill->$method(
311       $cust_bill_event->part_bill_event->templatename
312     );
313
314     if ( $job ) { #progressbar foo
315       $num++;
316       if ( time - $min_sec > $last ) {
317         my $error = $job->update_statustext(
318           int( 100 * $num / scalar(@cust_bill_event) )
319         );
320         die $error if $error;
321         $last = time;
322       }
323     }
324
325   }
326
327   #this doesn't work, but it would be nice
328   #if ( $job ) { #progressbar foo
329   #  my $error = $job->update_statustext(
330   #    scalar(@cust_bill_event). " invoices re-${method}ed"
331   #  );
332   #  die $error if $error;
333   #}
334
335 }
336
337 =back
338
339 =head1 BUGS
340
341 Far too early in the morning.
342
343 =head1 SEE ALSO
344
345 L<FS::part_bill_event>, L<FS::cust_bill>, L<FS::Record>, schema.html from the
346 base documentation.
347
348 =cut
349
350 1;
351