d46a4ffdee34c326ebd99b14e1e71812236c88c0
[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_pkg'          => 'Package',
36   #? or just svc_* ? 'cust_svc' => 
37   'svc_acct'          => 'Account',
38   'radius_usergroup'  => 'RADIUS group',
39   'svc_domain'        => 'Domain',
40   'svc_www'           => 'Hosting',
41   'svc_forward'       => 'Mail forward',
42   'svc_broadband'     => 'Broadband',
43   'svc_external'      => 'External service',
44   'svc_phone'         => 'Phone',
45   'svc_cable'         => 'Cable',
46   'phone_device'      => 'Phone device',
47   'cust_pkg_discount' => 'Discount',
48   #? it gets provisioned anyway 'phone_avail'         => 'Phone',
49 ;
50
51 my $pkg_join = "JOIN cust_pkg USING ( pkgnum )";
52 my $svc_join = "JOIN cust_svc USING ( svcnum ) $pkg_join";
53
54 my @svc_tables = qw(
55   svc_acct
56   svc_domain
57   svc_www
58   svc_forward
59   svc_broadband
60   svc_external
61   svc_phone
62   svc_cable
63 );
64
65 my %table_join = (
66   'radius_usergroup' => $svc_join,
67   'phone_device'     => $svc_join,
68   'cust_pkg_discount'=> $pkg_join,
69 );
70
71 %table_join = (%table_join, map { $_ => $svc_join } @svc_tables);
72
73
74 # cust_main
75 # cust_main_invoice
76
77 # cust_pkg
78 # cust_pkg_option?
79 # cust_pkg_detail
80 # cust_pkg_reason?  no
81
82 #cust_svc
83 #cust_svc_option?
84 #svc_*
85 # svc_acct
86 #  radius_usergroup
87 #  acct_snarf?  is this even used? it is now, for communigate RPOP
88 # svc_domain
89 #  domain_record
90 #  registrar
91 # svc_forward
92 # svc_www
93 # svc_broadband
94 #  (virtual fields?  eh... maybe when they're real)
95 # svc_external
96 # svc_phone
97 #  phone_device
98 #  phone_avail
99
100 # future:
101
102 # inventory_item (from services)
103 # pkg_referral? (changed?)
104
105 #random others:
106
107 # cust_location?
108 # cust_main-exemption?? (295.ca named tax exemptions)
109
110
111 my( $cust_main ) = @_;
112
113 my $conf = new FS::Conf;
114
115 my $curuser = $FS::CurrentUser::CurrentUser;
116
117 die "access denied"
118   unless $curuser->access_right('View customer history');
119
120 # find out the beginning of this customer history, if possible
121 my $h_insert = qsearchs({
122   'table'     => 'h_cust_main',
123   'hashref'   => { 'custnum'        => $cust_main->custnum,
124                    'history_action' => 'insert',
125                  },
126   'extra_sql' => 'ORDER BY historynum LIMIT 1',
127 });
128 my $start = $h_insert ? $h_insert->history_date : 0;
129
130 # retreive the history
131
132 my @history = ();
133
134 my $years = $conf->config('change_history-years') || .5;
135 if ( $cgi->param('change_history-years') =~ /^([\d\.]+)$/ ) {
136   $years = $1;
137 }
138 my $newer_than = int( time - $years * 31556736 ); #60*60*24*365.24
139
140 local($FS::Record::nowarn_classload) = 1;
141
142 my %foundsvcs;
143 foreach my $table ( keys %tables ) {
144   my @items = qsearch({
145     'table'     => "h_$table",
146     'addl_from' => $table_join{$table},
147     'hashref'   => { 'history_date' =>  { op=>'>=', value=>$newer_than }, },
148     'extra_sql' => ' AND custnum = '. $cust_main->custnum,
149   });
150   %foundsvcs = (%foundsvcs, map { $_->svcnum => 1 } @items)
151     if $table =~ /^svc/;
152   push @history, @items;
153 }
154
155 ### Load svcs that are no longer linked (cust_svc has been deleted)
156
157 # get list of all svcs for this customer from h_cust_svc
158 # could also load svcdb here by joining to part_svc on svcpart,
159 #   but it would spoil database optimizations on this lookup
160 my @svcnumobj = qsearch({
161   'select' => 'DISTINCT svcnum',
162   'hashref'   => { 'history_date' =>  { op=>'>=', value=>$newer_than } },
163   'table'  => 'h_cust_svc',
164   'addl_from' => 'JOIN cust_pkg USING (pkgnum)',
165   'extra_sql' => ' AND custnum = '. $cust_main->custnum,
166 });
167
168 # now grab those svcs explicitly 
169 foreach my $svcnum ( map { $_->get('svcnum') } @svcnumobj ) {
170   # can skip the services we already found
171   next if $foundsvcs{$svcnum};
172   # grab any given h_cust_svc record for this svcnum to get svcdb
173   my $svcdbobj = qsearchs({
174     'select' => 'svcdb',
175     'hashref' => { svcnum => $svcnum },
176     'table'  => 'h_cust_svc',
177     'addl_from' => 'JOIN part_svc USING (svcpart)',
178     'extra_sql' => 'LIMIT 1',
179   });
180   unless ($svcdbobj && $svcdbobj->get('svcdb')) {
181     # don't think this ever happens, but just in case, don't break history page over it
182     warn "Could not load svcdb for svcnum $svcnum";
183     next;
184   }
185   my @items = qsearch({
186     'table'     => "h_".$svcdbobj->get('svcdb'),
187     'hashref'   => { 'history_date' =>  { op=>'>=', value=>$newer_than },
188                      'svcnum' => $svcnum },
189   });
190   push @history, @items;
191 }
192
193 ## legacy history
194
195 my @legacy_items = qsearch({
196   'table'     => 'legacy_cust_history',
197   'hashref'   => { 'history_date' =>  { op=>'>=', value=>$newer_than }, },
198   'extra_sql' => ' AND custnum = '. $cust_main->custnum,
199 });
200 push @history, @legacy_items;
201
202 </%init>