Files
cbsnexora/src/app/cashier/cash-transfer/cash-transfer.component.ts
T
2026-06-24 16:32:37 +05:30

204 lines
6.0 KiB
TypeScript

/**
* Copyright since 2025 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { Component, OnInit, inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { MatIconModule } from '@angular/material/icon';
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatSelectModule } from '@angular/material/select';
import { MatInputModule } from '@angular/material/input';
import { of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { CashierService } from '../cashier.service';
interface Denom {
id: number;
value: number;
kind: 'note' | 'coin';
label: string;
}
/** Denomination-wise cash transfer between two parties (e.g. Vault -> Cashier). */
@Component({
selector: 'mifosx-cash-transfer',
templateUrl: './cash-transfer.component.html',
styleUrls: ['../cashier-shared.scss'],
standalone: true,
imports: [
CommonModule,
FormsModule,
MatIconModule,
MatSnackBarModule,
MatFormFieldModule,
MatSelectModule,
MatInputModule
]
})
export class CashTransferComponent implements OnInit {
private service = inject(CashierService);
private snackBar = inject(MatSnackBar);
notes: Denom[] = [];
coins: Denom[] = [];
counts: Record<number, number> = {};
transferDate = '';
fromParty = '';
toParty = '';
note = '';
saving = false;
/** Tellers — the "From" options (cash point / head). */
tellers: { id: number; name: string }[] = [];
/** Cashiers of the currently selected From-teller — the "To" options. */
cashiers: { id: number; label: string }[] = [];
fromTellerId: number | null = null;
toCashierId: number | null = null;
loadingCashiers = false;
/** All denominations (notes then coins) — used for total and the submit payload. */
get denoms(): Denom[] {
return [
...this.notes,
...this.coins
];
}
ngOnInit(): void {
const d = new Date();
this.transferDate = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
this.service.getActiveDenominations().subscribe({
next: (list) => {
const rows: Denom[] = (list || []).map((x) => ({
id: x.id,
value: x.denomination,
kind: x.kind,
label: '₹' + x.denomination
}));
this.notes = rows.filter((r) => r.kind === 'note').sort((a, b) => b.value - a.value);
this.coins = rows.filter((r) => r.kind === 'coin').sort((a, b) => b.value - a.value);
},
error: () => {
this.notes = [];
this.coins = [];
}
});
this.service
.getTellers()
.pipe(catchError(() => of([] as any[])))
.subscribe((list) => {
this.tellers = (list || []).map((t: any) => ({ id: t.id, name: t.name }));
if (this.tellers.length === 1) {
this.onTellerSelect(this.tellers[0].id);
}
});
}
/** When a From-teller is chosen, load ONLY that teller's cashiers into the To dropdown. */
onTellerSelect(tellerId: number): void {
this.fromTellerId = tellerId;
const teller = this.tellers.find((t) => t.id === tellerId);
this.fromParty = teller ? teller.name : '';
this.toParty = '';
this.toCashierId = null;
this.cashiers = [];
if (!tellerId) {
return;
}
this.loadingCashiers = true;
this.service
.getCashiersForTeller(tellerId)
.pipe(catchError(() => of({ cashiers: [] })))
.subscribe((r: any) => {
this.cashiers = (r?.cashiers || []).map((c: any) => ({ id: c.id, label: c.staffName }));
this.loadingCashiers = false;
if (this.cashiers.length === 1) {
this.onCashierSelect(this.cashiers[0].id);
}
});
}
/** When a To-cashier is chosen, capture its id and name. */
onCashierSelect(cashierId: number): void {
this.toCashierId = cashierId;
const c = this.cashiers.find((x) => x.id === cashierId);
this.toParty = c ? c.label : '';
}
countFor(d: Denom): number {
return this.counts[d.id] || 0;
}
subtotal(d: Denom): number {
return this.countFor(d) * d.value;
}
onChange(d: Denom, raw: any): void {
this.counts[d.id] = Math.max(0, Math.floor(Number(raw) || 0));
}
bump(d: Denom, delta: number): void {
this.counts[d.id] = Math.max(0, (this.counts[d.id] || 0) + delta);
}
get total(): number {
return this.denoms.reduce((s, d) => s + this.subtotal(d), 0);
}
get valid(): boolean {
return this.total > 0 && !!this.fromTellerId && !!this.toCashierId;
}
clear(): void {
this.counts = {};
}
submit(): void {
if (!this.valid || this.saving) {
return;
}
this.saving = true;
const payload = {
transferDate: this.transferDate,
fromTellerId: this.fromTellerId,
toCashierId: this.toCashierId,
fromParty: this.fromParty.trim(),
toParty: this.toParty.trim(),
note: this.note || null,
denominations: this.denoms
.filter((d) => this.countFor(d) > 0)
.map((d) => ({ denominationId: d.id, denominationValue: d.value, count: this.countFor(d) }))
};
this.service.createCashTransfer(payload).subscribe({
next: (res: any) => {
if (res?.glWarning) {
this.snackBar.open(`Transfer saved (GL warning: ${res.glWarning})`, 'Close', {
duration: 8000,
panelClass: ['snack-warn']
});
} else {
this.snackBar.open('Cash transfer recorded', 'Close', { duration: 3000 });
}
this.clear();
this.note = '';
this.fromParty = '';
this.toParty = '';
this.fromTellerId = null;
this.toCashierId = null;
this.cashiers = [];
this.saving = false;
},
error: () => {
this.saving = false;
}
});
}
}