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