From bddd5ec7493515a675a5440e9d62db01ce63f6a6 Mon Sep 17 00:00:00 2001 From: Amritanshu Date: Sun, 17 Jul 2022 14:31:19 +0530 Subject: [PATCH] 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 --- .../employee-attendance.component.ts | 14 +++++------ .../employee-benefits.component.ts | 7 +++--- .../src/app/issue/issue-dialog.component.ts | 22 ++++++++--------- overlord/src/app/issue/issue.component.ts | 16 ++++++------- .../app/journal/journal-dialog.component.ts | 7 +++--- overlord/src/app/journal/journal.component.ts | 8 +++---- overlord/src/app/ledger/ledger.component.ts | 16 ++++++------- .../app/payment/payment-dialog.component.ts | 7 +++--- overlord/src/app/payment/payment.component.ts | 7 +++--- .../product-ledger.component.ts | 16 ++++++------- .../purchase-return-dialog.component.ts | 22 ++++++++--------- .../purchase-return.component.ts | 24 +++++++++---------- .../app/purchase/purchase-dialog.component.ts | 14 +++++------ .../src/app/purchase/purchase.component.ts | 15 +++++------- .../rate-contract-detail.component.ts | 22 +++++++---------- .../app/receipt/receipt-dialog.component.ts | 7 +++--- overlord/src/app/receipt/receipt.component.ts | 7 +++--- 17 files changed, 104 insertions(+), 127 deletions(-) diff --git a/overlord/src/app/employee-attendance/employee-attendance.component.ts b/overlord/src/app/employee-attendance/employee-attendance.component.ts index 0868e047..f1f6b2b3 100644 --- a/overlord/src/app/employee-attendance/employee-attendance.component.ts +++ b/overlord/src/app/employee-attendance/employee-attendance.component.ts @@ -5,7 +5,7 @@ import { MatDialog } from '@angular/material/dialog'; import { ActivatedRoute, Router } from '@angular/router'; 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 { AttendanceType } from '../attendance/attendance-type'; import { AuthService } from '../auth/auth.service'; @@ -33,7 +33,7 @@ export class EmployeeAttendanceComponent implements OnInit, AfterViewInit { form: FormGroup<{ startDate: FormControl; finishDate: FormControl; - employee: FormControl; + employee: FormControl; attendances: FormArray< FormGroup<{ attendanceType: FormControl; @@ -60,13 +60,11 @@ export class EmployeeAttendanceComponent implements OnInit, AfterViewInit { this.form = new FormGroup({ startDate: new FormControl(new Date(), { nonNullable: true }), finishDate: new FormControl(new Date(), { nonNullable: true }), - employee: new FormControl(null), + employee: new FormControl(null), attendances: new FormArray }>>([]), }); // Listen to Employee Value Changes this.employees = this.form.controls.employee.valueChanges.pipe( - map((x) => ((x as Employee).name !== undefined ? (x as Employee).name : (x as string))), - map((x) => (x !== null && x.length >= 1 ? x : null)), debounceTime(150), distinctUntilChanged(), switchMap((x) => (x === null ? observableOf([]) : this.employeeSer.autocomplete(x))), @@ -81,7 +79,7 @@ export class EmployeeAttendanceComponent implements OnInit, AfterViewInit { this.attendanceTypes = data.attendanceTypes; this.form.controls.startDate.setValue(moment(this.info.startDate, 'DD-MMM-YYYY').toDate()); this.form.controls.finishDate.setValue(moment(this.info.finishDate, 'DD-MMM-YYYY').toDate()); - this.form.controls.employee.setValue(this.info.employee); + this.form.controls.employee.setValue(this.info.employee.name); this.form.controls.attendances.reset(); this.info.body.forEach((x) => this.form.controls.attendances.push( @@ -103,8 +101,8 @@ export class EmployeeAttendanceComponent implements OnInit, AfterViewInit { }, 0); } - displayFn(employee?: Employee): string { - return employee ? employee.name : ''; + displayFn(employee?: Employee | string): string { + return !employee ? '' : typeof employee === 'string' ? employee : employee.name; } selected(event: MatAutocompleteSelectedEvent): void { diff --git a/overlord/src/app/employee-benefits/employee-benefits.component.ts b/overlord/src/app/employee-benefits/employee-benefits.component.ts index bf667fde..6d53dbfc 100644 --- a/overlord/src/app/employee-benefits/employee-benefits.component.ts +++ b/overlord/src/app/employee-benefits/employee-benefits.component.ts @@ -5,7 +5,7 @@ import { MatDialog } from '@angular/material/dialog'; import { ActivatedRoute, Router } from '@angular/router'; 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 { AccountBalance } from '../core/account-balance'; @@ -75,7 +75,6 @@ export class EmployeeBenefitsComponent implements OnInit, AfterViewInit { }); // Setup Employee Autocomplete this.employees = this.form.controls.addRow.controls.employee.valueChanges.pipe( - map((x) => (x !== null && x.length >= 1 ? x : null)), debounceTime(150), distinctUntilChanged(), switchMap((x) => (x === null ? observableOf([]) : this.employeeSer.autocomplete(x))), @@ -134,8 +133,8 @@ export class EmployeeBenefitsComponent implements OnInit, AfterViewInit { this.benefitsObservable.next(this.voucher.employeeBenefits); } - displayFn(employee?: Employee): string { - return employee ? employee.name : ''; + displayFn(employee?: Employee | string): string { + return !employee ? '' : typeof employee === 'string' ? employee : employee.name; } employeeSelected(event: MatAutocompleteSelectedEvent): void { diff --git a/overlord/src/app/issue/issue-dialog.component.ts b/overlord/src/app/issue/issue-dialog.component.ts index 2ebc8078..d4d0282b 100644 --- a/overlord/src/app/issue/issue-dialog.component.ts +++ b/overlord/src/app/issue/issue-dialog.component.ts @@ -2,8 +2,8 @@ import { Component, Inject, OnInit } from '@angular/core'; import { FormControl, FormGroup } from '@angular/forms'; import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete'; import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; -import { Observable } from 'rxjs'; -import { debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators'; +import { Observable, of as observableOf } from 'rxjs'; +import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators'; import { Batch } from '../core/batch'; import { BatchService } from '../core/batch.service'; @@ -18,7 +18,7 @@ import { MathService } from '../shared/math.service'; export class IssueDialogComponent implements OnInit { batches: Observable; form: FormGroup<{ - batch: FormControl; + batch: FormControl; quantity: FormControl; }>; @@ -31,29 +31,29 @@ export class IssueDialogComponent implements OnInit { private batchSer: BatchService, ) { this.form = new FormGroup({ - batch: new FormControl(null), + batch: new FormControl(null), quantity: new FormControl('', { nonNullable: true }), }); // Listen to Batch Autocomplete Change this.batches = this.form.controls.batch.valueChanges.pipe( - map((x) => ((x as Batch).name !== undefined ? (x as Batch).name : (x as string))), - map((x) => (x !== null && x.length >= 1 ? x : '')), debounceTime(150), distinctUntilChanged(), - switchMap((x) => this.batchSer.autocomplete(this.data.date, x)), + switchMap((x) => + x === null ? observableOf([]) : this.batchSer.autocomplete(this.data.date, x), + ), ); } ngOnInit() { this.form.setValue({ - batch: this.data.inventory.batch, + batch: this.data.inventory.batch.name, quantity: `${this.data.inventory.quantity}`, }); - this.batch = this.data.inventory.batch as Batch; + this.batch = this.data.inventory.batch; } - displayFn(batch?: Batch): string { - return batch ? batch.name : ''; + displayFn(batch?: Batch | string): string { + return !batch ? '' : typeof batch === 'string' ? batch : batch.name; } batchSelected(event: MatAutocompleteSelectedEvent): void { diff --git a/overlord/src/app/issue/issue.component.ts b/overlord/src/app/issue/issue.component.ts index 944c8711..28a11686 100644 --- a/overlord/src/app/issue/issue.component.ts +++ b/overlord/src/app/issue/issue.component.ts @@ -6,7 +6,7 @@ import { ActivatedRoute, Router } from '@angular/router'; import { Hotkey, HotkeysService } from 'angular2-hotkeys'; import { round } from 'mathjs'; import * as moment from 'moment'; -import { BehaviorSubject, Observable } from 'rxjs'; +import { BehaviorSubject, Observable, of as observableOf } from 'rxjs'; import { debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators'; import { AuthService } from '../auth/auth.service'; @@ -45,7 +45,7 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy { destination: FormControl; amount: FormControl; addRow: FormGroup<{ - batch: FormControl; + batch: FormControl; quantity: FormControl; }>; narration: FormControl; @@ -78,19 +78,19 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy { destination: new FormControl(null), amount: new FormControl({ value: 0, disabled: true }, { nonNullable: true }), addRow: new FormGroup({ - batch: new FormControl(null), + batch: new FormControl(null), quantity: new FormControl('', { nonNullable: true }), }), narration: new FormControl('', { nonNullable: true }), }); // Listen to Batch Autocomplete Change this.batches = this.form.controls.addRow.controls.batch.valueChanges.pipe( - map((x) => ((x as Batch).name !== undefined ? (x as Batch).name : (x as string))), - map((x) => (x !== null && x.length >= 1 ? x : '')), debounceTime(150), distinctUntilChanged(), switchMap((x) => - this.batchSer.autocomplete(moment(this.form.value.date).format('DD-MMM-YYYY'), x), + x === null + ? observableOf([]) + : this.batchSer.autocomplete(moment(this.form.value.date).format('DD-MMM-YYYY'), x), ), ); // Listen to Date Change @@ -326,8 +326,8 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy { this.issueGridSer.issueGrid(date).subscribe((x) => this.gridObservable.next(x)); } - displayFn(batch?: Batch): string { - return batch ? batch.name : ''; + displayFn(batch?: Batch | string): string { + return !batch ? '' : typeof batch == 'string' ? batch : batch.name; } batchSelected(event: MatAutocompleteSelectedEvent): void { diff --git a/overlord/src/app/journal/journal-dialog.component.ts b/overlord/src/app/journal/journal-dialog.component.ts index cb8d4338..293acd34 100644 --- a/overlord/src/app/journal/journal-dialog.component.ts +++ b/overlord/src/app/journal/journal-dialog.component.ts @@ -3,7 +3,7 @@ import { FormControl, FormGroup } from '@angular/forms'; import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete'; import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; import { Observable, of as observableOf } from 'rxjs'; -import { debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators'; +import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators'; import { Account } from '../core/account'; import { AccountBalance } from '../core/account-balance'; @@ -41,7 +41,6 @@ export class JournalDialogComponent implements OnInit { this.accBal = null; // Setup Account Autocomplete this.accounts = this.form.controls.account.valueChanges.pipe( - map((x) => (x !== null && x.length >= 1 ? x : null)), debounceTime(150), distinctUntilChanged(), switchMap((x) => (x === null ? observableOf([]) : this.accountSer.autocomplete(x))), @@ -57,8 +56,8 @@ export class JournalDialogComponent implements OnInit { this.account = this.data.journal.account; } - displayFn(account?: Account): string { - return account ? account.name : ''; + displayFn(account?: Account | string): string { + return !account ? '' : typeof account === 'string' ? account : account.name; } accountSelected(event: MatAutocompleteSelectedEvent): void { diff --git a/overlord/src/app/journal/journal.component.ts b/overlord/src/app/journal/journal.component.ts index 50524280..1ff253f8 100644 --- a/overlord/src/app/journal/journal.component.ts +++ b/overlord/src/app/journal/journal.component.ts @@ -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'; @@ -80,7 +80,6 @@ export class JournalComponent implements OnInit, AfterViewInit, OnDestroy { this.accBal = null; // Setup Account Autocomplete this.accounts = this.form.controls.addRow.controls.account.valueChanges.pipe( - map((x) => (x !== null && x.length >= 1 ? x : null)), debounceTime(150), distinctUntilChanged(), switchMap((x) => (x === null ? observableOf([]) : this.accountSer.autocomplete(x))), @@ -201,6 +200,7 @@ export class JournalComponent implements OnInit, AfterViewInit, OnDestroy { 2, ); this.form.controls.addRow.reset({ + debit: this.form.value.addRow?.debit ?? 1, account: null, amount: `${amount}`, }); @@ -324,8 +324,8 @@ export class JournalComponent implements OnInit, AfterViewInit, OnDestroy { }); } - displayFn(account?: Account): string { - return account ? account.name : ''; + displayFn(account?: Account | string): string { + return !account ? '' : typeof account === 'string' ? account : account.name; } accountSelected(event: MatAutocompleteSelectedEvent): void { diff --git a/overlord/src/app/ledger/ledger.component.ts b/overlord/src/app/ledger/ledger.component.ts index b98c7639..1103e720 100644 --- a/overlord/src/app/ledger/ledger.component.ts +++ b/overlord/src/app/ledger/ledger.component.ts @@ -6,7 +6,7 @@ import { MatSort } from '@angular/material/sort'; import { ActivatedRoute, Router } from '@angular/router'; import * as moment from 'moment'; import { Observable, of as observableOf } from 'rxjs'; -import { debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators'; +import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators'; import { Account } from '../core/account'; import { AccountService } from '../core/account.service'; @@ -30,7 +30,7 @@ export class LedgerComponent implements OnInit, AfterViewInit { form: FormGroup<{ startDate: FormControl; finishDate: FormControl; - account: FormControl; + account: FormControl; }>; selectedRowId = ''; @@ -52,12 +52,10 @@ export class LedgerComponent implements OnInit, AfterViewInit { this.form = new FormGroup({ startDate: new FormControl(new Date(), { nonNullable: true }), finishDate: new FormControl(new Date(), { nonNullable: true }), - account: new FormControl(null), + account: new FormControl(null), }); 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))), @@ -71,7 +69,7 @@ export class LedgerComponent implements OnInit, AfterViewInit { this.info = data.info; this.calculateTotals(); this.form.setValue({ - account: this.info.account, + account: this.info.account.name, startDate: moment(this.info.startDate, 'DD-MMM-YYYY').toDate(), finishDate: moment(this.info.finishDate, 'DD-MMM-YYYY').toDate(), }); @@ -87,8 +85,8 @@ export class LedgerComponent implements OnInit, AfterViewInit { }, 0); } - displayFn(account?: Account): string { - return account ? account.name : ''; + displayFn(account?: Account | string): string { + return !account ? '' : typeof account === 'string' ? account : account.name; } calculateTotals() { @@ -131,7 +129,7 @@ export class LedgerComponent implements OnInit, AfterViewInit { const formModel = this.form.value; return new Ledger({ - account: formModel.account as Account, + account: this.info.account, startDate: moment(formModel.startDate).format('DD-MMM-YYYY'), finishDate: moment(formModel.finishDate).format('DD-MMM-YYYY'), }); diff --git a/overlord/src/app/payment/payment-dialog.component.ts b/overlord/src/app/payment/payment-dialog.component.ts index fa5784a9..bdf87d6c 100644 --- a/overlord/src/app/payment/payment-dialog.component.ts +++ b/overlord/src/app/payment/payment-dialog.component.ts @@ -3,7 +3,7 @@ import { FormControl, FormGroup } from '@angular/forms'; import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete'; import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; import { Observable, of as observableOf } from 'rxjs'; -import { debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators'; +import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators'; import { Account } from '../core/account'; import { AccountBalance } from '../core/account-balance'; @@ -39,7 +39,6 @@ export class PaymentDialogComponent implements OnInit { this.accBal = null; // Setup Account Autocomplete this.accounts = this.form.controls.account.valueChanges.pipe( - map((x) => (x !== null && x.length >= 1 ? x : null)), debounceTime(150), distinctUntilChanged(), switchMap((x) => (x === null ? observableOf([]) : this.accountSer.autocomplete(x))), @@ -54,8 +53,8 @@ export class PaymentDialogComponent implements OnInit { this.account = this.data.journal.account; } - displayFn(account?: Account): string { - return account ? account.name : ''; + displayFn(account?: Account | string): string { + return !account ? '' : typeof account === 'string' ? account : account.name; } accountSelected(event: MatAutocompleteSelectedEvent): void { diff --git a/overlord/src/app/payment/payment.component.ts b/overlord/src/app/payment/payment.component.ts index ce69ba32..8d2ebee6 100644 --- a/overlord/src/app/payment/payment.component.ts +++ b/overlord/src/app/payment/payment.component.ts @@ -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'; @@ -84,7 +84,6 @@ export class PaymentComponent implements OnInit, AfterViewInit, OnDestroy { this.accBal = null; // Listen to Account Autocomplete Change this.accounts = this.form.controls.addRow.controls.account.valueChanges.pipe( - map((x) => (x !== null && x.length >= 1 ? x : null)), debounceTime(150), distinctUntilChanged(), switchMap((x) => (x === null ? observableOf([]) : this.accountSer.autocomplete(x))), @@ -334,8 +333,8 @@ export class PaymentComponent implements OnInit, AfterViewInit, OnDestroy { }); } - displayFn(account?: Account): string { - return account ? account.name : ''; + displayFn(account?: Account | string): string { + return !account ? '' : typeof account === 'string' ? account : account.name; } accountSelected(event: MatAutocompleteSelectedEvent): void { diff --git a/overlord/src/app/product-ledger/product-ledger.component.ts b/overlord/src/app/product-ledger/product-ledger.component.ts index 63dff010..af540029 100644 --- a/overlord/src/app/product-ledger/product-ledger.component.ts +++ b/overlord/src/app/product-ledger/product-ledger.component.ts @@ -6,7 +6,7 @@ import { MatSort } from '@angular/material/sort'; import { ActivatedRoute, Router } from '@angular/router'; import * as moment from 'moment'; import { Observable, of as observableOf } from 'rxjs'; -import { debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators'; +import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators'; import { Product } from '../core/product'; import { ProductSku } from '../core/product-sku'; @@ -31,7 +31,7 @@ export class ProductLedgerComponent implements OnInit, AfterViewInit { form: FormGroup<{ startDate: FormControl; finishDate: FormControl; - product: FormControl; + product: FormControl; }>; selectedRowId = ''; @@ -67,12 +67,10 @@ export class ProductLedgerComponent implements OnInit, AfterViewInit { this.form = new FormGroup({ startDate: new FormControl(new Date(), { nonNullable: true }), finishDate: new FormControl(new Date(), { nonNullable: true }), - product: new FormControl(null), + product: new FormControl(null), }); this.products = this.form.controls.product.valueChanges.pipe( - map((x) => ((x as Product).name !== undefined ? (x as Product).name : (x as string))), - map((x) => (x !== null && x.length >= 1 ? x : null)), debounceTime(150), distinctUntilChanged(), switchMap((x) => @@ -88,7 +86,7 @@ export class ProductLedgerComponent implements OnInit, AfterViewInit { this.info = data.info; this.calculateTotals(); this.form.setValue({ - product: this.info.product, + product: this.info.product.name, startDate: moment(this.info.startDate, 'DD-MMM-YYYY').toDate(), finishDate: moment(this.info.finishDate, 'DD-MMM-YYYY').toDate(), }); @@ -104,8 +102,8 @@ export class ProductLedgerComponent implements OnInit, AfterViewInit { }, 0); } - displayFn(product?: Product): string { - return product ? product.name : ''; + displayFn(product?: Product | string): string { + return !product ? '' : typeof product === 'string' ? product : product.name; } calculateTotals() { @@ -150,7 +148,7 @@ export class ProductLedgerComponent implements OnInit, AfterViewInit { const formModel = this.form.value; return new ProductLedger({ - product: formModel.product as Product, + product: this.info.product, startDate: moment(formModel.startDate).format('DD-MMM-YYYY'), finishDate: moment(formModel.finishDate).format('DD-MMM-YYYY'), }); diff --git a/overlord/src/app/purchase-return/purchase-return-dialog.component.ts b/overlord/src/app/purchase-return/purchase-return-dialog.component.ts index 9a43f811..c75c5f10 100644 --- a/overlord/src/app/purchase-return/purchase-return-dialog.component.ts +++ b/overlord/src/app/purchase-return/purchase-return-dialog.component.ts @@ -2,8 +2,8 @@ import { Component, Inject, OnInit } from '@angular/core'; import { FormControl, FormGroup } from '@angular/forms'; import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete'; import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; -import { Observable } from 'rxjs'; -import { debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators'; +import { Observable, of as observableOf } from 'rxjs'; +import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators'; import { Batch } from '../core/batch'; import { BatchService } from '../core/batch.service'; @@ -18,7 +18,7 @@ import { MathService } from '../shared/math.service'; export class PurchaseReturnDialogComponent implements OnInit { batches: Observable; form: FormGroup<{ - batch: FormControl; + batch: FormControl; quantity: FormControl; }>; @@ -31,29 +31,29 @@ export class PurchaseReturnDialogComponent implements OnInit { private batchSer: BatchService, ) { this.form = new FormGroup({ - batch: new FormControl(null), + batch: new FormControl(null), quantity: new FormControl('', { nonNullable: true }), }); // Listen to Batch Autocomplete Change this.batches = this.form.controls.batch.valueChanges.pipe( - map((x) => ((x as Batch).name !== undefined ? (x as Batch).name : (x as string))), - map((x) => (x !== null && x.length >= 1 ? x : '')), debounceTime(150), distinctUntilChanged(), - switchMap((x) => this.batchSer.autocomplete(this.data.date, x)), + switchMap((x) => + x === null ? observableOf([]) : this.batchSer.autocomplete(this.data.date, x), + ), ); } ngOnInit() { this.form.setValue({ - batch: this.data.inventory.batch, + batch: this.data.inventory.batch.name, quantity: `${this.data.inventory.quantity}`, }); - this.batch = this.data.inventory.batch as Batch; + this.batch = this.data.inventory.batch; } - displayFn(batch?: Batch): string { - return batch ? batch.name : ''; + displayFn(batch?: Batch | string): string { + return !batch ? '' : typeof batch === 'string' ? batch : batch.name; } batchSelected(event: MatAutocompleteSelectedEvent): void { diff --git a/overlord/src/app/purchase-return/purchase-return.component.ts b/overlord/src/app/purchase-return/purchase-return.component.ts index 9b7e00ab..3c46d7aa 100644 --- a/overlord/src/app/purchase-return/purchase-return.component.ts +++ b/overlord/src/app/purchase-return/purchase-return.component.ts @@ -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'; @@ -42,10 +42,10 @@ export class PurchaseReturnComponent implements OnInit, AfterViewInit, OnDestroy dataSource: PurchaseReturnDataSource = new PurchaseReturnDataSource(this.inventoryObservable); form: FormGroup<{ date: FormControl; - account: FormControl; + account: FormControl; amount: FormControl; addRow: FormGroup<{ - batch: FormControl; + batch: FormControl; quantity: FormControl; }>; narration: FormControl; @@ -75,30 +75,28 @@ export class PurchaseReturnComponent implements OnInit, AfterViewInit, OnDestroy ) { this.form = new FormGroup({ date: new FormControl(new Date(), { nonNullable: true }), - account: new FormControl(null), + account: new FormControl(null), amount: new FormControl({ value: 0, disabled: true }, { nonNullable: true }), addRow: new FormGroup({ - batch: new FormControl(null), + batch: new FormControl(null), quantity: new FormControl('', { nonNullable: true }), }), narration: new FormControl('', { nonNullable: true }), }); // 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 Batch Autocomplete Change this.batches = this.form.controls.addRow.controls.batch.valueChanges.pipe( - map((x) => ((x as Batch).name !== undefined ? (x as Batch).name : (x as string))), - map((x) => (x !== null && x.length >= 1 ? x : '')), debounceTime(150), distinctUntilChanged(), switchMap((x) => - this.batchSer.autocomplete(moment(this.form.value.date).format('DD-MMM-YYYY'), x), + x === null + ? observableOf([]) + : this.batchSer.autocomplete(moment(this.form.value.date).format('DD-MMM-YYYY'), x), ), ); } @@ -161,7 +159,7 @@ export class PurchaseReturnComponent 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: { batch: '', @@ -333,8 +331,8 @@ export class PurchaseReturnComponent implements OnInit, AfterViewInit, OnDestroy }); } - displayFn(item?: Account | Batch): string { - return item ? item.name : ''; + displayFn(item?: Account | Batch | string): string { + return !item ? '' : typeof item === 'string' ? item : item.name; } accountSelected(event: MatAutocompleteSelectedEvent): void { diff --git a/overlord/src/app/purchase/purchase-dialog.component.ts b/overlord/src/app/purchase/purchase-dialog.component.ts index 6b79942e..ce199301 100644 --- a/overlord/src/app/purchase/purchase-dialog.component.ts +++ b/overlord/src/app/purchase/purchase-dialog.component.ts @@ -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; form: FormGroup<{ - product: FormControl; + product: FormControl; quantity: FormControl; price: FormControl; tax: FormControl; @@ -37,7 +37,7 @@ export class PurchaseDialogComponent implements OnInit { private productSer: ProductService, ) { this.form = new FormGroup({ - product: new FormControl(null), + product: new FormControl(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 { diff --git a/overlord/src/app/purchase/purchase.component.ts b/overlord/src/app/purchase/purchase.component.ts index 81853488..c3d17aab 100644 --- a/overlord/src/app/purchase/purchase.component.ts +++ b/overlord/src/app/purchase/purchase.component.ts @@ -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; - account: FormControl; + account: FormControl; amount: FormControl; addRow: FormGroup<{ product: FormControl; @@ -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(null), + account: new FormControl(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 { diff --git a/overlord/src/app/rate-contract/rate-contract-detail/rate-contract-detail.component.ts b/overlord/src/app/rate-contract/rate-contract-detail/rate-contract-detail.component.ts index 21dfda61..8ad7d138 100644 --- a/overlord/src/app/rate-contract/rate-contract-detail/rate-contract-detail.component.ts +++ b/overlord/src/app/rate-contract/rate-contract-detail/rate-contract-detail.component.ts @@ -5,7 +5,7 @@ import { MatDialog } from '@angular/material/dialog'; import { ActivatedRoute, Router } from '@angular/router'; 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 { Account } from '../../core/account'; import { AccountService } from '../../core/account.service'; @@ -33,7 +33,7 @@ export class RateContractDetailComponent implements OnInit, AfterViewInit { dataSource: RateContractDetailDatasource = new RateContractDetailDatasource(this.itemsObservable); form: FormGroup<{ date: FormControl; - account: FormControl; + account: FormControl; validFrom: FormControl; validTill: FormControl; addRow: FormGroup<{ @@ -45,6 +45,7 @@ export class RateContractDetailComponent implements OnInit, AfterViewInit { item: RateContract = new RateContract(); + account: Account | null = null; product: Product | null = null; displayedColumns = ['product', 'price', 'action']; @@ -64,7 +65,7 @@ export class RateContractDetailComponent implements OnInit, AfterViewInit { ) { this.form = new FormGroup({ date: new FormControl(new Date(), { nonNullable: true }), - account: new FormControl(null), + account: new FormControl(null), validFrom: new FormControl(new Date(), { nonNullable: true }), validTill: new FormControl(new Date(), { nonNullable: true }), addRow: new FormGroup({ @@ -74,15 +75,12 @@ export class RateContractDetailComponent implements OnInit, AfterViewInit { narration: new FormControl('', { nonNullable: true }), }); 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) => (x === null ? observableOf([]) : this.productSer.autocompleteSku(x, true))), @@ -102,7 +100,7 @@ export class RateContractDetailComponent implements OnInit, AfterViewInit { date: moment(this.item.date, 'DD-MMM-YYYY').toDate(), validFrom: moment(this.item.validFrom, 'DD-MMM-YYYY').toDate(), validTill: moment(this.item.validTill, 'DD-MMM-YYYY').toDate(), - account: this.item.vendor, + account: this.item.vendor.name, addRow: { product: '', price: '', @@ -155,8 +153,8 @@ export class RateContractDetailComponent implements OnInit, AfterViewInit { }, 0); } - displayFn(item?: Account | Product): string { - return item ? item.name : ''; + displayFn(item?: Account | Product | string): string { + return !item ? '' : typeof item === 'string' ? item : item.name; } updateView() { @@ -164,7 +162,7 @@ export class RateContractDetailComponent implements OnInit, AfterViewInit { } accountSelected(event: MatAutocompleteSelectedEvent): void { - this.form.controls.account.setValue(event.option.value); + this.account = event.option.value; } productSelected(event: MatAutocompleteSelectedEvent): void { @@ -218,9 +216,7 @@ export class RateContractDetailComponent implements OnInit, AfterViewInit { this.item.date = moment(formModel.date).format('DD-MMM-YYYY'); this.item.validFrom = moment(formModel.validFrom).format('DD-MMM-YYYY'); this.item.validTill = moment(formModel.validTill).format('DD-MMM-YYYY'); - if (formModel.account && typeof formModel.account !== 'string') { - this.item.vendor = formModel.account; - } + this.item.vendor = this.account ?? new Account(); this.item.narration = formModel.narration ?? ''; return this.item; } diff --git a/overlord/src/app/receipt/receipt-dialog.component.ts b/overlord/src/app/receipt/receipt-dialog.component.ts index 2423fc43..4947be1c 100644 --- a/overlord/src/app/receipt/receipt-dialog.component.ts +++ b/overlord/src/app/receipt/receipt-dialog.component.ts @@ -3,7 +3,7 @@ import { FormControl, FormGroup } from '@angular/forms'; import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete'; import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; import { Observable, of as observableOf } from 'rxjs'; -import { debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators'; +import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators'; import { Account } from '../core/account'; import { AccountBalance } from '../core/account-balance'; @@ -39,7 +39,6 @@ export class ReceiptDialogComponent implements OnInit { this.accBal = null; // Setup Account Autocomplete this.accounts = this.form.controls.account.valueChanges.pipe( - map((x) => (x !== null && x.length >= 1 ? x : null)), debounceTime(150), distinctUntilChanged(), switchMap((x) => (x === null ? observableOf([]) : this.accountSer.autocomplete(x))), @@ -54,8 +53,8 @@ export class ReceiptDialogComponent implements OnInit { this.account = this.data.journal.account; } - displayFn(account?: Account): string { - return account ? account.name : ''; + displayFn(account?: Account | string): string { + return !account ? '' : typeof account === 'string' ? account : account.name; } accountSelected(event: MatAutocompleteSelectedEvent): void { diff --git a/overlord/src/app/receipt/receipt.component.ts b/overlord/src/app/receipt/receipt.component.ts index 08b8494d..805d3845 100644 --- a/overlord/src/app/receipt/receipt.component.ts +++ b/overlord/src/app/receipt/receipt.component.ts @@ -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'; @@ -83,7 +83,6 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy { this.accBal = null; // Listen to Account Autocomplete Change this.accounts = this.form.controls.addRow.controls.account.valueChanges.pipe( - map((x) => (x !== null && x.length >= 1 ? x : null)), debounceTime(150), distinctUntilChanged(), switchMap((x) => (x === null ? observableOf([]) : this.accountSer.autocomplete(x))), @@ -333,8 +332,8 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy { }); } - displayFn(account?: Account): string { - return account ? account.name : ''; + displayFn(account?: Account | string): string { + return !account ? '' : typeof account === 'string' ? account : account.name; } accountSelected(event: MatAutocompleteSelectedEvent): void {