249 lines
7.6 KiB
TypeScript
249 lines
7.6 KiB
TypeScript
import { Component, OnInit } 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 * as moment from 'moment';
|
|
import { Observable, of as observableOf } from 'rxjs';
|
|
import { debounceTime, distinctUntilChanged, map, startWith, switchMap } from 'rxjs/operators';
|
|
|
|
import { environment } from '../../environments/environment';
|
|
import { AuthService } from '../auth/auth.service';
|
|
import { Product } from '../core/product';
|
|
import { ToasterService } from '../core/toaster.service';
|
|
import { ProductService } from '../product/product.service';
|
|
import { ConfirmDialogComponent } from '../shared/confirm-dialog/confirm-dialog.component';
|
|
|
|
import { SettingsService } from './settings.service';
|
|
|
|
@Component({
|
|
selector: 'app-settings',
|
|
templateUrl: './settings.component.html',
|
|
styleUrls: ['./settings.component.css'],
|
|
})
|
|
export class SettingsComponent implements OnInit {
|
|
lockDatesForm: FormGroup;
|
|
lockInformation: any;
|
|
|
|
rebaseDataForm: FormGroup;
|
|
|
|
resetStockForm: FormGroup;
|
|
product: Product;
|
|
products: Observable<Product[]>;
|
|
|
|
maintenance: any;
|
|
|
|
version: string;
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private fb: FormBuilder,
|
|
private dialog: MatDialog,
|
|
private toaster: ToasterService,
|
|
public auth: AuthService,
|
|
private ser: SettingsService,
|
|
private productSer: ProductService,
|
|
) {
|
|
this.createForm();
|
|
this.listenToLockForm();
|
|
this.lockInformation = {
|
|
lockOlder: false,
|
|
olderRolling: false,
|
|
lockNewer: false,
|
|
newerRolling: false,
|
|
};
|
|
this.listenToProductChanges();
|
|
|
|
this.maintenance = { enabled: false, user: '' };
|
|
this.version = environment.version;
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data.subscribe((data: { maintenance: any; lockInformation: any }) => {
|
|
this.maintenance = data.maintenance;
|
|
this.lockInformation = data.lockInformation;
|
|
this.lockDatesForm.patchValue({
|
|
lockOlder: this.lockInformation.lockOlder,
|
|
olderRolling: this.lockInformation.olderRolling,
|
|
olderDate: this.lockInformation.olderDate,
|
|
olderDays: this.lockInformation.olderDays,
|
|
lockNewer: this.lockInformation.lockNewer,
|
|
newerRolling: this.lockInformation.newerRolling,
|
|
newerDate: this.lockInformation.newerDate,
|
|
newerDays: this.lockInformation.newerDays,
|
|
});
|
|
});
|
|
}
|
|
|
|
createForm() {
|
|
const startDate = moment().date(1);
|
|
const finishDate = moment().date(startDate.daysInMonth());
|
|
this.lockDatesForm = this.fb.group({
|
|
lockOlder: null,
|
|
olderRolling: null,
|
|
olderDate: startDate,
|
|
olderDays: 0,
|
|
lockNewer: null,
|
|
newerRolling: null,
|
|
newerDate: finishDate,
|
|
newerDays: 0,
|
|
});
|
|
|
|
this.rebaseDataForm = this.fb.group({
|
|
date: moment(),
|
|
});
|
|
|
|
this.resetStockForm = this.fb.group({
|
|
resetDate: moment(),
|
|
stockDate: moment(),
|
|
product: null,
|
|
quantity: 0,
|
|
});
|
|
}
|
|
|
|
listenToLockForm() {
|
|
this.lockDatesForm.get('lockOlder').valueChanges.subscribe((x) => {
|
|
this.lockInformation.lockOlder = x;
|
|
});
|
|
this.lockDatesForm.get('lockNewer').valueChanges.subscribe((x) => {
|
|
this.lockInformation.lockNewer = x;
|
|
});
|
|
this.lockDatesForm.get('olderRolling').valueChanges.subscribe((x) => {
|
|
this.lockInformation.olderRolling = x;
|
|
});
|
|
this.lockDatesForm.get('newerRolling').valueChanges.subscribe((x) => {
|
|
this.lockInformation.newerRolling = x;
|
|
});
|
|
}
|
|
|
|
clearLockDates() {
|
|
this.ser.deleteLockInformation().subscribe((x) => {
|
|
this.lockInformation = x;
|
|
});
|
|
}
|
|
|
|
setLockDates() {
|
|
if (this.lockInformation.lockOlder && this.lockInformation.olderRolling) {
|
|
this.lockInformation.olderDays = +this.lockDatesForm.get('olderDays').value;
|
|
}
|
|
if (this.lockInformation.lockOlder && !this.lockInformation.olderRolling) {
|
|
this.lockInformation.olderDate = moment(this.lockDatesForm.get('olderDate').value).format(
|
|
'DD-MMM-YYYY',
|
|
);
|
|
}
|
|
if (this.lockInformation.lockNewer && this.lockInformation.newerRolling) {
|
|
this.lockInformation.newerDays = +this.lockDatesForm.get('newerDays').value;
|
|
}
|
|
if (this.lockInformation.lockOlder && !this.lockInformation.newerRolling) {
|
|
this.lockInformation.newerDate = moment(this.lockDatesForm.get('newerDate').value).format(
|
|
'DD-MMM-YYYY',
|
|
);
|
|
}
|
|
this.ser.setLockInformation(this.lockInformation).subscribe(
|
|
(result) => {
|
|
this.lockInformation = result;
|
|
this.toaster.show('Success', 'Lock information Updated');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Danger', error);
|
|
},
|
|
);
|
|
}
|
|
|
|
confirmRebase(): void {
|
|
const rebaseDate = moment(this.rebaseDataForm.get('date').value).format('DD-MMM-YYYY');
|
|
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
|
|
width: '250px',
|
|
data: {
|
|
title: 'Rebase the database?',
|
|
content: `Are you sure you want to rebase the database as on ${rebaseDate}? This action is destructive and cannot be undone.`,
|
|
},
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe((result: boolean) => {
|
|
if (result) {
|
|
this.rebaseData(rebaseDate);
|
|
}
|
|
});
|
|
}
|
|
|
|
rebaseData(rebaseDate: string) {
|
|
this.ser.rebaseDatabase(rebaseDate).subscribe(
|
|
(result) => {
|
|
this.lockInformation = result;
|
|
this.toaster.show('Success', 'Data has been rebased!');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Danger', error);
|
|
},
|
|
);
|
|
}
|
|
|
|
listenToProductChanges() {
|
|
this.products = this.resetStockForm.get('product').valueChanges.pipe(
|
|
startWith(''),
|
|
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;
|
|
}
|
|
|
|
selectedProduct(event: MatAutocompleteSelectedEvent): void {
|
|
this.product = event.option.value;
|
|
}
|
|
|
|
confirmResetStock(): void {
|
|
const resetDate = moment(this.resetStockForm.get('resetDate').value).format('DD-MMM-YYYY');
|
|
const stockDate = moment(this.resetStockForm.get('stockDate').value).format('DD-MMM-YYYY');
|
|
const quantity = +this.resetStockForm.get('quantity').value;
|
|
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
|
|
width: '250px',
|
|
data: {
|
|
title: 'Reset the stock of?',
|
|
content: `Are you sure you want to reset the stock of ${this.product.name}? This action is destructive and cannot be undone.`,
|
|
},
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe((result: boolean) => {
|
|
if (result) {
|
|
this.resetStock(resetDate, stockDate, this.product, quantity);
|
|
}
|
|
});
|
|
}
|
|
|
|
resetStock(resetDate: string, stockDate: string, product: Product, quantity: number) {
|
|
this.ser.resetStock(resetDate, stockDate, product, quantity).subscribe(
|
|
() => {
|
|
this.toaster.show('Success', 'Stock has been reset!');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Danger', error);
|
|
},
|
|
);
|
|
}
|
|
|
|
checkIntegrity() {
|
|
this.ser.checkDatabaseIntegrity().subscribe(
|
|
(result) => {
|
|
this.lockInformation = result;
|
|
this.toaster.show('Success', 'Database checked, it is fine!');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Danger', error);
|
|
},
|
|
);
|
|
}
|
|
|
|
toggleMaintenance() {
|
|
this.ser.setMaintenance(!this.maintenance.enabled).subscribe((x) => {
|
|
this.maintenance = x;
|
|
});
|
|
}
|
|
}
|