webstaff: add predictions after last item, not last displayed item
authorMike Rylander <mrylander@gmail.com>
Wed, 31 May 2017 17:02:21 +0000 (13:02 -0400)
committerMike Rylander <mrylander@gmail.com>
Wed, 31 May 2017 17:02:21 +0000 (13:02 -0400)
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Open-ILS/web/js/ui/default/staff/serials/services/core.js

index 5f057c4..615a459 100644 (file)
@@ -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);
-                }
-            );
+                );
+            });
         });
     }