1 package FS::cust_bill_event;
4 use vars qw( @ISA $DEBUG );
5 use FS::Record qw( qsearch qsearchs );
6 use FS::cust_main_Mixin;
8 use FS::part_bill_event;
10 @ISA = qw(FS::cust_main_Mixin FS::Record);
16 FS::cust_bill_event - Object methods for cust_bill_event records
20 use FS::cust_bill_event;
22 $record = new FS::cust_bill_event \%hash;
23 $record = new FS::cust_bill_event { 'column' => 'value' };
25 $error = $record->insert;
27 $error = $new_record->replace($old_record);
29 $error = $record->delete;
31 $error = $record->check;
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
47 Invoice (see L<FS::cust_bill>)
51 Event definition (see L<FS::part_bill_event>)
55 Specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
56 L<Time::Local> and L<Date::Parse> for conversion functions.
60 Event status: B<done> or B<failed>
64 Additional status detail (i.e. error message)
74 Creates a new completed invoice event. To add the compelted invoice event to
75 the database, see L<"insert">.
77 Note that this stores the hash reference, not a distinct copy of the hash it
78 points to. You can ask the object for a copy with the I<hash> method.
82 # the new method can be inherited from FS::Record, if a table method is defined
84 sub table { 'cust_bill_event'; }
86 sub cust_linked { $_[0]->cust_main_custnum; }
87 sub cust_unlinked_msg {
89 "WARNING: can't find cust_main.custnum ". $self->custnum.
90 ' (cust_bill.invnum '. $self->invnum. ')';
95 Adds this record to the database. If there is an error, returns the error,
96 otherwise returns false.
100 # the insert method can be inherited from FS::Record
104 Delete this record from the database.
108 # the delete method can be inherited from FS::Record
110 =item replace OLD_RECORD
112 Replaces the OLD_RECORD with this one in the database. If there is an error,
113 returns the error, otherwise returns false.
117 # the replace method can be inherited from FS::Record
121 Checks all fields to make sure this is a valid completed invoice event. If
122 there is an error, returns the error, otherwise returns false. Called by the
123 insert and replace methods.
127 # the check method should currently be supplied - FS::Record contains some
128 # data checking routines
133 my $error = $self->ut_numbern('eventnum')
134 || $self->ut_number('invnum')
135 || $self->ut_number('eventpart')
136 || $self->ut_number('_date')
137 || $self->ut_enum('status', [qw( done failed )])
138 || $self->ut_anything('statustext')
141 return "Unknown eventpart ". $self->eventpart
142 unless my $part_bill_event =
143 qsearchs( 'part_bill_event' ,{ 'eventpart' => $self->eventpart } );
145 return "Unknown invnum ". $self->invnum
146 unless qsearchs( 'cust_bill' ,{ 'invnum' => $self->invnum } );
151 =item part_bill_event
153 Returns the invoice event definition (see L<FS::part_bill_event>) for this
154 completed invoice event.
158 sub part_bill_event {
160 qsearchs( 'part_bill_event', { 'eventpart' => $self->eventpart } );
165 Returns the invoice (see L<FS::cust_bill>) for this completed invoice event.
171 qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
176 Changes the status of this event from B<done> to B<failed>, allowing it to be
183 return '' unless $self->status eq 'done';
184 my $old = ref($self)->new( { $self->hash } );
185 $self->status('failed');
186 $self->replace($old);
191 Changes the statustext of this event to B<retriable>, rendering it
192 retriable (should retry be called).
198 return '' unless $self->status eq 'done';
199 my $old = ref($self)->new( { $self->hash } );
200 $self->statustext('retriable');
201 $self->replace($old);
204 =item search_sql_where HASHREF
206 Class method which returns an SQL WHERE fragment to search for parameters
207 specified in HASHREF. Valid parameters are
215 An epoch date setting a lower bound for _date values
219 An epoch date setting a upper bound for _date values
223 Limits the search to failed events if true
227 Requires that the search be JOIN'd to part_bill_event # Bug?
233 Specifies the user for agent virtualization
239 sub search_sql_where {
240 my ($class, $params) = @_;
243 push @search, "agentnum = ". $params->{agentnum} if $params->{agentnum};
245 push @search, "cust_bill_event._date >= ". $params->{beginning}
246 if $params->{beginning};
247 push @search, "cust_bill_event._date <= ". $params->{ending}
248 if $params->{ending};
250 push @search, "statustext != ''",
251 "statustext IS NOT NULL",
252 "statustext != 'N/A'"
253 if $params->{failed};
255 push @search, "part_bill_event.payby = '". $params->{payby}. "'"
258 push @search, "cust_bill_event.invnum = '". $params->{invnum}. "'"
259 if $params->{invnum};
261 my $currentuser = $params->{currentuser} || $params->{CurrentUser};
263 my $access_user = qsearchs('access_user', { username => $currentuser });
265 push @search, $access_user->agentnums_sql;
270 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
273 join(' AND ', @search );
287 sub process_reprint {
288 process_re_X('print', @_);
295 sub process_reemail {
296 process_re_X('email', @_);
304 process_re_X('fax', @_);
307 use Storable qw(thaw);
311 my( $method, $job ) = ( shift, shift );
313 my $param = thaw(decode_base64(shift));
314 warn Dumper($param) if $DEBUG;
325 my($method, $param, $job) = @_;
327 my $where = FS::cust_bill_event->search_sql_where($param);
328 $where = " WHERE plan LIKE 'send%'". ( $where ? " AND $where" : "" );
330 my $from = 'LEFT JOIN part_bill_event USING ( eventpart )'.
331 'LEFT JOIN cust_bill USING ( invnum )'.
332 'LEFT JOIN cust_main USING ( custnum )';
334 my @cust_bill_event = qsearch( 'cust_bill_event', {}, '', $where, '', $from );
336 my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
337 foreach my $cust_bill_event ( @cust_bill_event ) {
339 $cust_bill_event->cust_bill->$method(
340 $cust_bill_event->part_bill_event->templatename
343 if ( $job ) { #progressbar foo
345 if ( time - $min_sec > $last ) {
346 my $error = $job->update_statustext(
347 int( 100 * $num / scalar(@cust_bill_event) )
349 die $error if $error;
356 #this doesn't work, but it would be nice
357 #if ( $job ) { #progressbar foo
358 # my $error = $job->update_statustext(
359 # scalar(@cust_bill_event). " invoices re-${method}ed"
361 # die $error if $error;
370 Far too early in the morning.
374 L<FS::part_bill_event>, L<FS::cust_bill>, L<FS::Record>, schema.html from the