RT# 82949 - changes section name from fees to pricing, better opiton
[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       if ( $action eq 'pkg_fee' && !$actionfields{feepart} ) {
96         return "Select a Fee Definition to use this Event Action";
97       }
98
99       my $balance_age_rx = qr/^(condition.+)\.balance_age\.age$/;
100
101       foreach my $param ( keys %{ $cgi->Vars() } ){
102
103         next unless ( $param =~ /$balance_age_rx/ );
104         next unless $cgi->param($1) eq 'balance_age';
105
106         my $errstr = FS::part_event::Condition::balance_age->
107           check_options( { age       => scalar($cgi->param($param)),
108                            age_units => scalar($cgi->param("${param}_units")),
109                          }
110                        );
111
112         return $errstr if $errstr;
113       }
114
115       return '';
116     },
117     'noerror_callback' => sub {
118       my ($cgi, $new) = @_;
119       if ( $cgi->param('_initialize') ) {
120         my $job = new FS::queue { 
121           'job' => 'FS::part_event::process_initialize'
122         };
123         my $error = $job->insert('eventpart' => $new->eventpart);
124         warn "error queueing job: $error\n" if $error; # can't do anything else
125       }
126     },
127
128     'agent_virt'       => 1,
129     'agent_null_right' => 'Edit global billing events',
130 )
131 %>
132 <%init>
133
134 die "access denied"
135   unless $FS::CurrentUser::CurrentUser->access_right('Edit billing events')
136       || $FS::CurrentUser::CurrentUser->access_right('Edit global billing events');
137
138 </%init>