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