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