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