Fix: Autocomplete could fuck up and was definitely doing it in product.
When checking for .name, it would error out if the input was null Fix: Journal addRow reset would reset Debit/Credit
This commit is contained in:
@ -4,7 +4,7 @@ import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
|
||||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
|
||||
import { round } from 'mathjs';
|
||||
import { Observable, of as observableOf } from 'rxjs';
|
||||
import { debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators';
|
||||
import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators';
|
||||
|
||||
import { Batch } from '../core/batch';
|
||||
import { Inventory } from '../core/inventory';
|
||||
@ -21,7 +21,7 @@ import { MathService } from '../shared/math.service';
|
||||
export class PurchaseDialogComponent implements OnInit {
|
||||
products: Observable<ProductSku[]>;
|
||||
form: FormGroup<{
|
||||
product: FormControl<ProductSku | string | null>;
|
||||
product: FormControl<string | null>;
|
||||
quantity: FormControl<string>;
|
||||
price: FormControl<string>;
|
||||
tax: FormControl<string>;
|
||||
@ -37,7 +37,7 @@ export class PurchaseDialogComponent implements OnInit {
|
||||
private productSer: ProductService,
|
||||
) {
|
||||
this.form = new FormGroup({
|
||||
product: new FormControl<ProductSku | string | null>(null),
|
||||
product: new FormControl<string | null>(null),
|
||||
quantity: new FormControl('', { nonNullable: true }),
|
||||
price: new FormControl('', { nonNullable: true }),
|
||||
tax: new FormControl('', { nonNullable: true }),
|
||||
@ -45,8 +45,6 @@ export class PurchaseDialogComponent implements OnInit {
|
||||
});
|
||||
// Listen to Product Autocomplete Change
|
||||
this.products = this.form.controls.product.valueChanges.pipe(
|
||||
map((x) => ((x as ProductSku).name !== undefined ? (x as ProductSku).name : (x as string))),
|
||||
map((x) => (x !== null && x.length >= 1 ? x : null)),
|
||||
debounceTime(150),
|
||||
distinctUntilChanged(),
|
||||
switchMap((x) => (x === null ? observableOf([]) : this.productSer.autocompleteSku(x, true))),
|
||||
@ -55,7 +53,7 @@ export class PurchaseDialogComponent implements OnInit {
|
||||
|
||||
ngOnInit() {
|
||||
this.form.setValue({
|
||||
product: this.data.inventory.batch?.sku,
|
||||
product: this.data.inventory.batch?.sku.name,
|
||||
quantity: `${this.data.inventory.quantity}`,
|
||||
price: `${this.data.inventory.rate}`,
|
||||
tax: `${this.data.inventory.tax}`,
|
||||
@ -64,8 +62,8 @@ export class PurchaseDialogComponent implements OnInit {
|
||||
this.product = this.data.inventory.batch?.sku;
|
||||
}
|
||||
|
||||
displayFn(product?: Product): string {
|
||||
return product ? product.name : '';
|
||||
displayFn(product?: Product | string): string {
|
||||
return !product ? '' : typeof product === 'string' ? product : product.name;
|
||||
}
|
||||
|
||||
productSelected(event: MatAutocompleteSelectedEvent): void {
|
||||
|
||||
@ -7,7 +7,7 @@ import { Hotkey, HotkeysService } from 'angular2-hotkeys';
|
||||
import { round } from 'mathjs';
|
||||
import * as moment from 'moment';
|
||||
import { BehaviorSubject, Observable, of as observableOf } from 'rxjs';
|
||||
import { debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators';
|
||||
import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators';
|
||||
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
import { Account } from '../core/account';
|
||||
@ -44,7 +44,7 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
dataSource: PurchaseDataSource = new PurchaseDataSource(this.inventoryObservable);
|
||||
form: FormGroup<{
|
||||
date: FormControl<Date>;
|
||||
account: FormControl<Account | string | null>;
|
||||
account: FormControl<string | null>;
|
||||
amount: FormControl<number>;
|
||||
addRow: FormGroup<{
|
||||
product: FormControl<string | null>;
|
||||
@ -80,7 +80,7 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
) {
|
||||
this.form = new FormGroup({
|
||||
date: new FormControl(new Date(), { nonNullable: true }),
|
||||
account: new FormControl<Account | string | null>(null),
|
||||
account: new FormControl<string | null>(null),
|
||||
amount: new FormControl({ value: 0, disabled: true }, { nonNullable: true }),
|
||||
addRow: new FormGroup({
|
||||
product: new FormControl(''),
|
||||
@ -94,15 +94,12 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
this.accBal = null;
|
||||
// Listen to Account Autocomplete Change
|
||||
this.accounts = this.form.controls.account.valueChanges.pipe(
|
||||
map((x) => ((x as Account).name !== undefined ? (x as Account).name : (x as string))),
|
||||
map((x) => (x !== null && x.length >= 1 ? x : null)),
|
||||
debounceTime(150),
|
||||
distinctUntilChanged(),
|
||||
switchMap((x) => (x === null ? observableOf([]) : this.accountSer.autocomplete(x))),
|
||||
);
|
||||
// Listen to Product Autocomplete Change
|
||||
this.products = this.form.controls.addRow.controls.product.valueChanges.pipe(
|
||||
map((x) => (x !== null && x.length >= 1 ? x : null)),
|
||||
debounceTime(150),
|
||||
distinctUntilChanged(),
|
||||
switchMap((x) =>
|
||||
@ -177,7 +174,7 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
this.voucher = voucher;
|
||||
this.form.setValue({
|
||||
date: moment(this.voucher.date, 'DD-MMM-YYYY').toDate(),
|
||||
account: this.voucher.vendor || null,
|
||||
account: this.voucher.vendor?.name ?? null,
|
||||
amount: Math.abs(this.voucher.inventories.map((x) => x.amount).reduce((p, c) => p + c, 0)),
|
||||
addRow: {
|
||||
product: '',
|
||||
@ -364,8 +361,8 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
});
|
||||
}
|
||||
|
||||
displayFn(item?: Account | Product): string {
|
||||
return item ? item.name : '';
|
||||
displayFn(item?: Account | Product | string): string {
|
||||
return !item ? '' : typeof item === 'string' ? item : item.name;
|
||||
}
|
||||
|
||||
accountSelected(event: MatAutocompleteSelectedEvent): void {
|
||||
|
||||
Reference in New Issue
Block a user