Bug 23710: Use API to place holds for patrons
authorAgustin Moyano <agustinmoyano@theke.io>
Tue, 1 Oct 2019 18:26:55 +0000 (15:26 -0300)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Mon, 7 Oct 2019 12:00:16 +0000 (13:00 +0100)
This patch effectively uses API to place holds for patrons. It adds a listener on submit event of the form in javascript, where it calls holds API.

To test:

1. Place a hold on any biblio for a patron
SUCCESS => hold is placed or rejected, but no blank page with JSON error is shown.
2. Place a multi hold for any patron
SUCCESS => holds are placed or rejected, but no blank page with JSON error is shown.
3. Sign off

Signed-off-by: Lisette Scheer <lisetteslatah@gmail.com>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt

index eef3c04..10c8cd7 100644 (file)
                     </div>
                 [% END %]
 
+                <div class="dialog alert hide holdalert">
+                </div>
+
                 <fieldset class="rows">
                     <legend>Hold details</legend>
                     <form action="/api/v1/holds" method="post" name="form" id="hold-request-form">
                                 <li>
                                     <label for="from">Hold starts on date:</label>
                                     <input name="reserve_date" id="from" size="10" class="datepickerfrom" type="text" >
+                                    <input type="hidden" class="datepickerfrom_hidden" />
                                     <a href="#" id="clear-date-from" class="clear-date">Clear date</a>
                                 </li>
                             [% END %]
                             <li>
                                 <label for="to">Hold expires on date:</label>
                                 <input name="expiration_date" id="to" size="10" class="datepickerto" type="text" />
+                                <input type="hidden" class="datepickerto_hidden" />
                                 <a href="#" id="clear-date-to" class="clear-date">Clear date</a>
                             </li>
 
                 $(".suspend_until_datepicker, .datepickerfrom, .datepickerto").datepicker("option", "minDate", 1);
             [% END %]
 
+            $(".datepickerto").datepicker("option", "altField", ".datepickerto_hidden");
+            $(".datepickerto").datepicker("option", "altFormat", "yy-mm-dd");
+
+            $(".datepickerfrom").datepicker("option", "altField", ".datepickerfrom_hidden");
+            $(".datepickerfrom").datepicker("option", "altFormat", "yy-mm-dd");
+
             var my_table = $("#requestspecific").dataTable($.extend(true, {}, dataTablesDefaults, {
                 'bPaginate': false,
                 "sDom": '<"top pager"ilf>t',
                 "margin-right":"0em"
             });
 
-            $("#club-request-form").on("submit", function() {
+            $("#club-request-form, #hold-request-form").on("submit", function() {
                 let $t = $(this);
                 $('.clubalert').addClass('hide');
-                let options = {
-                    url: $t.attr('action'),
-                    method: $t.attr('method').toUpperCase(),
-                    contentType: 'application/json',
-                    data: JSON.stringify({
-                        biblio_id: biblionumber,
-                        pickup_library_id: $('select[name="pickup"]').val()
-                    })
+                let biblionumbers = [biblionumber];
+                let biblionumbers_text;
+                const data = {
+                    pickup_library_id: $('select[name="pickup"]').val()
                 };
                 if($('input[name="checkitem"]:checked').length)
-                    options.data.item_id = $('input[name="checkitem"]:checked').val();
-                $.ajax(options)
-                    .then(function(result) {
-                        let url = 'request.pl?biblionumber='+biblionumber+($('input[name="multi_hold"]').length && $('input[name="multi_hold"]').val()?'&multi_hold=1':'');
-                        document.location = url;
-                    })
-                    .fail(function(err) {
-                        $('.clubalert').removeClass('hide').html(err.responseJSON.error);
-                    });
+                    data.item_id = $('input[name="checkitem"]:checked').val();
+                if($('input[name="borrowernumber"]').length)
+                    data.patron_id = $('input[name="borrowernumber"]').val();
+                if($('textarea[name="notes"]').length)
+                    data.notes = $('textarea[name="notes"]').val()||null;
+                if($('.datepickerto_hidden').length)
+                    data.expiration_date = $('.datepickerto_hidden').val()||null;
+                if($('.datepickerfrom_hidden').length)
+                    data.hold_date = $('.datepickerfrom_hidden').val()||null;
+                if($('input[name="itemtype"]').length) {
+                    data.item_type = $('input[name="itemtype"]').val()||null;
+                }
+                if($('input[name="biblionumbers"]').length) {
+                    biblionumbers_text = $('input[name="biblionumbers"]').val();
+                    biblionumbers = biblionumbers_text.replace(/\/$/, '').split('/')
+                }
+
+                const count = $('input[name="holds_to_place_count"]').length?$('input[name="holds_to_place_count"]').val():1;
+                biblionumbers.forEach(function(biblionumber) {
+                    data.biblio_id = biblionumber;
+                    let options = {
+                        url: $t.attr('action'),
+                        method: $t.attr('method').toUpperCase(),
+                        contentType: 'application/json',
+                        data: JSON.stringify(data)
+                    };
+                    for(let i = 0; i < count; i++) {
+                        $.ajax(options)
+                        .then(function(result) {
+                            let url = 'request.pl?biblionumber='+biblionumber;
+                            if(biblionumbers_text) {
+                                url = 'request.pl?biblionumbers='+biblionumbers_text+'&multi_hold=1';
+                            }
+                            document.location = url;
+                        })
+                        .fail(function(err) {
+                            $('.clubalert, .holdalert').removeClass('hide').html(err.responseJSON.error);
+                        });
+                    }
+                });
+
                 return false;
             });