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:
2020-10-07 18:41:17 +05:30
parent cefb3ebdcc
commit cfeef1795d
20 changed files with 202 additions and 120 deletions

View File

@ -1,9 +1,11 @@
import { Component, Inject, OnInit } from '@angular/core';
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { debounceTime, distinctUntilChanged, map, startWith, switchMap } from 'rxjs/operators';
import { FormBuilder, FormGroup } from '@angular/forms';
import { debounceTime, distinctUntilChanged, map, startWith, switchMap } from 'rxjs/operators';
import { Observable, of as observableOf } from 'rxjs';
import { round } from 'mathjs';
import { MathService } from '../shared/math.service';
import { Product } from '../core/product';
import { ProductService } from '../product/product.service';
@ -21,6 +23,7 @@ export class PurchaseDialogComponent implements OnInit {
public dialogRef: MatDialogRef<PurchaseDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: any,
private fb: FormBuilder,
private math: MathService,
private productSer: ProductService,
) {
this.createForm();
@ -70,16 +73,16 @@ export class PurchaseDialogComponent implements OnInit {
accept(): void {
const formValue = this.form.value;
const quantity = +formValue.quantity;
const price = +formValue.price;
const tax = +formValue.tax;
const discount = +formValue.discount;
const quantity = this.math.parseAmount(formValue.quantity, 2);
const price = this.math.parseAmount(formValue.price, 2);
const tax = this.math.parseAmount(formValue.tax, 5);
const discount = this.math.parseAmount(formValue.discount, 5);
this.data.inventory.product = this.product;
this.data.inventory.quantity = quantity;
this.data.inventory.rate = price;
this.data.inventory.tax = tax;
this.data.inventory.discount = discount;
this.data.inventory.amount = quantity * price * (1 + tax) * (1 - discount);
this.data.inventory.amount = round(quantity * price * (1 + tax) * (1 - discount), 2);
this.dialogRef.close(this.data.inventory);
}
}

View File

@ -10,6 +10,8 @@ import { VoucherService } from '../core/voucher.service';
import { AccountService } from '../core/account.service';
import { DbFile, Inventory, Voucher } from '../core/voucher';
import * as moment from 'moment';
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';
@ -19,6 +21,7 @@ import { ImageDialogComponent } from '../shared/image-dialog/image-dialog.compon
import { ProductService } from '../product/product.service';
import { Product } from '../core/product';
import { Hotkey, HotkeysService } from 'angular2-hotkeys';
import { conditionallyCreateMapObjectLiteral } from '@angular/compiler/src/render3/view/util';
@Component({
selector: 'app-purchase',
@ -49,6 +52,7 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
private hotkeys: HotkeysService,
private toaster: ToasterService,
private auth: AuthService,
private math: MathService,
private ser: VoucherService,
private productSer: ProductService,
private accountSer: AccountService,
@ -139,10 +143,10 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
addRow() {
const formValue = this.form.get('addRow').value;
const quantity = +formValue.quantity;
const price = +formValue.price;
const tax = +formValue.tax;
const discount = +formValue.discount;
const quantity = this.math.parseAmount(formValue.quantity, 2);
const price = this.math.parseAmount(formValue.price, 2);
const tax = this.math.parseAmount(formValue.tax, 5);
const discount = this.math.parseAmount(formValue.discount, 5);
if (this.product === null || quantity <= 0 || price <= 0) {
return;
}
@ -157,7 +161,7 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
rate: price,
tax: tax,
discount: discount,
amount: quantity * price * (1 + tax) * (1 - discount),
amount: round(quantity * price * (1 + tax) * (1 - discount), 2),
product: this.product,
batch: null,
});
@ -183,7 +187,10 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
this.inventoryObservable.next(this.voucher.inventories);
this.form
.get('amount')
.setValue(Math.abs(this.voucher.inventories.map((x) => x.amount).reduce((p, c) => p + c, 0)));
.setValue(
round(Math.abs(this.voucher.inventories.map((x) => x.amount).reduce((p, c) => p + c, 0))),
2,
);
}
editRow(row: Inventory) {