+ #|| $self->ut_snumber('seconds')
+ return $error if $error;
+
+ #quelle kludge
+ if ( $self->plandata =~ /^(agent_)?templatename\s+(.*)$/m ) {
+ my $name= $2;
+
+ foreach my $file (qw( template
+ latex latexnotes latexreturnaddress latexfooter
+ latexsmallfooter
+ html htmlnotes htmlreturnaddress htmlfooter
+ ))
+ {
+ unless ( $conf->exists("invoice_${file}_$name") ) {
+ $conf->set(
+ "invoice_${file}_$name" =>
+ join("\n", $conf->config("invoice_$file") )
+ );
+ }
+ }
+ }
+
+ if ($self->reason){
+ my $reasonr = qsearchs('reason', {'reasonnum' => $self->reason});
+ return "Unknown reason" unless $reasonr;
+ }
+
+ $self->SUPER::check;
+}
+
+=item templatename
+
+Returns the alternate invoice template name, if any, or false if there is
+no alternate template for this invoice event.
+
+=cut
+
+sub templatename {
+ my $self = shift;
+ if ( $self->plan =~ /^send_(alternate|agent)$/
+ && $self->plandata =~ /^(agent_)?templatename (.*)$/m
+ )
+ {
+ $2;
+ } else {
+ '';
+ }
+}
+
+=item due_events
+
+Returns the list of events due, if any, or false if there is none.
+Requires record and payby, but event_time and extra_sql are optional.
+
+=cut
+
+sub due_events {
+ my ($record, $payby, $event_time, $extra_sql) = @_;
+ my $interval = 0;
+ if ($record->_date){
+ $event_time = time unless $event_time;
+ $interval = $event_time - $record->_date;
+ }
+ sort { $a->seconds <=> $b->seconds
+ || $a->weight <=> $b->weight
+ || $a->eventpart <=> $b->eventpart }
+ grep { $_->seconds <= ( $interval )
+ && ! qsearch( 'cust_bill_event', {
+ 'invnum' => $record->get($record->dbdef_table->primary_key),
+ 'eventpart' => $_->eventpart,
+ 'status' => 'done',
+ } )
+ }
+ qsearch( {
+ 'table' => 'part_bill_event',
+ 'hashref' => { 'payby' => $payby,
+ 'disabled' => '', },
+ 'extra_sql' => $extra_sql,
+ } );
+
+
+}
+
+=item do_event
+
+Performs the event and returns any errors that occur.
+Requires a record on which to perform the event.
+Should only be performed inside a transaction.
+
+=cut
+
+sub do_event {
+ my ($self, $object, %options) = @_;
+ warn " calling event (". $self->eventcode. ") for " . $object->table . " " ,
+ $object->get($object->dbdef_table->primary_key) . "\n" if $DEBUG > 1;
+ my $oldAutoCommit = $FS::UID::AutoCommit;
+ local $FS::UID::AutoCommit = 0;
+
+ # for "callback" -- heh
+ my $cust_main = $object->cust_main;
+ my $cust_bill;
+ if ($object->table eq 'cust_bill'){
+ $cust_bill = $object;
+ }
+ my $cust_pay_batch;
+ if ($object->table eq 'cust_pay_batch'){
+ $cust_pay_batch = $object;
+ }
+
+ my $error;
+ {
+ local $SIG{__DIE__}; # don't want Mason __DIE__ handler active
+ $error = eval $self->eventcode;
+ }
+
+ my $status = '';
+ my $statustext = '';
+ if ( $@ ) {
+ $status = 'failed';
+ $statustext = $@;
+ } elsif ( $error ) {
+ $status = 'done';
+ $statustext = $error;
+ } else {
+ $status = 'done';
+ }
+
+ #add cust_bill_event
+ my $cust_bill_event = new FS::cust_bill_event {
+# 'invnum' => $object->get($object->dbdef_table->primary_key),
+ 'invnum' => $object->invnum,
+ 'eventpart' => $self->eventpart,
+ '_date' => time,
+ 'status' => $status,
+ 'statustext' => $statustext,
+ };
+ $error = $cust_bill_event->insert;
+ if ( $error ) {
+ my $e = 'WARNING: Event run but database not updated - '.
+ 'error inserting cust_bill_event, invnum #'. $object->invnum .
+ ', eventpart '. $self->eventpart.": $error";
+ warn $e;
+ return $e;
+ }
+ '';
+}
+
+=item reasontext
+
+Returns the text of any reason associated with this event.
+
+=cut
+
+sub reasontext {
+ my $self = shift;
+ my $r = qsearchs('reason', { 'reasonnum' => $self->reason });
+ if ($r){
+ $r->reason;
+ }else{
+ '';
+ }