340 lines
9.4 KiB
TypeScript
340 lines
9.4 KiB
TypeScript
import { AfterViewInit, Component, ElementRef, OnInit, OnDestroy, ViewChild } from '@angular/core';
|
|
import { FormBuilder, FormGroup } from '@angular/forms';
|
|
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
import { Hotkey, HotkeysService } from 'angular2-hotkeys';
|
|
import { round } from 'mathjs';
|
|
import * as moment from 'moment';
|
|
import { BehaviorSubject, Observable, of as observableOf } from 'rxjs';
|
|
import { debounceTime, distinctUntilChanged, map, startWith, switchMap } from 'rxjs/operators';
|
|
|
|
import { AuthService } from '../auth/auth.service';
|
|
import { Account } from '../core/account';
|
|
import { AccountService } from '../core/account.service';
|
|
import { DbFile } from '../core/db-file';
|
|
import { Journal } from '../core/journal';
|
|
import { ToasterService } from '../core/toaster.service';
|
|
import { Voucher } from '../core/voucher';
|
|
import { VoucherService } from '../core/voucher.service';
|
|
import { ConfirmDialogComponent } from '../shared/confirm-dialog/confirm-dialog.component';
|
|
import { ImageDialogComponent } from '../shared/image-dialog/image-dialog.component';
|
|
import { ImageService } from '../shared/image.service';
|
|
import { MathService } from '../shared/math.service';
|
|
|
|
import { JournalDataSource } from './journal-datasource';
|
|
import { JournalDialogComponent } from './journal-dialog.component';
|
|
|
|
@Component({
|
|
selector: 'app-journal',
|
|
templateUrl: './journal.component.html',
|
|
styleUrls: ['./journal.component.css'],
|
|
})
|
|
export class JournalComponent implements OnInit, AfterViewInit, OnDestroy {
|
|
@ViewChild('accountElement', { static: true }) accountElement: ElementRef;
|
|
@ViewChild('dateElement', { static: true }) dateElement: ElementRef;
|
|
public journalObservable = new BehaviorSubject<Journal[]>([]);
|
|
dataSource: JournalDataSource;
|
|
form: FormGroup;
|
|
voucher: Voucher;
|
|
account: Account;
|
|
accBal: any;
|
|
|
|
displayedColumns = ['debit', 'account', 'amount', 'action'];
|
|
|
|
accounts: Observable<Account[]>;
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private fb: FormBuilder,
|
|
private dialog: MatDialog,
|
|
private hotkeys: HotkeysService,
|
|
private toaster: ToasterService,
|
|
public auth: AuthService,
|
|
private math: MathService,
|
|
public image: ImageService,
|
|
private ser: VoucherService,
|
|
private accountSer: AccountService,
|
|
) {
|
|
this.account = null;
|
|
this.form = this.fb.group({
|
|
date: '',
|
|
addRow: this.fb.group({
|
|
debit: '1',
|
|
account: '',
|
|
amount: '',
|
|
}),
|
|
narration: '',
|
|
});
|
|
this.accBal = null;
|
|
this.setupAccountAutocomplete();
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data.subscribe((value) => {
|
|
const data = value as { voucher: Voucher };
|
|
|
|
this.loadVoucher(data.voucher);
|
|
});
|
|
this.hotkeys.add(
|
|
new Hotkey(
|
|
'f2',
|
|
(): boolean => {
|
|
setTimeout(() => {
|
|
this.dateElement.nativeElement.focus();
|
|
}, 0);
|
|
return false; // Prevent bubbling
|
|
},
|
|
['INPUT', 'SELECT', 'TEXTAREA'],
|
|
),
|
|
);
|
|
this.hotkeys.add(
|
|
new Hotkey(
|
|
'ctrl+s',
|
|
(): boolean => {
|
|
if (this.canSave()) {
|
|
this.save();
|
|
}
|
|
return false; // Prevent bubbling
|
|
},
|
|
['INPUT', 'SELECT', 'TEXTAREA'],
|
|
),
|
|
);
|
|
this.hotkeys.add(
|
|
new Hotkey(
|
|
'ctrl+p',
|
|
(): boolean => {
|
|
if (
|
|
this.voucher.id &&
|
|
!this.voucher.posted &&
|
|
this.auth.user.perms.indexOf('post-vouchers') !== -1
|
|
) {
|
|
this.post();
|
|
}
|
|
return false; // Prevent bubbling
|
|
},
|
|
['INPUT', 'SELECT', 'TEXTAREA'],
|
|
),
|
|
);
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
this.focusAccount();
|
|
}
|
|
|
|
ngOnDestroy() {
|
|
this.hotkeys.reset();
|
|
}
|
|
|
|
loadVoucher(voucher) {
|
|
this.voucher = voucher;
|
|
this.form.setValue({
|
|
date: moment(this.voucher.date, 'DD-MMM-YYYY').toDate(),
|
|
addRow: {
|
|
debit: '1',
|
|
account: '',
|
|
amount: 0,
|
|
},
|
|
narration: this.voucher.narration,
|
|
});
|
|
this.dataSource = new JournalDataSource(this.journalObservable);
|
|
this.journalObservable.next(this.voucher.journals);
|
|
}
|
|
|
|
focusAccount() {
|
|
setTimeout(() => {
|
|
this.accountElement.nativeElement.focus();
|
|
}, 0);
|
|
}
|
|
|
|
addRow() {
|
|
const formValue = this.form.get('addRow').value;
|
|
const amount = this.math.journalAmount(formValue.amount, +formValue.debit);
|
|
if (this.account === null || amount.amount === 0) {
|
|
return;
|
|
}
|
|
const oldFiltered = this.voucher.journals.filter((x) => x.account.id === this.account.id);
|
|
const old = oldFiltered.length ? oldFiltered[0] : null;
|
|
if (old) {
|
|
const a = old.debit * old.amount + amount.debit * amount.amount;
|
|
old.debit = a < 0 ? -1 : 1;
|
|
old.amount = Math.abs(a);
|
|
} else {
|
|
this.voucher.journals.push({
|
|
id: null,
|
|
debit: amount.debit,
|
|
amount: amount.amount,
|
|
account: this.account,
|
|
costCentre: null,
|
|
});
|
|
}
|
|
this.journalObservable.next(this.voucher.journals);
|
|
this.resetAddRow();
|
|
}
|
|
|
|
resetAddRow() {
|
|
const amount = round(
|
|
Math.abs(this.voucher.journals.map((x) => x.debit * x.amount).reduce((p, c) => p + c)),
|
|
2,
|
|
);
|
|
const { debit } = this.form.get('addRow').value;
|
|
this.form.get('addRow').reset({
|
|
debit,
|
|
account: null,
|
|
amount: `${amount}`,
|
|
});
|
|
this.account = null;
|
|
this.accBal = null;
|
|
setTimeout(() => {
|
|
this.accountElement.nativeElement.focus();
|
|
}, 0);
|
|
}
|
|
|
|
editRow(row: Journal) {
|
|
const dialogRef = this.dialog.open(JournalDialogComponent, {
|
|
width: '750px',
|
|
data: {
|
|
journal: { ...row },
|
|
date: moment(this.form.get('date').value).format('DD-MMM-YYYY'),
|
|
},
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe((result: boolean | Journal) => {
|
|
if (!result) {
|
|
return;
|
|
}
|
|
const j = result as Journal;
|
|
if (
|
|
j.account.id !== row.account.id &&
|
|
this.voucher.journals.filter((x) => x.account.id === j.account.id).length
|
|
) {
|
|
return;
|
|
}
|
|
Object.assign(row, j);
|
|
this.journalObservable.next(this.voucher.journals);
|
|
this.resetAddRow();
|
|
});
|
|
}
|
|
|
|
deleteRow(row: Journal) {
|
|
this.voucher.journals.splice(this.voucher.journals.indexOf(row), 1);
|
|
this.journalObservable.next(this.voucher.journals);
|
|
const amount = Math.abs(
|
|
this.voucher.journals.map((x) => x.debit * x.amount).reduce((p, c) => p + c, 0),
|
|
);
|
|
this.form.get('addRow').patchValue({
|
|
amount,
|
|
});
|
|
}
|
|
|
|
canSave() {
|
|
if (!this.voucher.id) {
|
|
return true;
|
|
}
|
|
if (this.voucher.posted && this.auth.user.perms.indexOf('edit-posted-vouchers') !== -1) {
|
|
return true;
|
|
}
|
|
return (
|
|
this.voucher.user.id === this.auth.user.id ||
|
|
this.auth.user.perms.indexOf("edit-other-user's-vouchers") !== -1
|
|
);
|
|
}
|
|
|
|
post() {
|
|
this.ser.post(this.voucher.id).subscribe(
|
|
(result) => {
|
|
this.loadVoucher(result);
|
|
this.toaster.show('Success', 'Voucher Posted');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Danger', error);
|
|
},
|
|
);
|
|
}
|
|
|
|
save() {
|
|
const voucher: Voucher = this.getVoucher();
|
|
this.ser.saveOrUpdate(voucher).subscribe(
|
|
(result) => {
|
|
this.toaster.show('Success', '');
|
|
if (voucher.id === result.id) {
|
|
this.loadVoucher(result);
|
|
} else {
|
|
this.router.navigate(['/journal', result.id]);
|
|
}
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Danger', error);
|
|
},
|
|
);
|
|
}
|
|
|
|
getVoucher(): Voucher {
|
|
const formModel = this.form.value;
|
|
this.voucher.date = moment(formModel.date).format('DD-MMM-YYYY');
|
|
this.voucher.narration = formModel.narration;
|
|
return this.voucher;
|
|
}
|
|
|
|
delete() {
|
|
this.ser.delete(this.voucher.id).subscribe(
|
|
() => {
|
|
this.toaster.show('Success', '');
|
|
this.router.navigate(['/journal'], { replaceUrl: true });
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Danger', error);
|
|
},
|
|
);
|
|
}
|
|
|
|
confirmDelete(): void {
|
|
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
|
|
width: '250px',
|
|
data: { title: 'Delete Voucher?', content: 'Are you sure? This cannot be undone.' },
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe((result: boolean) => {
|
|
if (result) {
|
|
this.delete();
|
|
}
|
|
});
|
|
}
|
|
|
|
setupAccountAutocomplete(): 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))),
|
|
);
|
|
}
|
|
|
|
displayFn(account?: Account): string | undefined {
|
|
return account ? account.name : undefined;
|
|
}
|
|
|
|
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) => {
|
|
this.accBal = v;
|
|
});
|
|
}
|
|
|
|
zoomImage(file: DbFile) {
|
|
this.dialog.open(ImageDialogComponent, {
|
|
width: '750px',
|
|
data: file.resized,
|
|
});
|
|
}
|
|
|
|
deleteImage(file: DbFile) {
|
|
const index = this.voucher.files.indexOf(file);
|
|
this.voucher.files.splice(index, 1);
|
|
}
|
|
}
|