1 package OpenILS::Application::Acq::Search;
2 use base "OpenILS::Application";
8 use OpenILS::Utils::CStoreEditor q/:funcs/;
9 use OpenILS::Utils::Fieldmapper;
10 use OpenILS::Application::Acq::Lineitem;
11 use OpenILS::Application::Acq::Financials;
12 use OpenILS::Application::Acq::Picklist;
13 use OpenILS::Application::Acq::Invoice;
17 \&{"OpenILS::Application::Acq::Lineitem::retrieve_lineitem_impl"},
19 \&{"OpenILS::Application::Acq::Picklist::retrieve_picklist_impl"},
20 "purchase_order" => \&{
21 "OpenILS::Application::Acq::Financials::retrieve_purchase_order_impl"
24 "OpenILS::Application::Acq::Invoice::fetch_invoice_impl"
28 sub F { $Fieldmapper::fieldmap->{"Fieldmapper::" . $_[0]}; }
30 # This subroutine returns 1 if the argument is a) a scalar OR
31 # b) an array of ONLY scalars. Otherwise it returns 0.
34 return 1 unless ref $o;
35 if (ref($o) eq "ARRAY") {
36 foreach (@$o) { return 0 if ref $_; }
42 # Returns 1 if and only if argument is an array of exactly two scalars.
45 if (ref $o eq "ARRAY") {
46 return 1 if (scalar(@$o) == 2 && (!ref $o->[0] && !ref $o->[1]));
51 sub castdate { +{"=" => {"transform" => "date", "value" => $_[0]}}; }
53 sub prepare_acqlia_search_and {
57 foreach my $unit (@{$acqlia}) {
59 "select" => {"acqlia" => ["id"]},
61 "where" => {"-and" => [{"lineitem" => {"=" => {"+jub" => "id"}}}]}
64 # castdate not supported for acqlia fields: they're all type text
65 my ($k, $v, $fuzzy, $between, $not) = breakdown_term($unit);
66 my $point = $subquery->{"where"}->{"-and"};
69 push @$point, {"definition" => $k};
71 if ($fuzzy and not ref $v) {
72 push @$point, {"attr_value" => {"ilike" => "%" . $v . "%"}};
73 } elsif ($between and could_be_range($v)) {
74 push @$point, {"attr_value" => {"between" => $v}};
75 } elsif (check_1d_max($v)) {
76 push @$point, {"attr_value" => $v};
81 my $operator = $not ? "-not-exists" : "-exists";
82 push @phrases, {$operator => $subquery};
87 sub prepare_acqlia_search_or {
91 my $result = {"+acqlia" => {"-or" => $point}};
93 foreach my $unit (@$acqlia) {
94 # castdate not supported for acqlia fields: they're all type text
95 my ($k, $v, $fuzzy, $between, $not) = breakdown_term($unit);
97 if ($fuzzy and not ref $v) {
101 "attr_value" => {"ilike" => "%" . $v . "%"}
104 } elsif ($between and could_be_range($v)) {
107 "definition" => $k, "attr_value" => {"between" => $v}
110 } elsif (check_1d_max($v)) {
112 "-and" => {"definition" => $k, "attr_value" => $v}
118 push @$point, $not ? {"-not" => $term_clause} : $term_clause;
126 my $key = (grep { !/^__/ } keys %$term)[0];
129 $term->{"__fuzzy"} ? 1 : 0,
130 $term->{"__between"} ? 1 : 0,
131 $term->{"__not"} ? 1 : 0,
132 $term->{"__castdate"} ? 1 : 0
136 sub get_fm_links_by_hint {
138 foreach my $field (values %{$Fieldmapper::fieldmap}) {
139 return $field->{"links"} if $field->{"hint"} eq $hint;
145 my ($value, $n) = @_;
148 {"+au$n" => {"usrname" => $value}},
149 {"+au$n" => {"first_given_name" => $value}},
150 {"+au$n" => {"second_given_name" => $value}},
151 {"+au$n" => {"family_name" => $value}},
152 {"+ac$n" => {"barcode" => $value}}
157 # go through the terms hash, find keys that correspond to fields links
158 # to actor.usr, and rewrite the search as one that searches not by
159 # actor.usr.id but by any of these user properties: card barcode, username,
160 # given names and family name.
161 sub prepare_au_terms {
162 my ($terms, $join_num) = @_;
168 foreach my $conj (qw/-and -or/) {
169 next unless exists $terms->{$conj};
171 my @new_outer_terms = ();
172 HINT_UNIT: foreach my $hint_unit (@{$terms->{$conj}}) {
173 my $hint = (keys %$hint_unit)[0];
174 (my $plain_hint = $hint) =~ y/+//d;
175 if ($hint eq "-not") {
176 $hint_unit = $hint_unit->{$hint};
181 if (my $links = get_fm_links_by_hint($plain_hint) and
182 $plain_hint ne "acqlia") {
184 my ($attr, $value) = breakdown_term($hint_unit->{$hint});
185 if ($links->{$attr} and
186 $links->{$attr}->{"class"} eq "au") {
187 push @joins, [$plain_hint, $attr, $join_num];
188 my $au_term = gen_au_term($value, $join_num);
190 $au_term = {"-not" => $au_term};
193 push @new_outer_terms, $au_term;
195 delete $hint_unit->{$hint};
199 $hint_unit = {"-not" => $hint_unit};
202 push @new_outer_terms, $hint_unit if scalar keys %$hint_unit;
204 $terms->{$conj} = [ @new_outer_terms ];
210 my ($terms, $is_and) = @_;
212 my $conj = $is_and ? "-and" : "-or";
213 my $outer_clause = {};
215 foreach my $class (qw/acqpo acqpl acqinv jub/) {
216 next if not exists $terms->{$class};
218 $outer_clause->{$conj} = [] unless $outer_clause->{$conj};
219 foreach my $unit (@{$terms->{$class}}) {
220 my ($k, $v, $fuzzy, $between, $not, $castdate) =
221 breakdown_term($unit);
224 if ($fuzzy and not ref $v) {
225 $term_clause = {$k => {"ilike" => "%" . $v . "%"}};
226 } elsif ($between and could_be_range($v)) {
227 $term_clause = {$k => {"between" => $v}};
228 } elsif (check_1d_max($v)) {
229 $v = castdate($v) if $castdate;
230 $term_clause = {$k => $v};
235 my $clause = {"+" . $class => $term_clause};
236 $clause = {"-not" => $clause} if $not;
237 push @{$outer_clause->{$conj}}, $clause;
241 if ($terms->{"acqlia"}) {
242 push @{$outer_clause->{$conj}},
243 $is_and ? prepare_acqlia_search_and($terms->{"acqlia"}) :
244 prepare_acqlia_search_or($terms->{"acqlia"});
247 return undef unless scalar keys %$outer_clause;
255 foreach my $join (@_) {
256 my ($hint, $attr, $num) = @$join;
258 if ($hint eq "jub") {
259 $start = $from->{$hint};
260 } elsif ($hint eq "acqinv") {
261 $start = $from->{"jub"}->{"acqie"}->{"join"}->{$hint};
263 $start = $from->{"jub"}->{$hint};
279 if ($hint eq "jub") {
280 $start->{"au$num"} = $clause;
282 $start->{"join"} ||= {};
283 $start->{"join"}->{"au$num"} = $clause;
290 __PACKAGE__->register_method(
291 method => "unified_search",
292 api_name => "open-ils.acq.lineitem.unified_search",
295 desc => q/Returns lineitems based on flexible search terms./,
297 {desc => "Authentication token", type => "string"},
298 {desc => "Field/value pairs for AND'ing", type => "object"},
299 {desc => "Field/value pairs for OR'ing", type => "object"},
300 {desc => "Conjunction between AND pairs and OR pairs " .
301 "(can be 'and' or 'or')", type => "string"},
302 {desc => "Retrieval options (clear_marc, flesh_notes, etc) " .
303 "- XXX detail all the options",
306 return => {desc => "A stream of LIs on success, Event on failure"}
310 __PACKAGE__->register_method(
311 method => "unified_search",
312 api_name => "open-ils.acq.purchase_order.unified_search",
315 desc => q/Returns purchase orders based on flexible search terms.
316 See open-ils.acq.lineitem.unified_search/,
317 return => {desc => "A stream of POs on success, Event on failure"}
321 __PACKAGE__->register_method(
322 method => "unified_search",
323 api_name => "open-ils.acq.picklist.unified_search",
326 desc => q/Returns pick lists based on flexible search terms.
327 See open-ils.acq.lineitem.unified_search/,
328 return => {desc => "A stream of PLs on success, Event on failure"}
332 __PACKAGE__->register_method(
333 method => "unified_search",
334 api_name => "open-ils.acq.invoice.unified_search",
337 desc => q/Returns invoices lists based on flexible search terms.
338 See open-ils.acq.lineitem.unified_search/,
339 return => {desc => "A stream of invoices on success, Event on failure"}
344 my ($self, $conn, $auth, $and_terms, $or_terms, $conj, $options) = @_;
347 my $e = new_editor("authtoken" => $auth);
348 return $e->die_event unless $e->checkauth;
350 # What kind of object are we returning? Important: (\w+) had better be
351 # a legit acq classname particle, so don't register any crazy api_names.
352 my $ret_type = ($self->api_name =~ /cq.(\w+).un/)[0];
353 my $retriever = $RETRIEVERS{$ret_type};
354 my $hint = F("acq::$ret_type")->{"hint"};
359 [{"column" => "id", "transform" => "distinct"}]
366 "fkey" => "purchase_order"
375 "field" => "lineitem",
387 "order_by" => { $hint => {"id" => {}}},
388 "offset" => ($options->{"offset"} || 0)
391 $query->{"limit"} = $options->{"limit"} if $options->{"limit"};
393 $and_terms = prepare_terms($and_terms, 1);
394 $or_terms = prepare_terms($or_terms, 0) and do {
395 $query->{"from"}->{"jub"}->{"acqlia"} = {
396 "type" => "left", "field" => "lineitem", "fkey" => "id",
400 my $offset = add_au_joins($query->{"from"}, prepare_au_terms($and_terms));
401 add_au_joins($query->{"from"}, prepare_au_terms($or_terms, $offset));
403 if ($and_terms and $or_terms) {
404 $query->{"where"} = {
405 "-" . (lc $conj eq "or" ? "or" : "and") => [$and_terms, $or_terms]
407 } elsif ($and_terms) {
408 $query->{"where"} = $and_terms;
409 } elsif ($or_terms) {
410 $query->{"where"} = $or_terms;
413 return new OpenILS::Event("BAD_PARAMS", "desc" => "No usable terms");
416 my $results = $e->json_query($query) or return $e->die_event;
417 if ($options->{"id_list"}) {
418 foreach (@$results) {
419 $conn->respond($_->{"id"}) if $_->{"id"};
422 foreach (@$results) {
423 $conn->respond($retriever->($e, $_->{"id"}, $options))