[open-ils-commits] r16062 - in trunk/Open-ILS: src/extras src/perlmods/OpenILS/Application/Acq web/js/dojo/openils/acq/nls web/js/ui/default/acq/po web/templates/default/acq/po (senator)

svn at svn.open-ils.org svn at svn.open-ils.org
Tue Mar 30 13:50:29 EDT 2010


Author: senator
Date: 2010-03-30 13:50:25 -0400 (Tue, 30 Mar 2010)
New Revision: 16062

Modified:
   trunk/Open-ILS/src/extras/ils_events.xml
   trunk/Open-ILS/src/perlmods/OpenILS/Application/Acq/Order.pm
   trunk/Open-ILS/web/js/dojo/openils/acq/nls/acq.js
   trunk/Open-ILS/web/js/ui/default/acq/po/view_po.js
   trunk/Open-ILS/web/templates/default/acq/po/view.tt2
Log:
Acq: PO interface now tells you if activating a PO would breach fund limits.


Modified: trunk/Open-ILS/src/extras/ils_events.xml
===================================================================
--- trunk/Open-ILS/src/extras/ils_events.xml	2010-03-30 17:05:22 UTC (rev 16061)
+++ trunk/Open-ILS/src/extras/ils_events.xml	2010-03-30 17:50:25 UTC (rev 16062)
@@ -651,6 +651,12 @@
 	<event code='1861' textcode='ACQ_PURCHASE_ORDER_TOO_LATE'>
 		<desc xml:lang='en-US'>The requested acq.purchase_order cannot be split because it has gone beyond the "pending" state</desc>
 	</event>
+	<event code='1862' textcode='ACQ_FUND_EXCEEDS_STOP_PERCENT'>
+		<desc xml:lang='en-US'>The requested acq.purchase_order cannot be activated because it would over-encumber a fund.</desc>
+	</event>
+	<event code='1863' textcode='ACQ_FUND_EXCEEDS_WARN_PERCENT'>
+		<desc xml:lang='en-US'>Activating the requested acq.purchase_order would encumber it beyond its warning level.</desc>
+	</event>
 	<event code='1870' textcode='ACQ_LINEITEM_DETAIL_NOT_FOUND'>
 		<desc xml:lang='en-US'>The requested acq.lineitem_detail was not found</desc>
 	</event>

Modified: trunk/Open-ILS/src/perlmods/OpenILS/Application/Acq/Order.pm
===================================================================
--- trunk/Open-ILS/src/perlmods/OpenILS/Application/Acq/Order.pm	2010-03-30 17:05:22 UTC (rev 16061)
+++ trunk/Open-ILS/src/perlmods/OpenILS/Application/Acq/Order.pm	2010-03-30 17:50:25 UTC (rev 16062)
@@ -606,7 +606,7 @@
 # Lineitem Debits
 # ----------------------------------------------------------------------------
 sub create_lineitem_debits {
-    my($mgr, $li) = @_; 
+    my ($mgr, $li, $dry_run) = @_; 
 
     unless($li->estimated_unit_price) {
         $mgr->editor->event(OpenILS::Event->new('ACQ_LINEITEM_NO_PRICE', payload => $li->id));
@@ -634,7 +634,7 @@
             }
         ]);
 
-        create_lineitem_detail_debit($mgr, $li, $lid) or return 0;
+        create_lineitem_detail_debit($mgr, $li, $lid, $dry_run) or return 0;
     }
 
     return 1;
@@ -644,7 +644,7 @@
 # flesh li->provider
 # flesh lid->fund
 sub create_lineitem_detail_debit {
-    my($mgr, $li, $lid) = @_;
+    my ($mgr, $li, $lid, $dry_run) = @_;
 
     my $li_id = ref($li) ? $li->id : $li;
 
@@ -684,6 +684,7 @@
 
     my $debit = create_fund_debit(
         $mgr, 
+        $dry_run,
         fund => $lid->fund->id,
         origin_amount => $li->estimated_unit_price,
         origin_currency_type => $li->provider->currency_type,
@@ -697,34 +698,66 @@
 }
 
 
-# ----------------------------------------------------------------------------
-# Fund Debit
-# ----------------------------------------------------------------------------
-sub create_fund_debit {
-    my($mgr, %args) = @_;
+sub fund_exceeds_balance_stop_percent {
+    return fund_exceeds_balance_percent(
+        @_, "balance_stop_percent", "ACQ_FUND_EXCEEDS_STOP_PERCENT"
+    );
+}
 
-    # Verify the fund is not being spent beyond the hard stop amount
-    my $fund = $mgr->editor->retrieve_acq_fund($args{fund}) or return 0;
+sub fund_exceeds_balance_warning_percent {
+    return fund_exceeds_balance_percent(
+        @_, "balance_warning_percent", "ACQ_FUND_EXCEEDS_WARN_PERCENT"
+    );
+}
 
-    if($fund->balance_stop_percent) {
+sub fund_exceeds_balance_percent {
+    my ($fund, $debit_amount, $e, $method_name, $event_name) = @_;
 
-        my $balance = $mgr->editor->search_acq_fund_combined_balance({fund => $fund->id})->[0];
-        my $allocations = $mgr->editor->search_acq_fund_allocation_total({fund => $fund->id})->[0];
+    if ($fund->$method_name) {
+        my $balance =
+            $e->search_acq_fund_combined_balance({"fund" => $fund->id})->[0];
+        my $allocations =
+            $e->search_acq_fund_allocation_total({"fund" => $fund->id})->[0];
+
         $balance = ($balance) ? $balance->amount : 0;
         $allocations = ($allocations) ? $allocations->amount : 0;
 
-        if( 
+        if ( 
             $allocations == 0 || # if no allocations were ever made, assume we have hit the stop percent
-            ( ( ( ($balance - $args{amount}) / $allocations ) * 100 ) < $fund->balance_stop_percent)) 
-        {
-                $mgr->editor->event(OpenILS::Event->new(
-                    'FUND_EXCEEDS_STOP_PERCENT', 
-                    payload => {fund => $fund->id, debit_amount => $args{amount}}
-                ));
-                return 0;
+            ((($balance - $debit_amount) / $allocations) * 100) <
+                $fund->$method_name
+        ) {
+                $e->event(
+                    new OpenILS::Event(
+                        $event_name, 
+                        "payload" => {
+                            "fund" => $fund,
+                            "debit_amount" => $debit_amount
+                        }
+                    )
+                );
+                return 1;
         }
     }
+    return 0;
+}
 
+# ----------------------------------------------------------------------------
+# Fund Debit
+# ----------------------------------------------------------------------------
+sub create_fund_debit {
+    my($mgr, $dry_run, %args) = @_;
+
+    # Verify the fund is not being spent beyond the hard stop amount
+    my $fund = $mgr->editor->retrieve_acq_fund($args{fund}) or return 0;
+
+    return 0 if
+        fund_exceeds_balance_stop_percent($fund, $args{"amount"}, $mgr->editor);
+    return 0 if
+        $dry_run and fund_exceeds_balance_warning_percent(
+            $fund, $args{"amount"}, $mgr->editor
+        );
+
     my $debit = Fieldmapper::acq::fund_debit->new;
     $debit->debit_type('purchase');
     $debit->encumbrance('t');
@@ -1984,6 +2017,11 @@
 
 __PACKAGE__->register_method(
 	method => 'activate_purchase_order',
+	api_name	=> 'open-ils.acq.purchase_order.activate.dry_run'
+);
+
+__PACKAGE__->register_method(
+	method => 'activate_purchase_order',
 	api_name	=> 'open-ils.acq.purchase_order.activate',
 	signature => {
         desc => q/Activates a purchase order.  This updates the status of the PO
@@ -1999,19 +2037,25 @@
 
 sub activate_purchase_order {
     my($self, $conn, $auth, $po_id) = @_;
+
+    my $dry_run = ($self->api_name =~ /\.dry_run/) ? 1 : 0;
     my $e = new_editor(xact=>1, authtoken=>$auth);
     return $e->die_event unless $e->checkauth;
     my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
-    my $die_event = activate_purchase_order_impl($mgr, $po_id);
+    my $die_event = activate_purchase_order_impl($mgr, $po_id, $dry_run);
     return $die_event if $die_event;
-    $e->commit;
+    if ($dry_run) {
+        $e->rollback;
+    } else {
+        $e->commit;
+    }
     $conn->respond_complete(1);
     $mgr->run_post_response_hooks;
     return undef;
 }
 
 sub activate_purchase_order_impl {
-    my($mgr, $po_id) = @_;
+    my ($mgr, $po_id, $dry_run) = @_;
     my $e = $mgr->editor;
 
     my $po = $e->retrieve_acq_purchase_order($po_id) or return $e->die_event;
@@ -2028,7 +2072,7 @@
 
     while( my $li = $e->search_acq_lineitem($query)->[0] ) {
         $li->state('on-order');
-        create_lineitem_debits($mgr, $li) or return $e->die_event;
+        create_lineitem_debits($mgr, $li, $dry_run) or return $e->die_event;
         update_lineitem($mgr, $li) or return $e->die_event;
         $mgr->post_process( sub { create_lineitem_status_events($mgr, $li->id, 'aur.ordered'); });
         $mgr->respond;

Modified: trunk/Open-ILS/web/js/dojo/openils/acq/nls/acq.js
===================================================================
--- trunk/Open-ILS/web/js/dojo/openils/acq/nls/acq.js	2010-03-30 17:05:22 UTC (rev 16061)
+++ trunk/Open-ILS/web/js/dojo/openils/acq/nls/acq.js	2010-03-30 17:50:25 UTC (rev 16062)
@@ -48,5 +48,10 @@
     'FUND_LIST_ROLLOVER_SUMMARY_FUNDS' : '<b>${1}</b> funds propagated for fiscal year ${0} for the selected locations',
     'FUND_LIST_ROLLOVER_SUMMARY_ROLLOVER_AMOUNT' : '<b>$${1}</b> unspent money rolled over to fiscal year ${0} for the selected locations',
     'FUND_XFER_SAME_SOURCE_AND_DEST': "Cannot transfer. The source and destination funds are the same.",
-    'FUND_XFER_CONFIRM': "Are you sure you're ready to commit this transfer?"
+    'FUND_XFER_CONFIRM': "Are you sure you're ready to commit this transfer?",
+    'PO_CHECKING': "[One moment...]",
+    'PO_COULD_ACTIVATE' : "Yes.",
+    'PO_WARNING_NO_BLOCK_ACTIVATION': "Yes; fund ${0} (${1}) would be encumbered beyond its warning level.",
+    'PO_STOP_BLOCKS_ACTIVATION': "No; fund ${0} (${1}) would be encumbered beyond its stop level.",
+    'PO_FUND_WARNING_CONFIRM': "Are you sure? Did you see the warning about over-encumbering a fund?"
 }

Modified: trunk/Open-ILS/web/js/ui/default/acq/po/view_po.js
===================================================================
--- trunk/Open-ILS/web/js/ui/default/acq/po/view_po.js	2010-03-30 17:05:22 UTC (rev 16061)
+++ trunk/Open-ILS/web/js/ui/default/acq/po/view_po.js	2010-03-30 17:50:25 UTC (rev 16062)
@@ -1,3 +1,4 @@
+dojo.require("dojo.string");
 dojo.require('dijit.layout.ContentPane');
 dojo.require('openils.User');
 dojo.require('openils.Util');
@@ -281,7 +282,8 @@
     dojo.byId("acq-po-view-notes").innerHTML = PO.notes().length;
 
     if(PO.state() == "pending") {
-        openils.Util.show("acq-po-activate");
+        openils.Util.show("acq-po-activate", "table-row");
+        checkCouldActivatePo();
         if (PO.lineitem_count() > 1)
             openils.Util.show("acq-po-split");
     }
@@ -327,12 +329,75 @@
     );
 }
 
+function checkCouldActivatePo() {
+    var d = dojo.byId("acq-po-activate-checking");
+    var a = dojo.byId("acq-po-activate-link");
+    d.innerHTML = localeStrings.PO_CHECKING;
+    var warnings = [];
+    var stops = [];
+
+    fieldmapper.standardRequest(
+        ["open-ils.acq", "open-ils.acq.purchase_order.activate.dry_run"], {
+            "params": [openils.User.authtoken, PO.id()],
+            "async": true,
+            "onresponse": function(r) {
+                if ((r = openils.Util.readResponse(r, true /* eventOk */))) {
+                    if (typeof(r.textcode) != "undefined") {
+                        switch(r.textcode) {
+                            case "ACQ_FUND_EXCEEDS_STOP_PERCENT":
+                                stops.push(r);
+                                break;
+                            case "ACQ_FUND_EXCEEDS_WARN_PERCENT":
+                                warnings.push(r);
+                                break;
+                        }
+                    }
+                }
+            },
+            "oncomplete": function() {
+                /* XXX in the future, this might be tweaked to display info
+                 * about more than one stop or warning event from the ML. */
+                if (!(warnings.length || stops.length)) {
+                    d.innerHTML = localeStrings.PO_COULD_ACTIVATE;
+                    openils.Util.show(a, "inline");
+                } else {
+                    if (stops.length) {
+                        d.innerHTML =
+                            dojo.string.substitute(
+                                localeStrings.PO_STOP_BLOCKS_ACTIVATION, [
+                                    stops[0].payload.fund.code(),
+                                    stops[0].payload.fund.year()
+                                ]
+                            );
+                        openils.Util.hide(a);
+                    } else {
+                        PO._warning_hack = true;
+                        d.innerHTML =
+                            dojo.string.substitute(
+                                localeStrings.PO_WARNING_NO_BLOCK_ACTIVATION, [
+                                    warnings[0].payload.fund.code(),
+                                    warnings[0].payload.fund.year()
+                                ]
+                            );
+                        openils.Util.show(a, "inline");
+                    }
+                }
+            }
+        }
+    );
+}
+
 function activatePo() {
-    if (
-        openils.Util.isTrue(PO.prepayment_required()) &&
-        !confirm(localeStrings.PREPAYMENT_REQUIRED_REMINDER)
-    ) return false;
+    if (openils.Util.isTrue(PO.prepayment_required())) {
+        if (!confirm(localeStrings.PREPAYMENT_REQUIRED_REMINDER))
+            return false;
+    }
 
+    if (PO._warning_hack) {
+        if (!confirm(localeStrings.PO_FUND_WARNING_CONFIRM))
+            return false;
+    }
+
     progressDialog.show(true);
     try {
         fieldmapper.standardRequest(

Modified: trunk/Open-ILS/web/templates/default/acq/po/view.tt2
===================================================================
--- trunk/Open-ILS/web/templates/default/acq/po/view.tt2	2010-03-30 17:05:22 UTC (rev 16061)
+++ trunk/Open-ILS/web/templates/default/acq/po/view.tt2	2010-03-30 17:50:25 UTC (rev 16062)
@@ -42,7 +42,13 @@
                             id="acq-po-view-notes"></a>
                     </td>
                 </tr>
-                <tr><td><a class='hidden' id='acq-po-activate' href='javascript:void(0);' onclick='activatePo()'>Activate Order</a></td></tr>
+                <tr id="acq-po-activate" class="hidden">
+                    <td>Activatable?</td>
+                    <td>
+                        <span id="acq-po-activate-checking"></span>
+                        <a class="hidden" id="acq-po-activate-link" href="javascript:void(0);" onclick="activatePo()">Activate Order</a>
+                    </td>
+                </tr>
                 <tr><td><a class="hidden" id="acq-po-split" href="javascript:void(0);" onclick="if (confirm(localeStrings.CONFIRM_SPLIT_PO)) splitPo();">Split Order by Lineitems</a></td></tr>
             </table>
         </div>



More information about the open-ils-commits mailing list