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