import { Component, OnInit, ViewChild } from '@angular/core'; import { StoreService } from '../../store/store.services'; import { NotificationService } from '../../../_services/notification.service'; import { ActivatedRoute } from '@angular/router'; import { AccountService } from '../accounts-services'; import { JournalVocherMastViewModel, JournalVoucherDetailViewModel } from '../accounts.class'; import { Subject, Subscription } from 'rxjs'; import { LocalService } from '../../../_services/local.service'; import { debounceTime } from 'rxjs/operators'; import { NgForm } from '@angular/forms'; import { FormCanDeactivate } from '../../../_guards/form-can-deactivate'; @Component({ selector: 'app-journal-voucher', templateUrl: './journal-voucher.component.html', }) export class JournalVoucherComponent extends FormCanDeactivate implements OnInit { @ViewChild('f') form: NgForm; public mast = new JournalVocherMastViewModel(); public dtl = new JournalVoucherDetailViewModel(); public saving = false; public nowDate = new Date(); public searchEvent = new Subject(); public searchSubcription: Subscription = null; public accountList = []; public JVCodeValid = false; public TD = 0; public TC = 0; public diff = 0; public isItemEdit = 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.getAccount(" ", "A"); this.getJVDetail(accId); } else { this.validateJVCode("-1"); } }); this.handleSearchEvents(); this.nowDate = new Date(); } public getJVDetail(e) { this._accountService.getJVById(e).subscribe((res: any) => { if (res) { console.log(res.acc); this.mast = res.acc; this.mast.CreationDate = new Date(res.acc.CreationDate); this.mast.Remarks = res.acc.Remarks; this.JVCodeValid = true; this.mast.detail = res.acc.detail; this.calculatTDTC(); } }, err => { }); } public calculatTDTC() { debugger; this.TD = 0; this.TC = 0; this.mast.detail.forEach(element => { if (element.Type == 'C') { this.TC += this.getParsedNumber(element.Amount); } if (element.Type == 'D') { this.TD += this.getParsedNumber(element.Amount); } }); this.getTransDiff(); console.log(this.diff); } public RemoveCatlog(item, i) { if (item.Type == 'C') { this.TC -= this.getParsedNumber(item.Amount); } if (item.Type == 'D') { this.TD -= this.getParsedNumber(item.Amount); } this.getTransDiff(); this.mast.detail.splice(i, 1); } EditItem(item, i) { if (this.isItemEdit == false) { this.isItemEdit = true; this.dtl = item; if (item.Type == 'C') { this.TC -= this.getParsedNumber(item.Amount); } if (item.Type == 'D') { this.TD -= this.getParsedNumber(item.Amount); } this.getTransDiff(); } } public validateJVCode(x) { debugger; this._accountService.validateJVCode(x).subscribe((res: any) => { if (res.msg == "Valid") { this.JVCodeValid = true; this.mast.Code = res.code; } else { this.JVCodeValid = false; } }, err => { this._notificationService.notify("danger", err); }); } public onSubmit() { this.saving = true; let emp: any = Object.assign({}, this.mast); if (this.mast.CreationDate) emp.CreationDate = this.mast.CreationDate.toDateString(); this._accountService.saveJVList(emp).subscribe((res: any) => { this._notificationService.notify("success", "Save Successfully"); this.mast = new JournalVocherMastViewModel(); this.validateJVCode("-1"); this.saving = false; this.TD = 0; this.TC = 0; }, err => { this._notificationService.notify("danger", err); this.saving = false; }); } public saveBayBookList() { } public ClearList() { } public ClearForm() { } getParsedNumber(num: any) { let rnum = Number(num).toFixed(4); return Number(rnum); } getTransDiff() { this.diff = Math.abs(Math.round(this.getParsedNumber((this.TD - this.TC)))); } public AddInJV() { if (this.mast.detail.length == 0) { if (this.dtl.Type === 'C') { this._notificationService.notify("danger", "First entery should be Debit"); return; } } if (this.isItemEdit == false) { this.mast.detail.push(this.dtl); } else { this.mast.detail.splice(this.mast.detail.indexOf(this.dtl), 1, this.dtl); this.isItemEdit = false; } if (this.dtl.Type == 'C') { this.TC += this.getParsedNumber(this.dtl.Amount); } if (this.dtl.Type == 'D') { this.TD += this.getParsedNumber(this.dtl.Amount); } this.getTransDiff(); this.dtl = new JournalVoucherDetailViewModel(); } public getAccount(x: string, event: string) { this._storeService.getAccountBySearch(x, event).subscribe((res: any) => { if (res) { this.accountList = 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 == "allAccount") { this.getAccount(x.event, "A"); } } }); } private updateMaxTime() { this.nowDate.setHours(this.nowDate.getHours() + 2); } }