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 );
9 @ISA = qw( FS::Record );
10 @EXPORT_OK = qw( due_events );
15 FS::part_bill_event - Object methods for part_bill_event records
19 use FS::part_bill_event;
21 $record = new FS::part_bill_event \%hash;
22 $record = new FS::part_bill_event { 'column' => 'value' };
24 $error = $record->insert;
26 $error = $new_record->replace($old_record);
28 $error = $record->delete;
30 $error = $record->check;
32 $error = $record->do_event( $direct_object );
34 @events = due_events ( { 'record' => $event_triggering_record,
36 'event_time => $_date,
37 'extra_sql => $extra } );
41 An FS::part_bill_event object represents a deprecated, old-style invoice event
42 definition - a callback which is triggered when an invoice is a certain amount
43 of time overdue. FS::part_bill_event inherits from FS::Record. The following
44 fields are currently supported:
48 =item eventpart - primary key
50 =item payby - CARD, DCRD, CHEK, DCHK, LECB, BILL, or COMP
52 =item event - event name
54 =item eventcode - event action
56 =item seconds - how long after the invoice date events of this type are triggered
58 =item weight - ordering for events with identical seconds
60 =item plan - eventcode plan
62 =item plandata - additional plan data
64 =item reason - an associated reason for this event to fire
66 =item disabled - Disabled flag, empty or `Y'
72 Old-style invoice events are only useful for legacy migrations - if you are
73 looking for current events see L<FS::part_event>.
81 Creates a new invoice event definition. To add the invoice event definition to
82 the database, see L<"insert">.
84 Note that this stores the hash reference, not a distinct copy of the hash it
85 points to. You can ask the object for a copy with the I<hash> method.
89 # the new method can be inherited from FS::Record, if a table method is defined
91 sub table { 'part_bill_event'; }
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 invoice event definition. 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 $self->weight(0) unless $self->weight;
135 my $conf = new FS::Conf;
136 if ( $conf->exists('safe-part_bill_event') ) {
137 my $error = $self->ut_anything('eventcode');
138 return $error if $error;
140 my $c = $self->eventcode;
142 #yay, these regexen will go away with the event refactor
144 $c =~ /^\s*\$cust_main\->(suspend|cancel|invoicing_list_addpost|bill|collect)\(\);\s*("";)?\s*$/
146 or $c =~ /^\s*\$cust_bill\->(comp|realtime_(card|ach|lec)|batch_card|send)\((%options)*\);\s*$/
148 or $c =~ /^\s*\$cust_bill\->send(_if_newest)?\(\'[\w\-\s]+\'\s*(,\s*(\d+|\[\s*\d+(,\s*\d+)*\s*\])\s*,\s*'[\w\@\.\-\+]*'\s*)?\);\s*$/
150 # or $c =~ /^\s*\$cust_main\->apply_payments; \$cust_main->apply_credits; "";\s*$/
151 or $c =~ /^\s*\$cust_main\->apply_payments_and_credits; "";\s*$/
153 or $c =~ /^\s*\$cust_main\->charge\( \s*\d*\.?\d*\s*,\s*\'[\w \!\@\#\$\%\&\(\)\-\+\;\:\"\,\.\?\/]*\'\s*\);\s*$/
155 or $c =~ /^\s*\$cust_main\->suspend_(if|unless)_pkgpart\([\d\,\s]*\);\s*$/
157 or $c =~ /^\s*\$cust_bill\->cust_suspend_if_balance_over\([\d\.\s]*\);\s*$/
161 return "illegal eventcode: $c";
166 my $error = $self->ut_numbern('eventpart')
167 || $self->ut_enum('payby', [qw( CARD DCLN DCRD CHEK DCHK LECB BILL COMP )] )
168 || $self->ut_text('event')
169 || $self->ut_anything('eventcode')
170 || $self->ut_number('seconds')
171 || $self->ut_enum('disabled', [ '', 'Y' ] )
172 || $self->ut_number('weight')
173 || $self->ut_textn('plan')
174 || $self->ut_anything('plandata')
175 || $self->ut_numbern('reason')
177 #|| $self->ut_snumber('seconds')
178 return $error if $error;
181 if ( $self->plandata =~ /^(agent_)?templatename\s+(.*)$/m ) {
184 foreach my $file (qw( template
185 latex latexnotes latexreturnaddress latexfooter
187 html htmlnotes htmlreturnaddress htmlfooter
190 unless ( $conf->exists("invoice_${file}_$name") ) {
192 "invoice_${file}_$name" =>
193 join("\n", $conf->config("invoice_$file") )
200 my $reasonr = qsearchs('reason', {'reasonnum' => $self->reason});
201 return "Unknown reason" unless $reasonr;
209 Returns the alternate invoice template name, if any, or false if there is
210 no alternate template for this invoice event.
216 if ( $self->plan =~ /^send_(alternate|agent)$/
217 && $self->plandata =~ /^(agent_)?templatename (.*)$/m
228 Returns the list of events due, if any, or false if there is none.
229 Requires record and payby, but event_time and extra_sql are optional.
234 my ($record, $payby, $event_time, $extra_sql) = @_;
236 #cluck "DEPRECATED: FS::part_bill_event::due_events called on $record";
237 confess "DEPRECATED: FS::part_bill_event::due_events called on $record";
241 $event_time = time unless $event_time;
242 $interval = $event_time - $record->_date;
244 sort { $a->seconds <=> $b->seconds
245 || $a->weight <=> $b->weight
246 || $a->eventpart <=> $b->eventpart }
247 grep { $_->seconds <= ( $interval )
248 && ! qsearch( 'cust_bill_event', {
249 'invnum' => $record->get($record->dbdef_table->primary_key),
250 'eventpart' => $_->eventpart,
255 'table' => 'part_bill_event',
256 'hashref' => { 'payby' => $payby,
258 'extra_sql' => $extra_sql,
266 Performs the event and returns any errors that occur.
267 Requires a record on which to perform the event.
268 Should only be performed inside a transaction.
273 my ($self, $object, %options) = @_;
275 #cluck "DEPRECATED: FS::part_bill_event::do_event called on $self";
276 confess "DEPRECATED: FS::part_bill_event::do_event called on $self";
278 warn " calling event (". $self->eventcode. ") for " . $object->table . " " ,
279 $object->get($object->dbdef_table->primary_key) . "\n" if $DEBUG > 1;
280 my $oldAutoCommit = $FS::UID::AutoCommit;
281 local $FS::UID::AutoCommit = 0;
283 # for "callback" -- heh
284 my $cust_main = $object->cust_main;
286 if ($object->table eq 'cust_bill'){
287 $cust_bill = $object;
290 if ($object->table eq 'cust_pay_batch'){
291 $cust_pay_batch = $object;
296 local $SIG{__DIE__}; # don't want Mason __DIE__ handler active
297 $error = eval $self->eventcode;
307 $statustext = $error;
313 my $cust_bill_event = new FS::cust_bill_event {
314 # 'invnum' => $object->get($object->dbdef_table->primary_key),
315 'invnum' => $object->invnum,
316 'eventpart' => $self->eventpart,
319 'statustext' => $statustext,
321 $error = $cust_bill_event->insert;
323 my $e = 'WARNING: Event run but database not updated - '.
324 'error inserting cust_bill_event, invnum #'. $object->invnum .
325 ', eventpart '. $self->eventpart.": $error";
334 Returns the text of any reason associated with this event.
340 my $r = qsearchs('reason', { 'reasonnum' => $self->reason });
352 The whole "eventcode" idea is bunk. This should be refactored with subclasses
353 like part_pkg/ and part_export/
357 L<FS::cust_bill>, L<FS::cust_bill_event>, L<FS::Record>, schema.html from the