Bug 33864: Trigger the 'change' event when setting quantity
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / modules / acqui / orderreceive.tt
1 [% USE raw %]
2 [% USE Asset %]
3 [% USE Koha %]
4 [% USE KohaDates %]
5 [% USE Branches %]
6 [% USE AuthorisedValues %]
7 [% USE ItemTypes %]
8 [% USE Price %]
9 [% SET footerjs = 1 %]
10 [% INCLUDE 'doc-head-open.inc' %]
11 <title>Receipt summary for : [% name | html %] [% IF ( invoice ) %]invoice, [% invoice | html %][% END %] &rsaquo; Acquisitions &rsaquo; Koha</title>
12 [% INCLUDE 'doc-head-close.inc' %]
13     <style>
14         @media (min-width: 1200px) {
15             #receive-modal, #receive-modal {
16                 width: 98%;
17             }
18         }
19         #receive-modal .modal-body {
20             background-color: #F3F4F4;
21         }
22
23         .lbl {
24             font-weight: 700;
25             text-align: right;
26         }
27         .modal-body ol {
28             list-style-type: none;
29         }
30
31         .modal-body ol li {
32             list-style-type: none;
33             padding-bottom: 1em;
34             width: 100%;
35         }
36
37         .modal-body h4 {
38             margin-left: 1em;
39             padding: .2em .5em;
40             margin-bottom: 17px;
41         }
42
43         .modal-body label {
44             margin-right: 1em;
45             text-align: right;
46             width: 11em;
47             display: inline-block;
48             font-weight: 700;
49         }
50
51         .modal-body textarea {
52             vertical-align: text-top;
53         }
54
55         .modal-header {
56             display: flex;
57             justify-content: space-between;
58         }
59
60         .modal-header::before,
61         .modal-header::after {
62             content: none;
63         }
64
65     </style>
66 </head>
67
68 <body id="acq_orderreceive" class="acq">
69 [% WRAPPER 'header.inc' %]
70     [% INCLUDE 'acquisitions-search.inc' %]
71 [% END %]
72
73 [% WRAPPER 'sub-header.inc' %]
74     [% WRAPPER breadcrumbs %]
75         [% WRAPPER breadcrumb_item %]
76             <a href="/cgi-bin/koha/acqui/acqui-home.pl">Acquisitions</a>
77         [% END %]
78         [% WRAPPER breadcrumb_item %]
79             <a href="/cgi-bin/koha/acqui/supplier.pl?booksellerid=[% vendor.id | uri %]">[% name | html %]</a>
80         [% END %]
81         [% WRAPPER breadcrumb_item bc_active= 1 %]
82             [% IF ( invoice ) %]
83                 <span>Receive items from: [% name | html %] [[% invoice | html %]] (order #[% multiple_orders | html %])</span>
84             [% ELSE %]
85                 <span>Receive items from: [% name | html %] (order #[% multiple_orders | html %])</span>
86             [% END %]
87         [% END %]
88     [% END #/ WRAPPER breadcrumbs %]
89 [% END #/ WRAPPER sub-header.inc %]
90
91 <div class="main container-fluid">
92     <div class="row">
93         <div class="col-sm-10 col-sm-push-2">
94             <main>
95
96 <h1>Receive items from : [% name | html %] [% IF ( invoice ) %][[% invoice | html %]] [% END %] (order #[% multiple_orders | html %])</h1>
97 [% SET only_one_order = ( multiple_orders.split(',').size == 1 ) %]
98 [% IF multiple_orders %]
99     <table id="multiple_orders" class="table table-bordered table-striped">
100         <thead>
101             <tr>
102                 <th>Order</th>
103                 <th>Title</th>
104                 <th>Author</th>
105                 <th>ISBN</th>
106                 <th>Date received</th>
107                 <th>Fund</th>
108                 <th>Quantity</th>
109                 <th>&nbsp;</th>
110             </tr>
111         </thead>
112     </table>
113
114     <div class="col">
115         <fieldset class="action">
116             <button class="save btn btn-primary" disabled>Confirm</button>
117             <a class="cancel btn btn-default" href="/cgi-bin/koha/acqui/parcel.pl?invoiceid=[% invoiceid | html %]&sticky_filters=1">Cancel</a>
118         </fieldset>
119         <div id="jobpanel">
120             <div id="jobstatus" class="progress_panel">Job progress: <div id="jobprogress"></div> <span id="jobprogresspercent">0</span>%</div>
121         </div>
122     </div>
123
124     <div class="modal fade" id="order_edit" tabindex="-1" role="dialog" aria-labelledby="Order edit">
125         <div id="receive-modal" class="modal-dialog modal-lg" role="document">
126             <div class="modal-content">
127                 <div class="modal-header">
128                     <h4 class="col-md-10 modal-title"></h4>
129                     <button type="button" class="close col-md-1" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
130                 </div>
131                 <div class="modal-body">
132                     <div class="hide" id="loading">Loading ...</div>
133                     <div id="modal-order-main" class="row">
134                         <!--ul>
135                             <li><a href="#info-panel">Info</a></li>
136                             <li><a href="#accounting-panel">Accounting</a></li>
137                             <li><a href="#history-panel">Receipt history</a></li>
138                             <li><a href="#items-panel">Items</a></li>
139                         </ul-->
140
141                         <div class="col-md-12 col-lg-6">
142                             <div id="info-panel" class="page-section">
143                                 <div>
144                                     <h4>Catalog details</h4>
145
146                                     <div class="row"> <span class="lbl col-sm-4">Title: </span><span class="col-sm-8" id="biblio_title"></span></div>
147                                     <div class="row"> <span class="lbl col-sm-4">Author: </span><span class="col-sm-8" id="biblio_author"></span></div>
148                                     <div class="row"> <span class="lbl col-sm-4">Copyright: </span><span class="col-sm-8" id="biblio_copyright_date"></span></div>
149                                     <div class="row"> <span class="lbl col-sm-4">ISBN: </span><span class="col-sm-8" id="biblio_isbn"></span></div>
150                                     <div class="row"> <span class="lbl col-sm-4">Series: </span><span class="col-sm-8" id="biblio_series_title"></span></div>
151
152                                 </div>
153                                 <div id="suggestion_fieldset">
154                                     <h4>Suggestion</h4>
155                                     <div class="row">
156                                         <span class="lbl col-sm-4">Suggested by: </span> <span class="col-sm-8" id="biblio_suggestion_suggester"></span>
157                                     </div>
158                                     <div class="row" id="suggestion_reason">
159                                         <span class="lbl col-sm-4">Reason:</span>
160                                         <div class="col-sm-8">
161                                             [% SET suggestion_reasons = AuthorisedValues.GetAuthValueDropbox( 'SUGGEST' ) %]
162                                             <select class="select-reason" id="reason" name="reason">
163                                                 <option value=""> -- Choose a reason -- </option>
164                                                 [% FOREACH reason IN suggestion_reasons %]
165                                                 <option value="[% reason.lib | html %]">[% reason.lib | html %]</option>
166                                                 [% END %]
167                                                 <option value="other">Others...</option>
168                                             </select>
169
170                                             <span id="other_reason" name="other_reason">
171                                                 <input type="text" size="31" id="select-other_reason" name="other_reason" placeholder="please note your reason here..." />
172                                                 <a href="#back">Cancel</a>
173                                             </span>
174                                         </div>
175                                     </div>
176                                 </div>
177                             </div>
178                             <div id="history-panel" class="page-section">
179                                 <div id="child_orders">
180                                     <h4>Receipt history for this subscription</h4>
181                                     <table id="child_orders_table">
182                                         <thead>
183                                             <tr>
184                                                 <th>Invoice</th>
185                                                 <th>Order number</th>
186                                                 <th>Creation date</th>
187                                                 <th>Receive date</th>
188                                                 <th>Quantity received</th>
189                                                 <th>Status</th>
190                                                 <th title="Actual cost tax exc. / Actual cost tax inc.">Spent</th>
191                                                 <th>Internal note</th>
192                                             </tr>
193                                         </thead>
194                                     </table>
195                                 </div>
196                             </div>
197                             <div id="items-panel" class="page-section">
198                                 <div id="acq-create-receiving">
199                                     <div id="items_list" style="display: none">
200                                         <h4>Items list</h4>
201                                         <div style="width:100%;overflow:auto;">
202                                             <table>
203                                                 <thead>
204                                                     <tr>
205                                                         <th>Actions</th>
206                                                         <th>Barcode</th>
207                                                         <th>Home library</th>
208                                                         <th>Holding library</th>
209                                                         <th>Not for loan</th>
210                                                         <th>Restricted</th>
211                                                         <th>Location</th>
212                                                         <th>Call number</th>
213                                                         <th>Copy number</th>
214                                                         <th>Inventory number</th>
215                                                         <th>Collection</th>
216                                                         <th>Item type</th>
217                                                         <th>Materials</th>
218                                                         <th>Notes</th>
219                                                     </tr>
220                                                 </thead>
221                                                 <tbody>
222                                                 </tbody>
223                                             </table>
224                                         </div>
225                                     </div>
226
227                                     <div id="itemfieldset">
228                                         <h4>Item</h4>
229                                         [% IF ( NoACQframework ) %]
230                                             <p class="required">
231                                                 No ACQ framework, using default. You should create a
232                                                 framework with code ACQ, the items framework would be
233                                                 used
234                                             </p>
235                                         [% END %]
236                                         <div id="outeritemblock"></div>
237                                     </div>
238                                 </div>
239                                 <div id="acq-create-ordering">
240                                     <h4>Items</h4>
241                                     <div style="width:100%;overflow:auto">
242                                         <table>
243                                             <thead>
244                                                 <tr>
245                                                     <th>Receive?</th>
246                                                     <th>&nbsp;</th>
247                                                     <th>Barcode</th>
248                                                     <th>Home library</th>
249                                                     <th>Current library</th>
250                                                     <th>Not for loan</th>
251                                                     <th>Restricted</th>
252                                                     <th>Location</th>
253                                                     <th>Call number</th>
254                                                     <th>Copy number</th>
255                                                     <th>Inventory number</th>
256                                                     <th>Collection</th>
257                                                     <th>Item type</th>
258                                                     <th>Materials</th>
259                                                     <th>Notes</th>
260                                                 </tr>
261                                             </thead>
262                                             <tbody>
263                                             </tbody>
264                                         </table>
265                                     </div>
266                                 </div>
267                             </div>
268                         </div>
269                         <div class="col-md-12 col-lg-6">
270                             <div id="accounting-panel" class="page-section">
271                                 <h4>Accounting details</h4>
272                                 <ol>
273                                     <li>
274                                         <label for="datereceived">Date received: </label>
275                                         <input type="text" size="10" id="datereceived" name="datereceived" class="flatpickr" />
276                                     </li>
277                                     <li>
278                                         <label for="bookfund" class="required">Fund: </label>
279                                         <select id="bookfund" name="bookfund">
280                                             <optgroup label="Current">
281                                                 <option id="selected_bookfund" selected="selected"></option>
282                                             </optgroup>
283                                         [% FOREACH budget_period_id IN budget_loops.keys %]
284                                             [% SET budget_period = budget_loops.$budget_period_id %]
285                                             [% IF budget_period.active %]
286                                                 <optgroup label="[% budget_period.description | html %]">
287                                             [% ELSE %]
288                                                 <optgroup class="inactive_budget" label="[% budget_period.description | html %] (Inactive)">
289                                             [% END %]
290                                             [% FOREACH budget_loo IN budget_period.funds %]
291                                             [% level_indent_cnt = 0 %]
292                                                 [% level_indent = "" %]
293                                                 [% WHILE level_indent_cnt < budget_loo.b_level %]
294                                                     [% level_indent = level_indent _ " -- " %]
295                                                     [% level_indent_cnt = level_indent_cnt +1 %]
296                                                 [% END %]
297
298                                                 [% IF ( budget_loo.b_sel ) %]
299                                                     [% active_count = 0 #select no other fund %]
300                                                     <option value="[% budget_loo.b_id | html %]" selected="selected" data-sort1-authcat="[% budget_loo.b_sort1_authcat | html %]" data-sort2-authcat="[% budget_loo.b_sort2_authcat | html %]">
301                                                 [% ELSIF active_count==1 && budget_loo.b_active %]
302                                                     <option value="[% budget_loo.b_id | html %]" selected="selected" data-sort1-authcat="[% budget_loo.b_sort1_authcat | html %]" data-sort2-authcat="[% budget_loo.b_sort2_authcat | html %]">
303                                                 [% ELSE %]
304                                                     [% bdgclass=budget_loo.b_active? "": "inactive_budget" | html %]
305                                                     <option value="[% budget_loo.b_id | html %]" class="[% bdgclass | html %]" data-sort1-authcat="[% budget_loo.b_sort1_authcat | html %]" data-sort2-authcat="[% budget_loo.b_sort2_authcat | html %]">
306                                                 [% END %]
307                                                     [% level_indent | html %][% budget_loo.b_txt | html %][% IF !budget_loo.b_active %] (inactive)[% END %]
308                                                 </option>
309                                             [% END %]
310                                             </optgroup>
311                                         [% END %]
312                                         </select>
313                                         <span class="required">Required</span>
314                                     </li>
315                                     <li>
316                                         <label>&nbsp;</label>
317                                         <span id="current-fund" class="hint"></span>
318                                     </li>
319                                     <li>
320                                         <label for="showallbudgets">&nbsp;Show inactive:</label>
321                                         <input type="checkbox" id="showallbudgets" />
322                                     </li>
323                                     <li>
324                                         <label for="creator">Ordered by: </label>
325                                         <span id="creator"></span>
326                                     </li>
327                                     <li>
328                                         <label for="quantity_to_receive">Quantity ordered: </label>
329                                         <input type="text" readonly="readonly" id="quantity_to_receive" name="quantity" />
330                                     </li>
331                                     <li>
332                                         <label for="quantity">Quantity received: </label>
333                                         <input type="text" inputmode="numeric" pattern="[0-9]*" size="20" name="quantityrec" id="quantity" />
334                                         <div id="qtyrecerror" style="display:none">
335                                             <p class="error">Warning, you have entered more items than expected.
336                                             Items will not be created.</p>
337                                         </div>
338                                     </li>
339
340                                     [% IF ( gst_values ) %]
341                                     <li>
342                                         <label for="tax_rate">Tax rate: </label>
343                                         <select name="tax_rate" id="tax_rate">
344                                         [% FOREACH gst IN gst_values %]
345                                             <option value="[% gst.option | html %]">[% gst.option * 100 | html %]%</option>
346                                         [% END %]
347                                         </select>
348                                     </li>
349                                     [% END %]
350
351                                     <li>
352                                         <label for="rrp">Retail price: </label>
353                                         <span id="rrp"></span>
354                                     <li>
355                                         <label for="replacementprice">Replacement price:</label>
356                                         <input type="text" size="20" name="replacementprice" id="replacementprice" />
357                                     </li>
358                                     <li>
359                                         <label for="ecost">Budgeted cost: </label>
360                                         <span id="ecost"></span>
361                                     </li>
362                                     <li>
363                                         <label for="unitprice">Actual cost:</label>
364                                         <input type="text" size="20" name="unitprice" id="unitprice" />
365                                         <span id="unitprice_hint" class="hint"></span>
366                                     </li>
367                                     <li>
368                                         <label style="margin-left: 12em; text-align: left; font-weight: inherit; float:none;"><input type="checkbox" name="change_currency">Change currency</label>
369                                     </li>
370                                     <li id="select_currency">
371                                         <label for="invoice_unitprice"></label>
372                                         <input type="text" size="20" name="invoice_unitprice" id="invoice_unitprice" value="" />
373                                         [% IF currencies.count %]
374                                         <select name="invoice_currency" id="invoice_currency">
375                                             <option value="[% active_currency.currency | html %]" data-rate="[% active_currency.rate | html %]" selected>[% active_currency.currency | html %] ([% active_currency.symbol | html %])</option>
376                                             [% FOR currency IN currencies %]
377                                             <option value="[% currency.currency | html %]" data-rate="[% currency.rate | html %]">[% currency.currency | html %] ([% currency.symbol | html %])</option>
378                                             [% END %]
379                                         </select>
380                                         [% END %]
381                                     </li>
382                                     <li>
383                                         <label for="order_internalnote">Internal note: </label>
384                                         <textarea name="order_internalnote" id="order_internalnote" width="40" rows="8" ></textarea>
385                                     </li>
386                                     <li>
387                                         <label for="order_vendornote">Vendor note: </label>
388                                         <span id="order_vendornote"></span>
389                                     </li>
390                                 </ol>
391                             </div>
392                         </div>
393                     </div>
394                 </div>
395                 <div class="modal-footer">
396                 [% IF only_one_order %]
397                     <button type="button" class="btn btn-primary modal-save" disabled>Confirm</button>
398                     <a class="cancel btn btn-default" href="/cgi-bin/koha/acqui/parcel.pl?invoiceid=[% invoiceid | html %]&sticky_filters=1">Cancel</a>
399                 [% ELSE %]
400                     <button type="button" class="btn btn-default modal-prev">Previous order</button>
401                     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
402                     <button type="button" class="btn btn-primary modal-save">Save changes</button>
403                     <button type="button" class="btn btn-default modal-next">Next order</button>
404                 [% END %]
405                 </div>
406             </div>
407         </div>
408     </div>
409     <div class="modal fade" id="modal_messages" tabindex="-1" role="dialog" aria-labelledby="Order edit">
410         <div class="modal-dialog" role="document">
411             <div class="modal-content">
412                 <div class="modal-header">
413                     <h4 class="col-md-11 modal-title"></h4>
414                     <button type="button" class="close col-md-1" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
415                 </div>
416                 <div class="modal-body">
417                     <ul></ul>
418                 </div>
419                 <div class="modal-footer">
420                     <button type="button" class="btn btn-primary accept">Accept</button>
421                     <button type="button" class="btn btn-default cancel">Cancel</button>
422                 </div>
423             </div>
424         </div>
425     </div>
426 [% ELSE %]
427     This ordernumber does not exist.
428 [% END %]
429
430 </main>
431 </div> <!-- /.col-sm-10.col-sm-push-2 -->
432
433 <div class="col-sm-2 col-sm-pull-10">
434     <aside>
435         [% INCLUDE 'acquisitions-menu.inc' %]
436     </aside>
437 </div> <!-- /.col-sm-2.col-sm-pull-10 -->
438 </div> <!-- /.row -->
439 [% MACRO jsinclude BLOCK %]
440     [% Asset.js("js/acquisitions-menu.js") | $raw %]
441     [% INCLUDE 'calendar.inc' %]
442     [% Asset.js("js/additem.js") | $raw %]
443     [% Asset.js("js/cataloging.js") | $raw %]
444     [% INCLUDE 'select2.inc' %]
445     [% INCLUDE 'datatables.inc' %]
446     [% INCLUDE 'js-date-format.inc' %]
447     [% INCLUDE 'format_price.inc' %]
448     [% Asset.js("lib/jquery/plugins/jquery.dataTables.columnFilter.js") | $raw %]
449     <script>
450         function display_string(key, strings, fallback) {
451             if ( strings.hasOwnProperty(key) ) {
452                 return strings[key].str && escape_str(strings[key].str) || '';
453             }
454             return fallback != null ? fallback : '';
455         }
456
457         var _build_item = function(item, tr) {
458             var chb = $('<input type="checkbox" name="items_to_receive" value="'+item.item_id+'" />')
459                 .prop('checked', item._checked)
460                 .change(function() {
461                     item._checked = $(this).prop('checked');
462                 });
463             tr.append($('<td style="text-align:center"></td>').append(chb));
464             tr.append('<td><a style="cursor:pointer" onclick="PopupEditPage('+item.biblio_id+', '+item.item_id+');">'+EDIT[0].toUpperCase()+EDIT.substr(1).toLowerCase()+'</a></td>');
465             tr.append('<td>'+(item.external_id||'')+'</td>');
466             tr.append('<td>'+display_string( 'home_library_id', item._strings, item.home_library_id ) || ''+'</td>');
467             tr.append('<td>'+display_string( 'holding_library_id', item._strings, item.holding_library_id )+'</td>');
468             tr.append('<td>'+display_string( 'not_for_loan_status', item._strings, item.not_for_loan_status )+'</td>');
469             tr.append('<td>'+display_string( 'restricted_status', item._strings, item.restricted_status )+'</td>');
470             tr.append('<td><span class="shelvingloc">'+display_string( 'location', item._strings, item.location )+'</span></td>');
471             tr.append('<td>'+(item.callnumber||'')+'</td>');
472             tr.append('<td>'+(item.copy_number||'')+'</td>');
473             tr.append('<td>'+(item.inventory_number||'')+'</td>');
474             tr.append('<td>'+display_string( 'collection', item._strings, item.collection )+'</td>');
475             tr.append('<td>'+display_string( 'item_type_id', item._strings, item.item_type_id )+'</td>');
476             tr.append('<td>'+display_string( 'materials_notes', item._strings, item.materials_notes )+'</td>');
477             tr.append('<td>'+(item.public_notes||'')+'</td>');
478         };
479
480         var items_columns = [null, null, 'external_id', 'home_library_id',
481                 'holding_library_id', 'not_for_loan_status', 'restricted_status', 'location',
482                 'callnumber', 'copy_number', 'inventory_number', 'collection_code',
483                 'item_type', 'materials_notes', 'public_notes'];
484
485         function PopupEditPage(biblionumber, itemnumber) {
486             var url = "/cgi-bin/koha/cataloguing/additem.pl?op=edititem&biblionumber="
487                 + biblionumber + "&itemnumber=" + itemnumber + "&popup=1#edititem";
488             var w = window.open(url);
489             var watchClose = setInterval(function() {
490                 if (w.closed) {
491                     clearTimeout(watchClose);
492                     $.ajax({
493                         dataType: "json",
494                         headers: {
495                             "x-koha-embed": "+strings"
496                         },
497                         url: '/api/v1/items/'+itemnumber,
498                         success: function(item) {
499                             var tr = $("#item_"+itemnumber);
500                             tr.html('');
501                             _build_item(item, tr);
502                         }
503                     });
504                 }
505             }, 500);
506         }
507
508         var periods={};
509         var funds={};
510         var funds_tree = [];
511
512         var QTY_TOTAL = _("Receiving %0$s out of %1$s");
513         var EDIT = _("Edit");
514         var MOD_TITLE = _("Edit order #%s (%s)");
515         var SUGGESTION = _("suggestion #%s");
516         var FUND_KEEP = _("Keep current (%0$s - %1$s)");
517         var FUNC_CUR = _("(Current: %0$s - %1$s)");
518         var ADJ_TAX_INC = _("(adjusted for %s, tax inclusive)");
519         var ADJ_TAX_EXC = _("(adjusted for %s, tax exclusive)");
520         var TAX_INC = _("(tax inclusive)");
521         var TAX_EXC = _("(tax exclusive)");
522         var SAVE_WARNING = _("Order %s: Warning, you have entered more items than expected. Extra items will not be created.");
523         var SAVE_WARNING_NO_ITEMS = _("Order %s: No quantity to receive set. No items will be created.");
524         var SAVE_ERROR = _("Order %s: An error occurred while saving");
525         var SAVE_WARNING_TITLE = _("Warning");
526         var SAVE_ERROR_TITLE = _("Error");
527
528
529         var CAN_user_acquisition = "[% CAN_user_acquisition | html %]";
530         var AcqCreateItem = "[% Koha.Preference('AcqCreateItem') | html %]";
531         var edit_mode = "[% edit | html %]";
532         var invoiceincgst = "[% invoiceincgst | html %]";
533         var active_currency = "[% active_currency.currency | html %]";
534         var invoice_id = "[% invoiceid | html %]";
535
536         $(document).ready(function(){
537             // keep a copy for re-rendering
538             var $funds_tree = $('#bookfund').html();
539             var base_query = { "order_id": {"in": [[% multiple_orders | html %]]}};
540             var pending_orders_url = "/api/v1/acquisitions/orders?only_active=1";
541             var options = {
542                 "ajax": {
543                     "url": pending_orders_url + "&q=" + encodeURI(JSON.stringify(base_query))
544                 },
545                 "embed": [
546                     "basket",
547                     "biblio.suggestions.suggester",
548                     "fund.budget",
549                     "items+strings",
550                     "creator"
551                 ],
552                 'dom': 'C<"top pager"ilpfB><"#filter_c">tr<"bottom pager"ip>',
553                 "columns": [
554                     {
555                         "data": "order_id",
556                         "searchable": true,
557                         "orderable": true
558                     },
559                     {
560                         "data": "biblio.title",
561                         "searchable": true,
562                         "orderable": true,
563                         "render": function(data, type, row, meta) {
564                             if ( data == null ) {
565                                 return "";
566                             }
567                             else {
568                                 return data;
569                             }
570                         }
571                     },
572                     {
573                         "data": "biblio.author",
574                         "searchable": true,
575                         "orderable": true,
576                         "render": function(data, type, row, meta) {
577                             if ( data == null ) {
578                                 return "";
579                             }
580                             else {
581                                 return data;
582                             }
583                         }
584                     },
585                     {
586                         "data": "biblio.isbn",
587                         "searchable": true,
588                         "orderable": true,
589                         "render": function(data, type, row, meta) {
590                             if ( data == null ) {
591                                 return "";
592                             }
593                             else {
594                                 return data;
595                             }
596                         }
597                     },
598                     {
599                         "searchable": false,
600                         "orderable": false,
601                         "data": function(row, type, val, meta) {
602                             return $date(row.date_received||new Date().toISOString());
603                         }
604                     },
605                     {
606                         "data": "fund.name",
607                         "searchable": true,
608                         "orderable": false,
609                         "render": function(data, type, row, meta) {
610                             return row.fund.budget.budget_period_description+" - "+row.fund.name;
611                         }
612                     },
613                     {
614                         "searchable": false,
615                         "orderable": true,
616                         "data": "quantity_received",
617                         "render": function(data, type, row, meta) {
618                             var data = $("#order_edit").data();
619                             return QTY_TOTAL.format(row.subscription_id&&(!data.saved||!data.saved.hasOwnProperty(row.order_id))?row.quantity:row.quantity_received, row.quantity);
620                         }
621                     },
622                     {
623                         "searchable": false,
624                         "orderable": false,
625                         "render": function(data, type, row, meta) {
626                             return '<a class="btn btn-default btn-xs order_edit_toggle" data-toggle="modal" href="#order_edit" data-row="'+meta.row+'" role="button"><i class="fa fa-pencil" aria-hidden="true"></i> '+EDIT+'</a>';
627                         }
628                     }
629                 ],
630                 [% IF only_one_order %]
631                     "drawCallback": function( settings ) {
632                         $(this).find("tbody tr td:last-child a.order_edit_toggle").click();
633                     },
634                 [% END %]
635             };
636             var orders_table = $("#multiple_orders").kohaTable(options);
637             var api = orders_table.api();
638
639             api.on('preDraw', function() {
640                 var saved = $("#order_edit").data('saved');
641                 if(saved) {
642                     var data = api.data();
643                     for(var i = 0; i<data.length; i++) {
644                         var row = data[i];
645                         var srow = saved[row.order_id];
646                         if(srow) {
647                             if(row.fund_id != srow.fund_id) {
648                                 row.fund.budget.budget_period_description = $("#bookfund option[value="+srow.fund_id+"]").parent().attr('label');
649                                 row.fund.name = $("#bookfund option[value="+srow.fund_id+"]").html();
650                             }
651                             row.date_received = srow.date_received;
652                             row.quantity = srow.quantity;
653                             row.quantity_received = srow.quantity_received;
654                         }
655                     }
656                 }
657             });
658
659             [% IF only_one_order %]
660             $("#quantity").on('change', function() {
661                 $('.modal-save').prop('disabled', $(this).val() < 1 );
662             });
663             [% END %]
664
665             var _doSave = function(params) {
666                 $.ajax($.extend({
667                     method: 'POST',
668                     url: '/cgi-bin/koha/acqui/finishreceive.pl'
669                 }, params));
670             };
671
672             var _transform_row = function(row, origrec) {
673                 var params = {};
674                 params['biblionumber'] = row.biblio_id;
675                 params['invoiceid'] = invoice_id;
676                 params['ordernumber'] = row.order_id;
677                 params['booksellerid'] = row.basket.vendor_id;
678
679                 if(row.biblio.suggestions.length && row.biblio.suggestions[0].reason) {
680                     params["suggestionid"] = row.biblio.suggestions[0].suggestion_id;
681                     if($("#reason option[value='"+row.biblio.suggestions[0].reason+"']").length) {
682                         params['reason'] =  row.biblio.suggestions[0].reason;
683                     } else {
684                         params['reason'] = 'other';
685                         params['other_reason'] = row.biblio.suggestions[0].reason;
686                     }
687                 }
688                 params['datereceived'] = row.date_received;
689                 params['bookfund'] = row.fund_id;
690                 params['quantity'] = row.quantity;
691                 params['quantityrec'] = row.quantity_received;
692                 params['origquantityrec'] = origrec;
693                 var effective_create_items = row.basket.create_items || AcqCreateItem;
694                 params['tax_rate'] = (effective_create_items == 'receiving')?row.tax_rate_on_receiving:row.tax_rate_on_ordering;
695                 params['replacementprice'] = row.replacement_price;
696                 params['unitprice'] = ( invoiceincgst=="1" ) ? ( row.unit_price_tax_included || row.ecost_tax_included ) : ( row.unit_price_tax_excluded || row.ecost_tax_excluded );
697                 params['order_internalnote'] = row.internal_note;
698                 if(effective_create_items == 'receiving') {
699                     Object.keys(row.items).forEach(function(key) {
700                         var item = row.items[key];
701                         Object.keys(item).forEach(function(key) {
702                             var field = item[key];
703                             Object.keys(field).forEach(function(key) {
704                                 if(!params[key]) params[key] = [];
705                                 params[key].push(item[key]);
706                             });
707                         });
708                     });
709                 } else if(effective_create_items == 'ordering') {
710                     params['items_to_receive[]'] = (row.items||[])
711                         .filter(function(item) {
712                             return item._checked
713                         })
714                         .map(function(item) {
715                             return item.item_id;
716                         })
717                 }
718                 params['invoice_currency'] = row.invoice_currency;
719                 params['invoice_unitprice'] = row.invoice_unit_price;
720                 return params;
721             };
722
723             var save_rows = function(rows, data) {
724                 if(rows.length) {
725                     $('#jobpanel, #jobstatus').show();
726                     errors = []
727                     var loopRows = function(i) {
728                         var row = rows[i];
729                         if(!row) {
730                             if(!errors.length) {
731                                 location.href = "/cgi-bin/koha/acqui/parcel.pl?invoiceid="+invoice_id
732                             } else {
733                                 $('#modal_messages .modal-header h4').html(SAVE_ERROR_TITLE);
734                                 $('#modal_messages .modal-body ul').html('');
735                                 errors.forEach(function(error) {
736                                     $('#modal_messages .modal-body ul').append('<li>'+error+'</li>');
737                                 });
738                                 $('#modal_messages .modal-footer .cancel').hide();
739                                 $('#modal_messages .modal-footer .accept').html(_("Accept"));
740                                 $('#modal_messages .modal-footer .accept').one('click', function() {
741                                     location.href = "/cgi-bin/koha/acqui/parcel.pl?invoiceid="+invoice_id
742                                 });
743                                 $('#modal_messages').modal('show');
744                             }
745                             return;
746                         }
747                         if(data.saved && data.saved[row.order_id]) {
748                             row = data.saved[row.order_id];
749                             var origrec = data.origrec[row.order_id];
750                         } else {
751                             var origrec = row.quantity_received;
752                         }
753                         var _set_percentage = function() {
754                             var percentage = Math.round(( (i+1) / rows.length) * 100);
755                             var bgproperty = (parseInt(percentage*2)-300)+"px 0px";
756                             $("#jobprogress").css("background-position",bgproperty);
757                             $("#jobprogresspercent").text(percentage);
758                         }
759                         _doSave({
760                             data: _transform_row(row, origrec),
761                             success: function() {
762                                 _set_percentage();
763                                 loopRows(i+1);
764                             },
765                             error: function() {
766                                 _set_percentage()
767                                 errors.push(SAVE_ERROR.format(row.order_id));
768                                 loopRows(i+1);
769                             }
770                         });
771                     };
772                     loopRows(0)
773                 }
774             };
775
776             var get_warnings = function(rows) {
777                 var warnings = [];
778                 rows.forEach(function(row) {
779                     if(row.quantity_received > row.quantity) {
780                         warnings.push(SAVE_WARNING.format(row.order_id));
781                         row.quantity_received = row.quantity;
782                     }
783                     if(row.quantity_received == '0') {
784                         warnings.push(SAVE_WARNING_NO_ITEMS.format(row.order_id));
785                     }
786                 })
787                 return warnings;
788             }
789
790             $('#modal_messages .modal-footer .cancel').on('click', function() {
791                 $('#modal_messages .modal-footer .accept').off('click');
792                 $('#modal_messages').modal('hide');
793             })
794             $('.save').click(function() {
795                 var data = $("#order_edit").data();
796                 var rows = api.rows().data().toArray();
797                 var redirect = true;
798                 var warnings = get_warnings(rows);
799                 if(warnings.length) {
800                     $('#modal_messages .modal-header h4').html(SAVE_WARNING_TITLE);
801                     $('#modal_messages .modal-body ul').html('');
802                     warnings.forEach(function(warning) {
803                         $('#modal_messages .modal-body ul').append('<li>'+warning+'</li>');
804                     });
805                     $('#modal_messages .modal-footer .cancel').show();
806                     $('#modal_messages .modal-footer .accept').html(_("Continue"));
807                     $('#modal_messages .modal-footer .accept').one('click', function() {
808                         $('#modal_messages').modal('hide');
809                         save_rows(rows, data);
810                     });
811                     $('#modal_messages').modal('show');
812                 } else {
813                     save_rows(rows, data);
814                 }
815             });
816
817
818             $("#order_edit").on("change", "#reason", function() {
819                 var val = $(this).val();
820                 var row = $("#order_edit").data('row');
821                 if(val == 'other') {
822                     $("#other_reason").show();
823                     $(this).hide();
824                 } else {
825                     row.biblio.suggestions[0].reason = val;
826                 }
827             });
828
829             $("#order_edit").on("change", "#select-other_reason", function() {
830                 var val = $(this).val();
831                 var row = $("#order_edit").data('row');
832                 row.biblio.suggestions[0].reason = val;
833             });
834
835             $("#order_edit").on("click", "#other_reason a", function() {
836                 $("#other_reason").hide();
837                 $("#reason").val(null).show();
838             });
839
840             $("#order_edit").on("change", "#datereceived", function() {
841                 var val = $(this).val();
842                 var row = $("#order_edit").data('row');
843                 row.date_received = val;
844             });
845
846             $("#order_edit").on("change", "#bookfund", function() {
847                 var val = $(this).val();
848                 var row = $("#order_edit").data('row');
849                 row.fund_id = val;
850             });
851
852             $("#order_edit").on("change", "#quantity_to_receive", function() {
853                 var val = $(this).val();
854                 var row = $("#order_edit").data('row');
855                 row.quantity = val;
856                 if(row.subscription_id) {
857                     $("#quantity").val(val).change();
858                 }
859             });
860
861             $("#order_edit").on("change", "#quantity", function() {
862                 var val = $(this).val();
863                 var row = $("#order_edit").data('row');
864                 row.quantity_received = val;
865                 var qtyto = parseInt($("#quantity_to_receive").val());
866                 if(parseInt(val) > qtyto) {
867                     $("#qtyrecerror").show();
868                 } else {
869                     $("#qtyrecerror").hide();
870                 }
871                 $("input[name='items_to_receive']").each(function() {
872                     $(this).prop('checked', false).change();
873                 });
874                 $("input[name='items_to_receive']:lt("+val+")").each(function () {
875                     $(this).prop('checked', true).change();
876                 });
877             });
878
879             $("#order_edit").on("change", "input[name='items_to_receive']", function() {
880                 var qty = $("input[name='items_to_receive']:checked").length;
881                 $("#quantity").val(qty);
882                 var row = $("#order_edit").data('row');
883                 row.quantity_received = qty;
884             [% IF only_one_order %]
885                 $('.modal-save').prop('disabled', qty < 1 );
886             [% END %]
887             })
888
889             $("#order_edit").on("change", "#tax_rate", function() {
890                 var val = $(this).val();
891                 if(val === null) $(this).val($('option:first-child', this).attr('value'));
892                 val = $(this).val();
893                 var row = $("#order_edit").data('row');
894                 var effective_create_items = row.basket.create_items || AcqCreateItem;
895                 if(effective_create_items == 'receiving') {
896                     row.tax_rate_on_receiving = val;
897                 } else {
898                     row.tax_rate_on_ordering = val;
899                 }
900
901             });
902
903             $("#order_edit").on("change", "#unitprice", function() {
904                 var val = $(this).val();
905                 var row = $("#order_edit").data('row');
906                 if(invoiceincgst == "1") {
907                     row.unit_price_tax_included = val;
908                 } else {
909                     row.unit_price_tax_excluded = val;
910                 }
911
912             });
913
914             $("#order_edit").on("change", "input[name='change_currency']", function(){
915                 if ( $(this).is(":checked") ) {
916                     $("#select_currency").show();
917                     if(!$("#invoice_unitprice").val())
918                         $("#invoice_unitprice").val($('#unitprice').val());
919                     $("#unitprice").prop("readonly", "true");
920                 } else {
921                     $("#select_currency").hide();
922                     $("#unitprice").prop("readonly", "");
923                 }
924             });
925
926             function _update_unitprice() {
927                 var row = $("#order_edit").data('row');
928                 var rate = Number($("select[name='invoice_currency'] option:selected").data('rate'));
929                 var invoice_currency = $("select[name='invoice_currency'] option:selected").val();
930                 var invoice_unitprice = $("#invoice_unitprice").val();
931                 var unitprice = Number( invoice_unitprice * rate ).toFixed(2);
932                 $("#unitprice").val(unitprice).change();
933                 row.invoice_currency = invoice_currency;
934                 row.invoice_unit_price = invoice_unitprice;
935             }
936             $("#order_edit").on("change", "select[name='invoice_currency']", _update_unitprice );
937             $("#order_edit").on("change", "#invoice_unitprice", _update_unitprice );
938
939             $("#order_edit").on("change", "#replacementprice", function() {
940                 var val = $(this).val();
941                 var row = $("#order_edit").data('row');
942                 row.replacement_price = val;
943             });
944
945             $("#order_edit").on("change", "#order_internalnote", function() {
946                 var val = $(this).val();
947                 var row = $("#order_edit").data('row');
948                 row.internal_note = val;
949
950             });
951
952             var keep_row = function() {
953                 var row = $("#order_edit").data('row');
954                 var kept = $("#order_edit").data('kept')||{};
955                 if($('#items_list tbody tr').length) {
956                     row.items = [];
957                     $('#items_list tbody tr').each(function() {
958                         var item = {};
959                         $('#'+$(this).attr('idblock')).find('*[name=kohafield]').each(function() {
960                             var kf = $(this).val();
961                             var obj = {};
962                             $(this).parent().find('*[name]').each(function() {
963                                 obj[$(this).prop('name')] = $(this).val();
964                             });
965                             item[kf] = obj;
966                         });
967                         row.items.push(item);
968                     });
969                 }
970                 kept[row.order_id] = row;
971                 $("#order_edit").data('kept', kept);
972             };
973
974             var save_row = function() {
975                 keep_row();
976                 var saved = $("#order_edit").data('saved')||{};
977                 var kept = $("#order_edit").data('kept');
978                 var saved_rows = $.extend(saved, kept);
979                 $("#order_edit").data('saved', saved_rows);
980                 api.draw();
981                 return saved_rows;
982             }
983
984             var set_modal_buttons = function() {
985                 var n = $("#order_edit").data('n');
986                 var info = api.page.info();
987
988                 $('.modal-prev').prop('disabled', info.page == 0 && n == 0);
989                 $('.modal-next').prop('disabled', info.pages - 1 == info.page && info.end - info.start - 1 == n);
990             };
991
992             var show_subs = function(row) {
993                 //$("#child_orders").show();
994                 var had_rows = false;
995                 var base_query = { "subscription_id": row.subscription_id, "parent_order_id": row.order_id, "order_id": {"!=": row.order_id}};
996                 var pending_orders_url = "/api/v1/acquisitions/orders";
997                 var options = {
998                     "ajax": {
999                         "url": pending_orders_url + "?q=" + encodeURI(JSON.stringify(base_query))
1000                     },
1001                     "header_filter": true,
1002                     "embed": [
1003                         "invoice",
1004                         "basket"
1005                     ],
1006                     "order": [[1, 'asc']],
1007                     'dom': 'C<"top pager"ilpfB>tr<"bottom pager"ip>',
1008                     "columns": [
1009                         {
1010                             "searchable": false,
1011                             "orderable": false,
1012                             "data": function(row, type, val, meta) {
1013                                 if(row.invoice) {
1014                                     if(CAN_user_acquisition) {
1015                                         return '<a href="/cgi-bin/koha/acqui/invoice.pl?invoiceid='+row.invoice_id+'" title="Invoice detail page">'+row.invoice.invoice_number+"</a>";
1016                                     }
1017                                     return row.invoice.invoice_number;
1018                                 }
1019                             }
1020                         },
1021                         {
1022                             "data": "order_id",
1023                             "searchable": false,
1024                             "orderable": false
1025                         },
1026                         {
1027                             "searchable": false,
1028                             "orderable": false,
1029                             "data": "basket.creation_date",
1030                             "render": function(data, type, row, meta) {
1031                                 return $date(row.basket.creation_date);
1032                             }
1033                         },
1034                         {
1035                             "searchable": false,
1036                             "orderable": false,
1037                             "data": function(row, type, val, meta) {
1038                                 return $date(row.date_received);
1039                             }
1040                         },
1041                         {
1042                             "searchable": false,
1043                             "orderable": false,
1044                             "data": function(row, type, val, meta) {
1045                                 return row.quantity_received;
1046                             }
1047                         },
1048                         {
1049                             "searchable": false,
1050                             "orderable": true,
1051                             "data": function(row, type, val, meta) {
1052                                 if(!row.status) return;
1053                                 var first_letter = row.status[0].toUpperCase();
1054                                 return first_letter+row.status.substr(1).toLowerCase();
1055                             }
1056                         },
1057                         {
1058                             "searchable": false,
1059                             "orderable": false,
1060                             "data": function(row, type, val, meta) {
1061                                 if(!row.date_received) return;
1062                                 return Number(row.unit_price_tax_excluded * row.quantity_received).format_price()+' / '+Number(row.unit_price_tax_included * row.quantity_received).format_price()
1063                             }
1064                         },
1065                         {
1066                             "searchable": false,
1067                             "orderable": false,
1068                             "data": function(row, type, val, meta) {
1069                                 return row.internal_note;
1070                             }
1071                         }
1072                     ]
1073                 };
1074                 var child_orders_table = $("#child_orders_table").api(options);
1075                 var child_api = child_orders_table.api();
1076                 child_api.on('preDraw', function() {
1077                     if(!child_api.data().length && !had_rows) return;
1078                     $("#history-panel").show();
1079                     had_rows = true;
1080                 });
1081             };
1082
1083             var set_editor = function() {
1084                 var modal = $("#order_edit");
1085                 var row = modal.data('row');
1086                 var origrec = $("#order_edit").data('origrec')||{};
1087                 if(!origrec.hasOwnProperty(row.order_id)) {
1088                     origrec[row.order_id] = row.quantity_received;
1089                     $("#order_edit").data('origrec', origrec);
1090                 }
1091                 if(row.subscription_id) {
1092                     row.quantity_received = row.quantity;
1093                 }
1094                 var keep = $("#order_edit").data('kept');
1095                 if(keep && keep[row.order_id]) {
1096                     row = keep[row.order_id];
1097                     modal.data('row', row);
1098                 }
1099                 $('input, select, textarea', '.modal-body').val(null)
1100                 modal.find(".modal-title").text(MOD_TITLE.format(escape_str(row.order_id),escape_str(row.biblio.title)));
1101                 ["title", "author", "copyright_date", "isbn", "series_title"].forEach(function(key){
1102                     var o = modal.find(".modal-body #biblio_"+key);
1103                     if(row.biblio[key] !== null) {
1104                         o.parent().show();
1105                         o.html(row.biblio[key]);
1106                     } else {
1107                         o.parent().hide();
1108                     }
1109                 });
1110                 if(row.biblio.suggestions.length) {
1111                     $("#suggestion_fieldset").show();
1112                     if(row.biblio.suggestions[0].suggester) {
1113                         $("#biblio_suggestion_suggester").parent().show();
1114                         $("#biblio_suggestion_suggester")
1115                             .html(
1116                                 [row.biblio.suggestions[0].suggester.surname, row.biblio.suggestions[0].suggester.firstname]
1117                                     .filter(function(name){
1118                                         return name
1119                                     })
1120                                     .join(', ')+' (<a href="/cgi-bin/koha/suggestion/suggestion.pl?suggestionid='+row.biblio.suggestions[0].suggestion_id+'&op=show">'+SUGGESTION.format(row.biblio.suggestions[0].suggestion_id)+'</a>)'
1121                             );
1122                     } else {
1123                         $("#biblio_suggestion_suggester").parent().hide();
1124                     }
1125                     if(row.biblio.suggestions[0].reason) {
1126                         $("#suggestion_reason").show();
1127                         if($("#reason option[value='"+row.biblio.suggestions[0].reason+"']").length) {
1128                              $("#other_reason a").click();
1129                             $("#reason").val(row.biblio.suggestions[0].reason);
1130                             $("#select-other_reason").val(null);
1131                         } else {
1132                             $("#reason").val("other").change();
1133                             $("#select-other_reason").val(row.biblio.suggestions[0].reason);
1134                         }
1135
1136                     } else {
1137                         $("#suggestion_reason").hide();
1138                     }
1139                 } else {
1140                     $("#suggestion_fieldset").hide();
1141                 }
1142
1143                 var effective_create_items = row.basket.create_items || AcqCreateItem;
1144
1145                 // set today if no date_received in row
1146                 document.querySelector("#datereceived")._flatpickr.setDate(row.date_received||new Date());
1147
1148                 // restore the full tree
1149                 $("#bookfund").html($funds_tree);
1150                 $("#selected_bookfund")
1151                     .html(FUND_KEEP.format(row.fund.budget.budget_period_description, row.fund.name))
1152                     .attr( 'data-sort1-authcat', row.fund.sort1_authcat )
1153                     .attr( 'data-sort2-authcat', row.fund.sort2_authcat );
1154                 if (row.fund_id != row.fund.fund_id) {
1155                     $("#bookfund").val(row.fund_id);
1156                 }
1157                 $("#bookfund").select2({ width: '50%' });
1158                 $("#current-fund").html(FUNC_CUR.format(row.fund.budget.budget_period_description, row.fund.name));
1159                 $("#creator").html([row.creator.surname, row.creator.firstname].filter(function(name){return name}).join(', ')+" ("+row.creator.patron_id+')')
1160                 $("#quantity_to_receive").val(row.quantity).prop('readonly', !row.subscription_id);
1161                 $("#quantity").val( effective_create_items == 'cataloguing' ? row.quantity_received || 1 : row.quantity_received )
1162                               .prop('readonly', !row.subscription_id && effective_create_items == 'receiving')
1163                               .change();
1164                 [% IF only_one_order %]
1165                     $(".modal-save").prop('disabled', $("#quantity").val() == 0);
1166                 [% END %]
1167                 $('#qtyrecerror').hide();
1168                 var tax_rate = row.tax_rate_on_receiving || row.tax_rate_on_ordering;
1169                 $("#tax_rate").val(tax_rate).change();
1170                 var rrp_txt;
1171                 var ecost_txt;
1172                 if(invoiceincgst == "1") {
1173                     rrp_txt = Number(row.rrp_tax_included).format_price()+'<span class="hint"> '+ADJ_TAX_INC.format(active_currency)+"</span>";
1174                     ecost_txt = Number(row.ecost_tax_included).format_price()+'<span class="hint"> '+TAX_INC+"</span>";
1175                     $("#unitprice").val(row.unit_price_tax_included > 0 ? Number(row.unit_price_tax_included).format_price() : Number(row.ecost_tax_included).format_price());
1176                     $("#unitprice_hint").html(TAX_INC);
1177                 } else {
1178                     rrp_txt = Number(row.rrp_tax_excluded).format_price()+'<span class="hint"> '+ADJ_TAX_EXC.format(active_currency)+"</span>";
1179                     ecost_txt = Number(row.ecost_tax_excluded).format_price()+'<span class="hint"> '+TAX_EXC+"</span>";
1180                     $("#unitprice").val(row.unit_price_tax_excluded > 0 ? Number(row.unit_price_tax_excluded).format_price() : Number(row.ecost_tax_excluded).format_price());
1181                     $("#unitprice_hint").html(TAX_EXC);
1182                 }
1183                 $("#rrp").html(rrp_txt);
1184                 $("#replacementprice").val(row.replacement_price);
1185                 $("#ecost").html(ecost_txt);
1186                 $("#order_internalnote").val(row.internal_note);
1187                 if(row.vendor_note) {
1188                     $("#order_vendornote").html(row.vendor_note);
1189                     $("#order_vendornote").parent().show();
1190                 } else {
1191                     $("#order_vendornote").parent().hide();
1192                 }
1193
1194
1195                 $("#history-panel").hide();
1196
1197                 $('#items_list tbody tr, #outeritemblock > *, #acq-create-ordering tbody tr').remove();
1198                 $('#items_list').hide();
1199                 if(row.subscription_id) {
1200                     $('#modal-order-main #items-panel').removeClass('show').addClass('hide');
1201                     $('#modal-order-main #history-panel').removeClass('hide').addClass('show');
1202                 } else {
1203                     $('#modal-order-main #history-panel').removeClass('show').addClass('hide');
1204                     $('#modal-order-main #items-panel').removeClass('hide').addClass('show');
1205                     if(effective_create_items == 'receiving') {
1206                         $("#acq-create-receiving").show();
1207                         $("#acq-create-ordering").hide();
1208                         if(row.items && row.items.length) {
1209                             row.items.forEach(function(item, index) {
1210                                 cloneItemBlock(index, '[% UniqueItemFields | html %]', function(block_id) {
1211                                     var block = $('#'+block_id).hide();
1212                                     Object.keys(item).forEach(function(key) {
1213                                         block
1214                                             .find('*[name=kohafield][value="'+key+'"]')
1215                                             .parent()
1216                                             .find('*[name=field_value]')
1217                                             .val(item[key].field_value);
1218                                     });
1219                                     addItemInList(block_id, '[% UniqueItemFields | html %]');
1220                                     block.find("input[name='buttonPlus']").val( (window.MSG_ADDITEM_JS_UPDATEITEM ) );
1221                                     block.find("input[name='buttonPlusMulti']").remove();
1222                                     block.find("input[name='multiValue']").remove();
1223                                 });
1224                             });
1225                         }
1226                         cloneItemBlock((row.items && row.items.length) || 0, '[% UniqueItemFields | html %]');
1227                     } else if (effective_create_items == 'ordering') {
1228                         $("#acq-create-receiving").hide();
1229                         $("#acq-create-ordering").show();
1230                         if(row.items.length) {
1231                             $("#acq-create-ordering tbody").append(
1232                                 row.items.map(function(item) {
1233                                     var tr = $('<tr id="item_'+item.item_id+'"/>');
1234                                     _build_item(item, tr);
1235                                     return tr;
1236                                 })
1237                             );
1238                         }
1239                     } else {
1240                         $('#modal-order-main #items-panel').removeClass('show').addClass('hide');
1241                     }
1242                 }
1243                 if(row.invoice_unit_price) {
1244                     $("#select_currency").show();
1245                     $("#unitprice").prop("readonly", true);
1246                     $("input[name='change_currency']").prop('checked', true);
1247                     $("#invoice_currency").val(row.invoice_currency);
1248                     $("#invoice_unitprice").val(row.invoice_unit_price);
1249                 } else {
1250                     $("#select_currency").hide();
1251                     $("#unitprice").prop("readonly", "");
1252                     $("input[name='change_currency']").prop('checked', false);
1253                     $("#invoice_currency").val(active_currency);
1254                     $("#invoice_unitprice").val('');
1255                 }
1256
1257                 disabledBudgetsCopy = $('#bookfund').html();
1258                 $('#bookfund .inactive_budget').remove();
1259
1260                 $('#showallbudgets').click(function() {
1261                     if ($(this).is(":checked")) {
1262                         $('#bookfund').html(disabledBudgetsCopy); //Puts back all the funds
1263                     }
1264                     else {
1265                         $('#bookfund .inactive_budget').remove();
1266                     }
1267                 });
1268                 set_modal_buttons();
1269             };
1270
1271             $('.modal-prev').click(function() {
1272                 var modal = $("#order_edit");
1273                 keep_row();
1274                 var n = modal.data('n');
1275                 if(n > 0) {
1276                     n--;
1277                     modal.data('row', JSON.parse(JSON.stringify(api.row(n).data())));
1278                     modal.data('n', n);
1279                     set_editor();
1280                 } else {
1281                     $('.modal-next, .modal-prev').prop('disabled', true);
1282                     orders_table.one('draw.dt', function() {
1283                         var info = api.page.info();
1284                         n = info.end - info.start - 1;
1285                         modal.data('row', JSON.parse(JSON.stringify(api.row(n).data())));
1286                         modal.data('n', n);
1287                         set_editor();
1288                     });
1289                     api.page('previous').draw( 'page' );
1290                 }
1291             });
1292
1293             $('.modal-next').click(function() {
1294                 var modal = $("#order_edit");
1295                 keep_row();
1296                 var n = modal.data('n');
1297                 var info = api.page.info();
1298                 if(n < info.end - info.start - 1) {
1299                     n++;
1300                     modal.data('row', JSON.parse(JSON.stringify(api.row(n).data())));
1301                     modal.data('n', n);
1302                     set_editor();
1303                 } else {
1304                     $('.modal-next, .modal-prev').prop('disabled', true);
1305                     orders_table.one('draw.dt', function() {
1306                         var info = api.page.info();
1307                         n = 0;
1308                         modal.data('row', JSON.parse(JSON.stringify(api.row(n).data())));
1309                         modal.data('n', n);
1310                         set_editor();
1311                     });
1312                     api.page('next').draw( 'page' );
1313                 }
1314             });
1315
1316             $('.modal-save').click(function() {
1317                 var saved_rows = save_row();
1318                 if(Object.keys(saved_rows).length) $('.save').prop('disabled', false);
1319                 $("#order_edit").modal('hide');
1320                 [% IF only_one_order %]
1321                     $(".save").click();
1322                 [% END %]
1323             })
1324
1325             $("#order_edit").on("show.bs.modal", function (event) {
1326                 var anchor = $(event.relatedTarget);
1327                 var n = anchor.data("row");
1328                 var row = api.row(n).data();
1329
1330                 var modal = $(this);
1331                 modal.data('row', JSON.parse(JSON.stringify(row)));
1332                 modal.data('n', n);
1333                 modal.data('keep', modal.data('saved')||{});
1334                 //$('#modal-order-main').tabs("option", "active", 0);
1335                 set_editor();
1336             });
1337
1338             $("#order_edit").on("hide.bs.modal", function() {
1339             [% IF only_one_order %] [%# On the single case, dismissing the modal equals to cancelling %]
1340                 location.href = "/cgi-bin/koha/acqui/parcel.pl?invoiceid=[% invoiceid | html %]&sticky_filters=1";
1341             [% ELSE %]
1342                 $("#child_orders_table").DataTable().off('preDraw').destroy();
1343             [% END %]
1344             });
1345         });
1346     </script>
1347 [% END %]
1348
1349 [% INCLUDE 'intranet-bottom.inc' %]