From 292ad799ed84127026534e351bdd16663f76b1f8 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Tue, 23 Aug 2011 17:03:28 -0400 Subject: [PATCH] Add to existing PO (by ID) from related items page In the View/Place orders page for a bib record, it's now possible to add a lineitem representing the bib record to an existing purchase order. Includes a new general API call for adding a LI to a PO. Signed-off-by: Bill Erickson Signed-off-by: Mike Rylander --- .../lib/OpenILS/Application/Acq/Order.pm | 49 +++++++++++++++++++ Open-ILS/web/js/dojo/openils/acq/nls/acq.js | 5 +- .../web/js/ui/default/acq/lineitem/related.js | 46 +++++++++++++++++ .../default/acq/lineitem/related.tt2 | 19 +++++++ 4 files changed, 118 insertions(+), 1 deletion(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Order.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Order.pm index dafa288e6e..8530709363 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Order.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Order.pm @@ -3173,5 +3173,54 @@ sub clone_distrib_form { return $new_form->id; } +__PACKAGE__->register_method( + method => 'add_li_to_po', + api_name => 'open-ils.acq.purchase_order.add_lineitem', + signature => { + desc => q/Adds a lineitem to an existing purchase order/, + params => [ + {desc => 'Authentication token', type => 'string'}, + {desc => 'The purchase order id', type => 'number'}, + {desc => 'The lineitem ID', type => 'number'}, + ], + return => {desc => 'Streams a total versus completed counts object, event on error'} + } +); + +sub add_li_to_po { + my($self, $conn, $auth, $po_id, $li_id) = @_; + + my $e = new_editor(authtoken => $auth, xact => 1); + return $e->die_event unless $e->checkauth; + + my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn); + + my $po = $e->retrieve_acq_purchase_order($po_id) + or return $e->die_event; + + my $li = $e->retrieve_acq_lineitem($li_id) + or return $e->die_event; + + return $e->die_event unless + $e->allowed('CREATE_PURCHASE_ORDER', $po->ordering_agency); + + unless ($po->state =~ /new|pending/) { + $e->rollback; + return {success => 0, po => $po, error => 'bad-po-state'}; + } + + unless ($li->state =~ /new|order-ready|pending-order/) { + $e->rollback; + return {success => 0, li => $li, error => 'bad-li-state'}; + } + + $li->purchase_order($po_id); + $li->state('pending-order'); + update_lineitem($mgr, $li) or return $e->die_event; + + $e->commit; + return {success => 1}; +} + 1; diff --git a/Open-ILS/web/js/dojo/openils/acq/nls/acq.js b/Open-ILS/web/js/dojo/openils/acq/nls/acq.js index 2d89f12c28..0525d48e11 100644 --- a/Open-ILS/web/js/dojo/openils/acq/nls/acq.js +++ b/Open-ILS/web/js/dojo/openils/acq/nls/acq.js @@ -78,5 +78,8 @@ "LOAD_TERMS_FIRST" : "You can't retrieve records until you've loaded a CSV file\nwith bibliographic IDs in the first column.", "SELECT_SEARCH_FIELD": "Select Search Field", "LIBRARY_INITIATED": "Library Initiated", - "DEL_LI_FROM_PO": "That item has already been ordered! Deleting it now will not revoke or modify any order that has been placed with a vendor. Deleting the item may put the system's idea of your purchase order in a state that is inconsistent with reality. Are you sure you mean to do this?" + "DEL_LI_FROM_PO": "That item has already been ordered! Deleting it now will not revoke or modify any order that has been placed with a vendor. Deleting the item may put the system's idea of your purchase order in a state that is inconsistent with reality. Are you sure you mean to do this?", + "ADD_LI_TO_PO_BAD_PO_STATE" : "The selected PO has already been activated", + "ADD_LI_TO_PO_BAD_LI_STATE" : "The selected lineitem is not in a state that can be added to a purchase order" + } diff --git a/Open-ILS/web/js/ui/default/acq/lineitem/related.js b/Open-ILS/web/js/ui/default/acq/lineitem/related.js index c898314593..0f3b91b833 100644 --- a/Open-ILS/web/js/ui/default/acq/lineitem/related.js +++ b/Open-ILS/web/js/ui/default/acq/lineitem/related.js @@ -6,6 +6,9 @@ dojo.require("openils.PermaCrud"); dojo.require('openils.BibTemplate'); dojo.require('fieldmapper.OrgUtils'); +dojo.requireLocalization('openils.acq', 'acq'); +var localeStrings = dojo.i18n.getLocalization('openils.acq', 'acq'); + var liTable; var identTarget; var bibRecord; @@ -112,6 +115,11 @@ function prepareButtons() { acqLitSavePlDialog.show(); } ); + addToPoButton.onClick = createLi( + function() { /* oncomplete */ + addToPoDialog.show(); + } + ); createPoButton.onClick = createLi( function() { /* oncomplete */ liTable._loadPOSelect(); @@ -139,6 +147,44 @@ function load() { prepareButtons(); fetchRelated(); + dojo.connect(addToPoSave, 'onClick', addToPo) + openils.Util.registerEnterHandler(addToPoInput.domNode, addToPo); +} + +var _addToPoHappened = false; +function addToPo(args) { + var poId = addToPoInput.attr('value'); + if (!poId) return false; + if (_addToPoHappened) return false; + + var liId = liTable.getSelected()[0].id(); + console.log("adding li " + liId + " to PO " + poId); + + // hmm, addToPo is invoked twice for some reason... + _addToPoHappened = true; + + fieldmapper.standardRequest( + ['open-ils.acq', 'open-ils.acq.purchase_order.add_lineitem'], + { async : true, + params : [openils.User.authtoken, poId, liId], + oncomplete : function(r) { + var resp = openils.Util.readResponse(r); + if (resp.success) { + location.href = oilsBasePath + '/acq/po/view/' + poId; + } else { + _addToPoHappened = false; + if (resp.error == 'bad-po-state') { + alert(localeStrings.ADD_LI_TO_PO_BAD_PO_STATE); + } else if (resp.error == 'bad-li-state') { + alert(localeStrings.ADD_LI_TO_PO_BAD_LI_STATE); + } + } + } + } + ); + + addToPoDialog.hide(); + return false; // prevent form submission } openils.Util.addOnLoad(load); diff --git a/Open-ILS/web/templates/default/acq/lineitem/related.tt2 b/Open-ILS/web/templates/default/acq/lineitem/related.tt2 index 6d47ec9f33..61ee19fef0 100644 --- a/Open-ILS/web/templates/default/acq/lineitem/related.tt2 +++ b/Open-ILS/web/templates/default/acq/lineitem/related.tt2 @@ -40,11 +40,30 @@ + + + [% INCLUDE "default/acq/common/info.tt2" which = "Related" %] [% INCLUDE "default/acq/common/li_table.tt2" %] -- 2.43.2