import { Component, OnInit, ViewChild } from '@angular/core'; import { EmpSalaryMastModel } from '../emp-salary-class'; import { Subject, Subscription } from 'rxjs'; 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 { debounceTime } from 'rxjs/operators'; 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-flat-salary', templateUrl: './flat-salary.component.html', }) export class FlatSalaryComponent 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 Totalsalary = 0; public Absdeduction = 0; public FineDeduction = 0; public AdjustIncrement = 0; public TipIncrement = 0; public OTIncrement = 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 = 'Flat'; this._MaxDateToday.setHours(this._MaxDateToday.getHours() + 2); this.activatedRoute.queryParams.subscribe(params => { const accId = params['id']; if (accId != null) { this.getAccount(' ', 'Exp'); this.getAccount(' ', 'S'); this.getContrectList(' '); this.getSalaryInvDetail(accId); } else { } }); this.handleSearchEvents(); } 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); } }, err => { }); } public onSubmit() { this.saving = true; this.commision.NetTotal = this.commision.Total; 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(); } if (this.commision.CreationDate) { emp.CreationDate = this.commision.CreationDate.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 = 'Flat'; } public RemoveFromList(i) { this.commision.Total -= this.commision.EmpList[i].STotal; this.commision.EmpList.splice(i, 1); } public FeatchEmpdetail() { 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 => { console.log(err); }); } 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, 'Internal').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; if (x.filter == 'FK_Exp_Acc') { this.getAccount(x.event, 'Exp'); } if (x.filter == 'FK_Revinew_Account') { this.getAccount(x.event, 'S'); } if (x.filter == 'Customer') { this.getContrectList(x.event); } } }); } public ContrectSelected(event) { if (event) { this.commision.DutyHour = this.contrectList.find(x => x.Id == this.commision.FK_Contrect.Id).ShiftHour; } else { this.commision.DutyHour = null; } } public CalculateDays(e) { if (this.commision.FromDate == null) { this._notificationService.notify('danger', 'Select From Date First'); this.commision.ToDate = null; return; } if (this.commision.ToDate == null) { this._notificationService.notify('warning', 'Now Select To Date'); return; } let timeDiff = Math.abs(new Date(this.commision.ToDate).getTime() - new Date(this.commision.FromDate).getTime()); this.commision.NoDays = Math.ceil(timeDiff / (1000 * 3600 * 24)) + 1; e.SWorkDays = this.commision.NoDays - this.commision.OffDays; } // public CalculateOerDAyAndOTHour(c){ // if(c.IsLock){ // this._notificationService.notify('danger', 'Unlock the record first'); // return; // } // debugger; // if(c.SWorkDays){ // c.SPerDay=c.SMothely/this.commision.NoDays; // c.SOTPerHour = (c.SMothely*12)/ ((this.commision.DutyHour*6) *52.14285714); // if(this.Totalsalary==0){ // c.STotal += (c.SWorkDays*c.SPerDay) // this.Totalsalary = c.STotal ; // } // else{ // c.STotal-=this.Totalsalary; // c.STotal += (c.SWorkDays*c.SPerDay); // this.Totalsalary = c.STotal; // } // }else{ // c.STotal-=this.Totalsalary; // this.Totalsalary =0; // } // } // public CalculatAbsentDays(e ,c){ // if(c.IsLock){ // this._notificationService.notify('danger', 'Unlock the record first'); // return; // } // if(c.SAbsentDays){ // if(this.Absdeduction==0){ // c.SPerDayDeduction=c.SMothely/c.SWorkDays ; // c.SAbsDeduction=c.SPerDayDeduction*c.SAbsentDays; // this.Absdeduction = c.SAbsDeduction; // c.STotal-=c.SAbsDeduction; // } // else{ // c.STotal +=this.Absdeduction; // c.STotal-=c.SAbsDeduction; // this.Absdeduction = c.SAbsDeduction; // } // } // else{ // c.SAbsDeduction = 0; // c.STotal +=this.Absdeduction; // this.Absdeduction =0; // } // } // public DeductFine(e , c){ // if(c.IsLock){ // this._notificationService.notify('danger', 'Unlock the record first'); // return; // } // debugger; // if(c.SFines){ // if(this.FineDeduction==0){ // this.FineDeduction = c.SFines; // c.STotal-=c.SFines; // } // else{ // c.STotal+=this.FineDeduction; // c.STotal-=c.SFines; // this.FineDeduction = c.SFines // } // } // else{ // c.STotal +=this.FineDeduction; // this.FineDeduction =0; // } // } // public Adjustement(e , c){ // if(c.IsLock){ // this._notificationService.notify('danger', 'Unlock the record first'); // return; // } // debugger; // if(c.SAdjustement){ // if(this.AdjustIncrement==0){ // this.AdjustIncrement = c.SAdjustement; // c.STotal+=c.SAdjustement; // } // else{ // c.STotal-=this.AdjustIncrement; // c.STotal+=c.SAdjustement; // this.AdjustIncrement = c.SAdjustement // } // } // else{ // c.STotal -=this.AdjustIncrement; // this.AdjustIncrement =0; // } // } // public Tip(e , c){ // if(c.IsLock){ // this._notificationService.notify('danger', 'Unlock the record first'); // return; // } // debugger; // if(c.Stip){ // if(this.TipIncrement==0){ // this.TipIncrement = c.Stip; // c.STotal+=c.Stip; // } // else{ // c.STotal-=this.TipIncrement; // c.STotal+=c.Stip; // this.TipIncrement = c.Stip // } // } // else{ // c.STotal -=this.TipIncrement; // this.TipIncrement =0; // } // } // public OverTime(e , c){ // if(c.IsLock){ // this._notificationService.notify('danger', 'Unlock the record first'); // return; // } // if(c.SOT){ // c.SOTSalery = c.SOT*c.SOTPerHour; // if(this.OTIncrement==0){ // this.OTIncrement = c.SOTSalery; // c.STotal+=c.SOTSalery; // } // else{ // c.STotal-=this.OTIncrement; // c.STotal+=c.SOTSalery; // this.OTIncrement = c.SOTSalery // } // } // else{ // c.STotal -=this.OTIncrement; // this.OTIncrement =0; // } // } public UnlockTheRecord(c) { console.log(c); c.SOTPerHour = (c.SOTPerHour) ? c.SOTPerHour : (c.SMothely * 12) / ((this.commision.DutyHour * 6) * 52.14285714); if (c.IsLock) { this.commision.Total -= c.STotal; c.SWorkDays = this.commision.NoDays - this.commision.OffDays; c.STotal = null; } else { if (c.SWorkDays) { c.SPerDay = c.SMothely / this.commision.NoDays; c.STotal += (c.SWorkDays * c.SPerDay); } if (c.SOT) { c.SOTSalery = c.SOT * c.SOTPerHour; c.STotal += c.SOTSalery; } if (c.Stip) { c.STotal += c.Stip; } if (c.SAdjustement) { c.STotal += c.SAdjustement; } if (c.SFines) { c.STotal -= c.SFines; } if (c.SAbsentDays) { c.SPerDayDeduction = (c.SPerDayDeduction) ? c.SPerDayDeduction : c.SMothely / (c.SWorkDays - this.commision.OffDays); c.SAbsDeduction = c.SPerDayDeduction * c.SAbsentDays; c.STotal -= c.SAbsDeduction; } else { c.SPerDayDeduction = (c.SPerDayDeduction) ? c.SPerDayDeduction : c.SMothely / (c.SWorkDays - this.commision.OffDays); c.SAbsDeduction = 0; } this.commision.Total += c.STotal; } c.IsLock = !c.IsLock; } }