1 package FS::part_bill_event;
4 use vars qw( @ISA $DEBUG @EXPORT_OK );
5 use Carp qw(cluck confess);
6 use FS::Record qw( dbh qsearch qsearchs );
11 @ISA = qw( FS::Record );
12 @EXPORT_OK = qw( due_events );
17 FS::part_bill_event - Object methods for part_bill_event records
21 use FS::part_bill_event;
23 $record = new FS::part_bill_event \%hash;
24 $record = new FS::part_bill_event { 'column' => 'value' };
26 $error = $record->insert;
28 $error = $new_record->replace($old_record);
30 $error = $record->delete;
32 $error = $record->check;
34 $error = $record->do_event( $direct_object );
36 @events = due_events ( { 'record' => $event_triggering_record,
38 'event_time => $_date,
39 'extra_sql => $extra } );
43 An FS::part_bill_event object represents a deprecated, old-style invoice event
44 definition - a callback which is triggered when an invoice is a certain amount
45 of time overdue. FS::part_bill_event inherits from FS::Record. The following
46 fields are currently supported:
50 =item eventpart - primary key
52 =item payby - CARD, DCRD, CHEK, DCHK, LECB, BILL, or COMP
54 =item event - event name
56 =item eventcode - event action
58 =item seconds - how long after the invoice date events of this type are triggered
60 =item weight - ordering for events with identical seconds
62 =item plan - eventcode plan
64 =item plandata - additional plan data
66 =item reason - an associated reason for this event to fire
68 =item disabled - Disabled flag, empty or `Y'
74 Old-style invoice events are only useful for legacy migrations - if you are
75 looking for current events see L<FS::part_event>.
83 Creates a new invoice event definition. To add the invoice event definition to
84 the database, see L<"insert">.
86 Note that this stores the hash reference, not a distinct copy of the hash it
87 points to. You can ask the object for a copy with the I<hash> method.
91 # the new method can be inherited from FS::Record, if a table method is defined
93 sub table { 'part_bill_event'; }
97 Adds this record to the database. If there is an error, returns the error,
98 otherwise returns false.
102 # the insert method can be inherited from FS::Record
106 Delete this record from the database.
110 # the delete method can be inherited from FS::Record
112 =item replace OLD_RECORD
114 Replaces the OLD_RECORD with this one in the database. If there is an error,
115 returns the error, otherwise returns false.
119 # the replace method can be inherited from FS::Record
123 Checks all fields to make sure this is a valid invoice event definition. If
124 there is an error, returns the error, otherwise returns false. Called by the
125 insert and replace methods.
129 # the check method should currently be supplied - FS::Record contains some
130 # data checking routines
135 $self->weight(0) unless $self->weight;
137 my $conf = new FS::Conf;
138 if ( $conf->exists('safe-part_bill_event') ) {
139 my $error = $self->ut_anything('eventcode');
140 return $error if $error;
142 my $c = $self->eventcode;
144 #yay, these regexen will go away with the event refactor
146 $c =~ /^\s*\$cust_main\->(suspend|cancel|invoicing_list_addpost|bill|collect)\(\);\s*("";)?\s*$/
148 or $c =~ /^\s*\$cust_bill\->(comp|realtime_(card|ach|lec)|batch_card|send)\((%options)*\);\s*$/
150 or $c =~ /^\s*\$cust_bill\->send(_if_newest)?\(\'[\w\-\s]+\'\s*(,\s*(\d+|\[\s*\d+(,\s*\d+)*\s*\])\s*,\s*'[\w\@\.\-\+]*'\s*)?\);\s*$/
152 # or $c =~ /^\s*\$cust_main\->apply_payments; \$cust_main->apply_credits; "";\s*$/
153 or $c =~ /^\s*\$cust_main\->apply_payments_and_credits; "";\s*$/
155 or $c =~ /^\s*\$cust_main\->charge\( \s*\d*\.?\d*\s*,\s*\'[\w \!\@\#\$\%\&\(\)\-\+\;\:\"\,\.\?\/]*\'\s*\);\s*$/
157 or $c =~ /^\s*\$cust_main\->suspend_(if|unless)_pkgpart\([\d\,\s]*\);\s*$/
159 or $c =~ /^\s*\$cust_bill\->cust_suspend_if_balance_over\([\d\.\s]*\);\s*$/
163 return "illegal eventcode: $c";
168 my $error = $self->ut_numbern('eventpart')
169 || $self->ut_enum('payby', [qw( CARD DCLN DCRD CHEK DCHK LECB BILL COMP )] )
170 || $self->ut_text('event')
171 || $self->ut_anything('eventcode')
172 || $self->ut_number('seconds')
173 || $self->ut_enum('disabled', [ '', 'Y' ] )
174 || $self->ut_number('weight')
175 || $self->ut_textn('plan')
176 || $self->ut_anything('plandata')
177 || $self->ut_numbern('reason')
179 #|| $self->ut_snumber('seconds')
180 return $error if $error;
183 if ( $self->plandata =~ /^(agent_)?templatename\s+(.*)$/m ) {
186 foreach my $file (qw( template
187 latex latexnotes latexreturnaddress latexfooter
189 html htmlnotes htmlreturnaddress htmlfooter
192 unless ( $conf->exists("invoice_${file}_$name") ) {
194 "invoice_${file}_$name" =>
195 join("\n", $conf->config("invoice_$file") )
202 my $reasonr = qsearchs('reason', {'reasonnum' => $self->reason});
203 return "Unknown reason" unless $reasonr;
211 Returns the alternate invoice template name, if any, or false if there is
212 no alternate template for this invoice event.
218 if ( $self->plan =~ /^send_(alternate|agent)$/
219 && $self->plandata =~ /^(agent_)?templatename (.*)$/m
230 Returns the list of events due, if any, or false if there is none.
231 Requires record and payby, but event_time and extra_sql are optional.
236 my ($record, $payby, $event_time, $extra_sql) = @_;
238 #cluck "DEPRECATED: FS::part_bill_event::due_events called on $record";
239 confess "DEPRECATED: FS::part_bill_event::due_events called on $record";
243 $event_time = time unless $event_time;
244 $interval = $event_time - $record->_date;
246 sort { $a->seconds <=> $b->seconds
247 || $a->weight <=> $b->weight
248 || $a->eventpart <=> $b->eventpart }
249 grep { ref($record) ne 'FS::cust_bill' || $_->eventcode !~ /honor_dundate/
250 || $event_time > $record->cust_main->dundate
252 grep { $_->seconds <= ( $interval )
253 && ! qsearch( 'cust_bill_event', {
254 'invnum' => $record->get($record->dbdef_table->primary_key),
255 'eventpart' => $_->eventpart,
260 'table' => 'part_bill_event',
261 'hashref' => { 'payby' => $payby,
263 'extra_sql' => $extra_sql,
271 Performs the event and returns any errors that occur.
272 Requires a record on which to perform the event.
273 Should only be performed inside a transaction.
278 my ($self, $object, %options) = @_;
280 #cluck "DEPRECATED: FS::part_bill_event::do_event called on $self";
281 confess "DEPRECATED: FS::part_bill_event::do_event called on $self";
283 warn " calling event (". $self->eventcode. ") for " . $object->table . " " ,
284 $object->get($object->dbdef_table->primary_key) . "\n" if $DEBUG > 1;
285 my $oldAutoCommit = $FS::UID::AutoCommit;
286 local $FS::UID::AutoCommit = 0;
288 # for "callback" -- heh
289 my $cust_main = $object->cust_main;
291 if ($object->table eq 'cust_bill'){
292 $cust_bill = $object;
295 if ($object->table eq 'cust_pay_batch'){
296 $cust_pay_batch = $object;
301 local $SIG{__DIE__}; # don't want Mason __DIE__ handler active
302 $error = eval $self->eventcode;
312 $statustext = $error;
318 my $cust_bill_event = new FS::cust_bill_event {
319 # 'invnum' => $object->get($object->dbdef_table->primary_key),
320 'invnum' => $object->invnum,
321 'eventpart' => $self->eventpart,
324 'statustext' => $statustext,
326 $error = $cust_bill_event->insert;
328 my $e = 'WARNING: Event run but database not updated - '.
329 'error inserting cust_bill_event, invnum #'. $object->invnum .
330 ', eventpart '. $self->eventpart.": $error";
339 Returns the text of any reason associated with this event.
345 my $r = qsearchs('reason', { 'reasonnum' => $self->reason });
357 The whole "eventcode" idea is bunk. This should be refactored with subclasses
358 like part_pkg/ and part_export/
362 L<FS::cust_bill>, L<FS::cust_bill_event>, L<FS::Record>, schema.html from the