+ if ($table eq 'cust_contact') { next; } #skip as dont need to search table but hack to show contact name
+ elsif ($table eq 'contact') {
+
+ my $contact_addl_from = "
+ 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
+ ";
+
+ ## get list with add and remove dates of contacts for customer
+ my @h_cust_contacts = qsearch({
+ 'table' => "h_cust_contact",
+ 'select' => "a.contactnum, a.min_history_date, a.max_history_date, history_action AS max_history_action",
+ 'addl_from' => $contact_addl_from,
+ 'extra_sql' => " WHERE historynum = a.max_historynum",
+ });
+
+ foreach my $c (@h_cust_contacts) {
+ my $max_date_sql;
+ $max_date_sql = " AND history_date <= ".$c->{Hash}->{max_history_date} if $c->{Hash}->{max_history_action} eq 'delete';
+
+ ## get history of contact while attached to customer
+ my @contact_history = qsearch({
+ 'table' => "h_contact",
+ 'hashref' => {
+ 'history_date' => { op=>'>=', value=>$c->{Hash}->{min_history_date} },
+ 'contactnum' => $c->{Hash}->{contactnum},
+ },
+ 'extra_sql' => $max_date_sql,
+ });
+
+ ## get history of customer specific changes to contact.
+ my @cust_contact_history = qsearch({
+ 'table' => "h_cust_contact",
+ 'hashref' => {
+ 'history_date' => { op=>'>=', value=>$c->{Hash}->{min_history_date} },
+ 'contactnum' => $c->{Hash}->{contactnum},
+ 'custnum' => $cust_main->custnum,
+ },
+ });
+
+ push @history, @contact_history; push @history, @cust_contact_history;
+ }
+ }
+ else {
+ my @items = qsearch({
+ 'table' => "h_$table",
+ 'addl_from' => $table_join{$table},
+ 'hashref' => { 'history_date' => { op=>'>=', value=>$newer_than }, },
+ 'extra_sql' => $extra_sql,
+ });
+ %foundsvcs = (%foundsvcs, map { $_->svcnum => 1 } @items)
+ if $table =~ /^svc/;
+ push @history, @items;
+ }
+}
+
+### Load svcs that are no longer linked (cust_svc has been deleted)
+
+# get list of all svcs for this customer from h_cust_svc
+# could also load svcdb here by joining to part_svc on svcpart,
+# but it would spoil database optimizations on this lookup
+my @svcnumobj = qsearch({
+ 'select' => 'DISTINCT svcnum',
+ 'hashref' => { 'history_date' => { op=>'>=', value=>$newer_than } },
+ 'table' => 'h_cust_svc',
+ 'addl_from' => 'JOIN cust_pkg USING (pkgnum)',
+ 'extra_sql' => ' AND custnum = '. $cust_main->custnum,
+});
+
+# now grab those svcs explicitly
+foreach my $svcnum ( map { $_->get('svcnum') } @svcnumobj ) {
+ # can skip the services we already found
+ next if $foundsvcs{$svcnum};
+ # grab any given h_cust_svc record for this svcnum to get svcdb
+ my $svcdbobj = qsearchs({
+ 'select' => 'svcdb',
+ 'hashref' => { svcnum => $svcnum },
+ 'table' => 'h_cust_svc',
+ 'addl_from' => 'JOIN part_svc USING (svcpart)',
+ 'extra_sql' => 'LIMIT 1',
+ });
+ unless ($svcdbobj && $svcdbobj->get('svcdb')) {
+ # don't think this ever happens, but just in case, don't break history page over it
+ warn "Could not load svcdb for svcnum $svcnum";
+ next;
+ }