import { Component, OnInit, ViewChild } from '@angular/core'; import { EmpSalaryMastModel } from '../emp-salary-class'; import { Subject, Subscription } from 'rxjs'; import { debounceTime } from 'rxjs/operators'; import { StoreService } from '../../store/store.services'; import { NotificationService } from '../../../_services/notification.service'; import { CoustomerServices } from '../../coustomer/coustome-services'; import { EmployeeSalaryServices } from '../emp-salary-services'; import { ActivatedRoute } from '@angular/router'; import { LocalService } from '../../../_services/local.service'; import { FormCanDeactivate } from '../../../_guards/form-can-deactivate'; import { NgForm } from '@angular/forms'; @Component({ selector: 'app-commesion-salary', templateUrl: './commesion-salary.component.html' }) export class CommesionSalaryComponent extends FormCanDeactivate implements OnInit { @ViewChild('f') form: NgForm; public commision = new EmpSalaryMastModel(); public expAccountList: any[] = []; public revAccountList: any[] = []; public contrectList: any[] = []; public searchEvent = new Subject(); public searchSubcription: Subscription = null; public saving = false; public GTotal = 0; public VatTotal = 0; public Total = 0; public CT = 0; public ET = 0; public _MaxDateToday = new Date(); constructor(public _storeService: StoreService, public _notificationService: NotificationService, private _coustomerServices: CoustomerServices, private _employeeSalaryServices: EmployeeSalaryServices, private activatedRoute: ActivatedRoute, private _localService: LocalService) { super(); } ngOnInit() { this.commision.CreationDate = new Date(); this.commision.Total = 0; this.commision.VatAmount = 0; this.commision.SaleryType = "Commission"; this._MaxDateToday.setHours(this._MaxDateToday.getHours() + 2); this.activatedRoute.queryParams.subscribe(params => { const accId = params['id']; if (accId != null) { this.getAccount(" ", "Exp"); this.getAccount(" ", "Incom"); this.getContrectList(" "); this.getSalaryInvDetail(accId); } else { } }); this.handleSearchEvents(); } resetTotals() { this.GTotal = 0; this.VatTotal = 0; this.Total = 0; this.CT = 0; this.ET = 0; } public getSalaryInvDetail(e) { this._employeeSalaryServices.getSalaryInvDetail(e).subscribe((res: any) => { if (res) { this.commision = res; this.commision.EmpList = res.EmpList; this.commision.CreationDate = new Date(res.CreationDate); this.commision.ToDate = new Date(res.ToDate); this.commision.FromDate = new Date(res.FromDate); this.commision.EmpList.forEach(c => { this.CT += c.CSahre; this.ET += c.Empshare; this.GTotal += c.Gross; this.VatTotal += c.Vat; this.Total += c.Total; }); } }, err => { }); } public onSubmit() { if (this.commision.EmpList.find(x => x.IsLock == false) != null) { this._notificationService.notify("danger", "Lock the all record first"); return; } this.saving = true; let emp: any = Object.assign({}, this.commision); if (this.commision.ToDate) emp.ToDate = this.commision.ToDate.toDateString(); if (this.commision.FromDate) emp.FromDate = this.commision.FromDate.toDateString(); this._employeeSalaryServices.saveSalary(emp).subscribe((res: any) => { this._notificationService.notify("success", "Save Successfully"); this.ClearForm(); this.saving = false; this._MaxDateToday.setHours(this._MaxDateToday.getHours() + 2); }, err => { this._notificationService.notify("danger", err.error); this.saving = false; }); } public ClearForm() { this.commision = new EmpSalaryMastModel(); this.commision.CreationDate = new Date(); this.commision.Total = 0; this.commision.VatAmount = 0; this.commision.SaleryType = "Commission" } public FeatchEmpdetail() { debugger; if (this.commision.ToDate == null) { this._notificationService.notify("danger", "Select End Date First"); return; } if (this.commision.FromDate == null) { this._notificationService.notify("danger", "Select Start Date First"); return; } let td = this.commision.ToDate.toLocaleDateString(); let fd = this.commision.FromDate.toLocaleDateString(); this._employeeSalaryServices.getEmployeefroCommissionSalary(this.commision.FK_Contrect.Id, td, fd).subscribe((res: any) => { if (res) { if (this.commision.EmpList !== undefined && this.commision.EmpList.length > 0) { res.forEach(rec => { let exists = this.commision.EmpList.find(x => x.FK_Emp.Name === rec.FK_Emp.Name); if (exists == undefined) { this.commision.EmpList.push(rec); } }); } else { this.commision.EmpList = res; } } }, err => { }); } public RemoveFromList(i) { var c = this.commision.EmpList[i]; this.commision.VatAmount -= c.Vat; this.commision.Total -= c.Total; this.commision.NetTotal -= (c.Total + c.Vat); this.CT -= c.CSahre; this.ET -= c.Empshare; this.GTotal -= c.Gross; this.VatTotal -= c.Vat; this.Total -= c.Total; c.Total = 0; this.commision.EmpList.splice(i, 1); } public CalculateVat(c) { c.Vat = (c.Gross * this.commision.VatPer) / (100 + this.commision.VatPer); c.Total = c.Gross - c.Vat; this.commision.Total += c.Total; this.commision.VatAmount += c.Vat; this.commision.NetTotal = this.commision.Total + this.commision.VatAmount; } public CalculateEmpShare(c) { c.Empshare = c.Total - c.CSahre; } public getAccount(x: string, event: string) { debugger; this._storeService.getAccountBySearch(x, event).subscribe((res: any) => { if (res) { if (event == 'Exp') this.expAccountList = res else this.revAccountList = res } }, err => { this._notificationService.notify("danger", err.msg); }); } public getContrectList(x: string) { this._coustomerServices.getContrectListDDL(x, 'Customer').subscribe((res: any) => { if (res) { debugger; this.contrectList = 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) { debugger; this._MaxDateToday = new Date(); if (x.filter == "FK_Exp_Acc") { this.getAccount(x.event, "Exp"); } if (x.filter == "FK_Revinew_Account") { this.getAccount(x.event, "Incom"); } if (x.filter == "Customer") { this.getContrectList(x.event); } } }); } public UnlockTheRecord(c) { console.log(c); if (c.IsLock) { this.commision.VatAmount -= c.Vat; this.commision.Total -= c.Total; this.commision.NetTotal -= (c.Total + c.Vat); this.CT -= c.CSahre; this.ET -= c.Empshare; this.GTotal -= c.Gross; this.VatTotal -= c.Vat; this.Total -= c.Total; c.Total = 0; } else { if (c.Vat == null) c.Vat = (c.Gross * this.commision.VatPer) / (100 + this.commision.VatPer); c.Total = c.Gross - c.Vat; this.commision.Total += c.Total; this.commision.VatAmount += c.Vat; this.commision.NetTotal = this.commision.Total + this.commision.VatAmount; c.Empshare = c.Total - c.CSahre; this.CT += c.CSahre; this.ET += c.Empshare; this.GTotal += c.Gross; this.VatTotal += c.Vat; this.Total += c.Total; } c.IsLock = !c.IsLock; } }