Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / httemplate / search / cust_event.html
index f9cce46..241a026 100644 (file)
@@ -1,4 +1,4 @@
-<% include( 'elements/search.html',
+<& elements/search.html,
                  'title'       => $title,
                  'html_init'   => $html_init,
                  'menubar'     => $menubar,
                               #'',
                               FS::UI::Web::cust_styles(),
                             ],
-             )
-%>
+&>
 <%once>
 
 my $status_sub = sub { 
   my $cust_event = shift;
 
   my $status = $cust_event->status;
-  $status .= ': '.$cust_event->statustext
+  $status .= ': '. encode_entities($cust_event->statustext)
     if $cust_event->statustext;
 
   my $part_event = $cust_event->part_event;
 
-  if ( $part_event->eventtable eq 'cust_bill' && $part_event->templatename ) {
-    my $alt_templatename = $part_event->templatename;
-    my $alt_link = "$alt_templatename-". $cust_event->tablenum;
+  if ( $part_event->eventtable eq 'cust_bill'
+       && ( $part_event->templatename || $part_event->option('notice_name') )
+     )
+  {
+    my $link = 'invnum='. $cust_event->tablenum;
+    $link .= ';template='. uri_escape($part_event->templatename)
+      if $part_event->templatename;
+    $link .= ';notice_name='. uri_escape($part_event->option('notice_name'))
+      if $part_event->option('notice_name');
 
     my $conf = new FS::Conf;
     my $cust_bill = $cust_event->cust_X;
 
     $status .= qq{
-          ( <A HREF="${p}view/cust_bill.cgi?$alt_link">view</A>
-          | <A HREF="${p}view/cust_bill-pdf.cgi?$alt_link.pdf">view
-              typeset</A>
-          | <A HREF="${p}misc/print-invoice.cgi?$alt_link">re-print</A>
+          ( <A HREF="${p}view/cust_bill.cgi?$link">view</A>
+          | <A HREF="${p}view/cust_bill-pdf.cgi?$link">view&nbsp;typeset</A>
+          | <A HREF="${p}misc/send-invoice.cgi?method=print;$link">re-print</A>
     };
 
     if ( grep { $_ ne 'POST' } $cust_bill->cust_main->invoicing_list ) { 
       $status .= qq{
-            | <A HREF="${p}misc/email-invoice.cgi?$alt_link">re-email</A>
+            | <A HREF="${p}misc/send-invoice.cgi?method=email;$link">re-email</A>
       };
     } 
    
     if ( $conf->exists('hylafax') && length($cust_bill->cust_main->fax) ) { 
       $status .= qq{
-            | <A HREF="${p}misc/fax-invoice.cgi?$alt_link">re-fax</A>
+            | <A HREF="${p}misc/send-invoice.cgi?method=fax;$link">re-fax</A>
       }
     } 
 
@@ -124,7 +128,12 @@ my $trigger_link = sub {
   my $eventtable = $cust_event->eventtable;
   if ( $eventtable eq 'cust_pkg' ) {
     my $custnum = $cust_event->cust_main_custnum;
-    [ "${p}view/cust_main.cgi?$custnum#cust_pkg", 'tablenum' ];
+    my $show = $FS::CurrentUser::CurrentUser->default_customer_view =~ /^(jumbo|packages)$/
+                 ? ''
+                 : ';show=packages';
+    my $pkgnum = $cust_event->tablenum;
+    my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
+    [ "${p}view/cust_main.cgi?custnum=$custnum$show;fragment=$frag#cust_pkg", 'tablenum' ];
   } else {
     [ "${p}view/$eventtable.cgi?", 'tablenum' ];
   }
@@ -142,63 +151,42 @@ die "access denied"
               || $cgi->param('invnum')  =~ /^(\d+)$/
               || $cgi->param('pkgnum')  =~ /^(\d+)$/
             );
-         
 
-my $title = $cgi->param('failed')
-              ? 'Failed billing events'
-              : 'Billing events';
-
-my @search = ();
-
-if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
-  push @search, "agentnum = $1";
-  #my $agent = qsearchs('agent', { 'agentnum' => $1 } );
-  #die "unknown agentnum $1" unless $agent;
+my @statuses = $cgi->multi_param('event_status');
+my $title = 'Billing events';
+if ( $statuses[0] eq 'failed' and !defined($statuses[1]) ) {
+  # tweak the title if we're showing only failed events
+  $title = 'Failed billing events';
 }
 
-my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
-push @search, "cust_event._date >= $beginning",
-              "cust_event._date <= $ending";
+my %search = ();
 
-if ( $cgi->param('failed') ) {
-  push @search, "statustext != ''",
-                "statustext IS NOT NULL",
-                "statustext != 'N/A'";
+my @scalars = qw( agentnum status custnum invnum pkgnum failed );
+for my $param (@scalars) {
+  $search{$param} = scalar( $cgi->param($param) )
+    if $cgi->param($param);
 }
+$search{event_status} = \@statuses;
 
-#if ( $cgi->param('part_event.payby') =~ /^(\w+)$/ ) {
-#  push @search, "part_event.payby = '$1'";
-#}
-
-if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
-  push @search, "cust_main.custnum = '$1'";
-}
-if ( $cgi->param('invnum') =~ /^(\d+)$/ ) {
-  push @search, "part_event.eventtable = 'cust_bill'",
-                "tablenum = '$1'";
-}
-if ( $cgi->param('pkgnum') =~ /^(\d+)$/ ) {
-  push @search, "part_event.eventtable = 'cust_pkg'",
-                "tablenum = '$1'";
+#lists
+my @lists = qw( eventpart );
+foreach my $param (@lists) {
+  $search{$param} = [ $cgi->param($param) ];
 }
 
-#here is the agent virtualization
-my $agent_sql = $curuser->agentnums_sql;
-$agent_sql =~ s/agentnum/cust_main.agentnum/g;
-push @search, $agent_sql;
+my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
+$search{'beginning'} = $beginning;
+$search{'ending'}    = $ending;
 
-my $where = 'WHERE '. join(' AND ', @search );
+my $where = ' WHERE '. FS::cust_event->search_sql_where( \%search );
 
-my $join = "
-       JOIN part_event USING ( eventpart )
-  LEFT JOIN cust_bill ON ( eventtable = 'cust_bill' AND tablenum = invnum  )
-  LEFT JOIN cust_pkg  ON ( eventtable = 'cust_pkg'  AND tablenum = pkgnum  )
-  LEFT JOIN cust_main ON (    ( eventtable = 'cust_main' AND tablenum = cust_main.custnum )
-                           OR ( eventtable = 'cust_bill' AND cust_bill.custnum = cust_main.custnum )
-                           OR ( eventtable = 'cust_pkg'  AND cust_pkg.custnum  = cust_main.custnum )
-                         )
-";
-           #'LEFT JOIN cust_main  USING ( custnum   ) ';
+my $join = FS::cust_event->join_sql() .
+  'LEFT JOIN cust_location bill_location '.
+  'ON (cust_main.bill_locationnum = bill_location.locationnum) '.
+  'LEFT JOIN cust_location ship_location '.
+  'ON (cust_main.ship_locationnum = ship_location.locationnum)';
+  # warning: does not show the true service address for package events.
+  # the query to do that would be painfully slow.
 
 my $sql_query = {
   'table'     => 'cust_event',
@@ -211,30 +199,39 @@ my $sql_query = {
                     FS::UI::Web::cust_sql_fields(),
                   ),
   'hashref'   => {}, 
-  'extra_sql' => "$where ORDER BY _date ASC",
+  'extra_sql' => $where,
+  'order_by'  => 'ORDER BY _date ASC',
   'addl_from' => $join,
+  'debug' => 2,
 };
 
 my $count_sql = "SELECT COUNT(*) FROM cust_event $join $where";
 
 my $conf = new FS::Conf;
 
-my $failed = $cgi->param('failed');
+my @params = ( @scalars, qw( beginning ending ) );
 
 my $html_init = join("\n", map {
   ( my $action = $_ ) =~ s/_$//;
   include('/elements/progress-init.html',
             $_.'form',
-            [ 'action', 'beginning', 'ending', 'failed' ],
+            [ 'action', @params ],
             "../misc/${_}events.cgi",
             { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
             $_, #key
          ),
   qq!<FORM NAME="${_}form">!,
   qq!<INPUT TYPE="hidden" NAME="action" VALUE="$_">!, #not used though
-  qq!<INPUT TYPE="hidden" NAME="beginning" VALUE="$beginning">!,
-  qq!<INPUT TYPE="hidden" NAME="ending"    VALUE="$ending">!,
-  qq!<INPUT TYPE="hidden" NAME="failed"    VALUE="$failed">!,
+  ( map { my $value = encode_entities( $search{$_} );
+          qq(<INPUT TYPE="hidden" NAME="$_" VALUE="$value">);
+        }
+        @params #keys %search
+  ),
+  ( map { my $value = encode_entities( join(',', @{ $search{$_} } ) );
+          qq(<INPUT TYPE="hidden" NAME="$_" VALUE="$value">);
+        }
+        @lists
+  ),
   qq!</FORM>!
 } qw( print_ email_ fax_ ) ).
 
@@ -261,16 +258,21 @@ function confirm_fax_process() {
 
 </SCRIPT>';
 
-my $menubar =  [
-                 'Re-print these events' =>
-                   "javascript:confirm_print_process()",
-                 'Re-email these events' =>
-                   "javascript:confirm_email_process()",
-               ];
+my $menubar = [];
+
+if ( $curuser->access_right('Resend invoices') ) {
+
+  push @$menubar, 'Re-print these events' =>
+                    "javascript:confirm_print_process()",
+                  'Re-email these events' =>
+                    "javascript:confirm_email_process()",
+                ;
 
-push @$menubar, 'Re-fax these events' =>
-                  "javascript:confirm_fax_process()"
-  if $conf->exists('hylafax');
+  push @$menubar, 'Re-fax these events' =>
+                    "javascript:confirm_fax_process()"
+    if $conf->exists('hylafax');
+
+}
 
 my $link_cust = sub {
   my $cust_event = shift;