diff --git a/overlord/src/app/purchase/purchase.component.ts b/overlord/src/app/purchase/purchase.component.ts index 59ad0556..259a9e0e 100644 --- a/overlord/src/app/purchase/purchase.component.ts +++ b/overlord/src/app/purchase/purchase.component.ts @@ -114,7 +114,7 @@ export class PurchaseComponent implements OnInit, AfterViewInit { dataSource: PurchaseDataSource = new PurchaseDataSource(this.inventoryObservable); form: FormGroup<{ date: FormControl; - account: FormControl; + account: FormControl; amount: FormControl; addRow: FormGroup<{ product: FormControl; @@ -140,7 +140,7 @@ export class PurchaseComponent implements OnInit, AfterViewInit { constructor() { this.form = new FormGroup({ date: new FormControl(moment(new Date()), { nonNullable: true }), - account: new FormControl(null), + account: new FormControl(null), amount: new FormControl({ value: 0, disabled: true }, { nonNullable: true }), addRow: new FormGroup({ product: new FormControl(''), @@ -157,7 +157,7 @@ export class PurchaseComponent implements OnInit, AfterViewInit { this.accounts = this.form.controls.account.valueChanges.pipe( debounceTime(150), distinctUntilChanged(), - switchMap((x) => (x === null ? observableOf([]) : this.accountSer.autocomplete(x))), + switchMap((x) => (x === null ? observableOf([]) : this.accountSer.autocomplete(typeof x === 'string' ? x : x.name))), ); this.tags = this.form.controls.tags.valueChanges.pipe( debounceTime(150), @@ -169,17 +169,18 @@ export class PurchaseComponent implements OnInit, AfterViewInit { this.products = this.form.controls.addRow.controls.product.valueChanges.pipe( debounceTime(150), distinctUntilChanged(), - switchMap((x) => - x === null + switchMap((x) => { + const account = this.form.value.account; + const vendorId = account && typeof account === 'object' ? account.id : undefined; + return x === null ? observableOf([]) : this.productSer.autocompleteSku( - x, - true, - moment(this.form.value.date).format('DD-MMM-YYYY'), - // this.form.value.account?.id, - '', - ), - ), + x, + true, + moment(this.form.value.date).format('DD-MMM-YYYY'), + vendorId, + ); + }), ); } @@ -199,7 +200,7 @@ export class PurchaseComponent implements OnInit, AfterViewInit { this.voucher = voucher; this.form.setValue({ date: moment(this.voucher.date, 'DD-MMM-YYYY'), - account: this.voucher.vendor?.name ?? null, + account: this.voucher.vendor ?? null, amount: Math.abs(this.voucher.inventories.map((x) => x.amount).reduce((p, c) => p + c, 0)), addRow: { product: '', @@ -303,6 +304,10 @@ export class PurchaseComponent implements OnInit, AfterViewInit { } canSave() { + const account = this.form.value.account; + if (!account || typeof account === 'string') { + return false; + } if (!this.voucher.id) { return true; } @@ -344,7 +349,9 @@ export class PurchaseComponent implements OnInit, AfterViewInit { getVoucher(): Voucher { const formModel = this.form.value; this.voucher.date = moment(formModel.date).format('DD-MMM-YYYY'); - if (formModel.account !== null && typeof formModel.account !== 'string') { + if (!formModel.account || typeof formModel.account === 'string') { + this.voucher.vendor = undefined; + } else { this.voucher.vendor = formModel.account; } this.voucher.narration = formModel.narration ?? '';