deposit slips
[freeside.git] / httemplate / search / cust_msg.html
index 64fae30..65460f7 100644 (file)
@@ -1,56 +1,83 @@
-<& 'elements/search.html',
-       'title' => $title,
-       'name'  => 'messages',
-       'query' => $query,
-       'count_query' => $count_query,
-       'header' => [ 
-                     'Date',
-                     'Template',
-                     'Destination',
-                     'Status',
-                     '', #error
-                  ],
-       'fields' => [ 
-                     sub { 
-                       my $date = $_[0]->_date;
-                       $date ? time2str('%Y-%m-%d %T',$_[0]->_date) : '' 
-                     },
-                     'msgname',
-                     sub {
-                       join('<BR>', split(/,\s*/, $_[0]->env_to) )
-                     },
-                     'status',
-                     sub { encode_entities($_[0]->error) },
-                  ],
-       'align' => 'rllcl',
-       'links' => [ ],
+<& elements/search.html,
+       'title'         => $title,
+       'name_singular' => 'message',
+       'query'         => $query,
+       'count_query'   => $count_query,
+       'header'        => [ 
+                            'Date',
+                            'Type',
+                            'Destination',
+                            'Status',
+                            '', #error
+                         ],
+       'fields'        => [ 
+                            sub { 
+                              my $date = $_[0]->_date;
+                              $date ? time2str('%Y-%m-%d %T',$_[0]->_date) : '' 
+                            },
+                            sub {
+                              ucfirst($_[0]->msgtype) || $_[0]->msgname
+                            },
+                            sub {
+                              join('<BR>',
+                                map { encode_entities($_->format) }
+                                Email::Address->parse($_[0]->env_to)
+                              )
+                            },
+                            'status',
+                            sub { encode_entities($_[0]->error) },
+                         ],
+       'sort_fields'   => [ '_date',
+                            'msgtype',
+                            'env_to',
+                            'status',
+                            'error',
+                          ],
+       'align'         => 'rllcl',
+       'links'         => [],
        'link_onclicks' => [ 
-                    $sub_popup_link,
-                    $sub_popup_link,
-                    $sub_popup_link,
-                    '',
-                    '',
-                  ],
-       'color' => [ ('') x 3, 
-                     $statuscolor,
-                     $statuscolor,
-                  ],
-       'html_init' => $html_init,
+                            $sub_popup_link,
+                            $sub_popup_link,
+                            $sub_popup_link,
+                            '',
+                            '',
+                          ],
+       'color'         => [ '',
+                            $typecolor,
+                            '',
+                            $statuscolor,
+                            $statuscolor,
+                          ],
+       'html_init'     => $html_init,
        'really_disable_download' => 1,
+       'order_by'      => '_date DESC',
 &>
 <%init>
 #hmm...
 die "access denied"
-  unless $FS::CurrentUser::CurrentUser->access_right('View customers of all agents');
+  unless $FS::CurrentUser::CurrentUser->access_right('View email logs');
 
 my $conf = new FS::Conf;
 
 my $title = 'Outgoing Message Log';
 
-my @where;
+#here is the agent virtualization
+my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql(
+  'table' => 'cust_main',
+  'null_right' => [ 'View system logs', 'Configuration' ],
+);
+
+my @where = ( $agentnums_sql );
+
 if ( $cgi->param('status') =~ /^(\w+)$/ ) {
   push @where, "status = '$1'";
 }
+if ( $cgi->param('msgtype') =~ /^(\w+)$/ ) {
+  push @where, "msgtype = '$1'";
+}
+if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
+  push @where, "cust_msg.custnum = $1";
+}
 my ($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, '');
 push @where, "(_date >= $beginning AND _date <= $ending)";
 
@@ -59,57 +86,77 @@ if ( $cgi->param('order_by') =~ /^(\w+)$/ ) {
   $order_by = $1;
 }
 
-my $where = '';
-$where = ' WHERE '.join(' AND ', @where) if @where;
+my $where = ' WHERE '. join(' AND ', @where);
+
+my $join_cust_main = ' LEFT JOIN cust_main USING ( custnum ) ';
+
 my $query = {
   'table'     => 'cust_msg',
   'select'    => join(', ',
                    'cust_msg.*',
                    'msg_template.msgname',
                  ),
-  'addl_from' => ' LEFT JOIN msg_template USING ( msgnum )',
+  'addl_from' => ' LEFT JOIN msg_template USING ( msgnum ) '. $join_cust_main,
   'hashref'   => {},
   'extra_sql' => $where,
   'order_by'  => "ORDER BY $order_by",
 };
-my $count_query = 'SELECT COUNT(*) FROM cust_msg'.$where;
+my $count_query = "SELECT COUNT(*) FROM cust_msg $join_cust_main $where";
 
 my $sub_popup_link = sub {
   my $custmsgnum = $_[0]->custmsgnum;
   include('/elements/popup_link_onclick.html',
     'action'      => $p. 'view/cust_msg.html?' . $custmsgnum,
     'actionlabel' => 'Message detail',
-    'width'       => 600,
-    'height'      => 500,
+    'width'       => 680,
+    'height'      => 550,
   );
 };
 
 my %color = (
   'failed'   => 'FF0000',
   'sent'     => '',
+
+  'invoice'  => '00CC00',
+  'receipt'  => '0000CC',
+  'admin'    => 'CC0000',
+  ''         => '000000',
 );
 my $statuscolor = sub { $color{$_[0]->status} };
+my $typecolor = sub { $color{$_[0]->msgtype} };
 
 my $html_init = qq!<FORM ACTION="$p/search/cust_msg.html" METHOD="GET">
 <TABLE cellspacing="10">!.
 '<TR><TD>From '.
 include('/elements/input-date-field.html',
-  { 'name'  => 'beginning', 'value' => $cgi->param('beginning') }
+  { 'name'  => 'beginning', 'value' => scalar($cgi->param('beginning')) }
 ).
 '</TD><TD> To '.
 include('/elements/input-date-field.html',
-  { 'name'  => 'ending', 'value' => ($cgi->param('ending') || ''), 
+  { 'name'  => 'ending', 'value' => (scalar($cgi->param('ending')) || ''), 
     'noinit' => 1, }
 ).
 '</TD><TD> Status '.
 include('/elements/select.html',
   'field' => 'status',
-  'curr_value' => $cgi->param('status') || '',
+  'curr_value' => scalar($cgi->param('status')) || '',
   'options' => [ '', 'failed', 'sent', ],
   'labels' => { ''        => '(any)', 
                 'failed'  => 'failed',
                 'sent'    => 'sent', },
 ) .
+'</TD><TD> Type '.
+include('/elements/select.html',
+  'field' => 'msgtype',
+  'curr_value' => scalar($cgi->param('msgtype')) || '',
+  'options' => [ '', 'invoice', 'receipt', 'admin', 'report' ],
+  'labels'  => { ''         => '(any)',
+                 'invoice'  => 'Invoices',
+                 'receipt'  => 'Receipts',
+                 'admin'    => 'Admin notices',
+                 'report'   => 'Reports',
+               },
+) .
 '</TD>
 <TD><INPUT type="submit" value="Search"></TD></TR>
 </TABLE></FORM><BR>