diff --git a/overlord/package.json b/overlord/package.json index ceb33f6d..7fd4098d 100644 --- a/overlord/package.json +++ b/overlord/package.json @@ -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" diff --git a/overlord/src/app/journal/journal.component.ts b/overlord/src/app/journal/journal.component.ts index 8188523f..273aeac3 100644 --- a/overlord/src/app/journal/journal.component.ts +++ b/overlord/src/app/journal/journal.component.ts @@ -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; diff --git a/overlord/src/app/payment/payment.component.ts b/overlord/src/app/payment/payment.component.ts index 32d0678d..db51524d 100644 --- a/overlord/src/app/payment/payment.component.ts +++ b/overlord/src/app/payment/payment.component.ts @@ -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, diff --git a/overlord/src/app/receipt/receipt.component.ts b/overlord/src/app/receipt/receipt.component.ts index 5528ddc7..d65977db 100644 --- a/overlord/src/app/receipt/receipt.component.ts +++ b/overlord/src/app/receipt/receipt.component.ts @@ -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,