Strict done!!

This commit is contained in:
2020-11-23 16:42:54 +05:30
parent af343cb7f9
commit afe746ecdc
142 changed files with 1258 additions and 907 deletions
+55 -59
View File
@@ -1,5 +1,5 @@
import { AfterViewInit, Component, ElementRef, OnInit, OnDestroy, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
import { MatDialog } from '@angular/material/dialog';
import { ActivatedRoute, Router } from '@angular/router';
@@ -15,6 +15,7 @@ import { AccountService } from '../core/account.service';
import { DbFile } from '../core/db-file';
import { Journal } from '../core/journal';
import { ToasterService } from '../core/toaster.service';
import { User } from '../core/user';
import { Voucher } from '../core/voucher';
import { VoucherService } from '../core/voucher.service';
import { ConfirmDialogComponent } from '../shared/confirm-dialog/confirm-dialog.component';
@@ -34,12 +35,12 @@ export class PaymentComponent implements OnInit, AfterViewInit, OnDestroy {
@ViewChild('accountElement', { static: true }) accountElement?: ElementRef;
@ViewChild('dateElement', { static: true }) dateElement?: ElementRef;
public journalObservable = new BehaviorSubject<Journal[]>([]);
dataSource: PaymentDataSource;
dataSource: PaymentDataSource = new PaymentDataSource(this.journalObservable);
form: FormGroup;
paymentAccounts: Account[];
paymentJournal: Journal;
voucher: Voucher;
account: Account;
paymentAccounts: Account[] = [];
paymentJournal: Journal = new Journal();
voucher: Voucher = new Voucher();
account: Account | null;
accBal: any;
displayedColumns = ['account', 'amount', 'action'];
@@ -71,8 +72,24 @@ export class PaymentComponent implements OnInit, AfterViewInit, OnDestroy {
narration: '',
});
this.accBal = null;
this.listenToAccountAutocompleteChange();
this.listenToPaymentAccountChange();
// Listen to Account Autocomplete Change
this.accounts = ((this.form.get('addRow') as FormControl).get(
'account',
) as FormControl).valueChanges.pipe(
startWith(null),
map((x) => (x !== null && x.length >= 1 ? x : null)),
debounceTime(150),
distinctUntilChanged(),
switchMap((x) => (x === null ? observableOf([]) : this.accountSer.autocomplete(x))),
);
// Listen to Payment Account Change
(this.form.get('paymentAccount') as FormControl).valueChanges.subscribe((x) =>
this.router.navigate([], {
relativeTo: this.route,
queryParams: { a: x },
replaceUrl: true,
}),
);
}
ngOnInit() {
@@ -87,7 +104,7 @@ export class PaymentComponent implements OnInit, AfterViewInit, OnDestroy {
'f2',
(): boolean => {
setTimeout(() => {
this.dateElement.nativeElement.focus();
if (this.dateElement) this.dateElement.nativeElement.focus();
}, 0);
return false; // Prevent bubbling
},
@@ -108,11 +125,7 @@ export class PaymentComponent implements OnInit, AfterViewInit, OnDestroy {
new Hotkey(
'ctrl+p',
(): boolean => {
if (
this.voucher.id &&
!this.voucher.posted &&
this.auth.user.perms.indexOf('post-vouchers') !== -1
)
if (this.voucher.id && !this.voucher.posted && this.auth.allowed('post-vouchers'))
this.post();
return false; // Prevent bubbling
},
@@ -148,17 +161,19 @@ export class PaymentComponent implements OnInit, AfterViewInit, OnDestroy {
focusAccount() {
setTimeout(() => {
this.accountElement.nativeElement.focus();
if (this.accountElement) this.accountElement.nativeElement.focus();
}, 0);
}
addRow() {
const amount = this.math.parseAmount(this.form.get('addRow').value.amount, 2);
const amount = this.math.parseAmount((this.form.get('addRow') as FormControl).value.amount, 2);
const debit = 1;
if (this.account === null || amount <= 0) {
return;
}
const oldFiltered = this.voucher.journals.filter((x) => x.account.id === this.account.id);
const oldFiltered = this.voucher.journals.filter(
(x) => x.account.id === (this.account as Account).id,
);
const old = oldFiltered.length ? oldFiltered[0] : null;
if (old && (old.debit === -1 || old.id === this.paymentJournal.id)) {
return;
@@ -166,27 +181,28 @@ export class PaymentComponent implements OnInit, AfterViewInit, OnDestroy {
if (old) {
old.amount += amount;
} else {
this.voucher.journals.push({
id: null,
debit,
amount,
account: this.account,
costCentre: null,
});
this.voucher.journals.push(
new Journal({
debit,
amount,
account: this.account,
costCentre: null,
}),
);
}
this.resetAddRow();
this.updateView();
}
resetAddRow() {
this.form.get('addRow').reset({
(this.form.get('addRow') as FormControl).reset({
account: null,
amount: null,
});
this.account = null;
this.accBal = null;
setTimeout(() => {
this.accountElement.nativeElement.focus();
if (this.accountElement) this.accountElement.nativeElement.focus();
}, 0);
}
@@ -197,7 +213,7 @@ export class PaymentComponent implements OnInit, AfterViewInit, OnDestroy {
Math.abs(journals.map((x) => x.amount).reduce((p, c) => p + c, 0)),
2,
);
this.form.get('paymentAmount').setValue(this.paymentJournal.amount);
(this.form.get('paymentAmount') as FormControl).setValue(this.paymentJournal.amount);
}
editRow(row: Journal) {
@@ -205,7 +221,7 @@ export class PaymentComponent implements OnInit, AfterViewInit, OnDestroy {
width: '750px',
data: {
journal: { ...row },
date: moment(this.form.get('date').value).format('DD-MMM-YYYY'),
date: moment((this.form.get('date') as FormControl).value).format('DD-MMM-YYYY'),
},
});
@@ -234,17 +250,17 @@ export class PaymentComponent implements OnInit, AfterViewInit, OnDestroy {
if (!this.voucher.id) {
return true;
}
if (this.voucher.posted && this.auth.user.perms.indexOf('edit-posted-vouchers') !== -1) {
if (this.voucher.posted && this.auth.allowed('edit-posted-vouchers')) {
return true;
}
return (
this.voucher.user.id === this.auth.user.id ||
this.auth.user.perms.indexOf("edit-other-user's-vouchers") !== -1
this.voucher.user.id === (this.auth.user as User).id ||
this.auth.allowed("edit-other-user's-vouchers")
);
}
post() {
this.ser.post(this.voucher.id).subscribe(
this.ser.post(this.voucher.id as string).subscribe(
(result) => {
this.loadVoucher(result);
this.toaster.show('Success', 'Voucher Posted');
@@ -281,7 +297,7 @@ export class PaymentComponent implements OnInit, AfterViewInit, OnDestroy {
}
delete() {
this.ser.delete(this.voucher.id).subscribe(
this.ser.delete(this.voucher.id as string).subscribe(
() => {
this.toaster.show('Success', '');
this.router.navigate(['/payment'], { replaceUrl: true });
@@ -305,35 +321,15 @@ export class PaymentComponent implements OnInit, AfterViewInit, OnDestroy {
});
}
listenToAccountAutocompleteChange(): void {
const control = this.form.get('addRow').get('account');
this.accounts = control.valueChanges.pipe(
startWith(null),
map((x) => (x !== null && x.length >= 1 ? x : null)),
debounceTime(150),
distinctUntilChanged(),
switchMap((x) => (x === null ? observableOf([]) : this.accountSer.autocomplete(x))),
);
}
listenToPaymentAccountChange(): void {
this.form.get('paymentAccount').valueChanges.subscribe((x) =>
this.router.navigate([], {
relativeTo: this.route,
queryParams: { a: x },
replaceUrl: true,
}),
);
}
displayFn(account?: Account): string | undefined {
return account ? account.name : undefined;
displayFn(account?: Account): string {
return account ? account.name : '';
}
accountSelected(event: MatAutocompleteSelectedEvent): void {
this.account = event.option.value;
const date = moment(this.form.get('date').value).format('DD-MMM-YYYY');
this.accountSer.balance(this.account.id, date).subscribe((v) => {
const account = event.option.value;
this.account = account;
const date = moment((this.form.get('date') as FormControl).value).format('DD-MMM-YYYY');
this.accountSer.balance(account.id as string, date).subscribe((v) => {
this.accBal = v;
});
}