Bug 34022: Adjust items data structure
[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                     row.items.forEach(function(item){
700                         Object.keys(item).forEach(function(item_field){
701                             Object.keys(item[item_field]).forEach(function(key){
702                                 if (!params[key]) params[key] = [];
703                                 params[key].push(item[item_field][key]);
704                             });
705                         });
706                     });
707                 } else if(effective_create_items == 'ordering') {
708                     params['items_to_receive[]'] = (row.items||[])
709                         .filter(function(item) {
710                             return item._checked
711                         })
712                         .map(function(item) {
713                             return item.item_id;
714                         })
715                 }
716                 params['invoice_currency'] = row.invoice_currency;
717                 params['invoice_unitprice'] = row.invoice_unit_price;
718                 return params;
719             };
720
721             var save_rows = function(rows, data) {
722                 if(rows.length) {
723                     $('#jobpanel, #jobstatus').show();
724                     errors = []
725                     var loopRows = function(i) {
726                         var row = rows[i];
727                         if(!row) {
728                             if(!errors.length) {
729                                 location.href = "/cgi-bin/koha/acqui/parcel.pl?invoiceid="+invoice_id
730                             } else {
731                                 $('#modal_messages .modal-header h4').html(SAVE_ERROR_TITLE);
732                                 $('#modal_messages .modal-body ul').html('');
733                                 errors.forEach(function(error) {
734                                     $('#modal_messages .modal-body ul').append('<li>'+error+'</li>');
735                                 });
736                                 $('#modal_messages .modal-footer .cancel').hide();
737                                 $('#modal_messages .modal-footer .accept').html(_("Accept"));
738                                 $('#modal_messages .modal-footer .accept').one('click', function() {
739                                     location.href = "/cgi-bin/koha/acqui/parcel.pl?invoiceid="+invoice_id
740                                 });
741                                 $('#modal_messages').modal('show');
742                             }
743                             return;
744                         }
745                         if(data.saved && data.saved[row.order_id]) {
746                             row = data.saved[row.order_id];
747                             var origrec = data.origrec[row.order_id];
748                         } else {
749                             var origrec = row.quantity_received;
750                         }
751                         var _set_percentage = function() {
752                             var percentage = Math.round(( (i+1) / rows.length) * 100);
753                             var bgproperty = (parseInt(percentage*2)-300)+"px 0px";
754                             $("#jobprogress").css("background-position",bgproperty);
755                             $("#jobprogresspercent").text(percentage);
756                         }
757                         _doSave({
758                             data: _transform_row(row, origrec),
759                             success: function() {
760                                 _set_percentage();
761                                 loopRows(i+1);
762                             },
763                             error: function() {
764                                 _set_percentage()
765                                 errors.push(SAVE_ERROR.format(row.order_id));
766                                 loopRows(i+1);
767                             }
768                         });
769                     };
770                     loopRows(0)
771                 }
772             };
773
774             var get_warnings = function(rows) {
775                 var warnings = [];
776                 rows.forEach(function(row) {
777                     if(row.quantity_received > row.quantity) {
778                         warnings.push(SAVE_WARNING.format(row.order_id));
779                         row.quantity_received = row.quantity;
780                     }
781                     if(row.quantity_received == '0') {
782                         warnings.push(SAVE_WARNING_NO_ITEMS.format(row.order_id));
783                     }
784                 })
785                 return warnings;
786             }
787
788             $('#modal_messages .modal-footer .cancel').on('click', function() {
789                 $('#modal_messages .modal-footer .accept').off('click');
790                 $('#modal_messages').modal('hide');
791             })
792             $('.save').click(function() {
793                 var data = $("#order_edit").data();
794                 var rows = api.rows().data().toArray();
795                 var redirect = true;
796                 var warnings = get_warnings(rows);
797                 if(warnings.length) {
798                     $('#modal_messages .modal-header h4').html(SAVE_WARNING_TITLE);
799                     $('#modal_messages .modal-body ul').html('');
800                     warnings.forEach(function(warning) {
801                         $('#modal_messages .modal-body ul').append('<li>'+warning+'</li>');
802                     });
803                     $('#modal_messages .modal-footer .cancel').show();
804                     $('#modal_messages .modal-footer .accept').html(_("Continue"));
805                     $('#modal_messages .modal-footer .accept').one('click', function() {
806                         $('#modal_messages').modal('hide');
807                         save_rows(rows, data);
808                     });
809                     $('#modal_messages').modal('show');
810                 } else {
811                     save_rows(rows, data);
812                 }
813             });
814
815
816             $("#order_edit").on("change", "#reason", function() {
817                 var val = $(this).val();
818                 var row = $("#order_edit").data('row');
819                 if(val == 'other') {
820                     $("#other_reason").show();
821                     $(this).hide();
822                 } else {
823                     row.biblio.suggestions[0].reason = val;
824                 }
825             });
826
827             $("#order_edit").on("change", "#select-other_reason", function() {
828                 var val = $(this).val();
829                 var row = $("#order_edit").data('row');
830                 row.biblio.suggestions[0].reason = val;
831             });
832
833             $("#order_edit").on("click", "#other_reason a", function() {
834                 $("#other_reason").hide();
835                 $("#reason").val(null).show();
836             });
837
838             $("#order_edit").on("change", "#datereceived", function() {
839                 var val = $(this).val();
840                 var row = $("#order_edit").data('row');
841                 row.date_received = val;
842             });
843
844             $("#order_edit").on("change", "#bookfund", function() {
845                 var val = $(this).val();
846                 var row = $("#order_edit").data('row');
847                 row.fund_id = val;
848             });
849
850             $("#order_edit").on("change", "#quantity_to_receive", function() {
851                 var val = $(this).val();
852                 var row = $("#order_edit").data('row');
853                 row.quantity = val;
854                 if(row.subscription_id) {
855                     $("#quantity").val(val).change();
856                 }
857             });
858
859             $("#order_edit").on("change", "#quantity", function() {
860                 var val = $(this).val();
861                 var row = $("#order_edit").data('row');
862                 row.quantity_received = val;
863                 var qtyto = parseInt($("#quantity_to_receive").val());
864                 if(parseInt(val) > qtyto) {
865                     $("#qtyrecerror").show();
866                 } else {
867                     $("#qtyrecerror").hide();
868                 }
869                 $("input[name='items_to_receive']").each(function() {
870                     $(this).prop('checked', false).change();
871                 });
872                 $("input[name='items_to_receive']:lt("+val+")").each(function () {
873                     $(this).prop('checked', true).change();
874                 });
875             });
876
877             $("#order_edit").on("change", "input[name='items_to_receive']", function() {
878                 var qty = $("input[name='items_to_receive']:checked").length;
879                 $("#quantity").val(qty);
880                 var row = $("#order_edit").data('row');
881                 row.quantity_received = qty;
882             [% IF only_one_order %]
883                 $('.modal-save').prop('disabled', qty < 1 );
884             [% END %]
885             })
886
887             $("#order_edit").on("change", "#tax_rate", function() {
888                 var val = $(this).val();
889                 if(val === null) $(this).val($('option:first-child', this).attr('value'));
890                 val = $(this).val();
891                 var row = $("#order_edit").data('row');
892                 var effective_create_items = row.basket.create_items || AcqCreateItem;
893                 if(effective_create_items == 'receiving') {
894                     row.tax_rate_on_receiving = val;
895                 } else {
896                     row.tax_rate_on_ordering = val;
897                 }
898
899             });
900
901             $("#order_edit").on("change", "#unitprice", function() {
902                 var val = $(this).val();
903                 var row = $("#order_edit").data('row');
904                 if(invoiceincgst == "1") {
905                     row.unit_price_tax_included = val;
906                 } else {
907                     row.unit_price_tax_excluded = val;
908                 }
909
910             });
911
912             $("#order_edit").on("change", "input[name='change_currency']", function(){
913                 if ( $(this).is(":checked") ) {
914                     $("#select_currency").show();
915                     if(!$("#invoice_unitprice").val())
916                         $("#invoice_unitprice").val($('#unitprice').val());
917                     $("#unitprice").prop("readonly", "true");
918                 } else {
919                     $("#select_currency").hide();
920                     $("#unitprice").prop("readonly", "");
921                 }
922             });
923
924             function _update_unitprice() {
925                 var row = $("#order_edit").data('row');
926                 var rate = Number($("select[name='invoice_currency'] option:selected").data('rate'));
927                 var invoice_currency = $("select[name='invoice_currency'] option:selected").val();
928                 var invoice_unitprice = $("#invoice_unitprice").val();
929                 var unitprice = Number( invoice_unitprice * rate ).toFixed(2);
930                 $("#unitprice").val(unitprice).change();
931                 row.invoice_currency = invoice_currency;
932                 row.invoice_unit_price = invoice_unitprice;
933             }
934             $("#order_edit").on("change", "select[name='invoice_currency']", _update_unitprice );
935             $("#order_edit").on("change", "#invoice_unitprice", _update_unitprice );
936
937             $("#order_edit").on("change", "#replacementprice", function() {
938                 var val = $(this).val();
939                 var row = $("#order_edit").data('row');
940                 row.replacement_price = val;
941             });
942
943             $("#order_edit").on("change", "#order_internalnote", function() {
944                 var val = $(this).val();
945                 var row = $("#order_edit").data('row');
946                 row.internal_note = val;
947
948             });
949
950             var keep_row = function() {
951                 var row = $("#order_edit").data('row');
952                 var kept = $("#order_edit").data('kept')||{};
953                 if($('#items_list tbody tr').length) {
954                     row.items = [];
955                     $('#items_list tbody tr').each(function() {
956                         var item = {};
957                         $('#'+$(this).attr('idblock')).find('*[name=kohafield]').each(function() {
958                             var kf = $(this).val();
959                             var obj = {};
960                             $(this).parent().find('*[name]').each(function() {
961                                 obj[$(this).prop('name')] = $(this).val();
962                             });
963                             item[kf] = obj;
964                         });
965                         row.items.push(item);
966                     });
967                 }
968                 kept[row.order_id] = row;
969                 $("#order_edit").data('kept', kept);
970             };
971
972             var save_row = function() {
973                 keep_row();
974                 var saved = $("#order_edit").data('saved')||{};
975                 var kept = $("#order_edit").data('kept');
976                 var saved_rows = $.extend(saved, kept);
977                 $("#order_edit").data('saved', saved_rows);
978                 api.draw();
979                 return saved_rows;
980             }
981
982             var set_modal_buttons = function() {
983                 var n = $("#order_edit").data('n');
984                 var info = api.page.info();
985
986                 $('.modal-prev').prop('disabled', info.page == 0 && n == 0);
987                 $('.modal-next').prop('disabled', info.pages - 1 == info.page && info.end - info.start - 1 == n);
988             };
989
990             var show_subs = function(row) {
991                 //$("#child_orders").show();
992                 var had_rows = false;
993                 var base_query = { "subscription_id": row.subscription_id, "parent_order_id": row.order_id, "order_id": {"!=": row.order_id}};
994                 var pending_orders_url = "/api/v1/acquisitions/orders";
995                 var options = {
996                     "ajax": {
997                         "url": pending_orders_url + "?q=" + encodeURI(JSON.stringify(base_query))
998                     },
999                     "header_filter": true,
1000                     "embed": [
1001                         "invoice",
1002                         "basket"
1003                     ],
1004                     "order": [[1, 'asc']],
1005                     'dom': 'C<"top pager"ilpfB>tr<"bottom pager"ip>',
1006                     "columns": [
1007                         {
1008                             "searchable": false,
1009                             "orderable": false,
1010                             "data": function(row, type, val, meta) {
1011                                 if(row.invoice) {
1012                                     if(CAN_user_acquisition) {
1013                                         return '<a href="/cgi-bin/koha/acqui/invoice.pl?invoiceid='+row.invoice_id+'" title="Invoice detail page">'+row.invoice.invoice_number+"</a>";
1014                                     }
1015                                     return row.invoice.invoice_number;
1016                                 }
1017                             }
1018                         },
1019                         {
1020                             "data": "order_id",
1021                             "searchable": false,
1022                             "orderable": false
1023                         },
1024                         {
1025                             "searchable": false,
1026                             "orderable": false,
1027                             "data": "basket.creation_date",
1028                             "render": function(data, type, row, meta) {
1029                                 return $date(row.basket.creation_date);
1030                             }
1031                         },
1032                         {
1033                             "searchable": false,
1034                             "orderable": false,
1035                             "data": function(row, type, val, meta) {
1036                                 return $date(row.date_received);
1037                             }
1038                         },
1039                         {
1040                             "searchable": false,
1041                             "orderable": false,
1042                             "data": function(row, type, val, meta) {
1043                                 return row.quantity_received;
1044                             }
1045                         },
1046                         {
1047                             "searchable": false,
1048                             "orderable": true,
1049                             "data": function(row, type, val, meta) {
1050                                 if(!row.status) return;
1051                                 var first_letter = row.status[0].toUpperCase();
1052                                 return first_letter+row.status.substr(1).toLowerCase();
1053                             }
1054                         },
1055                         {
1056                             "searchable": false,
1057                             "orderable": false,
1058                             "data": function(row, type, val, meta) {
1059                                 if(!row.date_received) return;
1060                                 return Number(row.unit_price_tax_excluded * row.quantity_received).format_price()+' / '+Number(row.unit_price_tax_included * row.quantity_received).format_price()
1061                             }
1062                         },
1063                         {
1064                             "searchable": false,
1065                             "orderable": false,
1066                             "data": function(row, type, val, meta) {
1067                                 return row.internal_note;
1068                             }
1069                         }
1070                     ]
1071                 };
1072                 var child_orders_table = $("#child_orders_table").api(options);
1073                 var child_api = child_orders_table.api();
1074                 child_api.on('preDraw', function() {
1075                     if(!child_api.data().length && !had_rows) return;
1076                     $("#history-panel").show();
1077                     had_rows = true;
1078                 });
1079             };
1080
1081             var set_editor = function() {
1082                 var modal = $("#order_edit");
1083                 var row = modal.data('row');
1084                 var origrec = $("#order_edit").data('origrec')||{};
1085                 if(!origrec.hasOwnProperty(row.order_id)) {
1086                     origrec[row.order_id] = row.quantity_received;
1087                     $("#order_edit").data('origrec', origrec);
1088                 }
1089                 if(row.subscription_id) {
1090                     row.quantity_received = row.quantity;
1091                 }
1092                 var keep = $("#order_edit").data('kept');
1093                 if(keep && keep[row.order_id]) {
1094                     row = keep[row.order_id];
1095                     modal.data('row', row);
1096                 }
1097                 $('input, select, textarea', '.modal-body').val(null)
1098                 modal.find(".modal-title").text(MOD_TITLE.format(escape_str(row.order_id),escape_str(row.biblio.title)));
1099                 ["title", "author", "copyright_date", "isbn", "series_title"].forEach(function(key){
1100                     var o = modal.find(".modal-body #biblio_"+key);
1101                     if(row.biblio[key] !== null) {
1102                         o.parent().show();
1103                         o.html(row.biblio[key]);
1104                     } else {
1105                         o.parent().hide();
1106                     }
1107                 });
1108                 if(row.biblio.suggestions.length) {
1109                     $("#suggestion_fieldset").show();
1110                     if(row.biblio.suggestions[0].suggester) {
1111                         $("#biblio_suggestion_suggester").parent().show();
1112                         $("#biblio_suggestion_suggester")
1113                             .html(
1114                                 [row.biblio.suggestions[0].suggester.surname, row.biblio.suggestions[0].suggester.firstname]
1115                                     .filter(function(name){
1116                                         return name
1117                                     })
1118                                     .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>)'
1119                             );
1120                     } else {
1121                         $("#biblio_suggestion_suggester").parent().hide();
1122                     }
1123                     if(row.biblio.suggestions[0].reason) {
1124                         $("#suggestion_reason").show();
1125                         if($("#reason option[value='"+row.biblio.suggestions[0].reason+"']").length) {
1126                              $("#other_reason a").click();
1127                             $("#reason").val(row.biblio.suggestions[0].reason);
1128                             $("#select-other_reason").val(null);
1129                         } else {
1130                             $("#reason").val("other").change();
1131                             $("#select-other_reason").val(row.biblio.suggestions[0].reason);
1132                         }
1133
1134                     } else {
1135                         $("#suggestion_reason").hide();
1136                     }
1137                 } else {
1138                     $("#suggestion_fieldset").hide();
1139                 }
1140
1141                 var effective_create_items = row.basket.create_items || AcqCreateItem;
1142
1143                 // set today if no date_received in row
1144                 document.querySelector("#datereceived")._flatpickr.setDate(row.date_received||new Date());
1145
1146                 // restore the full tree
1147                 $("#bookfund").html($funds_tree);
1148                 $("#selected_bookfund")
1149                     .html(FUND_KEEP.format(row.fund.budget.budget_period_description, row.fund.name))
1150                     .attr( 'data-sort1-authcat', row.fund.sort1_authcat )
1151                     .attr( 'data-sort2-authcat', row.fund.sort2_authcat );
1152                 if (row.fund_id != row.fund.fund_id) {
1153                     $("#bookfund").val(row.fund_id);
1154                 }
1155                 $("#bookfund").select2({ width: '50%' });
1156                 $("#current-fund").html(FUNC_CUR.format(row.fund.budget.budget_period_description, row.fund.name));
1157                 if( row.creator ) {
1158                     $("#creator").html([row.creator.surname, row.creator.firstname].filter(function(name){return name}).join(', ')+" ("+row.creator.patron_id+')')
1159                 } else {
1160                     $("#creator").html(__("Account has been deleted"));
1161                 }
1162                 $("#quantity_to_receive").val(row.quantity).prop('readonly', !row.subscription_id);
1163                 $("#quantity").val( effective_create_items == 'cataloguing' ? row.quantity_received || 1 : row.quantity_received )
1164                               .prop('readonly', !row.subscription_id && effective_create_items == 'receiving')
1165                               .change();
1166                 [% IF only_one_order %]
1167                     $(".modal-save").prop('disabled', $("#quantity").val() == 0);
1168                 [% END %]
1169                 $('#qtyrecerror').hide();
1170                 var tax_rate = row.tax_rate_on_receiving || row.tax_rate_on_ordering;
1171                 $("#tax_rate").val(tax_rate).change();
1172                 var rrp_txt;
1173                 var ecost_txt;
1174                 if(invoiceincgst == "1") {
1175                     rrp_txt = Number(row.rrp_tax_included).format_price()+'<span class="hint"> '+ADJ_TAX_INC.format(active_currency)+"</span>";
1176                     ecost_txt = Number(row.ecost_tax_included).format_price()+'<span class="hint"> '+TAX_INC+"</span>";
1177                     $("#unitprice").val(row.unit_price_tax_included > 0 ? Number(row.unit_price_tax_included).format_price() : Number(row.ecost_tax_included).format_price());
1178                     $("#unitprice_hint").html(TAX_INC);
1179                 } else {
1180                     rrp_txt = Number(row.rrp_tax_excluded).format_price()+'<span class="hint"> '+ADJ_TAX_EXC.format(active_currency)+"</span>";
1181                     ecost_txt = Number(row.ecost_tax_excluded).format_price()+'<span class="hint"> '+TAX_EXC+"</span>";
1182                     $("#unitprice").val(row.unit_price_tax_excluded > 0 ? Number(row.unit_price_tax_excluded).format_price() : Number(row.ecost_tax_excluded).format_price());
1183                     $("#unitprice_hint").html(TAX_EXC);
1184                 }
1185                 $("#rrp").html(rrp_txt);
1186                 $("#replacementprice").val(row.replacement_price);
1187                 $("#ecost").html(ecost_txt);
1188                 $("#order_internalnote").val(row.internal_note);
1189                 if(row.vendor_note) {
1190                     $("#order_vendornote").html(row.vendor_note);
1191                     $("#order_vendornote").parent().show();
1192                 } else {
1193                     $("#order_vendornote").parent().hide();
1194                 }
1195
1196
1197                 $("#history-panel").hide();
1198
1199                 $('#items_list tbody tr, #outeritemblock > *, #acq-create-ordering tbody tr').remove();
1200                 $('#items_list').hide();
1201                 if(row.subscription_id) {
1202                     $('#modal-order-main #items-panel').removeClass('show').addClass('hide');
1203                     $('#modal-order-main #history-panel').removeClass('hide').addClass('show');
1204                 } else {
1205                     $('#modal-order-main #history-panel').removeClass('show').addClass('hide');
1206                     $('#modal-order-main #items-panel').removeClass('hide').addClass('show');
1207                     if(effective_create_items == 'receiving') {
1208                         $("#acq-create-receiving").show();
1209                         $("#acq-create-ordering").hide();
1210                         if(row.items && row.items.length) {
1211                             row.items.forEach(function(item, index) {
1212                                 cloneItemBlock(index, '[% UniqueItemFields | html %]', function(block_id) {
1213                                     var block = $('#'+block_id).hide();
1214                                     Object.keys(item).forEach(function(key) {
1215                                         block
1216                                             .find('*[name=kohafield][value="'+key+'"]')
1217                                             .parent()
1218                                             .find('*[name=field_value]')
1219                                             .val(item[key].field_value);
1220                                     });
1221                                     addItemInList(block_id, '[% UniqueItemFields | html %]');
1222                                     block.find("input[name='buttonPlus']").val( (window.MSG_ADDITEM_JS_UPDATEITEM ) );
1223                                     block.find("input[name='buttonPlusMulti']").remove();
1224                                     block.find("input[name='multiValue']").remove();
1225                                 });
1226                             });
1227                         }
1228                         cloneItemBlock((row.items && row.items.length) || 0, '[% UniqueItemFields | html %]');
1229                     } else if (effective_create_items == 'ordering') {
1230                         $("#acq-create-receiving").hide();
1231                         $("#acq-create-ordering").show();
1232                         if(row.items.length) {
1233                             $("#acq-create-ordering tbody").append(
1234                                 row.items.map(function(item) {
1235                                     var tr = $('<tr id="item_'+item.item_id+'"/>');
1236                                     _build_item(item, tr);
1237                                     return tr;
1238                                 })
1239                             );
1240                         }
1241                     } else {
1242                         $('#modal-order-main #items-panel').removeClass('show').addClass('hide');
1243                     }
1244                 }
1245                 if(row.invoice_unit_price) {
1246                     $("#select_currency").show();
1247                     $("#unitprice").prop("readonly", true);
1248                     $("input[name='change_currency']").prop('checked', true);
1249                     $("#invoice_currency").val(row.invoice_currency);
1250                     $("#invoice_unitprice").val(row.invoice_unit_price);
1251                 } else {
1252                     $("#select_currency").hide();
1253                     $("#unitprice").prop("readonly", "");
1254                     $("input[name='change_currency']").prop('checked', false);
1255                     $("#invoice_currency").val(active_currency);
1256                     $("#invoice_unitprice").val('');
1257                 }
1258
1259                 disabledBudgetsCopy = $('#bookfund').html();
1260                 $('#bookfund .inactive_budget').remove();
1261
1262                 $('#showallbudgets').click(function() {
1263                     if ($(this).is(":checked")) {
1264                         $('#bookfund').html(disabledBudgetsCopy); //Puts back all the funds
1265                     }
1266                     else {
1267                         $('#bookfund .inactive_budget').remove();
1268                     }
1269                 });
1270                 set_modal_buttons();
1271             };
1272
1273             $('.modal-prev').click(function() {
1274                 var modal = $("#order_edit");
1275                 keep_row();
1276                 var n = modal.data('n');
1277                 if(n > 0) {
1278                     n--;
1279                     modal.data('row', JSON.parse(JSON.stringify(api.row(n).data())));
1280                     modal.data('n', n);
1281                     set_editor();
1282                 } else {
1283                     $('.modal-next, .modal-prev').prop('disabled', true);
1284                     orders_table.one('draw.dt', function() {
1285                         var info = api.page.info();
1286                         n = info.end - info.start - 1;
1287                         modal.data('row', JSON.parse(JSON.stringify(api.row(n).data())));
1288                         modal.data('n', n);
1289                         set_editor();
1290                     });
1291                     api.page('previous').draw( 'page' );
1292                 }
1293             });
1294
1295             $('.modal-next').click(function() {
1296                 var modal = $("#order_edit");
1297                 keep_row();
1298                 var n = modal.data('n');
1299                 var info = api.page.info();
1300                 if(n < info.end - info.start - 1) {
1301                     n++;
1302                     modal.data('row', JSON.parse(JSON.stringify(api.row(n).data())));
1303                     modal.data('n', n);
1304                     set_editor();
1305                 } else {
1306                     $('.modal-next, .modal-prev').prop('disabled', true);
1307                     orders_table.one('draw.dt', function() {
1308                         var info = api.page.info();
1309                         n = 0;
1310                         modal.data('row', JSON.parse(JSON.stringify(api.row(n).data())));
1311                         modal.data('n', n);
1312                         set_editor();
1313                     });
1314                     api.page('next').draw( 'page' );
1315                 }
1316             });
1317
1318             $('.modal-save').click(function() {
1319                 var saved_rows = save_row();
1320                 if(Object.keys(saved_rows).length) $('.save').prop('disabled', false);
1321                 $("#order_edit").modal('hide');
1322                 [% IF only_one_order %]
1323                     $(".save").click();
1324                 [% END %]
1325             })
1326
1327             $("#order_edit").on("show.bs.modal", function (event) {
1328                 var anchor = $(event.relatedTarget);
1329                 var n = anchor.data("row");
1330                 var row = api.row(n).data();
1331
1332                 var modal = $(this);
1333                 modal.data('row', JSON.parse(JSON.stringify(row)));
1334                 modal.data('n', n);
1335                 modal.data('keep', modal.data('saved')||{});
1336                 //$('#modal-order-main').tabs("option", "active", 0);
1337                 set_editor();
1338             });
1339
1340             $("#order_edit").on("hide.bs.modal", function() {
1341             [% IF only_one_order %] [%# On the single case, dismissing the modal equals to cancelling %]
1342                 location.href = "/cgi-bin/koha/acqui/parcel.pl?invoiceid=[% invoiceid | html %]&sticky_filters=1";
1343             [% ELSE %]
1344                 $("#child_orders_table").DataTable().off('preDraw').destroy();
1345             [% END %]
1346             });
1347         });
1348     </script>
1349 [% END %]
1350
1351 [% INCLUDE 'intranet-bottom.inc' %]