From ee5ad1cf9327e8a06c800bfe506ad388a5e8b3d5 Mon Sep 17 00:00:00 2001
From: senator <senator@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Date: Wed, 7 Apr 2010 19:36:51 +0000
Subject: [PATCH] Acq: in acquistions unified search, make timestamp fields
 searchable

Even if only at whole-day precision


git-svn-id: svn://svn.open-ils.org/ILS/trunk@16159 dcc99617-32d9-48b4-a31d-7c20da2025e4
---
 .../src/perlmods/OpenILS/Application/Acq/Search.pm     | 12 ++++++++++--
 Open-ILS/web/js/ui/default/acq/search/unified.js       | 18 ++++++++++++++----
 2 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Acq/Search.pm b/Open-ILS/src/perlmods/OpenILS/Application/Acq/Search.pm
index 5090c53e0a..fb74708531 100644
--- a/Open-ILS/src/perlmods/OpenILS/Application/Acq/Search.pm
+++ b/Open-ILS/src/perlmods/OpenILS/Application/Acq/Search.pm
@@ -44,6 +44,8 @@ sub could_be_range {
     0;
 }
 
+sub castdate { +{"=" => {"transform" => "date", "value" => $_[0]}}; }
+
 sub prepare_acqlia_search_and {
     my ($acqlia) = @_;
 
@@ -55,6 +57,7 @@ sub prepare_acqlia_search_and {
             "where" => {"-and" => [{"lineitem" => {"=" => {"+jub" => "id"}}}]}
         };
 
+        # castdate not supported for acqlia fields: they're all type text
         my ($k, $v, $fuzzy, $between, $not) = breakdown_term($unit);
         my $point = $subquery->{"where"}->{"-and"};
         my $term_clause;
@@ -84,6 +87,7 @@ sub prepare_acqlia_search_or {
     my $result = {"+acqlia" => {"-or" => $point}};
 
     foreach my $unit (@$acqlia) {
+        # castdate not supported for acqlia fields: they're all type text
         my ($k, $v, $fuzzy, $between, $not) = breakdown_term($unit);
         my $term_clause;
         if ($fuzzy and not ref $v) {
@@ -120,7 +124,8 @@ sub breakdown_term {
         $key, $term->{$key},
         $term->{"__fuzzy"} ? 1 : 0,
         $term->{"__between"} ? 1 : 0,
-        $term->{"__not"} ? 1 : 0
+        $term->{"__not"} ? 1 : 0,
+        $term->{"__castdate"} ? 1 : 0
     );
 }
 
@@ -208,13 +213,16 @@ sub prepare_terms {
 
         $outer_clause->{$conj} = [] unless $outer_clause->{$conj};
         foreach my $unit (@{$terms->{$class}}) {
-            my ($k, $v, $fuzzy, $between, $not) = breakdown_term($unit);
+            my ($k, $v, $fuzzy, $between, $not, $castdate) =
+                breakdown_term($unit);
+
             my $term_clause;
             if ($fuzzy and not ref $v) {
                 $term_clause = {$k => {"ilike" => "%" . $v . "%"}};
             } elsif ($between and could_be_range($v)) {
                 $term_clause = {$k => {"between" => $v}};
             } elsif (check_1d_max($v)) {
+                $v = castdate($v) if $castdate;
                 $term_clause = {$k => $v};
             } else {
                 next;
diff --git a/Open-ILS/web/js/ui/default/acq/search/unified.js b/Open-ILS/web/js/ui/default/acq/search/unified.js
index 4f19542b00..0686e82435 100644
--- a/Open-ILS/web/js/ui/default/acq/search/unified.js
+++ b/Open-ILS/web/js/ui/default/acq/search/unified.js
@@ -1,3 +1,4 @@
+dojo.require("dojo.date.stamp");
 dojo.require("openils.widget.AutoGrid");
 dojo.require("openils.widget.AutoWidget");
 dojo.require("openils.PermaCrud");
@@ -276,17 +277,26 @@ function TermManager() {
                 this._match_how(id).getValue().split(",").filter(Boolean);
 
             var value;
-            try {
-                value = this.widgets[id].attr("value");
-            } catch (E) {
+            if (typeof(this.widgets[id].declaredClass) != "undefined") {
+                if (this.widgets[id].declaredClass.match(/Date/)) {
+                    value =
+                        dojo.date.stamp.toISOString(this.widgets[id].value).
+                            split("T")[0];
+                } else {
+                    value = this.widgets[id].attr("value");
+                }
+            } else {
                 value = this.widgets[id].value;
             }
 
             if (!so[hint])
                 so[hint] = [];
+
             var unit = {};
-            match_how.forEach(function(key) { unit[key] = true; });
             unit[attr] = value;
+            match_how.forEach(function(key) { unit[key] = true; });
+            if (this.terms[hint][attr].datatype == "timestamp")
+                unit.__castdate = true;
 
             so[hint].push(unit);
         }
-- 
2.11.0