1 <% include( 'elements/edit.html',
2 'name' => 'Billing event definition',
3 'table' => 'part_event',
6 { field => 'eventtable',
8 options => [ FS::part_event->eventtables ],
9 labels => $eventtable_labels,
10 onchange => 'eventtable_changed',
12 { field => 'agentnum',
13 type => 'select-agent',
14 disable_empty => $disable_empty_agent,
16 { field => 'check_freq',
18 options => [ '1d', '1m' ],
19 labels => $check_freq_labels,
21 { field => 'disabled',
26 value => 'Event Conditions',
28 { field => 'conditionname',
29 type => 'selectlayers',
30 options => [ keys %all_conditions ],
31 labels => \%condition_labels,
32 onchange => 'condition_changed(what);',
33 layer_fields => \%condition_fields,
34 layer_values_callback => $condition_layer_values,
35 html_between => n_a('action'),
36 m2name_table => 'part_event_condition',
37 m2name_namecol => 'conditionname',
38 m2_label => 'Condition',
39 m2_new_default => \@implicit_condition_objs,
40 m2_error_callback => $condition_error_callback,
41 m2_remove_warnings => \%condition_remove_warnings,
42 m2_new_js => 'condition_repop',
43 m2_remove_js => 'condition_add',
46 value => 'Event Action',
49 type => 'selectlayers',
50 options => [ keys %all_actions ],
51 labels => \%action_labels,
52 onchange => 'action_changed(what);',
53 layer_fields => \%action_fields,
54 layer_values_callback => $action_layer_values,
55 html_between => n_a('action'),
60 'eventpart' => 'Event',
61 'event' => 'Event name',
62 'eventtable' => 'Type',
63 'agentnum' => 'Agent',
64 'check_freq' => 'Check frequency',
65 'disabled' => 'Disable event',
67 'conditionname' => 'Add new condition',
71 'viewall_dir' => 'browse',
72 'new_callback' => sub { #start empty for new events only
73 my( $cgi, $object, $fields_listref ) = @_;
74 unshift @{ $fields_listref->[1]{'options'} }, '';
76 'error_callback' => $error_callback,
79 'agent_null_right' => 'Edit global billing events',
82 <SCRIPT TYPE="text/javascript">
84 window.onload = function () { eventtable_changed(document.getElementById('eventtable')) };
87 function eventtable_changed(what) {
90 alert('eventtable_changed called on ' + what );
93 var eventtable = what.options[what.selectedIndex].value;
95 alert ("eventtable: " + eventtable);
97 var eventdesc = what.options[what.selectedIndex].text;
99 //remove the ** Select type **
100 if ( what.options[0].value == '' && notonload++ > 0 ) {
101 what.options[0] = null;
105 // XXX gray out conditions that can't apply (in addition to the warning)?
109 // update condition selects
112 for ( var cnum=0; document.getElementById('conditionname'+cnum); cnum++ ) {
113 var cond_id = 'conditionname' + cnum;
114 var cond_select = document.getElementById(cond_id);
117 alert('updating ' + cond_id);
120 // save off the current value
121 var conditionname = cond_select.options[cond_select.selectedIndex].value;
122 var cond_desc = cond_select.options[cond_select.selectedIndex].text;
124 var seen_condition = condition_repop(cond_select);
126 var warning = document.getElementById(cond_id + '_warning');
128 alert('turning off warning; setting style.display of '+ cond_id +
129 '_warning (' + warning + ') to none');
131 warning.style.display = 'none';
133 if ( ! seen_condition && conditionname != '' ) {
134 // add the current (not valid) condition back
135 opt(cond_select, conditionname, cond_desc, true );
136 if ( ! condition_is_implicit(conditionname) ) {
137 cond_select.parentNode.parentNode.style.display = '';
138 cond_select.disabled = '';
139 // turn on a warning and gray out the condition row
141 alert('turning on warning; setting style.display of '+ cond_id +
142 '_warning (' + warning + ') to ""');
144 warning.innerHTML = 'Not applicable to ' + eventdesc + ' events';
145 warning.style.display = '';
147 if ( ! condition_in_eventtable(conditionname) ) {
149 alert(conditionname + " not in " + eventtable + "; disabling");
151 cond_select.parentNode.parentNode.style.display = 'none';
152 cond_select.disabled = 'disabled';
155 alert(conditionname + " implicit for " + eventtable + "; enabling");
157 cond_select.parentNode.parentNode.style.display = '';
158 cond_select.disabled = '';
167 // update action select
170 // save off the current value first!!
171 var action = what.form.action.options[what.form.action.selectedIndex].value;
172 var a_desc = what.form.action.options[what.form.action.selectedIndex].text;
173 var seen_action = false;
175 // blank the current action select
176 for ( var i = what.form.action.length; i >= 0; i-- )
177 what.form.action.options[i] = null;
179 if ( action == '' ) {
180 opt(what.form.action, action, a_desc, true );
184 % foreach my $eventtable ( FS::part_event->eventtables ) {
185 % tie my %actions, 'Tie::IxHash', FS::part_event->actions($eventtable);
186 % #use Data::Dumper; warn Dumper(%actions);
188 if ( eventtable == '<% $eventtable %>' ) {
190 % foreach my $action ( keys %actions ) {
191 % ( my $description = $actions{$action}->{'description'} ) =~ s/'/\\'/g;
194 if ( action == '<% $action %>' ) {
198 opt( what.form.action, '<% $action %>', '<% $description %>', sel );
205 // by default, turn off warnings and enable the submit button
206 var warning = document.getElementById('action_warning');
207 warning.style.display = 'none';
208 var submit_button = document.getElementById('submit');
209 submit_button.disabled = '';
211 if ( ! seen_action && action != '' ) {
212 // add the current (not valid) action back
213 opt( what.form.action, action, a_desc, true );
214 // turn on a warning and disable the submit button
215 //warning.innerHTML = a_desc + ' event not available as a ' +
216 warning.innerHTML = 'Not available as a ' + eventdesc + ' action';
217 warning.style.display = '';
218 submit_button.disabled = 'disabled';
223 function opt(what,value,text,selected) {
224 var optionName = new Option(text, value, false, selected);
225 var length = what.length;
226 what.options[length] = optionName;
229 function action_changed(what) {
230 // remove '** Select new **'
231 if ( what.options[0].value == '' ) {
232 what.options[0] = null;
234 // remove the warning, remove the invalid action, enable the submit button
235 var warning = document.getElementById('action_warning');
236 if ( warning.style.display == '' ) {
237 warning.style.display = 'none';
238 what.options[what.length-1] = null;
239 document.getElementById('submit').disabled = '';
243 function condition_changed(what) {
244 // remove '** Select new **'
245 if ( what.options[0].value == '' ) {
246 what.options[0] = null;
249 var previousValue = what.getAttribute('previousValue');
250 var previousText = what.getAttribute('previousText');
251 var value = what.options[what.selectedIndex].value;
252 var text = what.options[what.selectedIndex].text;
254 % foreach my $value ( keys %condition_remove_warnings ) {
255 if ( previousValue == '<% $value %>' ) {
256 if ( !confirm( <% $condition_remove_warnings{$value} |js_string %> ) ) {
257 for ( var i=0; i < what.length; i++ ) {
258 if ( what.options[i].value == previousValue ) {
259 what.selectedIndex = i;
267 //alert(previous + ' changed to ' + value);
269 var field_regex = /(\d+)$/;
270 var match = field_regex.exec(what.name);
272 alert(what.name + " didn't match?!");
276 //add the previous condition *back* to all the other selects...
277 condition_add(previousValue, previousText, match[1]);
279 what.setAttribute('previousValue', value);
280 what.setAttribute('previousText', text);
282 // remove the new condition from all other selects
283 condition_remove(value, match[1]);
287 function condition_avail(check_cond, curnum) {
288 for ( var cnum=0; document.getElementById('conditionname'+cnum); cnum++ ) {
289 if ( cnum == curnum ) continue;
291 var cond_id = 'conditionname' + cnum;
292 var cond_select = document.getElementById(cond_id);
294 //alert("checking " + cond_id + " (" + cond_select.disabled + ")");
296 if ( cond_select.disabled ) continue;
299 var conditionname = cond_select.options[cond_select.selectedIndex].value;
301 if ( check_cond == conditionname ) return false;
309 function condition_remove(remove_cond, curnum) {
311 if ( remove_cond.length == 0 ) return;
313 for ( var cnum=0; document.getElementById('conditionname'+cnum); cnum++ ) {
314 if ( cnum == curnum ) continue;
316 var cond_id = 'conditionname' + cnum;
317 var cond_select = document.getElementById(cond_id);
319 //for ( var i = cond_select.length; i >= 0; i-- ) {
320 for ( var i=0; i < cond_select.length; i++ ) {
321 if ( cond_select.options[i].value == remove_cond ) {
322 cond_select.options[i] = null;
330 function condition_add(add_condname, add_conddesc, curnum) {
332 if ( add_condname.length == 0 ) return;
334 var in_eventtable = condition_in_eventtable(add_condname);
336 if ( ! in_eventtable ) return;
338 for ( var cnum=0; document.getElementById('conditionname'+cnum); cnum++ ) {
339 if ( cnum == curnum ) continue;
341 var cond_id = 'conditionname' + cnum;
342 var cond_select = document.getElementById(cond_id);
344 if ( cond_select.disabled ) continue;
346 //alert("adding " + add_condname + " to " + cond_id);
348 opt(cond_select, add_condname, add_conddesc, false );
350 cond_select.parentNode.parentNode.style.display = '';
356 function condition_in_eventtable(condname) {
358 var eventtable_el = document.getElementById('eventtable');
359 var eventtable = eventtable_el.options[eventtable_el.selectedIndex].value;
361 var in_eventtable = false;
363 % foreach my $eventtable ( FS::part_event->eventtables ) {
364 % tie my %conditions, 'Tie::IxHash',
365 % FS::part_event_condition->conditions($eventtable);
367 if ( eventtable == '<% $eventtable %>' ) {
369 % foreach my $conditionname ( keys %conditions ) {
371 if ( condname == '<% $conditionname %>' ) {
372 in_eventtable = true;
381 return in_eventtable;
385 function condition_is_implicit(condname) {
387 if ( true <% @implicit_conditions
388 ? ( ' && '. join(' && ', map { "condname != '$_'" }
400 function condition_repop(cond_select) {
402 var eventtable_el = document.getElementById('eventtable');
403 var eventtable = eventtable_el.options[eventtable_el.selectedIndex].value;
405 // save off the current value
406 var conditionname = cond_select.options[cond_select.selectedIndex].value;
407 var cond_desc = cond_select.options[cond_select.selectedIndex].text;
408 var seen_condition = false;
410 //skip deleted conditions
411 if ( cond_select.disabled && conditionname != '' && ! condition_is_implicit(conditionname) ) {
415 var field_regex = /(\d+)$/;
416 var match = field_regex.exec(cond_select.name);
418 alert(what.name + " didn't match?!");
423 // blank the current condition select
424 for ( var i = cond_select.length; i >= 0; i-- )
425 cond_select.options[i] = null;
427 if ( conditionname == '' ) {
428 opt(cond_select, conditionname, cond_desc, true );
432 % foreach my $eventtable ( FS::part_event->eventtables ) {
433 % tie my %conditions, 'Tie::IxHash',
434 % FS::part_event_condition->conditions($eventtable);
436 if ( eventtable == '<% $eventtable %>' ) {
438 % foreach my $conditionname ( keys %conditions ) {
439 % my $description = $conditions{$conditionname}->{'description'};
440 % $description =~ s/'/\\'/g;
443 if ( conditionname == '<% $conditionname %>' ) {
444 seen_condition = true;
448 if ( condition_avail("<% $conditionname %>", cnum) ) {
449 opt(cond_select, '<% $conditionname %>', '<% $description %>', sel);
458 if ( cond_select.length > 1 || cond_select.length == 1 && cond_select.options[0].value.length > 0 ) {
460 cond_select.parentNode.parentNode.style.display = '';
461 cond_select.disabled = '';
464 cond_select.parentNode.parentNode.style.display = 'none';
465 cond_select.disabled = 'disabled';
468 return seen_condition;
475 #misc (eventtable, check_freq)
477 my $eventtable_labels = FS::part_event->eventtable_labels;
478 $eventtable_labels->{''} = '** Select type **';
480 my $check_freq_labels = FS::part_event->check_freq_labels;
484 tie my %all_conditions, 'Tie::IxHash',
485 '' => { 'description' => '*** Select new condition ***', },
486 FS::part_event_condition->conditions();
488 my %condition_labels = map { $_ => $all_conditions{$_}->{'description'} }
489 keys %all_conditions;
491 #my %condition_fields = map { $_ => $all_conditions{$_}->{option_fields} }
492 # keys %all_conditions;
493 my %condition_fields = map { my $c = $_;
494 tie my %opts, 'Tie::IxHash',
495 @{ $all_conditions{$c}->{'option_fields'} || []};
496 %opts = ( map { ( "$c.$_" => $opts{$_} ); }
501 keys %all_conditions;
503 my @implicit_conditions = sort { $all_conditions{$a}->{'implicit_flag'} <=>
504 $all_conditions{$b}->{'implicit_flag'}
506 grep { $all_conditions{$_}->{'implicit_flag'} }
507 keys %all_conditions;
509 my @implicit_condition_objs = map {
510 new FS::part_event_condition {
511 'conditionname' => $_,
514 @implicit_conditions;
516 my %condition_remove_warnings =
517 map { ( $_ => $all_conditions{$_}->{'remove_warning'} ); }
518 grep { $all_conditions{$_}->{'remove_warning'} }
519 keys %all_conditions;
523 tie my %all_actions, 'Tie::IxHash',
524 '' => { 'description' => '*** Select event action ***', },
525 FS::part_event->actions();
527 my %action_labels = map { $_ => $all_actions{$_}->{'description'} }
530 #my %action_fields = map { $_ => $all_actions{$_}->{option_fields} }
532 my %action_fields = map { my $action = $_;
533 tie my %opts, 'Tie::IxHash',
534 @{ $all_actions{$action}->{option_fields} || [] };
535 %opts = ( map { ( "$action.$_" => $opts{$_} ); }
538 ( $action => [ %opts ] );
549 qq( <FONT ID="${field}_warning" STYLE="display:none" COLOR="#FF0000">).
550 "Party Party Join us Join us".
555 my $action_layer_values = sub {
556 my( $cgi, $part_event ) = @_;
557 my $action = $cgi->param('action') || $part_event->action;
558 return {} unless $action;
559 scalar( #force hashref
561 #map { $_ => { $part_event->options } }
562 # keys %action_fields
563 map { my $action = $_;
564 my %fields = @{ $action_fields{$action} };
565 my %obj_opts = $part_event->options;
566 %obj_opts = map { ( "$action.$_" => $obj_opts{$_} ); }
569 map { #false laziness w/process/part_event.html
571 my $value = scalar($cgi->param($_)) || $obj_opts{$_};
573 if ( $option =~ /^(.*)\.reasonnum$/ && $value == -1 ) {
575 'typenum' => scalar( $cgi->param( "new${option}T" ) ),
576 'reason' => scalar( $cgi->param( "new${option}" ) ),
580 ( $option => $value );
584 ( $action => \%opts );
591 tie my %cgi_conditions, 'Tie::IxHash';
593 my $error_callback = sub {
594 my( $cgi, $object, $fields_listref ) = @_;
596 my @cond_params = grep /^conditionname\d+$/, $cgi->param;
598 %cgi_conditions = map {
600 my $conditionname = $cgi->param($param);
605 $cgi_key =~ /^$param\.$conditionname\.(.*)$/ or die 'wtf!';
607 #my $value = $cgi->param($_);
609 #my $info = $all_conditions->{$conditionname}
611 @{ $all_conditions{$conditionname}->{'option_fields'} || []};
612 my $info = $cond_opts{$key};
615 #false laziness w/process/part_event.html
616 if ( $info->{'type'} =~ /^(select|checkbox)-?multiple$/
617 or $info->{'type'} =~ /^select/ && $info->{'multiple'} ) {
618 $value = { map { $_ => 1 } $cgi->param($cgi_key) };
619 } elsif ( $info->{'type'} eq 'freq' ) {
620 $value = $cgi->param($cgi_key). $cgi->param($cgi_key.'_units');
622 $value = $cgi->param($cgi_key);
627 } grep /^$param\.$conditionname\./, $cgi->param
629 } grep $cgi->param($_), grep /^conditionname\d+$/, $cgi->param;
633 my $condition_error_callback = sub {
635 new FS::part_event_condition { 'conditionname' => $_, };
636 } keys %cgi_conditions;
639 my $condition_layer_values = sub {
640 #m2_table option causes this to be
641 # part_event_condition instead of part_event
642 my ( $cgi, $part_event_condition, $switches ) = @_;
643 scalar( #force hashref
645 #map { $_ => { $part_event_condition->options } }
646 # keys %condition_fields
647 map { my $conditionname = $_;
648 my %opts = $switches->{'mode'} eq 'error'
649 ? %{ $cgi_conditions{$conditionname} || {} }
650 : $part_event_condition->options;
652 map { ( "$conditionname.$_" => $opts{$_} ); }
655 ( $conditionname => \%opts );
657 keys %condition_fields
666 my $curuser = $FS::CurrentUser::CurrentUser;
669 unless $curuser->access_right('Edit billing events')
670 || $curuser->access_right('Edit global billing events');
672 my $disable_empty_agent= ! $curuser->access_right('Edit global billing events');
674 %cgi_conditions = ();
675 my $use_cgi_conditions = 0;