Add to existing PO (by ID) from related items page
authorBill Erickson <berick@esilibrary.com>
Tue, 23 Aug 2011 21:03:28 +0000 (17:03 -0400)
committerMike Rylander <mrylander@gmail.com>
Wed, 24 Aug 2011 12:47:59 +0000 (08:47 -0400)
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 <berick@esilibrary.com>
Signed-off-by: Mike Rylander <mrylander@gmail.com>

Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Order.pm
Open-ILS/web/js/dojo/openils/acq/nls/acq.js
Open-ILS/web/js/ui/default/acq/lineitem/related.js
Open-ILS/web/templates/default/acq/lineitem/related.tt2

index dafa288..8530709 100644 (file)
@@ -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;
 
index 2d89f12..0525d48 100644 (file)
@@ -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"
+
 }
index c898314..0f3b91b 100644 (file)
@@ -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);
index 6d47ec9..61ee19f 100644 (file)
         <button jsId="addToPlButton" dojoType="dijit.form.Button">
             Add to Selection List
         </button>
+        <button jsId="addToPoButton" dojoType="dijit.form.Button">
+            Add to Purchase Order
+        </button>
         <button jsId="createPoButton" dojoType="dijit.form.Button">
             Create Purchase Order
         </button>
     </div>
 
+    <div class="hidden">
+        <div dojoType="dijit.Dialog" jsId='addToPoDialog'>
+            <table class='dijitTooltipTable'>
+                <tr>
+                    <td><label>Enter the PO #: </label></td>
+                    <td><input jsId='addToPoInput' dojoType="dijit.form.TextBox" /></td>
+                </tr>
+                <tr>
+                    <td colspan='2' align='center'>
+                        <button dojoType='dijit.form.Button' jsId='addToPoSave' type="submit">Save</button>
+                    </td>
+                </tr>
+            </table>
+        </div>
+    </div>
+
 </div>
 [% INCLUDE "default/acq/common/info.tt2" which = "Related" %]
 [% INCLUDE "default/acq/common/li_table.tt2" %]