Added: Mathjs for evaluating expressions in journal amount

This commit is contained in:
tanshu 2018-06-29 13:32:52 +05:30
parent c6272762da
commit ede445ac1f
4 changed files with 42 additions and 8 deletions

View File

@ -27,6 +27,7 @@
"@ngx-loading-bar/http-client": "^2.1.0",
"@ngx-loading-bar/router": "^2.1.0",
"core-js": "^2.5.4",
"mathjs": "^5.0.0",
"moment": "^2.22.1",
"rxjs": "^6.2.0",
"zone.js": "^0.8.26"

View File

@ -9,6 +9,7 @@ import {VoucherService} from './voucher.service';
import {AccountService} from '../account/account.service';
import {DbFile, Journal, Voucher} from './voucher';
import * as moment from 'moment';
import * as math from 'mathjs';
import {AuthService} from '../auth/auth.service';
import {ConfirmDialogComponent} from '../shared/confirm-dialog/confirm-dialog.component';
import {ToasterService} from '../core/toaster.service';
@ -44,6 +45,7 @@ export class JournalComponent implements OnInit, AfterViewInit {
private ser: VoucherService,
private accountSer: AccountService
) {
this.account = null;
this.createForm();
this.setupAccountAutocomplete();
}
@ -82,22 +84,21 @@ export class JournalComponent implements OnInit, AfterViewInit {
addRow() {
const formValue = this.form.get('addRow').value;
const amount = +formValue.amount;
const debit = +formValue.debit;
if (this.account === null || amount === 0) {
const amount = this.rowAmount(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) + (debit * amount);
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: debit,
amount: amount,
debit: amount.debit,
amount: amount.amount,
account: this.account,
costCentre: null
});
@ -106,6 +107,18 @@ export class JournalComponent implements OnInit, AfterViewInit {
this.resetAddRow();
}
rowAmount(amount: string = '', debit: number): any {
try {
const val = math.eval(amount.trim().replace(',', ''));
if (val < 0) {
debit *= -1;
}
return {debit: debit, amount: Math.abs(val)};
} catch {
return {debit: debit, amount: 0};
}
}
resetAddRow() {
const amount = Math.abs(this.voucher.journals.map((x) => x.debit * x.amount).reduce((p, c) => p + c));
const debit = this.form.get('addRow').value.debit;

View File

@ -9,6 +9,7 @@ import {VoucherService} from '../journal/voucher.service';
import {AccountService} from '../account/account.service';
import {DbFile, Journal, Voucher} from '../journal/voucher';
import * as moment from 'moment';
import * as math from 'mathjs';
import {AuthService} from '../auth/auth.service';
import {ConfirmDialogComponent} from '../shared/confirm-dialog/confirm-dialog.component';
import {ToasterService} from '../core/toaster.service';
@ -46,6 +47,7 @@ export class PaymentComponent implements OnInit, AfterViewInit {
private ser: VoucherService,
private accountSer: AccountService
) {
this.account = null;
this.createForm();
this.listenToAccountAutocompleteChange();
this.listenToPaymentAccountChange();
@ -88,7 +90,7 @@ export class PaymentComponent implements OnInit, AfterViewInit {
}
addRow() {
const amount = +(this.form.get('addRow').value.amount);
const amount = this.rowAmount(this.form.get('addRow').value.amount);
const debit = 1;
if (this.account === null || amount <= 0) {
return;
@ -113,6 +115,14 @@ export class PaymentComponent implements OnInit, AfterViewInit {
this.updateView();
}
rowAmount(amount: string = ''): number {
try {
return math.eval(amount.trim().replace(',', ''));
} catch {
return 0;
}
}
resetAddRow() {
this.form.get('addRow').reset({
account: null,

View File

@ -9,6 +9,7 @@ import {VoucherService} from '../journal/voucher.service';
import {AccountService} from '../account/account.service';
import {DbFile, Journal, Voucher} from '../journal/voucher';
import * as moment from 'moment';
import * as math from 'mathjs';
import {AuthService} from '../auth/auth.service';
import {ConfirmDialogComponent} from '../shared/confirm-dialog/confirm-dialog.component';
import {ToasterService} from '../core/toaster.service';
@ -46,6 +47,7 @@ export class ReceiptComponent implements OnInit, AfterViewInit {
private ser: VoucherService,
private accountSer: AccountService
) {
this.account = null;
this.createForm();
this.listenToAccountAutocompleteChange();
this.listenToReceiptAccountChange();
@ -87,7 +89,7 @@ export class ReceiptComponent implements OnInit, AfterViewInit {
}
addRow() {
const amount = +(this.form.get('addRow').value.amount);
const amount = this.rowAmount(this.form.get('addRow').value.amount);
const debit = -1;
if (this.account === null || amount <= 0) {
return;
@ -112,6 +114,14 @@ export class ReceiptComponent implements OnInit, AfterViewInit {
this.updateView();
}
rowAmount(amount: string = ''): number {
try {
return math.eval(amount.trim().replace(',', ''));
} catch {
return 0;
}
}
resetAddRow() {
this.form.get('addRow').reset({
account: null,