Bug 34109: Check mandatory item fields when creating during recipt
[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                                 <form action="/cgi-bin/koha/acqui/orderreceive.pl" class="validated">
273                                     <ol>
274                                         <li>
275                                             <label for="datereceived">Date received: </label>
276                                             <input type="text" size="10" id="datereceived" name="datereceived" class="flatpickr" />
277                                         </li>
278                                         <li>
279                                             <label for="bookfund" class="required">Fund: </label>
280                                             <select id="bookfund" name="bookfund">
281                                                 <optgroup label="Current">
282                                                     <option id="selected_bookfund" selected="selected"></option>
283                                                 </optgroup>
284                                             [% FOREACH budget_period_id IN budget_loops.keys %]
285                                                 [% SET budget_period = budget_loops.$budget_period_id %]
286                                                 [% IF budget_period.active %]
287                                                     <optgroup label="[% budget_period.description | html %]">
288                                                 [% ELSE %]
289                                                     <optgroup class="inactive_budget" label="[% budget_period.description | html %] (Inactive)">
290                                                 [% END %]
291                                                 [% FOREACH budget_loo IN budget_period.funds %]
292                                                 [% level_indent_cnt = 0 %]
293                                                     [% level_indent = "" %]
294                                                     [% WHILE level_indent_cnt < budget_loo.b_level %]
295                                                         [% level_indent = level_indent _ " -- " %]
296                                                         [% level_indent_cnt = level_indent_cnt +1 %]
297                                                     [% END %]
298
299                                                     [% IF ( budget_loo.b_sel ) %]
300                                                         [% active_count = 0 #select no other fund %]
301                                                         <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 %]">
302                                                     [% ELSIF active_count==1 && budget_loo.b_active %]
303                                                         <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 %]">
304                                                     [% ELSE %]
305                                                         [% bdgclass=budget_loo.b_active? "": "inactive_budget" | html %]
306                                                         <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 %]">
307                                                     [% END %]
308                                                         [% level_indent | html %][% budget_loo.b_txt | html %][% IF !budget_loo.b_active %] (inactive)[% END %]
309                                                     </option>
310                                                 [% END %]
311                                                 </optgroup>
312                                             [% END %]
313                                             </select>
314                                             <span class="required">Required</span>
315                                         </li>
316                                         <li>
317                                             <label>&nbsp;</label>
318                                             <span id="current-fund" class="hint"></span>
319                                         </li>
320                                         <li>
321                                             <label for="showallbudgets">&nbsp;Show inactive:</label>
322                                             <input type="checkbox" id="showallbudgets" />
323                                         </li>
324                                         <li>
325                                             <label for="creator">Ordered by: </label>
326                                             <span id="creator"></span>
327                                         </li>
328                                         <li>
329                                             <label for="quantity_to_receive">Quantity ordered: </label>
330                                             <input type="text" readonly="readonly" id="quantity_to_receive" name="quantity" />
331                                         </li>
332                                         <li>
333                                             <label for="quantity">Quantity received: </label>
334                                             <input type="text" inputmode="numeric" pattern="[0-9]*" size="20" name="quantityrec" id="quantity" />
335                                             <div id="qtyrecerror" style="display:none">
336                                                 <p class="error">Warning, you have entered more items than expected.
337                                                 Items will not be created.</p>
338                                             </div>
339                                         </li>
340
341                                         [% IF ( gst_values ) %]
342                                         <li>
343                                             <label for="tax_rate">Tax rate: </label>
344                                             <select name="tax_rate" id="tax_rate">
345                                             [% FOREACH gst IN gst_values %]
346                                                 <option value="[% gst.option | html %]">[% gst.option * 100 | html %]%</option>
347                                             [% END %]
348                                             </select>
349                                         </li>
350                                         [% END %]
351
352                                         <li>
353                                             <label for="rrp">Retail price: </label>
354                                             <span id="rrp"></span>
355                                         <li>
356                                             <label for="replacementprice">Replacement price:</label>
357                                             <input class="decimal" type="text" size="20" name="replacementprice" id="replacementprice" />
358                                         </li>
359                                         <li>
360                                             <label for="ecost">Budgeted cost: </label>
361                                             <span id="ecost"></span>
362                                         </li>
363                                         <li>
364                                             <label for="unitprice">Actual cost:</label>
365                                             <input class="decimal" type="text" size="20" name="unitprice" id="unitprice" />
366                                             <span id="unitprice_hint" class="hint"></span>
367                                         </li>
368                                         <li>
369                                             <label style="margin-left: 12em; text-align: left; font-weight: inherit; float:none;"><input type="checkbox" id="change_currency" name="change_currency">Change currency</label>
370                                         </li>
371                                         <li id="select_currency">
372                                             <label for="invoice_unitprice"></label>
373                                             <input class="decimal" type="text" size="20" name="invoice_unitprice" id="invoice_unitprice" value="" />
374                                             [% IF currencies.count %]
375                                             <select name="invoice_currency" id="invoice_currency">
376                                                 <option value="[% active_currency.currency | html %]" data-rate="[% active_currency.rate | html %]" selected>[% active_currency.currency | html %] ([% active_currency.symbol | html %])</option>
377                                                 [% FOR currency IN currencies %]
378                                                 <option value="[% currency.currency | html %]" data-rate="[% currency.rate | html %]">[% currency.currency | html %] ([% currency.symbol | html %])</option>
379                                                 [% END %]
380                                             </select>
381                                             [% END %]
382                                         </li>
383                                         <li>
384                                             <label for="order_internalnote">Internal note: </label>
385                                             <textarea name="order_internalnote" id="order_internalnote" width="40" rows="8" ></textarea>
386                                         </li>
387                                         <li>
388                                             <label for="order_vendornote">Vendor note: </label>
389                                             <span id="order_vendornote"></span>
390                                         </li>
391                                     </ol>
392                                 </form>
393                             </div>
394                         </div>
395                     </div>
396                 </div>
397                 <div class="modal-footer">
398                 [% IF only_one_order %]
399                     <button type="button" class="btn btn-primary modal-save" disabled>Confirm</button>
400                     <a class="cancel btn btn-default" href="/cgi-bin/koha/acqui/parcel.pl?invoiceid=[% invoiceid | html %]&sticky_filters=1">Cancel</a>
401                 [% ELSE %]
402                     <button type="button" class="btn btn-default modal-prev">Previous order</button>
403                     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
404                     <button type="button" class="btn btn-primary modal-save">Save changes</button>
405                     <button type="button" class="btn btn-default modal-next">Next order</button>
406                 [% END %]
407                 </div>
408             </div>
409         </div>
410     </div>
411     <div class="modal fade" id="modal_messages" tabindex="-1" role="dialog" aria-labelledby="Order edit">
412         <div class="modal-dialog" role="document">
413             <div class="modal-content">
414                 <div class="modal-header">
415                     <h4 class="col-md-11 modal-title"></h4>
416                     <button type="button" class="close col-md-1" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
417                 </div>
418                 <div class="modal-body">
419                     <ul></ul>
420                 </div>
421                 <div class="modal-footer">
422                     <button type="button" class="btn btn-primary accept">Accept</button>
423                     <button type="button" class="btn btn-default cancel">Cancel</button>
424                 </div>
425             </div>
426         </div>
427     </div>
428 [% ELSE %]
429     This ordernumber does not exist.
430 [% END %]
431
432 </main>
433 </div> <!-- /.col-sm-10.col-sm-push-2 -->
434
435 <div class="col-sm-2 col-sm-pull-10">
436     <aside>
437         [% INCLUDE 'acquisitions-menu.inc' %]
438     </aside>
439 </div> <!-- /.col-sm-2.col-sm-pull-10 -->
440 </div> <!-- /.row -->
441 [% MACRO jsinclude BLOCK %]
442     [% Asset.js("js/acquisitions-menu.js") | $raw %]
443     [% INCLUDE 'calendar.inc' %]
444     [% Asset.js("js/additem.js") | $raw %]
445     [% Asset.js("js/cataloging.js") | $raw %]
446     [% INCLUDE 'select2.inc' %]
447     [% INCLUDE 'datatables.inc' %]
448     [% INCLUDE 'js-date-format.inc' %]
449     [% INCLUDE 'format_price.inc' %]
450     [% Asset.js("lib/jquery/plugins/jquery.dataTables.columnFilter.js") | $raw %]
451     <script>
452         function display_string(key, strings, fallback) {
453             if ( strings.hasOwnProperty(key) ) {
454                 return strings[key].str && escape_str(strings[key].str) || '';
455             }
456             return fallback != null ? fallback : '';
457         }
458
459         var _build_item = function(item, tr) {
460             var chb = $('<input type="checkbox" name="items_to_receive" value="'+item.item_id+'" />')
461                 .prop('checked', item._checked)
462                 .change(function() {
463                     item._checked = $(this).prop('checked');
464                 });
465             tr.append($('<td style="text-align:center"></td>').append(chb));
466             tr.append('<td><a style="cursor:pointer" onclick="PopupEditPage('+item.biblio_id+', '+item.item_id+');">'+EDIT[0].toUpperCase()+EDIT.substr(1).toLowerCase()+'</a></td>');
467             tr.append('<td>'+(item.external_id||'')+'</td>');
468             tr.append('<td>'+display_string( 'home_library_id', item._strings, item.home_library_id ) || ''+'</td>');
469             tr.append('<td>'+display_string( 'holding_library_id', item._strings, item.holding_library_id )+'</td>');
470             tr.append('<td>'+display_string( 'not_for_loan_status', item._strings, item.not_for_loan_status )+'</td>');
471             tr.append('<td>'+display_string( 'restricted_status', item._strings, item.restricted_status )+'</td>');
472             tr.append('<td><span class="shelvingloc">'+display_string( 'location', item._strings, item.location )+'</span></td>');
473             tr.append('<td>'+(item.callnumber||'')+'</td>');
474             tr.append('<td>'+(item.copy_number||'')+'</td>');
475             tr.append('<td>'+(item.inventory_number||'')+'</td>');
476             tr.append('<td>'+display_string( 'collection', item._strings, item.collection )+'</td>');
477             tr.append('<td>'+display_string( 'item_type_id', item._strings, item.item_type_id )+'</td>');
478             tr.append('<td>'+display_string( 'materials_notes', item._strings, item.materials_notes )+'</td>');
479             tr.append('<td>'+(item.public_notes||'')+'</td>');
480         };
481
482         var items_columns = [null, null, 'external_id', 'home_library_id',
483                 'holding_library_id', 'not_for_loan_status', 'restricted_status', 'location',
484                 'callnumber', 'copy_number', 'inventory_number', 'collection_code',
485                 'item_type', 'materials_notes', 'public_notes'];
486
487         function PopupEditPage(biblionumber, itemnumber) {
488             var url = "/cgi-bin/koha/cataloguing/additem.pl?op=edititem&biblionumber="
489                 + biblionumber + "&itemnumber=" + itemnumber + "&popup=1#edititem";
490             var w = window.open(url);
491             var watchClose = setInterval(function() {
492                 if (w.closed) {
493                     clearTimeout(watchClose);
494                     $.ajax({
495                         dataType: "json",
496                         headers: {
497                             "x-koha-embed": "+strings"
498                         },
499                         url: '/api/v1/items/'+itemnumber,
500                         success: function(item) {
501                             var tr = $("#item_"+itemnumber);
502                             item._checked = $(tr).find('input[type="checkbox"]').prop('checked');
503                             tr.html('');
504                             _build_item(item, tr);
505                         }
506                     });
507                 }
508             }, 500);
509         }
510
511         var periods={};
512         var funds={};
513         var funds_tree = [];
514
515         var QTY_TOTAL = _("Receiving %0$s out of %1$s");
516         var EDIT = _("Edit");
517         var MOD_TITLE = _("Edit order #%s (%s)");
518         var SUGGESTION = _("suggestion #%s");
519         var FUND_KEEP = _("Keep current (%0$s - %1$s)");
520         var FUNC_CUR = _("(Current: %0$s - %1$s)");
521         var ADJ_TAX_INC = _("(adjusted for %s, tax inclusive)");
522         var ADJ_TAX_EXC = _("(adjusted for %s, tax exclusive)");
523         var TAX_INC = _("(tax inclusive)");
524         var TAX_EXC = _("(tax exclusive)");
525         var SAVE_WARNING = _("Order %s: Warning, you have entered more items than expected. Extra items will not be created.");
526         var SAVE_WARNING_NO_ITEMS = _("Order %s: No quantity to receive set. No items will be created.");
527         var SAVE_ERROR = _("Order %s: An error occurred while saving");
528         var SAVE_WARNING_TITLE = _("Warning");
529         var SAVE_ERROR_TITLE = _("Error");
530
531
532         var CAN_user_acquisition = "[% CAN_user_acquisition | html %]";
533         var AcqCreateItem = "[% Koha.Preference('AcqCreateItem') | html %]";
534         var edit_mode = "[% edit | html %]";
535         var invoiceincgst = "[% invoiceincgst | html %]";
536         var active_currency = "[% active_currency.currency | html %]";
537         var invoice_id = "[% invoiceid | html %]";
538
539         $(document).ready(function(){
540             // keep a copy for re-rendering
541             var $funds_tree = $('#bookfund').html();
542             var base_query = { "order_id": {"in": [[% multiple_orders | html %]]}};
543             var pending_orders_url = "/api/v1/acquisitions/orders?only_active=1";
544             var options = {
545                 "ajax": {
546                     "url": pending_orders_url + "&q=" + encodeURI(JSON.stringify(base_query))
547                 },
548                 "embed": [
549                     "basket",
550                     "biblio.suggestions.suggester",
551                     "fund.budget",
552                     "items+strings",
553                     "creator"
554                 ],
555                 'dom': 'C<"top pager"ilpfB><"#filter_c">tr<"bottom pager"ip>',
556                 "columns": [
557                     {
558                         "data": "order_id",
559                         "searchable": true,
560                         "orderable": true
561                     },
562                     {
563                         "data": "biblio.title",
564                         "searchable": true,
565                         "orderable": true,
566                         "render": function(data, type, row, meta) {
567                             if ( data == null ) {
568                                 return "";
569                             }
570                             else {
571                                 return data;
572                             }
573                         }
574                     },
575                     {
576                         "data": "biblio.author",
577                         "searchable": true,
578                         "orderable": true,
579                         "render": function(data, type, row, meta) {
580                             if ( data == null ) {
581                                 return "";
582                             }
583                             else {
584                                 return data;
585                             }
586                         }
587                     },
588                     {
589                         "data": "biblio.isbn",
590                         "searchable": true,
591                         "orderable": true,
592                         "render": function(data, type, row, meta) {
593                             if ( data == null ) {
594                                 return "";
595                             }
596                             else {
597                                 return data;
598                             }
599                         }
600                     },
601                     {
602                         "searchable": false,
603                         "orderable": false,
604                         "data": function(row, type, val, meta) {
605                             return $date(row.date_received||new Date().toISOString());
606                         }
607                     },
608                     {
609                         "data": "fund.name",
610                         "searchable": true,
611                         "orderable": false,
612                         "render": function(data, type, row, meta) {
613                             return row.fund.budget.budget_period_description+" - "+row.fund.name;
614                         }
615                     },
616                     {
617                         "searchable": false,
618                         "orderable": true,
619                         "data": "quantity_received",
620                         "render": function(data, type, row, meta) {
621                             var data = $("#order_edit").data();
622                             return QTY_TOTAL.format(row.subscription_id&&(!data.saved||!data.saved.hasOwnProperty(row.order_id))?row.quantity:row.quantity_received, row.quantity);
623                         }
624                     },
625                     {
626                         "searchable": false,
627                         "orderable": false,
628                         "render": function(data, type, row, meta) {
629                             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>';
630                         }
631                     }
632                 ],
633                 [% IF only_one_order %]
634                     "drawCallback": function( settings ) {
635                         $(this).find("tbody tr td:last-child a.order_edit_toggle").click();
636                     },
637                 [% END %]
638             };
639             var orders_table = $("#multiple_orders").kohaTable(options);
640             var api = orders_table.api();
641
642             api.on('preDraw', function() {
643                 var saved = $("#order_edit").data('saved');
644                 if(saved) {
645                     var data = api.data();
646                     for(var i = 0; i<data.length; i++) {
647                         var row = data[i];
648                         var srow = saved[row.order_id];
649                         if(srow) {
650                             if(row.fund_id != srow.fund_id) {
651                                 row.fund.budget.budget_period_description = $("#bookfund option[value="+srow.fund_id+"]").parent().attr('label');
652                                 row.fund.name = $("#bookfund option[value="+srow.fund_id+"]").html();
653                             }
654                             row.date_received = srow.date_received;
655                             row.quantity = srow.quantity;
656                             row.quantity_received = srow.quantity_received;
657                         }
658                     }
659                 }
660             });
661
662             [% IF only_one_order %]
663             $("#quantity").on('change', function() {
664                 $('.modal-save').prop('disabled', $(this).val() < 1 );
665             });
666             [% END %]
667
668             var _doSave = function(params) {
669                 $.ajax($.extend({
670                     method: 'POST',
671                     url: '/cgi-bin/koha/acqui/finishreceive.pl'
672                 }, params));
673             };
674
675             var _transform_row = function(row, origrec) {
676                 var params = {};
677                 params['biblionumber'] = row.biblio_id;
678                 params['invoiceid'] = invoice_id;
679                 params['ordernumber'] = row.order_id;
680                 params['booksellerid'] = row.basket.vendor_id;
681
682                 if(row.biblio.suggestions.length && row.biblio.suggestions[0].reason) {
683                     params["suggestionid"] = row.biblio.suggestions[0].suggestion_id;
684                     if($("#reason option[value='"+row.biblio.suggestions[0].reason+"']").length) {
685                         params['reason'] =  row.biblio.suggestions[0].reason;
686                     } else {
687                         params['reason'] = 'other';
688                         params['other_reason'] = row.biblio.suggestions[0].reason;
689                     }
690                 }
691                 params['datereceived'] = row.date_received;
692                 params['bookfund'] = row.fund_id;
693                 params['quantity'] = row.quantity;
694                 params['quantityrec'] = row.quantity_received;
695                 params['origquantityrec'] = origrec;
696                 var effective_create_items = row.basket.create_items || AcqCreateItem;
697                 params['tax_rate'] = (effective_create_items == 'receiving')?row.tax_rate_on_receiving:row.tax_rate_on_ordering;
698                 params['replacementprice'] = row.replacement_price;
699                 params['unitprice'] = ( invoiceincgst=="1" ) ? ( row.unit_price_tax_included || row.ecost_tax_included ) : ( row.unit_price_tax_excluded || row.ecost_tax_excluded );
700                 params['order_internalnote'] = row.internal_note;
701                 if (effective_create_items == 'receiving') {
702                     row.items.forEach(function(item){
703                         Object.keys(item).forEach(function(item_field){
704                             Object.keys(item[item_field]).forEach(function(key){
705                                 if (!params[key]) params[key] = [];
706                                 params[key].push(item[item_field][key]);
707                             });
708                         });
709                     });
710                 } else if(effective_create_items == 'ordering') {
711                     params['items_to_receive[]'] = (row.items||[])
712                         .filter(function(item) {
713                             return item._checked
714                         })
715                         .map(function(item) {
716                             return item.item_id;
717                         })
718                 }
719                 params['invoice_currency'] = row.invoice_currency;
720                 params['invoice_unitprice'] = row.invoice_unit_price;
721                 return params;
722             };
723
724             var save_rows = function(rows, data) {
725                 if(rows.length) {
726                     $('#jobpanel, #jobstatus').show();
727                     errors = []
728                     var loopRows = function(i) {
729                         var row = rows[i];
730                         if(!row) {
731                             if(!errors.length) {
732                                 location.href = "/cgi-bin/koha/acqui/parcel.pl?invoiceid="+invoice_id
733                             } else {
734                                 $('#modal_messages .modal-header h4').html(SAVE_ERROR_TITLE);
735                                 $('#modal_messages .modal-body ul').html('');
736                                 errors.forEach(function(error) {
737                                     $('#modal_messages .modal-body ul').append('<li>'+error+'</li>');
738                                 });
739                                 $('#modal_messages .modal-footer .cancel').hide();
740                                 $('#modal_messages .modal-footer .accept').html(_("Accept"));
741                                 $('#modal_messages .modal-footer .accept').one('click', function() {
742                                     location.href = "/cgi-bin/koha/acqui/parcel.pl?invoiceid="+invoice_id
743                                 });
744                                 $('#modal_messages').modal('show');
745                             }
746                             return;
747                         }
748                         if(data.saved && data.saved[row.order_id]) {
749                             row = data.saved[row.order_id];
750                             var origrec = data.origrec[row.order_id];
751                         } else {
752                             var origrec = row.quantity_received;
753                         }
754                         var _set_percentage = function() {
755                             var percentage = Math.round(( (i+1) / rows.length) * 100);
756                             var bgproperty = (parseInt(percentage*2)-300)+"px 0px";
757                             $("#jobprogress").css("background-position",bgproperty);
758                             $("#jobprogresspercent").text(percentage);
759                         }
760                         _doSave({
761                             data: _transform_row(row, origrec),
762                             success: function() {
763                                 _set_percentage();
764                                 loopRows(i+1);
765                             },
766                             error: function() {
767                                 _set_percentage()
768                                 errors.push(SAVE_ERROR.format(row.order_id));
769                                 loopRows(i+1);
770                             }
771                         });
772                     };
773                     loopRows(0)
774                 }
775             };
776
777             var get_warnings = function(rows) {
778                 var warnings = [];
779                 rows.forEach(function(row) {
780                     if(row.quantity_received > row.quantity) {
781                         warnings.push(SAVE_WARNING.format(row.order_id));
782                         row.quantity_received = row.quantity;
783                     }
784                     if(row.quantity_received == '0') {
785                         warnings.push(SAVE_WARNING_NO_ITEMS.format(row.order_id));
786                     }
787                 })
788                 return warnings;
789             }
790
791             $('#modal_messages .modal-footer .cancel').on('click', function() {
792                 $('#modal_messages .modal-footer .accept').off('click');
793                 $('#modal_messages').modal('hide');
794             })
795             $('.save').click(function() {
796                 var data = $("#order_edit").data();
797                 var rows = api.rows().data().toArray();
798                 var redirect = true;
799                 var warnings = get_warnings(rows);
800                 if(warnings.length) {
801                     $('#modal_messages .modal-header h4').html(SAVE_WARNING_TITLE);
802                     $('#modal_messages .modal-body ul').html('');
803                     warnings.forEach(function(warning) {
804                         $('#modal_messages .modal-body ul').append('<li>'+warning+'</li>');
805                     });
806                     $('#modal_messages .modal-footer .cancel').show();
807                     $('#modal_messages .modal-footer .accept').html(_("Continue"));
808                     $('#modal_messages .modal-footer .accept').one('click', function() {
809                         $('#modal_messages').modal('hide');
810                         save_rows(rows, data);
811                     });
812                     $('#modal_messages').modal('show');
813                 } else {
814                     save_rows(rows, data);
815                 }
816             });
817
818
819             $("#order_edit").on("change", "#reason", function() {
820                 var val = $(this).val();
821                 var row = $("#order_edit").data('row');
822                 if(val == 'other') {
823                     $("#other_reason").show();
824                     $(this).hide();
825                 } else {
826                     row.biblio.suggestions[0].reason = val;
827                 }
828             });
829
830             $("#order_edit").on("change", "#select-other_reason", function() {
831                 var val = $(this).val();
832                 var row = $("#order_edit").data('row');
833                 row.biblio.suggestions[0].reason = val;
834             });
835
836             $("#order_edit").on("click", "#other_reason a", function() {
837                 $("#other_reason").hide();
838                 $("#reason").val(null).show();
839             });
840
841             $("#order_edit").on("change", "#datereceived", function() {
842                 var val = $(this).val();
843                 var row = $("#order_edit").data('row');
844                 row.date_received = val;
845             });
846
847             $("#order_edit").on("change", "#bookfund", function() {
848                 var val = $(this).val();
849                 var row = $("#order_edit").data('row');
850                 row.fund_id = val;
851             });
852
853             $("#order_edit").on("change", "#quantity_to_receive", function() {
854                 var val = $(this).val();
855                 var row = $("#order_edit").data('row');
856                 row.quantity = val;
857                 if(row.subscription_id) {
858                     $("#quantity").val(val).change();
859                 }
860             });
861
862             $("#order_edit").on("change", "#quantity", function() {
863                 var val = $(this).val();
864                 var row = $("#order_edit").data('row');
865                 row.quantity_received = val;
866                 var qtyto = parseInt($("#quantity_to_receive").val());
867                 if(parseInt(val) > qtyto) {
868                     $("#qtyrecerror").show();
869                 } else {
870                     $("#qtyrecerror").hide();
871                 }
872                 $("input[name='items_to_receive']").each(function() {
873                     $(this).prop('checked', false).change();
874                 });
875                 $("input[name='items_to_receive']:lt("+val+")").each(function () {
876                     $(this).prop('checked', true).change();
877                 });
878             });
879
880             $("#order_edit").on("change", "input[name='items_to_receive']", function() {
881                 var qty = $("input[name='items_to_receive']:checked").length;
882                 $("#quantity").val(qty);
883                 var row = $("#order_edit").data('row');
884                 row.quantity_received = qty;
885             [% IF only_one_order %]
886                 $('.modal-save').prop('disabled', qty < 1 );
887             [% END %]
888             })
889
890             $("#order_edit").on("change", "#tax_rate", function() {
891                 var val = $(this).val();
892                 if(val === null) $(this).val($('option:first-child', this).attr('value'));
893                 val = $(this).val();
894                 var row = $("#order_edit").data('row');
895                 var effective_create_items = row.basket.create_items || AcqCreateItem;
896                 if(effective_create_items == 'receiving') {
897                     row.tax_rate_on_receiving = val;
898                 } else {
899                     row.tax_rate_on_ordering = val;
900                 }
901
902             });
903
904             $("#order_edit").on("change", "#unitprice", function() {
905                 var val = $(this).val();
906                 var row = $("#order_edit").data('row');
907                 if(invoiceincgst == "1") {
908                     row.unit_price_tax_included = val;
909                 } else {
910                     row.unit_price_tax_excluded = val;
911                 }
912
913             });
914
915             function _update_unitprice() {
916                 let row = $("#order_edit").data('row');
917
918                 let unitprice = $("#unitprice").val();
919                 let invoice_currency = $("#invoice_currency").val();
920                 let invoice_unitprice = $("#invoice_unitprice").val();
921                 let change_currency = $("#change_currency").is(":checked");
922
923                 if ( change_currency ) {
924                     $("#select_currency").show();
925                     if(!$("#invoice_unitprice").val()){
926                         invoice_unitprice = unitprice;
927                     }
928                     var rate = Number($("#invoice_currency option:selected").data('rate'));
929                     unitprice = Number( invoice_unitprice * rate ).toFixed(2);
930                     $("#unitprice").prop("readonly", "true");
931                 } else {
932                     $("#select_currency").hide();
933                     $("#unitprice").prop("readonly", "");
934                     invoice_unitprice = "";
935                     invoice_currency = active_currency;
936                 }
937
938                 $("#invoice_unitprice").val(invoice_unitprice);
939                 $("#invoice_currency").val(invoice_currency);
940                 $("#unitprice").val(unitprice).change();
941
942                 row.invoice_currency = invoice_currency;
943                 row.invoice_unit_price = invoice_unitprice;
944             }
945             $("#order_edit").on("change", "#invoice_currency",  _update_unitprice );
946             $("#order_edit").on("change", "#invoice_unitprice", _update_unitprice );
947             $("#order_edit").on("change", "#change_currency",   _update_unitprice );
948
949             $("#order_edit").on("change", "#replacementprice", function() {
950                 var val = $(this).val();
951                 var row = $("#order_edit").data('row');
952                 row.replacement_price = val;
953             });
954
955             $("#order_edit").on("change", "#order_internalnote", function() {
956                 var val = $(this).val();
957                 var row = $("#order_edit").data('row');
958                 row.internal_note = val;
959
960             });
961
962             var keep_row = function() {
963                 var row = $("#order_edit").data('row');
964                 var kept = $("#order_edit").data('kept')||{};
965                 if($('#items_list tbody tr').length) {
966                     row.items = [];
967                     $('#items_list tbody tr').each(function() {
968                         var item = {};
969                         $('#'+$(this).attr('idblock')).find('*[name=kohafield]').each(function() {
970                             var kf = $(this).val();
971                             var obj = {};
972                             $(this).parent().find('*[name]').each(function() {
973                                 obj[$(this).prop('name')] = $(this).val();
974                             });
975                             item[kf] = obj;
976                         });
977                         row.items.push(item);
978                     });
979                 }
980                 kept[row.order_id] = row;
981                 $("#order_edit").data('kept', kept);
982             };
983
984             var save_row = function() {
985                 keep_row();
986                 var saved = $("#order_edit").data('saved')||{};
987                 var kept = $("#order_edit").data('kept');
988                 var saved_rows = $.extend(saved, kept);
989                 $("#order_edit").data('saved', saved_rows);
990                 api.draw();
991                 return saved_rows;
992             }
993
994             var set_modal_buttons = function() {
995                 var n = $("#order_edit").data('n');
996                 var info = api.page.info();
997
998                 $('.modal-prev').prop('disabled', info.page == 0 && n == 0);
999                 $('.modal-next').prop('disabled', info.pages - 1 == info.page && info.end - info.start - 1 == n);
1000             };
1001
1002             var show_subs = function(row) {
1003                 //$("#child_orders").show();
1004                 var had_rows = false;
1005                 var base_query = { "subscription_id": row.subscription_id, "parent_order_id": row.order_id, "order_id": {"!=": row.order_id}};
1006                 var pending_orders_url = "/api/v1/acquisitions/orders";
1007                 var options = {
1008                     "ajax": {
1009                         "url": pending_orders_url + "?q=" + encodeURI(JSON.stringify(base_query))
1010                     },
1011                     "header_filter": true,
1012                     "embed": [
1013                         "invoice",
1014                         "basket"
1015                     ],
1016                     "order": [[1, 'asc']],
1017                     'dom': 'C<"top pager"ilpfB>tr<"bottom pager"ip>',
1018                     "columns": [
1019                         {
1020                             "searchable": false,
1021                             "orderable": false,
1022                             "data": function(row, type, val, meta) {
1023                                 if(row.invoice) {
1024                                     if(CAN_user_acquisition) {
1025                                         return '<a href="/cgi-bin/koha/acqui/invoice.pl?invoiceid='+row.invoice_id+'" title="Invoice detail page">'+row.invoice.invoice_number+"</a>";
1026                                     }
1027                                     return row.invoice.invoice_number;
1028                                 }
1029                             }
1030                         },
1031                         {
1032                             "data": "order_id",
1033                             "searchable": false,
1034                             "orderable": false
1035                         },
1036                         {
1037                             "searchable": false,
1038                             "orderable": false,
1039                             "data": "basket.creation_date",
1040                             "render": function(data, type, row, meta) {
1041                                 return $date(row.basket.creation_date);
1042                             }
1043                         },
1044                         {
1045                             "searchable": false,
1046                             "orderable": false,
1047                             "data": function(row, type, val, meta) {
1048                                 return $date(row.date_received);
1049                             }
1050                         },
1051                         {
1052                             "searchable": false,
1053                             "orderable": false,
1054                             "data": function(row, type, val, meta) {
1055                                 return row.quantity_received;
1056                             }
1057                         },
1058                         {
1059                             "searchable": false,
1060                             "orderable": true,
1061                             "data": function(row, type, val, meta) {
1062                                 if(!row.status) return;
1063                                 var first_letter = row.status[0].toUpperCase();
1064                                 return first_letter+row.status.substr(1).toLowerCase();
1065                             }
1066                         },
1067                         {
1068                             "searchable": false,
1069                             "orderable": false,
1070                             "data": function(row, type, val, meta) {
1071                                 if(!row.date_received) return;
1072                                 return Number(row.unit_price_tax_excluded * row.quantity_received).format_price()+' / '+Number(row.unit_price_tax_included * row.quantity_received).format_price()
1073                             }
1074                         },
1075                         {
1076                             "searchable": false,
1077                             "orderable": false,
1078                             "data": function(row, type, val, meta) {
1079                                 return row.internal_note;
1080                             }
1081                         }
1082                     ]
1083                 };
1084                 var child_orders_table = $("#child_orders_table").api(options);
1085                 var child_api = child_orders_table.api();
1086                 child_api.on('preDraw', function() {
1087                     if(!child_api.data().length && !had_rows) return;
1088                     $("#history-panel").show();
1089                     had_rows = true;
1090                 });
1091             };
1092
1093             var set_editor = function() {
1094                 var modal = $("#order_edit");
1095                 var row = modal.data('row');
1096                 var origrec = $("#order_edit").data('origrec')||{};
1097                 if(!origrec.hasOwnProperty(row.order_id)) {
1098                     origrec[row.order_id] = row.quantity_received;
1099                     $("#order_edit").data('origrec', origrec);
1100                 }
1101                 if(row.subscription_id) {
1102                     row.quantity_received = row.quantity;
1103                 }
1104                 var keep = $("#order_edit").data('kept');
1105                 if(keep && keep[row.order_id]) {
1106                     row = keep[row.order_id];
1107                     modal.data('row', row);
1108                 }
1109                 $('input, select, textarea', '.modal-body').val(null)
1110                 modal.find(".modal-title").text(MOD_TITLE.format(escape_str(row.order_id),escape_str(row.biblio.title)));
1111                 ["title", "author", "copyright_date", "isbn", "series_title"].forEach(function(key){
1112                     var o = modal.find(".modal-body #biblio_"+key);
1113                     if(row.biblio[key] !== null) {
1114                         o.parent().show();
1115                         o.html(row.biblio[key]);
1116                     } else {
1117                         o.parent().hide();
1118                     }
1119                 });
1120                 if(row.biblio.suggestions.length) {
1121                     $("#suggestion_fieldset").show();
1122                     if(row.biblio.suggestions[0].suggester) {
1123                         $("#biblio_suggestion_suggester").parent().show();
1124                         $("#biblio_suggestion_suggester")
1125                             .html(
1126                                 [row.biblio.suggestions[0].suggester.surname, row.biblio.suggestions[0].suggester.firstname]
1127                                     .filter(function(name){
1128                                         return name
1129                                     })
1130                                     .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>)'
1131                             );
1132                     } else {
1133                         $("#biblio_suggestion_suggester").parent().hide();
1134                     }
1135                     if(row.biblio.suggestions[0].reason) {
1136                         $("#suggestion_reason").show();
1137                         if($("#reason option[value='"+row.biblio.suggestions[0].reason+"']").length) {
1138                              $("#other_reason a").click();
1139                             $("#reason").val(row.biblio.suggestions[0].reason);
1140                             $("#select-other_reason").val(null);
1141                         } else {
1142                             $("#reason").val("other").change();
1143                             $("#select-other_reason").val(row.biblio.suggestions[0].reason);
1144                         }
1145
1146                     } else {
1147                         $("#suggestion_reason").hide();
1148                     }
1149                 } else {
1150                     $("#suggestion_fieldset").hide();
1151                 }
1152
1153                 var effective_create_items = row.basket.create_items || AcqCreateItem;
1154
1155                 // set today if no date_received in row
1156                 document.querySelector("#datereceived")._flatpickr.setDate(row.date_received||new Date());
1157
1158                 // restore the full tree
1159                 $("#bookfund").html($funds_tree);
1160                 $("#selected_bookfund")
1161                     .html(FUND_KEEP.format(row.fund.budget.budget_period_description, row.fund.name))
1162                     .attr( 'data-sort1-authcat', row.fund.sort1_authcat )
1163                     .attr( 'data-sort2-authcat', row.fund.sort2_authcat );
1164                 if (row.fund_id != row.fund.fund_id) {
1165                     $("#bookfund").val(row.fund_id);
1166                 }
1167                 $("#bookfund").select2({ width: '50%' });
1168                 $("#current-fund").html(FUNC_CUR.format(row.fund.budget.budget_period_description, row.fund.name));
1169                 if( row.creator ) {
1170                     $("#creator").html([row.creator.surname, row.creator.firstname].filter(function(name){return name}).join(', ')+" ("+row.creator.patron_id+')')
1171                 } else {
1172                     $("#creator").html(__("Account has been deleted"));
1173                 }
1174                 $("#quantity_to_receive").val(row.quantity).prop('readonly', !row.subscription_id);
1175                 $("#quantity").val( effective_create_items == 'cataloguing' ? row.quantity_received || 1 : row.quantity_received )
1176                               .prop('readonly', !row.subscription_id && effective_create_items == 'receiving')
1177                               .change();
1178                 [% IF only_one_order %]
1179                     $(".modal-save").prop('disabled', $("#quantity").val() == 0);
1180                 [% END %]
1181                 $('#qtyrecerror').hide();
1182                 var tax_rate = row.tax_rate_on_receiving || row.tax_rate_on_ordering;
1183                 $("#tax_rate").val(tax_rate).change();
1184                 var rrp_txt;
1185                 var ecost_txt;
1186                 if(invoiceincgst == "1") {
1187                     rrp_txt = Number(row.rrp_tax_included).format_price()+'<span class="hint"> '+ADJ_TAX_INC.format(active_currency)+"</span>";
1188                     ecost_txt = Number(row.ecost_tax_included).format_price()+'<span class="hint"> '+TAX_INC+"</span>";
1189                     $("#unitprice").val(row.unit_price_tax_included > 0 ? Number(row.unit_price_tax_included).format_price() : Number(row.ecost_tax_included).format_price());
1190                     $("#unitprice_hint").html(TAX_INC);
1191                 } else {
1192                     rrp_txt = Number(row.rrp_tax_excluded).format_price()+'<span class="hint"> '+ADJ_TAX_EXC.format(active_currency)+"</span>";
1193                     ecost_txt = Number(row.ecost_tax_excluded).format_price()+'<span class="hint"> '+TAX_EXC+"</span>";
1194                     $("#unitprice").val(row.unit_price_tax_excluded > 0 ? Number(row.unit_price_tax_excluded).format_price() : Number(row.ecost_tax_excluded).format_price());
1195                     $("#unitprice_hint").html(TAX_EXC);
1196                 }
1197                 $("#rrp").html(rrp_txt);
1198                 $("#replacementprice").val(row.replacement_price);
1199                 $("#ecost").html(ecost_txt);
1200                 $("#order_internalnote").val(row.internal_note);
1201                 if(row.vendor_note) {
1202                     $("#order_vendornote").html(row.vendor_note);
1203                     $("#order_vendornote").parent().show();
1204                 } else {
1205                     $("#order_vendornote").parent().hide();
1206                 }
1207
1208
1209                 $("#history-panel").hide();
1210
1211                 $('#items_list tbody tr, #outeritemblock > *, #acq-create-ordering tbody tr').remove();
1212                 $('#items_list').hide();
1213                 if(row.subscription_id) {
1214                     $('#modal-order-main #items-panel').removeClass('show').addClass('hide');
1215                     $('#modal-order-main #history-panel').removeClass('hide').addClass('show');
1216                 } else {
1217                     $('#modal-order-main #history-panel').removeClass('show').addClass('hide');
1218                     $('#modal-order-main #items-panel').removeClass('hide').addClass('show');
1219                     if(effective_create_items == 'receiving') {
1220                         $("#acq-create-receiving").show();
1221                         $("#acq-create-ordering").hide();
1222                         if(row.items && row.items.length) {
1223                             row.items.forEach(function(item, index) {
1224                                 cloneItemBlock(index, '[% UniqueItemFields | html %]', function(block_id) {
1225                                     var block = $('#'+block_id).hide();
1226                                     Object.keys(item).forEach(function(key) {
1227                                         block
1228                                             .find('*[name=kohafield][value="'+key+'"]')
1229                                             .parent()
1230                                             .find('*[name=field_value]')
1231                                             .val(item[key].field_value);
1232                                     });
1233                                     addItemInList(block_id, '[% UniqueItemFields | html %]');
1234                                     block.find("input[name='buttonPlus']").val( (window.MSG_ADDITEM_JS_UPDATEITEM ) );
1235                                     block.find("input[name='buttonPlusMulti']").remove();
1236                                     block.find("input[name='multiValue']").remove();
1237                                 });
1238                             });
1239                         }
1240                         cloneItemBlock((row.items && row.items.length) || 0, '[% UniqueItemFields | html %]');
1241                     } else if (effective_create_items == 'ordering') {
1242                         $("#acq-create-receiving").hide();
1243                         $("#acq-create-ordering").show();
1244                         if(row.items.length) {
1245                             $("#acq-create-ordering tbody").append(
1246                                 row.items.map(function(item) {
1247                                     var tr = $('<tr id="item_'+item.item_id+'"/>');
1248                                     _build_item(item, tr);
1249                                     return tr;
1250                                 })
1251                             );
1252                         }
1253                     } else {
1254                         $('#modal-order-main #items-panel').removeClass('show').addClass('hide');
1255                     }
1256                 }
1257                 if(row.invoice_currency && row.invoice_currency != active_currency) {
1258                     $("#select_currency").show();
1259                     $("#unitprice").prop("readonly", true);
1260                     $("#change_currency").prop('checked', true);
1261                     $("#invoice_currency").val(row.invoice_currency);
1262                     $("#invoice_unitprice").val(row.invoice_unit_price);
1263                 } else {
1264                     $("#select_currency").hide();
1265                     $("#unitprice").prop("readonly", "");
1266                     $("#change_currency").prop('checked', false);
1267                     $("#invoice_currency").val(active_currency);
1268                     $("#invoice_unitprice").val('');
1269                 }
1270
1271                 disabledBudgetsCopy = $('#bookfund').html();
1272                 $('#bookfund .inactive_budget').remove();
1273
1274                 $('#showallbudgets').click(function() {
1275                     if ($(this).is(":checked")) {
1276                         $('#bookfund').html(disabledBudgetsCopy); //Puts back all the funds
1277                     }
1278                     else {
1279                         $('#bookfund .inactive_budget').remove();
1280                     }
1281                 });
1282                 set_modal_buttons();
1283             };
1284
1285             $('.modal-prev').click(function() {
1286                 var modal = $("#order_edit");
1287                 keep_row();
1288                 var n = modal.data('n');
1289                 if(n > 0) {
1290                     n--;
1291                     modal.data('row', JSON.parse(JSON.stringify(api.row(n).data())));
1292                     modal.data('n', n);
1293                     set_editor();
1294                 } else {
1295                     $('.modal-next, .modal-prev').prop('disabled', true);
1296                     orders_table.one('draw.dt', function() {
1297                         var info = api.page.info();
1298                         n = info.end - info.start - 1;
1299                         modal.data('row', JSON.parse(JSON.stringify(api.row(n).data())));
1300                         modal.data('n', n);
1301                         set_editor();
1302                     });
1303                     api.page('previous').draw( 'page' );
1304                 }
1305             });
1306
1307             $('.modal-next').click(function() {
1308                 var modal = $("#order_edit");
1309                 keep_row();
1310                 var n = modal.data('n');
1311                 var info = api.page.info();
1312                 if(n < info.end - info.start - 1) {
1313                     n++;
1314                     modal.data('row', JSON.parse(JSON.stringify(api.row(n).data())));
1315                     modal.data('n', n);
1316                     set_editor();
1317                 } else {
1318                     $('.modal-next, .modal-prev').prop('disabled', true);
1319                     orders_table.one('draw.dt', function() {
1320                         var info = api.page.info();
1321                         n = 0;
1322                         modal.data('row', JSON.parse(JSON.stringify(api.row(n).data())));
1323                         modal.data('n', n);
1324                         set_editor();
1325                     });
1326                     api.page('next').draw( 'page' );
1327                 }
1328             });
1329
1330             $('.modal-save').click(function() {
1331
1332                 //We need to validate the item forms here - if one is opened with missing subfields at
1333                 //confirm, it would be submitted without this
1334                 var _alertString= _("Form not submitted because of the following problem(s)")+"\n";
1335                 _alertString +="-------------------------------------------------------------------\n\n";
1336                 var empty_mandatory_fields = 0;
1337                 $('div[id^="itemblock"]').each(function(){
1338                     var item_form = $(this);
1339                     var empty_item_mandatory = CheckMandatorySubfields(item_form);
1340                     empty_mandatory_fields += empty_item_mandatory;
1341                 });
1342                 if (empty_mandatory_fields > 0) {
1343                     _alertString +="\n- " + _("%s item mandatory fields empty").format(empty_mandatory_fields);
1344                     alert(_alertString);
1345                     return false;
1346                 }
1347
1348                 var saved_rows = save_row();
1349                 if(Object.keys(saved_rows).length) $('.save').prop('disabled', false);
1350                 $("#order_edit").modal('hide');
1351                 [% IF only_one_order %]
1352                     $(".save").click();
1353                 [% END %]
1354             })
1355
1356             $("#order_edit").on("show.bs.modal", function (event) {
1357                 var anchor = $(event.relatedTarget);
1358                 var n = anchor.data("row");
1359                 var row = api.row(n).data();
1360
1361                 var modal = $(this);
1362                 modal.data('row', JSON.parse(JSON.stringify(row)));
1363                 modal.data('n', n);
1364                 modal.data('keep', modal.data('saved')||{});
1365                 //$('#modal-order-main').tabs("option", "active", 0);
1366                 set_editor();
1367             });
1368
1369             $("#order_edit").on("hide.bs.modal", function() {
1370             [% IF only_one_order %] [%# On the single case, dismissing the modal equals to cancelling %]
1371                 location.href = "/cgi-bin/koha/acqui/parcel.pl?invoiceid=[% invoiceid | html %]&sticky_filters=1";
1372             [% ELSE %]
1373                 $("#child_orders_table").DataTable().off('preDraw').destroy();
1374             [% END %]
1375             });
1376         });
1377     </script>
1378 [% END %]
1379
1380 [% INCLUDE 'intranet-bottom.inc' %]