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;
14 use OpenILS::Application::Acq::Order;
18 \&{"OpenILS::Application::Acq::Lineitem::retrieve_lineitem_impl"},
20 \&{"OpenILS::Application::Acq::Picklist::retrieve_picklist_impl"},
21 "purchase_order" => \&{
22 "OpenILS::Application::Acq::Financials::retrieve_purchase_order_impl"
25 "OpenILS::Application::Acq::Invoice::fetch_invoice_impl"
29 sub F { $Fieldmapper::fieldmap->{"Fieldmapper::" . $_[0]}; }
31 # This subroutine returns 1 if the argument is a) a scalar OR
32 # b) an array of ONLY scalars. Otherwise it returns 0.
35 return 1 unless ref $o;
36 if (ref($o) eq "ARRAY") {
37 foreach (@$o) { return 0 if ref $_; }
43 # Returns 1 if and only if argument is an array of exactly two scalars.
46 if (ref $o eq "ARRAY") {
47 return 1 if (scalar(@$o) == 2 && (!ref $o->[0] && !ref $o->[1]));
53 my ($value, $gte, $lte) = @_;
59 +{$op => {"transform" => "date", "value" => $value}};
62 sub prepare_acqlia_search_and {
66 foreach my $unit (@{$acqlia}) {
68 "select" => {"acqlia" => ["id"]},
70 "where" => {"-and" => [{"lineitem" => {"=" => {"+jub" => "id"}}}]}
73 # castdate not supported for acqlia fields: they're all type text
74 my ($k, $v, $fuzzy, $between, $not) = breakdown_term($unit);
75 my $point = $subquery->{"where"}->{"-and"};
78 push @$point, {"definition" => $k};
80 if ($fuzzy and not ref $v) {
81 push @$point, {"attr_value" => {"ilike" => "%" . $v . "%"}};
82 } elsif ($between and could_be_range($v)) {
83 push @$point, {"attr_value" => {"between" => $v}};
84 } elsif (check_1d_max($v)) {
85 push @$point, {"attr_value" => $v};
90 my $operator = $not ? "-not-exists" : "-exists";
91 push @phrases, {$operator => $subquery};
96 sub prepare_acqlia_search_or {
100 my $result = {"+acqlia" => {"-or" => $point}};
102 foreach my $unit (@$acqlia) {
103 # castdate not supported for acqlia fields: they're all type text
104 my ($k, $v, $fuzzy, $between, $not) = breakdown_term($unit);
106 if ($fuzzy and not ref $v) {
110 "attr_value" => {"ilike" => "%" . $v . "%"}
113 } elsif ($between and could_be_range($v)) {
116 "definition" => $k, "attr_value" => {"between" => $v}
119 } elsif (check_1d_max($v)) {
121 "-and" => {"definition" => $k, "attr_value" => $v}
127 push @$point, $not ? {"-not" => $term_clause} : $term_clause;
135 my $key = (grep { !/^__/ } keys %$term)[0];
138 $term->{"__fuzzy"} ? 1 : 0,
139 $term->{"__between"} ? 1 : 0,
140 $term->{"__not"} ? 1 : 0,
141 $term->{"__castdate"} ? 1 : 0,
142 $term->{"__gte"} ? 1 : 0,
143 $term->{"__lte"} ? 1 : 0
147 sub get_fm_links_by_hint {
149 foreach my $field (values %{$Fieldmapper::fieldmap}) {
150 return $field->{"links"} if $field->{"hint"} eq $hint;
156 my ($value, $n) = @_;
159 {"+au$n" => {"usrname" => $value}},
160 {"+au$n" => {"first_given_name" => $value}},
161 {"+au$n" => {"second_given_name" => $value}},
162 {"+au$n" => {"family_name" => $value}},
163 {"+ac$n" => {"barcode" => $value}}
168 # go through the terms hash, find keys that correspond to fields links
169 # to actor.usr, and rewrite the search as one that searches not by
170 # actor.usr.id but by any of these user properties: card barcode, username,
171 # given names and family name.
172 sub prepare_au_terms {
173 my ($terms, $join_num) = @_;
179 foreach my $conj (qw/-and -or/) {
180 next unless exists $terms->{$conj};
182 my @new_outer_terms = ();
183 HINT_UNIT: foreach my $hint_unit (@{$terms->{$conj}}) {
184 my $hint = (keys %$hint_unit)[0];
185 (my $plain_hint = $hint) =~ y/+//d;
186 if ($hint eq "-not") {
187 $hint_unit = $hint_unit->{$hint};
192 if (my $links = get_fm_links_by_hint($plain_hint) and
193 $plain_hint ne "acqlia") {
195 my ($attr, $value) = breakdown_term($hint_unit->{$hint});
196 if ($links->{$attr} and
197 $links->{$attr}->{"class"} eq "au") {
198 push @joins, [$plain_hint, $attr, $join_num];
199 my $au_term = gen_au_term($value, $join_num);
201 $au_term = {"-not" => $au_term};
204 push @new_outer_terms, $au_term;
206 delete $hint_unit->{$hint};
210 $hint_unit = {"-not" => $hint_unit};
213 push @new_outer_terms, $hint_unit if scalar keys %$hint_unit;
215 $terms->{$conj} = [ @new_outer_terms ];
221 my ($terms, $is_and) = @_;
223 my $conj = $is_and ? "-and" : "-or";
224 my $outer_clause = {};
226 foreach my $class (qw/acqpo acqpl acqinv jub/) {
227 next if not exists $terms->{$class};
229 $outer_clause->{$conj} = [] unless $outer_clause->{$conj};
230 foreach my $unit (@{$terms->{$class}}) {
231 my ($k, $v, $fuzzy, $between, $not, $castdate, $gte, $lte) =
232 breakdown_term($unit);
235 if ($fuzzy and not ref $v) {
236 $term_clause = {$k => {"ilike" => "%" . $v . "%"}};
237 } elsif ($between and could_be_range($v)) {
238 $term_clause = {$k => {"between" => $v}};
239 } elsif (check_1d_max($v)) {
240 $v = castdate($v, $gte, $lte) if $castdate;
241 $term_clause = {$k => $v};
246 my $clause = {"+" . $class => $term_clause};
247 $clause = {"-not" => $clause} if $not;
248 push @{$outer_clause->{$conj}}, $clause;
252 if ($terms->{"acqlia"}) {
253 push @{$outer_clause->{$conj}},
254 $is_and ? prepare_acqlia_search_and($terms->{"acqlia"}) :
255 prepare_acqlia_search_or($terms->{"acqlia"});
258 return undef unless scalar keys %$outer_clause;
266 foreach my $join (@_) {
267 my ($hint, $attr, $num) = @$join;
269 if ($hint eq "jub") {
270 $start = $from->{$hint};
271 } elsif ($hint eq "acqinv") {
272 $start = $from->{"jub"}->{"acqie"}->{"join"}->{$hint};
274 $start = $from->{"jub"}->{$hint};
290 if ($hint eq "jub") {
291 $start->{"au$num"} = $clause;
293 $start->{"join"} ||= {};
294 $start->{"join"}->{"au$num"} = $clause;
301 __PACKAGE__->register_method(
302 method => "unified_search",
303 api_name => "open-ils.acq.lineitem.unified_search",
306 desc => q/Returns lineitems based on flexible search terms./,
308 {desc => "Authentication token", type => "string"},
309 {desc => "Field/value pairs for AND'ing", type => "object"},
310 {desc => "Field/value pairs for OR'ing", type => "object"},
311 {desc => "Conjunction between AND pairs and OR pairs " .
312 "(can be 'and' or 'or')", type => "string"},
313 {desc => "Retrieval options (clear_marc, flesh_notes, etc) " .
314 "- XXX detail all the options",
317 return => {desc => "A stream of LIs on success, Event on failure"}
321 __PACKAGE__->register_method(
322 method => "unified_search",
323 api_name => "open-ils.acq.purchase_order.unified_search",
326 desc => q/Returns purchase orders based on flexible search terms.
327 See open-ils.acq.lineitem.unified_search/,
328 return => {desc => "A stream of POs on success, Event on failure"}
332 __PACKAGE__->register_method(
333 method => "unified_search",
334 api_name => "open-ils.acq.picklist.unified_search",
337 desc => q/Returns pick lists based on flexible search terms.
338 See open-ils.acq.lineitem.unified_search/,
339 return => {desc => "A stream of PLs on success, Event on failure"}
343 __PACKAGE__->register_method(
344 method => "unified_search",
345 api_name => "open-ils.acq.invoice.unified_search",
348 desc => q/Returns invoices lists based on flexible search terms.
349 See open-ils.acq.lineitem.unified_search/,
350 return => {desc => "A stream of invoices on success, Event on failure"}
355 my ($self, $conn, $auth, $and_terms, $or_terms, $conj, $options) = @_;
358 my $e = new_editor("authtoken" => $auth);
359 return $e->die_event unless $e->checkauth;
361 # What kind of object are we returning? Important: (\w+) had better be
362 # a legit acq classname particle, so don't register any crazy api_names.
363 my $ret_type = ($self->api_name =~ /cq.(\w+).un/)[0];
364 my $retriever = $RETRIEVERS{$ret_type};
365 my $hint = F("acq::$ret_type")->{"hint"};
368 "select" => {$hint => [{"column" => "id", "transform" => "distinct"}]},
374 "fkey" => "purchase_order"
383 "field" => "lineitem",
395 "order_by" => {$hint => {"id" => {}}},
396 "offset" => ($options->{"offset"} || 0)
399 $query->{"limit"} = $options->{"limit"} if $options->{"limit"};
401 # XXX for the future? but it doesn't quite work as is.
402 # # Remove anything in temporary picklists from search results.
404 # $and_terms->{"acqpl"} ||= [];
405 # push @{$and_terms->{"acqpl"}}, {"name" => "", "__not" => 1};
407 $and_terms = prepare_terms($and_terms, 1);
408 $or_terms = prepare_terms($or_terms, 0) and do {
409 $query->{"from"}->{"jub"}->{"acqlia"} = {
410 "type" => "left", "field" => "lineitem", "fkey" => "id",
414 my $offset = add_au_joins($query->{"from"}, prepare_au_terms($and_terms));
415 add_au_joins($query->{"from"}, prepare_au_terms($or_terms, $offset));
417 if ($and_terms and $or_terms) {
418 $query->{"where"} = {
419 "-" . (lc $conj eq "or" ? "or" : "and") => [$and_terms, $or_terms]
421 } elsif ($and_terms) {
422 $query->{"where"} = $and_terms;
423 } elsif ($or_terms) {
424 $query->{"where"} = $or_terms;
427 return new OpenILS::Event("BAD_PARAMS", "desc" => "No usable terms");
430 my $results = $e->json_query($query) or return $e->die_event;
431 if ($options->{"id_list"}) {
432 $conn->respond($_->{"id"}) foreach (grep { $_->{"id"} } @$results);
434 $conn->respond($retriever->($e, $_->{"id"}, $options))
435 foreach (grep { $_->{"id"} } @$results);
442 __PACKAGE__->register_method(
443 method => "bib_search",
444 api_name => "open-ils.acq.biblio.wrapped_search",
447 desc => q/Returns new lineitems for each matching bib record/,
449 {desc => "Authentication token", type => "string"},
450 {desc => "search string", type => "string"},
451 {desc => "search options", type => "object"}
453 return => {desc => "A stream of LIs on success, Event on failure"}
457 # This is very similar to zsearch() in Order.pm
459 my ($self, $conn, $auth, $search, $options) = @_;
461 my $e = new_editor("authtoken" => $auth, "xact" => 1);
462 return $e->die_event unless $e->checkauth;
463 return $e->die_event unless $e->allowed("CREATE_PICKLIST");
465 my $mgr = new OpenILS::Application::Acq::BatchManager(
466 "editor" => $e, "conn" => $conn
470 $options->{"limit"} ||= 10;
472 my $ses = create OpenSRF::AppSession("open-ils.search");
473 my $req = $ses->request(
474 "open-ils.search.biblio.multiclass.query.staff", $options, $search
479 while (my $resp = $req->recv("timeout" => 60)) {
480 $picklist = OpenILS::Application::Acq::Order::zsearch_build_pl(
481 $mgr, undef # XXX could have per-user name for temp picklist here?
484 my $result = $resp->content;
486 # The result object contains a whole heck of a lot more information
487 # than just bib IDs, so maybe we could tell the client something
488 # useful (progress meter at least) in the future...
489 foreach my $id (@{$result->{"ids"}}) {
492 $RETRIEVERS{"lineitem"}->(
493 $e, OpenILS::Application::Acq::Order::create_lineitem(
495 "picklist" => $picklist->id,
496 "source_label" => $resp->content->{"service"},
497 "marc" => $e->retrieve_biblio_record_entry($id)->marc,