summaryrefslogtreecommitdiff
path: root/FS/FS
diff options
context:
space:
mode:
authorMark Wells <mark@freeside.biz>2015-06-27 15:51:29 -0700
committerMark Wells <mark@freeside.biz>2015-06-27 19:34:22 -0700
commit2e8ee5e367d3920181db795726c79b0040768cd7 (patch)
tree630968003a0bef84986d9ea107c6665bab22ffd9 /FS/FS
parent617d1718dd13e4015d35710ac9d030ad95ca87cd (diff)
reports with row grouping for payment/refund search, #25944
Diffstat (limited to 'FS/FS')
-rw-r--r--FS/FS/Mason.pm4
-rw-r--r--FS/FS/Query.pm118
2 files changed, 121 insertions, 1 deletions
diff --git a/FS/FS/Mason.pm b/FS/FS/Mason.pm
index 6163197..f05ac03 100644
--- a/FS/FS/Mason.pm
+++ b/FS/FS/Mason.pm
@@ -57,7 +57,7 @@ if ( -e $addl_handler_use_file ) {
use CGI::Cookie;
use List::Util qw( max min sum );
use List::MoreUtils qw( first_index uniq );
- use Scalar::Util qw( blessed );
+ use Scalar::Util qw( blessed looks_like_number );
use Data::Dumper;
use Date::Format;
use Time::Local;
@@ -156,6 +156,8 @@ if ( -e $addl_handler_use_file ) {
use FS::Locales;
use FS::Maketext qw( mt emt js_mt );
+ use FS::Query;
+
use FS::agent;
use FS::agent_type;
use FS::domain_record;
diff --git a/FS/FS/Query.pm b/FS/FS/Query.pm
new file mode 100644
index 0000000..8ecf1c4
--- /dev/null
+++ b/FS/FS/Query.pm
@@ -0,0 +1,118 @@
+package FS::Query;
+
+use strict;
+use FS::Record; # don't import qsearch
+use Storable 'dclone';
+
+=head1 NAME
+
+FS::Query - A thin wrapper around qsearch argument hashes.
+
+=head1 DESCRIPTION
+
+This module exists because we pass qsearch argument lists around a lot,
+and add new joins or WHERE expressions in several stages, and I got tired
+of doing this:
+
+ my $andwhere = "mycolumn IN('perl','python','javascript')";
+ if ( ($search->{hashref} and keys( %{$search->{hashref}} ))
+ or $search->{extra_sql} =~ /^\s*WHERE/ ) {
+ $search->{extra_sql} .= " AND $andwhere";
+ } else {
+ $search->{extra_sql} = " WHERE $andwhere ";
+ }
+
+and then having it fail under some conditions if it's done wrong (as the above
+example is, obviously).
+
+We may eventually switch over to SQL::Abstract or something for this, but for
+now it's a couple of crude manipulations and a wrapper to qsearch.
+
+=head1 METHODS
+
+=over 4
+
+=item new HASHREF
+
+Turns HASHREF (a qsearch argument list) into an FS::Query object. None of
+the params are really required, but you should at least supply C<table>.
+
+In the Future this may do a lot more stuff.
+
+=cut
+
+sub new {
+ my ($class, $hashref) = @_;
+
+ my $self = bless {
+ table => '',
+ select => '*',
+ hashref => {},
+ addl_from => '',
+ extra_sql => '',
+ order_by => '',
+ %$hashref,
+ };
+ # load FS::$table? validate anything?
+ $self;
+}
+
+=item clone
+
+Returns another object that's a copy of this one.
+
+=cut
+
+sub clone {
+ my $self = shift;
+ $self->new( dclone($self) );
+}
+
+=item and_where EXPR
+
+Adds a constraint to the WHERE clause of the query. All other constraints in
+the WHERE clause should be joined with AND already; if not, they should be
+grouped with parentheses.
+
+=cut
+
+sub and_where {
+ my $self = shift;
+ my $where = shift;
+
+ if ($self->{extra_sql} =~ /^\s*(?:WHERE|AND)\s+(.*)/is) {
+ $where = "($where) AND $1";
+ }
+ if (keys %{ $self->{hashref} }) {
+ $where = " AND $where";
+ } else {
+ $where = " WHERE $where";
+ }
+ $self->{extra_sql} = $where;
+
+ return $self;
+}
+
+=item qsearch
+
+Runs the query and returns all results.
+
+=cut
+
+sub qsearch {
+ my $self = shift;
+ FS::Record::qsearch({ %$self });
+}
+
+=item qsearchs
+
+Runs the query and returns only one result.
+
+=cut
+
+sub qsearchs {
+ my $self = shift;
+ FS::Record::qsearchs({ %$self });
+}
+
+1;