Strict done!!
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import { Component, Inject, OnInit } from '@angular/core';
|
||||
import { FormBuilder, FormGroup } from '@angular/forms';
|
||||
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
|
||||
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
|
||||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
|
||||
import { round } from 'mathjs';
|
||||
@ -18,7 +18,7 @@ import { MathService } from '../shared/math.service';
|
||||
export class PurchaseDialogComponent implements OnInit {
|
||||
products: Observable<Product[]>;
|
||||
form: FormGroup;
|
||||
product: Product;
|
||||
product: Product = new Product();
|
||||
|
||||
constructor(
|
||||
public dialogRef: MatDialogRef<PurchaseDialogComponent>,
|
||||
@ -34,7 +34,14 @@ export class PurchaseDialogComponent implements OnInit {
|
||||
tax: '',
|
||||
discount: '',
|
||||
});
|
||||
this.listenToProductAutocompleteChange();
|
||||
// Listen to Product Autocomplete Change
|
||||
this.products = (this.form.get('product') as FormControl).valueChanges.pipe(
|
||||
startWith(null),
|
||||
map((x) => (x !== null && x.length >= 1 ? x : null)),
|
||||
debounceTime(150),
|
||||
distinctUntilChanged(),
|
||||
switchMap((x) => (x === null ? observableOf([]) : this.productSer.autocomplete(x))),
|
||||
);
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
@ -48,24 +55,13 @@ export class PurchaseDialogComponent implements OnInit {
|
||||
this.product = this.data.inventory.product;
|
||||
}
|
||||
|
||||
listenToProductAutocompleteChange(): void {
|
||||
const control = this.form.get('product');
|
||||
this.products = control.valueChanges.pipe(
|
||||
startWith(null),
|
||||
map((x) => (x !== null && x.length >= 1 ? x : null)),
|
||||
debounceTime(150),
|
||||
distinctUntilChanged(),
|
||||
switchMap((x) => (x === null ? observableOf([]) : this.productSer.autocomplete(x))),
|
||||
);
|
||||
}
|
||||
|
||||
displayFn(product?: Product): string | undefined {
|
||||
return product ? product.name : undefined;
|
||||
displayFn(product?: Product): string {
|
||||
return product ? product.name : '';
|
||||
}
|
||||
|
||||
productSelected(event: MatAutocompleteSelectedEvent): void {
|
||||
this.product = event.option.value;
|
||||
this.form.get('price').setValue(this.product.price);
|
||||
(this.form.get('price') as FormControl).setValue(this.product.price);
|
||||
}
|
||||
|
||||
accept(): void {
|
||||
|
||||
@ -231,7 +231,7 @@
|
||||
mat-raised-button
|
||||
(click)="post()"
|
||||
*ngIf="voucher.id"
|
||||
[disabled]="voucher.posted || auth.user.perms.indexOf('post-vouchers') === -1"
|
||||
[disabled]="voucher.posted || auth.allowed('post-vouchers')"
|
||||
>
|
||||
{{ voucher.posted ? 'Posted' : 'Post' }}
|
||||
</button>
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { AfterViewInit, Component, ElementRef, OnInit, OnDestroy, ViewChild } from '@angular/core';
|
||||
import { FormBuilder, FormGroup } from '@angular/forms';
|
||||
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
|
||||
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
|
||||
import { MatDialog } from '@angular/material/dialog';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
@ -16,6 +16,7 @@ import { DbFile } from '../core/db-file';
|
||||
import { Inventory } from '../core/inventory';
|
||||
import { Product } from '../core/product';
|
||||
import { ToasterService } from '../core/toaster.service';
|
||||
import { User } from '../core/user';
|
||||
import { Voucher } from '../core/voucher';
|
||||
import { VoucherService } from '../core/voucher.service';
|
||||
import { ProductService } from '../product/product.service';
|
||||
@ -37,10 +38,10 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
@ViewChild('productElement', { static: true }) productElement?: ElementRef;
|
||||
@ViewChild('dateElement', { static: true }) dateElement?: ElementRef;
|
||||
public inventoryObservable = new BehaviorSubject<Inventory[]>([]);
|
||||
dataSource: PurchaseDataSource;
|
||||
dataSource: PurchaseDataSource = new PurchaseDataSource(this.inventoryObservable);
|
||||
form: FormGroup;
|
||||
voucher: Voucher;
|
||||
product: Product;
|
||||
voucher: Voucher = new Voucher();
|
||||
product: Product | null = null;
|
||||
accBal: any;
|
||||
|
||||
displayedColumns = ['product', 'quantity', 'rate', 'tax', 'discount', 'amount', 'action'];
|
||||
@ -76,8 +77,24 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
narration: '',
|
||||
});
|
||||
this.accBal = null;
|
||||
this.listenToAccountAutocompleteChange();
|
||||
this.listenToProductAutocompleteChange();
|
||||
// Listen to Account Autocomplete Change
|
||||
this.accounts = (this.form.get('account') as FormControl).valueChanges.pipe(
|
||||
startWith(null),
|
||||
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.get('addRow') as FormControl).get(
|
||||
'product',
|
||||
) as FormControl).valueChanges.pipe(
|
||||
startWith(null),
|
||||
map((x) => (x !== null && x.length >= 1 ? x : null)),
|
||||
debounceTime(150),
|
||||
distinctUntilChanged(),
|
||||
switchMap((x) => (x === null ? observableOf([]) : this.productSer.autocomplete(x))),
|
||||
);
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
@ -91,7 +108,7 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
'f2',
|
||||
(): boolean => {
|
||||
setTimeout(() => {
|
||||
this.dateElement.nativeElement.focus();
|
||||
if (this.dateElement) this.dateElement.nativeElement.focus();
|
||||
}, 0);
|
||||
return false; // Prevent bubbling
|
||||
},
|
||||
@ -114,11 +131,7 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
new Hotkey(
|
||||
'ctrl+p',
|
||||
(): boolean => {
|
||||
if (
|
||||
this.voucher.id &&
|
||||
!this.voucher.posted &&
|
||||
this.auth.user.perms.indexOf('post-vouchers') !== -1
|
||||
) {
|
||||
if (this.voucher.id && !this.voucher.posted && this.auth.allowed('post-vouchers')) {
|
||||
this.post();
|
||||
}
|
||||
return false; // Prevent bubbling
|
||||
@ -136,7 +149,7 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
this.hotkeys.reset();
|
||||
}
|
||||
|
||||
loadVoucher(voucher) {
|
||||
loadVoucher(voucher: Voucher) {
|
||||
this.voucher = voucher;
|
||||
this.form.setValue({
|
||||
date: moment(this.voucher.date, 'DD-MMM-YYYY').toDate(),
|
||||
@ -157,12 +170,12 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
|
||||
focusAccount() {
|
||||
setTimeout(() => {
|
||||
this.accountElement.nativeElement.focus();
|
||||
if (this.accountElement) this.accountElement.nativeElement.focus();
|
||||
}, 0);
|
||||
}
|
||||
|
||||
addRow() {
|
||||
const formValue = this.form.get('addRow').value;
|
||||
const formValue = (this.form.get('addRow') as FormControl).value;
|
||||
const quantity = this.math.parseAmount(formValue.quantity, 2);
|
||||
const price = this.math.parseAmount(formValue.price, 2);
|
||||
const tax = this.math.parseAmount(formValue.tax, 5);
|
||||
@ -170,27 +183,30 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
if (this.product === null || quantity <= 0 || price <= 0) {
|
||||
return;
|
||||
}
|
||||
const oldFiltered = this.voucher.inventories.filter((x) => x.product.id === this.product.id);
|
||||
const oldFiltered = this.voucher.inventories.filter(
|
||||
(x) => x.product.id === (this.product as Product).id,
|
||||
);
|
||||
if (oldFiltered.length) {
|
||||
this.toaster.show('Danger', 'Product already added');
|
||||
return;
|
||||
}
|
||||
this.voucher.inventories.push({
|
||||
id: null,
|
||||
quantity,
|
||||
rate: price,
|
||||
tax,
|
||||
discount,
|
||||
amount: round(quantity * price * (1 + tax) * (1 - discount), 2),
|
||||
product: this.product,
|
||||
batch: null,
|
||||
});
|
||||
this.voucher.inventories.push(
|
||||
new Inventory({
|
||||
quantity,
|
||||
rate: price,
|
||||
tax,
|
||||
discount,
|
||||
amount: round(quantity * price * (1 + tax) * (1 - discount), 2),
|
||||
product: this.product,
|
||||
batch: null,
|
||||
}),
|
||||
);
|
||||
this.resetAddRow();
|
||||
this.updateView();
|
||||
}
|
||||
|
||||
resetAddRow() {
|
||||
this.form.get('addRow').reset({
|
||||
(this.form.get('addRow') as FormControl).reset({
|
||||
product: null,
|
||||
quantity: '',
|
||||
price: '',
|
||||
@ -199,18 +215,15 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
});
|
||||
this.product = null;
|
||||
setTimeout(() => {
|
||||
this.productElement.nativeElement.focus();
|
||||
if (this.productElement) this.productElement.nativeElement.focus();
|
||||
}, 0);
|
||||
}
|
||||
|
||||
updateView() {
|
||||
this.inventoryObservable.next(this.voucher.inventories);
|
||||
this.form
|
||||
.get('amount')
|
||||
.setValue(
|
||||
round(Math.abs(this.voucher.inventories.map((x) => x.amount).reduce((p, c) => p + c, 0))),
|
||||
2,
|
||||
);
|
||||
(this.form.get('amount') as FormControl).setValue(
|
||||
round(Math.abs(this.voucher.inventories.map((x) => x.amount).reduce((p, c) => p + c, 0)), 2),
|
||||
);
|
||||
}
|
||||
|
||||
editRow(row: Inventory) {
|
||||
@ -244,17 +257,17 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
if (!this.voucher.id) {
|
||||
return true;
|
||||
}
|
||||
if (this.voucher.posted && this.auth.user.perms.indexOf('edit-posted-vouchers') !== -1) {
|
||||
if (this.voucher.posted && this.auth.allowed('edit-posted-vouchers')) {
|
||||
return true;
|
||||
}
|
||||
return (
|
||||
this.voucher.user.id === this.auth.user.id ||
|
||||
this.auth.user.perms.indexOf("edit-other-user's-vouchers") !== -1
|
||||
this.voucher.user.id === (this.auth.user as User).id ||
|
||||
this.auth.allowed("edit-other-user's-vouchers")
|
||||
);
|
||||
}
|
||||
|
||||
post() {
|
||||
this.ser.post(this.voucher.id).subscribe(
|
||||
this.ser.post(this.voucher.id as string).subscribe(
|
||||
(result) => {
|
||||
this.loadVoucher(result);
|
||||
this.toaster.show('Success', 'Voucher Posted');
|
||||
@ -291,7 +304,7 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
}
|
||||
|
||||
delete() {
|
||||
this.ser.delete(this.voucher.id).subscribe(
|
||||
this.ser.delete(this.voucher.id as string).subscribe(
|
||||
() => {
|
||||
this.toaster.show('Success', '');
|
||||
this.router.navigate(['/purchase'], { replaceUrl: true });
|
||||
@ -315,39 +328,18 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
});
|
||||
}
|
||||
|
||||
listenToAccountAutocompleteChange(): void {
|
||||
const control = this.form.get('account');
|
||||
this.accounts = control.valueChanges.pipe(
|
||||
startWith(null),
|
||||
map((x) => (x !== null && x.length >= 1 ? x : null)),
|
||||
debounceTime(150),
|
||||
distinctUntilChanged(),
|
||||
switchMap((x) => (x === null ? observableOf([]) : this.accountSer.autocomplete(x))),
|
||||
);
|
||||
}
|
||||
|
||||
listenToProductAutocompleteChange(): void {
|
||||
const control = this.form.get('addRow').get('product');
|
||||
this.products = control.valueChanges.pipe(
|
||||
startWith(null),
|
||||
map((x) => (x !== null && x.length >= 1 ? x : null)),
|
||||
debounceTime(150),
|
||||
distinctUntilChanged(),
|
||||
switchMap((x) => (x === null ? observableOf([]) : this.productSer.autocomplete(x))),
|
||||
);
|
||||
}
|
||||
|
||||
displayFn(item?: Account | Product): string | undefined {
|
||||
return item ? item.name : undefined;
|
||||
displayFn(item?: Account | Product): string {
|
||||
return item ? item.name : '';
|
||||
}
|
||||
|
||||
accountSelected(event: MatAutocompleteSelectedEvent): void {
|
||||
this.form.get('account').setValue(event.option.value);
|
||||
(this.form.get('account') as FormControl).setValue(event.option.value);
|
||||
}
|
||||
|
||||
productSelected(event: MatAutocompleteSelectedEvent): void {
|
||||
this.product = event.option.value;
|
||||
this.form.get('addRow').get('price').setValue(this.product.price);
|
||||
const product = event.option.value;
|
||||
this.product = product;
|
||||
((this.form.get('addRow') as FormControl).get('price') as FormControl).setValue(product.price);
|
||||
}
|
||||
|
||||
zoomImage(file: DbFile) {
|
||||
|
||||
Reference in New Issue
Block a user