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/http-client": "^2.1.0",
"@ngx-loading-bar/router": "^2.1.0", "@ngx-loading-bar/router": "^2.1.0",
"core-js": "^2.5.4", "core-js": "^2.5.4",
"mathjs": "^5.0.0",
"moment": "^2.22.1", "moment": "^2.22.1",
"rxjs": "^6.2.0", "rxjs": "^6.2.0",
"zone.js": "^0.8.26" "zone.js": "^0.8.26"

View File

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

View File

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