query billing events to see affected objects, #15142
[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'   => {},
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?'.$part_event->eventpart,
49     actionlabel => 'Event query - '.$part_event->event,
50     width       => 650,
51     height      => 420,
52     close_text  => 'Close',
53   );
54   [#rows
55     [#subcolumns
56       {
57         'data' => $part_event->event,
58         'link' => $p.'edit/part_event.html?'.$part_event->eventpart,
59       },
60       {
61         'data' => ' (query) ',
62         'size' => '-1',
63         'data_style'  => 'b',
64         'onclick' => $onclick,
65       },
66     ],
67   ];
68 };
69
70 my $eventtable_labels = FS::part_event->eventtable_labels;
71 my $eventtable_sub = sub { $eventtable_labels->{ shift->eventtable }; };
72
73 my $check_freq_labels = FS::part_event->check_freq_labels;
74 my $check_freq_sub = sub { $check_freq_labels->{ shift->check_freq }; };
75
76 my $conditions_sub = sub {
77   my $part_event = shift;
78   my $addl = 0;
79
80   [
81     map {
82            my $part_event_condition = $_;
83            my %options = $part_event_condition->options;
84
85            [
86              {
87                'data'       => $part_event_condition->description,
88                'width'      => '100%',
89                'align'      => 'center',
90                'colspan'    => 2,
91                'style'      => ( $addl++ ? 'border-top: 1px solid gray' : '' ), 
92              },
93            ],
94
95            map {
96
97              my $data = $options{$_};
98              if ( ref($data) ) {
99                $data = join('<BR>', keys %$data); #XXX display hash values too?
100              }
101
102              [
103                {
104                  'data'   => $part_event_condition->option_label($_). ':',
105                  'align'  => 'right',
106                  'valign' => 'top',
107                  'size'   => '-1',
108                },
109                {
110                  'data'  => $data,
111                  'align' => 'left',
112                  'size'  => '-1',
113                },
114              ];
115
116            } keys %options
117
118         }
119         $part_event->part_event_condition
120
121   ];
122
123 };
124
125 my $action_sub = sub {
126   my $part_event = shift;
127
128   my %options = $part_event->options;
129   
130   [
131
132     [
133       {
134         'data'       => $part_event->description,
135         'width'      => '100%',
136         'align'      => 'center',
137         'colspan'    => 2,
138       },
139     ],
140
141     map {
142           [
143             {
144               'data'  => $part_event->option_label($_). ':',
145               'align' => 'right',
146               'size'  => '-1',
147             },
148             {
149              'data'  => $options{$_},
150              'align' => 'left',
151               'size'  => '-1',
152             },
153           ];
154         }
155
156         keys %options
157   ];
158
159 };
160
161 </%once>
162 <%init>
163
164 die "access denied"
165   unless $FS::CurrentUser::CurrentUser->access_right('Edit billing events')
166       || $FS::CurrentUser::CurrentUser->access_right('Edit global billing events');
167
168 my $html_init =
169   #XXX better description
170   'Events are billing, collection or other actions triggered when certain '.
171   'customer, invoice, package or other conditions are met.<BR><BR>'.
172   qq!<FORM METHOD="POST" ACTION="${p}edit/part_event.html">!.
173   qq!<A HREF="${p}edit/part_event.html"><I>Add a new event</I></A>!.
174   '&nbsp;or&nbsp;<SELECT NAME="clone"><OPTION></OPTION>';
175
176 foreach my $part_event ( qsearch('part_event', {'diabled'=>''}) ) {
177   $html_init .=  '<OPTION VALUE="'. $part_event->eventpart. '">'.
178                   $part_event->eventpart. ': '. $part_event->event. '</OPTION>';
179 }
180
181 $html_init .= '</SELECT><INPUT TYPE="submit" VALUE="Clone existing event">'.
182               '</FORM><BR>';
183      
184 my $count_query = 'SELECT COUNT(*) FROM part_event WHERE '.
185                   $FS::CurrentUser::CurrentUser->agentnums_sql(
186                     'null_right'    => 'Edit global billing events',
187                     'viewall_right' => 'None',
188                   );
189
190 my $join_conditions  = FS::part_event_condition->join_conditions_sql;
191 my $order_conditions = FS::part_event_condition->order_conditions_sql;
192
193 </%init>