event refactor, landing on HEAD!
[freeside.git] / FS / FS / part_event.pm
1 package FS::part_event;
2
3 use strict;
4 use vars qw( @ISA $DEBUG );
5 use Carp qw(confess);
6 use FS::Record qw( dbh qsearch qsearchs );
7 use FS::option_Common;
8 use FS::m2name_Common;
9 use FS::Conf;
10 use FS::part_event_option;
11 use FS::part_event_condition;
12 use FS::cust_event;
13 use FS::agent;
14
15 @ISA = qw( FS::m2name_Common FS::option_Common ); # FS::Record );
16 $DEBUG = 0;
17
18 =head1 NAME
19
20 FS::part_event - Object methods for part_event records
21
22 =head1 SYNOPSIS
23
24   use FS::part_event;
25
26   $record = new FS::part_event \%hash;
27   $record = new FS::part_event { 'column' => 'value' };
28
29   $error = $record->insert( { 'option' => 'value' } );
30   $error = $record->insert( \%options );
31
32   $error = $new_record->replace($old_record);
33
34   $error = $record->delete;
35
36   $error = $record->check;
37
38   $error = $record->do_event( $direct_object );
39   
40 =head1 DESCRIPTION
41
42 An FS::part_event object represents an event definition - a billing, collection
43 or other callback which is triggered when certain customer, invoice, package or
44 other conditions are met.  FS::part_event inherits from FS::Record.  The
45 following fields are currently supported:
46
47 =over 4
48
49 =item eventpart - primary key
50
51 =item agentnum - Optional agentnum (see L<FS::agent>)
52
53 =item event - event name
54
55 =item eventtable - table name against which this event is triggered; currently "cust_bill" (the traditional invoice events), "cust_main" (customer events) or "cust_pkg (package events)
56
57 =item check_freq - how often events of this type are checked; currently "1d" (daily) and "1m" (monthly) are recognized.  Note that the apprioriate freeside-daily and/or freeside-monthly cron job needs to be in place.
58
59 =item weight - ordering for events
60
61 =item action - event action (like part_bill_event.plan - eventcode plan)
62
63 =item disabled - Disabled flag, empty or `Y'
64
65 =back
66
67 =head1 METHODS
68
69 =over 4
70
71 =item new HASHREF
72
73 Creates a new invoice event definition.  To add the invoice event definition to
74 the database, see L<"insert">.
75
76 Note that this stores the hash reference, not a distinct copy of the hash it
77 points to.  You can ask the object for a copy with the I<hash> method.
78
79 =cut
80
81 # the new method can be inherited from FS::Record, if a table method is defined
82
83 sub table { 'part_event'; }
84
85 =item insert [ HASHREF ]
86
87 Adds this record to the database.  If there is an error, returns the error,
88 otherwise returns false.
89
90 If a list or hash reference of options is supplied, part_export_option records
91 are created (see L<FS::part_event_option>).
92
93 =cut
94
95 # the insert method can be inherited from FS::Record
96
97 =item delete
98
99 Delete this record from the database.
100
101 =cut
102
103 # the delete method can be inherited from FS::Record
104
105 =item replace OLD_RECORD [ HASHREF | OPTION => VALUE ... ]
106
107 Replaces the OLD_RECORD with this one in the database.  If there is an error,
108 returns the error, otherwise returns false.
109
110 If a list or hash reference of options is supplied, part_event_option
111 records are created or modified (see L<FS::part_event_option>).
112
113 =cut
114
115 # the replace method can be inherited from FS::Record
116
117 =item check
118
119 Checks all fields to make sure this is a valid invoice event definition.  If
120 there is an error, returns the error, otherwise returns false.  Called by the
121 insert and replace methods.
122
123 =cut
124
125 # the check method should currently be supplied - FS::Record contains some
126 # data checking routines
127
128 sub check {
129   my $self = shift;
130
131   $self->weight(0) unless $self->weight;
132
133   my $error = 
134        $self->ut_numbern('eventpart')
135     || $self->ut_text('event')
136     || $self->ut_enum('eventtable', [ 'cust_bill', 'cust_main', 'cust_pkg' ] )
137     || $self->ut_enum('check_freq', [ '1d', '1m' ])
138     || $self->ut_number('weight')
139     || $self->ut_alpha('action')
140     || $self->ut_enum('disabled', [ '', 'Y' ] )
141   ;
142   return $error if $error;
143
144   #XXX check action to make sure a module exists?
145   # well it'll die in _rebless...
146
147   $self->SUPER::check;
148 }
149
150 =item _rebless
151
152 Reblesses the object into the FS::part_event::Action::ACTION class, where
153 ACTION is the object's I<action> field.
154
155 =cut
156
157 sub _rebless {
158   my $self = shift;
159   my $action = $self->action or return $self;
160   #my $class = ref($self). "::$action";
161   my $class = "FS::part_event::Action::$action";
162   eval "use $class";
163   die $@ if $@;
164   bless($self, $class); # unless $@;
165   $self;
166 }
167
168 =item part_event_condition
169
170 Returns the conditions associated with this event, as FS::part_event_condition
171 objects (see L<FS::part_event_condition>)
172
173 =cut
174
175 sub part_event_condition {
176   my $self = shift;
177   qsearch( 'part_event_condition', { 'eventpart' => $self->eventpart } );
178 }
179
180 =item new_cust_event OBJECT
181
182 Creates a new customer event (see L<FS::cust_event>) for the provided object.
183
184 =cut
185
186 sub new_cust_event {
187   my( $self, $object ) = @_;
188
189   confess "**** $object is not a ". $self->eventtable
190     if ref($object) ne "FS::". $self->eventtable;
191
192   my $pkey = $object->primary_key;
193
194   new FS::cust_event {
195     'eventpart' => $self->eventpart,
196     'tablenum'  => $object->$pkey(),
197     '_date'     => time, #i think we always want the real "now" here.
198     'status'    => 'new',
199   };
200 }
201
202 #surely this doesn't work
203 sub reasontext { confess "part_event->reasontext deprecated"; }
204 #=item reasontext
205 #
206 #Returns the text of any reason associated with this event.
207 #
208 #=cut
209 #
210 #sub reasontext {
211 #  my $self = shift;
212 #  my $r = qsearchs('reason', { 'reasonnum' => $self->reason });
213 #  if ($r){
214 #    $r->reason;
215 #  }else{
216 #    '';
217 #  }
218 #}
219
220 =item agent 
221
222 Returns the associated agent for this event, if any, as an FS::agent object.
223
224 =cut
225
226 sub agent {
227   my $self = shift;
228   qsearchs('agent', { 'agentnum' => $self->agentnum } );
229 }
230
231 =item templatename
232
233 Returns the alternate invoice template name, if any, or false if there is
234 no alternate template for this event.
235
236 =cut
237
238 sub templatename {
239
240   my $self = shift;
241   if (    $self->action   =~ /^cust_bill_send_(alternate|agent)$/
242           && (    $self->option('agent_templatename')
243                || $self->option('templatename')       )
244      )
245   {
246        $self->option('agent_templatename')
247     || $self->option('templatename');
248
249   } else {
250     '';
251   }
252 }
253
254 =back
255
256 =head1 CLASS METHODS
257
258 =over 4
259
260 =item eventtable_labels
261
262 Returns a hash reference of labels for eventtable values,
263 i.e. 'cust_main'=>'Customer'
264
265 =cut
266
267 sub eventtable_labels {
268   #my $class = shift;
269
270   tie my %hash, 'Tie::IxHash',
271     'cust_pkg'       => 'Package',
272     'cust_bill'      => 'Invoice',
273     'cust_main'      => 'Customer',
274     'cust_pay_batch' => 'Batch payment',
275   ;
276
277   \%hash
278 }
279
280 =item eventtable_pkey_sql
281
282 Returns a hash reference of full SQL primary key names for eventtable values,
283 i.e. 'cust_main'=>'cust_main.custnum'
284
285 =cut
286
287 sub eventtable_pkey_sql {
288   #my $class = shift;
289
290   my %hash = (
291     'cust_main'      => 'cust_main.custnum',
292     'cust_bill'      => 'cust_bill.invnum',
293     'cust_pkg'       => 'cust_pkg.pkgnum',
294     'cust_pay_batch' => 'cust_pay_batch.paybatchnum',
295   );
296
297   \%hash;
298 }
299
300
301 =item eventtables
302
303 Returns a list of eventtable values (default ordering; suited for display).
304
305 =cut
306
307 sub eventtables {
308   my $class = shift;
309   my $eventtables = $class->eventtable_labels;
310   keys %$eventtables;
311 }
312
313 =item eventtables_runorder
314
315 Returns a list of eventtable values (run order).
316
317 =cut
318
319 sub eventtables_runorder {
320   shift->eventtables; #same for now
321 }
322
323 =item check_freq_labels
324
325 Returns a hash reference of labels for check_freq values,
326 i.e. '1d'=>'daily'
327
328 =cut
329
330 sub check_freq_labels {
331   #my $class = shift;
332
333   #Tie::IxHash??
334   {
335     '1d' => 'daily',
336     '1m' => 'monthly',
337   };
338 }
339
340 =item actions [ EVENTTABLE ]
341
342 Return information about the available actions.  If an eventtable is specified,
343 only return information about actions available for that eventtable.
344
345 Information is returned as key-value pairs.  Keys are event names.  Values are
346 hashrefs with the following keys:
347
348 =over 4
349
350 =item description
351
352 =item eventtable_hashref
353
354 =item option_fields
355
356 =item default_weight
357
358 =item deprecated
359
360 =back
361
362 See L<FS::part_event::Action> for more information.
363
364 =cut
365
366 #false laziness w/part_event_condition.pm
367 #some false laziness w/part_export & part_pkg
368 my %actions;
369 foreach my $INC ( @INC ) {
370   foreach my $file ( glob("$INC/FS/part_event/Action/*.pm") ) {
371     warn "attempting to load Action from $file\n" if $DEBUG;
372     $file =~ /\/(\w+)\.pm$/ or do {
373       warn "unrecognized file in $INC/FS/part_event/Action/: $file\n";
374       next;
375     };
376     my $mod = $1;
377     eval "use FS::part_event::Action::$mod;";
378     if ( $@ ) {
379       die "error using FS::part_event::Action::$mod (skipping): $@\n" if $@;
380       #warn "error using FS::part_event::Action::$mod (skipping): $@\n" if $@;
381       #next;
382     }
383     $actions{$mod} = {
384       ( map { $_ => "FS::part_event::Action::$mod"->$_() }
385             qw( description eventtable_hashref default_weight deprecated )
386             #option_fields_hashref
387       ),
388       'option_fields' => [ "FS::part_event::Action::$mod"->option_fields() ],
389     };
390   }
391 }
392
393 sub actions {
394   my( $class, $eventtable ) = @_;
395   (
396     map  { $_ => $actions{$_} }
397     sort { $actions{$a}->{'default_weight'}<=>$actions{$b}->{'default_weight'} }
398     $class->all_actions( $eventtable )
399   );
400
401 }
402
403 =item all_actions [ EVENTTABLE ]
404
405 Returns a list of just the action names
406
407 =cut
408
409 sub all_actions {
410   my ( $class, $eventtable ) = @_;
411
412   grep { !$eventtable || $actions{$_}->{'eventtable_hashref'}{$eventtable} }
413        keys %actions
414 }
415
416 =back
417
418 =head1 SEE ALSO
419
420 L<FS::part_event_option>, L<FS::part_event_condition>, L<FS::cust_main>,
421 L<FS::cust_pkg>, L<FS::cust_bill>, L<FS::cust_bill_event>, L<FS::Record>,
422 schema.html from the base documentation.
423
424 =cut
425
426 1;
427