Sort POs by title by default; lineitem search cleanup
authorBill Erickson <berick@esilibrary.com>
Fri, 26 Aug 2011 14:56:55 +0000 (10:56 -0400)
committerBill Erickson <berick@esilibrary.com>
Fri, 26 Aug 2011 15:12:13 +0000 (11:12 -0400)
* Added lineitem attribute sorting to open-ils.acq.lineitem.search

* Pillaged and removed open-ils.acq.lineitem.picklist.retrieve.  Code
now uses open-ils.acq.lineitem.search instead.

* PO's now sort by title by default since that's the default sort for
open-ils.acq.lineitem.search

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Lineitem.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Picklist.pm
Open-ILS/web/js/ui/default/acq/picklist/view.js
Open-ILS/web/js/ui/default/acq/po/view_po.js

index c36bf60..f328abd 100644 (file)
@@ -306,34 +306,93 @@ sub update_lineitem_impl {
 
 __PACKAGE__->register_method(
        method => 'lineitem_search',
-       api_name => 'open-ils.acq.lineitem.search',
+       api_name        => 'open-ils.acq.lineitem.search',
     stream => 1,
+    authoritative => 1,
        signature => {
         desc => 'Searches lineitems',
         params => [
-            {desc => 'Authentication token',       type => 'string'},
-            {desc => 'Search definition',          type => 'object'},
-            {desc => 'Options hash.  idlist=true', type => 'object'},
-            {desc => 'List of lineitems',          type => 'object/number'},
-        ]
+            {desc => 'Authentication token', type => 'string'},
+            {desc => q/
+                Search hash.  This will be the WHERE component {"+jub": {...}} of a json_query 
+                E.g. { "purchase_order" : 123 } /, 
+                type => 'hash'},
+            {desc => q/Options, including 
+                "sort_attr", which defines the attribute to sort on; 
+                "sort_attr_type", which defines the attribute type sort on; 
+                "sort_dir", which defines the sort order between "asc" and "desc";
+                "limit", retrieval limit;
+                "offset", retrieval offset;
+                "idlist", return a list of IDs instead of objects
+                "flesh_attrs", additionaly return the list of flattened attributes
+                "clear_marc", discards the raw MARC data to reduce data size
+                "flesh_notes", flesh lineitem notes
+                "flesh_cancel_reason", flesh cancel_reason
+                /, 
+                type => 'hash'}
+        ],
+        return => {desc => 'Array of lineitem objects or IDs,  on success, Event on error'}
     }
 );
 
+my $LI_SEARCH_JSON_QUERY = {
+    select => {jub => ['id'], 'acqlia' => ['attr_value']},
+    from => {
+        jub => {
+            acqlia => {
+                fkey => 'id', 
+                field => 'lineitem', 
+                type => 'left', 
+                filter => {
+                    attr_type => 'lineitem_marc_attr_definition', 
+                    attr_name => 'title' 
+                }
+            }
+        }
+    }, 
+    where => {'+jub' => {}},
+    order_by => {acqlia => {attr_value => {direction => 'asc'}}}, 
+    distinct => 1,
+    limit => 10,
+    offset => 0
+};
+
 sub lineitem_search {
-    my($self, $conn, $auth, $search, $options) = @_;
+    my($self, $conn, $auth, $where, $options) = @_;
+
     my $e = new_editor(authtoken=>$auth);
     return $e->event unless $e->checkauth;
-    return $e->event unless $e->allowed('CREATE_PICKLIST');
-    # XXX needs permissions consideration
-    my $lis = $e->search_acq_lineitem($search, {idlist=>1});
-    for my $li_id (@$lis) {
-        if($$options{idlist}) {
-            $conn->respond($li_id);
+    # Permissions are checked per-lineitem in retrieve_lineitem()
+
+    return undef unless $where;
+
+    my $sort_attr = $$options{sort_attr} || 'title';
+    my $sort_attr_type = $$options{sort_attr_type} || 'lineitem_marc_attr_definition';
+    my $sort_dir = $$options{sort_dir} || 'asc';
+    my $offset = $$options{offset} || 0;
+    my $limit = $$options{limit} || 10;
+
+    $LI_SEARCH_JSON_QUERY->{where}->{'+jub'} = $where;
+    $LI_SEARCH_JSON_QUERY->{from}->{jub}->{acqlia}->{filter}->{attr_name} = $sort_attr;
+    $LI_SEARCH_JSON_QUERY->{from}->{jub}->{acqlia}->{filter}->{attr_type} = $sort_attr_type;
+    $LI_SEARCH_JSON_QUERY->{order_by}->{acqlia}->{attr_value}->{direction} = $sort_dir;
+    $LI_SEARCH_JSON_QUERY->{limit} = $limit;
+    $LI_SEARCH_JSON_QUERY->{offset} = $offset;
+
+    my $li_ids = $e->json_query($LI_SEARCH_JSON_QUERY);
+
+    for my $li_id ( map {$_->{id}} @$li_ids ) {
+        my $res = retrieve_lineitem_impl($e, $li_id, $options);
+
+        if( my $ecode = $U->event_code($res) ) {
+            next if $ecode == 5000;  # PERM_FAILURE, return nothing
         } else {
-            my $res = retrieve_lineitem($self, $conn, $auth, $li_id, $options);
-            $conn->respond($res) unless $U->event_code($res);
+            $res = $li_id if $$options{idlist};
         }
+
+        $conn->respond($res);
     }
+
     return undef;
 }
 
index eee578f..4b96d13 100644 (file)
@@ -279,115 +279,6 @@ sub retrieve_all_user_picklist {
 }
 
 __PACKAGE__->register_method(
-       method => 'retrieve_pl_lineitem',
-       api_name        => 'open-ils.acq.lineitem.picklist.retrieve',
-    stream => 1,
-       signature => {
-        desc => 'Retrieves lineitem objects according to picklist',
-        params => [
-            {desc => 'Authentication token', type => 'string'},
-            {desc => 'Picklist ID whose entries to retrieve', type => 'number'},
-            {desc => q/Options, including 
-                "sort_attr", which defines the attribute to sort on; 
-                "sort_attr_type", which defines the attribute type sort on; 
-                "sort_dir", which defines the sort order between "asc" and "desc";
-                "limit", retrieval limit;
-                "offset", retrieval offset;
-                "idlist", return a list of IDs instead of objects
-                "flesh_attrs", additionaly return the list of flattened attributes
-                "clear_marc", discards the raw MARC data to reduce data size
-                "flesh_notes", flesh lineitem notes
-                "flesh_cancel_reason", flesh cancel_reason
-                /, 
-                type => 'hash'}
-        ],
-        return => {desc => 'Array of lineitem objects or IDs,  on success, Event on error'}
-    }
-);
-
-
-my $PL_ENTRY_JSON_QUERY = {
-    select => {jub => ["id"], "acqlia" => ["attr_value"]},
-    "from" => {
-        "jub" => {
-            "acqlia" => {
-                "fkey" => "id", 
-                "field" => "lineitem", 
-                "type" => "left", 
-                "filter" => {
-                    "attr_type" => "lineitem_marc_attr_definition", 
-                    "attr_name" => "author" 
-                }
-            }
-        }
-    }, 
-    "order_by" => {"acqlia" => {"attr_value" => {"direction"=>"asc"}}}, 
-    "limit" => 10,
-    "where" => {"+jub" => {"picklist"=>2}},
-    "offset" => 0
-};
-
-sub retrieve_pl_lineitem {
-    my($self, $conn, $auth, $picklist_id, $options) = @_;
-    my $e = new_editor(authtoken=>$auth);
-    return $e->event unless $e->checkauth;
-
-    # collect the retrieval options
-    my $sort_attr = $$options{sort_attr} || 'title';
-    my $sort_attr_type = $$options{sort_attr_type} || 'lineitem_marc_attr_definition';
-    my $sort_dir = $$options{sort_dir} || 'asc';
-    my $limit = $$options{limit} || 10;
-    my $offset = $$options{offset} || 0;
-
-    $PL_ENTRY_JSON_QUERY->{where}->{'+jub'}->{picklist} = $picklist_id;
-    $PL_ENTRY_JSON_QUERY->{from}->{jub}->{acqlia}->{filter}->{attr_name} = $sort_attr;
-    $PL_ENTRY_JSON_QUERY->{from}->{jub}->{acqlia}->{filter}->{attr_type} = $sort_attr_type;
-    $PL_ENTRY_JSON_QUERY->{order_by}->{acqlia}->{attr_value}->{direction} = $sort_dir;
-    $PL_ENTRY_JSON_QUERY->{limit} = $limit;
-    $PL_ENTRY_JSON_QUERY->{offset} = $offset;
-
-    my $entries = $e->json_query($PL_ENTRY_JSON_QUERY);
-
-    my @ids;
-    for my $entry (@$entries) {
-        push(@ids, $entry->{id}) unless grep { $_ eq $entry->{id} } @ids;
-    }
-
-    for my $id (@ids) {
-        if($$options{idlist}) {
-            $conn->respond($id);
-            next;
-        } 
-
-        my $entry;
-        my $flesh = {};
-        if($$options{flesh_attrs} or $$options{flesh_notes} or $$options{flesh_cancel_reason}) {
-            $flesh = {flesh => 2, flesh_fields => {jub => []}};
-            if($$options{flesh_notes}) {
-                push(@{$flesh->{flesh_fields}->{jub}}, 'lineitem_notes');
-                $flesh->{flesh_fields}->{acqlin} = ['alert_text'];
-            }
-            push(@{$flesh->{flesh_fields}->{jub}}, 'attributes') if $$options{flesh_attrs};
-            push @{$flesh->{flesh_fields}->{jub}}, 'cancel_reason' if $$options{flesh_cancel_reason};
-        }
-
-        $entry = $e->retrieve_acq_lineitem([$id, $flesh]);
-        my $details = $e->search_acq_lineitem_detail({lineitem => $id}, {idlist=>1});
-        $entry->item_count(scalar(@$details));
-        $entry->clear_marc if $$options{clear_marc};
-        $conn->respond($entry);
-    }
-
-    return undef;
-}
-
-=head comment
-request open-ils.cstore open-ils.cstore.json_query.atomic {"select":{"jub":[{"transform":"count", "attregate":1, "column":"id","alias":"count"}]}, "from":"jub","where":{"picklist":1}}
-=cut
-
-
-
-__PACKAGE__->register_method(
     method    => "record_distribution_formula_application",
     api_name  => "open-ils.acq.distribution_formula.record_application",
     signature => {
index 7cbc0c5..b6688d7 100644 (file)
@@ -75,9 +75,9 @@ function loadLIs() {
 
 
     fieldmapper.standardRequest(
-        ['open-ils.acq', 'open-ils.acq.lineitem.picklist.retrieve'],
+        ['open-ils.acq', 'open-ils.acq.lineitem.search'],
         {   async: true,
-            params: [openils.User.authtoken, plId
+            params: [openils.User.authtoken, {picklist : plId}
                 {flesh_notes:true, flesh_cancel_reason:true, flesh_attrs:true, clear_marc:true, offset:plOffset, limit:plLimit}],
             onresponse: function(r) {
                 var li = openils.Util.readResponse(r);
index 8e510f9..5738f52 100644 (file)
@@ -396,7 +396,7 @@ function init() {
         {   async: true,
             params: [
                 openils.User.authtoken, 
-                [{purchase_order:poId}, {"order_by": {"jub": "id ASC"}}]
+                {purchase_order : poId}
                 {flesh_attrs:true, flesh_notes:true, flesh_cancel_reason:true, clear_marc:true}
             ],
             onresponse: function(r) {