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