no need for a one-choice "Add new location" location dropdown on prospect add, RT...
[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   <TR>
49
50     <TD ALIGN="left" CLASS="grid" BGCOLOR="<% $bgcolor %>">
51 %     my $otaker = '';
52 %     if ( $item->history_usernum ) {
53 %       $access_user{ $item->history_usernum } ||=
54 %         FS::access_user->by_key( $item->history_usernum );
55 %       $otaker = $access_user{ $item->history_usernum }->username
56 %         if $access_user{ $item->history_usernum };
57 %     }
58 %     $otaker ||= $item->history_user;
59 %     $otaker = '<i>auto billing</i>'          if $otaker eq 'fs_daily';
60 %     $otaker = '<i>customer self-service</i>' if $otaker eq 'fs_selfservice';
61 %     $otaker = '<i>job queue</i>'             if $otaker eq 'fs_queue';
62 %     $otaker = '<i>system bootstrap user</i>' if $otaker eq 'fs_bootstrap';
63       <% $otaker %>
64     </TD>
65
66     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
67 %     my $d = time2str('%b %o, %Y', $item->history_date );
68 %     $d =~ s/ /&nbsp;/g;
69       <% $d %>
70     </TD>
71
72     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
73 %     my $t = time2str('%r', $item->history_date );
74 %     $t =~ s/ /&nbsp;/g;
75       <% $t %>
76     </TD>
77
78 % unless ( $single_cust ) {
79     <TD ALIGN="center" CLASS="grid" BGCOLOR="<% $bgcolor %>">
80 %    my $cust_main = qsearchs('cust_main', { custnum => $item->custnum });
81       <% $cust_main ? "<A HREF='${p}view/cust_main.cgi?"
82                             .$cust_main->custnum."'>".$cust_main->name.'</A>' : '' %>
83     </TD>
84 % }
85
86     <TD ALIGN="center" CLASS="grid" BGCOLOR="<% $bgcolor %>">
87 %       my $label = '';
88 %       if ( $item->table eq 'legacy_cust_history' ) {
89 %         $label = $item->item;
90 %       } else {
91 %         $label = $item->table;
92 %         $label =~ s/^h_//;
93 %         $label = $tables{$label};
94 %         $label = &{ $h_table_labelsub{$item->table} }( $item, $label )
95 %           if $single_cust && $h_table_labelsub{$item->table};
96 %       }
97       <% $label %>
98     </TD>
99
100     <TD ALIGN="left" CLASS="grid" BGCOLOR="<% $bgcolor %>">
101       <% $action{$item->history_action} || ucfirst($item->history_action) |h %>
102     </TD>
103
104     <TD ALIGN="left" CLASS="grid" BGCOLOR="<% $bgcolor %>">
105 %     my $description = '';
106 %     if ( $item->table eq 'legacy_cust_history' ) {
107         <% $item->description |h %>
108 %     } elsif ( $item->table eq 'h_cust_tag' ) {
109 %        $description = &{ $h_table_descripsub{$item->table} }( $item, $tables{'cust_tag'} )
110 %         if $single_cust && $h_table_descripsub{$item->table};
111          <% $description %>
112 %     } else {
113         <% join(', ',
114              map  { my $value = ( $_ =~ /(^pay(info|cvv)|^ss|_password)$/ ) 
115                                   ? 'N/A'
116                                   : $item->get($_);
117                     $value = time2str($cust_pkg_date_format, $value)
118                      if $item->table eq 'h_cust_pkg'
119                      && $cust_pkg_date_fields{$_}
120                      && $value;
121
122                     $value = substr($value, 0, 77).'...' if length($value) > 80;
123                     $value = encode_entities($value);
124                     "<I>$_</I>:<B>$value</B>";
125                   }
126              grep { $history_other
127                       ? ( $item->get($_) ne $history_other->get($_) )
128                       : ( $item->get($_) =~ /\S/ )
129                   }
130              grep { ! /^(history|custnum$)/i }
131                   $item->fields
132            )
133         %>
134 %       if ( $single_cust && $h_table_descripsub{$item->table} ) {
135           <% &{ $h_table_descripsub{$item->table} }( $item ) %>
136 %       }
137 %     }  
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 %h_table_labelsub = (
212   'h_cust_pkg'      => $pkg_labelsub,
213   'h_svc_acct'      => $svc_labelsub,
214   #'h_radius_usergroup' =>
215   'h_svc_domain'    => $svc_labelsub,
216   'h_svc_www'       => $svc_labelsub,
217   'h_svc_forward'   => $svc_labelsub,
218   'h_svc_broadband' => $svc_labelsub,
219   'h_svc_external'  => $svc_labelsub,
220   'h_svc_phone'     => $svc_labelsub,
221   #'h_phone_device'
222   #'h_cust_tag'      => $tag_labelsub,
223 );
224
225 my $tag_descripsub = sub {
226   my($item, $label) = @_;
227   my $part_tag = qsearchs('part_tag',{ tagnum => $item->tagnum });
228   '<SPAN STYLE="background-color: #'.$part_tag->tagcolor.'">'. encode_entities($part_tag->tagname). '</SPAN>';
229 };
230
231 my $discounts = {};
232 my $discount_descripsub = sub {
233   my($item) = @_;
234   $pkgpart{$item->pkgpart} ||= qsearchs({
235     'table' => 'part_pkg',
236     'hashref' => {'pkgpart' => $item->pkgpart}
237   })->pkg;
238   my $dnum = $item->discountnum;
239   $discounts->{$dnum} ||= qsearchs({
240     'table'=>'discount',
241     'hashref'=>{'discountnum'=>$dnum}
242   });
243   my $d = $discounts->{$dnum};
244   '(<b>' . encode_entities($d->description_short) . '</b>'
245   . ' on <b>' . encode_entities($pkgpart{$item->pkgpart}) . '</b>)';
246 };
247
248 my %h_table_descripsub = (
249   'h_cust_pkg_discount' => $discount_descripsub,
250   'h_cust_tag'          => $tag_descripsub,
251 );
252
253 my $cust_pkg_date_format = '%b %o, %Y';
254 $cust_pkg_date_format .= ' %l:%M:%S%P'
255   if $conf->exists('cust_pkg-display_times')
256   || $curuser->option('cust_pkg-display_times');
257
258 @history = sort { $a->history_date <=> $b->history_date
259                   || $a->historynum <=> $b->historynum } @history;
260
261 if ( $curuser->option('history_order') eq 'newest' ) {
262   @history = reverse @history;
263 }
264
265 my %access_user = (); #cache
266
267 </%init>