Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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 % if ( $single_cust && $h_table_descripsub{$item->table} ) {
110       <% &{ $h_table_descripsub{$item->table} }( $item ) %>
111 % }
112     </TD>
113   </TR>
114
115 % }
116 </TABLE>
117 <%init>
118
119 my %opt = @_;
120 my @history = @{$opt{'history'}};
121 my %tables = %{$opt{'tables'}};
122 my $single_cust = $opt{'single_cust'};
123 my $conf = new FS::Conf;
124 my $curuser = $FS::CurrentUser::CurrentUser;
125
126 die "access deined"
127   unless $curuser->access_right('View customer history');
128
129 my %action = (
130   'insert'      => 'Insert', #'Create',
131   'replace_old' => 'Change&nbsp;from',
132   'replace_new' => 'Change&nbsp;to',
133   'delete'      => 'Remove',
134 );
135
136 my %cust_pkg_date_fields = map { $_=>1 } qw(
137   start_date setup bill last_bill susp adjourn cancel expire contract_end
138   resume change_date
139 );
140
141 # finding the other replace row
142
143 my %replace_other = (
144   'replace_new' => 'replace_old',
145   'replace_old' => 'replace_new',
146 );
147 my %replace_dir = (
148   'replace_new' => '<',
149   'replace_old' => '>',
150 );
151 my %replace_direq = (
152   'replace_new' => '<=',
153   'replace_old' => '>=',
154 );
155 my %replace_op = (
156   'replace_new' => '-',
157   'replace_old' => '+',
158 );
159 my %replace_ord = (
160   'replace_new' => 'DESC',
161   'replace_old' => 'ASC',
162 );
163
164 my $fuzz = 5; #seems like a lot
165
166 my %pkgpart = ();
167 my $pkg_labelsub = sub {
168   my($item, $label) = @_;
169   $pkgpart{$item->pkgpart} ||= $item->part_pkg->pkg;
170   $label. ': <b>'. encode_entities($pkgpart{$item->pkgpart}). '</b>';
171 };
172
173 my $svc_labelsub = sub {
174   my($item, $label) = @_;
175   $label. ': <b>'. encode_entities($item->label($item->history_date)). '</b>';
176 };
177
178 my %h_table_labelsub = (
179   'h_cust_pkg'      => $pkg_labelsub,
180   'h_svc_acct'      => $svc_labelsub,
181   #'h_radius_usergroup' =>
182   'h_svc_domain'    => $svc_labelsub,
183   'h_svc_www'       => $svc_labelsub,
184   'h_svc_forward'   => $svc_labelsub,
185   'h_svc_broadband' => $svc_labelsub,
186   'h_svc_external'  => $svc_labelsub,
187   'h_svc_phone'     => $svc_labelsub,
188   #'h_phone_device'
189 );
190
191 my $discounts = {};
192 my $discount_descripsub = sub {
193   my($item) = @_;
194   $pkgpart{$item->pkgpart} ||= $item->part_pkg->pkg;
195   my $dnum = $item->discountnum;
196   $discounts->{$dnum} ||= qsearchs({
197     'table'=>'discount',
198     'hashref'=>{'discountnum'=>$dnum}
199   });
200   my $d = $discounts->{$dnum};
201   '(<b>' . encode_entities($d->description_short) . '</b>'
202   . ' on <b>' . encode_entities($pkgpart{$item->pkgpart}) . '</b>)';
203 };
204
205 my %h_table_descripsub = (
206   'h_cust_pkg_discount' => $discount_descripsub,
207 );
208
209 my $cust_pkg_date_format = '%b %o, %Y';
210 $cust_pkg_date_format .= ' %l:%M:%S%P'
211   if $conf->exists('cust_pkg-display_times')
212   || $curuser->option('cust_pkg-display_times');
213
214 @history = sort { $a->history_date <=> $b->history_date
215                   || $a->historynum <=> $b->historynum } @history;
216
217 if ( $curuser->option('history_order') eq 'newest' ) {
218   @history = reverse @history;
219 }
220
221 </%init>