import { Component, OnInit, ViewChild } from '@angular/core'; import { InstalmentVewModel } from '../../store/store-model'; import { Subject, Subscription } from 'rxjs'; import { debounceTime } from 'rxjs/operators'; import { StoreService } from '../../store/store.services'; import { NotificationService } from '../../../_services/notification.service'; import { AccountService } from '../accounts-services'; import { ActivatedRoute } from '@angular/router'; import { LocalService } from '../../../_services/local.service'; import { NgForm } from '@angular/forms'; import { FormCanDeactivate } from '../../../_guards/form-can-deactivate'; @Component({ selector: 'app-day-book', templateUrl: './day-book.component.html', }) export class DayBookComponent extends FormCanDeactivate implements OnInit { @ViewChild('f') form: NgForm; public instalment: InstalmentVewModel; public accountList: any[] = []; public allAccountList: any[] = []; public dayBookList: any[] = []; public maxDate = new Date(); public saving = false; public TotalTo = 0; public TotalFrom = 0; public showSave = false; public searchEvent = new Subject(); public searchSubcription: Subscription = null; public isEdit = false; constructor( public _accountService: AccountService, public _storeService: StoreService, public _notificationService: NotificationService, private activatedRoute: ActivatedRoute, private _localService: LocalService ) { super(); } ngOnInit() { this.activatedRoute.queryParams.subscribe(params => { const accId = params['id']; if (accId != null) { this.getDayBookDetail(accId); this.isEdit = true; } else { this.instalment = new InstalmentVewModel(); this.instalment.SP_Date = new Date(); this.instalment.IsDayBook = true; this.instalment.Type = 'C'; this.isEdit = false; } }); this.maxDate = new Date(); this.maxDate.setHours(this.maxDate.getHours() + 2); this.handleSearchEvents(); } public attachfile(event) { this.instalment.file = event.target.files[0]; } public getDayBookDetail(e) { this._storeService.getDayBookDetail(e).subscribe((res: any) => { if (res) { // debugger; console.log(res) this.instalment = new InstalmentVewModel(); this.instalment = res; this.instalment.SP_Date = new Date(res.SP_Date); this.getAccount(" ", "CaB"); this.getAccount(this.instalment.FK_From_Account.Name, "A"); this.maxDate = new Date(res.SP_Date); this.maxDate.setHours(this.maxDate.getHours() + 2); } }, err => { this._notificationService.notify("danger", err.msg); }); } public onSubmit() { debugger; this.dayBookList.push(this.instalment); console.log(this.dayBookList); if (this.instalment.Type == 'D') { this.TotalFrom += this.instalment.Amount; } else { this.TotalTo += this.instalment.Amount; } this.instalment = new InstalmentVewModel(); this.instalment.SP_Date = new Date(); this.instalment.IsDayBook = true; this.instalment.Type = 'C'; this.showSave = true; this.updateMaxTime(); } RemoveItem(item) { if (item.Type == 'D') { this.TotalFrom -= item.Amount; } else { this.TotalTo -= item.Amount; } this.dayBookList.splice(this.dayBookList.indexOf(item), 1); this.showSave = this.dayBookList.length == 0 ? false : true; } public saveBayBookList() { this.saving = true; let count = this.dayBookList.length; let itemsSaved = 0; this.dayBookList.forEach(element => { let emp: any = Object.assign({}, element); if (element.SP_Date) { emp.SP_Date = element.SP_Date.toDateString(); } this._accountService.saveDayBook(emp).subscribe((res: any) => { itemsSaved = itemsSaved + 1; if (itemsSaved == count) { this.daybooksaveComplete(); } }, err => { this._notificationService.notify("danger", err.error); this.saving = false; }); }); } daybooksaveComplete() { this._notificationService.notify("success", "Save Successfully"); this.showSave = false; this.saving = false; this.updateMaxTime(); } public ClearList() { this.dayBookList = []; this.TotalFrom = 0; this.TotalTo = 0; } public getAccount(x: string, event: string) { this._storeService.getAccountBySearch(x, event).subscribe((res: any) => { if (res) { if (event == "CaB") { this.accountList = res; } else { this.allAccountList = res; } } }, err => { this._notificationService.notify("danger", err.msg); }); } clearDaybookRecordsIfSaved($event) { if (this.showSave == false) { this.ClearList(); } } public handleSearchEvents() { this.searchSubcription = this.searchEvent.pipe( debounceTime(400)).subscribe((x: any) => { console.log(x); if (x) { debugger; if (x.filter == "FK_Account") { this.getAccount(x.event, "CaB"); } if (x.filter == "allAccount") { this.getAccount(x.event, "A"); } } }); } private updateMaxTime() { this.maxDate.setHours(this.maxDate.getHours() + 1); } }