Moved from tslint to eslint as tslint was depreciated.
Added prettier and also prettied all the typescript files using prettier ESLint is using the AirBnB rules which are the most strict to lint the files.
This commit is contained in:
@ -1,29 +1,29 @@
|
||||
import {AfterViewInit, Component, ElementRef, OnInit, OnDestroy, ViewChild} from '@angular/core';
|
||||
import {FormBuilder, FormGroup} from '@angular/forms';
|
||||
import { AfterViewInit, Component, ElementRef, OnInit, OnDestroy, ViewChild } from '@angular/core';
|
||||
import { FormBuilder, FormGroup } from '@angular/forms';
|
||||
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
|
||||
import { MatDialog } from '@angular/material/dialog';
|
||||
import {ActivatedRoute, Router} from '@angular/router';
|
||||
import {BehaviorSubject, fromEvent, Observable, of, of as observableOf, zip} from 'rxjs';
|
||||
import {PurchaseDataSource} from './purchase-datasource';
|
||||
import {Account} from '../core/account';
|
||||
import {VoucherService} from '../core/voucher.service';
|
||||
import {AccountService} from '../core/account.service';
|
||||
import {DbFile, Inventory, Voucher} from '../core/voucher';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { BehaviorSubject, fromEvent, Observable, of, of as observableOf, zip } from 'rxjs';
|
||||
import { PurchaseDataSource } from './purchase-datasource';
|
||||
import { Account } from '../core/account';
|
||||
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 {AuthService} from '../auth/auth.service';
|
||||
import {ConfirmDialogComponent} from '../shared/confirm-dialog/confirm-dialog.component';
|
||||
import {ToasterService} from '../core/toaster.service';
|
||||
import {debounceTime, distinctUntilChanged, map, startWith, switchMap} from 'rxjs/operators';
|
||||
import {PurchaseDialogComponent} from './purchase-dialog.component';
|
||||
import {ImageDialogComponent} from '../shared/image-dialog/image-dialog.component';
|
||||
import {ProductService} from '../product/product.service';
|
||||
import {Product} from '../core/product';
|
||||
import {Hotkey, HotkeysService} from 'angular2-hotkeys';
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
import { ConfirmDialogComponent } from '../shared/confirm-dialog/confirm-dialog.component';
|
||||
import { ToasterService } from '../core/toaster.service';
|
||||
import { debounceTime, distinctUntilChanged, map, startWith, switchMap } from 'rxjs/operators';
|
||||
import { PurchaseDialogComponent } from './purchase-dialog.component';
|
||||
import { ImageDialogComponent } from '../shared/image-dialog/image-dialog.component';
|
||||
import { ProductService } from '../product/product.service';
|
||||
import { Product } from '../core/product';
|
||||
import { Hotkey, HotkeysService } from 'angular2-hotkeys';
|
||||
|
||||
@Component({
|
||||
selector: 'app-purchase',
|
||||
templateUrl: './purchase.component.html',
|
||||
styleUrls: ['./purchase.component.css']
|
||||
styleUrls: ['./purchase.component.css'],
|
||||
})
|
||||
export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
@ViewChild('accountElement', { static: true }) accountElement: ElementRef;
|
||||
@ -51,7 +51,7 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
private auth: AuthService,
|
||||
private ser: VoucherService,
|
||||
private productSer: ProductService,
|
||||
private accountSer: AccountService
|
||||
private accountSer: AccountService,
|
||||
) {
|
||||
this.createForm();
|
||||
this.listenToAccountAutocompleteChange();
|
||||
@ -59,28 +59,49 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.route.data
|
||||
.subscribe((data: { voucher: Voucher }) => {
|
||||
this.loadVoucher(data.voucher);
|
||||
});
|
||||
this.hotkeys.add(new Hotkey('f2', (event: KeyboardEvent): boolean => {
|
||||
setTimeout(() => {
|
||||
this.dateElement.nativeElement.focus();
|
||||
}, 0);
|
||||
return false; // Prevent bubbling
|
||||
}, ['INPUT', 'SELECT', 'TEXTAREA']));
|
||||
this.hotkeys.add(new Hotkey('ctrl+s', (event: KeyboardEvent): boolean => {
|
||||
if (this.canSave()) {
|
||||
this.save();
|
||||
}
|
||||
return false; // Prevent bubbling
|
||||
}, ['INPUT', 'SELECT', 'TEXTAREA']));
|
||||
this.hotkeys.add(new Hotkey('ctrl+p', (event: KeyboardEvent): boolean => {
|
||||
if (this.voucher.id && !this.voucher.posted && this.auth.user.perms.indexOf('post-vouchers') !== -1) {
|
||||
this.post();
|
||||
}
|
||||
return false; // Prevent bubbling
|
||||
}, ['INPUT', 'SELECT', 'TEXTAREA']));
|
||||
this.route.data.subscribe((data: { voucher: Voucher }) => {
|
||||
this.loadVoucher(data.voucher);
|
||||
});
|
||||
this.hotkeys.add(
|
||||
new Hotkey(
|
||||
'f2',
|
||||
(event: KeyboardEvent): boolean => {
|
||||
setTimeout(() => {
|
||||
this.dateElement.nativeElement.focus();
|
||||
}, 0);
|
||||
return false; // Prevent bubbling
|
||||
},
|
||||
['INPUT', 'SELECT', 'TEXTAREA'],
|
||||
),
|
||||
);
|
||||
this.hotkeys.add(
|
||||
new Hotkey(
|
||||
'ctrl+s',
|
||||
(event: KeyboardEvent): boolean => {
|
||||
if (this.canSave()) {
|
||||
this.save();
|
||||
}
|
||||
return false; // Prevent bubbling
|
||||
},
|
||||
['INPUT', 'SELECT', 'TEXTAREA'],
|
||||
),
|
||||
);
|
||||
this.hotkeys.add(
|
||||
new Hotkey(
|
||||
'ctrl+p',
|
||||
(event: KeyboardEvent): boolean => {
|
||||
if (
|
||||
this.voucher.id &&
|
||||
!this.voucher.posted &&
|
||||
this.auth.user.perms.indexOf('post-vouchers') !== -1
|
||||
) {
|
||||
this.post();
|
||||
}
|
||||
return false; // Prevent bubbling
|
||||
},
|
||||
['INPUT', 'SELECT', 'TEXTAREA'],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
ngAfterViewInit() {
|
||||
@ -102,9 +123,9 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
quantity: '',
|
||||
price: '',
|
||||
tax: '',
|
||||
discount: ''
|
||||
discount: '',
|
||||
},
|
||||
narration: this.voucher.narration
|
||||
narration: this.voucher.narration,
|
||||
});
|
||||
this.dataSource = new PurchaseDataSource(this.inventoryObservable);
|
||||
this.updateView();
|
||||
@ -118,10 +139,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 = +formValue.quantity;
|
||||
const price = +formValue.price;
|
||||
const tax = +formValue.tax;
|
||||
const discount = +formValue.discount;
|
||||
if (this.product === null || quantity <= 0 || price <= 0) {
|
||||
return;
|
||||
}
|
||||
@ -138,7 +159,7 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
discount: discount,
|
||||
amount: quantity * price * (1 + tax) * (1 - discount),
|
||||
product: this.product,
|
||||
batch: null
|
||||
batch: null,
|
||||
});
|
||||
this.resetAddRow();
|
||||
this.updateView();
|
||||
@ -150,7 +171,7 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
quantity: '',
|
||||
price: '',
|
||||
tax: '',
|
||||
discount: ''
|
||||
discount: '',
|
||||
});
|
||||
this.product = null;
|
||||
setTimeout(() => {
|
||||
@ -160,13 +181,15 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
|
||||
updateView() {
|
||||
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)));
|
||||
this.form
|
||||
.get('amount')
|
||||
.setValue(Math.abs(this.voucher.inventories.map((x) => x.amount).reduce((p, c) => p + c, 0)));
|
||||
}
|
||||
|
||||
editRow(row: Inventory) {
|
||||
const dialogRef = this.dialog.open(PurchaseDialogComponent, {
|
||||
width: '750px',
|
||||
data: {inventory: Object.assign({}, row)}
|
||||
data: { inventory: Object.assign({}, row) },
|
||||
});
|
||||
|
||||
dialogRef.afterClosed().subscribe((result: boolean | Inventory) => {
|
||||
@ -174,7 +197,10 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
return;
|
||||
}
|
||||
const j = result as Inventory;
|
||||
if (j.product.id !== row.product.id && this.voucher.inventories.filter((x) => x.product.id === j.product.id).length) {
|
||||
if (
|
||||
j.product.id !== row.product.id &&
|
||||
this.voucher.inventories.filter((x) => x.product.id === j.product.id).length
|
||||
) {
|
||||
return;
|
||||
}
|
||||
Object.assign(row, j);
|
||||
@ -191,15 +217,15 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
this.form = this.fb.group({
|
||||
date: '',
|
||||
account: '',
|
||||
amount: {value: '', disabled: true},
|
||||
amount: { value: '', disabled: true },
|
||||
addRow: this.fb.group({
|
||||
product: '',
|
||||
quantity: '',
|
||||
price: '',
|
||||
tax: '',
|
||||
discount: ''
|
||||
discount: '',
|
||||
}),
|
||||
narration: ''
|
||||
narration: '',
|
||||
});
|
||||
this.accBal = null;
|
||||
}
|
||||
@ -210,39 +236,40 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
} else if (this.voucher.posted && this.auth.user.perms.indexOf('edit-posted-vouchers') !== -1) {
|
||||
return true;
|
||||
} else {
|
||||
return this.voucher.user.id === this.auth.user.id || this.auth.user.perms.indexOf("edit-other-user's-vouchers") !== -1;
|
||||
return (
|
||||
this.voucher.user.id === this.auth.user.id ||
|
||||
this.auth.user.perms.indexOf("edit-other-user's-vouchers") !== -1
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
post() {
|
||||
this.ser.post(this.voucher.id)
|
||||
.subscribe(
|
||||
(result) => {
|
||||
this.loadVoucher(result);
|
||||
this.toaster.show('Success', 'Voucher Posted');
|
||||
},
|
||||
(error) => {
|
||||
this.toaster.show('Danger', error);
|
||||
}
|
||||
);
|
||||
this.ser.post(this.voucher.id).subscribe(
|
||||
(result) => {
|
||||
this.loadVoucher(result);
|
||||
this.toaster.show('Success', 'Voucher Posted');
|
||||
},
|
||||
(error) => {
|
||||
this.toaster.show('Danger', error);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
save() {
|
||||
const voucher: Voucher = this.getVoucher();
|
||||
this.ser.saveOrUpdate(voucher)
|
||||
.subscribe(
|
||||
(result) => {
|
||||
this.toaster.show('Success', '');
|
||||
if (voucher.id === result.id) {
|
||||
this.loadVoucher(result);
|
||||
} else {
|
||||
this.ser.saveOrUpdate(voucher).subscribe(
|
||||
(result) => {
|
||||
this.toaster.show('Success', '');
|
||||
if (voucher.id === result.id) {
|
||||
this.loadVoucher(result);
|
||||
} else {
|
||||
this.router.navigate(['/purchase', result.id]);
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
this.toaster.show('Danger', error);
|
||||
}
|
||||
);
|
||||
},
|
||||
(error) => {
|
||||
this.toaster.show('Danger', error);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
getVoucher(): Voucher {
|
||||
@ -254,22 +281,21 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
}
|
||||
|
||||
delete() {
|
||||
this.ser.delete(this.voucher.id)
|
||||
.subscribe(
|
||||
(result) => {
|
||||
this.toaster.show('Success', '');
|
||||
this.router.navigate(['/purchase'], {replaceUrl: true});
|
||||
},
|
||||
(error) => {
|
||||
this.toaster.show('Danger', error);
|
||||
}
|
||||
);
|
||||
this.ser.delete(this.voucher.id).subscribe(
|
||||
(result) => {
|
||||
this.toaster.show('Success', '');
|
||||
this.router.navigate(['/purchase'], { replaceUrl: true });
|
||||
},
|
||||
(error) => {
|
||||
this.toaster.show('Danger', error);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
confirmDelete(): void {
|
||||
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
|
||||
width: '250px',
|
||||
data: {title: 'Delete Voucher?', content: 'Are you sure? This cannot be undone.'}
|
||||
data: { title: 'Delete Voucher?', content: 'Are you sure? This cannot be undone.' },
|
||||
});
|
||||
|
||||
dialogRef.afterClosed().subscribe((result: boolean) => {
|
||||
@ -281,26 +307,24 @@ 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))
|
||||
);
|
||||
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))
|
||||
);
|
||||
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))),
|
||||
);
|
||||
}
|
||||
|
||||
displayAccount(account?: Account): string | undefined {
|
||||
@ -323,7 +347,7 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
zoomImage(file: DbFile) {
|
||||
this.dialog.open(ImageDialogComponent, {
|
||||
width: '750px',
|
||||
data: file.resized
|
||||
data: file.resized,
|
||||
});
|
||||
}
|
||||
|
||||
@ -338,11 +362,12 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
for (const file of files) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = (e: any) => {
|
||||
zip(of(e.target.result),
|
||||
zip(
|
||||
of(e.target.result),
|
||||
this.resizeImage(e.target.result, 100, 150),
|
||||
this.resizeImage(e.target.result, 825, 1170)
|
||||
).subscribe(
|
||||
val => this.voucher.files.push({id: null, thumbnail: val[1], resized: val[2]})
|
||||
this.resizeImage(e.target.result, 825, 1170),
|
||||
).subscribe((val) =>
|
||||
this.voucher.files.push({ id: null, thumbnail: val[1], resized: val[2] }),
|
||||
);
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
@ -371,7 +396,7 @@ export class PurchaseComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
canvas.height = height;
|
||||
ctx.drawImage(img, 0, 0, width, height);
|
||||
return canvas.toDataURL('image/jpeg', 0.95);
|
||||
})
|
||||
}),
|
||||
);
|
||||
img.src = image;
|
||||
return ex;
|
||||
|
||||
Reference in New Issue
Block a user