RT# 82949 - changes section name from fees to pricing, better opiton
[freeside.git] / httemplate / browse / part_event.html
1 <% include( 'elements/browse.html',
2                 'title'              => 'Billing Event Definitions',
3                 'html_init'          => $html_init,
4                 'name'               => 'billing event definitions',
5                 'disableable'        => 1,
6                 'disabled_statuspos' => 2,
7                 'agent_virt'         => 1,
8                 'agent_null_right'   => 'Edit global billing events',
9                 'agent_pos'          => 3,
10                 'query'              => { 'select'    => 'part_event.*',
11                                           'table'     => 'part_event',
12                                           'addl_from' => $join_conditions,
13                                           'hashref'   => $hashref,
14                                           'order_by'  => $order_conditions,
15                                         },
16                 'count_query'        => $count_query,
17                 'header'             => [ '#',
18                                           'Event',
19                                           'Type',
20                                           'Check freq.',
21                                           'Conditions',
22                                           'Action',
23                                         ],
24                 'fields'             => [ 'eventpart',
25                                           $event_sub,
26                                           $eventtable_sub,
27                                           $check_freq_sub,
28                                           $conditions_sub,
29                                           $action_sub,
30                                         ],
31                 'links'              => [ $link,
32                                           '',
33                                           '',
34                                           '',
35                                           '',
36                                           '',
37                                         ],
38                 'align'              => 'rllccc',
39           )
40 %>
41 <%once>
42
43 my $link = [ $p.'edit/part_event.html?', 'eventpart' ];
44
45 my $event_sub = sub {
46   my $part_event = shift;
47   my $onclick = include('/elements/popup_link_onclick.html',
48     action      => $p.'view/part_event-targets.html?eventpart='.
49                     $part_event->eventpart,
50     actionlabel => 'Event query', #no, XSS - '.$part_event->event,
51     width       => 650,
52     height      => 420,
53     close_text  => 'Close',
54   );
55   [#rows
56     [#subcolumns
57       {
58         'data'       => encode_entities($part_event->event),
59         'link'       => $p.'edit/part_event.html?'.$part_event->eventpart,
60       },
61       {
62         'data'       => '(query) ',
63         'size'       => '-1',
64         'data_style' => 'b',
65         'onclick'    => $onclick,
66       },
67     ],
68   ];
69 };
70
71 my $eventtable_labels = FS::part_event->eventtable_labels;
72 my $eventtable_sub = sub { $eventtable_labels->{ shift->eventtable }; };
73
74 my $check_freq_labels = FS::part_event->check_freq_labels;
75 my $check_freq_sub = sub { $check_freq_labels->{ shift->check_freq }; };
76
77 my $conditions_sub = sub {
78   my $part_event = shift;
79   my $addl = 0;
80
81
82   [
83     map {
84            my $part_event_condition = $_;
85            my %options = $part_event_condition->options;
86
87            my $desc = $part_event_condition->description;
88
89            if ( $cgi->param('test_eventtable_num') =~ /^(\d+)$/ ) {
90
91              my $tablenum = $1;
92
93              if ( $cgi->param('eventtable') =~ /^(cust_main|cust_pkg|cust_bill)$/ ) {
94                my $eventtable = "FS::$1";
95                my $object = $eventtable->by_key($tablenum);
96
97                my $cust_event = $part_event->new_cust_event($object);
98
99                my $test = $part_event_condition->condition(
100                             $object,
101                             'cust_event' => $cust_event,
102                           );
103                                                  
104                my $yesno = $test ? '(<FONT COLOR="#00ff00">yes</FONT>)'
105                                  : '(<FONT COLOR="#ff0000">no</FONT>)';
106
107                $desc .= " $yesno";
108              }
109            }
110
111            [
112              {
113                'data'       => $desc,
114                'width'      => '100%',
115                'align'      => 'center',
116                'colspan'    => 2,
117                'style'      => ( $addl++ ? 'border-top: 1px solid gray' : '' ), 
118              },
119            ],
120
121            map {
122
123              my $data = $options{$_};
124              if ( ref($data) ) {
125                $data = join('<BR>', keys %$data); #XXX display hash values too?
126              }
127
128              [
129                {
130                  'data'   => $part_event_condition->option_label($_). ':',
131                  'align'  => 'right',
132                  'valign' => 'top',
133                  'size'   => '-1',
134                },
135                {
136                  'data'  => $data,
137                  'align' => 'left',
138                  'size'  => '-1',
139                },
140              ];
141
142            } keys %options
143
144         }
145         $part_event->part_event_condition
146
147   ];
148
149 };
150
151 my $action_sub = sub {
152   my $part_event = shift;
153
154   my %options = $part_event->options;
155   
156   [
157
158     [
159       {
160         'data'       => $part_event->description,
161         'width'      => '100%',
162         'align'      => 'center',
163         'colspan'    => 2,
164       },
165     ],
166
167     map {
168           [
169             {
170               'data'  => $part_event->option_label($_). ':',
171               'align' => 'right',
172               'size'  => '-1',
173             },
174             {
175              'data'  => $options{$_},
176              'align' => 'left',
177               'size'  => '-1',
178             },
179           ];
180         }
181
182         keys %options
183   ];
184
185 };
186
187 </%once>
188 <%init>
189
190 die "access denied"
191   unless $FS::CurrentUser::CurrentUser->access_right('Edit billing events')
192       || $FS::CurrentUser::CurrentUser->access_right('Edit global billing events');
193
194
195 my $html_init =
196   #XXX better description
197   'Events are billing, collection or other actions triggered when certain '.
198   'customer, invoice, package or other conditions are met.<BR><BR>'.
199   qq!<FORM METHOD="POST" ACTION="${p}edit/part_event.html">!.
200   qq!<A HREF="${p}edit/part_event.html"><I>Add a new event</I></A>!.
201   '&nbsp;or&nbsp;<SELECT NAME="clone"><OPTION></OPTION>';
202
203 foreach my $part_event ( qsearch('part_event', {'disabled'=>''}) ) {
204   $html_init .= '<OPTION VALUE="'. $part_event->eventpart. '">'.
205                   $part_event->eventpart. ': '. 
206                   encode_entities($part_event->event).
207                 '</OPTION>';
208 }
209
210 $html_init .= '</SELECT><INPUT TYPE="submit" VALUE="Clone existing event">'.
211               '</FORM><BR>';
212
213 my $hashref = {};
214
215 my $count_query = 'SELECT COUNT(*) FROM part_event WHERE '.
216                   $FS::CurrentUser::CurrentUser->agentnums_sql(
217                     'null_right'    => 'Edit global billing events',
218                     'viewall_right' => 'None',
219                   );
220
221 if ( $cgi->param('eventtable') =~ /^(\w+)$/ ) {
222   $hashref->{eventtable} = $1;
223   $count_query .= " AND eventtable = '$1' ";
224 }
225
226 my $tablenum = '';
227 if ( $cgi->param('test_eventtable_num') =~ /^(\d+)$/ ) {
228   $tablenum = $1;
229 }
230
231 my $join_conditions  = FS::part_event_condition->join_conditions_sql('', 'time' => time);
232 my $order_conditions = FS::part_event_condition->order_conditions_sql;
233
234 </%init>