RT# 21110 Unparsable age error. Added javascript to validate field prior to submittin...
[freeside.git] / httemplate / edit / process / part_event.html
1 <% include( 'elements/process.html',
2     #'debug'          => 1,
3     'table'          => 'part_event',
4     'viewall_dir'    => 'browse',
5     'process_m2name' =>
6       {
7         'link_table'    => 'part_event_condition',
8         'num_col'       => 'eventpart',
9         'name_col'      => 'conditionname',
10         'names_list'    => [ FS::part_event_condition->all_conditionnames() ],
11         'param_style'   => 'name_colN values',
12         'args_callback' => sub { # FS/FS/m2name_Common.pm
13           my( $object, $prefix, $params, $listref ) = @_;
14           #warn "$object $prefix $params $listref\n";
15
16           my $cond = $object->conditionname;
17
18           my %option_fields = $object->option_fields;
19
20           push @$listref, map {
21                                 my $field = $_;
22
23                                 my $cgi_field = "$prefix$cond.$field";
24
25                                 my $value = $params->{$cgi_field};
26
27                                 my $info = $option_fields{$_};
28                                 $info = { label=>$info, type=>'text' }
29                                   unless ref($info);
30
31                                 if ( $info->{'type'} =~
32                                        /^(select|checkbox)-?multiple$/
33                                      or $info->{'type'} =~ /^select/
34                                         && $info->{'multiple'}
35                                    )
36                                 {
37                                   #special processing for compound fields
38                                   $value = { map { $_ => 1 }
39                                                  split(/\0/, $value)
40                                            };
41                                 } elsif ( $info->{'type'} eq 'freq' ) {
42                                   $value = '0' if !length($value) and !$info->{'allow_blank'};
43                                   $value = '0' if $value =~ /[^\d]/;
44                                   $value .= $params->{$cgi_field.'_units'} if length($value);
45                                 }
46
47                                 #warn "value of $cgi_field is $value\n";
48
49                                 ( $field => $value );
50                               }
51                               keys %option_fields;
52         },
53       },
54
55     'args_callback' => sub {
56
57       my( $cgi, $object ) = @_;
58
59       my $prefix = $object->action.'.';
60
61       map { my $option = $_;
62             #my $value = scalar( $cgi->param( "$prefix$option" ) );
63             my $value = join(',', $cgi->param( "$prefix$option" ) );
64
65             if ( $option eq 'reasonnum' && $value == -1 ) {
66               my $reason_prefix = $object->action . '_' . $option .  '_new_';
67               my $new_reason = FS::reason->new;
68               foreach ( qw( reason_type reason unsuspend pkgpart 
69                             unsuspend_hold unused_credit ) ) {
70                 $new_reason->set($_, $cgi->param("$reason_prefix$_"));
71               }
72               warn Dumper $new_reason;
73               my $error = $new_reason->insert;
74               die "error creating reason: $error" if $error;
75               $value = $new_reason->reasonnum;
76             }
77             ( $option => $value );
78           }
79           @{ $object->option_fields_listref || [] };
80
81     },
82     'precheck_callback' => sub {
83       my $cgi = shift;
84       my $action = $cgi->param('action') or return;
85       my %actionfields = map { $_ =~ /^$action\.(.*)/; $1 => scalar($cgi->param($_)) }
86                          grep { /^$action\./ } $cgi->param;
87       if ( exists($actionfields{'reasonnum'}) and 
88            length($actionfields{'reasonnum'}) == 0 ) {
89         return 'Reason required';
90       }
91       if ( $cgi->param('_initialize') ) {
92         $cgi->param('disabled', 'Y');
93       }
94
95       my $balance_age_rx = qr/^(condition.+)\.balance_age\.age$/;
96
97       foreach my $param ( keys %{ $cgi->Vars() } ){
98
99         next unless ( $param =~ /$balance_age_rx/ );
100         next unless $cgi->param($1) eq 'balance_age';
101
102         my $errstr = FS::part_event::Condition::balance_age->
103           check_options( { age       => scalar($cgi->param($param)),
104                            age_units => scalar($cgi->param("${param}_units")),
105                          }
106                        );
107
108         return $errstr if $errstr;
109       }
110
111       return '';
112     },
113     'noerror_callback' => sub {
114       my ($cgi, $new) = @_;
115       if ( $cgi->param('_initialize') ) {
116         my $job = new FS::queue { 
117           'job' => 'FS::part_event::process_initialize'
118         };
119         my $error = $job->insert('eventpart' => $new->eventpart);
120         warn "error queueing job: $error\n" if $error; # can't do anything else
121       }
122     },
123
124     'agent_virt'       => 1,
125     'agent_null_right' => 'Edit global billing events',
126 )
127 %>
128 <%init>
129
130 die "access denied"
131   unless $FS::CurrentUser::CurrentUser->access_right('Edit billing events')
132       || $FS::CurrentUser::CurrentUser->access_right('Edit global billing events');
133
134 </%init>