This commit was generated by cvs2svn to compensate for changes in r11022,
[freeside.git] / httemplate / view / cust_main / change_history.html
1 % if ( int( time - (keys %years)[0] * 31556736 ) > $start ) {
2     Show:
3 %   my $chy = $cgi->param('change_history-years');
4 %   foreach my $y (keys %years) {
5 %     if ( $y == $years ) {
6         <FONT SIZE="+1"><% $years{$y} %></FONT>
7 %     } else {
8 %       $cgi->param('change_history-years', $y);
9         <A HREF="<% $cgi->self_url %>"><% $years{$y} %></A>
10 %     }
11 %     last if int( time - $y * 31556736 ) < $start;
12 %   }
13 %   $cgi->param('change_history-years', $chy);
14 % }
15
16 <% include("/elements/table-grid.html") %>
17 % my $bgcolor1 = '#eeeeee';
18 %   my $bgcolor2 = '#ffffff';
19 %   my $bgcolor = '';
20
21 <TR>
22   <TH CLASS="grid" BGCOLOR="#cccccc">User</TH>
23   <TH CLASS="grid" BGCOLOR="#cccccc">Date</TH>
24   <TH CLASS="grid" BGCOLOR="#cccccc">Time</TH>
25   <TH CLASS="grid" BGCOLOR="#cccccc">Item</TH>
26   <TH CLASS="grid" BGCOLOR="#cccccc">Action</TH>
27   <TH CLASS="grid" BGCOLOR="#cccccc">Description</TH>
28 </TR>
29
30 % foreach my $item ( sort { $a->history_date <=> $b->history_date
31 %                           #|| table order
32 %                           || $a->historynum <=> $b->historynum
33 %                         }
34 %                         @history
35 %                  )
36 % {
37 %
38 %   my $history_other = '';
39 %   my $act  = $item->history_action;
40 %   if ( $act =~ /^replace/ ) {
41 %     my $pkey = $item->primary_key;
42 %     my $date = $item->history_date;
43 %     $history_other = qsearchs({
44 %       'table'     => $item->table,
45 %       'hashref'   => { $pkey            => $item->$pkey(),
46 %                        'history_action' => $replace_other{$act},
47 %                        'historynum'     => { 'op'    => $replace_dir{$act},
48 %                                              'value' => $item->historynum
49 %                                            },
50 %                      },
51 %       'extra_sql' => "
52 %         AND history_date $replace_direq{$act} $date
53 %         AND ($date $replace_op{$act} $fuzz) $replace_direq{$act} history_date
54 %         ORDER BY historynum $replace_ord{$act} LIMIT 1
55 %       ",
56 %     });
57 %   }
58 %
59 %   if ( $bgcolor eq $bgcolor1 ) {
60 %     $bgcolor = $bgcolor2;
61 %   } else {
62 %     $bgcolor = $bgcolor1;
63 %   }
64
65   <TR>
66     <TD ALIGN="left" CLASS="grid" BGCOLOR="<% $bgcolor %>">
67 %     my $otaker = $item->history_user;
68 %     $otaker = '<i>auto billing</i>'          if $otaker eq 'fs_daily';
69 %     $otaker = '<i>customer self-service</i>' if $otaker eq 'fs_selfservice';
70 %     $otaker = '<i>job queue</i>'             if $otaker eq 'fs_queue';
71       <% $otaker %>
72     </TD>
73     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
74 %     my $d = time2str('%b %o, %Y', $item->history_date );
75 %     $d =~ s/ /&nbsp;/g;
76       <% $d %>
77     </TD>
78     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
79 %     my $t = time2str('%r', $item->history_date );
80 %     $t =~ s/ /&nbsp;/g;
81       <% $t %>
82     </TD>
83     <TD ALIGN="center" CLASS="grid" BGCOLOR="<% $bgcolor %>">
84 %     my $label = $h_tables{$item->table};
85 %     $label = &{ $h_table_labelsub{$item->table} }( $item, $label )
86 %       if $h_table_labelsub{$item->table};
87       <% $label %>
88     </TD>
89     <TD ALIGN="left" CLASS="grid" BGCOLOR="<% $bgcolor %>">
90       <% $action{$item->history_action} %>
91     </TD>
92     <TD ALIGN="left" CLASS="grid" BGCOLOR="<% $bgcolor %>">
93       <% join(', ',
94            map  { my $value = ( $_ =~ /(^pay(info|cvv)|^ss|_password)$/ ) 
95                                 ? 'N/A'
96                                 : $item->get($_);
97                   $value = time2str($cust_pkg_date_format, $value)
98                    if $item->table eq 'h_cust_pkg'
99                    && $cust_pkg_date_fields{$_}
100                    && $value;
101
102                   $value = substr($value, 0, 77).'...' if length($value) > 80;
103                   $value = encode_entities($value);
104                   "<I>$_</I>:<B>$value</B>";
105                 }
106            grep { $history_other
107                     ? ( $item->get($_) ne $history_other->get($_) )
108                     : ( $item->get($_) =~ /\S/ )
109                 }
110            grep { ! /^(history|custnum$)/i }
111                 $item->fields
112          )
113       %>
114     </TD>
115   </TR>
116
117 % }
118
119 </TABLE>
120 <%once>
121
122 # length-switching 
123
124 tie my %years, 'Tie::IxHash',
125     .5 => '6 months',
126    1  => '1 year',
127    2  => '2 years',
128    5  => '5 years',
129   39  => 'all history',
130 ;
131
132 # labeling history rows
133
134 my %action = (
135   'insert'      => 'Insert', #'Create',
136   'replace_old' => 'Change&nbsp;from',
137   'replace_new' => 'Change&nbsp;to',
138   'delete'      => 'Remove',
139 );
140
141 # finding the other replace row
142
143 my %replace_other = (
144   'replace_new' => 'replace_old',
145   'replace_old' => 'replace_new',
146 );
147 my %replace_dir = (
148   'replace_new' => '<',
149   'replace_old' => '>',
150 );
151 my %replace_direq = (
152   'replace_new' => '<=',
153   'replace_old' => '>=',
154 );
155 my %replace_op = (
156   'replace_new' => '-',
157   'replace_old' => '+',
158 );
159 my %replace_ord = (
160   'replace_new' => 'DESC',
161   'replace_old' => 'ASC',
162 );
163
164 my $fuzz = 5; #seems like a lot
165
166 # which tables to search and what to call them
167
168 tie my %tables, 'Tie::IxHash',
169   'cust_main'         => 'Customer',
170   'cust_main_invoice' => 'Invoice destination',
171   'cust_pkg'          => 'Package',
172   #? or just svc_* ? 'cust_svc' => 
173   'svc_acct'          => 'Account',
174   'radius_usergroup'  => 'RADIUS group',
175   'svc_domain'        => 'Domain',
176   'svc_www'           => 'Hosting',
177   'svc_forward'       => 'Mail forward',
178   'svc_broadband'     => 'Broadband',
179   'svc_external'      => 'External service',
180   'svc_phone'         => 'Phone',
181   'phone_device'      => 'Phone device',
182   #? it gets provisioned anyway 'phone_avail'         => 'Phone',
183 ;
184
185 my $svc_join = 'JOIN cust_svc USING ( svcnum ) JOIN cust_pkg USING ( pkgnum )';
186
187 my %table_join = (
188   'svc_acct'         => $svc_join,
189   'radius_usergroup' => $svc_join,
190   'svc_domain'       => $svc_join,
191   'svc_www'          => $svc_join,
192   'svc_forward'      => $svc_join,
193   'svc_broadband'    => $svc_join,
194   'svc_external'     => $svc_join,
195   'svc_phone'        => $svc_join,
196   'phone_device'     => $svc_join,
197 );
198
199 my %h_tables = map { ( "h_$_" => $tables{$_} ) } keys %tables;
200
201 my %pkgpart = ();
202 my $pkg_labelsub = sub {
203   my($item, $label) = @_;
204   $pkgpart{$item->pkgpart} ||= $item->part_pkg->pkg;
205   $label. ': <b>'. encode_entities($pkgpart{$item->pkgpart}). '</b>';
206 };
207
208 my $svc_labelsub = sub {
209   my($item, $label) = @_;
210   $label. ': <b>'. encode_entities($item->label($item->history_date)). '</b>';
211 };
212
213 my %h_table_labelsub = (
214   'h_cust_pkg'      => $pkg_labelsub,
215   'h_svc_acct'      => $svc_labelsub,
216   #'h_radius_usergroup' =>
217   'h_svc_domain'    => $svc_labelsub,
218   'h_svc_www'       => $svc_labelsub,
219   'h_svc_forward'   => $svc_labelsub,
220   'h_svc_broadband' => $svc_labelsub,
221   'h_svc_external'  => $svc_labelsub,
222   'h_svc_phone'     => $svc_labelsub,
223   #'h_phone_device'
224 );
225
226 # cust_main
227 # cust_main_invoice
228
229 # cust_pkg
230 # cust_pkg_option?
231 # cust_pkg_detail
232 # cust_pkg_reason?  no
233
234 #cust_svc
235 #cust_svc_option?
236 #svc_*
237 # svc_acct
238 #  radius_usergroup
239 #  acct_snarf?  is this even used? it is now, for communigate RPOP
240 # svc_domain
241 #  domain_record
242 #  registrar
243 # svc_forward
244 # svc_www
245 # svc_broadband
246 #  (virtual fields?  eh... maybe when they're real)
247 # svc_external
248 # svc_phone
249 #  phone_device
250 #  phone_avail
251
252 # future:
253
254 # inventory_item (from services)
255 # pkg_referral? (changed?)
256
257 #random others:
258
259 # cust_location?
260 # cust_main-exemption?? (295.ca named tax exemptions)
261
262 </%once>
263 <%init>
264
265 my( $cust_main ) = @_;
266
267 my $conf = new FS::Conf;
268
269 my $curuser = $FS::CurrentUser::CurrentUser;
270
271 die "access deined"
272   unless $curuser->access_right('View customer history');
273
274 my $cust_pkg_date_format = '%b %o, %Y';
275 $cust_pkg_date_format .= ' %l:%M:%S%P'
276   if $conf->exists('cust_pkg-display_times')
277   || $curuser->option('cust_pkg-display_times');
278
279 my %cust_pkg_date_fields = map { $_=>1 } qw(
280   start_date setup bill last_bill susp adjourn cancel expire contract_end
281   change_date
282 );
283
284 # find out the beginning of this customer history, if possible
285 my $h_insert = qsearchs({
286   'table'     => 'h_cust_main',
287   'hashref'   => { 'custnum'        => $cust_main->custnum,
288                    'history_action' => 'insert',
289                  },
290   'extra_sql' => 'ORDER BY historynum LIMIT 1',
291 });
292 my $start = $h_insert ? $h_insert->history_date : 0;
293
294 # retreive the history
295
296 my @history = ();
297
298 my $years = $conf->config('change_history-years') || .5;
299 if ( $cgi->param('change_history-years') =~ /^([\d\.]+)$/ ) {
300   $years = $1;
301 }
302 my $newer_than = int( time - $years * 31556736 ); #60*60*24*365.24
303
304 local($FS::Record::nowarn_classload) = 1;
305
306 foreach my $table ( keys %tables ) {
307   my @items = qsearch({
308     'table'     => "h_$table",
309     'addl_from' => $table_join{$table},
310     'hashref'   => { 'history_date' =>  { op=>'>=', value=>$newer_than }, },
311     'extra_sql' => ' AND custnum = '. $cust_main->custnum,
312   });
313   push @history, @items;
314
315 }
316
317 </%init>