summaryrefslogtreecommitdiff
path: root/httemplate/edit/process/part_event.html
blob: 0293af8863eebc43cc8616c8ace46b501765e91f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<% include( 'elements/process.html',
    #'debug'          => 1,
    'table'          => 'part_event',
    'viewall_dir'    => 'browse',
    'process_m2name' =>
      {
        'link_table'    => 'part_event_condition',
        'num_col'       => 'eventpart',
        'name_col'      => 'conditionname',
        'names_list'    => [ FS::part_event_condition->all_conditionnames() ],
        'param_style'   => 'name_colN values',
        'args_callback' => sub { # FS/FS/m2name_Common.pm
          my( $object, $prefix, $params, $listref ) = @_;
          #warn "$object $prefix $params $listref\n";

          my $cond = $object->conditionname;

          my %option_fields = $object->option_fields;

          push @$listref, map {
                                my $field = $_;

                                my $cgi_field = "$prefix$cond.$field";

                                my $value = $params->{$cgi_field};

                                my $info = $option_fields{$_};
                                $info = { label=>$info, type=>'text' }
                                  unless ref($info);

                                if ( $info->{'type'} =~
                                       /^(select|checkbox)-?multiple$/
                                     or $info->{'type'} =~ /^select/
                                        && $info->{'multiple'}
                                   )
                                {
                                  #special processing for compound fields
                                  $value = { map { $_ => 1 }
                                                 split(/\0/, $value)
                                           };
                                } elsif ( $info->{'type'} eq 'freq' ) {
                                  $value = '0' if !length($value) and !$info->{'allow_blank'};
                                  $value .= $params->{$cgi_field.'_units'} if length($value);
                                }

                                #warn "value of $cgi_field is $value\n";

                                ( $field => $value );
                              }
                              keys %option_fields;
        },
      },

    'args_callback' => sub {

      my( $cgi, $object ) = @_;

      my $prefix = $object->action.'.';

      map { my $option = $_;
            #my $value = scalar( $cgi->param( "$prefix$option" ) );
            my $value = join(',', $cgi->param( "$prefix$option" ) );

            if ( $option eq 'reasonnum' && $value == -1 ) {
              my $reason_prefix = $object->action . '_' . $option .  '_new_';
              my $new_reason = FS::reason->new;
              foreach ( qw( reason_type reason unsuspend pkgpart 
                            unsuspend_hold unused_credit ) ) {
                $new_reason->set($_, $cgi->param("$reason_prefix$_"));
              }
              warn Dumper $new_reason;
              my $error = $new_reason->insert;
              die "error creating reason: $error" if $error;
              $value = $new_reason->reasonnum;
            }
            ( $option => $value );
          }
          @{ $object->option_fields_listref };

    },
    'precheck_callback' => sub {
      my $cgi = shift;
      my $action = $cgi->param('action') or return;
      my %actionfields = map { $_ =~ /^$action\.(.*)/; $1 => $cgi->param($_) }
                         grep { /^$action\./ } $cgi->param;
      if ( exists($actionfields{'reasonnum'}) and 
           length($actionfields{'reasonnum'}) == 0 ) {
        return 'Reason required';
      }
      if ( $cgi->param('_initialize') ) {
        $cgi->param('disabled', 'Y');
      }

      my $balance_age_rx = qr/^(condition.+)\.balance_age\.age$/;

      foreach my $param ( keys %{ $cgi->Vars() } ){

	next unless ( $param =~ /$balance_age_rx/ );
	next unless $cgi->param($1) eq 'balance_age';

	my $errstr = FS::part_event::Condition::balance_age->
	  check_options( { age       => $cgi->param($param),
			   age_units => $cgi->param("${param}_units") } );

	return $errstr if $errstr;
      }

      return '';
    },
    'noerror_callback' => sub {
      my ($cgi, $new) = @_;
      if ( $cgi->param('_initialize') ) {
        my $job = new FS::queue { 
          'job' => 'FS::part_event::process_initialize'
        };
        my $error = $job->insert('eventpart' => $new->eventpart);
        warn "error queueing job: $error\n" if $error; # can't do anything else
      }
    },

    'agent_virt'       => 1,
    'agent_null_right' => 'Edit global billing events',
)
%>
<%init>

die "access denied"
  unless $FS::CurrentUser::CurrentUser->access_right('Edit billing events')
      || $FS::CurrentUser::CurrentUser->access_right('Edit global billing events');

</%init>