summaryrefslogtreecommitdiff
path: root/httemplate/search
diff options
context:
space:
mode:
authorivan <ivan>2006-04-12 12:36:39 +0000
committerivan <ivan>2006-04-12 12:36:39 +0000
commitd453a487d95258e1f11f134fc5864f7e6458a6ef (patch)
tree57b0c5dc7523d5e88312000211ef4c6d8ab24a0a /httemplate/search
parent7bdf17a005cf4c0fe8b6b6ad1ce97abaa52a4510 (diff)
zip code report
Diffstat (limited to 'httemplate/search')
-rw-r--r--httemplate/search/cust_main-zip.html95
-rwxr-xr-xhttemplate/search/cust_main.cgi14
-rw-r--r--httemplate/search/elements/search.html14
-rw-r--r--httemplate/search/report_cust_main-zip.html46
4 files changed, 156 insertions, 13 deletions
diff --git a/httemplate/search/cust_main-zip.html b/httemplate/search/cust_main-zip.html
new file mode 100644
index 000000000..333a1e0b1
--- /dev/null
+++ b/httemplate/search/cust_main-zip.html
@@ -0,0 +1,95 @@
+<%
+
+# 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',
+ 'query' => $sql_query,
+ 'count_query' => $count_sql,
+ 'header' => [ 'Zip code', 'Customers', ],
+ #'fields' => [ 'zip', 'num_cust', ],
+ 'links' => [ '', sub { 'somewhere'; } ],
+ )
+%>
diff --git a/httemplate/search/cust_main.cgi b/httemplate/search/cust_main.cgi
index 665f5637d..36ad39da8 100755
--- a/httemplate/search/cust_main.cgi
+++ b/httemplate/search/cust_main.cgi
@@ -102,18 +102,8 @@ if ( $cgi->param('browse')
&& ! $cgi->param('showcancelledcustomers') )
) {
#grep { $_->ncancelled_pkgs || ! $_->all_pkgs }
- push @qual, "
- ( 0 < ( SELECT COUNT(*) FROM cust_pkg
- WHERE cust_pkg.custnum = cust_main.custnum
- AND ( cust_pkg.cancel IS NULL
- OR cust_pkg.cancel = 0
- )
- )
- OR 0 = ( SELECT COUNT(*) FROM cust_pkg
- WHERE cust_pkg.custnum = cust_main.custnum
- )
- )
- ";
+ push @qual, FS::cust_main->uncancel_sql;
+
}
push @qual, FS::cust_main->cancel_sql if $cgi->param('cancelled');
diff --git a/httemplate/search/elements/search.html b/httemplate/search/elements/search.html
index b14bded10..7f7243588 100644
--- a/httemplate/search/elements/search.html
+++ b/httemplate/search/elements/search.html
@@ -68,6 +68,8 @@
# # or a coderef that returns the same
# 'redirect' =>
+ my $DEBUG = 0;
+
my(%opt) = @_;
#warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
@@ -118,6 +120,7 @@
my $header = $opt{'header'};
my $rows;
if ( ref($opt{'query'}) ) {
+
#eval "use FS::$opt{'query'};";
$rows = [ qsearch(
$opt{'query'}->{'table'},
@@ -127,7 +130,9 @@
'',
(exists($opt{'query'}->{'addl_from'}) ? $opt{'query'}->{'addl_from'} : '')
) ];
+
} else {
+
my $sth = dbh->prepare("$opt{'query'} $limit")
or die "Error preparing $opt{'query'}: ". dbh->errstr;
$sth->execute
@@ -137,8 +142,15 @@
$rows = $sth->fetchall_arrayref;
$header ||= $sth->{NAME};
+
}
+ warn scalar(@$rows). ' rows returned from '.
+ ( ref($opt{'query'}) ? 'qsearch query' : 'literal SQL query' )
+ if $DEBUG || $opt{'debug'};
+
+ # display the results - csv, xls or html
+
if ( $type eq 'csv' ) {
#http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
@@ -437,7 +449,7 @@
<% } %>
<% } else { %>
<% foreach ( @$row ) { %>
- <TD CLASS="grid" BGCOLOR="$bgcolor"><%= $_ %></TD>
+ <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><%= $_ %></TD>
<% } %>
<% } %>
</TR>
diff --git a/httemplate/search/report_cust_main-zip.html b/httemplate/search/report_cust_main-zip.html
new file mode 100644
index 000000000..30020f3aa
--- /dev/null
+++ b/httemplate/search/report_cust_main-zip.html
@@ -0,0 +1,46 @@
+<%= include('/elements/header.html', 'Zip code report') %>
+
+ <FORM ACTION="cust_main-zip.html" METHOD="GET">
+
+ <TABLE>
+
+ <TR>
+ <TD ALIGN="right">Billing or service zip</TD>
+ <TD>
+ <SELECT NAME="column">
+ <OPTION VALUE="zip">Billing zip
+ <OPTION VALUE="ship_zip">Service zip
+ </SELECT>
+ </TD>
+ </TR>
+
+ <TR>
+ <TD ALIGN="right">Ignore +4 for US zip codes</TD>
+ <TD><INPUT TYPE="checkbox" NAME="ignore_plus4" VALUE="yes" CHECKED> </TD>
+ </TR>
+
+ <TR>
+ <TD ALIGN="right">Show customers with status:</TD>
+ <TD>
+ <SELECT NAME="status">
+ <OPTION VALUE="">all
+ <OPTION VALUE="prospect">prospect (no packages ever)
+ <OPTION SELECTED VALUE="uncancel">all except cancelled
+ <OPTION VALUE="active">active recurring packages
+ <OPTION VALUE="susp">suspended
+ <OPTION VALUE="cancel">cancelled
+ </SELECT>
+ </TD>
+ </TR>
+
+ <%= include( '/elements/tr-select-agent.html',
+ $cgi->param('agentnum'),
+ 'label' => 'for agent: ',
+ )
+ %>
+
+ </TABLE>
+ <BR><INPUT TYPE="submit" VALUE="Get Report">
+ </FORM>
+ </BODY>
+</HTML>