Bug 26988: Add API route to fetch hold pickup locations and use it in the holds table
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / holds.js
1 /* global __ dataTablesDefaults borrowernumber SuspendHoldsIntranet */
2 $(document).ready(function() {
3     var holdsTable;
4
5     // Don't load holds table unless it is clicked on
6     $("#holds-tab").on( "click", function(){ load_holds_table() } );
7
8     // If the holds tab is preselected on load, we need to load the table
9     if ( $("#holds-tab").parent().hasClass('ui-state-active') ) { load_holds_table() }
10
11     function load_holds_table() {
12         var holds = new Array();
13         if ( ! holdsTable ) {
14             var title;
15             holdsTable = $("#holds-table").dataTable($.extend(true, {}, dataTablesDefaults, {
16                 "bAutoWidth": false,
17                 "sDom": "rt",
18                 "columns": [
19                     {
20                         "data": { _: "reservedate_formatted", "sort": "reservedate" }
21                     },
22                     {
23                         "mDataProp": function ( oObj ) {
24                             title = "<a href='/cgi-bin/koha/reserve/request.pl?biblionumber="
25                                   + oObj.biblionumber
26                                   + "'>"
27                                   + oObj.title.escapeHtml();
28
29                             $.each(oObj.subtitle, function( index, value ) {
30                                 title += " " + value.escapeHtml();
31                             });
32
33                             title += " " + oObj.part_number + " " + oObj.part_name;
34
35                             if ( oObj.enumchron ) {
36                                 title += " (" + oObj.enumchron.escapeHtml() + ")";
37                             }
38
39                             title += "</a>";
40
41                             if ( oObj.author ) {
42                                 title += " " + __("by _AUTHOR_").replace("_AUTHOR_", oObj.author.escapeHtml());
43                             }
44
45                             if ( oObj.itemnotes ) {
46                                 var span_class = "";
47                                 if ( $.datepicker.formatDate('yy-mm-dd', new Date(oObj.issuedate) ) == ymd ) {
48                                     span_class = "circ-hlt";
49                                 }
50                                 title += " - <span class='" + span_class + "'>" + oObj.itemnotes.escapeHtml() + "</span>"
51                             }
52
53                             return title;
54                         }
55                     },
56                     {
57                         "mDataProp": function( oObj ) {
58                             return oObj.itemcallnumber && oObj.itemcallnumber.escapeHtml() || "";
59                         }
60                     },
61                     {
62                         "mDataProp": function( oObj ) {
63                             var data = "";
64                             if ( oObj.barcode ) {
65                                 data += " <a href='/cgi-bin/koha/catalogue/moredetail.pl?biblionumber="
66                                   + oObj.biblionumber
67                                   + "&itemnumber="
68                                   + oObj.itemnumber
69                                   + "#item"
70                                   + oObj.itemnumber
71                                   + "'>"
72                                   + oObj.barcode.escapeHtml()
73                                   + "</a>";
74                             }
75                             return data;
76                         }
77                     },
78                     {
79                         "mDataProp": function( oObj ) {
80                             if( oObj.branches.length > 1 && oObj.found !== 'W' && oObj.found !== 'T' ){
81                                 var branchSelect='<select priority='+oObj.priority+' class="hold_location_select" reserve_id="'+oObj.reserve_id+'" name="pick-location">';
82                                 for ( var i=0; i < oObj.branches.length; i++ ){
83                                     var selectedbranch;
84                                     var setbranch;
85                                     if( oObj.branches[i].selected ){
86
87                                         selectedbranch = " selected='selected' ";
88                                         setbranch = __(" (current) ");
89                                     } else if ( oObj.branches[i].pickup_location == 0 ) {
90                                         continue;
91                                     } else{
92                                         selectedbranch = '';
93                                         setbranch = '';
94                                     }
95                                     branchSelect += '<option value="'+ oObj.branches[i].branchcode.escapeHtml() +'"'+selectedbranch+'>'+oObj.branches[i].branchname.escapeHtml()+setbranch+'</option>';
96                                 }
97                                 branchSelect +='</select>';
98                                 return branchSelect;
99                             }
100                             else { return oObj.branchcode.escapeHtml() || ""; }
101                         }
102                     },
103                     { "data": { _: "expirationdate_formatted", "sort": "expirationdate" } },
104                     {
105                         "mDataProp": function( oObj ) {
106                             if ( oObj.priority && parseInt( oObj.priority ) && parseInt( oObj.priority ) > 0 ) {
107                                 return oObj.priority;
108                             } else {
109                                 return "";
110                             }
111                         }
112                     },
113                     {
114                         "bSortable": false,
115                         "mDataProp": function( oObj ) {
116                             return "<select name='rank-request'>"
117                                  +"<option value='n'>" + __("No") + "</option>"
118                                  +"<option value='del'>" + __("Yes") + "</option>"
119                                  + "</select>"
120                                  + "<input type='hidden' name='biblionumber' value='" + oObj.biblionumber + "'>"
121                                  + "<input type='hidden' name='borrowernumber' value='" + borrowernumber + "'>"
122                                  + "<input type='hidden' name='reserve_id' value='" + oObj.reserve_id + "'>";
123                         }
124                     },
125                     {
126                         "bSortable": false,
127                         "visible": SuspendHoldsIntranet,
128                         "mDataProp": function( oObj ) {
129                             holds[oObj.reserve_id] = oObj; //Store holds for later use
130
131                             if ( oObj.found ) {
132                                 return "";
133                             } else if ( oObj.suspend == 1 ) {
134                                 return "<a class='hold-resume btn btn-default btn-xs' id='resume" + oObj.reserve_id + "'>"
135                                      +"<i class='fa fa-play'></i> " + __("Resume") + "</a>";
136                             } else {
137                                 return "<a class='hold-suspend btn btn-default btn-xs' id='suspend" + oObj.reserve_id + "'>"
138                                      +"<i class='fa fa-pause'></i> " + __("Suspend") + "</a>";
139                             }
140                         }
141                     },
142                     {
143                         "mDataProp": function( oObj ) {
144                             var data = "";
145
146                             if ( oObj.suspend == 1 ) {
147                                 data += "<p>" + __("Hold is <strong>suspended</strong>");
148                                 if ( oObj.suspend_until ) {
149                                     data += " " + __("until %s").format(oObj.suspend_until_formatted);
150                                 }
151                                 data += "</p>";
152                             }
153
154                             if ( oObj.itemtype_limit ) {
155                                 data += __("Next available %s item").format(oObj.itemtype_limit);
156                             }
157
158                             if ( oObj.barcode ) {
159                                 data += "<em>";
160                                 if ( oObj.found == "W" ) {
161
162                                     if ( oObj.waiting_here ) {
163                                         data += __("Item is <strong>waiting here</strong>");
164                                         if (oObj.desk_name) {
165                                             data += ", " + __("at %s").format(oObj.desk_name.escapeHtml());
166                                         }
167                                     } else {
168                                         data += __("Item is <strong>waiting</strong>");
169                                         data += " " + __("at %s").format(oObj.waiting_at);
170                                         if (oObj.desk_name) {
171                                             data += ", " + __("at %s").format(oObj.desk_name.escapeHtml());
172                                         }
173
174                                     }
175
176                                 } else if ( oObj.transferred ) {
177                                     data += __("Item is <strong>in transit</strong> from %s since %s").format(oObj.from_branch, oObj.date_sent);
178                                 } else if ( oObj.not_transferred ) {
179                                     data += __("Item hasn't been transferred yet from %s").format(oObj.not_transferred_by);
180                                 }
181                                 data += "</em>";
182                             }
183                             return data;
184                         }
185                     }
186                 ],
187                 "bPaginate": false,
188                 "bProcessing": true,
189                 "bServerSide": false,
190                 "ajax": {
191                     "url": '/cgi-bin/koha/svc/holds',
192                     "data": function ( d ) {
193                         d.borrowernumber = borrowernumber;
194                     }
195                 },
196             }));
197
198             $('#holds-table').on( 'draw.dt', function () {
199                 $(".hold-suspend").on( "click", function() {
200                     var id = $(this).attr("id").replace("suspend", "");
201                     var hold = holds[id];
202                     $("#suspend-modal-title").html( hold.title );
203                     $("#suspend-modal-reserve_id").val( hold.reserve_id );
204                     $('#suspend-modal').modal('show');
205                 });
206
207                 $(".hold-resume").on( "click", function() {
208                     var id = $(this).attr("id").replace("resume", "");
209                     var hold = holds[id];
210                     $.post('/cgi-bin/koha/svc/hold/resume', { "reserve_id": hold.reserve_id }, function( data ){
211                       if ( data.success ) {
212                           holdsTable.api().ajax.reload();
213                       } else {
214                         if ( data.error == "HOLD_NOT_FOUND" ) {
215                             alert( __("Unable to resume, hold not found") );
216                             holdsTable.api().ajax.reload();
217                         }
218                       }
219                     });
220                 });
221
222                 $(".hold_location_select").change(function(){
223                     $(this).prop("disabled",true);
224                     var cur_select = $(this);
225                     var res_id = $(this).attr('reserve_id');
226                     $(this).after('<div id="updating_reserveno'+res_id+'" class="waiting"><img src="/intranet-tmpl/prog/img/spinner-small.gif" alt="" /><span class="waiting_msg"></span></div>');
227                     var api_url = '/api/v1/holds/'+res_id;
228                     var update_info = JSON.stringify({ pickup_library_id: $(this).val(), priority: parseInt($(this).attr("priority"),10) });
229                     $.ajax({
230                         method: "PUT",
231                         url: api_url,
232                         data: update_info ,
233                         success: function( data ){ holdsTable.api().ajax.reload(); },
234                         error: function( jqXHR, textStatus, errorThrown) {
235                             alert('There was an error:'+textStatus+" "+errorThrown);
236                             cur_select.prop("disabled",false);
237                             $("#updating_reserveno"+res_id).remove();
238                             cur_select.val( cur_select.children('option[selected="selected"]').val() );
239                         },
240                     });
241                 });
242
243             });
244
245             if ( $("#holds-table").length ) {
246                 $("#holds-table_processing").position({
247                     of: $( "#holds-table" ),
248                     collision: "none"
249                 });
250             }
251         }
252     }
253
254     $("body").append("\
255         <div id='suspend-modal' class='modal fade' role='dialog' aria-hidden='true'>\
256             <div class='modal-dialog'>\
257             <div class='modal-content'>\
258             <form id='suspend-modal-form' class='form-inline'>\
259                 <div class='modal-header'>\
260                     <button type='button' class='closebtn' data-dismiss='modal' aria-hidden='true'>×</button>\
261                     <h3 id='suspend-modal-label'>" + __("Suspend hold on") + " <i><span id='suspend-modal-title'></span></i></h3>\
262                 </div>\
263 \
264                 <div class='modal-body'>\
265                     <input type='hidden' id='suspend-modal-reserve_id' name='reserve_id' />\
266 \
267                     <label for='suspend-modal-until'>" + __("Suspend until:") + "</label>\
268                     <input name='suspend_until' id='suspend-modal-until' class='suspend-until' size='10' />\
269 \
270                     <p><a class='btn btn-link' id='suspend-modal-clear-date' >" + __("Clear date to suspend indefinitely") + "</a></p>\
271 \
272                 </div>\
273 \
274                 <div class='modal-footer'>\
275                     <button id='suspend-modal-submit' class='btn btn-primary' type='submit' name='submit'>" + __("Suspend") + "</button>\
276                     <a href='#' data-dismiss='modal' aria-hidden='true' class='cancel'>" + __("Cancel") + "</a>\
277                 </div>\
278             </form>\
279             </div>\
280             </div>\
281         </div>\
282     ");
283
284     $("#suspend-modal-until").datepicker({ minDate: 1 }); // Require that "until date" be in the future
285     $("#suspend-modal-clear-date").on( "click", function() { $("#suspend-modal-until").val(""); } );
286
287     $("#suspend-modal-submit").on( "click", function( e ) {
288         e.preventDefault();
289         $.post('/cgi-bin/koha/svc/hold/suspend', $('#suspend-modal-form').serialize(), function( data ){
290           $('#suspend-modal').modal('hide');
291           if ( data.success ) {
292               holdsTable.api().ajax.reload();
293           } else {
294             if ( data.error == "INVALID_DATE" ) {
295                 alert( __("Unable to suspend hold, invalid date") );
296             }
297             else if ( data.error == "HOLD_NOT_FOUND" ) {
298                 alert( __("Unable to suspend hold, hold not found") );
299                 holdsTable.api().ajax.reload();
300             }
301           }
302         });
303     });
304
305     $(".pickup_location_dropdown").on( "click",function(){
306         var this_dropdown = $(this);
307         if(this_dropdown.data('loaded')===1){ return true};
308         var hold_id = $(this).data('hold_id');
309         $(".loading_"+hold_id).show();
310         var preselected = $(this).data('selected');
311         var api_url = '/api/v1/holds/'+hold_id+'/pickup_locations';
312         $.ajax({
313             method: "GET",
314             url: api_url,
315             success: function( data ){
316                 var dropdown = "";
317                 $.each(data, function(index,library) {
318                     if( preselected == library.branchcode ){
319                         selected = ' selected="selected" ';
320                     } else { selected = ""; }
321                     dropdown += '<option value="'+library.branchcode+'"'+selected+'>'+library.branchname+'</option>';
322                 });
323                 this_dropdown.html( dropdown);
324                 this_dropdown.data("loaded",1);
325                 $(".loading_"+hold_id).hide();
326             },
327             error: function( jqXHR, textStatus, errorThrown) {
328                 alert('There was an error:'+textStatus+" "+errorThrown);
329                 $(".loading_"+hold_id).hide();
330             },
331         });
332     });
333
334
335 });