2020-07-16 12:55:44 +00:00
|
|
|
import { AfterViewInit, Component, ElementRef, OnInit, OnDestroy, ViewChild } from '@angular/core';
|
|
|
|
import { FormBuilder, FormGroup } from '@angular/forms';
|
2019-06-12 11:55:10 +00:00
|
|
|
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
|
|
|
|
import { MatDialog } from '@angular/material/dialog';
|
2020-07-16 12:55:44 +00:00
|
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
|
|
import { BehaviorSubject, fromEvent, Observable, of, of as observableOf, zip } from 'rxjs';
|
|
|
|
import { ReceiptDataSource } from './receipt-datasource';
|
|
|
|
import { Account } from '../core/account';
|
|
|
|
import { VoucherService } from '../core/voucher.service';
|
|
|
|
import { AccountService } from '../core/account.service';
|
|
|
|
import { DbFile, Journal, Voucher } from '../core/voucher';
|
2018-05-25 13:49:00 +00:00
|
|
|
import * as moment from 'moment';
|
2020-07-16 12:55:44 +00:00
|
|
|
import { evaluate } from 'mathjs';
|
|
|
|
import { AuthService } from '../auth/auth.service';
|
|
|
|
import { ConfirmDialogComponent } from '../shared/confirm-dialog/confirm-dialog.component';
|
|
|
|
import { ToasterService } from '../core/toaster.service';
|
|
|
|
import { debounceTime, distinctUntilChanged, map, startWith, switchMap } from 'rxjs/operators';
|
|
|
|
import { ReceiptDialogComponent } from './receipt-dialog.component';
|
|
|
|
import { ImageDialogComponent } from '../shared/image-dialog/image-dialog.component';
|
|
|
|
import { Hotkey, HotkeysService } from 'angular2-hotkeys';
|
2018-05-25 13:49:00 +00:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-receipt',
|
|
|
|
templateUrl: './receipt.component.html',
|
2020-10-01 15:21:22 +00:00
|
|
|
styleUrls: ['./receipt.component.css'],
|
2018-05-25 13:49:00 +00:00
|
|
|
})
|
2019-05-07 09:29:51 +00:00
|
|
|
export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
2019-06-12 11:55:10 +00:00
|
|
|
@ViewChild('accountElement', { static: true }) accountElement: ElementRef;
|
|
|
|
@ViewChild('dateElement', { static: true }) dateElement: ElementRef;
|
2018-05-25 13:49:00 +00:00
|
|
|
public journalObservable = new BehaviorSubject<Journal[]>([]);
|
|
|
|
dataSource: ReceiptDataSource;
|
|
|
|
form: FormGroup;
|
|
|
|
receiptAccounts: Account[];
|
|
|
|
receiptJournal: Journal;
|
|
|
|
voucher: Voucher;
|
|
|
|
account: Account;
|
|
|
|
accBal: any;
|
|
|
|
|
|
|
|
displayedColumns = ['account', 'amount', 'action'];
|
|
|
|
|
|
|
|
accounts: Observable<Account[]>;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private router: Router,
|
|
|
|
private fb: FormBuilder,
|
|
|
|
private dialog: MatDialog,
|
2019-05-07 09:29:51 +00:00
|
|
|
private hotkeys: HotkeysService,
|
2018-05-25 13:49:00 +00:00
|
|
|
private toaster: ToasterService,
|
|
|
|
private auth: AuthService,
|
|
|
|
private ser: VoucherService,
|
2020-10-01 15:21:22 +00:00
|
|
|
private accountSer: AccountService,
|
2018-05-25 13:49:00 +00:00
|
|
|
) {
|
2018-06-29 08:02:52 +00:00
|
|
|
this.account = null;
|
2018-05-25 13:49:00 +00:00
|
|
|
this.createForm();
|
|
|
|
this.listenToAccountAutocompleteChange();
|
|
|
|
this.listenToReceiptAccountChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2020-10-01 15:21:22 +00:00
|
|
|
this.route.data.subscribe((data: { voucher: Voucher; receiptAccounts: Account[] }) => {
|
|
|
|
this.receiptAccounts = data.receiptAccounts;
|
|
|
|
this.loadVoucher(data.voucher);
|
|
|
|
});
|
|
|
|
this.hotkeys.add(
|
|
|
|
new Hotkey(
|
|
|
|
'f2',
|
|
|
|
(event: KeyboardEvent): boolean => {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.dateElement.nativeElement.focus();
|
|
|
|
}, 0);
|
|
|
|
return false; // Prevent bubbling
|
|
|
|
},
|
|
|
|
['INPUT', 'SELECT', 'TEXTAREA'],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
this.hotkeys.add(
|
|
|
|
new Hotkey(
|
|
|
|
'ctrl+s',
|
|
|
|
(event: KeyboardEvent): boolean => {
|
|
|
|
if (this.canSave()) {
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
return false; // Prevent bubbling
|
|
|
|
},
|
|
|
|
['INPUT', 'SELECT', 'TEXTAREA'],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
this.hotkeys.add(
|
|
|
|
new Hotkey(
|
|
|
|
'ctrl+p',
|
|
|
|
(event: KeyboardEvent): 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'],
|
|
|
|
),
|
|
|
|
);
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ngAfterViewInit() {
|
|
|
|
this.focusAccount();
|
|
|
|
}
|
|
|
|
|
2019-05-07 09:29:51 +00:00
|
|
|
ngOnDestroy() {
|
|
|
|
this.hotkeys.reset();
|
|
|
|
}
|
|
|
|
|
2018-05-25 13:49:00 +00:00
|
|
|
loadVoucher(voucher: Voucher) {
|
|
|
|
this.voucher = voucher;
|
2020-10-01 15:21:22 +00:00
|
|
|
this.receiptJournal = this.voucher.journals.filter((x) => x.debit === 1)[0];
|
2018-05-25 13:49:00 +00:00
|
|
|
this.form.setValue({
|
|
|
|
date: moment(this.voucher.date, 'DD-MMM-YYYY').toDate(),
|
|
|
|
receiptAccount: this.receiptJournal.account.id,
|
|
|
|
receiptAmount: this.receiptJournal.amount,
|
|
|
|
addRow: {
|
|
|
|
account: '',
|
2020-10-01 15:21:22 +00:00
|
|
|
amount: 0,
|
2018-05-25 13:49:00 +00:00
|
|
|
},
|
2020-10-01 15:21:22 +00:00
|
|
|
narration: this.voucher.narration,
|
2018-05-25 13:49:00 +00:00
|
|
|
});
|
|
|
|
this.dataSource = new ReceiptDataSource(this.journalObservable);
|
|
|
|
this.updateView();
|
|
|
|
}
|
|
|
|
|
|
|
|
focusAccount() {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.accountElement.nativeElement.focus();
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
addRow() {
|
2018-06-29 08:02:52 +00:00
|
|
|
const amount = this.rowAmount(this.form.get('addRow').value.amount);
|
2018-05-25 13:49:00 +00:00
|
|
|
const debit = -1;
|
|
|
|
if (this.account === null || 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 && (old.debit === 1 || old.id === this.receiptJournal.id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (old) {
|
|
|
|
old.amount += amount;
|
|
|
|
} else {
|
|
|
|
this.voucher.journals.push({
|
|
|
|
id: null,
|
|
|
|
debit: debit,
|
|
|
|
amount: amount,
|
|
|
|
account: this.account,
|
2020-10-01 15:21:22 +00:00
|
|
|
costCentre: null,
|
2018-05-25 13:49:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
this.resetAddRow();
|
|
|
|
this.updateView();
|
|
|
|
}
|
|
|
|
|
2018-06-29 08:02:52 +00:00
|
|
|
rowAmount(amount: string = ''): number {
|
|
|
|
try {
|
2020-10-01 15:21:22 +00:00
|
|
|
amount = amount.replace(new RegExp('(₹[s]*)|(,)|(s)', 'g'), '');
|
2020-07-16 12:55:44 +00:00
|
|
|
return evaluate(amount);
|
2018-06-29 08:02:52 +00:00
|
|
|
} catch {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 13:49:00 +00:00
|
|
|
resetAddRow() {
|
|
|
|
this.form.get('addRow').reset({
|
|
|
|
account: null,
|
2020-10-01 15:21:22 +00:00
|
|
|
amount: null,
|
2018-05-25 13:49:00 +00:00
|
|
|
});
|
|
|
|
this.account = null;
|
|
|
|
this.accBal = null;
|
|
|
|
setTimeout(() => {
|
|
|
|
this.accountElement.nativeElement.focus();
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateView() {
|
2020-10-01 15:21:22 +00:00
|
|
|
const journals = this.voucher.journals.filter((x) => x.debit === -1);
|
2018-05-25 13:49:00 +00:00
|
|
|
this.journalObservable.next(journals);
|
|
|
|
this.receiptJournal.amount = Math.abs(journals.map((x) => x.amount).reduce((p, c) => p + c, 0));
|
|
|
|
this.form.get('receiptAmount').setValue(this.receiptJournal.amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
editRow(row: Journal) {
|
|
|
|
const dialogRef = this.dialog.open(ReceiptDialogComponent, {
|
|
|
|
width: '750px',
|
2020-10-01 15:21:22 +00:00
|
|
|
data: {
|
|
|
|
journal: Object.assign({}, row),
|
|
|
|
date: moment(this.form.get('date').value).format('DD-MMM-YYYY'),
|
|
|
|
},
|
2018-05-25 13:49:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
dialogRef.afterClosed().subscribe((result: boolean | Journal) => {
|
|
|
|
if (!result) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const j = result as Journal;
|
2020-10-01 15:21:22 +00:00
|
|
|
if (
|
|
|
|
j.account.id !== row.account.id &&
|
|
|
|
this.voucher.journals.filter((x) => x.account.id === j.account.id).length
|
|
|
|
) {
|
2018-05-25 13:49:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Object.assign(row, j);
|
|
|
|
this.updateView();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteRow(row: Journal) {
|
|
|
|
this.voucher.journals.splice(this.voucher.journals.indexOf(row), 1);
|
|
|
|
this.updateView();
|
|
|
|
}
|
|
|
|
|
|
|
|
createForm() {
|
|
|
|
this.form = this.fb.group({
|
|
|
|
date: '',
|
|
|
|
receiptAccount: '',
|
2020-10-01 15:21:22 +00:00
|
|
|
receiptAmount: { value: '', disabled: true },
|
2018-05-25 13:49:00 +00:00
|
|
|
addRow: this.fb.group({
|
|
|
|
account: '',
|
2020-10-01 15:21:22 +00:00
|
|
|
amount: '',
|
2018-05-25 13:49:00 +00:00
|
|
|
}),
|
2020-10-01 15:21:22 +00:00
|
|
|
narration: '',
|
2018-05-25 13:49:00 +00:00
|
|
|
});
|
|
|
|
this.accBal = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
canSave() {
|
|
|
|
if (!this.voucher.id) {
|
|
|
|
return true;
|
2020-05-30 04:59:23 +00:00
|
|
|
} else if (this.voucher.posted && this.auth.user.perms.indexOf('edit-posted-vouchers') !== -1) {
|
2018-05-25 13:49:00 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2020-10-01 15:21:22 +00:00
|
|
|
return (
|
|
|
|
this.voucher.user.id === this.auth.user.id ||
|
|
|
|
this.auth.user.perms.indexOf("edit-other-user's-vouchers") !== -1
|
|
|
|
);
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
post() {
|
2020-10-01 15:21:22 +00:00
|
|
|
this.ser.post(this.voucher.id).subscribe(
|
|
|
|
(result) => {
|
|
|
|
this.loadVoucher(result);
|
|
|
|
this.toaster.show('Success', 'Voucher Posted');
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
this.toaster.show('Danger', error);
|
|
|
|
},
|
|
|
|
);
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
2020-06-02 03:05:36 +00:00
|
|
|
const voucher: Voucher = this.getVoucher();
|
2020-10-01 15:21:22 +00:00
|
|
|
this.ser.saveOrUpdate(voucher).subscribe(
|
|
|
|
(result) => {
|
|
|
|
this.toaster.show('Success', '');
|
|
|
|
if (voucher.id === result.id) {
|
|
|
|
this.loadVoucher(result);
|
|
|
|
} else {
|
2019-06-12 14:04:50 +00:00
|
|
|
this.router.navigate(['/receipt', result.id]);
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
2020-10-01 15:21:22 +00:00
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
this.toaster.show('Danger', error);
|
|
|
|
},
|
|
|
|
);
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getVoucher(): Voucher {
|
|
|
|
const formModel = this.form.value;
|
|
|
|
this.voucher.date = moment(formModel.date).format('DD-MMM-YYYY');
|
|
|
|
this.receiptJournal.account.id = formModel.receiptAccount;
|
|
|
|
this.voucher.narration = formModel.narration;
|
|
|
|
return this.voucher;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete() {
|
2020-10-01 15:21:22 +00:00
|
|
|
this.ser.delete(this.voucher.id).subscribe(
|
|
|
|
(result) => {
|
|
|
|
this.toaster.show('Success', '');
|
|
|
|
this.router.navigate(['/receipt'], { replaceUrl: true });
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
this.toaster.show('Danger', error);
|
|
|
|
},
|
|
|
|
);
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
confirmDelete(): void {
|
|
|
|
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
|
|
|
|
width: '250px',
|
2020-10-01 15:21:22 +00:00
|
|
|
data: { title: 'Delete Voucher?', content: 'Are you sure? This cannot be undone.' },
|
2018-05-25 13:49:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
dialogRef.afterClosed().subscribe((result: boolean) => {
|
|
|
|
if (result) {
|
|
|
|
this.delete();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
listenToAccountAutocompleteChange(): void {
|
|
|
|
const control = this.form.get('addRow').get('account');
|
2020-10-01 15:21:22 +00:00
|
|
|
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))),
|
|
|
|
);
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
listenToReceiptAccountChange(): void {
|
2020-10-01 15:21:22 +00:00
|
|
|
this.form.get('receiptAccount').valueChanges.subscribe((x) =>
|
|
|
|
this.router.navigate([], {
|
|
|
|
relativeTo: this.route,
|
|
|
|
queryParams: { a: x },
|
|
|
|
replaceUrl: true,
|
|
|
|
}),
|
|
|
|
);
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
displayAccount(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',
|
2020-10-01 15:21:22 +00:00
|
|
|
data: file.resized,
|
2018-05-25 13:49:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteImage(file: DbFile) {
|
|
|
|
const index = this.voucher.files.indexOf(file);
|
|
|
|
this.voucher.files.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
detectFiles(event) {
|
|
|
|
const files = event.target.files;
|
|
|
|
if (files) {
|
|
|
|
for (const file of files) {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = (e: any) => {
|
2020-10-01 15:21:22 +00:00
|
|
|
zip(
|
|
|
|
of(e.target.result),
|
2018-05-25 13:49:00 +00:00
|
|
|
this.resizeImage(e.target.result, 100, 150),
|
2020-10-01 15:21:22 +00:00
|
|
|
this.resizeImage(e.target.result, 825, 1170),
|
|
|
|
).subscribe((val) =>
|
|
|
|
this.voucher.files.push({ id: null, thumbnail: val[1], resized: val[2] }),
|
2018-05-25 13:49:00 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resizeImage(image, MAX_WIDTH, MAX_HEIGHT) {
|
|
|
|
const canvas = document.createElement('canvas');
|
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
const img = new Image();
|
|
|
|
const ex = fromEvent(img, 'load').pipe(
|
|
|
|
map((e) => {
|
|
|
|
let width = img.naturalWidth,
|
|
|
|
height = img.naturalHeight;
|
|
|
|
const ratio = Math.min(1, MAX_WIDTH / width, MAX_HEIGHT / height);
|
|
|
|
|
|
|
|
if (ratio === 1) {
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
width *= ratio;
|
|
|
|
height *= ratio;
|
|
|
|
|
|
|
|
canvas.width = width;
|
|
|
|
canvas.height = height;
|
|
|
|
ctx.drawImage(img, 0, 0, width, height);
|
|
|
|
return canvas.toDataURL('image/jpeg', 0.95);
|
2020-10-01 15:21:22 +00:00
|
|
|
}),
|
2018-05-25 13:49:00 +00:00
|
|
|
);
|
|
|
|
img.src = image;
|
|
|
|
return ex;
|
|
|
|
}
|
|
|
|
}
|