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