1 /* The code in this file relies on common.js */
3 dojo.require("dojo.cookie");
4 dojo.require("openils.Util");
5 dojo.require("openils.User");
6 dojo.require("openils.CGI");
7 dojo.require("openils.PermaCrud");
12 return dojo.byId("serialStrings").getString("batch_receive." + k).
17 return dojo.byId("serialStrings").
18 getFormattedString("batch_receive." + k, args).replace("\\n", "\n");
21 function BatchReceiver() {
24 this.init = function(authtoken, bib_id, sub_id) {
26 this.user = new openils.User({"authtoken": authtoken});
27 this.pcrud = new openils.PermaCrud({"authtoken": authtoken});
28 this.authtoken = authtoken;
31 hide("batch_receive_sub");
32 hide("batch_receive_entry");
33 hide("batch_receive_bibdata_bits");
34 hide("batch_receive_sub_bits");
35 hide("batch_receive_issuance_bits");
36 hide("batch_receive_issuance");
38 dojo.byId("bib_lookup_submit").disabled = false;
39 dojo.byId("bib_search_term").value = "";
42 show("batch_receive_bib");
43 dojo.byId("bib_search_term").focus();
46 if (!this.entry_tbody) {
47 this.entry_tbody = dojo.byId("entry_tbody");
48 this.template = this.entry_tbody.removeChild(
49 dojo.byId("entry_template")
53 this._clear_entry_batch_row();
55 this._call_number_cache = null;
56 this._prepared_call_number_controls = {};
57 this._location_by_lib = {};
59 /* empty the entry receiving table if we're starting over */
60 if (this.item_cache) {
61 for (var id in this.item_cache) {
62 this.finish_receipt(this.item_cache[id]);
63 hard_empty(this.entry_tbody);
65 /* XXX incredibly, running hard_empty() more than once seems to be
66 * good and necessary. There's a bug under the covers somewhere,
67 * but this keeps it out of sight for the moment. */
68 hard_empty(this.entry_tbody);
70 hard_empty(this.entry_tbody);
76 this.bib_lookup(bib_id, null, true, sub_id);
81 this._clear_entry_batch_row = function() {
83 dojo.byId("entry_batch_row").childNodes,
85 if (node.nodeType == 1 &&
86 node.getAttribute("name") != "barcode")
92 this._show_bibdata_bits = function() {
93 hard_empty("title_here");
94 dojo.byId("title_here").appendChild(T(this.bibdata.mvr.title()));
95 hard_empty("author_here");
97 if (this.bibdata.mvr.author()) {
98 dojo.byId("author_here").appendChild(T(this.bibdata.mvr.author()));
99 show("author_here_holder");
101 hide("author_here_holder");
104 show("batch_receive_bibdata_bits");
107 this._sub_label = function(sub) {
108 /* XXX use a formatting string from serial.properties */
109 return sub.id() + ": (" + sub.owning_lib().shortname() + ") " +
110 D(sub.start_date()) + " - " + D(sub.end_date());
113 this._show_sub_bits = function() {
114 hard_empty("sublabel_here");
116 T(this._sub_label(this.sub)),
120 hide("batch_receive_sub");
121 show("batch_receive_sub_bits");
124 this._show_issuance_bits = function() {
125 hide("batch_receive_issuance");
126 hard_empty("issuance_label_here");
128 T(this.issuance.label()),
129 "issuance_label_here",
132 show("batch_receive_issuance_bits");
135 this._get_receivable_issuances = function() {
140 fieldmapper.standardRequest(
141 ["open-ils.serial", "open-ils.serial.issuances.receivable"], {
142 "params": [this.authtoken, this.sub.id()],
144 "onresponse": function(r) {
145 if (r = openils.Util.readResponse(r))
158 this._build_circ_modifier_dropdown = function() {
159 if (!this._built_circ_modifier_dropdown) {
160 var menulist = dojo.create("menulist");
161 var menupopup = dojo.create("menupopup", null, menulist, "only");
163 "menuitem", {"value": 0, "label": S("none")},
168 fieldmapper.standardRequest(
169 ["open-ils.circ", "open-ils.circ.circ_modifier.retrieve.all"],{
170 "params": [{"full": true}],
172 "onresponse": function(r) {
173 if (mods = openils.Util.readResponse(r)) {
176 return a.code() > b.code() ? 1 :
177 b.code() > a.code() ? -1 :
185 "label": document.getElementById('commonStrings').getFormattedString('staff.circ_modifier.display',[mod.code(),mod.name(),mod.description()])
195 /* in this case, discard menulist and menupopup */
196 this._built_circ_modifier_dropdown =
197 dojo.create("description", {"value": "-"});
199 this._built_circ_modifier_dropdown = menulist;
203 return dojo.clone(this._built_circ_modifier_dropdown);
206 this._extend_circ_modifier_for_batch = function(control) {
208 "menuitem", {"value": -1, "label": "---"},
209 dojo.query("menupopup", control)[0],
215 this._build_location_dropdown = function(locs, add_unset_value) {
216 var menulist = dojo.create("menulist");
217 var menupopup = dojo.create("menupopup", null, menulist, "only");
219 if (add_unset_value) {
221 "menuitem", {"value": -1, "label": "---"}, menupopup, "first"
229 "value": loc.id(), "label": loc.name()
238 this._get_locations_for_lib = function(lib) {
239 if (!this._location_by_lib[lib]) {
240 fieldmapper.standardRequest(
241 ["open-ils.circ", "open-ils.circ.copy_location.retrieve.all"],{
242 "params": [lib, false, true],
244 "onresponse": function(r) {
245 if (locs = openils.Util.readResponse(r))
246 self._location_by_lib[lib] = locs;
252 return this._location_by_lib[lib];
255 this._build_call_number_control = function(item) {
256 /* In any case, give a dropdown of call numbers related to the
257 * same bre as the subscription relates to. */
258 if (!this._call_number_cache) {
259 this._call_number_cache = this.pcrud.search(
261 "record": this.sub.record_entry()
263 "order_by": {"acn": "label"}, /* XXX wrong sorting? */
268 if (typeof item == "undefined") {
269 /* In this case, no further limiting of call numbers for now,
270 * although ideally it might be nice to limit to call numbers
271 * with owning_lib matching the holding_lib of the distribs
272 * that ultimately relate to the items. */
274 var menulist = dojo.create("menulist", {
275 "editable": "true", "className": "cn"
277 var menupopup = dojo.create("menupopup", null, menulist, "only");
279 openils.Util.uniqueObjects(this._call_number_cache, "label").
284 "value": cn.label(), "label": cn.label()
292 /* In this case, limit call numbers by owning_lib matching
293 * distributions's holding_lib. */
295 var lib = item.stream().distribution().holding_lib().id();
296 if (!this._prepared_call_number_controls[lib]) {
297 var menulist = dojo.create("menulist", {
298 "editable": "true", "className": "cn"
300 var menupopup = dojo.create("menupopup", null, menulist,"only");
301 this._call_number_cache.filter(
302 function(cn) { return cn.owning_lib() == lib; }
307 "value": cn.label(), "label": cn.label()
312 this._prepared_call_number_controls[lib] = menulist;
314 return dojo.clone(this._prepared_call_number_controls[lib]);
318 this._build_batch_location_dropdown = function() {
319 var menulist = dojo.create("menulist");
320 var menupopup = dojo.create("menupopup",null,menulist);
321 dojo.create("menuitem", {"value": -1, "label": "---"}, menupopup);
323 fieldmapper.standardRequest(
325 "open-ils.circ.copy_location.retrieve.distinct.atomic"],{
328 "onresponse": function(r) {
329 if (list = openils.Util.readResponse(r)) {
334 "value": locname, "label": locname
347 this._build_receive_toggle = function(item) {
350 "oncommand": function(ev) {
351 self._disable_row(item.id(), !ev.target.checked);
358 this._disable_row = function(item_id, disabled) {
359 var row = this.rows[item_id];
360 dojo.query("textbox,menulist", row).forEach(
361 function(element) { element.disabled = disabled; }
363 this._row_disabled(row, disabled);
366 this._row_disabled = function(row, disabled) {
367 if (typeof(row) == "string") row = this.rows[row];
369 var checkbox = dojo.query("checkbox", row)[0];
371 if (typeof(disabled) != "undefined")
372 checkbox.checked = !disabled;
374 return !checkbox.checked;
377 this._row_field_value = function(row, field, value) {
378 if (typeof(row) == "string") row = this.rows[row];
380 var node = dojo.query("*", node_by_name(field, row))[0];
382 if (typeof(value) == "undefined")
388 this._user_wants_autogen = function() {
389 return dojo.byId("autogen_barcodes").checked;
392 this._get_autogen_potentials = function(item_id) {
393 var hit_a_wall = false;
395 return [openils.Util.objectProperties(this.rows).sort(num_sort).filter(
399 } else if (id <= item_id || self._row_disabled(id)) {
401 } else if (self._row_field_value(id, "barcode")) {
411 this._prepare_autogen_control = function() {
412 dojo.attr("autogen_barcodes",
413 "command", function(ev) {
414 if (!ev.target.checked) {
415 var list = self._have_autogen_barcodes();
416 if (list.length && confirm(S("autogen_barcodes.remove"))) {
419 self._row_field_value(id, "barcode", "");
420 self.rows[id]._has_autogen_barcode = false;
429 this._have_autogen_barcodes = function() {
431 for (var id in this.rows)
432 if (this.rows[id]._has_autogen_barcode) list.push(id);
436 this._cn_exists_but_not_for_lib = function(lib, value) {
437 var exists = this._call_number_cache.filter(
438 function(cn) { return cn.label() == value }
440 var for_lib = exists.filter(
441 function(cn) { return cn.owning_lib() == lib; }
443 return (exists.length && !for_lib.length);
446 this._call_number_confirm_for_lib = function(lib, value) {
447 if (!this._has_confirmed_cn_for)
448 this._has_confirmed_cn_for = {};
450 if (typeof(this._has_confirmed_cn_for[lib.id()]) == "undefined") {
451 if (this._cn_exists_but_not_for_lib(lib.id(), value)) {
452 this._has_confirmed_cn_for[lib.id()] = confirm(
453 F("cn_for_lib", [lib.shortname()])
456 this._has_confirmed_cn_for[lib.id()] = true;
460 return this._has_confirmed_cn_for[lib.id()];
463 this._confirm_row_field_application = function(id, key, value) {
464 if (key == "call_number") { /* XXX make a dispatch table so we can do
465 this for other fields too */
466 return this._call_number_confirm_for_lib(
467 this.item_cache[id].stream().distribution().holding_lib(),
475 this._location_by_name = function(id, value) {
476 var lib = this.item_cache[id].stream().distribution().
478 var winners = this._location_by_lib[lib].filter(
479 function(loc) { return loc.name() == value; }
481 if (winners.length) {
482 return winners[0].id();
488 this._set_all_enabled_rows = function(key, value) {
489 /* do NOT do trimming here, set whitespace as is. */
490 for (var id in this.rows) {
491 if (!this._row_disabled(id)) {
492 if (this._confirm_row_field_application(id, key, value)) {
493 if (key == "location") /* kludge for this field */ {
494 if (actual = this._location_by_name(id, value))
495 this._row_field_value(id, key, actual);
497 this._row_field_value(id, key, value);
504 this.bib_lookup = function(bib_search_term, evt, is_actual_id, sub_id) {
505 if (evt && evt.keyCode != 13) return;
507 if (!bib_search_term) {
508 var bib_search_term = dojo.byId("bib_search_term").value.trim();
509 if (!bib_search_term.length) {
510 alert(S("bib_lookup.empty"));
515 hide("batch_receive_sub");
516 hide("batch_receive_entry");
519 dojo.byId("bib_lookup_submit").disabled = true;
520 fieldmapper.standardRequest(
522 "open-ils.serial.biblio.record_entry.by_identifier.atomic"], {
525 "require_subscriptions": true,
527 "is_actual_id": is_actual_id
531 "oncomplete": function(r) {
532 /* These two things better come before readResponse(),
533 * which can throw exceptions. */
535 dojo.byId("bib_lookup_submit").disabled = false;
537 var list = openils.Util.readResponse(r, false, true);
538 if (list && list.length) {
539 if (list.length > 1) {
540 /* XXX TODO just let the user pick one from a list,
541 * although this circumstance seems really
542 * unlikely. It just can't happen for TCN, and
543 * wouldn't be likely for ISxN or UPC... ? */
544 alert(S("bib_lookup.multiple"));
546 self.bibdata = list[0];
547 self._show_bibdata_bits();
548 self.choose_subscription(sub_id);
551 alert(S("bib_lookup.not_found"));
555 dojo.byId("bib_search_term").reset();
556 dojo.byId("bib_search_term").focus();
564 this.choose_subscription = function(sub_id) {
565 hide("batch_receive_bib");
566 hide("batch_receive_entry");
567 hide("batch_receive_sub_bits");
568 hide("batch_receive_issuance");
570 var subs = this.bibdata.bre.subscriptions();
573 this.choose_issuance(
574 subs.filter(function(o) { return o.id() == sub_id; })[0]
576 } else if (subs.length > 1) {
577 var menulist = dojo.create("menulist", {"id": "sub_chooser"});
578 var menupopup = dojo.create("menupopup", {}, menulist, "only");
580 this.bibdata.bre.subscriptions().forEach(
584 "label": self._sub_label(sub),
591 hard_empty(dojo.byId("sub_chooser_here"));
593 dojo.place(menulist, dojo.byId("sub_chooser_here"), "only");
594 show("batch_receive_sub");
596 this.choose_issuance(subs[0]);
600 this.choose_issuance = function(sub) {
601 hide("batch_receive_bib");
602 hide("batch_receive_entry");
603 hide("batch_receive_sub");
605 if (typeof(sub) == "undefined") { /* sub chosen from menu */
606 var sub_id = dojo.byId("sub_chooser").value;
607 this.sub = this.bibdata.bre.subscriptions().filter(
608 function(o) { return o.id() == sub_id; }
610 } else { /* only one sub possible, passed in directly */
614 this._show_sub_bits();
616 this.issuances = this._get_receivable_issuances(); /* sync */
618 if (this.issuances.length > 1) {
619 var menulist = dojo.create("menulist", {"id": "issuance_chooser"});
620 var menupopup = dojo.create("menupopup", {}, menulist, "only");
624 if (a.date_published()>b.date_published()) return 1;
625 else if (b.date_published()>a.date_published()) return -1;
632 "label": issuance.label(),
633 "value": issuance.id()
639 hard_empty("issuance_chooser_here");
640 dojo.place(menulist, dojo.byId("issuance_chooser_here"), "only");
642 show("batch_receive_issuance");
643 } else if (this.issuances.length) {
644 this.load_entry_form(this.issuances[0]);
646 alert(S("issuance_lookup.none"));
652 this.load_entry_form = function(issuance) {
653 if (typeof(issuance) == "undefined") {
654 var issuance_id = dojo.byId("issuance_chooser").value;
655 this.issuance = this.issuances.filter(
656 function(o) { return o.id() == issuance_id; }
659 this.issuance = issuance;
662 this._show_issuance_bits();
663 this._prepare_autogen_control();
667 fieldmapper.standardRequest(
669 "open-ils.serial.items.receivable.by_issuance.atomic"], {
670 "params": [this.authtoken, this.issuance.id()],
672 "onresponse": function(r) {
675 if (list = openils.Util.readResponse(r, false, true)) {
681 list.forEach(function(o) {self.add_entry_row(o);});
683 self.build_batch_entry_row();
684 dojo.byId("batch_receive_with_units").doCommand();
686 show("batch_receive_entry");
689 alert(S("item_lookup.none"));
690 if (self.issuances.length) self.choose_issuance();
699 this.toggle_receive_with_units = function(ev) {
700 var head_row = dojo.byId("batch_receive_entry_thead");
701 var batch_row = dojo.byId("entry_batch_row");
704 "barcode", "call_number", "price", "location", "circ_modifier"
707 var table_cell_func = ev.target.checked ?
708 show_table_cell : hide_table_cell;
711 if (batch_row) table_cell_func(node_by_name(key, batch_row));
712 if (head_row) table_cell_func(node_by_name(key, head_row));
714 for (var id in self.rows) {
715 table_cell_func(node_by_name(key, self.rows[id]));
720 if (!ev.target.checked) {
721 /* XXX As of the time of this writing, a blank barcode field will
722 * avoid unit creation */
723 this._set_all_enabled_rows("barcode", "");
727 this.toggle_all_receive = function(checked) {
728 for (var id in this.rows) {
729 this._disable_row(id, !checked);
733 this.build_batch_entry_row = function() {
734 var row = dojo.byId("entry_batch_row");
736 this.batch_controls = {};
738 node_by_name("note", row).appendChild(
739 this.batch_controls.note = dojo.create("textbox", {"size": 20})
742 node_by_name("location", row).appendChild(
743 this.batch_controls.location =
744 this._build_batch_location_dropdown()
747 node_by_name("circ_modifier", row).appendChild(
748 this.batch_controls.circ_modifier =
749 this._extend_circ_modifier_for_batch(
750 this._build_circ_modifier_dropdown() /* for all OUs */
754 node_by_name("call_number", row).appendChild(
755 this.batch_controls.call_number = this._build_call_number_control()
758 node_by_name("price", row).appendChild(
759 this.batch_controls.price = dojo.create("textbox", {"size": 9})
762 node_by_name("receive", row).appendChild(
765 "oncommand": function(ev) {
766 self.toggle_all_receive(ev.target.checked);
773 node_by_name("apply", row).appendChild(
774 dojo.create("button", {
776 "oncommand": function() { self.apply_batch_values(); }
781 this.apply_batch_values = function() {
782 var row = dojo.byId("entry_batch_row");
784 for (var key in this.batch_controls) {
785 var value = this.batch_controls[key].value;
786 if (value != "" && value != -1)
787 this._set_all_enabled_rows(key, value);
790 /* XXX genericize for all fields? */
791 delete this._has_confirmed_cn_for;
794 this.add_entry_row = function(item) {
795 this.item_cache[item.id()] = item;
796 var row = this.rows[item.id()] = dojo.clone(this.template);
798 function n(s) { return node_by_name(s, row); } /* typing saver */
800 var stream_dist_label = item.stream().distribution().label();
801 if (item.stream().routing_label())
802 stream_dist_label += " / " + item.stream().routing_label();
804 n("holding_lib").appendChild(
807 "value": item.stream().distribution().
808 holding_lib().shortname(),
809 "tooltiptext": stream_dist_label
814 n("barcode").appendChild(
818 "tabindex": 10000 + Number(item.id()), /* is this right? */
819 "onchange": function() {
820 self.autogen_if_appropriate(this, item.id());
826 n("location").appendChild(
827 this._build_location_dropdown(
828 this._get_locations_for_lib(
829 item.stream().distribution().holding_lib().id()
834 n("note").appendChild(dojo.create("textbox", {"size": 20}));
835 n("circ_modifier").appendChild(this._build_circ_modifier_dropdown());
836 n("call_number").appendChild(this._build_call_number_control(item));
837 n("price").appendChild(dojo.create("textbox", {"size": 9}));
838 n("receive").appendChild(this._build_receive_toggle(item));
840 this.entry_tbody.appendChild(row);
843 this.receive = function() {
845 var confirmed_missing_units = false;
847 for (var id in this.rows) {
848 if (this._row_disabled(id))
851 var item = this.item_cache[id];
853 /* Don't trim() call_number field, as existing call numbers
854 * are yielded by their label field, not by id, and if
855 * they start or end in spaces, we'll unintentionally create
856 * a new, different CN if we trim that */
857 var cn_string = this._row_field_value(id, "call_number");
858 var barcode = this._row_field_value(id, "barcode").trim();
860 if (barcode && cn_string.length) {
861 var unit = new sunit();
862 unit.barcode(barcode);
864 ["price", "location", "circ_modifier"].forEach(
866 var value = self._row_field_value(id, field).trim();
867 if (value) unit[field](value);
871 unit.call_number(cn_string);
873 } else if (barcode && !cn_string.length) {
874 alert(S("missing_cn"));
876 } else if (!confirmed_missing_units) {
878 (!dojo.byId("batch_receive_with_units").checked) ||
879 confirm(S("missing_units"))
881 confirmed_missing_units = true;
887 var note_value = this._row_field_value(id, "note").trim();
889 var note = new sin();
892 note.title(S("receive_time_note"));
893 note.value(note_value);
902 fieldmapper.standardRequest(
903 ["open-ils.serial", "open-ils.serial.receive_items.one_unit_per"],{
904 "params": [this.authtoken, items, this.sub.record_entry()],
906 "oncomplete": function(r) {
908 while (item_id = openils.Util.readResponse(r))
909 self.finish_receipt(item_id);
919 this.finish_receipt = function(item_id) {
920 hard_empty(this.rows[item_id]);
921 dojo.destroy(this.rows[item_id]);
922 delete this.rows[item_id];
923 delete this.item_cache[item_id];
926 this.autogen_if_appropriate = function(textbox, item_id) {
927 if (this._user_wants_autogen() && textbox.value) {
928 var [list, question] = this._get_autogen_potentials(item_id);
930 if (question && !confirm(S("autogen_barcodes.questionable")))
935 fieldmapper.standardRequest(
936 ["open-ils.cat", "open-ils.cat.item.barcode.autogen"], {
938 this.authtoken, textbox.value, list.length
941 "onresponse": function(r) {
942 r = openils.Util.readResponse(r, false, true);
944 for (var i = 0; i < r.length; i++) {
945 var row = self.rows[list[i]];
946 self._row_field_value(
949 row._has_autogen_barcode = true;
959 } /* do nothing for empty list */
963 this.init.apply(this, arguments);
967 var cgi = new openils.CGI();
969 batch_receiver = new BatchReceiver(
970 (typeof ses == "function" ? ses() : 0) ||
971 cgi.param("ses") || dojo.cookie("ses"),
972 cgi.param("docid") || null, cgi.param("subid") || null