option to show payment history in reverse order, #19698
[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/ ) {
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   <TR>
47     <TD ALIGN="left" CLASS="grid" BGCOLOR="<% $bgcolor %>">
48 %     my $otaker = $item->history_user;
49 %     $otaker = '<i>auto billing</i>'          if $otaker eq 'fs_daily';
50 %     $otaker = '<i>customer self-service</i>' if $otaker eq 'fs_selfservice';
51 %     $otaker = '<i>job queue</i>'             if $otaker eq 'fs_queue';
52       <% $otaker %>
53     </TD>
54     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
55 %     my $d = time2str('%b %o, %Y', $item->history_date );
56 %     $d =~ s/ /&nbsp;/g;
57       <% $d %>
58     </TD>
59     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
60 %     my $t = time2str('%r', $item->history_date );
61 %     $t =~ s/ /&nbsp;/g;
62       <% $t %>
63     </TD>
64
65 % unless ( $single_cust ) {
66     <TD ALIGN="center" CLASS="grid" BGCOLOR="<% $bgcolor %>">
67 %    my $cust_main = qsearchs('cust_main', { custnum => $item->custnum });
68       <% $cust_main ? "<A HREF='${p}view/cust_main.cgi?"
69                             .$cust_main->custnum."'>".$cust_main->name.'</A>' : '' %>
70     </TD>
71 % }
72
73     <TD ALIGN="center" CLASS="grid" BGCOLOR="<% $bgcolor %>">
74 %       my $label = '';
75 %       $label = $item->table;
76 %       $label =~ s/^h_//;
77 %       $label = $tables{$label};
78 % if ( $single_cust ) {
79 %     $label = &{ $h_table_labelsub{$item->table} }( $item, $label )
80 %       if $h_table_labelsub{$item->table};
81 % }
82       <% $label %>
83     </TD>
84     <TD ALIGN="left" CLASS="grid" BGCOLOR="<% $bgcolor %>">
85       <% $action{$item->history_action} %>
86     </TD>
87     <TD ALIGN="left" CLASS="grid" BGCOLOR="<% $bgcolor %>">
88       <% join(', ',
89            map  { my $value = ( $_ =~ /(^pay(info|cvv)|^ss|_password)$/ ) 
90                                 ? 'N/A'
91                                 : $item->get($_);
92                   $value = time2str($cust_pkg_date_format, $value)
93                    if $item->table eq 'h_cust_pkg'
94                    && $cust_pkg_date_fields{$_}
95                    && $value;
96
97                   $value = substr($value, 0, 77).'...' if length($value) > 80;
98                   $value = encode_entities($value);
99                   "<I>$_</I>:<B>$value</B>";
100                 }
101            grep { $history_other
102                     ? ( $item->get($_) ne $history_other->get($_) )
103                     : ( $item->get($_) =~ /\S/ )
104                 }
105            grep { ! /^(history|custnum$)/i }
106                 $item->fields
107          )
108       %>
109     </TD>
110   </TR>
111
112 % }
113 </TABLE>
114 <%init>
115
116 my %opt = @_;
117 my @history = @{$opt{'history'}};
118 my %tables = %{$opt{'tables'}};
119 my $single_cust = $opt{'single_cust'};
120 my $conf = new FS::Conf;
121 my $curuser = $FS::CurrentUser::CurrentUser;
122
123 die "access deined"
124   unless $curuser->access_right('View customer history');
125
126 my %action = (
127   'insert'      => 'Insert', #'Create',
128   'replace_old' => 'Change&nbsp;from',
129   'replace_new' => 'Change&nbsp;to',
130   'delete'      => 'Remove',
131 );
132
133 my %cust_pkg_date_fields = map { $_=>1 } qw(
134   start_date setup bill last_bill susp adjourn cancel expire contract_end
135   resume change_date
136 );
137
138 # finding the other replace row
139
140 my %replace_other = (
141   'replace_new' => 'replace_old',
142   'replace_old' => 'replace_new',
143 );
144 my %replace_dir = (
145   'replace_new' => '<',
146   'replace_old' => '>',
147 );
148 my %replace_direq = (
149   'replace_new' => '<=',
150   'replace_old' => '>=',
151 );
152 my %replace_op = (
153   'replace_new' => '-',
154   'replace_old' => '+',
155 );
156 my %replace_ord = (
157   'replace_new' => 'DESC',
158   'replace_old' => 'ASC',
159 );
160
161 my $fuzz = 5; #seems like a lot
162
163 my %pkgpart = ();
164 my $pkg_labelsub = sub {
165   my($item, $label) = @_;
166   $pkgpart{$item->pkgpart} ||= $item->part_pkg->pkg;
167   $label. ': <b>'. encode_entities($pkgpart{$item->pkgpart}). '</b>';
168 };
169
170 my $svc_labelsub = sub {
171   my($item, $label) = @_;
172   $label. ': <b>'. encode_entities($item->label($item->history_date)). '</b>';
173 };
174
175 my %h_table_labelsub = (
176   'h_cust_pkg'      => $pkg_labelsub,
177   'h_svc_acct'      => $svc_labelsub,
178   #'h_radius_usergroup' =>
179   'h_svc_domain'    => $svc_labelsub,
180   'h_svc_www'       => $svc_labelsub,
181   'h_svc_forward'   => $svc_labelsub,
182   'h_svc_broadband' => $svc_labelsub,
183   'h_svc_external'  => $svc_labelsub,
184   'h_svc_phone'     => $svc_labelsub,
185   #'h_phone_device'
186 );
187
188 my $cust_pkg_date_format = '%b %o, %Y';
189 $cust_pkg_date_format .= ' %l:%M:%S%P'
190   if $conf->exists('cust_pkg-display_times')
191   || $curuser->option('cust_pkg-display_times');
192
193 @history = sort { $a->history_date <=> $b->history_date
194                   || $a->historynum <=> $b->historynum } @history;
195
196 if ( $curuser->option('history_order') eq 'newest' ) {
197   @history = reverse @history;
198 }
199
200 </%init>