default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / httemplate / view / cust_main / change_history.html
1 % if ( int( time - (keys %years)[0] * 31556736 ) > $start ) {
2     Show:
3 %   my $chy = $cgi->param('change_history-years');
4 %   foreach my $y (keys %years) {
5 %     if ( $y == $years ) {
6         <FONT SIZE="+1"><% $years{$y} %></FONT>
7 %     } else {
8 %       $cgi->param('change_history-years', $y);
9         <A HREF="<% $cgi->self_url %>"><% $years{$y} %></A>
10 %     }
11 %     last if int( time - $y * 31556736 ) < $start;
12 %   }
13 %   $cgi->param('change_history-years', $chy);
14 % }
15
16 <% include("/elements/change_history_common.html",
17         'history'   => \@history,
18         'tables'    => \%tables,
19         'single_cust'   => 1,
20     ) %>
21
22 <%init>
23
24 tie my %years, 'Tie::IxHash',
25     .5 => '6 months',
26    1  => '1 year',
27    2  => '2 years',
28    5  => '5 years',
29   39  => 'all history',
30 ;
31
32 tie my %tables, 'Tie::IxHash',
33   'cust_main'         => 'Customer',
34   'cust_main_invoice' => 'Invoice destination',
35   'cust_main_note'    => 'Note',
36   'cust_pkg'          => 'Package',
37   #? or just svc_* ? 'cust_svc' => 
38   'svc_acct'          => 'Account',
39   'radius_usergroup'  => 'RADIUS group',
40   'svc_domain'        => 'Domain',
41   'svc_www'           => 'Hosting',
42   'svc_forward'       => 'Mail forward',
43   'svc_broadband'     => 'Broadband',
44   'svc_external'      => 'External service',
45   'svc_phone'         => 'Phone',
46   'svc_cable'         => 'Cable',
47   'phone_device'      => 'Phone device',
48   'cust_pkg_discount' => 'Discount',
49   #? it gets provisioned anyway 'phone_avail'         => 'Phone',
50   'cust_tag'          => 'Tag',
51   'contact'           => 'Contact',
52   'cust_contact'      => 'Contact',
53   'contact_phone'     => 'Contact',
54   'contact_email'     => 'Contact',
55 ;
56
57 my $pkg_join = "JOIN cust_pkg USING ( pkgnum )";
58 my $svc_join = "JOIN cust_svc USING ( svcnum ) $pkg_join";
59
60 my @svc_tables = qw(
61   svc_acct
62   svc_domain
63   svc_www
64   svc_forward
65   svc_broadband
66   svc_external
67   svc_phone
68   svc_cable
69 );
70
71 my %table_join = (
72   'radius_usergroup' => $svc_join,
73   'phone_device'     => $svc_join,
74   'cust_pkg_discount'=> $pkg_join,
75 );
76
77 %table_join = (%table_join, map { $_ => $svc_join } @svc_tables);
78
79
80 # cust_main
81 # cust_main_invoice
82
83 # cust_pkg
84 # cust_pkg_option?
85 # cust_pkg_detail
86 # cust_pkg_reason?  no
87
88 #cust_svc
89 #cust_svc_option?
90 #svc_*
91 # svc_acct
92 #  radius_usergroup
93 #  acct_snarf?  is this even used? it is now, for communigate RPOP
94 # svc_domain
95 #  domain_record
96 #  registrar
97 # svc_forward
98 # svc_www
99 # svc_broadband
100 #  (virtual fields?  eh... maybe when they're real)
101 # svc_external
102 # svc_phone
103 #  phone_device
104 #  phone_avail
105
106 # future:
107
108 # inventory_item (from services)
109 # pkg_referral? (changed?)
110
111 #random others:
112
113 # cust_location?
114 # cust_main-exemption?? (295.ca named tax exemptions)
115
116
117 my( $cust_main ) = @_;
118
119 my $conf = new FS::Conf;
120
121 my $curuser = $FS::CurrentUser::CurrentUser;
122
123 die "access denied"
124   unless $curuser->access_right('View customer history');
125
126 # find out the beginning of this customer history, if possible
127 my $h_insert = qsearchs({
128   'table'     => 'h_cust_main',
129   'hashref'   => { 'custnum'        => $cust_main->custnum,
130                    'history_action' => 'insert',
131                  },
132   'extra_sql' => 'ORDER BY historynum LIMIT 1',
133 });
134 my $start = $h_insert ? $h_insert->history_date : 0;
135
136 # retreive the history
137
138 my @history = ();
139
140 my $years = $conf->config('change_history-years') || .5;
141 if ( $cgi->param('change_history-years') =~ /^([\d\.]+)$/ ) {
142   $years = $1;
143 }
144 my $newer_than = int( time - $years * 31556736 ); #60*60*24*365.24
145
146 local($FS::Record::nowarn_classload) = 1;
147
148 my $extra_sql = ' AND custnum = '. $cust_main->custnum;
149
150 my %foundsvcs;
151 foreach my $table ( keys %tables ) {
152   if ($tables{$table} eq 'Contact') {
153
154     my $contact_addl_from = "
155       INNER JOIN (select contactnum, min(history_date) as min_history_date, max(history_date) as max_history_date, max(historynum) as max_historynum FROM h_cust_contact WHERE custnum = ".$cust_main->custnum." AND history_date >= $newer_than GROUP BY contactnum) a ON h_cust_contact.contactnum = a.contactnum
156     ";
157
158     ## get list with add and remove dates of contacts for customer
159     my @h_cust_contacts = qsearch({
160       'table' => "h_cust_contact",
161       'select' => "a.contactnum, a.min_history_date, a.max_history_date, history_action AS max_history_action",
162       'addl_from' => $contact_addl_from,
163       'extra_sql' => " WHERE historynum = a.max_historynum",
164     });
165
166     foreach my $c (@h_cust_contacts) {
167      my $max_date_sql;
168      $max_date_sql = " AND history_date <= ".$c->{Hash}->{max_history_date} if $c->{Hash}->{max_history_action} eq 'delete';
169
170      ## get history of contact while attached to customer
171      my @contact_history = qsearch({
172        'table'      => "h_$table",
173        'hashref'    => {
174           'history_date' =>  { op=>'>=', value=>($c->{Hash}->{min_history_date} - 1) },
175           'contactnum'   => $c->{Hash}->{contactnum},
176        },
177        'extra_sql' => $max_date_sql,
178      });
179      push @history, @contact_history;
180     }
181   }
182   else {
183     my @items = qsearch({
184       'table'     => "h_$table",
185       'addl_from' => $table_join{$table},
186       'hashref'   => { 'history_date' =>  { op=>'>=', value=>$newer_than }, },
187       'extra_sql' => $extra_sql,
188     });
189     %foundsvcs = (%foundsvcs, map { $_->svcnum => 1 } @items)
190       if $table =~ /^svc/;
191     push @history, @items;
192   }
193 }
194
195 ### Load svcs that are no longer linked (cust_svc has been deleted)
196
197 # get list of all svcs for this customer from h_cust_svc
198 # could also load svcdb here by joining to part_svc on svcpart,
199 #   but it would spoil database optimizations on this lookup
200 my @svcnumobj = qsearch({
201   'select' => 'DISTINCT svcnum',
202   'hashref'   => { 'history_date' =>  { op=>'>=', value=>$newer_than } },
203   'table'  => 'h_cust_svc',
204   'addl_from' => 'JOIN cust_pkg USING (pkgnum)',
205   'extra_sql' => ' AND custnum = '. $cust_main->custnum,
206 });
207
208 # now grab those svcs explicitly 
209 foreach my $svcnum ( map { $_->get('svcnum') } @svcnumobj ) {
210   # can skip the services we already found
211   next if $foundsvcs{$svcnum};
212   # grab any given h_cust_svc record for this svcnum to get svcdb
213   my $svcdbobj = qsearchs({
214     'select' => 'svcdb',
215     'hashref' => { svcnum => $svcnum },
216     'table'  => 'h_cust_svc',
217     'addl_from' => 'JOIN part_svc USING (svcpart)',
218     'extra_sql' => 'LIMIT 1',
219   });
220   unless ($svcdbobj && $svcdbobj->get('svcdb')) {
221     # don't think this ever happens, but just in case, don't break history page over it
222     warn "Could not load svcdb for svcnum $svcnum";
223     next;
224   }
225   my @items = qsearch({
226     'table'     => "h_".$svcdbobj->get('svcdb'),
227     'hashref'   => { 'history_date' =>  { op=>'>=', value=>$newer_than },
228                      'svcnum' => $svcnum },
229   });
230   push @history, @items;
231 }
232
233 ## legacy history
234
235 my @legacy_items = qsearch({
236   'table'     => 'legacy_cust_history',
237   'hashref'   => { 'history_date' =>  { op=>'>=', value=>$newer_than }, },
238   'extra_sql' => ' AND custnum = '. $cust_main->custnum,
239 });
240 push @history, @legacy_items;
241
242 </%init>