From 2326e5a4cc8651d3a4180c25ed68a3ecf2b5fa84 Mon Sep 17 00:00:00 2001 From: Mike Rylander Date: Wed, 31 May 2017 13:02:21 -0400 Subject: [PATCH] webstaff: add predictions after last item, not last displayed item Signed-off-by: Mike Rylander --- .../js/ui/default/staff/serials/services/core.js | 117 +++++++++++++-------- 1 file changed, 75 insertions(+), 42 deletions(-) diff --git a/Open-ILS/web/js/ui/default/staff/serials/services/core.js b/Open-ILS/web/js/ui/default/staff/serials/services/core.js index 5f057c43bc..615a4596f4 100644 --- a/Open-ILS/web/js/ui/default/staff/serials/services/core.js +++ b/Open-ILS/web/js/ui/default/staff/serials/services/core.js @@ -970,53 +970,86 @@ function(egCore , orderByFilter , $q , $filter , $uibModal , ngToast , egConfirm } service.add_issuances = function (mySsubId) { - var lastItem = service.itemList[service.itemList.length - 1]; - - if (lastItem) lastItem = egCore.idl.fromHash('siss', lastItem.issuance); - - return service.new_holding_code({ - title : egCore.strings.SERIALS_ISSUANCE_PREDICT, - request_count : true, - prev_iss : lastItem - }).then(function(hc) { - - var base_iss; - if (!lastItem) { - base_iss = new egCore.idl.siss(); - base_iss.creator( egCore.auth.user().id() ); - base_iss.editor( egCore.auth.user().id() ); - base_iss.date_published( hc.date.toISOString() ); - base_iss.subscription( mySsubId ); - base_iss.caption_and_pattern( hc.scap ); - base_iss.holding_code( JSON.stringify(hc.holding_code) ); - base_iss.holding_type( hc.type ); - } + if (!mySsubId && service.subId) mySsubId = service.subId; + if (!mySsubId) return $q.reject('fetchItemsForSub: no subscription id'); - // if we're predicting without a preexisting holding, reduce the count - if (!lastItem) hc.count--; + var sub = service.get_ssub(mySsubId); + if (!sub) return $q.reject('fetchItemsForSub: unknown subscription id'); - return egCore.net.request( - 'open-ils.serial', - 'open-ils.serial.make_predictions', - egCore.auth.token(), - { ssub_id : mySsubId, - include_base_issuance : lastItem ? 0 : 1, - num_to_predict : hc.count, - base_issuance : base_iss || lastItem + var streams = []; + angular.forEach(sub.distributions(), function(dist) { + angular.forEach( + dist.streams().map( + function (stream) { return stream.id() } + ), + function (sid) { streams.push(sid) } + ); + }); + + var options = { + order_by : [{class:'sitem',field:'date_expected',direction:'desc'}], // best aprox of pub date + limit : 1, + flesh : 1, + flesh_fields : { sitem : ['issuance'] } + }; + + return egCore.pcrud.search( + 'sitem', {stream:streams}, + { order_by : [{class:'sitem',field:'date_expected',direction:'desc'}], // best aprox of pub date + limit : 1, + flesh : 1, + flesh_fields : { sitem : ['issuance'] } + }, + { atomic : true } + ).then(function(list) { + var lastItem = list[0]; + + if (lastItem) lastItem = lastItem.issuance(); + + return service.new_holding_code({ + title : egCore.strings.SERIALS_ISSUANCE_PREDICT, + request_count : true, + prev_iss : lastItem + }).then(function(hc) { + + var base_iss; + if (!lastItem) { + base_iss = new egCore.idl.siss(); + base_iss.creator( egCore.auth.user().id() ); + base_iss.editor( egCore.auth.user().id() ); + base_iss.date_published( hc.date.toISOString() ); + base_iss.subscription( mySsubId ); + base_iss.caption_and_pattern( hc.scap ); + base_iss.holding_code( JSON.stringify(hc.holding_code) ); + base_iss.holding_type( hc.type ); } - ).then( - function(resp) { - var evt = egCore.evt.parse(resp); - if (evt) { + + // if we're predicting without a preexisting holding, reduce the count + if (!lastItem) hc.count--; + + return egCore.net.request( + 'open-ils.serial', + 'open-ils.serial.make_predictions', + egCore.auth.token(), + { ssub_id : mySsubId, + include_base_issuance : lastItem ? 0 : 1, + num_to_predict : hc.count, + base_issuance : base_iss || lastItem + } + ).then( + function(resp) { + var evt = egCore.evt.parse(resp); + if (evt) { + ngToast.danger(egCore.strings.SERIALS_ISSUANCE_FAIL_SAVE); + } else { + ngToast.success(egCore.strings.SERIALS_ISSUANCE_SUCCESS_SAVE); + } + }, + function(resp) { ngToast.danger(egCore.strings.SERIALS_ISSUANCE_FAIL_SAVE); - } else { - ngToast.success(egCore.strings.SERIALS_ISSUANCE_SUCCESS_SAVE); } - }, - function(resp) { - ngToast.danger(egCore.strings.SERIALS_ISSUANCE_FAIL_SAVE); - } - ); + ); + }); }); } -- 2.11.0