fix A/R report
[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 %     if ( $item->table eq 'legacy_cust_history' ) {
106         <% $item->description |h %>
107 %     } else {
108         <% join(', ',
109              map  { my $value = ( $_ =~ /(^pay(info|cvv)|^ss|_password)$/ ) 
110                                   ? 'N/A'
111                                   : $item->get($_);
112                     $value = time2str($cust_pkg_date_format, $value)
113                      if $item->table eq 'h_cust_pkg'
114                      && $cust_pkg_date_fields{$_}
115                      && $value;
116
117                     $value = substr($value, 0, 77).'...' if length($value) > 80;
118                     $value = encode_entities($value);
119                     "<I>$_</I>:<B>$value</B>";
120                   }
121              grep { $history_other
122                       ? ( $item->get($_) ne $history_other->get($_) )
123                       : ( $item->get($_) =~ /\S/ )
124                   }
125              grep { ! /^(history|custnum$)/i }
126                   $item->fields
127            )
128         %>
129 %       if ( $single_cust && $h_table_descripsub{$item->table} ) {
130           <% &{ $h_table_descripsub{$item->table} }( $item ) %>
131 %       }
132 %     }
133     </TD>
134
135   </TR>
136
137 % }
138 </TABLE>
139 <%init>
140
141 my %opt = @_;
142 my @history = @{$opt{'history'}};
143 my %tables = %{$opt{'tables'}};
144 my $single_cust = $opt{'single_cust'};
145 my $conf = new FS::Conf;
146 my $curuser = $FS::CurrentUser::CurrentUser;
147
148 die "access deined"
149   unless $curuser->access_right('View customer history');
150
151 my %action = (
152   'insert'      => 'Insert', #'Create',
153   'replace_old' => "Change\xA0from",
154   'replace_new' => "Change\xA0to",
155   'delete'      => 'Remove',
156 );
157
158 my %cust_pkg_date_fields = map { $_=>1 } qw(
159   start_date setup bill last_bill susp adjourn cancel expire contract_end
160   resume change_date
161 );
162
163 # finding the other replace row
164
165 my %replace_other = (
166   'replace_new' => 'replace_old',
167   'replace_old' => 'replace_new',
168 );
169 my %replace_dir = (
170   'replace_new' => '<',
171   'replace_old' => '>',
172 );
173 my %replace_direq = (
174   'replace_new' => '<=',
175   'replace_old' => '>=',
176 );
177 my %replace_op = (
178   'replace_new' => '-',
179   'replace_old' => '+',
180 );
181 my %replace_ord = (
182   'replace_new' => 'DESC',
183   'replace_old' => 'ASC',
184 );
185
186 my $fuzz = 5; #seems like a lot
187
188 my %pkgpart = ();
189 my $pkg_labelsub = sub {
190   my($item, $label) = @_;
191   $pkgpart{$item->pkgpart} ||= $item->part_pkg->pkg;
192   $label. ': <b>'. encode_entities($pkgpart{$item->pkgpart}). '</b>';
193 };
194
195 my $svc_labelsub = sub {
196   my($item, $label) = @_;
197   $label. ': <b>'. encode_entities($item->label($item->history_date)). '</b>';
198 };
199
200 my $tag_labelsub = sub {
201   my($item, $label) = @_;
202   my $part_tag = qsearchs('part_tag',{ tagnum => $item->tagnum });
203   $label. ': <SPAN STYLE="background-color: #'.$part_tag->tagcolor.'">'. encode_entities($part_tag->tagname). '</SPAN>';
204 };
205
206 my %h_table_labelsub = (
207   'h_cust_pkg'      => $pkg_labelsub,
208   'h_svc_acct'      => $svc_labelsub,
209   #'h_radius_usergroup' =>
210   'h_svc_domain'    => $svc_labelsub,
211   'h_svc_www'       => $svc_labelsub,
212   'h_svc_forward'   => $svc_labelsub,
213   'h_svc_broadband' => $svc_labelsub,
214   'h_svc_external'  => $svc_labelsub,
215   'h_svc_phone'     => $svc_labelsub,
216   #'h_phone_device'
217   'h_cust_tag'      => $tag_labelsub,
218 );
219
220 my $discounts = {};
221 my $discount_descripsub = sub {
222   my($item) = @_;
223   $pkgpart{$item->pkgpart} ||= qsearchs({
224     'table' => 'part_pkg',
225     'hashref' => {'pkgpart' => $item->pkgpart}
226   })->pkg;
227   my $dnum = $item->discountnum;
228   $discounts->{$dnum} ||= qsearchs({
229     'table'=>'discount',
230     'hashref'=>{'discountnum'=>$dnum}
231   });
232   my $d = $discounts->{$dnum};
233   '(<b>' . encode_entities($d->description_short) . '</b>'
234   . ' on <b>' . encode_entities($pkgpart{$item->pkgpart}) . '</b>)';
235 };
236
237 my %h_table_descripsub = (
238   'h_cust_pkg_discount' => $discount_descripsub,
239 );
240
241 my $cust_pkg_date_format = '%b %o, %Y';
242 $cust_pkg_date_format .= ' %l:%M:%S%P'
243   if $conf->exists('cust_pkg-display_times')
244   || $curuser->option('cust_pkg-display_times');
245
246 @history = sort { $a->history_date <=> $b->history_date
247                   || $a->historynum <=> $b->historynum } @history;
248
249 if ( $curuser->option('history_order') eq 'newest' ) {
250   @history = reverse @history;
251 }
252
253 my %access_user = (); #cache
254
255 </%init>