RT# 79497 - Changed email address check to all emails to be seperated by a space...
[freeside.git] / httemplate / view / cust_main / change_history.html
index 1700bc3..d8f0d1e 100644 (file)
 %   $cgi->param('change_history-years', $chy);
 % }
 
-<% include("/elements/table-grid.html") %>
-% my $bgcolor1 = '#eeeeee';
-%   my $bgcolor2 = '#ffffff';
-%   my $bgcolor = '';
-
-<TR>
-  <TH CLASS="grid" BGCOLOR="#cccccc">User</TH>
-  <TH CLASS="grid" BGCOLOR="#cccccc">Date</TH>
-  <TH CLASS="grid" BGCOLOR="#cccccc">Time</TH>
-  <TH CLASS="grid" BGCOLOR="#cccccc">Item</TH>
-  <TH CLASS="grid" BGCOLOR="#cccccc">Action</TH>
-  <TH CLASS="grid" BGCOLOR="#cccccc">Description</TH>
-</TR>
-
-% foreach my $item ( sort { $a->history_date <=> $b->history_date
-%                           #|| table order
-%                           || $a->historynum <=> $b->historynum
-%                         }
-%                         @history
-%                  )
-% {
-%
-%   my $history_other = '';
-%   my $act  = $item->history_action;
-%   if ( $act =~ /^replace/ ) {
-%     my $pkey = $item->primary_key;
-%     my $date = $item->history_date;
-%     $history_other = qsearchs({
-%       'table'     => $item->table,
-%       'hashref'   => { $pkey            => $item->$pkey(),
-%                        'history_action' => $replace_other{$act},
-%                        'historynum'     => { 'op'    => $replace_dir{$act},
-%                                              'value' => $item->historynum
-%                                            },
-%                      },
-%       'extra_sql' => "
-%         AND history_date $replace_direq{$act} $date
-%         AND ($date $replace_op{$act} $fuzz) $replace_direq{$act} history_date
-%         ORDER BY historynum $replace_ord{$act} LIMIT 1
-%       ",
-%     });
-%   }
-%
-%   if ( $bgcolor eq $bgcolor1 ) {
-%     $bgcolor = $bgcolor2;
-%   } else {
-%     $bgcolor = $bgcolor1;
-%   }
-
-  <TR>
-    <TD ALIGN="left" CLASS="grid" BGCOLOR="<% $bgcolor %>">
-%     my $otaker = $item->history_user;
-%     $otaker = '<i>auto billing</i>'          if $otaker eq 'fs_daily';
-%     $otaker = '<i>customer self-service</i>' if $otaker eq 'fs_selfservice';
-%     $otaker = '<i>job queue</i>'             if $otaker eq 'fs_queue';
-      <% $otaker %>
-    </TD>
-    <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
-%     my $d = time2str('%b %o, %Y', $item->history_date );
-%     $d =~ s/ /&nbsp;/g;
-      <% $d %>
-    </TD>
-    <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
-%     my $t = time2str('%r', $item->history_date );
-%     $t =~ s/ /&nbsp;/g;
-      <% $t %>
-    </TD>
-    <TD ALIGN="center" CLASS="grid" BGCOLOR="<% $bgcolor %>">
-%     my $label = $h_tables{$item->table};
-%     $label = &{ $h_table_labelsub{$item->table} }( $item, $label )
-%       if $h_table_labelsub{$item->table};
-      <% $label %>
-    </TD>
-    <TD ALIGN="left" CLASS="grid" BGCOLOR="<% $bgcolor %>">
-      <% $action{$item->history_action} %>
-    </TD>
-    <TD ALIGN="left" CLASS="grid" BGCOLOR="<% $bgcolor %>">
-      <% join(', ',
-           map  { my $value = ( $_ =~ /(^pay(info|cvv)|^ss|_password)$/ ) 
-                                ? 'N/A'
-                                : $item->get($_);
-                  $value = substr($value, 0, 77).'...' if length($value) > 80;
-                  $value = encode_entities($value);
-                  "<I>$_</I>:<B>$value</B>";
-                }
-           grep { $history_other
-                    ? ( $item->get($_) ne $history_other->get($_) )
-                    : ( $item->get($_) =~ /\S/ )
-                }
-           grep { ! /^(history|custnum$)/i }
-                $item->fields
-         )
-      %>
-    </TD>
-  </TR>
-
-% }
+<% include("/elements/change_history_common.html",
+        'history'   => \@history,
+        'tables'    => \%tables,
+        'single_cust'   => 1,
+    ) %>
 
-</TABLE>
-<%once>
-
-# length-switching 
+<%init>
 
 tie my %years, 'Tie::IxHash',
     .5 => '6 months',
@@ -124,45 +29,10 @@ tie my %years, 'Tie::IxHash',
   39  => 'all history',
 ;
 
-# labeling history rows
-
-my %action = (
-  'insert'      => 'Insert', #'Create',
-  'replace_old' => 'Change&nbsp;from',
-  'replace_new' => 'Change&nbsp;to',
-  'delete'      => 'Remove',
-);
-
-# finding the other replace row
-
-my %replace_other = (
-  'replace_new' => 'replace_old',
-  'replace_old' => 'replace_new',
-);
-my %replace_dir = (
-  'replace_new' => '<',
-  'replace_old' => '>',
-);
-my %replace_direq = (
-  'replace_new' => '<=',
-  'replace_old' => '>=',
-);
-my %replace_op = (
-  'replace_new' => '-',
-  'replace_old' => '+',
-);
-my %replace_ord = (
-  'replace_new' => 'DESC',
-  'replace_old' => 'ASC',
-);
-
-my $fuzz = 5; #seems like a lot
-
-# which tables to search and what to call them
-
 tie my %tables, 'Tie::IxHash',
   'cust_main'         => 'Customer',
   'cust_main_invoice' => 'Invoice destination',
+  'cust_main_note'    => 'Note',
   'cust_pkg'          => 'Package',
   #? or just svc_* ? 'cust_svc' => 
   'svc_acct'          => 'Account',
@@ -173,50 +43,37 @@ tie my %tables, 'Tie::IxHash',
   'svc_broadband'     => 'Broadband',
   'svc_external'      => 'External service',
   'svc_phone'         => 'Phone',
+  'svc_cable'         => 'Cable',
   'phone_device'      => 'Phone device',
+  'cust_pkg_discount' => 'Discount',
   #? it gets provisioned anyway 'phone_avail'         => 'Phone',
+  'cust_tag'          => 'Tag',
+  'contact'           => 'Contact',
 ;
 
-my $svc_join = 'JOIN cust_svc USING ( svcnum ) JOIN cust_pkg USING ( pkgnum )';
+my $pkg_join = "JOIN cust_pkg USING ( pkgnum )";
+my $svc_join = "JOIN cust_svc USING ( svcnum ) $pkg_join";
+
+my @svc_tables = qw(
+  svc_acct
+  svc_domain
+  svc_www
+  svc_forward
+  svc_broadband
+  svc_external
+  svc_phone
+  svc_cable
+);
 
 my %table_join = (
-  'svc_acct'         => $svc_join,
   'radius_usergroup' => $svc_join,
-  'svc_domain'       => $svc_join,
-  'svc_www'          => $svc_join,
-  'svc_forward'      => $svc_join,
-  'svc_broadband'    => $svc_join,
-  'svc_external'     => $svc_join,
-  'svc_phone'        => $svc_join,
   'phone_device'     => $svc_join,
+  'cust_pkg_discount'=> $pkg_join,
+  'contact'          => "JOIN cust_contact USING ( contactnum )",
 );
 
-my %h_tables = map { ( "h_$_" => $tables{$_} ) } keys %tables;
-
-my %pkgpart = ();
-my $pkg_labelsub = sub {
-  my($item, $label) = @_;
-  $pkgpart{$item->pkgpart} ||= $item->part_pkg->pkg;
-  $label. ': <b>'. encode_entities($pkgpart{$item->pkgpart}). '</b>';
-};
-
-my $svc_labelsub = sub {
-  my($item, $label) = @_;
-  $label. ': <b>'. encode_entities($item->label). '</b>';
-};
-
-my %h_table_labelsub = (
-  'h_cust_pkg'      => $pkg_labelsub,
-  'h_svc_acct'      => $svc_labelsub,
-  #'h_radius_usergroup' =>
-  'h_svc_domain'    => $svc_labelsub,
-  'h_svc_www'       => $svc_labelsub,
-  'h_svc_forward'   => $svc_labelsub,
-  'h_svc_broadband' => $svc_labelsub,
-  'h_svc_external'  => $svc_labelsub,
-  'h_svc_phone'     => $svc_labelsub,
-  #'h_phone_device'
-);
+%table_join = (%table_join, map { $_ => $svc_join } @svc_tables);
+
 
 # cust_main
 # cust_main_invoice
@@ -231,7 +88,7 @@ my %h_table_labelsub = (
 #svc_*
 # svc_acct
 #  radius_usergroup
-#  acct_snarf?  is this even used?
+#  acct_snarf?  is this even used? it is now, for communigate RPOP
 # svc_domain
 #  domain_record
 #  registrar
@@ -254,8 +111,6 @@ my %h_table_labelsub = (
 # cust_location?
 # cust_main-exemption?? (295.ca named tax exemptions)
 
-</%once>
-<%init>
 
 my( $cust_main ) = @_;
 
@@ -263,7 +118,7 @@ my $conf = new FS::Conf;
 
 my $curuser = $FS::CurrentUser::CurrentUser;
 
-die "access deined"
+die "access denied"
   unless $curuser->access_right('View customer history');
 
 # find out the beginning of this customer history, if possible
@@ -288,15 +143,67 @@ my $newer_than = int( time - $years * 31556736 ); #60*60*24*365.24
 
 local($FS::Record::nowarn_classload) = 1;
 
+my $extra_sql = ' AND custnum = '. $cust_main->custnum;
+
+my %foundsvcs;
 foreach my $table ( keys %tables ) {
+  $extra_sql = ' AND cust_contact.custnum = '. $cust_main->custnum if $table eq 'contact';
   my @items = qsearch({
     'table'     => "h_$table",
     'addl_from' => $table_join{$table},
     'hashref'   => { 'history_date' =>  { op=>'>=', value=>$newer_than }, },
-    'extra_sql' => ' AND custnum = '. $cust_main->custnum,
+    '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;
+  }
+  my @items = qsearch({
+    'table'     => "h_".$svcdbobj->get('svcdb'),
+    'hashref'   => { 'history_date' =>  { op=>'>=', value=>$newer_than },
+                     'svcnum' => $svcnum },
+  });
+  push @history, @items;
 }
 
+## legacy history
+
+my @legacy_items = qsearch({
+  'table'     => 'legacy_cust_history',
+  'hashref'   => { 'history_date' =>  { op=>'>=', value=>$newer_than }, },
+  'extra_sql' => ' AND custnum = '. $cust_main->custnum,
+});
+push @history, @legacy_items;
+
 </%init>