1 package FS::part_event::Condition;
4 use base qw( FS::part_event_condition );
5 use Time::Local qw(timelocal_nocheck);
6 use FS::UID qw( driver_name );
10 FS::part_event::Condition - Base class for event conditions
14 package FS::part_event::Condition::mycondition;
16 use base FS::part_event::Condition;
20 FS::part_event::Condition is a base class for event conditions classes.
24 These methods are implemented in each condition class.
30 Condition classes must define a description method. This method should return
31 a scalar description of the condition.
33 =item eventtable_hashref
35 Condition classes must define an eventtable_hashref method if they can only be
36 tested against some kinds of tables. This method should return a hash reference
37 of eventtables (values set true indicate the condition can be tested):
39 sub eventtable_hashref {
43 'cust_pay_batch' => 0,
44 'cust_statement' => 0,
51 sub eventtable_hashref {
55 'cust_pay_batch' => 1,
56 'cust_statement' => 1,
62 Condition classes may define an option_fields method to indicate that they
63 accept one or more options.
65 This method should return a list of option names and option descriptions.
66 Each option description can be a scalar description, for simple options, or a
67 hashref with the following values:
71 =item label - Description
73 =item type - Currently text, money, checkbox, checkbox-multiple, select, select-agent, select-pkg_class, select-part_referral, select-table, fixed, hidden, (others can be implemented as httemplate/elements/tr-TYPE.html mason components). Defaults to text.
75 =item options - For checkbox-multiple and select, a list reference of available option values.
77 =item option_labels - For checkbox-multiple (and select?), a hash reference of availble option values and labels.
79 =item value - for checkbox, fixed, hidden (also a default for text, money, more?)
81 =item table - for select-table
83 =item name_col - for select-table
85 =item NOTE: See httemplate/elements/select-table.html for a full list of the optinal options for the select-table type
89 NOTE: A database connection is B<not> yet available when this subroutine is
96 'field' => 'description',
98 'another_field' => { 'label'=>'Amount', 'type'=>'money', },
100 'third_field' => { 'label' => 'Types',
101 'type' => 'checkbox-multiple',
102 'options' => [ 'h', 's' ],
103 'option_labels' => { 'h' => 'Happy',
116 =item condition CUSTOMER_EVENT_OBJECT
118 Condition classes must define a condition method. This method is evaluated
119 to determine if the condition has been met. The object which triggered the
120 event (an FS::cust_main, FS::cust_bill or FS::cust_pkg object) is passed as
121 the first argument. Additional arguments are list of key-value pairs.
123 To retreive option values, call the option method on the desired option, i.e.:
125 my( $self, $cust_object, %opts ) = @_;
126 $value_of_field = $self->option('field');
128 Available additional arguments:
130 $time = $opt{'time'}; #use this instead of time or $^T
132 $cust_event = $opt{'cust_event'}; #to retreive the cust_event object being tested
134 Return a true value if the condition has been met, and a false value if it has
137 =item condition_sql EVENTTABLE
139 Condition classes may optionally define a condition_sql method. This B<class>
140 method should return an SQL fragment that tests for this condition. The
141 fragment is evaluated and a true value of this expression indicates that the
142 condition has been met. The event table (cust_main, cust_bill or cust_pkg) is
143 passed as an argument.
145 This method is used for optimizing event queries. You may want to add indices
146 for any columns referenced. It is acceptable to return an SQL fragment which
147 partially tests the condition; doing so will still reduce the number of
148 records which must be returned and tested with the B<condition> method.
154 my( $class, $eventtable ) = @_;
161 Condition classes may optionally define a disabled method. Returning a true
162 value disbles the condition entirely.
172 This is used internally by the I<once> and I<balance> conditions. You probably
173 do B<not> want to define this method for new custom conditions, unless you're
174 sure you want B<every> new action to start with your condition.
176 Condition classes may define an implicit_flag method that returns true to
177 indicate that all new events should start with this condition. (Currently,
178 condition classes which do so should be applicable to all kinds of
179 I<eventtable>s.) The numeric value of the flag also defines the ordering of
185 sub implicit_flag { 0; }
189 Again, used internally by the I<once> and I<balance> conditions; probably not
190 a good idea for new custom conditions.
192 Condition classes may define a remove_warning method containing a string
193 warning message to enable a confirmation dialog triggered when the condition
194 is removed from an event.
199 sub remove_warning { ''; }
203 This is used internally by the I<balance_age> and I<cust_bill_age> conditions
204 to declare ordering; probably not of general use for new custom conditions.
206 =item order_sql_weight
208 In conjunction with order_sql, this defines which order the ordering fragments
209 supplied by different B<order_sql> should be used.
213 sub order_sql_weight { ''; }
219 These methods are defined in the base class for use in condition classes.
223 =item cust_main CUST_OBJECT
225 Return the customer object (see L<FS::cust_main>) associated with the provided
226 object (the object itself if it is already a customer object).
231 my( $self, $cust_object ) = @_;
233 $cust_object->isa('FS::cust_main') ? $cust_object : $cust_object->cust_main;
237 =item option_label OPTIONNAME
239 Returns the label for the specified option name.
244 my( $self, $optionname ) = @_;
246 my %option_fields = $self->option_fields;
248 ref( $option_fields{$optionname} )
249 ? $option_fields{$optionname}->{'label'}
250 : $option_fields{$optionname}
256 =item option_age_from OPTION FROM_TIMESTAMP
258 Retreives a condition option, parses it from a frequency (such as "1d", "1w" or
259 "12m"), and subtracts that interval from the supplied timestamp. It is
260 primarily intended for use in B<condition>.
264 sub option_age_from {
265 my( $self, $option, $time ) = @_;
266 my $age = $self->option($option);
267 $age = '0m' unless length($age);
269 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($time) )[0,1,2,3,4,5];
271 if ( $age =~ /^(\d+)m$/i ) {
273 until ( $mon >= 0 ) { $mon += 12; $year--; }
274 } elsif ( $age =~ /^(\d+)y$/i ) {
276 } elsif ( $age =~ /^(\d+)w$/i ) {
278 } elsif ( $age =~ /^(\d+)d$/i ) {
280 } elsif ( $age =~ /^(\d+)h$/i ) {
283 die "unparsable age: $age";
286 timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year);
290 =item condition_sql_option OPTION
292 This is a class method that returns an SQL fragment for retreiving a condition
293 option. It is primarily intended for use in B<condition_sql>.
297 sub condition_sql_option {
298 my( $class, $option ) = @_;
300 ( my $condname = $class ) =~ s/^.*:://;
302 "( SELECT optionvalue FROM part_event_condition_option
303 WHERE part_event_condition_option.eventconditionnum =
304 cond_$condname.eventconditionnum
305 AND part_event_condition_option.optionname = '$option'
309 #c.f. part_event_condition_option.pm / part_event_condition_option_option
310 #used for part_event/Condition/payby.pm
311 sub condition_sql_option_option {
312 my( $class, $option ) = @_;
314 ( my $condname = $class ) =~ s/^.*:://;
317 "( SELECT optionnum FROM part_event_condition_option
318 WHERE part_event_condition_option.eventconditionnum =
319 cond_$condname.eventconditionnum
320 AND part_event_condition_option.optionname = '$option'
321 AND part_event_condition_option.optionvalue = 'HASH'
324 "( SELECT optionname FROM part_event_condition_option_option
325 WHERE optionnum = $optionnum
331 =item condition_sql_option_age_from OPTION FROM_TIMESTAMP
333 This is a class method that returns an SQL fragment that will retreive a
334 condition option, parse it from a frequency (such as "1d", "1w" or "12m"),
335 and subtract that interval from the supplied timestamp. It is primarily
336 intended for use in B<condition_sql>.
340 sub condition_sql_option_age_from {
341 my( $class, $option, $from ) = @_;
343 my $value = $class->condition_sql_option($option);
345 # my $str2time = str2time_sql;
347 if ( driver_name =~ /^Pg/i ) {
349 #can we do better with Pg now that we have $from? yes we can, bob
350 "( $from - EXTRACT( EPOCH FROM REPLACE( $value, 'm', 'mon')::interval ) )";
352 } elsif ( driver_name =~ /^mysql/i ) {
354 #hmm... is there a way we can save $value? we're just an expression, hmm
355 #we might be able to do something like "AS ${option}_value" except we get
356 #used in more complicated expressions and we need some sort of unique
357 #identifer passed down too... yow
359 "CASE WHEN $value IS NULL OR $value = ''
361 WHEN $value LIKE '%m'
363 FROM_UNIXTIME($from) - INTERVAL REPLACE( $value, 'm', '' ) MONTH
365 WHEN $value LIKE '%y'
367 FROM_UNIXTIME($from) - INTERVAL REPLACE( $value, 'y', '' ) YEAR
369 WHEN $value LIKE '%w'
371 FROM_UNIXTIME($from) - INTERVAL REPLACE( $value, 'w', '' ) WEEK
373 WHEN $value LIKE '%d'
375 FROM_UNIXTIME($from) - INTERVAL REPLACE( $value, 'd', '' ) DAY
377 WHEN $value LIKE '%h'
379 FROM_UNIXTIME($from) - INTERVAL REPLACE( $value, 'h', '' ) HOUR
385 die "FATAL: don't know how to subtract frequencies from dates for ".
386 driver_name. " databases";
392 =item condition_sql_option_age OPTION
394 This is a class method that returns an SQL fragment for retreiving a condition
395 option, and additionaly parsing it from a frequency (such as "1d", "1w" or
396 "12m") into an approximate number of seconds.
398 Note that since months vary in length, the results of this method should B<not>
399 be used in computations (use condition_sql_option_age_from for that). They are
400 useful for for ordering and comparison to other ages.
402 This method is primarily intended for use in B<order_sql>.
406 sub condition_sql_option_age {
407 my( $class, $option ) = @_;
408 $class->age2seconds_sql( $class->condition_sql_option($option) );
411 =item age2seconds_sql
413 Class method returns an SQL fragment for parsing an arbitrary frequeny (such
414 as "1d", "1w", "12m", "2y" or "12h") into an approximate number of seconds.
416 Approximate meaning: months are considered to be 30 days, years to be
417 365.25 days. Otherwise the numbers of seconds returned is exact.
421 sub age2seconds_sql {
422 my( $class, $value ) = @_;
424 if ( driver_name =~ /^Pg/i ) {
426 "EXTRACT( EPOCH FROM REPLACE( $value, 'm', 'mon')::interval )";
428 } elsif ( driver_name =~ /^mysql/i ) {
430 #hmm... is there a way we can save $value? we're just an expression, hmm
431 #we might be able to do something like "AS ${option}_age" except we get
432 #used in more complicated expressions and we need some sort of unique
433 #identifer passed down too... yow
434 # 2592000 = 30d "1 month"
435 # 31557600 = 365.25d "1 year"
437 "CASE WHEN $value IS NULL OR $value = ''
439 WHEN $value LIKE '%m'
440 THEN REPLACE( $value, 'm', '' ) * 2592000
441 WHEN $value LIKE '%y'
442 THEN REPLACE( $value, 'y', '' ) * 31557600
443 WHEN $value LIKE '%w'
444 THEN REPLACE( $value, 'w', '' ) * 604800
445 WHEN $value LIKE '%d'
446 THEN REPLACE( $value, 'd', '' ) * 86400
447 WHEN $value LIKE '%h'
448 THEN REPLACE( $value, 'h', '' ) * 3600
453 die "FATAL: don't know how to approximate frequencies for ". driver_name.
460 =item condition_sql_option_integer OPTION [ DRIVER_NAME ]
462 As I<condition_sql_option>, but cast the option value to an integer so that
463 comparison to other integers is type-correct.
467 sub condition_sql_option_integer {
468 my ($class, $option, $driver_name) = @_;
470 my $integer = ($driver_name =~ /^mysql/) ? 'UNSIGNED INTEGER' : 'INTEGER';
473 COALESCE('. $class->condition_sql_option($option).
478 =head1 NEW CONDITION CLASSES
480 A module should be added in FS/FS/part_event/Condition/ which implements the
481 methods desribed above in L</METHODS>. An example may be found in the
482 eg/part_event-Condition-template.pm file.