better legacy history display, RT#29311
[freeside.git] / httemplate / elements / change_history_common.html
1 <% include("/elements/table-grid.html") %>
2 % my $bgcolor1 = '#eeeeee';
3 %   my $bgcolor2 = '#ffffff';
4 %   my $bgcolor = '';
5
6 <TR>
7   <TH CLASS="grid" BGCOLOR="#cccccc">User</TH>
8   <TH CLASS="grid" BGCOLOR="#cccccc">Date</TH>
9   <TH CLASS="grid" BGCOLOR="#cccccc">Time</TH>
10   <% $single_cust ? '' : 
11            '<TH CLASS="grid" BGCOLOR="#cccccc">Customer</TH>'
12   %>
13   <TH CLASS="grid" BGCOLOR="#cccccc">Item</TH>
14   <TH CLASS="grid" BGCOLOR="#cccccc">Action</TH>
15   <TH CLASS="grid" BGCOLOR="#cccccc">Description</TH>
16 </TR>
17
18 % foreach my $item ( @history ) {
19 %   my $history_other = '';
20 %   my $act  = $item->history_action;
21 %   if ( $act =~ /^replace/ && $item->table ne 'legacy_cust_history' ) {
22 %     my $pkey = $item->primary_key;
23 %     my $date = $item->history_date;
24 %     $history_other = qsearchs({
25 %       'table'     => $item->table,
26 %       'hashref'   => { $pkey            => $item->$pkey(),
27 %                        'history_action' => $replace_other{$act},
28 %                        'historynum'     => { 'op'    => $replace_dir{$act},
29 %                                              'value' => $item->historynum
30 %                                            },
31 %                      },
32 %       'extra_sql' => "
33 %         AND history_date $replace_direq{$act} $date
34 %         AND ($date $replace_op{$act} $fuzz) $replace_direq{$act} history_date
35 %         ORDER BY historynum $replace_ord{$act} LIMIT 1
36 %       ",
37 %     });
38 %   }
39 %
40 %   if ( $bgcolor eq $bgcolor1 ) {
41 %     $bgcolor = $bgcolor2;
42 %   } else {
43 %     $bgcolor = $bgcolor1;
44 %   }
45
46   <TR>
47
48     <TD ALIGN="left" CLASS="grid" BGCOLOR="<% $bgcolor %>">
49 %     my $otaker = '';
50 %     if ( $item->history_usernum ) {
51 %       $access_user{ $item->history_usernum } ||=
52 %         FS::access_user->by_key( $item->history_usernum );
53 %       $otaker = $access_user{ $item->history_usernum }->username
54 %         if $access_user{ $item->history_usernum };
55 %     }
56 %     $otaker ||= $item->history_user;
57 %     $otaker = '<i>auto billing</i>'          if $otaker eq 'fs_daily';
58 %     $otaker = '<i>customer self-service</i>' if $otaker eq 'fs_selfservice';
59 %     $otaker = '<i>job queue</i>'             if $otaker eq 'fs_queue';
60 %     $otaker = '<i>system bootstrap user</i>' if $otaker eq 'fs_bootstrap';
61       <% $otaker %>
62     </TD>
63
64     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
65 %     my $d = time2str('%b %o, %Y', $item->history_date );
66 %     $d =~ s/ /&nbsp;/g;
67       <% $d %>
68     </TD>
69
70     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
71 %     my $t = time2str('%r', $item->history_date );
72 %     $t =~ s/ /&nbsp;/g;
73       <% $t %>
74     </TD>
75
76 % unless ( $single_cust ) {
77     <TD ALIGN="center" CLASS="grid" BGCOLOR="<% $bgcolor %>">
78 %    my $cust_main = qsearchs('cust_main', { custnum => $item->custnum });
79       <% $cust_main ? "<A HREF='${p}view/cust_main.cgi?"
80                             .$cust_main->custnum."'>".$cust_main->name.'</A>' : '' %>
81     </TD>
82 % }
83
84     <TD ALIGN="center" CLASS="grid" BGCOLOR="<% $bgcolor %>">
85 %       my $label = '';
86 %       if ( $item->table eq 'legacy_cust_history' ) {
87 %         $label = $item->item;
88 %       } else {
89 %         $label = $item->table;
90 %         $label =~ s/^h_//;
91 %         $label = $tables{$label};
92 %         $label = &{ $h_table_labelsub{$item->table} }( $item, $label )
93 %           if $single_cust && $h_table_labelsub{$item->table};
94 %       }
95       <% $label %>
96     </TD>
97
98     <TD ALIGN="left" CLASS="grid" BGCOLOR="<% $bgcolor %>">
99       <% $action{$item->history_action} || ucfirst($item->history_action) |h %>
100     </TD>
101
102     <TD ALIGN="left" CLASS="grid" BGCOLOR="<% $bgcolor %>">
103 %     if ( $item->table eq 'legacy_cust_history' ) {
104         <% $item->description |h %>
105 %     } else {
106         <% join(', ',
107              map  { my $value = ( $_ =~ /(^pay(info|cvv)|^ss|_password)$/ ) 
108                                   ? 'N/A'
109                                   : $item->get($_);
110                     $value = time2str($cust_pkg_date_format, $value)
111                      if $item->table eq 'h_cust_pkg'
112                      && $cust_pkg_date_fields{$_}
113                      && $value;
114
115                     $value = substr($value, 0, 77).'...' if length($value) > 80;
116                     $value = encode_entities($value);
117                     "<I>$_</I>:<B>$value</B>";
118                   }
119              grep { $history_other
120                       ? ( $item->get($_) ne $history_other->get($_) )
121                       : ( $item->get($_) =~ /\S/ )
122                   }
123              grep { ! /^(history|custnum$)/i }
124                   $item->fields
125            )
126         %>
127 %       if ( $single_cust && $h_table_descripsub{$item->table} ) {
128           <% &{ $h_table_descripsub{$item->table} }( $item ) %>
129 %       }
130 %     }
131     </TD>
132
133   </TR>
134
135 % }
136 </TABLE>
137 <%init>
138
139 my %opt = @_;
140 my @history = @{$opt{'history'}};
141 my %tables = %{$opt{'tables'}};
142 my $single_cust = $opt{'single_cust'};
143 my $conf = new FS::Conf;
144 my $curuser = $FS::CurrentUser::CurrentUser;
145
146 die "access deined"
147   unless $curuser->access_right('View customer history');
148
149 my %action = (
150   'insert'      => 'Insert', #'Create',
151   'replace_old' => 'Change&nbsp;from',
152   'replace_new' => 'Change&nbsp;to',
153   'delete'      => 'Remove',
154 );
155
156 my %cust_pkg_date_fields = map { $_=>1 } qw(
157   start_date setup bill last_bill susp adjourn cancel expire contract_end
158   resume change_date
159 );
160
161 # finding the other replace row
162
163 my %replace_other = (
164   'replace_new' => 'replace_old',
165   'replace_old' => 'replace_new',
166 );
167 my %replace_dir = (
168   'replace_new' => '<',
169   'replace_old' => '>',
170 );
171 my %replace_direq = (
172   'replace_new' => '<=',
173   'replace_old' => '>=',
174 );
175 my %replace_op = (
176   'replace_new' => '-',
177   'replace_old' => '+',
178 );
179 my %replace_ord = (
180   'replace_new' => 'DESC',
181   'replace_old' => 'ASC',
182 );
183
184 my $fuzz = 5; #seems like a lot
185
186 my %pkgpart = ();
187 my $pkg_labelsub = sub {
188   my($item, $label) = @_;
189   $pkgpart{$item->pkgpart} ||= $item->part_pkg->pkg;
190   $label. ': <b>'. encode_entities($pkgpart{$item->pkgpart}). '</b>';
191 };
192
193 my $svc_labelsub = sub {
194   my($item, $label) = @_;
195   $label. ': <b>'. encode_entities($item->label($item->history_date)). '</b>';
196 };
197
198 my %h_table_labelsub = (
199   'h_cust_pkg'      => $pkg_labelsub,
200   'h_svc_acct'      => $svc_labelsub,
201   #'h_radius_usergroup' =>
202   'h_svc_domain'    => $svc_labelsub,
203   'h_svc_www'       => $svc_labelsub,
204   'h_svc_forward'   => $svc_labelsub,
205   'h_svc_broadband' => $svc_labelsub,
206   'h_svc_external'  => $svc_labelsub,
207   'h_svc_phone'     => $svc_labelsub,
208   #'h_phone_device'
209 );
210
211 my $discounts = {};
212 my $discount_descripsub = sub {
213   my($item) = @_;
214   $pkgpart{$item->pkgpart} ||= qsearchs({
215     'table' => 'part_pkg',
216     'hashref' => {'pkgpart' => $item->pkgpart}
217   })->pkg;
218   my $dnum = $item->discountnum;
219   $discounts->{$dnum} ||= qsearchs({
220     'table'=>'discount',
221     'hashref'=>{'discountnum'=>$dnum}
222   });
223   my $d = $discounts->{$dnum};
224   '(<b>' . encode_entities($d->description_short) . '</b>'
225   . ' on <b>' . encode_entities($pkgpart{$item->pkgpart}) . '</b>)';
226 };
227
228 my %h_table_descripsub = (
229   'h_cust_pkg_discount' => $discount_descripsub,
230 );
231
232 my $cust_pkg_date_format = '%b %o, %Y';
233 $cust_pkg_date_format .= ' %l:%M:%S%P'
234   if $conf->exists('cust_pkg-display_times')
235   || $curuser->option('cust_pkg-display_times');
236
237 @history = sort { $a->history_date <=> $b->history_date
238                   || $a->historynum <=> $b->historynum } @history;
239
240 if ( $curuser->option('history_order') eq 'newest' ) {
241   @history = reverse @history;
242 }
243
244 my %access_user = (); #cache
245
246 </%init>