Feature: Allow math expressions on all journal inputs and also round them properly.
Chore: Prettied index.html, main.ts and styles.css Updated Dependencies
This commit is contained in:
@ -1,11 +1,12 @@
|
||||
import { Component, Inject, OnInit } from '@angular/core';
|
||||
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
|
||||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
|
||||
import { FormBuilder, FormGroup } from '@angular/forms';
|
||||
import { debounceTime, distinctUntilChanged, map, startWith, switchMap } from 'rxjs/operators';
|
||||
import { Observable, of as observableOf } from 'rxjs';
|
||||
import { MathService } from '../shared/math.service';
|
||||
import { Account } from '../core/account';
|
||||
import { AccountService } from '../core/account.service';
|
||||
import { FormBuilder, FormGroup } from '@angular/forms';
|
||||
import { Observable, of as observableOf } from 'rxjs';
|
||||
|
||||
@Component({
|
||||
selector: 'app-receipt-dialog',
|
||||
@ -22,6 +23,7 @@ export class ReceiptDialogComponent implements OnInit {
|
||||
public dialogRef: MatDialogRef<ReceiptDialogComponent>,
|
||||
@Inject(MAT_DIALOG_DATA) public data: any,
|
||||
private fb: FormBuilder,
|
||||
private math: MathService,
|
||||
private accountSer: AccountService,
|
||||
) {
|
||||
this.createForm();
|
||||
@ -68,7 +70,7 @@ export class ReceiptDialogComponent implements OnInit {
|
||||
|
||||
accept(): void {
|
||||
const formValue = this.form.value;
|
||||
const amount = +formValue.amount;
|
||||
const amount = this.math.parseAmount(formValue.amount, 2);
|
||||
this.data.journal.account = this.account;
|
||||
this.data.journal.amount = amount;
|
||||
this.dialogRef.close(this.data.journal);
|
||||
|
||||
@ -10,7 +10,8 @@ import { VoucherService } from '../core/voucher.service';
|
||||
import { AccountService } from '../core/account.service';
|
||||
import { DbFile, Journal, Voucher } from '../core/voucher';
|
||||
import * as moment from 'moment';
|
||||
import { evaluate } from 'mathjs';
|
||||
import { round } from 'mathjs';
|
||||
import { MathService } from '../shared/math.service';
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
import { ConfirmDialogComponent } from '../shared/confirm-dialog/confirm-dialog.component';
|
||||
import { ToasterService } from '../core/toaster.service';
|
||||
@ -48,6 +49,7 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
private hotkeys: HotkeysService,
|
||||
private toaster: ToasterService,
|
||||
private auth: AuthService,
|
||||
private math: MathService,
|
||||
private ser: VoucherService,
|
||||
private accountSer: AccountService,
|
||||
) {
|
||||
@ -136,7 +138,7 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
}
|
||||
|
||||
addRow() {
|
||||
const amount = this.rowAmount(this.form.get('addRow').value.amount);
|
||||
const amount = this.math.parseAmount(this.form.get('addRow').value.amount, 2);
|
||||
const debit = -1;
|
||||
if (this.account === null || amount <= 0) {
|
||||
return;
|
||||
@ -160,15 +162,15 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
this.resetAddRow();
|
||||
this.updateView();
|
||||
}
|
||||
|
||||
rowAmount(amount: string = ''): number {
|
||||
try {
|
||||
amount = amount.replace(new RegExp('(₹[s]*)|(,)|(s)', 'g'), '');
|
||||
return evaluate(amount);
|
||||
} catch {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
//
|
||||
// rowAmount(amount: string = ''): number {
|
||||
// try {
|
||||
// amount = amount.replace(new RegExp('(₹[s]*)|(,)|(s)', 'g'), '');
|
||||
// return round(evaluate(amount), 2);
|
||||
// } catch {
|
||||
// return 0;
|
||||
// }
|
||||
// }
|
||||
|
||||
resetAddRow() {
|
||||
this.form.get('addRow').reset({
|
||||
@ -185,7 +187,10 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
updateView() {
|
||||
const journals = this.voucher.journals.filter((x) => x.debit === -1);
|
||||
this.journalObservable.next(journals);
|
||||
this.receiptJournal.amount = Math.abs(journals.map((x) => x.amount).reduce((p, c) => p + c, 0));
|
||||
this.receiptJournal.amount = round(
|
||||
Math.abs(journals.map((x) => x.amount).reduce((p, c) => p + c, 0)),
|
||||
2,
|
||||
);
|
||||
this.form.get('receiptAmount').setValue(this.receiptJournal.amount);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user