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