Strict done!!
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import { Component, Inject, OnInit } from '@angular/core';
|
||||
import { FormBuilder, FormGroup } from '@angular/forms';
|
||||
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
|
||||
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
|
||||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
|
||||
import { Observable, of as observableOf } from 'rxjs';
|
||||
@ -17,7 +17,7 @@ import { MathService } from '../shared/math.service';
|
||||
export class ReceiptDialogComponent implements OnInit {
|
||||
accounts: Observable<Account[]>;
|
||||
form: FormGroup;
|
||||
account: Account;
|
||||
account: Account = new Account();
|
||||
accBal: any;
|
||||
|
||||
constructor(
|
||||
@ -32,7 +32,14 @@ export class ReceiptDialogComponent implements OnInit {
|
||||
amount: '',
|
||||
});
|
||||
this.accBal = null;
|
||||
this.setupAccountAutocomplete();
|
||||
// Setup Account Autocomplete
|
||||
this.accounts = (this.form.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))),
|
||||
);
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
@ -43,24 +50,14 @@ export class ReceiptDialogComponent implements OnInit {
|
||||
this.account = this.data.journal.account;
|
||||
}
|
||||
|
||||
setupAccountAutocomplete(): void {
|
||||
const control = this.form.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))),
|
||||
);
|
||||
}
|
||||
|
||||
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;
|
||||
this.accountSer.balance(this.account.id, this.data.date).subscribe((v) => {
|
||||
const account = event.option.value;
|
||||
this.account = account;
|
||||
this.accountSer.balance(account.id as string, this.data.date).subscribe((v) => {
|
||||
this.accBal = v;
|
||||
});
|
||||
}
|
||||
|
||||
@ -166,7 +166,7 @@
|
||||
mat-raised-button
|
||||
(click)="post()"
|
||||
*ngIf="voucher.id"
|
||||
[disabled]="voucher.posted || auth.user.perms.indexOf('post-vouchers') === -1"
|
||||
[disabled]="voucher.posted || auth.allowed('post-vouchers')"
|
||||
>
|
||||
{{ voucher.posted ? 'Posted' : 'Post' }}
|
||||
</button>
|
||||
|
||||
@ -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 ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
@ViewChild('accountElement', { static: true }) accountElement?: ElementRef;
|
||||
@ViewChild('dateElement', { static: true }) dateElement?: ElementRef;
|
||||
public journalObservable = new BehaviorSubject<Journal[]>([]);
|
||||
dataSource: ReceiptDataSource;
|
||||
dataSource: ReceiptDataSource = new ReceiptDataSource(this.journalObservable);
|
||||
form: FormGroup;
|
||||
receiptAccounts: Account[];
|
||||
receiptJournal: Journal;
|
||||
voucher: Voucher;
|
||||
account: Account;
|
||||
receiptAccounts: Account[] = [];
|
||||
receiptJournal: Journal = new Journal();
|
||||
voucher: Voucher = new Voucher();
|
||||
account: Account | null = null;
|
||||
accBal: any;
|
||||
|
||||
displayedColumns = ['account', 'amount', 'action'];
|
||||
@ -59,7 +60,6 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
private ser: VoucherService,
|
||||
private accountSer: AccountService,
|
||||
) {
|
||||
this.account = null;
|
||||
this.form = this.fb.group({
|
||||
date: '',
|
||||
receiptAccount: '',
|
||||
@ -71,8 +71,24 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
narration: '',
|
||||
});
|
||||
this.accBal = null;
|
||||
this.listenToAccountAutocompleteChange();
|
||||
this.listenToReceiptAccountChange();
|
||||
// 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 Receipt Account Change
|
||||
(this.form.get('receiptAccount') as FormControl).valueChanges.subscribe((x) =>
|
||||
this.router.navigate([], {
|
||||
relativeTo: this.route,
|
||||
queryParams: { a: x },
|
||||
replaceUrl: true,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
@ -87,7 +103,7 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
'f2',
|
||||
(): boolean => {
|
||||
setTimeout(() => {
|
||||
this.dateElement.nativeElement.focus();
|
||||
if (this.dateElement) this.dateElement.nativeElement.focus();
|
||||
}, 0);
|
||||
return false; // Prevent bubbling
|
||||
},
|
||||
@ -110,11 +126,7 @@ export class ReceiptComponent 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
|
||||
@ -151,17 +163,19 @@ export class ReceiptComponent 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.receiptJournal.id)) {
|
||||
return;
|
||||
@ -169,27 +183,28 @@ export class ReceiptComponent 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);
|
||||
}
|
||||
|
||||
@ -200,7 +215,7 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
Math.abs(journals.map((x) => x.amount).reduce((p, c) => p + c, 0)),
|
||||
2,
|
||||
);
|
||||
this.form.get('receiptAmount').setValue(this.receiptJournal.amount);
|
||||
(this.form.get('receiptAmount') as FormControl).setValue(this.receiptJournal.amount);
|
||||
}
|
||||
|
||||
editRow(row: Journal) {
|
||||
@ -208,7 +223,7 @@ export class ReceiptComponent 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'),
|
||||
},
|
||||
});
|
||||
|
||||
@ -237,17 +252,17 @@ export class ReceiptComponent 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');
|
||||
@ -284,7 +299,7 @@ export class ReceiptComponent 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(['/receipt'], { replaceUrl: true });
|
||||
@ -308,35 +323,15 @@ export class ReceiptComponent 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))),
|
||||
);
|
||||
}
|
||||
|
||||
listenToReceiptAccountChange(): void {
|
||||
this.form.get('receiptAccount').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;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user