This commit was generated by cvs2svn to compensate for changes in r4407,
[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_textn('statustext')
127   ;
128
129   return "Unknown invnum ". $self->invnum
130     unless qsearchs( 'cust_bill' ,{ 'invnum' => $self->invnum } );
131
132   return "Unknown eventpart ". $self->eventpart
133     unless qsearchs( 'part_bill_event' ,{ 'eventpart' => $self->eventpart } );
134
135   $self->SUPER::check;
136 }
137
138 =item part_bill_event
139
140 Returns the invoice event definition (see L<FS::part_bill_event>) for this
141 completed invoice event.
142
143 =cut
144
145 sub part_bill_event {
146   my $self = shift;
147   qsearchs( 'part_bill_event', { 'eventpart' => $self->eventpart } );
148 }
149
150 =item cust_bill
151
152 Returns the invoice (see L<FS::cust_bill>) for this completed invoice event.
153
154 =cut
155
156 sub cust_bill {
157   my $self = shift;
158   qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
159 }
160
161 =item retry
162
163 Changes the status of this event from B<done> to B<failed>, allowing it to be
164 retried.
165
166 =cut
167
168 sub retry {
169   my $self = shift;
170   return '' unless $self->status eq 'done';
171   my $old = ref($self)->new( { $self->hash } );
172   $self->status('failed');
173   $self->replace($old);
174 }
175
176 =back
177
178 =head1 SUBROUTINES
179
180 =over 4
181
182 =item reprint
183
184 =cut
185
186 sub process_reprint {
187   process_re_X('print', @_);
188 }
189
190 =item reemail
191
192 =cut
193
194 sub process_reemail {
195   process_re_X('email', @_);
196 }
197
198 =item refax
199
200 =cut
201
202 sub process_refax {
203   process_re_X('fax', @_);
204 }
205
206 use Storable qw(thaw);
207 use Data::Dumper;
208 use MIME::Base64;
209 sub process_re_X {
210   my( $method, $job ) = ( shift, shift );
211
212   my $param = thaw(decode_base64(shift));
213   warn Dumper($param) if $DEBUG;
214
215   re_X(
216     $method,
217     $param->{'beginning'},
218     $param->{'ending'},
219     $param->{'failed'},
220     $job,
221   );
222
223 }
224
225 sub re_X {
226   my($method, $beginning, $ending, $failed, $job) = @_;
227
228   my $where = " WHERE plan LIKE 'send%'".
229               "   AND cust_bill_event._date >= $beginning".
230               "   AND cust_bill_event._date <= $ending";
231   $where .= " AND statustext != '' AND statustext IS NOT NULL"
232     if $failed;
233
234   my $from = 'LEFT JOIN part_bill_event USING ( eventpart )';
235
236   my @cust_bill_event = qsearch( 'cust_bill_event', {}, '', $where, '', $from );
237
238   my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
239   foreach my $cust_bill_event ( @cust_bill_event ) {
240
241     $cust_bill_event->cust_bill->$method(
242       $cust_bill_event->part_bill_event->templatename
243     );
244
245     if ( $job ) { #progressbar foo
246       $num++;
247       if ( time - $min_sec > $last ) {
248         my $error = $job->update_statustext(
249           int( 100 * $num / scalar(@cust_bill_event) )
250         );
251         die $error if $error;
252         $last = time;
253       }
254     }
255
256   }
257
258   #this doesn't work, but it would be nice
259   #if ( $job ) { #progressbar foo
260   #  my $error = $job->update_statustext(
261   #    scalar(@cust_bill_event). " invoices re-${method}ed"
262   #  );
263   #  die $error if $error;
264   #}
265
266 }
267
268 =back
269
270 =head1 BUGS
271
272 Far too early in the morning.
273
274 =head1 SEE ALSO
275
276 L<FS::part_bill_event>, L<FS::cust_bill>, L<FS::Record>, schema.html from the
277 base documentation.
278
279 =cut
280
281 1;
282