This commit was generated by cvs2svn to compensate for changes in r9232,
[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 = substr($value, 0, 77).'...' if length($value) > 80;
98                   $value = encode_entities($value);
99                   "<I>$_</I>:<B>$value</B>";
100                 }
101            grep { $history_other
102                     ? ( $item->get($_) ne $history_other->get($_) )
103                     : ( $item->get($_) =~ /\S/ )
104                 }
105            grep { ! /^(history|custnum$)/i }
106                 $item->fields
107          )
108       %>
109     </TD>
110   </TR>
111
112 % }
113
114 </TABLE>
115 <%once>
116
117 # length-switching 
118
119 tie my %years, 'Tie::IxHash',
120     .5 => '6 months',
121    1  => '1 year',
122    2  => '2 years',
123    5  => '5 years',
124   39  => 'all history',
125 ;
126
127 # labeling history rows
128
129 my %action = (
130   'insert'      => 'Insert', #'Create',
131   'replace_old' => 'Change&nbsp;from',
132   'replace_new' => 'Change&nbsp;to',
133   'delete'      => 'Remove',
134 );
135
136 # finding the other replace row
137
138 my %replace_other = (
139   'replace_new' => 'replace_old',
140   'replace_old' => 'replace_new',
141 );
142 my %replace_dir = (
143   'replace_new' => '<',
144   'replace_old' => '>',
145 );
146 my %replace_direq = (
147   'replace_new' => '<=',
148   'replace_old' => '>=',
149 );
150 my %replace_op = (
151   'replace_new' => '-',
152   'replace_old' => '+',
153 );
154 my %replace_ord = (
155   'replace_new' => 'DESC',
156   'replace_old' => 'ASC',
157 );
158
159 my $fuzz = 5; #seems like a lot
160
161 # which tables to search and what to call them
162
163 tie my %tables, 'Tie::IxHash',
164   'cust_main'         => 'Customer',
165   'cust_main_invoice' => 'Invoice destination',
166   'cust_pkg'          => 'Package',
167   #? or just svc_* ? 'cust_svc' => 
168   'svc_acct'          => 'Account',
169   'radius_usergroup'  => 'RADIUS group',
170   'svc_domain'        => 'Domain',
171   'svc_www'           => 'Hosting',
172   'svc_forward'       => 'Mail forward',
173   'svc_broadband'     => 'Broadband',
174   'svc_external'      => 'External service',
175   'svc_phone'         => 'Phone',
176   'phone_device'      => 'Phone device',
177   #? it gets provisioned anyway 'phone_avail'         => 'Phone',
178 ;
179
180 my $svc_join = 'JOIN cust_svc USING ( svcnum ) JOIN cust_pkg USING ( pkgnum )';
181
182 my %table_join = (
183   'svc_acct'         => $svc_join,
184   'radius_usergroup' => $svc_join,
185   'svc_domain'       => $svc_join,
186   'svc_www'          => $svc_join,
187   'svc_forward'      => $svc_join,
188   'svc_broadband'    => $svc_join,
189   'svc_external'     => $svc_join,
190   'svc_phone'        => $svc_join,
191   'phone_device'     => $svc_join,
192 );
193
194 my %h_tables = map { ( "h_$_" => $tables{$_} ) } keys %tables;
195
196 my %pkgpart = ();
197 my $pkg_labelsub = sub {
198   my($item, $label) = @_;
199   $pkgpart{$item->pkgpart} ||= $item->part_pkg->pkg;
200   $label. ': <b>'. encode_entities($pkgpart{$item->pkgpart}). '</b>';
201 };
202
203 my $svc_labelsub = sub {
204   my($item, $label) = @_;
205   $label. ': <b>'. encode_entities($item->label($item->history_date)). '</b>';
206 };
207
208 my %h_table_labelsub = (
209   'h_cust_pkg'      => $pkg_labelsub,
210   'h_svc_acct'      => $svc_labelsub,
211   #'h_radius_usergroup' =>
212   'h_svc_domain'    => $svc_labelsub,
213   'h_svc_www'       => $svc_labelsub,
214   'h_svc_forward'   => $svc_labelsub,
215   'h_svc_broadband' => $svc_labelsub,
216   'h_svc_external'  => $svc_labelsub,
217   'h_svc_phone'     => $svc_labelsub,
218   #'h_phone_device'
219 );
220
221 # cust_main
222 # cust_main_invoice
223
224 # cust_pkg
225 # cust_pkg_option?
226 # cust_pkg_detail
227 # cust_pkg_reason?  no
228
229 #cust_svc
230 #cust_svc_option?
231 #svc_*
232 # svc_acct
233 #  radius_usergroup
234 #  acct_snarf?  is this even used?
235 # svc_domain
236 #  domain_record
237 #  registrar
238 # svc_forward
239 # svc_www
240 # svc_broadband
241 #  (virtual fields?  eh... maybe when they're real)
242 # svc_external
243 # svc_phone
244 #  phone_device
245 #  phone_avail
246
247 # future:
248
249 # inventory_item (from services)
250 # pkg_referral? (changed?)
251
252 #random others:
253
254 # cust_location?
255 # cust_main-exemption?? (295.ca named tax exemptions)
256
257 </%once>
258 <%init>
259
260 my( $cust_main ) = @_;
261
262 my $conf = new FS::Conf;
263
264 my $curuser = $FS::CurrentUser::CurrentUser;
265
266 die "access deined"
267   unless $curuser->access_right('View customer history');
268
269 # find out the beginning of this customer history, if possible
270 my $h_insert = qsearchs({
271   'table'     => 'h_cust_main',
272   'hashref'   => { 'custnum'        => $cust_main->custnum,
273                    'history_action' => 'insert',
274                  },
275   'extra_sql' => 'ORDER BY historynum LIMIT 1',
276 });
277 my $start = $h_insert ? $h_insert->history_date : 0;
278
279 # retreive the history
280
281 my @history = ();
282
283 my $years = $conf->config('change_history-years') || .5;
284 if ( $cgi->param('change_history-years') =~ /^([\d\.]+)$/ ) {
285   $years = $1;
286 }
287 my $newer_than = int( time - $years * 31556736 ); #60*60*24*365.24
288
289 local($FS::Record::nowarn_classload) = 1;
290
291 foreach my $table ( keys %tables ) {
292   my @items = qsearch({
293     'table'     => "h_$table",
294     'addl_from' => $table_join{$table},
295     'hashref'   => { 'history_date' =>  { op=>'>=', value=>$newer_than }, },
296     'extra_sql' => ' AND custnum = '. $cust_main->custnum,
297   });
298   push @history, @items;
299
300 }
301
302 </%init>