import { Component, OnInit, ViewChild } from '@angular/core'; import { NotificationService } from '../../../_services/notification.service'; import { AuthenticationService } from '../../../_services/authentication.service'; import { ChequeService } from '../cheque-services'; import { BankChequeViewModel, BankChequeTransactionViewModel } from '../cheque-class'; import { StoreService } from '../../store/store.services'; import { debounceTime } from 'rxjs/operators'; import { Subject, Subscription } from 'rxjs'; import { ItemViewModel } from '../../store/store-model'; import { LocalService } from '../../../_services/local.service'; import { ActivatedRoute } from '@angular/router'; import { FormCanDeactivate } from '../../../_guards/form-can-deactivate'; import { NgForm } from '@angular/forms'; @Component({ selector: 'app-manage-cheque', templateUrl: './manage-cheque.component.html' }) export class ManageChequeComponent extends FormCanDeactivate implements OnInit { @ViewChild('issueChequeForm') @ViewChild('recieveChequeForm') form: NgForm; public openDilog = false; public saving = false; public saving2 = false; public _bankViewModel: BankChequeViewModel; public _issuechequeTransaction: BankChequeTransactionViewModel; public _issueChequeBankList: any = []; public _issueChequeStatusList: any = []; public _recieveChequeTransaction: BankChequeTransactionViewModel; public _recieveChequeBankList: any = []; public _recieveChequeStatusList: any = []; public searchEvent = new Subject(); public searchSubcription: Subscription = null; public paymentAccountList: any = []; public _fileToUpload: File; public _startingChequeNumber = 0; public _alreadyUsedItems = []; public _isChequeValid = true; constructor(public _notificationService: NotificationService, public _authenticationService: AuthenticationService, public _chequeService: ChequeService, public _storeService: StoreService, private activatedRoute: ActivatedRoute, public _localService: LocalService) { super(); } ngOnInit() { this._bankViewModel = new BankChequeViewModel(); this._issuechequeTransaction = new BankChequeTransactionViewModel(); this._recieveChequeTransaction = new BankChequeTransactionViewModel(); this.getAccount(' ', 'CSE'); this.getChequeStatusesByChequeType(1); this.getChequeStatusesByChequeType(2); this.activatedRoute.queryParams.subscribe(params => { const accId = params['id']; this._issuechequeTransaction.IssueDate = new Date(); this._issuechequeTransaction.Fk_Status = new ItemViewModel(-1, 'None'); this._recieveChequeTransaction.IssueDate = new Date(); this._recieveChequeTransaction.Fk_Status = new ItemViewModel(-1, 'None'); if (accId != null) { this._chequeService.getChequeTransactionById(accId).subscribe((res: any) => { console.log(res); if (res.FK_ChequeType.Name == "Issued") { this._issuechequeTransaction = res; this._issuechequeTransaction.IssueDate = new Date(res.IssueDate); this._issuechequeTransaction.Fk_Status = new ItemViewModel(res.Fk_Status.Id, res.Fk_Status.Name); this._issuechequeTransaction.FK_ChequeType = new ItemViewModel(res.FK_ChequeType.Id, res.FK_ChequeType.Name); this._issuechequeTransaction.Fk_PaymentAccount = new ItemViewModel(res.Fk_PaymentAccount.Id, res.Fk_PaymentAccount.Name); this._issuechequeTransaction.Fk_Status = new ItemViewModel(res.Fk_Status.Id, res.Fk_Status.Name); } if (res.FK_ChequeType.Name == "Recieved") { this._recieveChequeTransaction = res; this._recieveChequeTransaction.IssueDate = new Date(res.IssueDate); this._recieveChequeTransaction.Fk_Status = new ItemViewModel(res.Fk_Status.Id, res.Fk_Status.Name); this._recieveChequeTransaction.FK_ChequeType = new ItemViewModel(res.FK_ChequeType.Id, res.FK_ChequeType.Name); this._recieveChequeTransaction.Fk_PaymentAccount = new ItemViewModel(res.Fk_PaymentAccount.Id, res.Fk_PaymentAccount.Name); this._recieveChequeTransaction.Fk_Status = new ItemViewModel(res.Fk_Status.Id, res.Fk_Status.Name); } }, (err) => { }); } }); this.getBankListForIssueCheque(); this.handleSearchEvents(); } onSubmitBank() { this._chequeService.saveBankCheque(this._bankViewModel).subscribe((res) => { this._notificationService.notify('success', 'Bank Cheque Detail Saved Successfully.'); this._bankViewModel = new BankChequeViewModel(); this.getBankListForIssueCheque(); this.close(''); }, (err) => { this._notificationService.notify('danger', err.msg); }); } onSubmitIsseCheque() { this.saving = true; this._issuechequeTransaction.FK_ChequeType = new ItemViewModel(1); this._issuechequeTransaction.BankName = this._issuechequeTransaction.Fk_BankAccount.Name; this._chequeService.saveBankChequeTransaction(this._issuechequeTransaction).subscribe((res) => { this._issuechequeTransaction = new BankChequeTransactionViewModel(); this._issuechequeTransaction.Fk_Status = new ItemViewModel(-1, 'None'); this._issuechequeTransaction.IssueDate = new Date(); this.saving = false; this._notificationService.notify('success', 'Bank Cheque Issued successfully!'); }, (err) => { this._notificationService.notify('danger', err.msg); }); } onSubmiRecieveCheque() { this.saving2 = true; this._recieveChequeTransaction.FK_ChequeType = new ItemViewModel(2); this._recieveChequeTransaction.BankName = this._recieveChequeTransaction.Fk_BankAccount.Name; this._chequeService.saveBankChequeTransaction(this._recieveChequeTransaction).subscribe((res) => { this._recieveChequeTransaction = new BankChequeTransactionViewModel(); this._recieveChequeTransaction.Fk_Status = new ItemViewModel(-1, 'None'); this._recieveChequeTransaction.IssueDate = new Date(); this.saving2 = false; this._notificationService.notify('success', 'Bank Cheque Received successfully!'); }, (err) => { this._notificationService.notify('danger', err.msg); }); } getBankListForIssueCheque() { this._chequeService.getBankList().subscribe((res: any) => { if (res) { console.log(res); this._issueChequeBankList = res; this.openDilog = false; } }, (err) => { console.log(err); }); } getChequeStatusesByChequeType(id: number) { this._chequeService.getChequeStatusesByChequeType(id).subscribe((res: any) => { if (id == 1) { this._issueChequeStatusList = res; } if (id == 2) { this._recieveChequeStatusList = res; } }, (err) => { console.log(err); }); } BankSelectionChanged(event) { console.log(event); this._chequeService.getAlreadyUsedChequeNumbersForCheque(event.Id).subscribe((res: any[]) => { if (res.length == 1) { this._issuechequeTransaction.ChequeNo = res[0]; } else { let lastItem = (res[res.length - 1]); this._issuechequeTransaction.ChequeNo = ++lastItem; } this._alreadyUsedItems = res; console.log(res); }, (err) => { }); this._chequeService.getBankChequeById(event.Id).subscribe((res: any) => { console.log(res); this._startingChequeNumber = res.StartingChequeNo; }, (err) => { }); } RecievedBankSelectionChanged(event) { } validateChequeNumber() { // console.log(this._issuechequeTransaction.ChequeNo); // console.log(this._alreadyUsedItems); let cNo = this._alreadyUsedItems.find(x => x == `${this._issuechequeTransaction.ChequeNo}`); this._isChequeValid = (cNo == undefined) ? true : false; } public close(status) { this.openDilog = false; } public getFile(event, id) { this._fileToUpload = event.target.files[0]; console.log(event.target.files); if (id == 1) { this._issuechequeTransaction.Attachement = event.target.files[0]; } if (id == 2) { this._recieveChequeTransaction.Attachement = event.target.files[0]; } } public getAccount(x: string, event: string) { this._storeService.getAccountBySearch(x, event).subscribe((res: any) => { if (res) { if (event === 'CSE') { this.paymentAccountList = res; } } }, err => { this._notificationService.notify('danger', err.msg); }); } public handleSearchEvents() { this.searchSubcription = this.searchEvent.pipe( debounceTime(400)).subscribe((x: any) => { // console.log(x); if (x) { if (x.filter == 'Fk_PaymentAccount') { this.getAccount(x.event, 'CSE'); } if (x.filter == 'Fk_BankAccount') { this.getBankListForIssueCheque(); } if (x.filter == 'Issed_Fk_Status') { // issued this.getChequeStatusesByChequeType(1); } if (x.filter == 'Recieved_Fk_Status') { // Recieved this.getChequeStatusesByChequeType(2); } } }); } }