[freeside-commits] freeside/httemplate/search cust_main.cgi, 1.71, 1.72 cust_bill.html, 1.18, 1.19 cdr.html, 1.4, 1.5 cust_bill_event.cgi, 1.11, 1.12 cust_bill_event.html, 1.10, 1.11 cust_bill_pkg.cgi, 1.6, 1.7 cust_credit.html, 1.11, 1.12 cust_main.html, 1.5, 1.6 cust_main-otaker.cgi, 1.6, 1.7 cust_main-zip.html, 1.2, 1.3 cust_pay_batch.cgi, 1.4, 1.5 cust_pay.cgi, 1.29, 1.30 cust_pkg.cgi, 1.40, 1.41 cust_svc.html, 1.3, 1.4 cust_tax_exempt_pkg.cgi, 1.4, 1.5 inventory_item.html, 1.4, 1.5 pay_batch.cgi, 1.2, 1.3 pay_batch.html, 1.1, 1.2 queue.html, 1.4, 1.5 reg_code.html, 1.3, 1.4 report_cdr.html, 1.4, 1.5 report_cust_bill.html, 1.4, 1.5 report_cust_credit.html, 1.9, 1.10 report_cust_main-zip.html, 1.2, 1.3 report_cust_pay_batch.html, 1.1, 1.2 report_cust_pay.html, 1.14, 1.15 report_cust_pkg.html, 1.3, 1.4 report_prepaid_income.cgi, 1.7, 1.8 report_prepaid_income.html, 1.5, 1.6 report_receivables.cgi, 1.33, 1.34 report_receivables.html, 1.2, 1.3 report_tax.cgi, 1.33, 1.34 report_tax.html, 1.11, 1.12 sql.html, 1.4, 1.5 sqlradius.cgi, 1.16, 1.17 sqlradius.html, 1.7, 1.8 svc_acct.cgi, 1.39, 1.40 svc_broadband.cgi, 1.3, 1.4 svc_domain.cgi, 1.22, 1.23 svc_external.cgi, 1.5, 1.6 svc_forward.cgi, 1.11, 1.12 svc_phone.cgi, 1.3, 1.4 svc_www.cgi, 1.8, 1.9

Ivan,,, ivan at wavetail.420.am
Sat Feb 3 03:36:32 PST 2007


Update of /home/cvs/cvsroot/freeside/httemplate/search
In directory wavetail:/tmp/cvs-serv7660/httemplate/search

Modified Files:
	cust_main.cgi cust_bill.html cdr.html cust_bill_event.cgi 
	cust_bill_event.html cust_bill_pkg.cgi cust_credit.html 
	cust_main.html cust_main-otaker.cgi cust_main-zip.html 
	cust_pay_batch.cgi cust_pay.cgi cust_pkg.cgi cust_svc.html 
	cust_tax_exempt_pkg.cgi inventory_item.html pay_batch.cgi 
	pay_batch.html queue.html reg_code.html report_cdr.html 
	report_cust_bill.html report_cust_credit.html 
	report_cust_main-zip.html report_cust_pay_batch.html 
	report_cust_pay.html report_cust_pkg.html 
	report_prepaid_income.cgi report_prepaid_income.html 
	report_receivables.cgi report_receivables.html report_tax.cgi 
	report_tax.html sql.html sqlradius.cgi sqlradius.html 
	svc_acct.cgi svc_broadband.cgi svc_domain.cgi svc_external.cgi 
	svc_forward.cgi svc_phone.cgi svc_www.cgi 
Log Message:
add customer status column to customer & most other reports.  also put the C in ACL in the search/ and graph/ directories.

Index: queue.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/queue.html,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- queue.html	10 Jan 2007 23:21:14 -0000	1.4
+++ queue.html	3 Feb 2007 11:36:30 -0000	1.5
@@ -1,17 +1,3 @@
-%
-%
-%my $hashref = {};
-%
-%my $conf = new FS::Conf;
-%my $dangerous = $conf->exists('queue_dangerous_controls');
-%
-%my $noactions = 0;
-%
-%my $count_query = 'SELECT COUNT(*) FROM queue'; # + $hashref
-%
-%my $areboxes = 0;
-%
-%
 <% include( 'elements/search.html',
                  'title'       => 'Job Queue',
                  'menubar'     => [ 'Main menu' => $p, ],
@@ -134,3 +120,20 @@
              )
 
 %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Job queue');
+
+my $hashref = {};
+
+my $conf = new FS::Conf;
+my $dangerous = $conf->exists('queue_dangerous_controls');
+
+my $noactions = 0;
+
+my $count_query = 'SELECT COUNT(*) FROM queue'; # + $hashref
+
+my $areboxes = 0;
+
+</%init>

Index: cust_main.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/cust_main.cgi,v
retrieving revision 1.71
retrieving revision 1.72
diff -u -d -r1.71 -r1.72
--- cust_main.cgi	29 Dec 2006 08:51:33 -0000	1.71
+++ cust_main.cgi	3 Feb 2007 11:36:30 -0000	1.72
@@ -1,3 +1,6 @@
+%die "access denied"
+%  unless $FS::CurrentUser::CurrentUser->access_right('List customers');
+%
 %my $conf = new FS::Conf;
 %my $maxrecords = $conf->config('maxsearchrecordsperpage');
 %
@@ -82,6 +85,8 @@
 %  }
 %
 %  if ( $cgi->param('otaker_on') ) {
+%    die "access denied"
+%      unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
 %    $cgi->param('otaker') =~ /^(\w{1,32})$/ or eidiot "Illegal otaker\n";
 %    $search{otaker} = $1;
 %  } elsif ( $cgi->param('agentnum_on') ) {
@@ -305,7 +310,8 @@
 %
 %  print "<BR><BR>". $pager. include('/elements/table-grid.html'). <<END;
 %      <TR>
-%        <TH CLASS="grid" BGCOLOR="#cccccc"></TH>
+%        <TH CLASS="grid" BGCOLOR="#cccccc">#</TH>
+%        <TH CLASS="grid" BGCOLOR="#cccccc">Status</TH>
 %        <TH CLASS="grid" BGCOLOR="#cccccc">(bill) name</TH>
 %        <TH CLASS="grid" BGCOLOR="#cccccc">company</TH>
 %END
@@ -369,10 +375,12 @@
 %      ? qq!<A HREF="$view"><FONT SIZE=-1>$company</FONT></A>!
 %      : '<FONT SIZE=-1>&nbsp;</FONT>';
 %    
-
+%    my $status = $cust_main->status;
+%    my $statuscol = $FS::cust_main::statuscolor{$status};
 
     <TR>
-      <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><A HREF="<% $view %>"><FONT SIZE=-1><% $custnum %></FONT></A></TD>
+      <TD CLASS="grid" ALIGN="right" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><A HREF="<% $view %>"><FONT SIZE=-1><% $custnum %></FONT></A></TD>
+      <TD CLASS="grid" ALIGN="center" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><FONT SIZE=-1 COLOR=<% $statuscol %>><B><% ucfirst($status) %></B></FONT></TD>
       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><A HREF="<% $view %>"><FONT SIZE=-1><% "$last, $first" %></FONT></A></TD>
       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><% $pcompany %></TD>
 %

Index: cust_main-zip.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/cust_main-zip.html,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- cust_main-zip.html	23 Aug 2006 22:25:38 -0000	1.2
+++ cust_main-zip.html	3 Feb 2007 11:36:30 -0000	1.3
@@ -1,89 +1,3 @@
-%
-%
-%# XXX link to customers
-%
-%my @where = ();
-%
-%# select status
-%
-%if ( $cgi->param('status') =~ /^(prospect|uncancel|active|susp|cancel)$/ ) {
-%  my $method = $1.'_sql';
-%  push @where, FS::cust_main->$method();
-%}
-%
-%# select agent
-%# XXX this needs to be virtualized by agent too (like lots of stuff)
-%
-%my $agentnum = '';
-%if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
-%  $agentnum = $1;
-%  push @where, "cust_main.agentnum = $agentnum";
-%}
-%my $where = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
-%
-%# bill zip vs ship zip
-%
-%sub fieldorempty {
-%  my $field = shift;
-%  "CASE WHEN $field IS NULL THEN '' ELSE $field END";
-%}
-%
-%sub strip_plus4 {
-%  my $field = shift;
-%  "CASE WHEN $field is NULL
-%    THEN ''
-%    ELSE CASE WHEN $field LIKE '_____-____'
-%           THEN SUBSTRING($field FROM 1 FOR 5)
-%           ELSE $field
-%         END
-%  END";
-%}
-%
-%my( $zip, $czip);
-%if ( $cgi->param('column') eq 'ship_zip' ) {
-%
-%  my $casewhen_noship =
-%    "CASE WHEN ( ship_last IS NULL OR ship_last = '' ) THEN ";
-%
-%  $czip = "$casewhen_noship zip ELSE ship_zip END";
-%
-%  if ( $cgi->param('ignore_plus4') ) {
-%    $zip = $casewhen_noship. strip_plus4('zip').
-%                   " ELSE ". strip_plus4('ship_zip'). ' END';
-%
-%  } else {
-%    $zip = $casewhen_noship. fieldorempty('zip').
-%                   " ELSE ". fieldorempty('ship_zip'). ' END';
-%  }
-%
-%} else {
-%
-%  $czip = 'zip';
-%
-%  if ( $cgi->param('ignore_plus4') ) {
-%    $zip = strip_plus4('zip');
-%  } else {
-%    $zip = fieldorempty('zip');
-%  }
-%
-%}
-%
-%# construct the queries and send 'em off
-%
-%my $sql_query = 
-%  "SELECT $zip AS zipcode,
-%          COUNT(*) AS num_cust
-%     FROM cust_main
-%     $where
-%     GROUP BY zipcode
-%     ORDER BY num_cust DESC
-%  ";
-%
-%my $count_sql = "select count(distinct $czip) from cust_main $where";
-%
-%# XXX should link...
-%
-%
 <% include( 'elements/search.html',
                  'title'       => 'Zip code Search Results',
                  'name'        => 'zip codes',
@@ -94,3 +8,92 @@
                  'links'       => [ '', sub { 'somewhere'; }  ],
              )
 %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List zip codes');
+
+# XXX link to customers
+
+my @where = ();
+
+# select status
+
+if ( $cgi->param('status') =~ /^(prospect|uncancel|active|susp|cancel)$/ ) {
+  my $method = $1.'_sql';
+  push @where, FS::cust_main->$method();
+}
+
+# select agent
+# XXX this needs to be virtualized by agent too (like lots of stuff)
+
+my $agentnum = '';
+if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
+  $agentnum = $1;
+  push @where, "cust_main.agentnum = $agentnum";
+}
+my $where = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
+
+# bill zip vs ship zip
+
+sub fieldorempty {
+  my $field = shift;
+  "CASE WHEN $field IS NULL THEN '' ELSE $field END";
+}
+
+sub strip_plus4 {
+  my $field = shift;
+  "CASE WHEN $field is NULL
+    THEN ''
+    ELSE CASE WHEN $field LIKE '_____-____'
+           THEN SUBSTRING($field FROM 1 FOR 5)
+           ELSE $field
+         END
+  END";
+}
+
+my( $zip, $czip);
+if ( $cgi->param('column') eq 'ship_zip' ) {
+
+  my $casewhen_noship =
+    "CASE WHEN ( ship_last IS NULL OR ship_last = '' ) THEN ";
+
+  $czip = "$casewhen_noship zip ELSE ship_zip END";
+
+  if ( $cgi->param('ignore_plus4') ) {
+    $zip = $casewhen_noship. strip_plus4('zip').
+                   " ELSE ". strip_plus4('ship_zip'). ' END';
+
+  } else {
+    $zip = $casewhen_noship. fieldorempty('zip').
+                   " ELSE ". fieldorempty('ship_zip'). ' END';
+  }
+
+} else {
+
+  $czip = 'zip';
+
+  if ( $cgi->param('ignore_plus4') ) {
+    $zip = strip_plus4('zip');
+  } else {
+    $zip = fieldorempty('zip');
+  }
+
+}
+
+# construct the queries and send 'em off
+
+my $sql_query = 
+  "SELECT $zip AS zipcode,
+          COUNT(*) AS num_cust
+     FROM cust_main
+     $where
+     GROUP BY zipcode
+     ORDER BY num_cust DESC
+  ";
+
+my $count_sql = "select count(distinct $czip) from cust_main $where";
+
+# XXX should link...
+
+</%init>

Index: svc_www.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/svc_www.cgi,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- svc_www.cgi	29 Dec 2006 08:51:33 -0000	1.8
+++ svc_www.cgi	3 Feb 2007 11:36:30 -0000	1.9
@@ -1,60 +1,3 @@
-%#my $conf = new FS::Conf;
-%
-%my $orderby = 'ORDER BY svcnum';
-%my @extra_sql = ();
-%if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
-%
-%  push @extra_sql, 'pkgnum IS NULL'
-%    if $cgi->param('magic') eq 'unlinked';
-%
-%  if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
-%    my $sortby = $1;
-%    $orderby = "ORDER BY $sortby";
-%  }
-%
-%} elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
-%  push @extra_sql, "svcpart = $1";
-%}
-%
-%my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
-%                ' LEFT JOIN part_svc  USING ( svcpart ) '.
-%                ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
-%                ' LEFT JOIN cust_main USING ( custnum ) ';
-%
-%#here is the agent virtualization
-%push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql;
-%
-%my $extra_sql = 
-%  scalar(@extra_sql)
-%    ? ' WHERE '. join(' AND ', @extra_sql )
-%    : '';
-%
-%
-%my $count_query = 'SELECT COUNT(*) FROM svc_www';
-%my $sql_query = {
-%  'table'     => 'svc_www',
-%  'hashref'   => {},
-%  'select'    => join(', ',
-%                   'svc_www.*',
-%                   'part_svc.svc',
-%                   'cust_main.custnum',
-%                   FS::UI::Web::cust_sql_fields(),
-%                 ),
-%  'extra_sql' => $orderby,
-%  'addl_from' => $addl_from,
-%};
-%
-%my $link  = [ "${p}view/svc_www.cgi?", 'svcnum', ];
-%#my $dlink = [ "${p}view/svc_www.cgi?", 'svcnum', ];
-%my $ulink = [ "${p}view/svc_acct.cgi?", 'usersvc', ];
-%
-%#smaller false laziness w/svc_*.cgi here
-%my $link_cust = sub {
-%  my $svc_x = shift;
-%  $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
-%};
-%
-%
 <% include( 'elements/search.html',
                  'title'       => 'Virtual Host Search Results',
                  'name'        => 'virtual hosts',
@@ -83,9 +26,86 @@
                                     $link,
                                     '',
                                     $ulink,
-                                    ( map { $link_cust }
+                                    ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
                                           FS::UI::Web::cust_header()
                                     ),
                                   ],
+                 'align' => 'rlll'. FS::UI::Web::cust_aligns(),
+                 'color' => [ 
+                              '',
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_colors(),
+                            ],
+                 'style' => [ 
+                              '',
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_styles(),
+                            ],
              )
 %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List services');
+
+#my $conf = new FS::Conf;
+
+my $orderby = 'ORDER BY svcnum';
+my @extra_sql = ();
+if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
+
+  push @extra_sql, 'pkgnum IS NULL'
+    if $cgi->param('magic') eq 'unlinked';
+
+  if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
+    my $sortby = $1;
+    $orderby = "ORDER BY $sortby";
+  }
+
+} elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
+  push @extra_sql, "svcpart = $1";
+}
+
+my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
+                ' LEFT JOIN part_svc  USING ( svcpart ) '.
+                ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
+                ' LEFT JOIN cust_main USING ( custnum ) ';
+
+#here is the agent virtualization
+push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql;
+
+my $extra_sql = 
+  scalar(@extra_sql)
+    ? ' WHERE '. join(' AND ', @extra_sql )
+    : '';
+
+
+my $count_query = 'SELECT COUNT(*) FROM svc_www';
+my $sql_query = {
+  'table'     => 'svc_www',
+  'hashref'   => {},
+  'select'    => join(', ',
+                   'svc_www.*',
+                   'part_svc.svc',
+                   'cust_main.custnum',
+                   FS::UI::Web::cust_sql_fields(),
+                 ),
+  'extra_sql' => $orderby,
+  'addl_from' => $addl_from,
+};
+
+my $link  = [ "${p}view/svc_www.cgi?", 'svcnum', ];
+#my $dlink = [ "${p}view/svc_www.cgi?", 'svcnum', ];
+my $ulink = [ "${p}view/svc_acct.cgi?", 'usersvc', ];
+
+#smaller false laziness w/svc_*.cgi here
+my $link_cust = sub {
+  my $svc_x = shift;
+  $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
+};
+
+</%init>

Index: cdr.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/cdr.html,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- cdr.html	23 Aug 2006 22:25:38 -0000	1.4
+++ cdr.html	3 Feb 2007 11:36:30 -0000	1.5
@@ -1,29 +1,3 @@
-%
-%
-%my $title = 'Call Detail Records';
-%my $hashref = {};
-%my $count_query = 'SELECT COUNT(*) FROM cdr';
-%
-%#process params for CDR search, populate $hashref...
-%# and fixup $count_query
-%
-%if ( $cgi->param('freesidestatus') eq 'NULL' ) {
-%
-%  my $title = "Unprocessed $title";
-%  $hashref->{'freesidestatus'} = ''; # Record.pm will take care of it
-%  #$count_query .= " AND ( freesidestatus IS NULL OR freesidestatus = '' )";
-%  $count_query .= " WHERE ( freesidestatus IS NULL OR freesidestatus = '' )";
-%
-%} elsif ( $cgi->param('freesidestatus') =~ /^([\w ]+)$/ ) {
-%
-%  my $title = "Processed $title";
-%  $hashref->{'freesidestatus'} = $1;
-%  #$count_query .= " AND freesidestatus = '$1'";
-%  $count_query .= " WHERE freesidestatus = '$1'";
-%
-%}
-%
-%
 <% include( 'elements/search.html',
                'title' => $title,
                'name'  => 'call detail records',
@@ -36,3 +10,32 @@
                                               # processing, etc.
              )
 %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
+
+my $title = 'Call Detail Records';
+my $hashref = {};
+my $count_query = 'SELECT COUNT(*) FROM cdr';
+
+#process params for CDR search, populate $hashref...
+# and fixup $count_query
+
+if ( $cgi->param('freesidestatus') eq 'NULL' ) {
+
+  my $title = "Unprocessed $title";
+  $hashref->{'freesidestatus'} = ''; # Record.pm will take care of it
+  #$count_query .= " AND ( freesidestatus IS NULL OR freesidestatus = '' )";
+  $count_query .= " WHERE ( freesidestatus IS NULL OR freesidestatus = '' )";
+
+} elsif ( $cgi->param('freesidestatus') =~ /^([\w ]+)$/ ) {
+
+  my $title = "Processed $title";
+  $hashref->{'freesidestatus'} = $1;
+  #$count_query .= " AND freesidestatus = '$1'";
+  $count_query .= " WHERE freesidestatus = '$1'";
+
+}
+
+</%init>

Index: sql.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/sql.html,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- sql.html	23 Aug 2006 22:25:38 -0000	1.4
+++ sql.html	3 Feb 2007 11:36:30 -0000	1.5
@@ -5,3 +5,9 @@
                                        || eidiot('Empty query') ),
     )
 %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Raw SQL');
+
+</%init>

Index: report_receivables.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/report_receivables.cgi,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -d -r1.33 -r1.34
--- report_receivables.cgi	14 Oct 2006 10:09:24 -0000	1.33
+++ report_receivables.cgi	3 Feb 2007 11:36:30 -0000	1.34
@@ -1,139 +1,3 @@
-%
-%
-%  sub owed {
-%    my($start, $end, %opt) = @_;
-%
-%    my @where = ();
-%
-%    #handle start and end ranges
-%
-%    #24h * 60m * 60s
-%    push @where, "cust_bill._date <= extract(epoch from now())-".
-%                 ($start * 86400)
-%      if $start;
-%  
-%    push @where, "cust_bill._date > extract(epoch from now()) - ".
-%                 ($end * 86400)
-%      if $end;
-%
-%    #handle 'cust' option
-%  
-%    push @where, "cust_main.custnum = cust_bill.custnum"
-%      if $opt{'cust'};
-%
-%    #handle 'agentnum' option
-%    my $join = '';
-%    if ( $opt{'agentnum'} ) {
-%      $join = 'LEFT JOIN cust_main USING ( custnum )';
-%      push @where, "agentnum = '$opt{'agentnum'}'";
-%    }
-%
-%    my $where = scalar(@where) ? 'WHERE '.join(' AND ', @where) : '';
-%  
-%    my $as = $opt{'noas'} ? '' : "as owed_${start}_$end";
-%
-%    my $charged = <<END;
-%  sum( charged
-%       - coalesce(
-%           ( select sum(amount) from cust_bill_pay
-%             where cust_bill.invnum = cust_bill_pay.invnum )
-%           ,0
-%         )
-%       - coalesce(
-%           ( select sum(amount) from cust_credit_bill
-%             where cust_bill.invnum = cust_credit_bill.invnum )
-%           ,0
-%         )
-%
-%     )
-%END
-%
-%    "coalesce( ( select $charged from cust_bill $join $where ) ,0 ) $as";
-%  
-%  }
-%
-%  my @ranges = (
-%    [  0, 30 ],
-%    [ 30, 60 ],
-%    [ 60, 90 ],
-%    [ 90,  0 ],
-%    [  0,  0 ],
-%  );
-%
-%  my $owed_cols = join(',', map owed( @$_, 'cust'=>1 ), @ranges );
-%
-%  my $select_count_pkgs = FS::cust_main->select_count_pkgs_sql;
-%
-%  my $active_sql    = FS::cust_pkg->active_sql;
-%  my $inactive_sql  = FS::cust_pkg->inactive_sql;
-%  my $suspended_sql = FS::cust_pkg->suspended_sql;
-%  my $cancelled_sql = FS::cust_pkg->cancelled_sql;
-%
-%  my $packages_cols = <<END;
-%       ( $select_count_pkgs                    ) AS num_pkgs_sql,
-%       ( $select_count_pkgs AND $active_sql    ) AS active_pkgs,
-%       ( $select_count_pkgs AND $inactive_sql  ) AS inactive_pkgs,
-%       ( $select_count_pkgs AND $suspended_sql ) AS suspended_pkgs,
-%       ( $select_count_pkgs AND $cancelled_sql ) AS cancelled_pkgs
-%END
-%
-%  my $where = "where ". owed(0, 0, 'cust'=>1, 'noas'=>1). " > 0";
-%
-%  my $agentnum = '';
-%  if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
-%    $agentnum = $1;
-%    $where .= " AND agentnum = '$agentnum' ";
-%  }
-%
-%  #here is the agent virtualization
-%  $where .= ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
-%
-%  my $count_sql = "select count(*) from cust_main $where";
-%
-%  my $sql_query = {
-%    'table'     => 'cust_main',
-%    'hashref'   => {},
-%    'select'    => "*, $owed_cols, $packages_cols",
-%    'extra_sql' => "$where order by coalesce(lower(company), ''), lower(last)",
-%  };
-%
-%  my $total_sql = "select ".
-%                    join(',', map owed( @$_, 'agentnum'=>$agentnum ), @ranges );
-%
-%  my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
-%  $total_sth->execute or die "error executing $total_sql: ". $total_sth->errstr;
-%  my $row = $total_sth->fetchrow_hashref();
-%
-%  my $conf = new FS::Conf;
-%  my $money_char = $conf->config('money_char') || '$';
-%
-%  my $align = join('', map { /#/ ? 'r' : 'l' } FS::UI::Web::cust_header() ).
-%             'crrrrr';
-%
-%  my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
-%
-%  my $status_statuscol = sub {
-%    #conceptual false laziness with cust_main::status...
-%    my $row = shift;
-%
-%    my $status = 'unknown';
-%    if ( $row->num_pkgs_sql == 0 ) {
-%      $status = 'prospect';
-%    } elsif ( $row->active_pkgs    > 0 ) {
-%      $status = 'active';
-%    } elsif ( $row->inactive_pkgs  > 0 ) {
-%      $status = 'inactive';
-%    } elsif ( $row->suspended_pkgs > 0 ) {
-%      $status = 'suspended';
-%    } elsif ( $row->cancelled_pkgs > 0 ) {
-%      $status = 'cancelled'
-%    }
-%
-%    ( ucfirst($status), $FS::cust_main::statuscolor{$status} );
-%  };
-%
-%
-%
 <% include( 'elements/search.html',
                  'title'       => 'Accounts Receivable Aging Summary',
                  'name'        => 'customers',
@@ -141,7 +5,7 @@
                  'count_query' => $count_sql,
                  'header'      => [
                                     FS::UI::Web::cust_header(),
-                                    'Status', # (me)',
+                                    #'Status', # (me)',
                                     #'Status', # (cust_main)',
                                     '0-30',
                                     '30-60',
@@ -156,7 +20,7 @@
                                             scalar(FS::UI::Web::cust_header()-1)
                                           )
                                     ),
-                                    '',
+                                    #'',
                                     #'',
                                     sprintf( $money_char.'%.2f',
                                              $row->{'owed_0_30'} ),
@@ -171,7 +35,7 @@
                                   ],
                  'fields'      => [
                                     \&FS::UI::Web::cust_fields,
-                                    sub { ( &{$status_statuscol}(shift) )[0] },
+                                    #sub { ( &{$status_statuscol}(shift) )[0] },
                                     #sub { ucfirst(shift->status) },
                                     sub { sprintf( $money_char.'%.2f',
                                                    shift->get('owed_0_30') ) },
@@ -185,8 +49,10 @@
                                                    shift->get('owed_0_0') ) },
                                   ],
                  'links'       => [
-                                    ( map $clink, FS::UI::Web::cust_header() ),
-                                    '',
+                                    ( map { $_ ne 'Cust. Status' ? $clink : '' }
+                                          FS::UI::Web::cust_header()
+                                    ),
+                                    #'',
                                     #'',
                                     '',
                                     '',
@@ -199,12 +65,14 @@
                  #'size'        => [ '', '', '-1', '-1', '', '', '', '',  '', ],
                  #'style'       => [ '', '',  'b',  'b', '', '', '', '', 'b', ],
                  'size'        => [ ( map '', FS::UI::Web::cust_header() ),
-                                    '-1', '', '', '', '',  '', ],
-                 'style'       => [ ( map '', FS::UI::Web::cust_header() ),
-                                    'b', '', '', '', '', 'b', ],
+                                    #'-1', '', '', '', '',  '', ],
+                                    '', '', '', '',  '', ],
+                 'style'       => [ FS::UI::Web::cust_styles(),
+                                    #'b', '', '', '', '', 'b', ],
+                                    '', '', '', '', 'b', ],
                  'color'       => [
-                                    ( map '', FS::UI::Web::cust_header() ),
-                                    sub { ( &{$status_statuscol}(shift) )[1] },
+                                    FS::UI::Web::cust_colors(),
+                                    #sub { ( &{$status_statuscol}(shift) )[1] },
                                     #sub { shift->statuscolor; },
                                     '',
                                     '',
@@ -215,3 +83,143 @@
 
              )
 %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
+
+sub owed {
+  my($start, $end, %opt) = @_;
+
+  my @where = ();
+
+  #handle start and end ranges
+
+  #24h * 60m * 60s
+  push @where, "cust_bill._date <= extract(epoch from now())-".
+               ($start * 86400)
+    if $start;
+
+  push @where, "cust_bill._date > extract(epoch from now()) - ".
+               ($end * 86400)
+    if $end;
+
+  #handle 'cust' option
+
+  push @where, "cust_main.custnum = cust_bill.custnum"
+    if $opt{'cust'};
+
+  #handle 'agentnum' option
+  my $join = '';
+  if ( $opt{'agentnum'} ) {
+    $join = 'LEFT JOIN cust_main USING ( custnum )';
+    push @where, "agentnum = '$opt{'agentnum'}'";
+  }
+
+  my $where = scalar(@where) ? 'WHERE '.join(' AND ', @where) : '';
+
+  my $as = $opt{'noas'} ? '' : "as owed_${start}_$end";
+
+  my $charged = <<END;
+sum( charged
+     - coalesce(
+         ( select sum(amount) from cust_bill_pay
+           where cust_bill.invnum = cust_bill_pay.invnum )
+         ,0
+       )
+     - coalesce(
+         ( select sum(amount) from cust_credit_bill
+           where cust_bill.invnum = cust_credit_bill.invnum )
+         ,0
+       )
+
+   )
+END
+
+  "coalesce( ( select $charged from cust_bill $join $where ) ,0 ) $as";
+
+}
+
+my @ranges = (
+  [  0, 30 ],
+  [ 30, 60 ],
+  [ 60, 90 ],
+  [ 90,  0 ],
+  [  0,  0 ],
+);
+
+my $owed_cols = join(',', map owed( @$_, 'cust'=>1 ), @ranges );
+
+my $select_count_pkgs = FS::cust_main->select_count_pkgs_sql;
+
+my $active_sql    = FS::cust_pkg->active_sql;
+my $inactive_sql  = FS::cust_pkg->inactive_sql;
+my $suspended_sql = FS::cust_pkg->suspended_sql;
+my $cancelled_sql = FS::cust_pkg->cancelled_sql;
+
+my $packages_cols = <<END;
+     ( $select_count_pkgs                    ) AS num_pkgs_sql,
+     ( $select_count_pkgs AND $active_sql    ) AS active_pkgs,
+     ( $select_count_pkgs AND $inactive_sql  ) AS inactive_pkgs,
+     ( $select_count_pkgs AND $suspended_sql ) AS suspended_pkgs,
+     ( $select_count_pkgs AND $cancelled_sql ) AS cancelled_pkgs
+END
+
+my $where = "where ". owed(0, 0, 'cust'=>1, 'noas'=>1). " > 0";
+
+my $agentnum = '';
+if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
+  $agentnum = $1;
+  $where .= " AND agentnum = '$agentnum' ";
+}
+
+#here is the agent virtualization
+$where .= ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
+
+my $count_sql = "select count(*) from cust_main $where";
+
+my $sql_query = {
+  'table'     => 'cust_main',
+  'hashref'   => {},
+  'select'    => "*, $owed_cols, $packages_cols",
+  'extra_sql' => "$where order by coalesce(lower(company), ''), lower(last)",
+};
+
+my $total_sql = "select ".
+                  join(',', map owed( @$_, 'agentnum'=>$agentnum ), @ranges );
+
+my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
+$total_sth->execute or die "error executing $total_sql: ". $total_sth->errstr;
+my $row = $total_sth->fetchrow_hashref();
+
+my $conf = new FS::Conf;
+my $money_char = $conf->config('money_char') || '$';
+
+#my $align = join('', map { /#/ ? 'r' : 'l' } FS::UI::Web::cust_header() ).
+#           'crrrrr';
+my $align = FS::UI::Web::cust_aligns().
+           'rrrrr';
+
+my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
+
+my $status_statuscol = sub {
+  #conceptual false laziness with cust_main::status...
+  my $row = shift;
+
+  my $status = 'unknown';
+  if ( $row->num_pkgs_sql == 0 ) {
+    $status = 'prospect';
+  } elsif ( $row->active_pkgs    > 0 ) {
+    $status = 'active';
+  } elsif ( $row->inactive_pkgs  > 0 ) {
+    $status = 'inactive';
+  } elsif ( $row->suspended_pkgs > 0 ) {
+    $status = 'suspended';
+  } elsif ( $row->cancelled_pkgs > 0 ) {
+    $status = 'cancelled'
+  }
+
+  ( ucfirst($status), $FS::cust_main::statuscolor{$status} );
+};
+
+</%init>

Index: cust_main-otaker.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/cust_main-otaker.cgi,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- cust_main-otaker.cgi	23 Aug 2006 22:25:38 -0000	1.6
+++ cust_main-otaker.cgi	3 Feb 2007 11:36:30 -0000	1.7
@@ -23,3 +23,9 @@
 </FORM>
 
 <% include('/elements/footer.html') %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
+
+</%init>

Index: report_tax.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/report_tax.html,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- report_tax.html	23 Aug 2006 22:25:38 -0000	1.11
+++ report_tax.html	3 Feb 2007 11:36:30 -0000	1.12
@@ -34,3 +34,9 @@
 </FORM>
 
 <% include('/elements/footer.html') %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
+
+</%init>

Index: cust_tax_exempt_pkg.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/cust_tax_exempt_pkg.cgi,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- cust_tax_exempt_pkg.cgi	15 Sep 2006 19:15:34 -0000	1.4
+++ cust_tax_exempt_pkg.cgi	3 Feb 2007 11:36:30 -0000	1.5
@@ -54,9 +54,29 @@
                    $ilink,
                    $ilink,
 
-                   ( map { $clink } FS::UI::Web::cust_header() ),
+                   ( map { $_ ne 'Cust. Status' ? $clink : '' }
+                         FS::UI::Web::cust_header()
+                   ),
                  ],
-                 'align' => 'rrrlrc', # 'rlrrrc',
+                 'align' => 'rrrlrc'.FS::UI::Web::cust_aligns(), # 'rlrrrc',
+                 'color' => [ 
+                              '',
+                              '',
+                              '',
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_colors(),
+                            ],
+                 'style' => [ 
+                              '',
+                              '',
+                              '',
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_styles(),
+                            ],
            )
 %>
 <%once>
@@ -80,6 +100,9 @@
 </%once>
 <%init>
 
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('View customer tax exemptions');
+
 my @where = ();
 
 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);

Index: report_cust_main-zip.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/report_cust_main-zip.html,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- report_cust_main-zip.html	23 Aug 2006 22:25:38 -0000	1.2
+++ report_cust_main-zip.html	3 Feb 2007 11:36:30 -0000	1.3
@@ -42,5 +42,11 @@
     </TABLE>
     <BR><INPUT TYPE="submit" VALUE="Get Report">
     </FORM>
-  </BODY>
-</HTML>
+
+<% include('/elements/footer.html') %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List zip codes');
+
+</%init>

Index: report_prepaid_income.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/report_prepaid_income.cgi,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- report_prepaid_income.cgi	23 Aug 2006 22:25:38 -0000	1.7
+++ report_prepaid_income.cgi	3 Feb 2007 11:36:30 -0000	1.8
@@ -1,65 +1,3 @@
-<!-- mason kludge -->
-%
-%
-%  #doesn't yet deal with daily/weekly packages
-%
-%  #needs to be re-written in sql for efficiency
-%
-%  my $time = time;
-%
-%  my $now = $cgi->param('date') && str2time($cgi->param('date')) || $time;
-%  $now =~ /^(\d+)$/ or die "unparsable date?";
-%  $now = $1;
-%
-%  my( $total, $total_legacy ) = ( 0, 0 );
-%
-%  my @cust_bill_pkg =
-%    grep { $_->cust_pkg && $_->cust_pkg->part_pkg->freq !~ /^([01]|\d+[dw])$/ }
-%      qsearch( 'cust_bill_pkg', {
-%                                  'recur' => { op=>'!=', value=>0 },
-%                                  'edate' => { op=>'>', value=>$now },
-%                                }, );
-%
-%  my @cust_pkg = 
-%    grep { $_->part_pkg->recur != 0
-%           && $_->part_pkg->freq !~ /^([01]|\d+[dw])$/
-%         }
-%      qsearch ( 'cust_pkg', {
-%                              'bill' => { op=>'>', value=>$now }
-%                            } );
-%
-%  foreach my $cust_bill_pkg ( @cust_bill_pkg) { 
-%    my $period = $cust_bill_pkg->edate - $cust_bill_pkg->sdate;
-%
-%    my $elapsed = $now - $cust_bill_pkg->sdate;
-%    $elapsed = 0 if $elapsed < 0;
-%
-%    my $remaining = 1 - $elapsed/$period;
-%
-%    my $unearned = $remaining * $cust_bill_pkg->recur;
-%    $total += $unearned;
-%
-%  }
-%
-%  foreach my $cust_pkg ( @cust_pkg ) {
-%    my $period = $cust_pkg->bill - $cust_pkg->last_bill;
-%
-%    my $elapsed = $now - $cust_pkg->last_bill;
-%    $elapsed = 0 if $elapsed < 0;
-%
-%    my $remaining = 1 - $elapsed/$period;
-%
-%    my $unearned = $remaining * $cust_pkg->part_pkg->recur; #!! only works for flat/legacy
-%    $total_legacy += $unearned;
-%
-%  }
-%
-%  $total = sprintf('%.2f', $total);
-%  $total_legacy = sprintf('%.2f', $total_legacy);
-%
-%
-
-
 <% include("/elements/header.html", 'Prepaid Income (Unearned Revenue) Report',
             menubar( 'Main Menu'=>$p, ) )               %>
 <% table() %>
@@ -85,3 +23,65 @@
 a previous billing system.
 </BODY>
 </HTML>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
+
+#doesn't yet deal with daily/weekly packages
+
+#needs to be re-written in sql for efficiency
+
+my $time = time;
+
+my $now = $cgi->param('date') && str2time($cgi->param('date')) || $time;
+$now =~ /^(\d+)$/ or die "unparsable date?";
+$now = $1;
+
+my( $total, $total_legacy ) = ( 0, 0 );
+
+my @cust_bill_pkg =
+  grep { $_->cust_pkg && $_->cust_pkg->part_pkg->freq !~ /^([01]|\d+[dw])$/ }
+    qsearch( 'cust_bill_pkg', {
+                                'recur' => { op=>'!=', value=>0 },
+                                'edate' => { op=>'>', value=>$now },
+                              }, );
+
+my @cust_pkg = 
+  grep { $_->part_pkg->recur != 0
+         && $_->part_pkg->freq !~ /^([01]|\d+[dw])$/
+       }
+    qsearch ( 'cust_pkg', {
+                            'bill' => { op=>'>', value=>$now }
+                          } );
+
+foreach my $cust_bill_pkg ( @cust_bill_pkg) { 
+  my $period = $cust_bill_pkg->edate - $cust_bill_pkg->sdate;
+
+  my $elapsed = $now - $cust_bill_pkg->sdate;
+  $elapsed = 0 if $elapsed < 0;
+
+  my $remaining = 1 - $elapsed/$period;
+
+  my $unearned = $remaining * $cust_bill_pkg->recur;
+  $total += $unearned;
+
+}
+
+foreach my $cust_pkg ( @cust_pkg ) {
+  my $period = $cust_pkg->bill - $cust_pkg->last_bill;
+
+  my $elapsed = $now - $cust_pkg->last_bill;
+  $elapsed = 0 if $elapsed < 0;
+
+  my $remaining = 1 - $elapsed/$period;
+
+  my $unearned = $remaining * $cust_pkg->part_pkg->recur; #!! only works for flat/legacy
+  $total_legacy += $unearned;
+
+}
+
+$total = sprintf('%.2f', $total);
+$total_legacy = sprintf('%.2f', $total_legacy);
+
+</%init>

Index: sqlradius.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/sqlradius.html,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- sqlradius.html	4 Sep 2006 20:05:58 -0000	1.7
+++ sqlradius.html	3 Feb 2007 11:36:30 -0000	1.8
@@ -51,3 +51,9 @@
 </FORM>
 
 <% include('/elements/footer.html') %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
+
+</%init>

Index: svc_domain.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/svc_domain.cgi,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -d -r1.22 -r1.23
--- svc_domain.cgi	29 Dec 2006 08:51:33 -0000	1.22
+++ svc_domain.cgi	3 Feb 2007 11:36:30 -0000	1.23
@@ -1,70 +1,3 @@
-%my $conf = new FS::Conf;
-%
-%my $orderby = 'ORDER BY svcnum';
-%my %svc_domain = ();
-%my @extra_sql = ();
-%if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
-%
-%  push @extra_sql, 'pkgnum IS NULL'
-%    if $cgi->param('magic') eq 'unlinked';
-%
-%  if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
-%    my $sortby = $1;
-%    $orderby = "ORDER BY $sortby";
-%  }
-%
-%} elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
-%  push @extra_sql, "svcpart = $1";
-%} else {
-%  $cgi->param('domain') =~ /^([\w\-\.]+)$/; 
-%  $svc_domain{'domain'} = $1;
-%}
-%
-%my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
-%                ' LEFT JOIN part_svc  USING ( svcpart ) '.
-%                ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
-%                ' LEFT JOIN cust_main USING ( custnum ) ';
-%
-%#here is the agent virtualization
-%push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql;
-%
-%my $extra_sql = '';
-%if ( @extra_sql ) {
-%  $extra_sql = ( keys(%svc_domain) ? ' AND ' : ' WHERE ' ).
-%               join(' AND ', @extra_sql );
-%}
-%
-%my $count_query = "SELECT COUNT(*) FROM svc_domain $addl_from ";
-%if ( keys %svc_domain ) {
-%  $count_query .= ' WHERE '.
-%                    join(' AND ', map "$_ = ". dbh->quote($svc_domain{$_}),
-%                                      keys %svc_domain
-%                        );
-%}
-%$count_query .= $extra_sql;
-%
-%my $sql_query = {
-%  'table'     => 'svc_domain',
-%  'hashref'   => \%svc_domain,
-%  'select'    => join(', ',
-%                   'svc_domain.*',
-%                   'part_svc.svc',
-%                   'cust_main.custnum',
-%                   FS::UI::Web::cust_sql_fields(),
-%                 ),
-%  'extra_sql' => "$extra_sql $orderby",
-%  'addl_from' => $addl_from,
-%};
-%
-%my $link = [ "${p}view/svc_domain.cgi?", 'svcnum' ];
-%
-%#smaller false laziness w/svc_*.cgi here
-%my $link_cust = sub {
-%  my $svc_x = shift;
-%  $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
-%};
-%
-%
 <% include( 'elements/search.html',
                  'title'             => "Domain Search Results",
                  'name'              => 'domains',
@@ -84,9 +17,94 @@
                  'links'             => [ $link,
                                           $link,
                                           $link,
-                                          ( map { $link_cust }
+                                          ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
                                                 FS::UI::Web::cust_header()
                                           ),
                                         ],
+                 'align' => 'rll'. FS::UI::Web::cust_aligns(),
+                 'color' => [ 
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_colors(),
+                            ],
+                 'style' => [ 
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_styles(),
+                            ],
               )
 %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List services');
+
+my $conf = new FS::Conf;
+
+my $orderby = 'ORDER BY svcnum';
+my %svc_domain = ();
+my @extra_sql = ();
+if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
+
+  push @extra_sql, 'pkgnum IS NULL'
+    if $cgi->param('magic') eq 'unlinked';
+
+  if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
+    my $sortby = $1;
+    $orderby = "ORDER BY $sortby";
+  }
+
+} elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
+  push @extra_sql, "svcpart = $1";
+} else {
+  $cgi->param('domain') =~ /^([\w\-\.]+)$/; 
+  $svc_domain{'domain'} = $1;
+}
+
+my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
+                ' LEFT JOIN part_svc  USING ( svcpart ) '.
+                ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
+                ' LEFT JOIN cust_main USING ( custnum ) ';
+
+#here is the agent virtualization
+push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql;
+
+my $extra_sql = '';
+if ( @extra_sql ) {
+  $extra_sql = ( keys(%svc_domain) ? ' AND ' : ' WHERE ' ).
+               join(' AND ', @extra_sql );
+}
+
+my $count_query = "SELECT COUNT(*) FROM svc_domain $addl_from ";
+if ( keys %svc_domain ) {
+  $count_query .= ' WHERE '.
+                    join(' AND ', map "$_ = ". dbh->quote($svc_domain{$_}),
+                                      keys %svc_domain
+                        );
+}
+$count_query .= $extra_sql;
+
+my $sql_query = {
+  'table'     => 'svc_domain',
+  'hashref'   => \%svc_domain,
+  'select'    => join(', ',
+                   'svc_domain.*',
+                   'part_svc.svc',
+                   'cust_main.custnum',
+                   FS::UI::Web::cust_sql_fields(),
+                 ),
+  'extra_sql' => "$extra_sql $orderby",
+  'addl_from' => $addl_from,
+};
+
+my $link = [ "${p}view/svc_domain.cgi?", 'svcnum' ];
+
+#smaller false laziness w/svc_*.cgi here
+my $link_cust = sub {
+  my $svc_x = shift;
+  $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
+};
+
+</%init>

Index: pay_batch.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/pay_batch.cgi,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- pay_batch.cgi	8 Oct 2006 08:17:06 -0000	1.2
+++ pay_batch.cgi	3 Feb 2007 11:36:30 -0000	1.3
@@ -1,44 +1,3 @@
-%
-%
-%my %statusmap = ('I'=>'In Transit', 'O'=>'Open', 'R'=>'Resolved');
-%my $hashref = {};
-%my $count_query = 'SELECT COUNT(*) FROM pay_batch';
-%
-%my($begin, $end) = ( '', '' );
-%
-%my @where;
-%if ( $cgi->param('beginning')
-%     && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
-%  $begin = str2time($1);
-%  push @where, "download >= $begin";
-%}
-%if ( $cgi->param('ending')
-%      && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
-%  $end = str2time($1) + 86399;
-%  push @where, "download < $end";
-%}
-%
-%my @status;
-%if ( $cgi->param('open') ) {
-%  push @status, "O";
-%}
-%
-%if ( $cgi->param('intransit') ) {
-%  push @status, "I";
-%}
-%
-%if ( $cgi->param('resolved') ) {
-%  push @status, "R";
-%}
-%
-%push @where,
-%     scalar(@status) ? q!(status='! . join(q!' OR status='!, @status) . q!')!
-%                     : q!status='X'!;  # kludgy, X is unused at present
-%
-%my $extra_sql = scalar(@where) ? 'WHERE ' . join(' AND ', @where) : ''; 
-%
-%my $link = [ "${p}search/cust_pay_batch.cgi?batchnum=", 'batchnum' ];
-%
 <% include( 'elements/search.html',
                  'title'         => 'Payment Batches',
 		 'name_singular' => 'batch',
@@ -123,5 +82,49 @@
       )
 
 %>
+<%init>
 
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Financial reports')
+      || $FS::CurrentUser::CurrentUser->access_right('Process batches');
+
+my %statusmap = ('I'=>'In Transit', 'O'=>'Open', 'R'=>'Resolved');
+my $hashref = {};
+my $count_query = 'SELECT COUNT(*) FROM pay_batch';
+
+my($begin, $end) = ( '', '' );
+
+my @where;
+if ( $cgi->param('beginning')
+     && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
+  $begin = str2time($1);
+  push @where, "download >= $begin";
+}
+if ( $cgi->param('ending')
+      && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
+  $end = str2time($1) + 86399;
+  push @where, "download < $end";
+}
+
+my @status;
+if ( $cgi->param('open') ) {
+  push @status, "O";
+}
 
+if ( $cgi->param('intransit') ) {
+  push @status, "I";
+}
+
+if ( $cgi->param('resolved') ) {
+  push @status, "R";
+}
+
+push @where,
+     scalar(@status) ? q!(status='! . join(q!' OR status='!, @status) . q!')!
+                     : q!status='X'!;  # kludgy, X is unused at present
+
+my $extra_sql = scalar(@where) ? 'WHERE ' . join(' AND ', @where) : ''; 
+
+my $link = [ "${p}search/cust_pay_batch.cgi?batchnum=", 'batchnum' ];
+
+</%init>

Index: cust_svc.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/cust_svc.html,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- cust_svc.html	21 Jan 2007 09:13:49 -0000	1.3
+++ cust_svc.html	3 Feb 2007 11:36:30 -0000	1.4
@@ -21,14 +21,28 @@
 	      'links'       => [ $link,
 	                         $link,
 				 # package?
-				 ( map { $link_cust }
+				 ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
                                        FS::UI::Web::cust_header()
                                  ),
 			       ],
+              'align' => 'rl'. FS::UI::Web::cust_aligns(),
+              'color' => [ 
+                           '',
+                           '',
+                           FS::UI::Web::cust_colors(),
+                         ],
+              'style' => [ 
+                           '',
+                           '',
+                           FS::UI::Web::cust_styles(),
+                         ],
           )
 %>
 <%init>
 
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List services');
+
 my $addl_from = ' LEFT JOIN part_svc  USING ( svcpart ) '.
                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
                 ' LEFT JOIN cust_main USING ( custnum ) ';

Index: cust_bill_event.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/cust_bill_event.html,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- cust_bill_event.html	2 Sep 2006 20:38:41 -0000	1.10
+++ cust_bill_event.html	3 Feb 2007 11:36:30 -0000	1.11
@@ -56,3 +56,9 @@
     </FORM>
 
 <% include('/elements/footer.html') %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Billing event reports');
+
+</%init>

Index: cust_pay_batch.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/cust_pay_batch.cgi,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- cust_pay_batch.cgi	10 Jan 2007 00:42:23 -0000	1.4
+++ cust_pay_batch.cgi	3 Feb 2007 11:36:30 -0000	1.5
@@ -1,116 +1,3 @@
-%my( $count_query, $sql_query );
-%my $hashref = {};
-%my @search = ();
-%my $orderby = 'paybatchnum';
-%
-%my( $pay_batch, $batchnum ) = ( '', '');
-%if ( $cgi->param('batchnum') && $cgi->param('batchnum') =~ /^(\d+)$/ ) {
-%  push @search, "batchnum = $1";
-%  $pay_batch = qsearchs('pay_batch', { 'batchnum' => $1 } );
-%  die "Batch $1 not found!" unless $pay_batch;
-%  $batchnum = $pay_batch->batchnum;
-%}
-%
-%if ( $cgi->param('payby') ) {
-%  $cgi->param('payby') =~ /^(CARD|CHEK)$/
-%    or die "illegal payby " . $cgi->param('payby');
-%
-%  push @search, "cust_pay_batch.payby = '$1'";
-%}
-%
-%if ( not $cgi->param('dcln') ) {
-%  push @search, "cpb.status IS DISTINCT FROM 'Approved'";
-%}
-%
-%my ($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
-%unless ($pay_batch){
-%  push @search, "pay_batch.upload >= $beginning" if ($beginning);
-%  push @search, "pay_batch.upload <= $ending" if ($ending < 4294967295);#2^32-1
-%  $orderby = "pay_batch.download,paybatchnum";
-%}
-%
-%push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
-%my $search = ' WHERE ' . join(' AND ', @search);
-%
-%$count_query = 'SELECT COUNT(*) FROM cust_pay_batch AS cpb ' .
-%                  'LEFT JOIN cust_main USING ( custnum ) ' .
-%                  'LEFT JOIN pay_batch USING ( batchnum )' .
-%		  $search;
-%
-%#grr
-%$sql_query = "SELECT paybatchnum,invnum,custnum,cpb.last,cpb.first," .
-%             "cpb.payname,cpb.payinfo,cpb.exp,amount,cpb.status " .
-%	     "FROM cust_pay_batch AS cpb " .
-%             'LEFT JOIN cust_main USING ( custnum ) ' .
-%             'LEFT JOIN pay_batch USING ( batchnum ) ' .
-%             "$search ORDER BY $orderby";
-%
-%my $html_init = '';
-%if ( $pay_batch ) {
-%  my $conf = new FS::Conf;
-%  my $fixed = $conf->config('batch-fixed_format-'. $pay_batch->payby);
-%  if (
-%       $pay_batch->status eq 'O' 
-%       || ( $pay_batch->status eq 'I'
-%            && $FS::CurrentUser::CurrentUser->access_right('Reprocess batches')
-%          ) 
-%  ) {
-%    $html_init .= qq!<FORM ACTION="$p/misc/download-batch.cgi" METHOD="POST">!;
-%    if ( $fixed ) {
-%      $html_init .= qq!<INPUT TYPE="hidden" NAME="format" VALUE="$fixed">!;
-%    } else {
-%      $html_init .= qq!Download batch in format <SELECT NAME="format">!.
-%                    qq!<OPTION VALUE="">Default batch mode</OPTION>!.
-%                    qq!<OPTION VALUE="csv-td_canada_trust-merchant_pc_batch">CSV file for TD Canada Trust Merchant PC Batch</OPTION>!.
-%                    qq!<OPTION VALUE="csv-chase_canada-E-xactBatch">CSV file for Chase Canada E-xactBatch</OPTION>!.
-%                    qq!<OPTION VALUE="PAP">80 byte file for TD Canada Trust PAP Batch</OPTION>!.
-%                    qq!<OPTION VALUE="BoM">Bank of Montreal ECA batch</OPTION>!.
-%                    qq!</SELECT>!;
-%    }
-%    $html_init .= qq!<INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum"><INPUT TYPE="submit" VALUE="Download"></FORM><BR>!;
-%  }
-%
-%  if (
-%       $pay_batch->status eq 'I' 
-%       || ( $pay_batch->status eq 'R'
-%            && $FS::CurrentUser::CurrentUser->access_right('Reprocess batches')
-%          ) 
-%  ) {
-%    $html_init .= qq!<FORM ACTION="$p/misc/upload-batch.cgi" METHOD="POST" ENCTYPE="multipart/form-data">!.
-%                  qq!Upload results<BR>!.
-%                  qq!Filename <INPUT TYPE="file" NAME="batch_results"><BR>!;
-%    if ( $fixed ) {
-%      $html_init .= qq!<INPUT TYPE="hidden" NAME="format" VALUE="$fixed">!;
-%    } else {
-%      $html_init .= qq!Format <SELECT NAME="format">!.
-%                    qq!<OPTION VALUE="">Default batch mode</OPTION>!.
-%                    qq!<OPTION VALUE="csv-td_canada_trust-merchant_pc_batch">CSV results from TD Canada Trust Merchant PC Batch</OPTION>!.
-%                    qq!<OPTION VALUE="csv-chase_canada-E-xactBatch">CSV file for Chase Canada E-xactBatch</OPTION>!.
-%                    qq!<OPTION VALUE="PAP">264 byte results for TD Canada Trust PAP Batch</OPTION>!.
-%                    qq!<OPTION VALUE="BoM">Bank of Montreal ECA results</OPTION>!.
-%                    qq!</SELECT><BR>!;
-%    }
-%    $html_init .= qq!<INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum">!;
-%    $html_init .= '<INPUT TYPE="submit" VALUE="Upload"></FORM><BR>';
-%  }
-%
-%}
-%
-%if ($pay_batch) {
-%  my $sth = dbh->prepare($count_query) or die dbh->errstr. "doing $count_query";
-%  $sth->execute or die "Error executing \"$count_query\": ". $sth->errstr;
-%  my $cards = $sth->fetchrow_arrayref->[0];
-%
-%  my $st = "SELECT SUM(amount) from cust_pay_batch WHERE batchnum=". $batchnum;
-%  $sth = dbh->prepare($st) or die dbh->errstr. "doing $st";
-%  $sth->execute or die "Error executing \"$st\": ". $sth->errstr;
-%  my $total = $sth->fetchrow_arrayref->[0];
-%
-%  $html_init .= "$cards credit card payments batched<BR>\$" .
-%                sprintf("%.2f", $total) ." total in batch<BR>";
-%}
-%
-%
 <% include('elements/search.html',
               'title'       => 'Batch payment details',
               'name'        => 'batch details',
@@ -169,4 +56,122 @@
 			       ],
       )
 %>
+<%init>
 
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Financial reports')
+      || $FS::CurrentUser::CurrentUser->access_right('Process batches');
+
+my( $count_query, $sql_query );
+my $hashref = {};
+my @search = ();
+my $orderby = 'paybatchnum';
+
+my( $pay_batch, $batchnum ) = ( '', '');
+if ( $cgi->param('batchnum') && $cgi->param('batchnum') =~ /^(\d+)$/ ) {
+  push @search, "batchnum = $1";
+  $pay_batch = qsearchs('pay_batch', { 'batchnum' => $1 } );
+  die "Batch $1 not found!" unless $pay_batch;
+  $batchnum = $pay_batch->batchnum;
+}
+
+if ( $cgi->param('payby') ) {
+  $cgi->param('payby') =~ /^(CARD|CHEK)$/
+    or die "illegal payby " . $cgi->param('payby');
+
+  push @search, "cust_pay_batch.payby = '$1'";
+}
+
+if ( not $cgi->param('dcln') ) {
+  push @search, "cpb.status IS DISTINCT FROM 'Approved'";
+}
+
+my ($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
+unless ($pay_batch){
+  push @search, "pay_batch.upload >= $beginning" if ($beginning);
+  push @search, "pay_batch.upload <= $ending" if ($ending < 4294967295);#2^32-1
+  $orderby = "pay_batch.download,paybatchnum";
+}
+
+push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
+my $search = ' WHERE ' . join(' AND ', @search);
+
+$count_query = 'SELECT COUNT(*) FROM cust_pay_batch AS cpb ' .
+                  'LEFT JOIN cust_main USING ( custnum ) ' .
+                  'LEFT JOIN pay_batch USING ( batchnum )' .
+		  $search;
+
+#grr
+$sql_query = "SELECT paybatchnum,invnum,custnum,cpb.last,cpb.first," .
+             "cpb.payname,cpb.payinfo,cpb.exp,amount,cpb.status " .
+	     "FROM cust_pay_batch AS cpb " .
+             'LEFT JOIN cust_main USING ( custnum ) ' .
+             'LEFT JOIN pay_batch USING ( batchnum ) ' .
+             "$search ORDER BY $orderby";
+
+my $html_init = '';
+if ( $pay_batch ) {
+  my $conf = new FS::Conf;
+  my $fixed = $conf->config('batch-fixed_format-'. $pay_batch->payby);
+  if (
+       $pay_batch->status eq 'O' 
+       || ( $pay_batch->status eq 'I'
+            && $FS::CurrentUser::CurrentUser->access_right('Reprocess batches')
+          ) 
+  ) {
+    $html_init .= qq!<FORM ACTION="$p/misc/download-batch.cgi" METHOD="POST">!;
+    if ( $fixed ) {
+      $html_init .= qq!<INPUT TYPE="hidden" NAME="format" VALUE="$fixed">!;
+    } else {
+      $html_init .= qq!Download batch in format <SELECT NAME="format">!.
+                    qq!<OPTION VALUE="">Default batch mode</OPTION>!.
+                    qq!<OPTION VALUE="csv-td_canada_trust-merchant_pc_batch">CSV file for TD Canada Trust Merchant PC Batch</OPTION>!.
+                    qq!<OPTION VALUE="csv-chase_canada-E-xactBatch">CSV file for Chase Canada E-xactBatch</OPTION>!.
+                    qq!<OPTION VALUE="PAP">80 byte file for TD Canada Trust PAP Batch</OPTION>!.
+                    qq!<OPTION VALUE="BoM">Bank of Montreal ECA batch</OPTION>!.
+                    qq!</SELECT>!;
+    }
+    $html_init .= qq!<INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum"><INPUT TYPE="submit" VALUE="Download"></FORM><BR>!;
+  }
+
+  if (
+       $pay_batch->status eq 'I' 
+       || ( $pay_batch->status eq 'R'
+            && $FS::CurrentUser::CurrentUser->access_right('Reprocess batches')
+          ) 
+  ) {
+    $html_init .= qq!<FORM ACTION="$p/misc/upload-batch.cgi" METHOD="POST" ENCTYPE="multipart/form-data">!.
+                  qq!Upload results<BR>!.
+                  qq!Filename <INPUT TYPE="file" NAME="batch_results"><BR>!;
+    if ( $fixed ) {
+      $html_init .= qq!<INPUT TYPE="hidden" NAME="format" VALUE="$fixed">!;
+    } else {
+      $html_init .= qq!Format <SELECT NAME="format">!.
+                    qq!<OPTION VALUE="">Default batch mode</OPTION>!.
+                    qq!<OPTION VALUE="csv-td_canada_trust-merchant_pc_batch">CSV results from TD Canada Trust Merchant PC Batch</OPTION>!.
+                    qq!<OPTION VALUE="csv-chase_canada-E-xactBatch">CSV file for Chase Canada E-xactBatch</OPTION>!.
+                    qq!<OPTION VALUE="PAP">264 byte results for TD Canada Trust PAP Batch</OPTION>!.
+                    qq!<OPTION VALUE="BoM">Bank of Montreal ECA results</OPTION>!.
+                    qq!</SELECT><BR>!;
+    }
+    $html_init .= qq!<INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum">!;
+    $html_init .= '<INPUT TYPE="submit" VALUE="Upload"></FORM><BR>';
+  }
+
+}
+
+if ($pay_batch) {
+  my $sth = dbh->prepare($count_query) or die dbh->errstr. "doing $count_query";
+  $sth->execute or die "Error executing \"$count_query\": ". $sth->errstr;
+  my $cards = $sth->fetchrow_arrayref->[0];
+
+  my $st = "SELECT SUM(amount) from cust_pay_batch WHERE batchnum=". $batchnum;
+  $sth = dbh->prepare($st) or die dbh->errstr. "doing $st";
+  $sth->execute or die "Error executing \"$st\": ". $sth->errstr;
+  my $total = $sth->fetchrow_arrayref->[0];
+
+  $html_init .= "$cards credit card payments batched<BR>\$" .
+                sprintf("%.2f", $total) ." total in batch<BR>";
+}
+
+</%init>

Index: inventory_item.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/inventory_item.html,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- inventory_item.html	23 Aug 2006 22:25:38 -0000	1.4
+++ inventory_item.html	3 Feb 2007 11:36:30 -0000	1.5
@@ -1,54 +1,4 @@
-%
-%
-%my $classnum = $cgi->param('classnum');
-%$classnum =~ /^(\d+)$/ or eidiot "illegal classnum $classnum";
-%$classnum = $1;
-%
-%my $inventory_class = qsearchs( {
-%  'table'     => 'inventory_class',
-%  'hashref'   => { 'classnum' => $classnum },
-%} );
-%
-%my $title = $inventory_class->classname. ' Inventory';
-%
-%#little false laziness with SQL fragments in inventory_class.pm
-%my $extra_sql = '';
-%if ( $cgi->param('avail') ) {
-%  $extra_sql = 'AND ( svcnum IS NULL OR svcnum = 0 )';
-%  $title .= ' - Available';
-%} elsif ( $cgi->param('used') ) {
-%  $extra_sql = 'AND svcnum IS NOT NULL AND svcnum > 0';
-%  $title .= ' - In use';
-%}
-%
-%my $count_query =
-%  "SELECT COUNT(*) FROM inventory_item WHERE classnum = $classnum $extra_sql";
-%
-%my $link = sub {
-%  my $inventory_item = shift;
-%  if ( $inventory_item->svcnum ) {
-%    [ "${p}view/svc_acct.cgi?", 'svcnum' ];
-%  } else {
-%    '';
-%  }
-%};
-%my $link_cust = sub {
-%  my $inventory_item = shift;
-%  if ( $inventory_item->custnum ) {
-%    [ "${p}view/cust_main.cgi?", 'custnum' ];
-%  } else {
-%    '';
-%  }
-%};
-%
-%my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
-%                ' LEFT JOIN part_svc  USING ( svcpart ) '.
-%                ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
-%                ' LEFT JOIN cust_main USING ( custnum ) ';
-%
-%
 <% include( 'elements/search.html',
-
                  'title'       => $title,
 
                  #less lame to use Lingua:: something to pluralize
@@ -97,13 +47,79 @@
                    \&FS::UI::Web::cust_fields,
 
                  ],
-
+                 'align'       => 'rll'.FS::UI::Web::cust_aligns(),
                  'links'       => [
                    '',
                    '',
                    $link,
-                   ( map { $link_cust } FS::UI::Web::cust_header() ),
+                   ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
+                         FS::UI::Web::cust_header()
+                   ),
                  ],
+                 'color' => [ 
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_colors(),
+                            ],
+                 'style' => [ 
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_styles(),
+                            ],
 
              )
 %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
+
+my $classnum = $cgi->param('classnum');
+$classnum =~ /^(\d+)$/ or eidiot "illegal classnum $classnum";
+$classnum = $1;
+
+my $inventory_class = qsearchs( {
+  'table'     => 'inventory_class',
+  'hashref'   => { 'classnum' => $classnum },
+} );
+
+my $title = $inventory_class->classname. ' Inventory';
+
+#little false laziness with SQL fragments in inventory_class.pm
+my $extra_sql = '';
+if ( $cgi->param('avail') ) {
+  $extra_sql = 'AND ( svcnum IS NULL OR svcnum = 0 )';
+  $title .= ' - Available';
+} elsif ( $cgi->param('used') ) {
+  $extra_sql = 'AND svcnum IS NOT NULL AND svcnum > 0';
+  $title .= ' - In use';
+}
+
+my $count_query =
+  "SELECT COUNT(*) FROM inventory_item WHERE classnum = $classnum $extra_sql";
+
+my $link = sub {
+  my $inventory_item = shift;
+  if ( $inventory_item->svcnum ) {
+    [ "${p}view/svc_acct.cgi?", 'svcnum' ];
+  } else {
+    '';
+  }
+};
+my $link_cust = sub {
+  my $inventory_item = shift;
+  if ( $inventory_item->custnum ) {
+    [ "${p}view/cust_main.cgi?", 'custnum' ];
+  } else {
+    '';
+  }
+};
+
+my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
+                ' LEFT JOIN part_svc  USING ( svcpart ) '.
+                ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
+                ' LEFT JOIN cust_main USING ( custnum ) ';
+
+</%init>

Index: report_cust_pkg.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/report_cust_pkg.html,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- report_cust_pkg.html	25 Oct 2006 04:44:48 -0000	1.3
+++ report_cust_pkg.html	3 Feb 2007 11:36:30 -0000	1.4
@@ -46,3 +46,9 @@
 </FORM>
 
 <% include('/elements/footer.html') %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List packages');
+
+</%init>

Index: report_cust_pay_batch.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/report_cust_pay_batch.html,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- report_cust_pay_batch.html	26 Aug 2006 23:15:14 -0000	1.1
+++ report_cust_pay_batch.html	3 Feb 2007 11:36:30 -0000	1.2
@@ -35,3 +35,9 @@
 </FORM>
 
 <% include('/elements/footer.html') %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
+
+</%init>

Index: svc_forward.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/svc_forward.cgi,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- svc_forward.cgi	29 Dec 2006 08:51:33 -0000	1.11
+++ svc_forward.cgi	3 Feb 2007 11:36:30 -0000	1.12
@@ -1,99 +1,3 @@
-%my $conf = new FS::Conf;
-%
-%my $orderby = 'ORDER BY svcnum';
-%my @extra_sql = ();
-%if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
-%
-%  push @extra_sql, 'pkgnum IS NULL'
-%    if $cgi->param('magic') eq 'unlinked';
-%
-%  if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
-%    my $sortby = $1;
-%    $orderby = "ORDER BY $sortby";
-%  }
-%
-%} elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
-%  push @extra_sql, "svcpart = $1";
-%}
-%
-%my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
-%                ' LEFT JOIN part_svc  USING ( svcpart ) '.
-%                ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
-%                ' LEFT JOIN cust_main USING ( custnum ) ';
-%
-%#here is the agent virtualization
-%push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql;
-%
-%my $extra_sql = 
-%  scalar(@extra_sql)
-%    ? ' WHERE '. join(' AND ', @extra_sql )
-%    : '';
-%
-%my $count_query = "SELECT COUNT(*) FROM svc_forward $addl_from $extra_sql";
-%my $sql_query = {
-%  'table'     => 'svc_forward',
-%  'hashref'   => {},
-%  'select'    => join(', ',
-%                   'svc_forward.*',
-%                   'part_svc.svc',
-%                   'cust_main.custnum',
-%                   FS::UI::Web::cust_sql_fields(),
-%                 ),
-%  'extra_sql' => "$extra_sql $orderby",
-%  'addl_from' => $addl_from,
-%};
-%
-%#        <TH>Service #<BR><FONT SIZE=-1>(click to view forward)</FONT></TH>
-%#        <TH>Mail to<BR><FONT SIZE=-1>(click to view account)</FONT></TH>
-%#        <TH>Forwards to<BR><FONT SIZE=-1>(click to view account)</FONT></TH>
-%
-%my $link = [ "${p}view/svc_forward.cgi?", 'svcnum' ];
-%
-%my $format_src = sub {
-%  my $svc_forward = shift;
-%  if ( $svc_forward->srcsvc_acct ) {
-%    $svc_forward->srcsvc_acct->email;
-%  } else {
-%    my $src = $svc_forward->src;
-%    $src = "<I>(anything)</I>$src" if $src =~ /^@/;
-%    $src;
-%  }
-%};
-%
-%my $link_src = sub {
-%  my $svc_forward = shift;
-%  if ( $svc_forward->srcsvc_acct ) {
-%    [ "${p}view/svc_acct.cgi?", 'srcsvc' ];
-%  } else {
-%    '';
-%  }
-%};
-%
-%my $format_dst = sub {
-%  my $svc_forward = shift;
-%  if ( $svc_forward->dstsvc_acct ) {
-%    $svc_forward->dstsvc_acct->email;
-%  } else {
-%    $svc_forward->dst;
-%  }
-%};
-%
-%my $link_dst = sub {
-%  my $svc_forward = shift;
-%  if ( $svc_forward->dstsvc_acct ) {
-%    [ "${p}view/svc_acct.cgi?", 'dstsvc' ];
-%  } else {
-%    '';
-%  }
-%};
-%
-%#smaller false laziness w/svc_*.cgi here
-%my $link_cust = sub {
-%  my $svc_x = shift;
-%  $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
-%};
-%
-%
 <% include( 'elements/search.html',
                  'title'             => "Mail forward Search Results",
                  'name'              => 'mail forwards',
@@ -116,9 +20,126 @@
                                           $link,
                                           $link_src,
                                           $link_dst,
-                                          ( map { $link_cust }
+                                          ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
                                                 FS::UI::Web::cust_header()
                                           ),
                                         ],
+                 'align' => 'rlll'. FS::UI::Web::cust_aligns(),
+                 'color' => [ 
+                              '',
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_colors(),
+                            ],
+                 'style' => [ 
+                              '',
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_styles(),
+                            ],
              )
 %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List services');
+
+
+my $conf = new FS::Conf;
+
+my $orderby = 'ORDER BY svcnum';
+my @extra_sql = ();
+if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
+
+  push @extra_sql, 'pkgnum IS NULL'
+    if $cgi->param('magic') eq 'unlinked';
+
+  if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
+    my $sortby = $1;
+    $orderby = "ORDER BY $sortby";
+  }
+
+} elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
+  push @extra_sql, "svcpart = $1";
+}
+
+my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
+                ' LEFT JOIN part_svc  USING ( svcpart ) '.
+                ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
+                ' LEFT JOIN cust_main USING ( custnum ) ';
+
+#here is the agent virtualization
+push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql;
+
+my $extra_sql = 
+  scalar(@extra_sql)
+    ? ' WHERE '. join(' AND ', @extra_sql )
+    : '';
+
+my $count_query = "SELECT COUNT(*) FROM svc_forward $addl_from $extra_sql";
+my $sql_query = {
+  'table'     => 'svc_forward',
+  'hashref'   => {},
+  'select'    => join(', ',
+                   'svc_forward.*',
+                   'part_svc.svc',
+                   'cust_main.custnum',
+                   FS::UI::Web::cust_sql_fields(),
+                 ),
+  'extra_sql' => "$extra_sql $orderby",
+  'addl_from' => $addl_from,
+};
+
+#        <TH>Service #<BR><FONT SIZE=-1>(click to view forward)</FONT></TH>
+#        <TH>Mail to<BR><FONT SIZE=-1>(click to view account)</FONT></TH>
+#        <TH>Forwards to<BR><FONT SIZE=-1>(click to view account)</FONT></TH>
+
+my $link = [ "${p}view/svc_forward.cgi?", 'svcnum' ];
+
+my $format_src = sub {
+  my $svc_forward = shift;
+  if ( $svc_forward->srcsvc_acct ) {
+    $svc_forward->srcsvc_acct->email;
+  } else {
+    my $src = $svc_forward->src;
+    $src = "<I>(anything)</I>$src" if $src =~ /^@/;
+    $src;
+  }
+};
+
+my $link_src = sub {
+  my $svc_forward = shift;
+  if ( $svc_forward->srcsvc_acct ) {
+    [ "${p}view/svc_acct.cgi?", 'srcsvc' ];
+  } else {
+    '';
+  }
+};
+
+my $format_dst = sub {
+  my $svc_forward = shift;
+  if ( $svc_forward->dstsvc_acct ) {
+    $svc_forward->dstsvc_acct->email;
+  } else {
+    $svc_forward->dst;
+  }
+};
+
+my $link_dst = sub {
+  my $svc_forward = shift;
+  if ( $svc_forward->dstsvc_acct ) {
+    [ "${p}view/svc_acct.cgi?", 'dstsvc' ];
+  } else {
+    '';
+  }
+};
+
+#smaller false laziness w/svc_*.cgi here
+my $link_cust = sub {
+  my $svc_x = shift;
+  $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
+};
+
+</%init>

Index: cust_bill.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/cust_bill.html,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -d -r1.18 -r1.19
--- cust_bill.html	23 Aug 2006 22:25:38 -0000	1.18
+++ cust_bill.html	3 Feb 2007 11:36:30 -0000	1.19
@@ -1,166 +1,3 @@
-%
-%
-%   my $join_cust_main = 'LEFT JOIN cust_main USING ( custnum )';
-%   #here is the agent virtualization
-%   my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
-%
-%   my( $count_query, $sql_query );
-%   my( $count_addl ) = ( '' );
-%   my( $distinct ) = ( '' );
-%   my($begin, $end) = ( '', '' );
-%   my($agentnum) = ( '' );
-%   my($open, $days) = ( '', '' );
-%   if ( $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/ ) {
-%     $count_query =
-%       "SELECT COUNT(*) FROM cust_bill $join_cust_main".
-%       "  WHERE invnum = $2 AND $agentnums_sql"; #agent virtualization
-%     $sql_query = {
-%       'table'     => 'cust_bill',
-%       'addl_from' => $join_cust_main,
-%       'hashref'   => { 'invnum' => $2 },
-%       #'select'    => '*',
-%       'extra_sql' => " AND $agentnums_sql", #agent virtualization
-%     };
-%   } else {
-%   #if ( $cgi->param('begin') || $cgi->param('end')
-%   #     || $cgi->param('beginning') || $cgi->param('ending')
-%   #     || $cgi->keywords
-%   #   )
-%   #{
-%
-%     #some false laziness w/cust_bill::re_X
-%     my @where;
-%     my $orderby = 'ORDER BY cust_bill._date';
-%
-%     if ( $cgi->param('beginning')
-%          && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
-%       $begin = str2time($1);
-%       push @where, "cust_bill._date >= $begin";
-%     }
-%     if ( $cgi->param('ending')
-%           && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
-%       $end = str2time($1) + 86399;
-%       push @where, "cust_bill._date < $end";
-%     }
-%
-%     if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
-%       $begin = $1;
-%       push @where, "cust_bill._date >= $begin";
-%     }
-%     if ( $cgi->param('end') =~ /^(\d+)$/ ) {
-%       $end = $1;
-%       push @where, "cust_bill._date < $end";
-%     }
-%
-%     if ( $cgi->param('invnum_min') =~ /^\s*(\d+)\s*$/ ) {
-%       push @where, "cust_bill.invnum >= $1";
-%     }
-%     if ( $cgi->param('invnum_max') =~ /^\s*(\d+)\s*$/ ) {
-%       push @where, "cust_bill.invnum <= $1";
-%     }
-%
-%     if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
-%       $agentnum = $1;
-%       push @where, "cust_main.agentnum = $agentnum";
-%     }
-%
-%     my $owed =
-%       "charged - ( SELECT COALESCE(SUM(amount),0) FROM cust_bill_pay
-%                    WHERE cust_bill_pay.invnum = cust_bill.invnum )
-%                - ( SELECT COALESCE(SUM(amount),0) FROM cust_credit_bill
-%                    WHERE cust_credit_bill.invnum = cust_bill.invnum )";
-%
-%     if ( $cgi->param('open') ) {
-%       push @where, "0 != $owed";
-%       $open = 1;
-%     }
-%
-%     my($query) = $cgi->keywords;
-%     if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
-%       ($open, $days, my $field) = ($1, $2, $3);
-%       $field = "_date" if $field eq 'date';
-%       $orderby = "ORDER BY cust_bill.$field";
-%       push @where, "0 != $owed" if $open;
-%       push @where, "cust_bill._date < ". (time-86400*$days) if $days;
-%     }
-%
-%     #here is the agent virtualization
-%     push @where, $agentnums_sql;
-%     my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
-%
-%     if ( $cgi->param('newest_percust') ) {
-%       $distinct = 'DISTINCT ON ( cust_bill.custnum )';
-%       $orderby = 'ORDER BY cust_bill.custnum ASC, cust_bill._date DESC';
-%       #$count_query = "SELECT 'N/A', 'N/A', 'N/A'"; #XXXXXXX fix
-%       $count_query = "SELECT COUNT(DISTINCT cust_bill.custnum), 'N/A', 'N/A'";
-%     }
-%   
-%     unless ( $count_query ) {
-%       $count_query = "SELECT COUNT(*), sum(charged), sum($owed)";
-%       $count_addl = [ '$%.2f total invoiced',
-%                       '$%.2f total outstanding balance',
-%                     ];
-%     }
-%     $count_query .=  " FROM cust_bill $join_cust_main $extra_sql";
-%   
-%     $sql_query = {
-%       'table'     => 'cust_bill',
-%       'addl_from' => $join_cust_main,
-%       'hashref'   => {},
-%       'select'    => "$distinct ". join(', ',
-%                        'cust_bill.*',
-%                        #( map "cust_main.$_", qw(custnum last first company) ),
-%                        'cust_main.custnum as cust_main_custnum',
-%                        FS::UI::Web::cust_sql_fields(),
-%                        "$owed as owed",
-%                      ),
-%       'extra_sql' => "$extra_sql $orderby"
-%     };
-%   
-%   }
-%
-%   my $link  = [ "${p}view/cust_bill.cgi?", 'invnum', ];
-%   my $clink = sub {
-%     my $cust_bill = shift;
-%     $cust_bill->cust_main_custnum
-%       ? [ "${p}view/cust_main.cgi?", 'custnum' ]
-%       : '';
-%   };
-%
-%  my $conf = new FS::Conf;
-%  my $money_char = $conf->config('money_char') || '$';
-%
-%  my $html_init = join("\n", map {
-%    ( my $action = $_ ) =~ s/_$//;
-%    include('/elements/progress-init.html',
-%              $_.'form',
-%              [ 'begin', 'end', 'agentnum', 'open', 'days', 'newest_percust' ],
-%              "../misc/${_}invoices.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="begin"     VALUE="$begin">!,
-%    qq!<INPUT TYPE="hidden" NAME="end"       VALUE="$end">!,
-%    qq!<INPUT TYPE="hidden" NAME="agentnum"  VALUE="$agentnum">!,
-%    qq!<INPUT TYPE="hidden" NAME="open"      VALUE="$open">!,
-%    qq!<INPUT TYPE="hidden" NAME="days"      VALUE="$days">!,
-%    qq!</FORM>!
-%  } qw( print_ email_ fax_ ) );
-%
-%  my $menubar =  [
-%                   'Main menu' => $p,
-%                   'Print these invoices' =>
-%                     "javascript:print_process()",
-%                   'Email these invoices' =>
-%                     "javascript:email_process()",
-%                 ];
-%
-%  push @$menubar, 'Fax these invoices' =>
-%                    "javascript:fax_process()"
-%    if $conf->exists('hylafax');
-%
-%
 <% include( 'elements/search.html',
                  'title'       => 'Invoice Search Results',
                  'html_init'   => $html_init,
@@ -183,14 +20,197 @@
                    sub { time2str('%b %d %Y', shift->_date ) },
                    \&FS::UI::Web::cust_fields,
                  ],
-                 'align' => 'rrrrll',
+                 'align' => 'rrrr'.FS::UI::Web::cust_aligns(),
                  'links' => [
                    $link,
                    $link,
                    $link,
                    $link,
-                   ( map { $clink } FS::UI::Web::cust_header() ),
+                   ( map { $_ ne 'Cust. Status' ? $clink : '' }
+                         FS::UI::Web::cust_header()
+                   ),
                  ],
+                 'color' => [ 
+                              '',
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_colors(),
+                            ],
+                 'style' => [ 
+                              '',
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_styles(),
+                            ],
+
   
       )
 %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List invoices');
+
+my $join_cust_main = 'LEFT JOIN cust_main USING ( custnum )';
+#here is the agent virtualization
+my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
+
+my( $count_query, $sql_query );
+my( $count_addl ) = ( '' );
+my( $distinct ) = ( '' );
+my($begin, $end) = ( '', '' );
+my($agentnum) = ( '' );
+my($open, $days) = ( '', '' );
+if ( $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/ ) {
+  $count_query =
+    "SELECT COUNT(*) FROM cust_bill $join_cust_main".
+    "  WHERE invnum = $2 AND $agentnums_sql"; #agent virtualization
+  $sql_query = {
+    'table'     => 'cust_bill',
+    'addl_from' => $join_cust_main,
+    'hashref'   => { 'invnum' => $2 },
+    #'select'    => '*',
+    'extra_sql' => " AND $agentnums_sql", #agent virtualization
+  };
+} else {
+#if ( $cgi->param('begin') || $cgi->param('end')
+#     || $cgi->param('beginning') || $cgi->param('ending')
+#     || $cgi->keywords
+#   )
+#{
+
+  #some false laziness w/cust_bill::re_X
+  my @where;
+  my $orderby = 'ORDER BY cust_bill._date';
+
+  if ( $cgi->param('beginning')
+       && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
+    $begin = str2time($1);
+    push @where, "cust_bill._date >= $begin";
+  }
+  if ( $cgi->param('ending')
+        && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
+    $end = str2time($1) + 86399;
+    push @where, "cust_bill._date < $end";
+  }
+
+  if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
+    $begin = $1;
+    push @where, "cust_bill._date >= $begin";
+  }
+  if ( $cgi->param('end') =~ /^(\d+)$/ ) {
+    $end = $1;
+    push @where, "cust_bill._date < $end";
+  }
+
+  if ( $cgi->param('invnum_min') =~ /^\s*(\d+)\s*$/ ) {
+    push @where, "cust_bill.invnum >= $1";
+  }
+  if ( $cgi->param('invnum_max') =~ /^\s*(\d+)\s*$/ ) {
+    push @where, "cust_bill.invnum <= $1";
+  }
+
+  if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
+    $agentnum = $1;
+    push @where, "cust_main.agentnum = $agentnum";
+  }
+
+  my $owed =
+    "charged - ( SELECT COALESCE(SUM(amount),0) FROM cust_bill_pay
+                 WHERE cust_bill_pay.invnum = cust_bill.invnum )
+             - ( SELECT COALESCE(SUM(amount),0) FROM cust_credit_bill
+                 WHERE cust_credit_bill.invnum = cust_bill.invnum )";
+
+  if ( $cgi->param('open') ) {
+    push @where, "0 != $owed";
+    $open = 1;
+  }
+
+  my($query) = $cgi->keywords;
+  if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
+    ($open, $days, my $field) = ($1, $2, $3);
+    $field = "_date" if $field eq 'date';
+    $orderby = "ORDER BY cust_bill.$field";
+    push @where, "0 != $owed" if $open;
+    push @where, "cust_bill._date < ". (time-86400*$days) if $days;
+  }
+
+  #here is the agent virtualization
+  push @where, $agentnums_sql;
+  my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
+
+  if ( $cgi->param('newest_percust') ) {
+    $distinct = 'DISTINCT ON ( cust_bill.custnum )';
+    $orderby = 'ORDER BY cust_bill.custnum ASC, cust_bill._date DESC';
+    #$count_query = "SELECT 'N/A', 'N/A', 'N/A'"; #XXXXXXX fix
+    $count_query = "SELECT COUNT(DISTINCT cust_bill.custnum), 'N/A', 'N/A'";
+  }
+
+  unless ( $count_query ) {
+    $count_query = "SELECT COUNT(*), sum(charged), sum($owed)";
+    $count_addl = [ '$%.2f total invoiced',
+                    '$%.2f total outstanding balance',
+                  ];
+  }
+  $count_query .=  " FROM cust_bill $join_cust_main $extra_sql";
+
+  $sql_query = {
+    'table'     => 'cust_bill',
+    'addl_from' => $join_cust_main,
+    'hashref'   => {},
+    'select'    => "$distinct ". join(', ',
+                     'cust_bill.*',
+                     #( map "cust_main.$_", qw(custnum last first company) ),
+                     'cust_main.custnum as cust_main_custnum',
+                     FS::UI::Web::cust_sql_fields(),
+                     "$owed as owed",
+                   ),
+    'extra_sql' => "$extra_sql $orderby"
+  };
+
+}
+
+my $link  = [ "${p}view/cust_bill.cgi?", 'invnum', ];
+my $clink = sub {
+  my $cust_bill = shift;
+  $cust_bill->cust_main_custnum
+    ? [ "${p}view/cust_main.cgi?", 'custnum' ]
+    : '';
+};
+
+my $conf = new FS::Conf;
+my $money_char = $conf->config('money_char') || '$';
+
+my $html_init = join("\n", map {
+ ( my $action = $_ ) =~ s/_$//;
+ include('/elements/progress-init.html',
+           $_.'form',
+           [ 'begin', 'end', 'agentnum', 'open', 'days', 'newest_percust' ],
+           "../misc/${_}invoices.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="begin"     VALUE="$begin">!,
+ qq!<INPUT TYPE="hidden" NAME="end"       VALUE="$end">!,
+ qq!<INPUT TYPE="hidden" NAME="agentnum"  VALUE="$agentnum">!,
+ qq!<INPUT TYPE="hidden" NAME="open"      VALUE="$open">!,
+ qq!<INPUT TYPE="hidden" NAME="days"      VALUE="$days">!,
+ qq!</FORM>!
+} qw( print_ email_ fax_ ) );
+
+my $menubar =  [
+                'Main menu' => $p,
+                'Print these invoices' =>
+                  "javascript:print_process()",
+                'Email these invoices' =>
+                  "javascript:email_process()",
+              ];
+
+push @$menubar, 'Fax these invoices' =>
+                 "javascript:fax_process()"
+ if $conf->exists('hylafax');
+
+</%init>

Index: report_tax.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/report_tax.cgi,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -d -r1.33 -r1.34
--- report_tax.cgi	23 Aug 2006 22:25:38 -0000	1.33
+++ report_tax.cgi	3 Feb 2007 11:36:30 -0000	1.34
@@ -1,374 +1,3 @@
-%
-%
-%my $conf = new FS::Conf;
-%my $money_char = $conf->config('money_char') || '$';
-%
-%my $user = getotaker;
-%
-%my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
-%
-%my $join_cust = "
-%    JOIN cust_bill USING ( invnum ) 
-%    LEFT JOIN cust_main USING ( custnum )
-%";
-%my $from_join_cust = "
-%    FROM cust_bill_pkg
-%    $join_cust
-%"; 
-%my $join_pkg = "
-%    LEFT JOIN cust_pkg USING ( pkgnum )
-%    LEFT JOIN part_pkg USING ( pkgpart )
-%";
-%
-%my $where = "WHERE _date >= $beginning AND _date <= $ending ";
-%my @base_param = qw( county county state state country );
-%if ( $conf->exists('tax-ship_address') ) {
-%
-%  $where .= "
-%      AND (    (     ( ship_last IS NULL     OR  ship_last  = '' )
-%                 AND ( county       = ? OR ? = '' )
-%                 AND ( state        = ? OR ? = '' )
-%                 AND   country      = ?
-%               )
-%            OR (       ship_last IS NOT NULL AND ship_last != ''
-%                 AND ( ship_county  = ? OR ? = '' )
-%                 AND ( ship_state   = ? OR ? = '' )
-%                 AND   ship_country = ?
-%               )
-%          )
-%  ";
-%  #    AND payby != 'COMP'
-%
-%  push @base_param, @base_param;
-%
-%} else {
-%
-%  $where .= "
-%      AND ( county  = ? OR ? = '' )
-%      AND ( state   = ? OR ? = '' )
-%      AND   country = ?
-%  ";
-%  #    AND payby != 'COMP'
-%
-%}
-%
-%my $agentname = '';
-%if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
-%  my $agent = qsearchs('agent', { 'agentnum' => $1 } );
-%  die "agent not found" unless $agent;
-%  $agentname = $agent->agent;
-%  $where .= ' AND agentnum = '. $agent->agentnum;
-%}
-%
-%my $gotcust = "
-%  WHERE 0 < ( SELECT COUNT(*) FROM cust_main
-%";
-%if ( $conf->exists('tax-ship_address') ) {
-%
-%  $gotcust .= "
-%                WHERE
-%
-%                (    cust_main_county.country = cust_main.country
-%                  OR cust_main_county.country = cust_main.ship_country
-%                )
-%
-%                AND
-%
-%                ( 
-%
-%                  (     ( ship_last IS NULL     OR  ship_last = '' )
-%                    AND (    cust_main_county.country = cust_main.country )
-%                    AND (    cust_main_county.state = cust_main.state
-%                          OR cust_main_county.state = ''
-%                          OR cust_main_county.state IS NULL )
-%                    AND (    cust_main_county.county = cust_main.county
-%                          OR cust_main_county.county = ''
-%                          OR cust_main_county.county IS NULL )
-%                  )
-%  
-%                  OR
-%  
-%                  (       ship_last IS NOT NULL AND ship_last != ''
-%                    AND (    cust_main_county.country = cust_main.ship_country )
-%                    AND (    cust_main_county.state = cust_main.ship_state
-%                          OR cust_main_county.state = ''
-%                          OR cust_main_county.state IS NULL )
-%                    AND (    cust_main_county.county = cust_main.ship_county
-%                          OR cust_main_county.county = ''
-%                          OR cust_main_county.county IS NULL )
-%                  )
-%
-%                )
-%
-%                LIMIT 1
-%            )
-%  ";
-%
-%} else {
-%
-%  $gotcust .= "
-%                WHERE ( cust_main.county  = cust_main_county.county
-%                        OR cust_main_county.county = ''
-%                        OR cust_main_county.county IS NULL )
-%                  AND ( cust_main.state   = cust_main_county.state
-%                        OR cust_main_county.state = ''
-%                        OR cust_main_county.state IS NULL )
-%                  AND ( cust_main.country = cust_main_county.country )
-%                LIMIT 1
-%            )
-%  ";
-%
-%}
-%
-%my($total, $tot_taxable, $owed, $tax) = ( 0, 0, 0, 0, 0 );
-%my( $exempt_cust, $exempt_pkg, $exempt_monthly ) = ( 0, 0 );
-%my $out = 'Out of taxable region(s)';
-%my %regions = ();
-%foreach my $r (qsearch('cust_main_county', {}, '', $gotcust) ) {
-%  #warn $r->county. ' '. $r->state. ' '. $r->country. "\n";
-%
-%  my $label = getlabel($r);
-%  $regions{$label}->{'label'} = $label;
-%  $regions{$label}->{'url_param'} = join(';', map "$_=".$r->$_(), qw( county state country ) );
-%
-%  my @param = @base_param;
-%  my $mywhere = $where;
-%
-%  if ( $r->taxclass ) {
-%    $mywhere .= " AND taxclass = ? ";
-%    push @param, 'taxclass';
-%    $regions{$label}->{'url_param'} .= ';taxclass='. $r->taxclass
-%      if $cgi->param('show_taxclasses');
-%  }
-%
-%  my $fromwhere = $from_join_cust. $join_pkg. $mywhere. " AND payby != 'COMP' ";
-%
-%#  my $label = getlabel($r);
-%#  $regions{$label}->{'label'} = $label;
-%
-%  my $nottax = 'pkgnum != 0';
-%
-%  ## calculate total for this region
-%
-%  my $t = scalar_sql($r, \@param,
-%    "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $fromwhere AND $nottax"
-%  );
-%  $total += $t;
-%  $regions{$label}->{'total'} += $t;
-%
-%  ## calculate customer-exemption for this region
-%
-%##  my $taxable = $t;
-%
-%#  my($taxable, $x_cust) = (0, 0);
-%#  foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i }
-%#                       qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
-%#    $taxable += scalar_sql($r, \@param, 
-%#      "SELECT SUM($e) $fromwhere AND $nottax AND ( tax != 'Y' OR tax IS NULL )"
-%#    );
-%#
-%#    $x_cust += scalar_sql($r, \@param, 
-%#      "SELECT SUM($e) $fromwhere AND $nottax AND tax = 'Y'"
-%#    );
-%#  }
-%
-%  my $x_cust = scalar_sql($r, \@param,
-%    "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur)
-%     $fromwhere AND $nottax AND tax = 'Y' "
-%  );
-%
-%  $exempt_cust += $x_cust;
-%  $regions{$label}->{'exempt_cust'} += $x_cust;
-%  
-%  ## calculate package-exemption for this region
-%
-%  my $x_pkg = scalar_sql($r, \@param,
-%    "SELECT SUM(
-%                 ( CASE WHEN part_pkg.setuptax = 'Y'
-%                        THEN cust_bill_pkg.setup
-%                        ELSE 0
-%                   END
-%                 )
-%                 +
-%                 ( CASE WHEN part_pkg.recurtax = 'Y'
-%                        THEN cust_bill_pkg.recur
-%                        ELSE 0
-%                   END
-%                 )
-%               )
-%       $fromwhere
-%       AND $nottax
-%       AND (
-%                ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
-%             OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 )
-%           )
-%       AND ( tax != 'Y' OR tax IS NULL )
-%    "
-%  );
-%  $exempt_pkg += $x_pkg;
-%  $regions{$label}->{'exempt_pkg'} += $x_pkg;
-%
-%  ## calculate monthly exemption (texas tax) for this region
-%
-%  # count up all the cust_tax_exempt_pkg records associated with
-%  # the actual line items.
-%
-%  my $x_monthly = scalar_sql($r, \@param,
-%    "SELECT SUM(amount)
-%       FROM cust_tax_exempt_pkg
-%       JOIN cust_bill_pkg USING ( billpkgnum )
-%       $join_cust $join_pkg
-%     $mywhere"
-%  );
-%#  if ( $x_monthly ) {
-%#    #warn $r->taxnum(). ": $x_monthly\n";
-%#    $taxable -= $x_monthly;
-%#  }
-%
-%  $exempt_monthly += $x_monthly;
-%  $regions{$label}->{'exempt_monthly'} += $x_monthly;
-%
-%  my $taxable = $t - $x_cust - $x_pkg - $x_monthly;
-%
-%  $tot_taxable += $taxable;
-%  $regions{$label}->{'taxable'} += $taxable;
-%
-%  $owed += $taxable * ($r->tax/100);
-%  $regions{$label}->{'owed'} += $taxable * ($r->tax/100);
-%
-%  if ( defined($regions{$label}->{'rate'})
-%       && $regions{$label}->{'rate'} != $r->tax.'%' ) {
-%    $regions{$label}->{'rate'} = 'variable';
-%  } else {
-%    $regions{$label}->{'rate'} = $r->tax.'%';
-%  }
-%
-%}
-%
-%my $taxwhere = "$from_join_cust $where AND payby != 'COMP' ";
-%my @taxparam = @base_param;
-%my %base_regions = ();
-%#foreach my $label ( keys %regions ) {
-%foreach my $r (
-%  qsearch( 'cust_main_county',
-%           {},
-%           'DISTINCT ON (country, state, county, taxname) *',
-%           $gotcust
-%         )
-%) {
-%
-%  #warn join('-', map { $r->$_() } qw( country state county taxname ) )."\n";
-%
-%  my $label = getlabel($r);
-%
-%  #my $fromwhere = $join_pkg. $where. " AND payby != 'COMP' ";
-%  #my @param = @base_param; 
-%
-%  #match itemdesc if necessary!
-%  my $named_tax =
-%    $r->taxname
-%      ? 'AND itemdesc = '. dbh->quote($r->taxname)
-%      : "AND ( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
-%  my $x = scalar_sql($r, \@taxparam,
-%    "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $taxwhere ".
-%    "AND pkgnum = 0 $named_tax",
-%  );
-%  $tax += $x;
-%  $regions{$label}->{'tax'} += $x;
-%
-%  if ( $cgi->param('show_taxclasses') ) {
-%    my $base_label = getlabel($r, 'no_taxclass'=>1 );
-%    $base_regions{$base_label}->{'label'} = $base_label;
-%    $base_regions{$base_label}->{'url_param'} =
-%      join(';', map "$_=".$r->$_(), qw( county state country ) );
-%    $base_regions{$base_label}->{'tax'} += $x;
-%  }
-%
-%}
-%
-%#ordering
-%my @regions =
-%  map $regions{$_},
-%  sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
-%  keys %regions;
-%
-%my @base_regions =
-%  map $base_regions{$_},
-%  sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
-%  keys %base_regions;
-%
-%push @regions, {
-%  'label'          => 'Total',
-%  'url_param'      => '',
-%  'total'          => $total,
-%  'exempt_cust'    => $exempt_cust,
-%  'exempt_pkg'     => $exempt_pkg,
-%  'exempt_monthly' => $exempt_monthly,
-%  'taxable'        => $tot_taxable,
-%  'rate'           => '',
-%  'owed'           => $owed,
-%  'tax'            => $tax,
-%};
-%
-%#-- 
-%
-%sub getlabel {
-%  my $r = shift;
-%  my %opt = @_;
-%
-%  my $label;
-%  if (
-%    $r->tax == 0 
-%    && ! scalar( qsearch('cust_main_county', { 'state'   => $r->state,
-%                                               'county'  => $r->county,
-%                                               'country' => $r->country,
-%                                               'tax' => { op=>'>', value=>0 },
-%                                             }
-%                        )
-%               )
-%
-%  ) {
-%    #kludge to avoid "will not stay shared" warning
-%    my $out = 'Out of taxable region(s)';
-%    $label = $out;
-%  } elsif ( $r->taxname ) {
-%    $label = $r->taxname;
-%#    $regions{$label}->{'taxname'} = $label;
-%#    push @{$regions{$label}->{$_}}, $r->$_() foreach qw( county state country );
-%  } else {
-%    $label = $r->country;
-%    $label = $r->state.", $label" if $r->state;
-%    $label = $r->county." county, $label" if $r->county;
-%    $label = "$label (". $r->taxclass. ")"
-%      if $r->taxclass
-%      && $cgi->param('show_taxclasses')
-%      && ! $opt{'no_taxclass'};
-%    #$label = $r->taxname. " ($label)" if $r->taxname;
-%  }
-%  return $label;
-%}
-%
-%#false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
-%#to FS::Report or FS::Record or who the fuck knows where)
-%sub scalar_sql {
-%  my( $r, $param, $sql ) = @_;
-%  #warn "$sql\n";
-%  my $sth = dbh->prepare($sql) or die dbh->errstr;
-%  $sth->execute( map $r->$_(), @$param )
-%    or die "Unexpected error executing statement $sql: ". $sth->errstr;
-%  $sth->fetchrow_arrayref->[0] || 0;
-%}
-%
-%
-%
-%my $dateagentlink = "begin=$beginning;end=$ending";
-%$dateagentlink .= ';agentnum='. $cgi->param('agentnum')
-%  if length($agentname);
-%my $baselink   = $p. "search/cust_bill_pkg.cgi?$dateagentlink";
-%my $exemptlink = $p. "search/cust_tax_exempt_pkg.cgi?$dateagentlink";
-%
-
-
 <% include("/elements/header.html", "$agentname Sales Tax Report - ".
               ( $beginning
                   ? time2str('%h %o %Y ', $beginning )
@@ -535,5 +164,376 @@
 
 </BODY>
 </HTML>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
+
+my $conf = new FS::Conf;
+my $money_char = $conf->config('money_char') || '$';
+
+my $user = getotaker;
+
+my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
+
+my $join_cust = "
+    JOIN cust_bill USING ( invnum ) 
+    LEFT JOIN cust_main USING ( custnum )
+";
+my $from_join_cust = "
+    FROM cust_bill_pkg
+    $join_cust
+"; 
+my $join_pkg = "
+    LEFT JOIN cust_pkg USING ( pkgnum )
+    LEFT JOIN part_pkg USING ( pkgpart )
+";
+
+my $where = "WHERE _date >= $beginning AND _date <= $ending ";
+my @base_param = qw( county county state state country );
+if ( $conf->exists('tax-ship_address') ) {
+
+  $where .= "
+      AND (    (     ( ship_last IS NULL     OR  ship_last  = '' )
+                 AND ( county       = ? OR ? = '' )
+                 AND ( state        = ? OR ? = '' )
+                 AND   country      = ?
+               )
+            OR (       ship_last IS NOT NULL AND ship_last != ''
+                 AND ( ship_county  = ? OR ? = '' )
+                 AND ( ship_state   = ? OR ? = '' )
+                 AND   ship_country = ?
+               )
+          )
+  ";
+  #    AND payby != 'COMP'
+
+  push @base_param, @base_param;
+
+} else {
+
+  $where .= "
+      AND ( county  = ? OR ? = '' )
+      AND ( state   = ? OR ? = '' )
+      AND   country = ?
+  ";
+  #    AND payby != 'COMP'
+
+}
+
+my $agentname = '';
+if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
+  my $agent = qsearchs('agent', { 'agentnum' => $1 } );
+  die "agent not found" unless $agent;
+  $agentname = $agent->agent;
+  $where .= ' AND agentnum = '. $agent->agentnum;
+}
+
+my $gotcust = "
+  WHERE 0 < ( SELECT COUNT(*) FROM cust_main
+";
+if ( $conf->exists('tax-ship_address') ) {
+
+  $gotcust .= "
+                WHERE
+
+                (    cust_main_county.country = cust_main.country
+                  OR cust_main_county.country = cust_main.ship_country
+                )
+
+                AND
+
+                ( 
+
+                  (     ( ship_last IS NULL     OR  ship_last = '' )
+                    AND (    cust_main_county.country = cust_main.country )
+                    AND (    cust_main_county.state = cust_main.state
+                          OR cust_main_county.state = ''
+                          OR cust_main_county.state IS NULL )
+                    AND (    cust_main_county.county = cust_main.county
+                          OR cust_main_county.county = ''
+                          OR cust_main_county.county IS NULL )
+                  )
+  
+                  OR
+  
+                  (       ship_last IS NOT NULL AND ship_last != ''
+                    AND (    cust_main_county.country = cust_main.ship_country )
+                    AND (    cust_main_county.state = cust_main.ship_state
+                          OR cust_main_county.state = ''
+                          OR cust_main_county.state IS NULL )
+                    AND (    cust_main_county.county = cust_main.ship_county
+                          OR cust_main_county.county = ''
+                          OR cust_main_county.county IS NULL )
+                  )
+
+                )
+
+                LIMIT 1
+            )
+  ";
 
+} else {
 
+  $gotcust .= "
+                WHERE ( cust_main.county  = cust_main_county.county
+                        OR cust_main_county.county = ''
+                        OR cust_main_county.county IS NULL )
+                  AND ( cust_main.state   = cust_main_county.state
+                        OR cust_main_county.state = ''
+                        OR cust_main_county.state IS NULL )
+                  AND ( cust_main.country = cust_main_county.country )
+                LIMIT 1
+            )
+  ";
+
+}
+
+my($total, $tot_taxable, $owed, $tax) = ( 0, 0, 0, 0, 0 );
+my( $exempt_cust, $exempt_pkg, $exempt_monthly ) = ( 0, 0 );
+my $out = 'Out of taxable region(s)';
+my %regions = ();
+foreach my $r (qsearch('cust_main_county', {}, '', $gotcust) ) {
+  #warn $r->county. ' '. $r->state. ' '. $r->country. "\n";
+
+  my $label = getlabel($r);
+  $regions{$label}->{'label'} = $label;
+  $regions{$label}->{'url_param'} = join(';', map "$_=".$r->$_(), qw( county state country ) );
+
+  my @param = @base_param;
+  my $mywhere = $where;
+
+  if ( $r->taxclass ) {
+    $mywhere .= " AND taxclass = ? ";
+    push @param, 'taxclass';
+    $regions{$label}->{'url_param'} .= ';taxclass='. $r->taxclass
+      if $cgi->param('show_taxclasses');
+  }
+
+  my $fromwhere = $from_join_cust. $join_pkg. $mywhere. " AND payby != 'COMP' ";
+
+#  my $label = getlabel($r);
+#  $regions{$label}->{'label'} = $label;
+
+  my $nottax = 'pkgnum != 0';
+
+  ## calculate total for this region
+
+  my $t = scalar_sql($r, \@param,
+    "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $fromwhere AND $nottax"
+  );
+  $total += $t;
+  $regions{$label}->{'total'} += $t;
+
+  ## calculate customer-exemption for this region
+
+##  my $taxable = $t;
+
+#  my($taxable, $x_cust) = (0, 0);
+#  foreach my $e ( grep { $r->get($_.'tax') !~ /^Y/i }
+#                       qw( cust_bill_pkg.setup cust_bill_pkg.recur ) ) {
+#    $taxable += scalar_sql($r, \@param, 
+#      "SELECT SUM($e) $fromwhere AND $nottax AND ( tax != 'Y' OR tax IS NULL )"
+#    );
+#
+#    $x_cust += scalar_sql($r, \@param, 
+#      "SELECT SUM($e) $fromwhere AND $nottax AND tax = 'Y'"
+#    );
+#  }
+
+  my $x_cust = scalar_sql($r, \@param,
+    "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur)
+     $fromwhere AND $nottax AND tax = 'Y' "
+  );
+
+  $exempt_cust += $x_cust;
+  $regions{$label}->{'exempt_cust'} += $x_cust;
+  
+  ## calculate package-exemption for this region
+
+  my $x_pkg = scalar_sql($r, \@param,
+    "SELECT SUM(
+                 ( CASE WHEN part_pkg.setuptax = 'Y'
+                        THEN cust_bill_pkg.setup
+                        ELSE 0
+                   END
+                 )
+                 +
+                 ( CASE WHEN part_pkg.recurtax = 'Y'
+                        THEN cust_bill_pkg.recur
+                        ELSE 0
+                   END
+                 )
+               )
+       $fromwhere
+       AND $nottax
+       AND (
+                ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
+             OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 )
+           )
+       AND ( tax != 'Y' OR tax IS NULL )
+    "
+  );
+  $exempt_pkg += $x_pkg;
+  $regions{$label}->{'exempt_pkg'} += $x_pkg;
+
+  ## calculate monthly exemption (texas tax) for this region
+
+  # count up all the cust_tax_exempt_pkg records associated with
+  # the actual line items.
+
+  my $x_monthly = scalar_sql($r, \@param,
+    "SELECT SUM(amount)
+       FROM cust_tax_exempt_pkg
+       JOIN cust_bill_pkg USING ( billpkgnum )
+       $join_cust $join_pkg
+     $mywhere"
+  );
+#  if ( $x_monthly ) {
+#    #warn $r->taxnum(). ": $x_monthly\n";
+#    $taxable -= $x_monthly;
+#  }
+
+  $exempt_monthly += $x_monthly;
+  $regions{$label}->{'exempt_monthly'} += $x_monthly;
+
+  my $taxable = $t - $x_cust - $x_pkg - $x_monthly;
+
+  $tot_taxable += $taxable;
+  $regions{$label}->{'taxable'} += $taxable;
+
+  $owed += $taxable * ($r->tax/100);
+  $regions{$label}->{'owed'} += $taxable * ($r->tax/100);
+
+  if ( defined($regions{$label}->{'rate'})
+       && $regions{$label}->{'rate'} != $r->tax.'%' ) {
+    $regions{$label}->{'rate'} = 'variable';
+  } else {
+    $regions{$label}->{'rate'} = $r->tax.'%';
+  }
+
+}
+
+my $taxwhere = "$from_join_cust $where AND payby != 'COMP' ";
+my @taxparam = @base_param;
+my %base_regions = ();
+#foreach my $label ( keys %regions ) {
+foreach my $r (
+  qsearch( 'cust_main_county',
+           {},
+           'DISTINCT ON (country, state, county, taxname) *',
+           $gotcust
+         )
+) {
+
+  #warn join('-', map { $r->$_() } qw( country state county taxname ) )."\n";
+
+  my $label = getlabel($r);
+
+  #my $fromwhere = $join_pkg. $where. " AND payby != 'COMP' ";
+  #my @param = @base_param; 
+
+  #match itemdesc if necessary!
+  my $named_tax =
+    $r->taxname
+      ? 'AND itemdesc = '. dbh->quote($r->taxname)
+      : "AND ( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
+  my $x = scalar_sql($r, \@taxparam,
+    "SELECT SUM(cust_bill_pkg.setup+cust_bill_pkg.recur) $taxwhere ".
+    "AND pkgnum = 0 $named_tax",
+  );
+  $tax += $x;
+  $regions{$label}->{'tax'} += $x;
+
+  if ( $cgi->param('show_taxclasses') ) {
+    my $base_label = getlabel($r, 'no_taxclass'=>1 );
+    $base_regions{$base_label}->{'label'} = $base_label;
+    $base_regions{$base_label}->{'url_param'} =
+      join(';', map "$_=".$r->$_(), qw( county state country ) );
+    $base_regions{$base_label}->{'tax'} += $x;
+  }
+
+}
+
+#ordering
+my @regions =
+  map $regions{$_},
+  sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
+  keys %regions;
+
+my @base_regions =
+  map $base_regions{$_},
+  sort { ( ($a eq $out) cmp ($b eq $out) ) || ($b cmp $a) }
+  keys %base_regions;
+
+push @regions, {
+  'label'          => 'Total',
+  'url_param'      => '',
+  'total'          => $total,
+  'exempt_cust'    => $exempt_cust,
+  'exempt_pkg'     => $exempt_pkg,
+  'exempt_monthly' => $exempt_monthly,
+  'taxable'        => $tot_taxable,
+  'rate'           => '',
+  'owed'           => $owed,
+  'tax'            => $tax,
+};
+
+#-- 
+
+sub getlabel {
+  my $r = shift;
+  my %opt = @_;
+
+  my $label;
+  if (
+    $r->tax == 0 
+    && ! scalar( qsearch('cust_main_county', { 'state'   => $r->state,
+                                               'county'  => $r->county,
+                                               'country' => $r->country,
+                                               'tax' => { op=>'>', value=>0 },
+                                             }
+                        )
+               )
+
+  ) {
+    #kludge to avoid "will not stay shared" warning
+    my $out = 'Out of taxable region(s)';
+    $label = $out;
+  } elsif ( $r->taxname ) {
+    $label = $r->taxname;
+#    $regions{$label}->{'taxname'} = $label;
+#    push @{$regions{$label}->{$_}}, $r->$_() foreach qw( county state country );
+  } else {
+    $label = $r->country;
+    $label = $r->state.", $label" if $r->state;
+    $label = $r->county." county, $label" if $r->county;
+    $label = "$label (". $r->taxclass. ")"
+      if $r->taxclass
+      && $cgi->param('show_taxclasses')
+      && ! $opt{'no_taxclass'};
+    #$label = $r->taxname. " ($label)" if $r->taxname;
+  }
+  return $label;
+}
+
+#false laziness w/FS::Report::Table::Monthly (sub should probably be moved up
+#to FS::Report or FS::Record or who the fuck knows where)
+sub scalar_sql {
+  my( $r, $param, $sql ) = @_;
+  #warn "$sql\n";
+  my $sth = dbh->prepare($sql) or die dbh->errstr;
+  $sth->execute( map $r->$_(), @$param )
+    or die "Unexpected error executing statement $sql: ". $sth->errstr;
+  $sth->fetchrow_arrayref->[0] || 0;
+}
+
+
+
+my $dateagentlink = "begin=$beginning;end=$ending";
+$dateagentlink .= ';agentnum='. $cgi->param('agentnum')
+  if length($agentname);
+my $baselink   = $p. "search/cust_bill_pkg.cgi?$dateagentlink";
+my $exemptlink = $p. "search/cust_tax_exempt_pkg.cgi?$dateagentlink";
+
+</%init>

Index: pay_batch.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/pay_batch.html,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- pay_batch.html	26 Aug 2006 23:15:14 -0000	1.1
+++ pay_batch.html	3 Feb 2007 11:36:30 -0000	1.2
@@ -25,3 +25,9 @@
 </FORM>
 
 <% include('/elements/footer.html') %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
+
+</%init>

Index: report_cdr.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/report_cdr.html,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- report_cdr.html	23 Aug 2006 22:25:38 -0000	1.4
+++ report_cdr.html	3 Feb 2007 11:36:30 -0000	1.5
@@ -9,4 +9,9 @@
 <INPUT TYPE="submit" VALUE="Search Call Detail Records">
 
 <% include('/elements/footer.html') %>
+<%init>
 
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
+
+</%init>

Index: report_cust_pay.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/report_cust_pay.html,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- report_cust_pay.html	24 Nov 2006 08:48:52 -0000	1.14
+++ report_cust_pay.html	3 Feb 2007 11:36:30 -0000	1.15
@@ -70,3 +70,9 @@
 </FORM>
 
 <% include('/elements/footer.html') %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
+
+</%init>

Index: svc_external.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/svc_external.cgi,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- svc_external.cgi	29 Dec 2006 08:51:33 -0000	1.5
+++ svc_external.cgi	3 Feb 2007 11:36:30 -0000	1.6
@@ -1,3 +1,6 @@
+%die "access denied"
+%  unless $FS::CurrentUser::CurrentUser->access_right('List services');
+%
 %my $conf = new FS::Conf;
 %
 %my @svc_external = ();

Index: cust_credit.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/cust_credit.html,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- cust_credit.html	24 Nov 2006 08:48:52 -0000	1.11
+++ cust_credit.html	3 Feb 2007 11:36:30 -0000	1.12
@@ -1,55 +1,3 @@
-%
-%   my $title = 'Credit Search Results';
-%   #my( $count_query, $sql_query );
-%
-%   my @search = ();
-%
-%   if ( $cgi->param('otaker') && $cgi->param('otaker') =~ /^([\w\.\-]+)$/ ) {
-%     push @search, "cust_credit.otaker = '$1'";
-%   }
-%
-%   if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
-%     push @search, "agentnum = $1";
-%     my $agent = qsearchs('agent', { 'agentnum' => $1 } );
-%     die "unknown agentnum $1" unless $agent;
-%     $title = $agent->agent. " $title";
-%   }
-%
-%   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
-%   push @search, "_date >= $beginning ",
-%                 "_date <= $ending";
-%
-%   push @search, FS::UI::Web::parse_lt_gt($cgi, 'amount' );
-%
-%   #here is the agent virtualization
-%   push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
-%
-%   my $where = 'WHERE '. join(' AND ', @search);
-%
-%   my $count_query = 'SELECT COUNT(*), SUM(amount) '.
-%                     'FROM cust_credit LEFT JOIN cust_main USING ( custnum ) '.
-%                     $where;
-%
-%   my $sql_query   = {
-%     'table'     => 'cust_credit',
-%     'select'    => join(', ',
-%                      'cust_credit.*',
-%                      'cust_main.custnum as cust_main_custnum',
-%                      FS::UI::Web::cust_sql_fields(),
-%                    ),
-%     'hashref'   => {},
-%     'extra_sql' => $where,
-%     'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
-%   };
-%
-%     my $clink = sub {
-%       my $cust_bill = shift;
-%       $cust_bill->cust_main_custnum
-%         ? [ "${p}view/cust_main.cgi?", 'custnum' ]
-%         : '';
-%     };
-%
-%
 <% include( 'elements/search.html',
                  'title'       => $title,
                  'name'        => 'credits',
@@ -72,13 +20,85 @@
                    'reason',
                  ],
                  #'align' => 'rrrllll',
-                 'align' => 'rr',
+                 'align' => 'rr'.FS::UI::Web::cust_aligns().'ll',
                  'links' => [
                    '',
                    '',
-                   ( map { $clink } FS::UI::Web::cust_header() ),
+                   ( map { $_ ne 'Cust. Status' ? $clink : '' }
+                         FS::UI::Web::cust_header()
+                   ),
                    '',
                    '',
                  ],
+                 'color' => [ 
+                              '',
+                              '',
+                              FS::UI::Web::cust_colors(),
+                              '',
+                              '',
+                            ],
+                 'style' => [ 
+                              '',
+                              '',
+                              FS::UI::Web::cust_styles(),
+                              '',
+                              '',
+                            ],
       )
 %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
+
+my $title = 'Credit Search Results';
+#my( $count_query, $sql_query );
+
+my @search = ();
+
+if ( $cgi->param('otaker') && $cgi->param('otaker') =~ /^([\w\.\-]+)$/ ) {
+  push @search, "cust_credit.otaker = '$1'";
+}
+
+if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
+  push @search, "agentnum = $1";
+  my $agent = qsearchs('agent', { 'agentnum' => $1 } );
+  die "unknown agentnum $1" unless $agent;
+  $title = $agent->agent. " $title";
+}
+
+my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
+push @search, "_date >= $beginning ",
+              "_date <= $ending";
+
+push @search, FS::UI::Web::parse_lt_gt($cgi, 'amount' );
+
+#here is the agent virtualization
+push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
+
+my $where = 'WHERE '. join(' AND ', @search);
+
+my $count_query = 'SELECT COUNT(*), SUM(amount) '.
+                  'FROM cust_credit LEFT JOIN cust_main USING ( custnum ) '.
+                  $where;
+
+my $sql_query   = {
+  'table'     => 'cust_credit',
+  'select'    => join(', ',
+                   'cust_credit.*',
+                   'cust_main.custnum as cust_main_custnum',
+                   FS::UI::Web::cust_sql_fields(),
+                 ),
+  'hashref'   => {},
+  'extra_sql' => $where,
+  'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
+};
+
+  my $clink = sub {
+    my $cust_bill = shift;
+    $cust_bill->cust_main_custnum
+      ? [ "${p}view/cust_main.cgi?", 'custnum' ]
+      : '';
+  };
+
+</%init>

Index: cust_bill_pkg.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/cust_bill_pkg.cgi,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- cust_bill_pkg.cgi	23 Aug 2006 22:25:38 -0000	1.6
+++ cust_bill_pkg.cgi	3 Feb 2007 11:36:30 -0000	1.7
@@ -1,130 +1,3 @@
-%
-%
-%my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
-%
-%my $join_cust = "
-%    JOIN cust_bill USING ( invnum ) 
-%    LEFT JOIN cust_main USING ( custnum )
-%";
-%
-%my $join_pkg = "
-%    LEFT JOIN cust_pkg USING ( pkgnum )
-%    LEFT JOIN part_pkg USING ( pkgpart )
-%";
-%
-%my $where = " WHERE _date >= $beginning AND _date <= $ending ";
-%
-%$where .= " AND payby != 'COMP' "
-%  unless $cgi->param('include_comp_cust');
-%
-%if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
-%  $where .= " AND agentnum = $1 ";
-%}
-%
-%if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
-%  if ( $1 == 0 ) {
-%    $where .= " AND classnum IS NULL ";
-%  } else {
-%    $where .= " AND classnum = $1 ";
-%  }
-%}
-%
-%if ( $cgi->param('out') ) {
-%
-%  $where .= "
-%    AND 0 = (
-%      SELECT COUNT(*) FROM cust_main_county
-%      WHERE (    cust_main_county.county  = cust_main.county
-%              OR ( cust_main_county.county IS NULL AND cust_main.county  =  '' )
-%              OR ( cust_main_county.county  =  ''  AND cust_main.county IS NULL)
-%              OR ( cust_main_county.county IS NULL AND cust_main.county IS NULL)
-%            )
-%        AND (    cust_main_county.state   = cust_main.state
-%              OR ( cust_main_county.state  IS NULL AND cust_main.state  =  ''  )
-%              OR ( cust_main_county.state   =  ''  AND cust_main.state IS NULL )
-%              OR ( cust_main_county.state  IS NULL AND cust_main.state IS NULL )
-%            )
-%        AND cust_main_county.country = cust_main.country
-%        AND cust_main_county.tax > 0
-%    )
-%  ";
-%
-%} elsif ( $cgi->param('country' ) ) {
-%
-%  my $county  = dbh->quote( $cgi->param('county')  );
-%  my $state   = dbh->quote( $cgi->param('state')   );
-%  my $country = dbh->quote( $cgi->param('country') );
-%  $where .= "
-%    AND ( county  = $county OR $county = '' )
-%    AND ( state   = $state  OR $state = '' )
-%    AND   country = $country
-%  ";
-%  $where .= ' AND taxclass = '. dbh->quote( $cgi->param('taxclass') )
-%    if $cgi->param('taxclass');
-%
-%}
-%
-%$where .= ' AND pkgnum != 0' if $cgi->param('nottax');
-%
-%$where .= ' AND pkgnum = 0' if $cgi->param('istax');
-%
-%$where .= " AND tax = 'Y'" if $cgi->param('cust_tax');
-%
-%my $count_query;
-%if ( $cgi->param('pkg_tax') ) {
-%
-%  $count_query =
-%    "SELECT COUNT(*), SUM(
-%                           ( CASE WHEN part_pkg.setuptax = 'Y'
-%                                  THEN cust_bill_pkg.setup
-%                                  ELSE 0
-%                             END
-%                           )
-%                           +
-%                           ( CASE WHEN part_pkg.recurtax = 'Y'
-%                                  THEN cust_bill_pkg.recur
-%                                  ELSE 0
-%                             END
-%                           )
-%                         )
-%    ";
-%
-%  $where .= " AND (
-%                       ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
-%                    OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 )
-%                  )
-%              AND ( tax != 'Y' OR tax IS NULL )
-%            ";
-%
-%} else {
-%
-%  $count_query =
-%    "SELECT COUNT(*), SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)";
-%
-%}
-%$count_query .= " FROM cust_bill_pkg $join_cust  $join_pkg $where";
-%
-%my $query = {
-%  'table'     => 'cust_bill_pkg',
-%  'addl_from' => "$join_cust $join_pkg",
-%  'hashref'   => {},
-%  'select'    => join(', ',
-%                   'cust_bill_pkg.*',
-%                   'cust_bill._date',
-%                   'part_pkg.pkg',
-%                   'cust_main.custnum',
-%                   FS::UI::Web::cust_sql_fields(),
-%                 ),
-%  'extra_sql' => $where,
-%};
-%
-%my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
-%my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
-%
-%my $conf = new FS::Conf;
-%my $money_char = $conf->config('money_char') || '$';
-%
-%
 <% include( 'elements/search.html',
                  'title'       => 'Line items',
                  'name'        => 'line items',
@@ -161,9 +34,158 @@
                    '',
                    $ilink,
                    $ilink,
-                   ( map { $clink } FS::UI::Web::cust_header() ),
+                   ( map { $_ ne 'Cust. Status' ? $clink : '' }
+                         FS::UI::Web::cust_header()
+                   ),
                  ],
-                 'align' => 'rlrrrc',
+                 'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(),
+                 'color' => [ 
+                              '',
+                              '',
+                              '',
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_colors(),
+                            ],
+                 'style' => [ 
+                              '',
+                              '',
+                              '',
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_styles(),
+                            ],
            )
 %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
+
+my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
+
+my $join_cust = "
+    JOIN cust_bill USING ( invnum ) 
+    LEFT JOIN cust_main USING ( custnum )
+";
+
+my $join_pkg = "
+    LEFT JOIN cust_pkg USING ( pkgnum )
+    LEFT JOIN part_pkg USING ( pkgpart )
+";
+
+my $where = " WHERE _date >= $beginning AND _date <= $ending ";
+
+$where .= " AND payby != 'COMP' "
+  unless $cgi->param('include_comp_cust');
+
+if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
+  $where .= " AND agentnum = $1 ";
+}
+
+if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
+  if ( $1 == 0 ) {
+    $where .= " AND classnum IS NULL ";
+  } else {
+    $where .= " AND classnum = $1 ";
+  }
+}
+
+if ( $cgi->param('out') ) {
+
+  $where .= "
+    AND 0 = (
+      SELECT COUNT(*) FROM cust_main_county
+      WHERE (    cust_main_county.county  = cust_main.county
+              OR ( cust_main_county.county IS NULL AND cust_main.county  =  '' )
+              OR ( cust_main_county.county  =  ''  AND cust_main.county IS NULL)
+              OR ( cust_main_county.county IS NULL AND cust_main.county IS NULL)
+            )
+        AND (    cust_main_county.state   = cust_main.state
+              OR ( cust_main_county.state  IS NULL AND cust_main.state  =  ''  )
+              OR ( cust_main_county.state   =  ''  AND cust_main.state IS NULL )
+              OR ( cust_main_county.state  IS NULL AND cust_main.state IS NULL )
+            )
+        AND cust_main_county.country = cust_main.country
+        AND cust_main_county.tax > 0
+    )
+  ";
+
+} elsif ( $cgi->param('country' ) ) {
+
+  my $county  = dbh->quote( $cgi->param('county')  );
+  my $state   = dbh->quote( $cgi->param('state')   );
+  my $country = dbh->quote( $cgi->param('country') );
+  $where .= "
+    AND ( county  = $county OR $county = '' )
+    AND ( state   = $state  OR $state = '' )
+    AND   country = $country
+  ";
+  $where .= ' AND taxclass = '. dbh->quote( $cgi->param('taxclass') )
+    if $cgi->param('taxclass');
+
+}
+
+$where .= ' AND pkgnum != 0' if $cgi->param('nottax');
+
+$where .= ' AND pkgnum = 0' if $cgi->param('istax');
+
+$where .= " AND tax = 'Y'" if $cgi->param('cust_tax');
+
+my $count_query;
+if ( $cgi->param('pkg_tax') ) {
+
+  $count_query =
+    "SELECT COUNT(*), SUM(
+                           ( CASE WHEN part_pkg.setuptax = 'Y'
+                                  THEN cust_bill_pkg.setup
+                                  ELSE 0
+                             END
+                           )
+                           +
+                           ( CASE WHEN part_pkg.recurtax = 'Y'
+                                  THEN cust_bill_pkg.recur
+                                  ELSE 0
+                             END
+                           )
+                         )
+    ";
+
+  $where .= " AND (
+                       ( part_pkg.setuptax = 'Y' AND cust_bill_pkg.setup > 0 )
+                    OR ( part_pkg.recurtax = 'Y' AND cust_bill_pkg.recur > 0 )
+                  )
+              AND ( tax != 'Y' OR tax IS NULL )
+            ";
+
+} else {
+
+  $count_query =
+    "SELECT COUNT(*), SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)";
+
+}
+$count_query .= " FROM cust_bill_pkg $join_cust  $join_pkg $where";
 
+my $query = {
+  'table'     => 'cust_bill_pkg',
+  'addl_from' => "$join_cust $join_pkg",
+  'hashref'   => {},
+  'select'    => join(', ',
+                   'cust_bill_pkg.*',
+                   'cust_bill._date',
+                   'part_pkg.pkg',
+                   'cust_main.custnum',
+                   FS::UI::Web::cust_sql_fields(),
+                 ),
+  'extra_sql' => $where,
+};
+
+my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
+my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
+
+my $conf = new FS::Conf;
+my $money_char = $conf->config('money_char') || '$';
+
+</%init>

Index: svc_broadband.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/svc_broadband.cgi,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- svc_broadband.cgi	29 Dec 2006 08:51:33 -0000	1.3
+++ svc_broadband.cgi	3 Feb 2007 11:36:30 -0000	1.4
@@ -1,3 +1,6 @@
+%die "access denied"
+%  unless $FS::CurrentUser::CurrentUser->access_right('List services');
+%
 %my $conf = new FS::Conf;
 %
 %my @svc_broadband = ();

Index: report_receivables.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/report_receivables.html,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- report_receivables.html	23 Aug 2006 22:25:38 -0000	1.2
+++ report_receivables.html	3 Feb 2007 11:36:30 -0000	1.3
@@ -13,4 +13,9 @@
 
   </BODY>
 </HTML>
+<%init>
 
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
+
+</%init>

Index: sqlradius.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/sqlradius.cgi,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -d -r1.16 -r1.17
--- sqlradius.cgi	4 Sep 2006 20:31:22 -0000	1.16
+++ sqlradius.cgi	3 Feb 2007 11:36:30 -0000	1.17
@@ -84,8 +84,11 @@
 
 % } 
 
-
 <%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List rating data');
+
 ###
 # parse cgi params
 ###

Index: report_prepaid_income.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/report_prepaid_income.html,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- report_prepaid_income.html	23 Aug 2006 22:25:38 -0000	1.5
+++ report_prepaid_income.html	3 Feb 2007 11:36:30 -0000	1.6
@@ -35,3 +35,9 @@
 <INPUT TYPE="submit" VALUE="Generate report">
 
 <% include('/elements/footer.html') %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
+
+</%init>

Index: cust_bill_event.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/cust_bill_event.cgi,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- cust_bill_event.cgi	2 Sep 2006 20:38:41 -0000	1.11
+++ cust_bill_event.cgi	3 Feb 2007 11:36:30 -0000	1.12
@@ -1,5 +1,72 @@
+<% include( 'elements/search.html',
+                 'title'       => $title,
+                 'html_init'   => $html_init,
+                 'menubar'     => $menubar,
+                 'name'        => 'billing events',
+                 'query'       => $sql_query,
+                 'count_query' => $count_sql,
+                 'header'      => [ 'Event',
+                                    'Date',
+                                    'Status',
+                                    #'Inv #', 'Inv Date', 'Cust #',
+                                    'Invoice',
+                                    FS::UI::Web::cust_header(),
+                                  ],
+                 'fields' => [
+                               'event',
+                               sub { time2str("%b %d %Y %T", $_[0]->_date) },
+                               sub { 
+                                     #my $cust_bill_event = shift;
+                                     my $status = $_[0]->status;
+                                     $status .= ': '.$_[0]->statustext
+                                       if $_[0]->statustext;
+                                     $status;
+                                   },
+                               sub {
+                                     #my $cust_bill_event = shift;
+                                     'Invoice #'. $_[0]->invnum.
+                                     ' ('.
+                                       time2str("%D", $_[0]->cust_bill_date).
+                                     ')';
+                                   },
+                               \&FS::UI::Web::cust_fields,
+                             ],
+                'align' => 'lrlr'.FS::UI::Web::cust_aligns(),
+                'links' => [
+                              '',
+                              '',
+                              '',
+                              sub {
+                                my $part_bill_event = shift;
+                                my $template = $part_bill_event->templatename;
+                                $template .= '-' if $template;
+                                [ "${p}view/cust_bill.cgi?$template", 'invnum'];
+                              },
+                              ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
+                                    FS::UI::Web::cust_header()
+                              ),
+                            ],
+                 'color' => [ 
+                              '',
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_colors(),
+                            ],
+                 'style' => [ 
+                              '',
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_styles(),
+                            ],
+             )
+%>
 <%init>
 
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Billing event reports');
+
 my $title = $cgi->param('failed')
               ? 'Failed invoice events'
               : 'Invoice events';
@@ -92,50 +159,3 @@
 };
 
 </%init>
-<% include( 'elements/search.html',
-                 'title'       => $title,
-                 'html_init'   => $html_init,
-                 'menubar'     => $menubar,
-                 'name'        => 'billing events',
-                 'query'       => $sql_query,
-                 'count_query' => $count_sql,
-                 'header'      => [ 'Event',
-                                    'Date',
-                                    'Status',
-                                    #'Inv #', 'Inv Date', 'Cust #',
-                                    'Invoice',
-                                    FS::UI::Web::cust_header(),
-                                  ],
-                 'fields' => [
-                               'event',
-                               sub { time2str("%b %d %Y %T", $_[0]->_date) },
-                               sub { 
-                                     #my $cust_bill_event = shift;
-                                     my $status = $_[0]->status;
-                                     $status .= ': '.$_[0]->statustext
-                                       if $_[0]->statustext;
-                                     $status;
-                                   },
-                               sub {
-                                     #my $cust_bill_event = shift;
-                                     'Invoice #'. $_[0]->invnum.
-                                     ' ('.
-                                       time2str("%D", $_[0]->cust_bill_date).
-                                     ')';
-                                   },
-                               \&FS::UI::Web::cust_fields,
-                             ],
-                 'links' => [
-                              '',
-                              '',
-                              '',
-                              sub {
-                                my $part_bill_event = shift;
-                                my $template = $part_bill_event->templatename;
-                                $template .= '-' if $template;
-                                [ "${p}view/cust_bill.cgi?$template", 'invnum'];
-                              },
-                              ( map { $link_cust } FS::UI::Web::cust_header() ),
-                            ],
-             )
-%>

Index: report_cust_credit.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/report_cust_credit.html,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- report_cust_credit.html	24 Nov 2006 08:48:52 -0000	1.9
+++ report_cust_credit.html	3 Feb 2007 11:36:30 -0000	1.10
@@ -42,6 +42,9 @@
 
 <%init>
 
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
+
 my $sth = dbh->prepare("SELECT DISTINCT otaker FROM cust_credit")
   or die dbh->errstr;
 $sth->execute or die $sth->errstr;

Index: cust_pay.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/cust_pay.cgi,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -d -r1.29 -r1.30
--- cust_pay.cgi	19 Dec 2006 10:02:06 -0000	1.29
+++ cust_pay.cgi	3 Feb 2007 11:36:30 -0000	1.30
@@ -1,167 +1,3 @@
-%
-%   my $title = 'Payment Search Results';
-%   my( $count_query, $sql_query );
-%   if ( $cgi->param('magic') ) {
-%
-%     my @search = ();
-%     my $orderby;
-%     if ( $cgi->param('magic') eq '_date' ) {
-%   
-%  
-%       if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
-%         push @search, "agentnum = $1"; # $search{'agentnum'} = $1;
-%         my $agent = qsearchs('agent', { 'agentnum' => $1 } );
-%         die "unknown agentnum $1" unless $agent;
-%         $title = $agent->agent. " $title";
-%       }
-%     
-%       if ( $cgi->param('payby') ) {
-%         $cgi->param('payby') =~
-%           /^(CARD|CHEK|BILL|PREP|CASH|WEST|MCRD)(-(VisaMC|Amex|Discover|Maestro))?$/
-%             or die "illegal payby ". $cgi->param('payby');
-%         push @search, "cust_pay.payby = '$1'";
-%         if ( $3 ) {
-%
-%           my $cardtype = $3;
-%
-%           my $search;
-%           if ( $cardtype eq 'VisaMC' ) {
-%             #avoid posix regexes for portability
-%             $search =
-%               " ( (     substring(cust_pay.payinfo from 1 for 1) = '4'     ".
-%               "     AND substring(cust_pay.payinfo from 1 for 4) != '4936' ".
-%               "     AND substring(cust_pay.payinfo from 1 for 6)           ".
-%               "         NOT SIMILAR TO '49030[2-9]'                        ".
-%               "     AND substring(cust_pay.payinfo from 1 for 6)           ".
-%               "         NOT SIMILAR TO '49033[5-9]'                        ".
-%               "     AND substring(cust_pay.payinfo from 1 for 6)           ".
-%               "         NOT SIMILAR TO '49110[1-2]'                        ".
-%               "     AND substring(cust_pay.payinfo from 1 for 6)           ".
-%               "         NOT SIMILAR TO '49117[4-9]'                        ".
-%               "     AND substring(cust_pay.payinfo from 1 for 6)           ".
-%               "         NOT SIMILAR TO '49118[1-2]'                        ".
-%               "   )".
-%               "   OR substring(cust_pay.payinfo from 1 for 2) = '51' ".
-%               "   OR substring(cust_pay.payinfo from 1 for 2) = '52' ".
-%               "   OR substring(cust_pay.payinfo from 1 for 2) = '53' ".
-%               "   OR substring(cust_pay.payinfo from 1 for 2) = '54' ".
-%               "   OR substring(cust_pay.payinfo from 1 for 2) = '54' ".
-%               "   OR substring(cust_pay.payinfo from 1 for 2) = '55' ".
-%               "   OR substring(cust_pay.payinfo from 1 for 2) = '36' ". #Diner's int'l processed as Visa/MC inside US
-%               " ) ";
-%           } elsif ( $cardtype eq 'Amex' ) {
-%             $search =
-%               " (    substring(cust_pay.payinfo from 1 for 2 ) = '34' ".
-%               "   OR substring(cust_pay.payinfo from 1 for 2 ) = '37' ".
-%               " ) ";
-%           } elsif ( $cardtype eq 'Discover' ) {
-%             $search =
-%               " (    substring(cust_pay.payinfo from 1 for 4 ) = '6011'  ".
-%               "   OR substring(cust_pay.payinfo from 1 for 2 ) = '65'    ".
-%               "   OR substring(cust_pay.payinfo from 1 for 3 ) = '622'   ". #China Union Pay processed as Discover outside CN
-%               " ) ";
-%           } elsif ( $cardtype eq 'Maestro' ) { 
-%             $search =
-%               " (    substring(cust_pay.payinfo from 1 for 2 ) = '63'     ".
-%               "   OR substring(cust_pay.payinfo from 1 for 2 ) = '67'     ".
-%               "   OR substring(cust_pay.payinfo from 1 for 6 ) = '564182' ".
-%               "   OR substring(cust_pay.payinfo from 1 for 4 ) = '4936'   ".
-%               "   OR substring(cust_pay.payinfo from 1 for 6 )            ".
-%               "      SIMILAR TO '49030[2-9]'                             ".
-%               "   OR substring(cust_pay.payinfo from 1 for 6 )            ".
-%               "      SIMILAR TO '49033[5-9]'                             ".
-%               "   OR substring(cust_pay.payinfo from 1 for 6 )            ".
-%               "      SIMILAR TO '49110[1-2]'                             ".
-%               "   OR substring(cust_pay.payinfo from 1 for 6 )            ".
-%               "      SIMILAR TO '49117[4-9]'                             ".
-%               "   OR substring(cust_pay.payinfo from 1 for 6 )            ".
-%               "      SIMILAR TO '49118[1-2]'                             ".
-%               " ) ";
-%           } else {
-%             die "unknown card type $cardtype";
-%           }
-%
-%           my $masksearch = $search;
-%           $masksearch =~ s/cust_pay\.payinfo/cust_pay.paymask/gi;
-%
-%           push @search,
-%             "( $search OR ( cust_pay.paymask IS NOT NULL AND $masksearch ) )";
-%
-%         }
-%       }
-%  
-%       my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
-%       push @search, "_date >= $beginning ",
-%                     "_date <= $ending";
-%
-%       push @search, FS::UI::Web::parse_lt_gt($cgi, 'paid' );
-%  
-%       $orderby = '_date';
-%   
-%     } elsif ( $cgi->param('magic') eq 'paybatch' ) {
-%
-%       $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
-%         or die "illegal paybatch: ". $cgi->param('paybatch');
-%
-%       push @search, "paybatch = '$1'";
-%
-%       $orderby = "LOWER(company || ' ' || last || ' ' || first )";
-%
-%     } else {
-%       die "unknown search magic: ". $cgi->param('magic');
-%     }
-%
-%     #here is the agent virtualization
-%     push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
-%
-%     my $search = ' WHERE '. join(' AND ', @search);
-%  
-%     $count_query = "SELECT COUNT(*), SUM(paid) ".
-%                    "FROM cust_pay LEFT JOIN cust_main USING ( custnum )".
-%                    $search;
-%
-%     $sql_query = {
-%       'table'     => 'cust_pay',
-%       'select'    => join(', ',
-%                        'cust_pay.*',
-%                        'cust_main.custnum as cust_main_custnum',
-%                        FS::UI::Web::cust_sql_fields(),
-%                      ),
-%       'hashref'   => {},
-%       'extra_sql' => "$search ORDER BY $orderby",
-%       'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
-%     };
-%
-%   } else {
-%   
-%     $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
-%     my $payinfo = $1;
-%   
-%     $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
-%     my $payby = $1;
-%   
-%     $count_query = "SELECT COUNT(*), SUM(paid) FROM cust_pay".
-%                    "  WHERE payinfo = '$payinfo' AND payby = '$payby'".
-%                    "  AND ". $FS::CurrentUser::CurrentUser->agentnums_sql;
-%   
-%     $sql_query = {
-%       'table'     => 'cust_pay',
-%       'hashref'   => { 'payinfo' => $payinfo,
-%                        'payby'   => $payby    },
-%       'extra_sql' => $FS::CurrentUser::CurrentUser->agentnums_sql.
-%                      " ORDER BY _date",
-%     };
-%   
-%   }
-%
-%   my $link = sub {
-%     my $cust_pay = shift;
-%     $cust_pay->cust_main_custnum
-%       ? [ "${p}view/cust_main.cgi?", 'custnum' ] 
-%       : '';
-%   };
-%
-%
 <% include( 'elements/search.html',
                  'title'       => $title,
                  'name'        => 'payments',
@@ -199,12 +35,194 @@
                    \&FS::UI::Web::cust_fields,
                  ],
                  #'align' => 'lrrrll',
-                 'align' => 'rrr',
+                 'align' => 'rrr'.FS::UI::Web::cust_aligns(),
                  'links' => [
                    '',
                    '',
                    '',
-                   ( map { $link } FS::UI::Web::cust_header() ),
+                   ( map { $_ ne 'Cust. Status' ? $link : '' }
+                         FS::UI::Web::cust_header()
+                   ),
                  ],
+                 'color' => [ 
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_colors(),
+                            ],
+                 'style' => [ 
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_styles(),
+                            ],
       )
 %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
+
+my $title = 'Payment Search Results';
+my( $count_query, $sql_query );
+if ( $cgi->param('magic') ) {
+
+  my @search = ();
+  my $orderby;
+  if ( $cgi->param('magic') eq '_date' ) {
+
+
+    if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
+      push @search, "agentnum = $1"; # $search{'agentnum'} = $1;
+      my $agent = qsearchs('agent', { 'agentnum' => $1 } );
+      die "unknown agentnum $1" unless $agent;
+      $title = $agent->agent. " $title";
+    }
+  
+    if ( $cgi->param('payby') ) {
+      $cgi->param('payby') =~
+        /^(CARD|CHEK|BILL|PREP|CASH|WEST|MCRD)(-(VisaMC|Amex|Discover|Maestro))?$/
+          or die "illegal payby ". $cgi->param('payby');
+      push @search, "cust_pay.payby = '$1'";
+      if ( $3 ) {
+
+        my $cardtype = $3;
+
+        my $search;
+        if ( $cardtype eq 'VisaMC' ) {
+          #avoid posix regexes for portability
+          $search =
+            " ( (     substring(cust_pay.payinfo from 1 for 1) = '4'     ".
+            "     AND substring(cust_pay.payinfo from 1 for 4) != '4936' ".
+            "     AND substring(cust_pay.payinfo from 1 for 6)           ".
+            "         NOT SIMILAR TO '49030[2-9]'                        ".
+            "     AND substring(cust_pay.payinfo from 1 for 6)           ".
+            "         NOT SIMILAR TO '49033[5-9]'                        ".
+            "     AND substring(cust_pay.payinfo from 1 for 6)           ".
+            "         NOT SIMILAR TO '49110[1-2]'                        ".
+            "     AND substring(cust_pay.payinfo from 1 for 6)           ".
+            "         NOT SIMILAR TO '49117[4-9]'                        ".
+            "     AND substring(cust_pay.payinfo from 1 for 6)           ".
+            "         NOT SIMILAR TO '49118[1-2]'                        ".
+            "   )".
+            "   OR substring(cust_pay.payinfo from 1 for 2) = '51' ".
+            "   OR substring(cust_pay.payinfo from 1 for 2) = '52' ".
+            "   OR substring(cust_pay.payinfo from 1 for 2) = '53' ".
+            "   OR substring(cust_pay.payinfo from 1 for 2) = '54' ".
+            "   OR substring(cust_pay.payinfo from 1 for 2) = '54' ".
+            "   OR substring(cust_pay.payinfo from 1 for 2) = '55' ".
+            "   OR substring(cust_pay.payinfo from 1 for 2) = '36' ". #Diner's int'l processed as Visa/MC inside US
+            " ) ";
+        } elsif ( $cardtype eq 'Amex' ) {
+          $search =
+            " (    substring(cust_pay.payinfo from 1 for 2 ) = '34' ".
+            "   OR substring(cust_pay.payinfo from 1 for 2 ) = '37' ".
+            " ) ";
+        } elsif ( $cardtype eq 'Discover' ) {
+          $search =
+            " (    substring(cust_pay.payinfo from 1 for 4 ) = '6011'  ".
+            "   OR substring(cust_pay.payinfo from 1 for 2 ) = '65'    ".
+            "   OR substring(cust_pay.payinfo from 1 for 3 ) = '622'   ". #China Union Pay processed as Discover outside CN
+            " ) ";
+        } elsif ( $cardtype eq 'Maestro' ) { 
+          $search =
+            " (    substring(cust_pay.payinfo from 1 for 2 ) = '63'     ".
+            "   OR substring(cust_pay.payinfo from 1 for 2 ) = '67'     ".
+            "   OR substring(cust_pay.payinfo from 1 for 6 ) = '564182' ".
+            "   OR substring(cust_pay.payinfo from 1 for 4 ) = '4936'   ".
+            "   OR substring(cust_pay.payinfo from 1 for 6 )            ".
+            "      SIMILAR TO '49030[2-9]'                             ".
+            "   OR substring(cust_pay.payinfo from 1 for 6 )            ".
+            "      SIMILAR TO '49033[5-9]'                             ".
+            "   OR substring(cust_pay.payinfo from 1 for 6 )            ".
+            "      SIMILAR TO '49110[1-2]'                             ".
+            "   OR substring(cust_pay.payinfo from 1 for 6 )            ".
+            "      SIMILAR TO '49117[4-9]'                             ".
+            "   OR substring(cust_pay.payinfo from 1 for 6 )            ".
+            "      SIMILAR TO '49118[1-2]'                             ".
+            " ) ";
+        } else {
+          die "unknown card type $cardtype";
+        }
+
+        my $masksearch = $search;
+        $masksearch =~ s/cust_pay\.payinfo/cust_pay.paymask/gi;
+
+        push @search,
+          "( $search OR ( cust_pay.paymask IS NOT NULL AND $masksearch ) )";
+
+      }
+    }
+
+    my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
+    push @search, "_date >= $beginning ",
+                  "_date <= $ending";
+
+    push @search, FS::UI::Web::parse_lt_gt($cgi, 'paid' );
+
+    $orderby = '_date';
+
+  } elsif ( $cgi->param('magic') eq 'paybatch' ) {
+
+    $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
+      or die "illegal paybatch: ". $cgi->param('paybatch');
+
+    push @search, "paybatch = '$1'";
+
+    $orderby = "LOWER(company || ' ' || last || ' ' || first )";
+
+  } else {
+    die "unknown search magic: ". $cgi->param('magic');
+  }
+
+  #here is the agent virtualization
+  push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
+
+  my $search = ' WHERE '. join(' AND ', @search);
+
+  $count_query = "SELECT COUNT(*), SUM(paid) ".
+                 "FROM cust_pay LEFT JOIN cust_main USING ( custnum )".
+                 $search;
+
+  $sql_query = {
+    'table'     => 'cust_pay',
+    'select'    => join(', ',
+                     'cust_pay.*',
+                     'cust_main.custnum as cust_main_custnum',
+                     FS::UI::Web::cust_sql_fields(),
+                   ),
+    'hashref'   => {},
+    'extra_sql' => "$search ORDER BY $orderby",
+    'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
+  };
+
+} else {
+
+  $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
+  my $payinfo = $1;
+
+  $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
+  my $payby = $1;
+
+  $count_query = "SELECT COUNT(*), SUM(paid) FROM cust_pay".
+                 "  WHERE payinfo = '$payinfo' AND payby = '$payby'".
+                 "  AND ". $FS::CurrentUser::CurrentUser->agentnums_sql;
+
+  $sql_query = {
+    'table'     => 'cust_pay',
+    'hashref'   => { 'payinfo' => $payinfo,
+                     'payby'   => $payby    },
+    'extra_sql' => $FS::CurrentUser::CurrentUser->agentnums_sql.
+                   " ORDER BY _date",
+  };
+
+}
+
+my $link = sub {
+  my $cust_pay = shift;
+  $cust_pay->cust_main_custnum
+    ? [ "${p}view/cust_main.cgi?", 'custnum' ] 
+    : '';
+};
+
+</%init>

Index: svc_phone.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/svc_phone.cgi,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- svc_phone.cgi	29 Dec 2006 08:51:33 -0000	1.3
+++ svc_phone.cgi	3 Feb 2007 11:36:30 -0000	1.4
@@ -1,70 +1,3 @@
-%my $conf = new FS::Conf;
-%
-%my $orderby = 'ORDER BY svcnum';
-%my %svc_phone = ();
-%my @extra_sql = ();
-%if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
-%
-%  push @extra_sql, 'pkgnum IS NULL'
-%    if $cgi->param('magic') eq 'unlinked';
-%
-%  if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
-%    my $sortby = $1;
-%    $orderby = "ORDER BY $sortby";
-%  }
-%
-%} elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
-%  push @extra_sql, "svcpart = $1";
-%} else {
-%  $cgi->param('phonenum') =~ /^([\d\- ]+)$/; 
-%  ( $svc_phone{'phonenum'} = $1 ) =~ s/\D//g;
-%}
-%
-%my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
-%                ' LEFT JOIN part_svc  USING ( svcpart ) '.
-%                ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
-%                ' LEFT JOIN cust_main USING ( custnum ) ';
-%
-%#here is the agent virtualization
-%push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql;
-%
-%my $extra_sql = '';
-%if ( @extra_sql ) {
-%  $extra_sql = ( keys(%svc_phone) ? ' AND ' : ' WHERE ' ).
-%               join(' AND ', @extra_sql );
-%}
-%
-%my $count_query = "SELECT COUNT(*) FROM svc_phone $addl_from ";
-%if ( keys %svc_phone ) {
-%  $count_query .= ' WHERE '.
-%                    join(' AND ', map "$_ = ". dbh->quote($svc_phone{$_}),
-%                                      keys %svc_phone
-%                        );
-%}
-%$count_query .= $extra_sql;
-%
-%my $sql_query = {
-%  'table'     => 'svc_phone',
-%  'hashref'   => \%svc_phone,
-%  'select'    => join(', ',
-%                   'svc_phone.*',
-%                   'part_svc.svc',
-%                    'cust_main.custnum',
-%                    FS::UI::Web::cust_sql_fields(),
-%                 ),
-%  'extra_sql' => "$extra_sql $orderby",
-%  'addl_from' => $addl_from,
-%};
-%
-%my $link = [ "${p}view/svc_phone.cgi?", 'svcnum' ];
-%
-%#smaller false laziness w/svc_*.cgi here
-%my $link_cust = sub {
-%  my $svc_x = shift;
-%  $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
-%};
-%
-%
 <% include( 'elements/search.html',
                  'title'             => "Phone number search results",
                  'name'              => 'phone numbers',
@@ -87,9 +20,96 @@
                                           $link,
                                           $link,
                                           $link,
-                                          ( map { $link_cust }
+                                          ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
                                                 FS::UI::Web::cust_header()
                                           ),
                                         ],
+                 'align' => 'rlrr'. FS::UI::Web::cust_aligns(),
+                 'color' => [ 
+                              '',
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_colors(),
+                            ],
+                 'style' => [ 
+                              '',
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_styles(),
+                            ],
               )
 %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List services');
+
+my $conf = new FS::Conf;
+
+my $orderby = 'ORDER BY svcnum';
+my %svc_phone = ();
+my @extra_sql = ();
+if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
+
+  push @extra_sql, 'pkgnum IS NULL'
+    if $cgi->param('magic') eq 'unlinked';
+
+  if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
+    my $sortby = $1;
+    $orderby = "ORDER BY $sortby";
+  }
+
+} elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
+  push @extra_sql, "svcpart = $1";
+} else {
+  $cgi->param('phonenum') =~ /^([\d\- ]+)$/; 
+  ( $svc_phone{'phonenum'} = $1 ) =~ s/\D//g;
+}
+
+my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
+                ' LEFT JOIN part_svc  USING ( svcpart ) '.
+                ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
+                ' LEFT JOIN cust_main USING ( custnum ) ';
+
+#here is the agent virtualization
+push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql;
+
+my $extra_sql = '';
+if ( @extra_sql ) {
+  $extra_sql = ( keys(%svc_phone) ? ' AND ' : ' WHERE ' ).
+               join(' AND ', @extra_sql );
+}
+
+my $count_query = "SELECT COUNT(*) FROM svc_phone $addl_from ";
+if ( keys %svc_phone ) {
+  $count_query .= ' WHERE '.
+                    join(' AND ', map "$_ = ". dbh->quote($svc_phone{$_}),
+                                      keys %svc_phone
+                        );
+}
+$count_query .= $extra_sql;
+
+my $sql_query = {
+  'table'     => 'svc_phone',
+  'hashref'   => \%svc_phone,
+  'select'    => join(', ',
+                   'svc_phone.*',
+                   'part_svc.svc',
+                    'cust_main.custnum',
+                    FS::UI::Web::cust_sql_fields(),
+                 ),
+  'extra_sql' => "$extra_sql $orderby",
+  'addl_from' => $addl_from,
+};
+
+my $link = [ "${p}view/svc_phone.cgi?", 'svcnum' ];
+
+#smaller false laziness w/svc_*.cgi here
+my $link_cust = sub {
+  my $svc_x = shift;
+  $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
+};
+
+</%init>

Index: svc_acct.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/svc_acct.cgi,v
retrieving revision 1.39
retrieving revision 1.40
diff -u -d -r1.39 -r1.40
--- svc_acct.cgi	29 Dec 2006 08:51:33 -0000	1.39
+++ svc_acct.cgi	3 Feb 2007 11:36:30 -0000	1.40
@@ -1,128 +1,3 @@
-%my @extra_sql = ();
-%
-% if ( $cgi->param('domain') ) { 
-%   my $svc_domain =
-%     qsearchs('svc_domain', { 'domain' => $cgi->param('domain') } );
-%   unless ( $svc_domain ) {
-%     #it would be nice if this looked more like the other "not found"
-%     #errors, but this will do for now.
-%     eidiot "Domain ". $cgi->param('domain'). " not found at all";
-%   } else {
-%     push @extra_sql, 'domsvc = '. $svc_domain->svcnum;
-%   }
-% }
-%
-%my $orderby = 'ORDER BY svcnum';
-%if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
-%
-%  push @extra_sql, 'pkgnum IS NULL'
-%    if $cgi->param('magic') eq 'unlinked';
-%
-%  if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
-%    my $sortby = $1;
-%    $sortby = "LOWER($sortby)"
-%      if $sortby eq 'username';
-%    push @extra_sql, "$sortby IS NOT NULL"
-%      if $sortby eq 'uid';
-%    $orderby = "ORDER BY $sortby";
-%  }
-%
-%} elsif ( $cgi->param('popnum') =~ /^(\d+)$/ ) {
-%  push @extra_sql, "popnum = $1";
-%  $orderby = "ORDER BY LOWER(username)";
-%} elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
-%  push @extra_sql, "svcpart = $1";
-%  $orderby = "ORDER BY uid";
-%  #$orderby = "ORDER BY svcnum";
-%} else {
-%  $orderby = "ORDER BY uid";
-%
-%  my @username_sql;
-%
-%  my %username_type;
-%  foreach ( $cgi->param('username_type') ) {
-%    $username_type{$_}++;
-%  }
-%
-%  $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
-%  my $username = $1;
-%
-%  push @username_sql, "username ILIKE '$username'"
-%    if $username_type{'Exact'}
-%    || $username_type{'Fuzzy'};
-%
-%  push @username_sql, "username ILIKE '\%$username\%'"
-%    if $username_type{'Substring'}
-%    || $username_type{'All'};
-%
-%  if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
-%    &FS::svc_acct::check_and_rebuild_fuzzyfiles;
-%    my $all_username = &FS::svc_acct::all_username;
-%
-%    my %username;
-%    if ( $username_type{'Fuzzy'} || $username_type{'All'} ) { 
-%      foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
-%        $username{$_}++; 
-%      }
-%    }
-%
-%    #if ($username_type{'Sound-alike'}) {
-%    #}
-%
-%    push @username_sql, "username = '$_'"
-%      foreach (keys %username);
-%
-%  }
-%
-%  push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
-%
-%}
-%
-%my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
-%                ' LEFT JOIN part_svc  USING ( svcpart ) '.
-%                ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
-%                ' LEFT JOIN cust_main USING ( custnum ) ';
-%
-%#here is the agent virtualization
-%push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql;
-%
-%my $extra_sql = 
-%  scalar(@extra_sql)
-%    ? ' WHERE '. join(' AND ', @extra_sql )
-%    : '';
-%
-%my $count_query = "SELECT COUNT(*) FROM svc_acct $addl_from $extra_sql";
-%#if ( keys %svc_acct ) {
-%#  $count_query .= ' WHERE '.
-%#                    join(' AND ', map "$_ = ". dbh->quote($svc_acct{$_}),
-%#                                      keys %svc_acct
-%#                        );
-%#}
-%
-%my $sql_query = {
-%  'table' => 'svc_acct',
-%  'hashref'   => {}, # \%svc_acct,
-%  'select'    => join(', ',
-%                    'svc_acct.*',
-%                    'part_svc.svc',
-%                    'cust_main.custnum',
-%                    FS::UI::Web::cust_sql_fields(),
-%                  ),
-%  'extra_sql' => "$extra_sql $orderby",
-%  'addl_from' => $addl_from,
-%};
-%
-%my $link      = [ "${p}view/svc_acct.cgi?",   'svcnum'  ];
-%my $link_cust = sub {
-%  my $svc_acct = shift;
-%  if ( $svc_acct->custnum ) {
-%    [ "${p}view/cust_main.cgi?", 'custnum' ];
-%  } else {
-%    '';
-%  }
-%};
-%
-%
 <% include( 'elements/search.html',
                  'title'       => 'Account Search Results',
                  'name'        => 'accounts',
@@ -145,9 +20,155 @@
                                     $link,
                                     $link,
                                     $link,
-                                    ( map { $link_cust }
+                                    ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
                                           FS::UI::Web::cust_header()
                                     ),
                                   ],
+                 'align' => 'rlll'. FS::UI::Web::cust_aligns(),
+                 'color' => [ 
+                              '',
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_colors(),
+                            ],
+                 'style' => [ 
+                              '',
+                              '',
+                              '',
+                              '',
+                              FS::UI::Web::cust_styles(),
+                            ],
              )
 %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List services');
+
+my @extra_sql = ();
+
+ if ( $cgi->param('domain') ) { 
+   my $svc_domain =
+     qsearchs('svc_domain', { 'domain' => $cgi->param('domain') } );
+   unless ( $svc_domain ) {
+     #it would be nice if this looked more like the other "not found"
+     #errors, but this will do for now.
+     eidiot "Domain ". $cgi->param('domain'). " not found at all";
+   } else {
+     push @extra_sql, 'domsvc = '. $svc_domain->svcnum;
+   }
+ }
+
+my $orderby = 'ORDER BY svcnum';
+if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
+
+  push @extra_sql, 'pkgnum IS NULL'
+    if $cgi->param('magic') eq 'unlinked';
+
+  if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
+    my $sortby = $1;
+    $sortby = "LOWER($sortby)"
+      if $sortby eq 'username';
+    push @extra_sql, "$sortby IS NOT NULL"
+      if $sortby eq 'uid';
+    $orderby = "ORDER BY $sortby";
+  }
+
+} elsif ( $cgi->param('popnum') =~ /^(\d+)$/ ) {
+  push @extra_sql, "popnum = $1";
+  $orderby = "ORDER BY LOWER(username)";
+} elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
+  push @extra_sql, "svcpart = $1";
+  $orderby = "ORDER BY uid";
+  #$orderby = "ORDER BY svcnum";
+} else {
+  $orderby = "ORDER BY uid";
+
+  my @username_sql;
+
+  my %username_type;
+  foreach ( $cgi->param('username_type') ) {
+    $username_type{$_}++;
+  }
+
+  $cgi->param('username') =~ /^([\w\-\.\&]+)$/; #untaint username_text
+  my $username = $1;
+
+  push @username_sql, "username ILIKE '$username'"
+    if $username_type{'Exact'}
+    || $username_type{'Fuzzy'};
+
+  push @username_sql, "username ILIKE '\%$username\%'"
+    if $username_type{'Substring'}
+    || $username_type{'All'};
+
+  if ( $username_type{'Fuzzy'} || $username_type{'All'} ) {
+    &FS::svc_acct::check_and_rebuild_fuzzyfiles;
+    my $all_username = &FS::svc_acct::all_username;
+
+    my %username;
+    if ( $username_type{'Fuzzy'} || $username_type{'All'} ) { 
+      foreach ( amatch($username, [ qw(i) ], @$all_username) ) {
+        $username{$_}++; 
+      }
+    }
+
+    #if ($username_type{'Sound-alike'}) {
+    #}
+
+    push @username_sql, "username = '$_'"
+      foreach (keys %username);
+
+  }
+
+  push @extra_sql, '( '. join( ' OR ', @username_sql). ' )';
+
+}
+
+my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
+                ' LEFT JOIN part_svc  USING ( svcpart ) '.
+                ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
+                ' LEFT JOIN cust_main USING ( custnum ) ';
+
+#here is the agent virtualization
+push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql;
+
+my $extra_sql = 
+  scalar(@extra_sql)
+    ? ' WHERE '. join(' AND ', @extra_sql )
+    : '';
+
+my $count_query = "SELECT COUNT(*) FROM svc_acct $addl_from $extra_sql";
+#if ( keys %svc_acct ) {
+#  $count_query .= ' WHERE '.
+#                    join(' AND ', map "$_ = ". dbh->quote($svc_acct{$_}),
+#                                      keys %svc_acct
+#                        );
+#}
+
+my $sql_query = {
+  'table' => 'svc_acct',
+  'hashref'   => {}, # \%svc_acct,
+  'select'    => join(', ',
+                    'svc_acct.*',
+                    'part_svc.svc',
+                    'cust_main.custnum',
+                    FS::UI::Web::cust_sql_fields(),
+                  ),
+  'extra_sql' => "$extra_sql $orderby",
+  'addl_from' => $addl_from,
+};
+
+my $link      = [ "${p}view/svc_acct.cgi?",   'svcnum'  ];
+my $link_cust = sub {
+  my $svc_acct = shift;
+  if ( $svc_acct->custnum ) {
+    [ "${p}view/cust_main.cgi?", 'custnum' ];
+  } else {
+    '';
+  }
+};
+
+</%init>
+

Index: cust_main.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/cust_main.html,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- cust_main.html	6 Mar 2005 03:04:29 -0000	1.5
+++ cust_main.html	3 Feb 2007 11:36:30 -0000	1.6
@@ -39,4 +39,9 @@
   </UL>
   </BODY>
 </HTML>
+<%init>
 
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List customers');
+
+</%init>

Index: cust_pkg.cgi
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/cust_pkg.cgi,v
retrieving revision 1.40
retrieving revision 1.41
diff -u -d -r1.40 -r1.41
--- cust_pkg.cgi	25 Oct 2006 04:44:48 -0000	1.40
+++ cust_pkg.cgi	3 Feb 2007 11:36:30 -0000	1.41
@@ -1,218 +1,4 @@
-%
-%
-%# my %part_pkg = map { $_->pkgpart => $_ } qsearch('part_pkg', {});
-%
-%my($query) = $cgi->keywords;
-%
-%my @where = ();
-%
-%##
-%# parse agent
-%##
-%
-%if ( $cgi->param('agentnum') =~ /^(\d+)$/ and $1 ) {
-%  push @where,
-%    "agentnum = $1";
-%}
-%
-%##
-%# parse status
-%##
-%
-%if (    $cgi->param('magic')  eq 'active'
-%     || $cgi->param('status') eq 'active' ) {
-%
-%  push @where, FS::cust_pkg->active_sql();
-%
-%} elsif (    $cgi->param('magic')  eq 'inactive'
-%          || $cgi->param('status') eq 'inactive' ) {
-%
-%  push @where, FS::cust_pkg->inactive_sql();
-%
-%
-%} elsif (    $cgi->param('magic')  eq 'suspended'
-%          || $cgi->param('status') eq 'suspended'  ) {
-%
-%  push @where, FS::cust_pkg->suspended_sql();
-%
-%} elsif (    $cgi->param('magic')  =~ /^cancell?ed$/
-%          || $cgi->param('status') =~ /^cancell?ed$/ ) {
-%
-%  push @where, FS::cust_pkg->cancelled_sql();
-%
-%} elsif ( $cgi->param('status') =~ /^(one-time charge|inactive)$/ ) {
-%
-%  push @where, FS::cust_pkg->inactive_sql();
-%
-%}
-%
-%###
-%# parse package class
-%###
-%
-%#false lazinessish w/graph/cust_bill_pkg.cgi
-%my $classnum = 0;
-%my @pkg_class = ();
-%if ( exists($cgi->Vars->{'classnum'})
-%     && $cgi->param('classnum') =~ /^(\d*)$/
-%   )
-%{
-%  $classnum = $1;
-%  if ( $classnum ) { #a specific class
-%    push @where, "classnum = $classnum";
-%
-%    #@pkg_class = ( qsearchs('pkg_class', { 'classnum' => $classnum } ) );
-%    #die "classnum $classnum not found!" unless $pkg_class[0];
-%    #$title .= $pkg_class[0]->classname.' ';
-%
-%  } elsif ( $classnum eq '' ) { #the empty class
-%
-%    push @where, "classnum IS NULL";
-%    #$title .= 'Empty class ';
-%    #@pkg_class = ( '(empty class)' );
-%  } elsif ( $classnum eq '0' ) {
-%    #@pkg_class = qsearch('pkg_class', {} ); # { 'disabled' => '' } );
-%    #push @pkg_class, '(empty class)';
-%  } else {
-%    die "illegal classnum";
-%  }
-%}
-%#eslaf
-%
-%###
-%# parse part_pkg
-%###
-%
-%my $pkgpart = join (' OR pkgpart=',
-%                    grep {$_} map { /^(\d+)$/; } ($cgi->param('pkgpart')));
-%push @where,  '(pkgpart=' . $pkgpart . ')' if $pkgpart;
-%
-%###
-%# parse magic, legacy, etc.
-%###
-%
-%my $orderby;
-%if ( $cgi->param('magic') && $cgi->param('magic') eq 'bill' ) {
-%  $orderby = 'ORDER BY bill';
-%
-%  my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
-%  push @where,
-%    #"bill >= $beginning ",
-%    #"bill <= $ending",
-%    "CASE WHEN bill IS NULL THEN 0 ELSE bill END >= $beginning ",
-%    "CASE WHEN bill IS NULL THEN 0 ELSE bill END <= $ending",
-%    #'( cancel IS NULL OR cancel = 0 )'
-%  ;
-%
-%} else {
-%
-%  if ( $cgi->param('magic') &&
-%       $cgi->param('magic') =~ /^(active|inactive|suspended|cancell?ed)$/
-%  ) {
-%
-%    $orderby = 'ORDER BY pkgnum';
-%
-%    if ( $cgi->param('pkgpart') =~ /^(\d+)$/ ) {
-%      push @where, "pkgpart = $1";
-%    }
-%
-%  } elsif ( $query eq 'pkgnum' ) {
-%
-%    $orderby = 'ORDER BY pkgnum';
-%
-%  } elsif ( $query eq 'APKG_pkgnum' ) {
-%  
-%    $orderby = 'ORDER BY pkgnum';
-%  
-%    push @where, '0 < (
-%      SELECT count(*) FROM pkg_svc
-%       WHERE pkg_svc.pkgpart =  cust_pkg.pkgpart
-%         AND pkg_svc.quantity > ( SELECT count(*) FROM cust_svc
-%                                   WHERE cust_svc.pkgnum  = cust_pkg.pkgnum
-%                                     AND cust_svc.svcpart = pkg_svc.svcpart
-%                                )
-%    )';
-%    
-%  } else {
-%    die "Empty or unknown QUERY_STRING!";
-%  }
-%
-%}
-%
-%##
-%# setup queries, links, subs, etc. for the search
-%##
-%
-%# here is the agent virtualization
-%push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
-%
-%my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
-%
-%my $addl_from = 'LEFT JOIN cust_main USING ( custnum  ) '.
-%                'LEFT JOIN part_pkg  USING ( pkgpart  ) '.
-%                'LEFT JOIN pkg_class USING ( classnum ) ';
-%
-%my $count_query = "SELECT COUNT(*) FROM cust_pkg $addl_from $extra_sql";
-%
-%my $sql_query = {
-%  'table'     => 'cust_pkg',
-%  'hashref'   => {},
-%  'select'    => join(', ',
-%                            'cust_pkg.*',
-%                            ( map "part_pkg.$_", qw( pkg freq ) ),
-%                            'pkg_class.classname',
-%                            'cust_main.custnum as cust_main_custnum',
-%                            FS::UI::Web::cust_sql_fields(
-%                              $cgi->param('cust_fields')
-%                            ),
-%                 ),
-%  'extra_sql' => "$extra_sql $orderby",
-%  'addl_from' => $addl_from,
-%};
-%
-%my $link = sub {
-%  [ "${p}view/cust_main.cgi?".shift->custnum.'#cust_pkg', 'pkgnum' ];
-%};
-%
-%my $clink = sub {
-%  my $cust_pkg = shift;
-%  $cust_pkg->cust_main_custnum
-%    ? [ "${p}view/cust_main.cgi?", 'custnum' ] 
-%    : '';
-%};
-%
-%#if ( scalar(@cust_pkg) == 1 ) {
-%#  print $cgi->redirect("${p}view/cust_main.cgi?". $cust_pkg[0]->custnum.
-%#                       "#cust_pkg". $cust_pkg[0]->pkgnum );
-%
-%#    my @cust_svc = qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } );
-%#    my $rowspan = scalar(@cust_svc) || 1;
-%
-%#    my $n2 = '';
-%#    foreach my $cust_svc ( @cust_svc ) {
-%#      my($label, $value, $svcdb) = $cust_svc->label;
-%#      my $svcnum = $cust_svc->svcnum;
-%#      my $sview = $p. "view";
-%#      print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
-%#            qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
-%#      $n2="</TR><TR>";
-%#    }
-%
-%sub time_or_blank {
-%   my $column = shift;
-%   return sub {
-%     my $record = shift;
-%     my $value = $record->get($column); #mmm closures
-%     $value ? time2str('%b %d %Y', $value ) : '';
-%   };
-%}
-%
-%###
-%# and finally, include the search template
-%### 
-%
-%
-<%  include( 'elements/search.html',
+<% include( 'elements/search.html',
                   'title'       => 'Package Search Results', 
                   'name'        => 'packages',
                   'query'       => $sql_query,
@@ -295,16 +81,13 @@
                     '',
                     '',
                     '',
-                    ( map { '' }
-                          FS::UI::Web::cust_header(
-                                                    $cgi->param('cust_fields')
-                                                  )
-                    ),
+                    FS::UI::Web::cust_colors(),
                     '',
                   ],
-                  'style' => [ '', '', '', 'b' ],
+                  'style' => [ '', '', '', 'b', '', '', '', '', '', '', '',
+                               FS::UI::Web::cust_styles() ],
                   'size'  => [ '', '', '', '-1', ],
-                  'align' => 'rllclrrrrrr',
+                  'align' => 'rllclrrrrrr'. FS::UI::Web::cust_aligns(). 'r',
                   'links' => [
                     $link,
                     $link,
@@ -317,7 +100,7 @@
                     '',
                     '',
                     '',
-                    ( map { $clink }
+                    ( map { $_ ne 'Cust. Status' ? $clink : '' }
                           FS::UI::Web::cust_header(
                                                     $cgi->param('cust_fields')
                                                   )
@@ -326,3 +109,216 @@
                   ],
               )
 %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List packages');
+
+# my %part_pkg = map { $_->pkgpart => $_ } qsearch('part_pkg', {});
+
+my($query) = $cgi->keywords;
+
+my @where = ();
+
+##
+# parse agent
+##
+
+if ( $cgi->param('agentnum') =~ /^(\d+)$/ and $1 ) {
+  push @where,
+    "agentnum = $1";
+}
+
+##
+# parse status
+##
+
+if (    $cgi->param('magic')  eq 'active'
+     || $cgi->param('status') eq 'active' ) {
+
+  push @where, FS::cust_pkg->active_sql();
+
+} elsif (    $cgi->param('magic')  eq 'inactive'
+          || $cgi->param('status') eq 'inactive' ) {
+
+  push @where, FS::cust_pkg->inactive_sql();
+
+
+} elsif (    $cgi->param('magic')  eq 'suspended'
+          || $cgi->param('status') eq 'suspended'  ) {
+
+  push @where, FS::cust_pkg->suspended_sql();
+
+} elsif (    $cgi->param('magic')  =~ /^cancell?ed$/
+          || $cgi->param('status') =~ /^cancell?ed$/ ) {
+
+  push @where, FS::cust_pkg->cancelled_sql();
+
+} elsif ( $cgi->param('status') =~ /^(one-time charge|inactive)$/ ) {
+
+  push @where, FS::cust_pkg->inactive_sql();
+
+}
+
+###
+# parse package class
+###
+
+#false lazinessish w/graph/cust_bill_pkg.cgi
+my $classnum = 0;
+my @pkg_class = ();
+if ( exists($cgi->Vars->{'classnum'})
+     && $cgi->param('classnum') =~ /^(\d*)$/
+   )
+{
+  $classnum = $1;
+  if ( $classnum ) { #a specific class
+    push @where, "classnum = $classnum";
+
+    #@pkg_class = ( qsearchs('pkg_class', { 'classnum' => $classnum } ) );
+    #die "classnum $classnum not found!" unless $pkg_class[0];
+    #$title .= $pkg_class[0]->classname.' ';
+
+  } elsif ( $classnum eq '' ) { #the empty class
+
+    push @where, "classnum IS NULL";
+    #$title .= 'Empty class ';
+    #@pkg_class = ( '(empty class)' );
+  } elsif ( $classnum eq '0' ) {
+    #@pkg_class = qsearch('pkg_class', {} ); # { 'disabled' => '' } );
+    #push @pkg_class, '(empty class)';
+  } else {
+    die "illegal classnum";
+  }
+}
+#eslaf
+
+###
+# parse part_pkg
+###
+
+my $pkgpart = join (' OR pkgpart=',
+                    grep {$_} map { /^(\d+)$/; } ($cgi->param('pkgpart')));
+push @where,  '(pkgpart=' . $pkgpart . ')' if $pkgpart;
+
+###
+# parse magic, legacy, etc.
+###
+
+my $orderby;
+if ( $cgi->param('magic') && $cgi->param('magic') eq 'bill' ) {
+  $orderby = 'ORDER BY bill';
+
+  my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
+  push @where,
+    #"bill >= $beginning ",
+    #"bill <= $ending",
+    "CASE WHEN bill IS NULL THEN 0 ELSE bill END >= $beginning ",
+    "CASE WHEN bill IS NULL THEN 0 ELSE bill END <= $ending",
+    #'( cancel IS NULL OR cancel = 0 )'
+  ;
+
+} else {
+
+  if ( $cgi->param('magic') &&
+       $cgi->param('magic') =~ /^(active|inactive|suspended|cancell?ed)$/
+  ) {
+
+    $orderby = 'ORDER BY pkgnum';
+
+    if ( $cgi->param('pkgpart') =~ /^(\d+)$/ ) {
+      push @where, "pkgpart = $1";
+    }
+
+  } elsif ( $query eq 'pkgnum' ) {
+
+    $orderby = 'ORDER BY pkgnum';
+
+  } elsif ( $query eq 'APKG_pkgnum' ) {
+  
+    $orderby = 'ORDER BY pkgnum';
+  
+    push @where, '0 < (
+      SELECT count(*) FROM pkg_svc
+       WHERE pkg_svc.pkgpart =  cust_pkg.pkgpart
+         AND pkg_svc.quantity > ( SELECT count(*) FROM cust_svc
+                                   WHERE cust_svc.pkgnum  = cust_pkg.pkgnum
+                                     AND cust_svc.svcpart = pkg_svc.svcpart
+                                )
+    )';
+    
+  } else {
+    die "Empty or unknown QUERY_STRING!";
+  }
+
+}
+
+##
+# setup queries, links, subs, etc. for the search
+##
+
+# here is the agent virtualization
+push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
+
+my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
+
+my $addl_from = 'LEFT JOIN cust_main USING ( custnum  ) '.
+                'LEFT JOIN part_pkg  USING ( pkgpart  ) '.
+                'LEFT JOIN pkg_class USING ( classnum ) ';
+
+my $count_query = "SELECT COUNT(*) FROM cust_pkg $addl_from $extra_sql";
+
+my $sql_query = {
+  'table'     => 'cust_pkg',
+  'hashref'   => {},
+  'select'    => join(', ',
+                            'cust_pkg.*',
+                            ( map "part_pkg.$_", qw( pkg freq ) ),
+                            'pkg_class.classname',
+                            'cust_main.custnum as cust_main_custnum',
+                            FS::UI::Web::cust_sql_fields(
+                              $cgi->param('cust_fields')
+                            ),
+                 ),
+  'extra_sql' => "$extra_sql $orderby",
+  'addl_from' => $addl_from,
+};
+
+my $link = sub {
+  [ "${p}view/cust_main.cgi?".shift->custnum.'#cust_pkg', 'pkgnum' ];
+};
+
+my $clink = sub {
+  my $cust_pkg = shift;
+  $cust_pkg->cust_main_custnum
+    ? [ "${p}view/cust_main.cgi?", 'custnum' ] 
+    : '';
+};
+
+#if ( scalar(@cust_pkg) == 1 ) {
+#  print $cgi->redirect("${p}view/cust_main.cgi?". $cust_pkg[0]->custnum.
+#                       "#cust_pkg". $cust_pkg[0]->pkgnum );
+
+#    my @cust_svc = qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } );
+#    my $rowspan = scalar(@cust_svc) || 1;
+
+#    my $n2 = '';
+#    foreach my $cust_svc ( @cust_svc ) {
+#      my($label, $value, $svcdb) = $cust_svc->label;
+#      my $svcnum = $cust_svc->svcnum;
+#      my $sview = $p. "view";
+#      print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
+#            qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
+#      $n2="</TR><TR>";
+#    }
+
+sub time_or_blank {
+   my $column = shift;
+   return sub {
+     my $record = shift;
+     my $value = $record->get($column); #mmm closures
+     $value ? time2str('%b %d %Y', $value ) : '';
+   };
+}
+
+</%init>

Index: report_cust_bill.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/report_cust_bill.html,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- report_cust_bill.html	23 Aug 2006 22:25:38 -0000	1.4
+++ report_cust_bill.html	3 Feb 2007 11:36:30 -0000	1.5
@@ -26,3 +26,9 @@
 </FORM>
 
 <% include('/elements/footer.html') %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('List invoices');
+
+</%init>

Index: reg_code.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/reg_code.html,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- reg_code.html	23 Aug 2006 22:25:38 -0000	1.3
+++ reg_code.html	3 Feb 2007 11:36:30 -0000	1.4
@@ -1,13 +1,3 @@
-%
-%
-%my $agentnum = $cgi->param('agentnum');
-%$agentnum =~ /^(\d+)$/ or eidiot "illegal agentnum $agentnum";
-%$agentnum = $1;
-%my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
-%
-%my $count_query = "SELECT COUNT(*) FROM reg_code WHERE agentnum = $agentnum";
-%
-%
 <% include( 'elements/search.html',
                  'title'       => 'Unused Registration Codes for '.
                                   $agent->agent,
@@ -35,3 +25,16 @@
                  ],
       )
 %>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
+
+my $agentnum = $cgi->param('agentnum');
+$agentnum =~ /^(\d+)$/ or eidiot "illegal agentnum $agentnum";
+$agentnum = $1;
+my $agent = qsearchs('agent', { 'agentnum' => $agentnum } );
+
+my $count_query = "SELECT COUNT(*) FROM reg_code WHERE agentnum = $agentnum";
+
+<%init>



More information about the freeside-commits mailing list