LP1984007 Fund warning logic to check balances
authorStephanie Leary <stephanie.leary@equinoxoli.org>
Tue, 16 May 2023 19:06:27 +0000 (19:06 +0000)
committerStephanie Leary <stephanie.leary@equinoxoli.org>
Tue, 16 May 2023 19:14:14 +0000 (19:14 +0000)
Adds logic to check fund balances in order to set CSS warning/stop
classes in funds dropdowns.

Signed-off-by: Stephanie Leary <stephanie.leary@equinoxoli.org>
Open-ILS/src/eg2/src/app/staff/acq/po/charges.component.html
Open-ILS/src/eg2/src/app/staff/acq/po/charges.component.ts

index 3b8fcdc..298917b 100644 (file)
@@ -31,6 +31,7 @@
         (onChange)="charge.fund($event ? $event.id : null)"
         i18n-placeholder placeholder="Fund..."
         [asyncSupportsEmptyTermClick]="true"
+        [displayTemplate]="fundTmpl"
         [required]="true" [readOnly]="!charge.isnew() && !charge.ischanged()"
         [idlQuerySort]="{acqf: 'year DESC, code'}"
         [idlQueryAnd]="{org: owners, active: 't'}">
@@ -87,3 +88,8 @@
     </div>
   </div>
 </ng-container>
+
+<ng-template #fundTmpl let-r="result" i18n>
+  <span [ngClass]="{'fund-balance-state-stop': checkFundBalance(r.fm.id()) === 'stop',
+                    'fund-balance-state-warning': checkFundBalance(r.fm.id()) === 'warning'}">{{r.label}}</span>
+</ng-template>
\ No newline at end of file
index 6e968c7..5da7fc8 100644 (file)
@@ -23,6 +23,8 @@ export class PoChargesComponent implements OnInit, OnDestroy {
     poSubscription: Subscription;
     owners: number[];
     classNames: string[];
+    _fundBalanceCache: string[] = [];
+    _inflight: Promise<string>[] = [];
 
     @ViewChild('disencumberChargeDialog') disencumberChargeDialog: DisencumberChargeDialogComponent;
 
@@ -176,5 +178,31 @@ export class PoChargesComponent implements OnInit, OnDestroy {
             }).then(_ => this.poService.refreshOrderSummary(true));
         }
     }
+
+    // duplicated from acq/lineitem/copy-attrs.component
+    checkFundBalance(fundId: number): string {
+        if (this._fundBalanceCache[fundId]) {
+            return this._fundBalanceCache[fundId];
+        }
+        if (this._inflight[fundId]) {
+            return 'ok';
+        }
+        this._inflight[fundId] = this.net.request(
+            'open-ils.acq',
+            'open-ils.acq.fund.check_balance_percentages',
+            this.auth.token(), fundId
+        ).toPromise().then(r => {
+            if (r[0]) {
+                this._fundBalanceCache[fundId] = 'stop';
+            } else if (r[1]) {
+                this._fundBalanceCache[fundId] = 'warning';
+            } else {
+                this._fundBalanceCache[fundId] = 'ok';
+            }
+            
+            delete this._inflight[fundId];
+            return this._fundBalanceCache[fundId];
+        });
+    }
 }